qcam: Add JPEG format support

When the camera provides MJPEG, use the QImage JPEG decompression code
to convert that to RGB.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Tested-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
Laurent Pinchart 2019-03-29 17:20:07 +02:00
parent 29f323eea8
commit 80e236e19b
5 changed files with 27 additions and 6 deletions

View file

@ -9,6 +9,8 @@
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include <QImage>
#include "format_converter.h" #include "format_converter.h"
#define RGBSHIFT 8 #define RGBSHIFT 8
@ -45,16 +47,28 @@ int FormatConverter::configure(unsigned int format, unsigned int width,
y_pos_ = 0; y_pos_ = 0;
cb_pos_ = 1; cb_pos_ = 1;
break; break;
case V4L2_PIX_FMT_MJPEG:
break;
default: default:
return -EINVAL; return -EINVAL;
}; };
format_ = format;
width_ = width; width_ = width;
height_ = height; height_ = height;
return 0; return 0;
} }
void FormatConverter::convert(const unsigned char *src, size_t size,
QImage *dst)
{
if (format_ == V4L2_PIX_FMT_MJPEG)
dst->loadFromData(src, size, "JPEG");
else
convertYUV(src, dst->bits());
}
static void yuv_to_rgb(int y, int u, int v, int *r, int *g, int *b) static void yuv_to_rgb(int y, int u, int v, int *r, int *g, int *b)
{ {
int c = y - 16; int c = y - 16;
@ -65,7 +79,7 @@ static void yuv_to_rgb(int y, int u, int v, int *r, int *g, int *b)
*b = CLIP(( 298 * c + 516 * d + 128) >> RGBSHIFT); *b = CLIP(( 298 * c + 516 * d + 128) >> RGBSHIFT);
} }
void FormatConverter::convert(const unsigned char *src, unsigned char *dst) void FormatConverter::convertYUV(const unsigned char *src, unsigned char *dst)
{ {
unsigned int src_x, src_y, dst_x, dst_y; unsigned int src_x, src_y, dst_x, dst_y;
unsigned int src_stride; unsigned int src_stride;

View file

@ -7,15 +7,22 @@
#ifndef __QCAM_FORMAT_CONVERTER_H__ #ifndef __QCAM_FORMAT_CONVERTER_H__
#define __QCAM_FORMAT_CONVERTER_H__ #define __QCAM_FORMAT_CONVERTER_H__
#include <stddef.h>
class QImage;
class FormatConverter class FormatConverter
{ {
public: public:
int configure(unsigned int format, unsigned int width, int configure(unsigned int format, unsigned int width,
unsigned int height); unsigned int height);
void convert(const unsigned char *src, unsigned char *dst); void convert(const unsigned char *src, size_t size, QImage *dst);
private: private:
void convertYUV(const unsigned char *src, unsigned char *dst);
unsigned int format_;
unsigned int width_; unsigned int width_;
unsigned int height_; unsigned int height_;
unsigned int y_pos_; unsigned int y_pos_;

View file

@ -222,7 +222,7 @@ int MainWindow::display(Buffer *buffer)
Plane &plane = buffer->planes().front(); Plane &plane = buffer->planes().front();
unsigned char *raw = static_cast<unsigned char *>(plane.mem()); unsigned char *raw = static_cast<unsigned char *>(plane.mem());
viewfinder_->display(raw); viewfinder_->display(raw, buffer->bytesused());
return 0; return 0;
} }

View file

@ -16,9 +16,9 @@ ViewFinder::ViewFinder(QWidget *parent)
{ {
} }
void ViewFinder::display(const unsigned char *raw) void ViewFinder::display(const unsigned char *raw, size_t size)
{ {
converter_.convert(raw, image_->bits()); converter_.convert(raw, size, image_);
QPixmap pixmap = QPixmap::fromImage(*image_); QPixmap pixmap = QPixmap::fromImage(*image_);
setPixmap(pixmap); setPixmap(pixmap);

View file

@ -20,7 +20,7 @@ public:
int setFormat(unsigned int format, unsigned int width, int setFormat(unsigned int format, unsigned int width,
unsigned int height); unsigned int height);
void display(const unsigned char *rgb); void display(const unsigned char *rgb, size_t size);
private: private:
unsigned int format_; unsigned int format_;