Rewrite jpegutils.c

The previous version of jpegutils.c was performing the downsampling of images manually
This commit rewrites the decompression to take advantage of the built in downsampling
and image color space conversion that is part of the JPEG libaries.

This function is currently only called from the vid_mjpegtoyuv420p routine.
This commit is contained in:
MrDave
2017-08-05 17:50:50 -06:00
parent ab9e800d59
commit eaaf8b47d9
3 changed files with 166 additions and 1164 deletions

View File

File diff suppressed because it is too large Load Diff

View File

@@ -11,50 +11,8 @@
#ifndef __JPEGUTILS_H__
#define __JPEGUTILS_H__
/*
* jpeg_data: buffer with input / output jpeg
* len: Length of jpeg buffer
* itype: Y4M_ILACE_NONE: Not interlaced
* Y4M_ILACE_TOP_FIRST: Interlaced, top-field-first
* Y4M_ILACE_BOTTOM_FIRST: Interlaced, bottom-field-first
* ctype Chroma format for decompression.
* Currently always 420 and hence ignored.
* raw0 buffer with input / output raw Y channel
* raw1 buffer with input / output raw U/Cb channel
* raw2 buffer with input / output raw V/Cr channel
* width width of Y channel (width of U/V is width/2)
* height height of Y channel (height of U/V is height/2)
*/
int jpgutl_decode_jpeg (unsigned char *jpeg_data, int len,
unsigned int width_in, unsigned int height_in, unsigned char *img);
#define Y4M_ILACE_NONE 0 /* non-interlaced, progressive frame */
#define Y4M_ILACE_TOP_FIRST 1 /* interlaced, top-field first */
#define Y4M_ILACE_BOTTOM_FIRST 2 /* interlaced, bottom-field first */
#define Y4M_ILACE_MIXED 3 /* mixed, "refer to frame header" */
#define Y4M_CHROMA_420JPEG 0 /* 4:2:0, H/V centered, for JPEG/MPEG-1 */
#define Y4M_CHROMA_420MPEG2 1 /* 4:2:0, H cosited, for MPEG-2 */
#define Y4M_CHROMA_420PALDV 2 /* 4:2:0, alternating Cb/Cr, for PAL-DV */
#define Y4M_CHROMA_444 3 /* 4:4:4, no subsampling, phew. */
#define Y4M_CHROMA_422 4 /* 4:2:2, H cosited */
#define Y4M_CHROMA_411 5 /* 4:1:1, H cosited */
#define Y4M_CHROMA_MONO 6 /* luma plane only */
#define Y4M_CHROMA_444ALPHA 7 /* 4:4:4 with an alpha channel */
int decode_jpeg_raw(unsigned char *jpeg_data, int len,
int itype, int ctype, unsigned int width,
unsigned int height, unsigned char *raw0,
unsigned char *raw1, unsigned char *raw2);
int decode_jpeg_gray_raw(unsigned char *jpeg_data, int len,
int itype, int ctype, unsigned int width,
unsigned int height, unsigned char *raw0,
unsigned char *raw1, unsigned char *raw2);
int encode_jpeg_raw(unsigned char *jpeg_data, int len, int quality,
int itype, int ctype, unsigned int width,
unsigned int height, unsigned char *raw0,
unsigned char *raw1, unsigned char *raw2);
#endif

View File

@@ -402,35 +402,31 @@ int vid_mjpegtoyuv420p(unsigned char *map, unsigned char *cap_map, int width, in
int ret = 0;
ptr_buffer = memmem(cap_map, size, "\xff\xd8", 2);
if (ptr_buffer != NULL) {
/**
Some cameras are sending multiple SOIs in the buffer.
Move the pointer to the last SOI in the buffer and proceed.
*/
while (ptr_buffer != NULL && ((size - soi_pos - 1) > 2) ){
soi_pos = ptr_buffer - cap_map;
ptr_buffer = memmem(cap_map + soi_pos + 1, size - soi_pos - 1, "\xff\xd8", 2);
}
if (soi_pos != 0){
MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "SOI position adjusted by %d bytes.", soi_pos);
}
memmove(cap_map, cap_map + soi_pos, size - soi_pos);
size -= soi_pos;
ret = decode_jpeg_raw(cap_map, size, 0, 420, width, height,
map,
map + (width * height),
map + (width * height) + (width * height) / 4);
} else {
//Buffer does not have a SOI
ret = 1;
if (ptr_buffer == NULL) {
MOTION_LOG(CRT, TYPE_VIDEO, NO_ERRNO, "Corrupt image ... continue");
return 1;
}
/**
Some cameras are sending multiple SOIs in the buffer.
Move the pointer to the last SOI in the buffer and proceed.
*/
while (ptr_buffer != NULL && ((size - soi_pos - 1) > 2) ){
soi_pos = ptr_buffer - cap_map;
ptr_buffer = memmem(cap_map + soi_pos + 1, size - soi_pos - 1, "\xff\xd8", 2);
}
if (ret == 1) {
if (soi_pos != 0){
MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "SOI position adjusted by %d bytes.", soi_pos);
}
memmove(cap_map, cap_map + soi_pos, size - soi_pos);
size -= soi_pos;
ret = jpgutl_decode_jpeg(cap_map,size, width, height, map);
if (ret == -1) {
MOTION_LOG(CRT, TYPE_VIDEO, NO_ERRNO, "Corrupt image ... continue");
ret = 2;
ret = 1;
}
return ret;
}