53 static void jpeg_buffer_src(j_decompress_ptr cinfo,
unsigned char *
buffer,
long num);
54 static void jpeg_buffer_dest(j_compress_ptr cinfo,
unsigned char *
buffer,
long len);
55 static void jpeg_skip_ff(j_decompress_ptr cinfo);
70 static void init_source(j_decompress_ptr cinfo)
85 static uint8_t EOI_data[2] = { 0xFF, 0xD9 };
87 static boolean fill_input_buffer(j_decompress_ptr cinfo)
89 cinfo->src->next_input_byte = EOI_data;
90 cinfo->src->bytes_in_buffer = 2;
100 static void skip_input_data(j_decompress_ptr cinfo,
long num_bytes)
104 if (num_bytes > (
long)cinfo->src->bytes_in_buffer)
105 num_bytes = (long)cinfo->src->bytes_in_buffer;
106 cinfo->src->next_input_byte += (
size_t)num_bytes;
107 cinfo->src->bytes_in_buffer -= (size_t)num_bytes;
116 static void term_source(j_decompress_ptr cinfo)
126 static void jpeg_buffer_src(j_decompress_ptr cinfo,
unsigned char *
buffer,
long num)
135 if (cinfo->src == NULL)
137 cinfo->src = (
struct jpeg_source_mgr *)(*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
138 sizeof(
struct jpeg_source_mgr));
141 cinfo->src->init_source = init_source;
142 cinfo->src->fill_input_buffer = fill_input_buffer;
143 cinfo->src->skip_input_data = skip_input_data;
144 cinfo->src->resync_to_restart = jpeg_resync_to_restart;
145 cinfo->src->term_source = term_source;
146 cinfo->src->bytes_in_buffer = num;
147 cinfo->src->next_input_byte = (JOCTET *)
buffer;
156 static void jpeg_skip_ff(j_decompress_ptr cinfo)
158 while (cinfo->src->bytes_in_buffer > 1 && cinfo->src->next_input_byte[0] == 0xff &&
159 cinfo->src->next_input_byte[1] == 0xff)
161 cinfo->src->bytes_in_buffer--;
162 cinfo->src->next_input_byte++;
179 static void init_destination(j_compress_ptr cinfo)
193 static boolean empty_output_buffer(j_compress_ptr cinfo)
208 static void term_destination(j_compress_ptr cinfo)
220 static void jpeg_buffer_dest(j_compress_ptr cinfo,
unsigned char *buf,
long len)
228 if (cinfo->dest == NULL)
230 cinfo->dest = (
struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
231 sizeof(
struct jpeg_destination_mgr));
234 cinfo->dest->init_destination = init_destination;
235 cinfo->dest->empty_output_buffer = empty_output_buffer;
236 cinfo->dest->term_destination = term_destination;
237 cinfo->dest->free_in_buffer = len;
238 cinfo->dest->next_output_byte = (JOCTET *)buf;
257 struct jpeg_error_mgr
pub;
261 JMETHOD(
void, original_emit_message, (j_common_ptr cinfo,
int msg_level));
266 static void my_error_exit(j_common_ptr cinfo)
273 (*cinfo->err->output_message)(cinfo);
279 static void my_emit_message(j_common_ptr cinfo,
int msg_level)
291 #define MAX_LUMA_WIDTH 4096
292 #define MAX_CHROMA_WIDTH 2048
302 static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr,
const UINT8 *bits,
const UINT8 *val)
307 if (*htblptr == NULL)
308 *htblptr = jpeg_alloc_huff_table((j_common_ptr)dinfo);
311 memcpy((*htblptr)->bits, bits,
sizeof((*htblptr)->bits));
319 for (len = 1; len <= 16; len++)
320 nsymbols += bits[len];
322 if (nsymbols < 1 || nsymbols > 256)
323 fprintf(stderr,
"%s: Given jpeg buffer was too small", __FUNCTION__);
325 memcpy((*htblptr)->huffval, val, nsymbols *
sizeof(UINT8));
328 static void std_huff_tables(j_decompress_ptr dinfo)
332 static const UINT8 bits_dc_luminance[17] = { 0,
349 static const UINT8 val_dc_luminance[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
351 static const UINT8 bits_dc_chrominance[17] = { 0,
368 static const UINT8 val_dc_chrominance[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
370 static const UINT8 bits_ac_luminance[17] = { 0,
387 static const UINT8 val_ac_luminance[] = {
388 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,
389 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
390 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37,
391 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
392 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83,
393 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3,
394 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
395 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
396 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
399 static const UINT8 bits_ac_chrominance[17] = { 0,
416 static const UINT8 val_ac_chrominance[] = {
417 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22,
418 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
419 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36,
420 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
421 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
422 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a,
423 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
424 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
425 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
428 add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[0], bits_dc_luminance, val_dc_luminance);
429 add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[0], bits_ac_luminance, val_ac_luminance);
430 add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[1], bits_dc_chrominance, val_dc_chrominance);
431 add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[1], bits_ac_chrominance, val_ac_chrominance);
434 static void guarantee_huff_tables(j_decompress_ptr dinfo)
436 if ((dinfo->dc_huff_tbl_ptrs[0] == NULL) && (dinfo->dc_huff_tbl_ptrs[1] == NULL) &&
437 (dinfo->ac_huff_tbl_ptrs[0] == NULL) && (dinfo->ac_huff_tbl_ptrs[1] == NULL))
439 std_huff_tables(dinfo);
461 int decode_jpeg_raw(
unsigned char *jpeg_data,
int len,
int itype,
int ctype,
unsigned int width,
unsigned int height,
462 unsigned char *raw0,
unsigned char *raw1,
unsigned char *raw2)
464 int numfields, hsf[3], field;
466 int i, xsl, xsc, xs, hdown;
467 unsigned int x, y = 0, vsf[3], xd;
469 JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3], buf0[4], buf0[5], buf0[6], buf0[7],
470 buf0[8], buf0[9], buf0[10], buf0[11], buf0[12], buf0[13], buf0[14], buf0[15] };
472 JSAMPROW row1[8] = { buf1[0], buf1[1], buf1[2], buf1[3], buf1[4], buf1[5], buf1[6], buf1[7] };
474 JSAMPROW row2[16] = { buf2[0], buf2[1], buf2[2], buf2[3], buf2[4], buf2[5], buf2[6], buf2[7] };
476 JSAMPROW row1_444[16], row2_444[16];
478 JSAMPARRAY scanarray[3] = { row0, row1, row2 };
480 struct jpeg_decompress_struct dinfo;
484 dinfo.err = jpeg_std_error(&jerr.
pub);
485 jerr.
pub.error_exit = my_error_exit;
487 jerr.original_emit_message = jerr.
pub.emit_message;
488 jerr.
pub.emit_message = my_emit_message;
495 jpeg_destroy_decompress(&dinfo);
499 jpeg_create_decompress(&dinfo);
501 jpeg_buffer_src(&dinfo, jpeg_data, len);
506 jpeg_read_header(&dinfo,
TRUE);
507 dinfo.raw_data_out =
TRUE;
508 dinfo.out_color_space = JCS_YCbCr;
509 dinfo.dct_method = JDCT_IFAST;
510 guarantee_huff_tables(&dinfo);
511 jpeg_start_decompress(&dinfo);
513 if (dinfo.output_components != 3)
515 fprintf(stderr,
"%s: Output components of JPEG image = %d, must be 3", __FUNCTION__, dinfo.output_components);
519 for (i = 0; i < 3; i++)
521 hsf[i] = dinfo.comp_info[i].h_samp_factor;
522 vsf[i] = dinfo.comp_info[i].v_samp_factor;
525 if ((hsf[0] != 2 && hsf[0] != 1) || hsf[1] != 1 || hsf[2] != 1 || (vsf[0] != 1 && vsf[0] != 2) || vsf[1] != 1 ||
529 "%s: Unsupported sampling factors, hsf=(%d, %d, %d) "
530 "vsf=(%d, %d, %d) !",
531 __FUNCTION__, hsf[0], hsf[1], hsf[2], vsf[0], vsf[1], vsf[2]);
540 "%s: YUV 4:4:4 sampling, but image height %d "
541 "not dividable by 8 !",
542 __FUNCTION__, height);
546 for (y = 0; y < 16; y++)
548 row1_444[y] = (
unsigned char *)malloc(dinfo.output_width *
sizeof(
unsigned char));
549 row2_444[y] = (
unsigned char *)malloc(dinfo.output_width *
sizeof(
unsigned char));
551 scanarray[1] = row1_444;
552 scanarray[2] = row2_444;
557 if (dinfo.output_height == height)
561 else if (2 * dinfo.output_height == height)
568 "%s: Read JPEG: requested height = %d, "
569 "height of image = %d",
570 __FUNCTION__, height, dinfo.output_height);
578 fprintf(stderr,
"%s: Image width of %d exceeds max", __FUNCTION__, dinfo.output_width);
582 if (width < 2 * dinfo.output_width / 3)
586 if (2 * width < dinfo.output_width)
587 xsl = (dinfo.output_width - 2 * width) / 2;
591 else if (width == 2 * dinfo.output_width / 3)
601 if (width < dinfo.output_width)
602 xsl = (dinfo.output_width - width) / 2;
614 for (field = 0; field < numfields; field++)
618 jpeg_read_header(&dinfo,
TRUE);
619 dinfo.raw_data_out =
TRUE;
620 dinfo.out_color_space = JCS_YCbCr;
621 dinfo.dct_method = JDCT_IFAST;
622 jpeg_start_decompress(&dinfo);
633 yl = yc = (1 - field);
636 fprintf(stderr,
"%s: Input is interlaced but no interlacing set", __FUNCTION__);
645 while (dinfo.output_scanline < dinfo.output_height)
648 jpeg_read_raw_data(&dinfo, scanarray, 8 * vsf[0]);
650 for (y = 0; y < 8 * vsf[0] && yl < height; yl += numfields, y++)
657 for (x = 0; x < width; x++)
658 raw0[xd++] = row0[y][xs++];
662 for (x = 0; x < width; x++, xs += 2)
663 raw0[xd++] = (row0[y][xs] + row0[y][xs + 1]) >> 1;
667 for (x = 0; x < width / 2; x++, xd += 2, xs += 3)
669 raw0[xd] = (2 * row0[y][xs] + row0[y][xs + 1]) / 3;
670 raw0[xd + 1] = (2 * row0[y][xs + 2] + row0[y][xs + 1]) / 3;
677 for (y = 0; y < 8; y++)
682 for (x = 0; x < width / 2; x++, xs++)
684 row1[y][xs] = (row1_444[y][2 * x] + row1_444[y][2 * x + 1]) >> 1;
685 row2[y][xs] = (row2_444[y][2 * x] + row2_444[y][2 * x + 1]) >> 1;
691 for (x = 0; x < width / 2; x++, xs++)
693 chr1[y][x] = row1[y][xs];
694 chr2[y][x] = row2[y][xs];
699 for (x = 0; x < width / 2; x++, xs += 2)
701 chr1[y][x] = (row1[y][xs] + row1[y][xs + 1]) >> 1;
702 chr2[y][x] = (row2[y][xs] + row2[y][xs + 1]) >> 1;
707 for (x = 0; x < width / 2; x += 2, xs += 3)
709 chr1[y][x] = (2 * row1[y][xs] + row1[y][xs + 1]) / 3;
710 chr1[y][x + 1] = (2 * row1[y][xs + 2] + row1[y][xs + 1]) / 3;
711 chr2[y][x] = (2 * row2[y][xs] + row2[y][xs + 1]) / 3;
712 chr2[y][x + 1] = (2 * row2[y][xs + 2] + row2[y][xs + 1]) / 3;
725 for (y = 0; y < 8 && yc < height; y++, yc += numfields)
729 for (x = 0; x < width / 2; x++, xd++)
731 raw1[xd] = chr1[y][x];
732 raw2[xd] = chr2[y][x];
739 for (y = 0; y < 8 && yc < height; y++)
743 for (x = 0; x < width / 2; x++, xd++)
745 raw1[xd] = chr1[y][x];
746 raw2[xd] = chr2[y][x];
752 for (x = 0; x < width / 2; x++, xd++)
754 raw1[xd] = chr1[y][x];
755 raw2[xd] = chr2[y][x];
771 for (y = 0; y < 8 && yc < height/2; y += 2, yc += numfields)
775 for (x = 0; x < width / 2; x++, xd++)
777 assert(xd < (width * height / 4));
778 raw1[xd] = (chr1[y][x] + chr1[y + 1][x]) >> 1;
779 raw2[xd] = (chr2[y][x] + chr2[y + 1][x]) >> 1;
786 for (y = 0; y < 8 && yc < height/2; y++, yc += numfields)
790 for (x = 0; x < width / 2; x++, xd++)
792 raw1[xd] = chr1[y][x];
793 raw2[xd] = chr2[y][x];
801 (void)jpeg_finish_decompress(&dinfo);
802 if (field == 0 && numfields > 1)
803 jpeg_skip_ff(&dinfo);
808 for (y = 0; y < 16; y++)
815 jpeg_destroy_decompress(&dinfo);
823 jpeg_destroy_decompress(&dinfo);
838 unsigned int height,
unsigned char *raw0,
unsigned char *raw1,
unsigned char *raw2)
840 int numfields, field, yl, yc, xsl, xsc, xs, xd, hdown;
841 unsigned int x, y, vsf[3];
843 JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3], buf0[4], buf0[5], buf0[6], buf0[7],
844 buf0[8], buf0[9], buf0[10], buf0[11], buf0[12], buf0[13], buf0[14], buf0[15] };
846 JSAMPARRAY scanarray[3] = { row0 };
847 struct jpeg_decompress_struct dinfo;
851 dinfo.err = jpeg_std_error(&jerr.
pub);
852 jerr.
pub.error_exit = my_error_exit;
858 jpeg_destroy_decompress(&dinfo);
862 jpeg_create_decompress(&dinfo);
864 jpeg_buffer_src(&dinfo, jpeg_data, len);
869 jpeg_read_header(&dinfo,
TRUE);
870 dinfo.raw_data_out =
TRUE;
871 dinfo.out_color_space = JCS_GRAYSCALE;
872 dinfo.dct_method = JDCT_IFAST;
874 if (dinfo.jpeg_color_space != JCS_GRAYSCALE)
876 fprintf(stderr,
"%s: Expected grayscale colorspace for JPEG raw decoding", __FUNCTION__);
880 guarantee_huff_tables(&dinfo);
881 jpeg_start_decompress(&dinfo);
892 if (dinfo.output_height == height)
896 else if (2 * dinfo.output_height == height)
903 "%s: Read JPEG: requested height = %d, "
904 "height of image = %d",
905 __FUNCTION__, height, dinfo.output_height);
913 fprintf(stderr,
"%s: Image width of %d exceeds max", __FUNCTION__, dinfo.output_width);
917 if (width < 2 * dinfo.output_width / 3)
921 if (2 * width < dinfo.output_width)
922 xsl = (dinfo.output_width - 2 * width) / 2;
926 else if (width == 2 * dinfo.output_width / 3)
936 if (width < dinfo.output_width)
937 xsl = (dinfo.output_width - width) / 2;
949 for (field = 0; field < numfields; field++)
953 jpeg_read_header(&dinfo,
TRUE);
954 dinfo.raw_data_out =
TRUE;
955 dinfo.out_color_space = JCS_GRAYSCALE;
956 dinfo.dct_method = JDCT_IFAST;
957 jpeg_start_decompress(&dinfo);
968 yl = yc = (1 - field);
971 fprintf(stderr,
"%s: Input is interlaced but no interlacing set", __FUNCTION__);
980 while (dinfo.output_scanline < dinfo.output_height)
982 jpeg_read_raw_data(&dinfo, scanarray, 16);
984 for (y = 0; y < 8 * vsf[0]; yl += numfields, y++)
991 for (x = 0; x < width; x++)
992 raw0[xd++] = row0[y][xs++];
996 for (x = 0; x < width; x++, xs += 2)
997 raw0[xd++] = (row0[y][xs] + row0[y][xs + 1]) >> 1;
1001 for (x = 0; x < width / 2; x++, xd += 2, xs += 3)
1003 raw0[xd] = (2 * row0[y][xs] + row0[y][xs + 1]) / 3;
1004 raw0[xd + 1] = (2 * row0[y][xs + 2] + row0[y][xs + 1]) / 3;
1009 for (y = 0; y < 8; y++)
1015 for (x = 0; x < width / 2; x++, xs++)
1021 else if (hdown == 1)
1023 for (x = 0; x < width / 2; x++, xs += 2)
1031 for (x = 0; x < width / 2; x += 2, xs += 3)
1049 for (y = 0; y < 8 ; y++, yc += numfields)
1051 xd = yc * width / 2;
1053 for (x = 0; x < width / 2; x++, xd++)
1063 for (y = 0; y < 8 ; y++)
1065 xd = yc * width / 2;
1067 for (x = 0; x < width / 2; x++, xd++)
1074 xd = yc * width / 2;
1076 for (x = 0; x < width / 2; x++, xd++)
1095 for (y = 0; y < 8; y += 2, yc += numfields)
1097 xd = yc * width / 2;
1099 for (x = 0; x < width / 2; x++, xd++)
1110 for (y = 0; y < 8; y++, yc += numfields)
1112 xd = yc * width / 2;
1114 for (x = 0; x < width / 2; x++, xd++)
1125 (void)jpeg_finish_decompress(&dinfo);
1127 if (field == 0 && numfields > 1)
1128 jpeg_skip_ff(&dinfo);
1131 jpeg_destroy_decompress(&dinfo);
1135 jpeg_destroy_decompress(&dinfo);
1156 int encode_jpeg_raw(
unsigned char *jpeg_data,
int len,
int quality,
int itype,
int ctype,
unsigned int width,
1157 unsigned int height,
unsigned char *raw0,
unsigned char *raw1,
unsigned char *raw2)
1159 int numfields, field, yl, yc, y, i;
1161 JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3], buf0[4], buf0[5], buf0[6], buf0[7],
1162 buf0[8], buf0[9], buf0[10], buf0[11], buf0[12], buf0[13], buf0[14], buf0[15] };
1164 JSAMPROW row1[8] = { buf1[0], buf1[1], buf1[2], buf1[3], buf1[4], buf1[5], buf1[6], buf1[7] };
1166 JSAMPROW row2[8] = { buf2[0], buf2[1], buf2[2], buf2[3], buf2[4], buf2[5], buf2[6], buf2[7] };
1168 JSAMPARRAY scanarray[3] = { row0, row1, row2 };
1170 struct jpeg_compress_struct cinfo;
1174 cinfo.err = jpeg_std_error(&jerr.
pub);
1175 jerr.
pub.error_exit = my_error_exit;
1181 jpeg_destroy_compress(&cinfo);
1185 jpeg_create_compress(&cinfo);
1187 jpeg_buffer_dest(&cinfo, jpeg_data, len);
1191 cinfo.input_components = 3;
1192 jpeg_set_defaults(&cinfo);
1193 jpeg_set_quality(&cinfo, quality,
FALSE);
1195 cinfo.raw_data_in =
TRUE;
1196 cinfo.in_color_space = JCS_YCbCr;
1197 cinfo.dct_method = JDCT_IFAST;
1199 cinfo.input_gamma = 1.0;
1201 cinfo.comp_info[0].h_samp_factor = 2;
1202 cinfo.comp_info[0].v_samp_factor = 1;
1203 cinfo.comp_info[1].h_samp_factor = 1;
1204 cinfo.comp_info[1].v_samp_factor = 1;
1205 cinfo.comp_info[2].h_samp_factor = 1;
1206 cinfo.comp_info[2].v_samp_factor = 1;
1208 if ((width > 4096) || (height > 4096))
1211 "%s: Image dimensions (%dx%d) exceed lavtools' max "
1213 __FUNCTION__, width, height);
1217 if ((width % 16) || (height % 16))
1219 fprintf(stderr,
"%s: Image dimensions (%dx%d) not multiples of 16", __FUNCTION__, width, height);
1223 cinfo.image_width = width;
1236 "%s: Image height (%d) exceeds lavtools max "
1237 "for non-interlaced frames",
1238 __FUNCTION__, height);
1243 cinfo.image_height = height / numfields;
1247 for (field = 0; field < numfields; field++)
1249 jpeg_start_compress(&cinfo,
FALSE);
1253 static const JOCTET marker0[40];
1255 jpeg_write_marker(&cinfo, JPEG_APP0, marker0, 14);
1256 jpeg_write_marker(&cinfo, JPEG_APP0 + 1, marker0, 40);
1264 yl = yc = (1 - field);
1267 fprintf(stderr,
"%s: Input is interlaced but no interlacing set", __FUNCTION__);
1276 while (cinfo.next_scanline < cinfo.image_height)
1278 for (y = 0; y < 8 * cinfo.comp_info[0].v_samp_factor; yl += numfields, y++)
1280 row0[y] = &raw0[yl * width];
1283 for (y = 0; y < 8; y++)
1285 row1[y] = &raw1[yc * width / 2];
1286 row2[y] = &raw2[yc * width / 2];
1292 jpeg_write_raw_data(&cinfo, scanarray, 8 * cinfo.comp_info[0].v_samp_factor);
1295 (void)jpeg_finish_compress(&cinfo);
1299 i = len - cinfo.dest->free_in_buffer;
1301 jpeg_destroy_compress(&cinfo);
1305 jpeg_destroy_compress(&cinfo);
1309 int decode_jpeg_rgb(
unsigned char *inBuffer,
unsigned long inSize, uint8_t **memptr,
size_t *memsize,
int *naxis,
int *w,
int *h)
1312 struct jpeg_decompress_struct cinfo;
1313 struct jpeg_error_mgr jerr;
1315 JSAMPROW row_pointer[1] = { NULL };
1318 cinfo.err = jpeg_std_error(&jerr);
1320 jpeg_create_decompress(&cinfo);
1322 jpeg_mem_src(&cinfo, inBuffer, inSize);
1325 jpeg_read_header(&cinfo, (
boolean)
TRUE);
1328 jpeg_start_decompress(&cinfo);
1330 *memsize = cinfo.output_width * cinfo.output_height * cinfo.num_components;
1331 *memptr = (uint8_t *)realloc(*memptr, *memsize);
1333 uint8_t *destmem = *memptr;
1335 *naxis = cinfo.num_components;
1336 *w = cinfo.output_width;
1337 *h = cinfo.output_height;
1340 row_pointer[0] = (
unsigned char *)malloc(cinfo.output_width * cinfo.num_components);
1343 for (
unsigned int row = 0; row < cinfo.image_height; row++)
1345 unsigned char *ppm8 = row_pointer[0];
1346 jpeg_read_scanlines(&cinfo, row_pointer, 1);
1347 memcpy(destmem, ppm8, cinfo.output_width * cinfo.num_components);
1348 destmem += cinfo.output_width * cinfo.num_components;
1352 jpeg_finish_decompress(&cinfo);
1353 jpeg_destroy_decompress(&cinfo);
1356 free(row_pointer[0]);
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)
encode raw JPEG buffer
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)
decode JPEG buffer
int decode_jpeg_rgb(unsigned char *inBuffer, unsigned long inSize, uint8_t **memptr, size_t *memsize, int *naxis, int *w, int *h)
decode_jpeg_rgb Read jpeg in memory buffer and produce RGB image
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)
decode JPEG raw gray buffer
#define Y4M_ILACE_TOP_FIRST
#define Y4M_ILACE_BOTTOM_FIRST
std::vector< uint8_t > buffer
JMETHOD(void, original_emit_message,(j_common_ptr cinfo, int msg_level))
struct jpeg_error_mgr pub