You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1164 lines
40 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * jpegutils.c: Some Utility programs for dealing with
  3. * JPEG encoded images
  4. *
  5. * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  6. * Copyright (C) 2001 pHilipp Zabel <pzabel@gmx.de>
  7. * Copyright (C) 2008 Angel Carpintero <motiondevelop@gmail.com>
  8. *
  9. * based on jdatasrc.c and jdatadst.c from the Independent
  10. * JPEG Group's software by Thomas G. Lane
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. //#include "config.h"
  28. #endif
  29. //#include "motion.h"
  30. #include "jpegutils.h"
  31. #include <setjmp.h>
  32. #include <jpeglib.h>
  33. #include <jerror.h>
  34. #include <assert.h>
  35. /*
  36. * jpeg_data: buffer with input / output jpeg
  37. * len: Length of jpeg buffer
  38. * itype: 0: Not interlaced
  39. * 1: Interlaced, Top field first
  40. * 2: Interlaced, Bottom field first
  41. * ctype Chroma format for decompression.
  42. * Currently only Y4M_CHROMA_{420JPEG,422} are available
  43. * raw0 buffer with input / output raw Y channel
  44. * raw1 buffer with input / output raw U/Cb channel
  45. * raw2 buffer with input / output raw V/Cr channel
  46. * width width of Y channel (width of U/V is width/2)
  47. * height height of Y channel (height of U/V is height/2)
  48. */
  49. static void jpeg_buffer_src(j_decompress_ptr cinfo, unsigned char *buffer, long num);
  50. static void jpeg_buffer_dest(j_compress_ptr cinfo, unsigned char *buffer, long len);
  51. static void jpeg_skip_ff(j_decompress_ptr cinfo);
  52. /*******************************************************************
  53. * *
  54. * The following routines define a JPEG Source manager which *
  55. * just reads from a given buffer (instead of a file as in *
  56. * the jpeg library) *
  57. * *
  58. *******************************************************************/
  59. /*
  60. * Initialize source --- called by jpeg_read_header
  61. * before any data is actually read.
  62. */
  63. static void init_source(j_decompress_ptr cinfo ){
  64. /* No work necessary here */
  65. }
  66. /*
  67. * Fill the input buffer --- called whenever buffer is emptied.
  68. *
  69. * Should never be called since all data should be allready provided.
  70. * Is nevertheless sometimes called - sets the input buffer to data
  71. * which is the JPEG EOI marker;
  72. *
  73. */
  74. static uint8_t EOI_data[2] = { 0xFF, 0xD9 };
  75. static boolean fill_input_buffer(j_decompress_ptr cinfo){
  76. cinfo->src->next_input_byte = EOI_data;
  77. cinfo->src->bytes_in_buffer = 2;
  78. return TRUE;
  79. }
  80. /*
  81. * Skip data --- used to skip over a potentially large amount of
  82. * uninteresting data (such as an APPn marker).
  83. *
  84. */
  85. static void skip_input_data(j_decompress_ptr cinfo, long num_bytes){
  86. if (num_bytes > 0) {
  87. if (num_bytes > (long) cinfo->src->bytes_in_buffer)
  88. num_bytes = (long) cinfo->src->bytes_in_buffer;
  89. cinfo->src->next_input_byte += (size_t) num_bytes;
  90. cinfo->src->bytes_in_buffer -= (size_t) num_bytes;
  91. }
  92. }
  93. /*
  94. * Terminate source --- called by jpeg_finish_decompress
  95. * after all data has been read. Often a no-op.
  96. */
  97. static void term_source(j_decompress_ptr cinfo ){
  98. /* No work necessary here */
  99. }
  100. /*
  101. * Prepare for input from a data buffer.
  102. */
  103. static void jpeg_buffer_src(j_decompress_ptr cinfo, unsigned char *buffer, long num){
  104. /* The source object and input buffer are made permanent so that a series
  105. * of JPEG images can be read from the same buffer by calling jpeg_buffer_src
  106. * only before the first one. (If we discarded the buffer at the end of
  107. * one image, we'd likely lose the start of the next one.)
  108. * This makes it unsafe to use this manager and a different source
  109. * manager serially with the same JPEG object. Caveat programmer.
  110. */
  111. if (cinfo->src == NULL) { /* First time for this JPEG object? */
  112. cinfo->src = (struct jpeg_source_mgr *)
  113. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  114. sizeof (struct jpeg_source_mgr));
  115. }
  116. cinfo->src->init_source = init_source;
  117. cinfo->src->fill_input_buffer = fill_input_buffer;
  118. cinfo->src->skip_input_data = skip_input_data;
  119. cinfo->src->resync_to_restart = jpeg_resync_to_restart; /* Use default method */
  120. cinfo->src->term_source = term_source;
  121. cinfo->src->bytes_in_buffer = num;
  122. cinfo->src->next_input_byte = (JOCTET *) buffer;
  123. }
  124. /*
  125. * jpeg_skip_ff is not a part of the source manager but it is
  126. * particularly useful when reading several images from the same buffer:
  127. * It should be called to skip padding 0xff bytes beetween images.
  128. */
  129. static void jpeg_skip_ff(j_decompress_ptr cinfo){
  130. while (cinfo->src->bytes_in_buffer > 1
  131. && cinfo->src->next_input_byte[0] == 0xff
  132. && cinfo->src->next_input_byte[1] == 0xff) {
  133. cinfo->src->bytes_in_buffer--;
  134. cinfo->src->next_input_byte++;
  135. }
  136. }
  137. /*******************************************************************
  138. * *
  139. * The following routines define a JPEG Destination manager *
  140. * which just reads from a given buffer (instead of a file *
  141. * as in the jpeg library) *
  142. * *
  143. *******************************************************************/
  144. /*
  145. * Initialize destination --- called by jpeg_start_compress
  146. * before any data is actually written.
  147. */
  148. static void init_destination(j_compress_ptr cinfo ){
  149. /* No work necessary here */
  150. }
  151. /*
  152. * Empty the output buffer --- called whenever buffer fills up.
  153. *
  154. * Should never be called since all data should be written to the buffer.
  155. * If it gets called, the given jpeg buffer was too small.
  156. *
  157. */
  158. static boolean empty_output_buffer(j_compress_ptr cinfo){
  159. /*FIXME: */
  160. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Given jpeg buffer was too small");
  161. ERREXIT (cinfo, JERR_BUFFER_SIZE); /* Shouldn't be FILE_WRITE but BUFFER_OVERRUN! */
  162. return TRUE;
  163. }
  164. /*
  165. * Terminate destination --- called by jpeg_finish_compress
  166. * after all data has been written. Usually needs to flush buffer.
  167. *
  168. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  169. * application must deal with any cleanup that should happen even
  170. * for error exit.
  171. */
  172. static void term_destination(j_compress_ptr cinfo ){
  173. /* No work necessary here */
  174. }
  175. /*
  176. * Prepare for output to a stdio stream.
  177. * The caller must have already opened the stream, and is responsible
  178. * for closing it after finishing compression.
  179. */
  180. static void jpeg_buffer_dest(j_compress_ptr cinfo, unsigned char *buf, long len)
  181. {
  182. /*
  183. * The destination object is made permanent so that multiple JPEG images
  184. * can be written to the same file without re-executing jpeg_stdio_dest.
  185. * This makes it dangerous to use this manager and a different destination
  186. * manager serially with the same JPEG object, because their private object
  187. * sizes may be different. Caveat programmer.
  188. */
  189. if (cinfo->dest == NULL) { /* First time for this JPEG object? */
  190. cinfo->dest = (struct jpeg_destination_mgr *)
  191. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  192. sizeof (struct jpeg_destination_mgr));
  193. }
  194. cinfo->dest->init_destination = init_destination;
  195. cinfo->dest->empty_output_buffer = empty_output_buffer;
  196. cinfo->dest->term_destination = term_destination;
  197. cinfo->dest->free_in_buffer = len;
  198. cinfo->dest->next_output_byte = (JOCTET *) buf;
  199. }
  200. /*******************************************************************
  201. * *
  202. * decode_jpeg_data: Decode a (possibly interlaced) JPEG frame *
  203. * *
  204. *******************************************************************/
  205. /*
  206. * ERROR HANDLING:
  207. *
  208. * We want in all cases to return to the user.
  209. * The following kind of error handling is from the
  210. * example.c file in the Independent JPEG Group's JPEG software
  211. */
  212. struct my_error_mgr {
  213. struct jpeg_error_mgr pub; /* "public" fields */
  214. jmp_buf setjmp_buffer; /* For return to caller */
  215. /* Original emit_message method. */
  216. JMETHOD(void, original_emit_message, (j_common_ptr cinfo, int msg_level));
  217. /* Was a corrupt-data warning seen. */
  218. int warning_seen;
  219. };
  220. static void my_error_exit(j_common_ptr cinfo){
  221. /* cinfo->err really points to a my_error_mgr struct, so coerce pointer. */
  222. struct my_error_mgr *myerr = (struct my_error_mgr *) cinfo->err;
  223. /*
  224. * Always display the message.
  225. * We could postpone this until after returning, if we chose.
  226. */
  227. (*cinfo->err->output_message) (cinfo);
  228. /* Return control to the setjmp point. */
  229. longjmp (myerr->setjmp_buffer, 1);
  230. }
  231. static void my_emit_message(j_common_ptr cinfo, int msg_level){
  232. /* cinfo->err really points to a my_error_mgr struct, so coerce pointer. */
  233. struct my_error_mgr *myerr = (struct my_error_mgr *) cinfo->err;
  234. if (msg_level < 0)
  235. myerr->warning_seen = 1;
  236. /* Call original emit_message() */
  237. (myerr->original_emit_message)(cinfo, msg_level);
  238. }
  239. #define MAX_LUMA_WIDTH 4096
  240. #define MAX_CHROMA_WIDTH 2048
  241. static unsigned char buf0[16][MAX_LUMA_WIDTH];
  242. static unsigned char buf1[8][MAX_CHROMA_WIDTH];
  243. static unsigned char buf2[8][MAX_CHROMA_WIDTH];
  244. static unsigned char chr1[8][MAX_CHROMA_WIDTH];
  245. static unsigned char chr2[8][MAX_CHROMA_WIDTH];
  246. #if 1 /* Generation of 'std' Huffman tables... */
  247. static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr,
  248. const UINT8 *bits, const UINT8 *val)
  249. /* Define a Huffman table */
  250. {
  251. int nsymbols, len;
  252. if (*htblptr == NULL)
  253. *htblptr = jpeg_alloc_huff_table((j_common_ptr) dinfo);
  254. /* Copy the number-of-symbols-of-each-code-length counts. */
  255. memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits));
  256. /*
  257. * Validate the counts. We do this here mainly so we can copy the right
  258. * number of symbols from the val[] array, without risking marching off
  259. * the end of memory. jchuff.c will do a more thorough test later.
  260. */
  261. nsymbols = 0;
  262. for (len = 1; len <= 16; len++)
  263. nsymbols += bits[len];
  264. //if (nsymbols < 1 || nsymbols > 256) MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Given jpeg buffer was too small");
  265. memcpy((*htblptr)->huffval, val, nsymbols * sizeof(UINT8));
  266. }
  267. static void std_huff_tables (j_decompress_ptr dinfo)
  268. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  269. /* IMPORTANT: these are only valid for 8-bit data precision! */
  270. {
  271. static const UINT8 bits_dc_luminance[17] =
  272. { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  273. static const UINT8 val_dc_luminance[] =
  274. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  275. static const UINT8 bits_dc_chrominance[17] =
  276. { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  277. static const UINT8 val_dc_chrominance[] =
  278. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  279. static const UINT8 bits_ac_luminance[17] =
  280. { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  281. static const UINT8 val_ac_luminance[] =
  282. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  283. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  284. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  285. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  286. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  287. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  288. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  289. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  290. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  291. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  292. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  293. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  294. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  295. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  296. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  297. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  298. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  299. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  300. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  301. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  302. 0xf9, 0xfa };
  303. static const UINT8 bits_ac_chrominance[17] =
  304. { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  305. static const UINT8 val_ac_chrominance[] =
  306. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  307. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  308. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  309. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  310. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  311. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  312. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  313. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  314. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  315. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  316. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  317. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  318. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  319. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  320. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  321. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  322. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  323. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  324. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  325. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  326. 0xf9, 0xfa };
  327. add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[0],
  328. bits_dc_luminance, val_dc_luminance);
  329. add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[0],
  330. bits_ac_luminance, val_ac_luminance);
  331. add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[1],
  332. bits_dc_chrominance, val_dc_chrominance);
  333. add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[1],
  334. bits_ac_chrominance, val_ac_chrominance);
  335. }
  336. static void guarantee_huff_tables(j_decompress_ptr dinfo)
  337. {
  338. if ((dinfo->dc_huff_tbl_ptrs[0] == NULL) &&
  339. (dinfo->dc_huff_tbl_ptrs[1] == NULL) &&
  340. (dinfo->ac_huff_tbl_ptrs[0] == NULL) &&
  341. (dinfo->ac_huff_tbl_ptrs[1] == NULL)) {
  342. std_huff_tables(dinfo);
  343. }
  344. }
  345. #endif /* ...'std' Huffman table generation */
  346. /*
  347. * jpeg_data: Buffer with jpeg data to decode
  348. * len: Length of buffer
  349. * itype: 0: Not interlaced
  350. * 1: Interlaced, Top field first
  351. * 2: Interlaced, Bottom field first
  352. * ctype Chroma format for decompression.
  353. * Currently only Y4M_CHROMA_{420JPEG,422} are available
  354. * returns:
  355. * -1 on fatal error
  356. * 0 on success
  357. * 1 if jpeg lib threw a "corrupt jpeg data" warning.
  358. * in this case, "a damaged output image is likely."
  359. *
  360. */
  361. int decode_jpeg_raw (unsigned char *jpeg_data, int len,
  362. int itype, int ctype, unsigned int width,
  363. unsigned int height, unsigned char *raw0,
  364. unsigned char *raw1, unsigned char *raw2)
  365. {
  366. int numfields, hsf[3], field, yl, yc;
  367. int i, xsl, xsc, xs, hdown;
  368. unsigned int x, y = 0, vsf[3], xd;
  369. JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3],
  370. buf0[4], buf0[5], buf0[6], buf0[7],
  371. buf0[8], buf0[9], buf0[10], buf0[11],
  372. buf0[12], buf0[13], buf0[14], buf0[15]};
  373. JSAMPROW row1[8] = { buf1[0], buf1[1], buf1[2], buf1[3],
  374. buf1[4], buf1[5], buf1[6], buf1[7]};
  375. JSAMPROW row2[16] = { buf2[0], buf2[1], buf2[2], buf2[3],
  376. buf2[4], buf2[5], buf2[6], buf2[7]};
  377. JSAMPROW row1_444[16], row2_444[16];
  378. JSAMPARRAY scanarray[3] = { row0, row1, row2};
  379. struct jpeg_decompress_struct dinfo;
  380. struct my_error_mgr jerr;
  381. /* We set up the normal JPEG error routines, then override error_exit. */
  382. dinfo.err = jpeg_std_error (&jerr.pub);
  383. jerr.pub.error_exit = my_error_exit;
  384. /* Also hook the emit_message routine to note corrupt-data warnings. */
  385. jerr.original_emit_message = jerr.pub.emit_message;
  386. jerr.pub.emit_message = my_emit_message;
  387. jerr.warning_seen = 0;
  388. /* Establish the setjmp return context for my_error_exit to use. */
  389. if (setjmp (jerr.setjmp_buffer)) {
  390. /* If we get here, the JPEG code has signaled an error. */
  391. jpeg_destroy_decompress (&dinfo);
  392. return -1;
  393. }
  394. jpeg_create_decompress (&dinfo);
  395. jpeg_buffer_src (&dinfo, jpeg_data, len);
  396. /*
  397. * Read header, make some checks and try to figure out what the
  398. * user really wants.
  399. */
  400. jpeg_read_header (&dinfo, TRUE);
  401. dinfo.raw_data_out = TRUE;
  402. #if JPEG_LIB_VERSION >= 70
  403. dinfo.do_fancy_upsampling = FALSE;
  404. #endif
  405. dinfo.out_color_space = JCS_YCbCr;
  406. dinfo.dct_method = JDCT_IFAST;
  407. guarantee_huff_tables(&dinfo);
  408. jpeg_start_decompress (&dinfo);
  409. if (dinfo.output_components != 3) {
  410. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Output components of JPEG image= %d, must be 3", dinfo.output_components);
  411. goto ERR_EXIT;
  412. }
  413. for (i = 0; i < 3; i++) {
  414. hsf[i] = dinfo.comp_info[i].h_samp_factor;
  415. vsf[i] = dinfo.comp_info[i].v_samp_factor;
  416. }
  417. if ((hsf[0] != 2 && hsf[0] != 1) || hsf[1] != 1 || hsf[2] != 1 ||
  418. (vsf[0] != 1 && vsf[0] != 2) || vsf[1] != 1 || vsf[2] != 1) {
  419. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Unsupported sampling factors, hsf=(%d, %d, %d) vsf=(%d, %d, %d) !", hsf[0], hsf[1], hsf[2], vsf[0], vsf[1], vsf[2]);
  420. goto ERR_EXIT;
  421. }
  422. if (hsf[0] == 1) {
  423. if (height % 8 != 0) {
  424. // MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: YUV 4:4:4 sampling, but image height %d not dividable by 8 !", height);
  425. goto ERR_EXIT;
  426. }
  427. for (y = 0; y < 16; y++) { // Allocate a special buffer for the extra sampling depth.
  428. row1_444[y] = (unsigned char *)malloc(dinfo.output_width * sizeof(char));
  429. row2_444[y] = (unsigned char *)malloc(dinfo.output_width * sizeof(char));
  430. }
  431. scanarray[1] = row1_444;
  432. scanarray[2] = row2_444;
  433. }
  434. /* Height match image height or be exact twice the image height. */
  435. if (dinfo.output_height == height) {
  436. numfields = 1;
  437. } else if (2 * dinfo.output_height == height) {
  438. numfields = 2;
  439. } else {
  440. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Read JPEG: requested height = %d, height of image = %d", height, dinfo.output_height);
  441. goto ERR_EXIT;
  442. }
  443. /* Width is more flexible */
  444. if (dinfo.output_width > MAX_LUMA_WIDTH) {
  445. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Image width of %d exceeds max", dinfo.output_width);
  446. goto ERR_EXIT;
  447. }
  448. if (width < 2 * dinfo.output_width / 3) {
  449. /* Downsample 2:1 */
  450. hdown = 1;
  451. if (2 * width < dinfo.output_width)
  452. xsl = (dinfo.output_width - 2 * width) / 2;
  453. else
  454. xsl = 0;
  455. } else if (width == 2 * dinfo.output_width / 3) {
  456. /* Special case of 3:2 downsampling */
  457. hdown = 2;
  458. xsl = 0;
  459. } else {
  460. /* No downsampling */
  461. hdown = 0;
  462. if (width < dinfo.output_width)
  463. xsl = (dinfo.output_width - width) / 2;
  464. else
  465. xsl = 0;
  466. }
  467. /* Make xsl even, calculate xsc */
  468. xsl = xsl & ~1;
  469. xsc = xsl / 2;
  470. yl = yc = 0;
  471. for (field = 0; field < numfields; field++) {
  472. if (field > 0) {
  473. jpeg_read_header (&dinfo, TRUE);
  474. dinfo.raw_data_out = TRUE;
  475. #if JPEG_LIB_VERSION >= 70
  476. dinfo.do_fancy_upsampling = FALSE;
  477. #endif
  478. dinfo.out_color_space = JCS_YCbCr;
  479. dinfo.dct_method = JDCT_IFAST;
  480. jpeg_start_decompress (&dinfo);
  481. }
  482. if (numfields == 2) {
  483. switch (itype) {
  484. case Y4M_ILACE_TOP_FIRST:
  485. yl = yc = field;
  486. break;
  487. case Y4M_ILACE_BOTTOM_FIRST:
  488. yl = yc = (1 - field);
  489. break;
  490. default:
  491. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Input is interlaced but no interlacing set");
  492. goto ERR_EXIT;
  493. }
  494. } else {
  495. yl = yc = 0;
  496. }
  497. while (dinfo.output_scanline < dinfo.output_height) {
  498. /* Read raw data */
  499. jpeg_read_raw_data (&dinfo, scanarray, 8 * vsf[0]);
  500. for (y = 0; y < 8 * vsf[0]; yl += numfields, y++) {
  501. xd = yl * width;
  502. xs = xsl;
  503. if (hdown == 0) {
  504. for (x = 0; x < width; x++)
  505. raw0[xd++] = row0[y][xs++];
  506. } else if (hdown == 1) {
  507. for (x = 0; x < width; x++, xs += 2)
  508. raw0[xd++] = (row0[y][xs] + row0[y][xs + 1]) >> 1;
  509. } else {
  510. for (x = 0; x < width / 2; x++, xd += 2, xs += 3) {
  511. raw0[xd] = (2 * row0[y][xs] + row0[y][xs + 1]) / 3;
  512. raw0[xd + 1] = (2 * row0[y][xs + 2] + row0[y][xs + 1]) / 3;
  513. }
  514. }
  515. }
  516. /* Horizontal downsampling of chroma */
  517. for (y = 0; y < 8; y++) {
  518. xs = xsc;
  519. if (hsf[0] == 1)
  520. for (x = 0; x < width / 2; x++, xs++) {
  521. row1[y][xs] = (row1_444[y][2*x] + row1_444[y][2*x + 1]) >> 1;
  522. row2[y][xs] = (row2_444[y][2*x] + row2_444[y][2*x + 1]) >> 1;
  523. }
  524. xs = xsc;
  525. if (hdown == 0) {
  526. for (x = 0; x < width / 2; x++, xs++) {
  527. chr1[y][x] = row1[y][xs];
  528. chr2[y][x] = row2[y][xs];
  529. }
  530. } else if (hdown == 1) {
  531. for (x = 0; x < width / 2; x++, xs += 2) {
  532. chr1[y][x] = (row1[y][xs] + row1[y][xs + 1]) >> 1;
  533. chr2[y][x] = (row2[y][xs] + row2[y][xs + 1]) >> 1;
  534. }
  535. } else {
  536. for (x = 0; x < width / 2; x += 2, xs += 3) {
  537. chr1[y][x] = (2 * row1[y][xs] + row1[y][xs + 1]) / 3;
  538. chr1[y][x + 1] = (2 * row1[y][xs + 2] + row1[y][xs + 1]) / 3;
  539. chr2[y][x] = (2 * row2[y][xs] + row2[y][xs + 1]) / 3;
  540. chr2[y][x + 1] = (2 * row2[y][xs + 2] + row2[y][xs + 1]) / 3;
  541. }
  542. }
  543. }
  544. /* Vertical resampling of chroma */
  545. switch (ctype) {
  546. case Y4M_CHROMA_422:
  547. if (vsf[0] == 1) {
  548. /* Just copy */
  549. for (y = 0; y < 8 /*&& yc < height */; y++, yc += numfields) {
  550. xd = yc * width / 2;
  551. for (x = 0; x < width / 2; x++, xd++) {
  552. raw1[xd] = chr1[y][x];
  553. raw2[xd] = chr2[y][x];
  554. }
  555. }
  556. } else {
  557. /* upsample */
  558. for (y = 0; y < 8 /*&& yc < height */; y++) {
  559. xd = yc * width / 2;
  560. for (x = 0; x < width / 2; x++, xd++) {
  561. raw1[xd] = chr1[y][x];
  562. raw2[xd] = chr2[y][x];
  563. }
  564. yc += numfields;
  565. xd = yc * width / 2;
  566. for (x = 0; x < width / 2; x++, xd++) {
  567. raw1[xd] = chr1[y][x];
  568. raw2[xd] = chr2[y][x];
  569. }
  570. yc += numfields;
  571. }
  572. }
  573. break;
  574. default:
  575. /*
  576. * Should be case Y4M_CHROMA_420JPEG: but use default: for compatibility. Some
  577. * pass things like '420' in with the expectation that anything other than
  578. * Y4M_CHROMA_422 will default to 420JPEG.
  579. */
  580. if (vsf[0] == 1) {
  581. /* Really downsample */
  582. for (y = 0; y < 8 /*&& yc < height/2*/; y += 2, yc += numfields) {
  583. xd = yc * width / 2;
  584. for (x = 0; x < width / 2; x++, xd++) {
  585. assert(xd < (width * height / 4));
  586. raw1[xd] = (chr1[y][x] + chr1[y + 1][x]) >> 1;
  587. raw2[xd] = (chr2[y][x] + chr2[y + 1][x]) >> 1;
  588. }
  589. }
  590. } else {
  591. /* Just copy */
  592. for (y = 0; y < 8 /* && yc < height / 2 */; y++, yc += numfields) {
  593. xd = yc * width / 2;
  594. for (x = 0; x < width / 2; x++, xd++) {
  595. raw1[xd] = chr1[y][x];
  596. raw2[xd] = chr2[y][x];
  597. }
  598. }
  599. }
  600. break;
  601. }
  602. }
  603. (void) jpeg_finish_decompress (&dinfo);
  604. if (field == 0 && numfields > 1)
  605. jpeg_skip_ff (&dinfo);
  606. }
  607. if (hsf[0] == 1) {
  608. for (y = 0; y < 16; y++) { // Allocate a special buffer for the extra sampling depth.
  609. free(row1_444[y]);
  610. free(row2_444[y]);
  611. }
  612. }
  613. jpeg_destroy_decompress (&dinfo);
  614. if (jerr.warning_seen)
  615. return 1;
  616. else
  617. return 0;
  618. ERR_EXIT:
  619. jpeg_destroy_decompress (&dinfo);
  620. return -1;
  621. }
  622. /*
  623. * jpeg_data: Buffer with jpeg data to decode, must be grayscale mode
  624. * len: Length of buffer
  625. * itype: 0: Not interlaced
  626. * 1: Interlaced, Top field first
  627. * 2: Interlaced, Bottom field first
  628. * ctype Chroma format for decompression.
  629. * Currently only Y4M_CHROMA_{420JPEG,422} are available
  630. */
  631. int decode_jpeg_gray_raw(unsigned char *jpeg_data, int len,
  632. int itype, int ctype, unsigned int width,
  633. unsigned int height, unsigned char *raw0,
  634. unsigned char *raw1, unsigned char *raw2)
  635. {
  636. int numfields, hsf[3], field, yl, yc, xsl, xsc, xs, xd, hdown;
  637. unsigned int x, y, vsf[3];
  638. JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3],
  639. buf0[4], buf0[5], buf0[6], buf0[7],
  640. buf0[8], buf0[9], buf0[10], buf0[11],
  641. buf0[12], buf0[13], buf0[14], buf0[15]};
  642. JSAMPARRAY scanarray[3] = { row0 };
  643. struct jpeg_decompress_struct dinfo;
  644. struct my_error_mgr jerr;
  645. /* We set up the normal JPEG error routines, then override error_exit. */
  646. dinfo.err = jpeg_std_error (&jerr.pub);
  647. jerr.pub.error_exit = my_error_exit;
  648. /* Establish the setjmp return context for my_error_exit to use. */
  649. if (setjmp (jerr.setjmp_buffer)) {
  650. /* If we get here, the JPEG code has signaled an error. */
  651. jpeg_destroy_decompress (&dinfo);
  652. return -1;
  653. }
  654. jpeg_create_decompress (&dinfo);
  655. jpeg_buffer_src (&dinfo, jpeg_data, len);
  656. /*
  657. * Read header, make some checks and try to figure out what the
  658. * user really wants.
  659. */
  660. jpeg_read_header (&dinfo, TRUE);
  661. dinfo.raw_data_out = TRUE;
  662. #if JPEG_LIB_VERSION >= 70
  663. dinfo.do_fancy_upsampling = FALSE;
  664. #endif
  665. dinfo.out_color_space = JCS_GRAYSCALE;
  666. dinfo.dct_method = JDCT_IFAST;
  667. if (dinfo.jpeg_color_space != JCS_GRAYSCALE) {
  668. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Expected grayscale colorspace for JPEG raw decoding");
  669. goto ERR_EXIT;
  670. }
  671. guarantee_huff_tables(&dinfo);
  672. jpeg_start_decompress (&dinfo);
  673. hsf[0] = 1; hsf[1] = 1; hsf[2] = 1;
  674. vsf[0]= 1; vsf[1] = 1; vsf[2] = 1;
  675. /* Height match image height or be exact twice the image height. */
  676. if (dinfo.output_height == height) {
  677. numfields = 1;
  678. } else if (2 * dinfo.output_height == height) {
  679. numfields = 2;
  680. } else {
  681. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Read JPEG: requested height = %d, height of image = %d", height, dinfo.output_height);
  682. goto ERR_EXIT;
  683. }
  684. /* Width is more flexible */
  685. if (dinfo.output_width > MAX_LUMA_WIDTH) {
  686. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Image width of %d exceeds max",dinfo.output_width);
  687. goto ERR_EXIT;
  688. }
  689. if (width < 2 * dinfo.output_width / 3) {
  690. /* Downsample 2:1 */
  691. hdown = 1;
  692. if (2 * width < dinfo.output_width)
  693. xsl = (dinfo.output_width - 2 * width) / 2;
  694. else
  695. xsl = 0;
  696. } else if (width == 2 * dinfo.output_width / 3) {
  697. /* special case of 3:2 downsampling */
  698. hdown = 2;
  699. xsl = 0;
  700. } else {
  701. /* No downsampling */
  702. hdown = 0;
  703. if (width < dinfo.output_width)
  704. xsl = (dinfo.output_width - width) / 2;
  705. else
  706. xsl = 0;
  707. }
  708. /* Make xsl even, calculate xsc */
  709. xsl = xsl & ~1;
  710. xsc = xsl / 2;
  711. yl = yc = 0;
  712. for (field = 0; field < numfields; field++) {
  713. if (field > 0) {
  714. jpeg_read_header (&dinfo, TRUE);
  715. dinfo.raw_data_out = TRUE;
  716. #if JPEG_LIB_VERSION >= 70
  717. dinfo.do_fancy_upsampling = FALSE;
  718. #endif
  719. dinfo.out_color_space = JCS_GRAYSCALE;
  720. dinfo.dct_method = JDCT_IFAST;
  721. jpeg_start_decompress (&dinfo);
  722. }
  723. if (numfields == 2) {
  724. switch (itype) {
  725. case Y4M_ILACE_TOP_FIRST:
  726. yl = yc = field;
  727. break;
  728. case Y4M_ILACE_BOTTOM_FIRST:
  729. yl = yc = (1 - field);
  730. break;
  731. default:
  732. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Input is interlaced but no interlacing set");
  733. goto ERR_EXIT;
  734. }
  735. } else {
  736. yl = yc = 0;
  737. }
  738. while (dinfo.output_scanline < dinfo.output_height) {
  739. jpeg_read_raw_data (&dinfo, scanarray, 16);
  740. for (y = 0; y < 8 * vsf[0]; yl += numfields, y++) {
  741. xd = yl * width;
  742. xs = xsl;
  743. if (hdown == 0) { // No horiz downsampling
  744. for (x = 0; x < width; x++)
  745. raw0[xd++] = row0[y][xs++];
  746. } else if (hdown == 1) { // Half the res
  747. for (x = 0; x < width; x++, xs += 2)
  748. raw0[xd++] = (row0[y][xs] + row0[y][xs + 1]) >> 1;
  749. } else { // 2:3 downsampling
  750. for (x = 0; x < width / 2; x++, xd += 2, xs += 3) {
  751. raw0[xd] = (2 * row0[y][xs] + row0[y][xs + 1]) / 3;
  752. raw0[xd + 1] = (2 * row0[y][xs + 2] + row0[y][xs + 1]) / 3;
  753. }
  754. }
  755. }
  756. for (y = 0; y < 8; y++) {
  757. xs = xsc;
  758. if (hdown == 0) {
  759. for (x = 0; x < width / 2; x++, xs++) {
  760. chr1[y][x] = 0; //row1[y][xs];
  761. chr2[y][x] = 0; //row2[y][xs];
  762. }
  763. } else if (hdown == 1) {
  764. for (x = 0; x < width / 2; x++, xs += 2) {
  765. chr1[y][x] = 0; //(row1[y][xs] + row1[y][xs + 1]) >> 1;
  766. chr2[y][x] = 0; //(row2[y][xs] + row2[y][xs + 1]) >> 1;
  767. }
  768. } else {
  769. for (x = 0; x < width / 2; x += 2, xs += 3) {
  770. chr1[y][x] = 0; //(2 * row1[y][xs] + row1[y][xs + 1]) / 3;
  771. chr1[y][x + 1] = 0;
  772. //(2 * row1[y][xs + 2] + row1[y][xs + 1]) / 3;
  773. chr2[y][x] = 0; // (2 * row2[y][xs] + row2[y][xs + 1]) / 3;
  774. chr2[y][x + 1] = 0;
  775. //(2 * row2[y][xs + 2] + row2[y][xs + 1]) / 3;
  776. }
  777. }
  778. }
  779. switch (ctype) {
  780. case Y4M_CHROMA_422:
  781. if (vsf[0] == 1) {
  782. /* Just copy */
  783. for (y = 0; y < 8 /* && yc < height */; y++, yc += numfields) {
  784. xd = yc * width / 2;
  785. for (x = 0; x < width / 2; x++, xd++) {
  786. raw1[xd] = 127; //chr1[y][x];
  787. raw2[xd] = 127; //chr2[y][x];
  788. }
  789. }
  790. } else {
  791. /* upsample */
  792. for (y = 0; y < 8 /* && yc < height */; y++) {
  793. xd = yc * width / 2;
  794. for (x = 0; x < width / 2; x++, xd++) {
  795. raw1[xd] = 127; //chr1[y][x];
  796. raw2[xd] = 127; //chr2[y][x];
  797. }
  798. yc += numfields;
  799. xd = yc * width / 2;
  800. for (x = 0; x < width / 2; x++, xd++) {
  801. raw1[xd] = 127; //chr1[y][x];
  802. raw2[xd] = 127; //chr2[y][x];
  803. }
  804. yc += numfields;
  805. }
  806. }
  807. break;
  808. /*
  809. * Should be case Y4M_CHROMA_420JPEG: but use default: for compatibility. Some
  810. * pass things like '420' in with the expectation that anything other than
  811. * Y4M_CHROMA_422 will default to 420JPEG.
  812. */
  813. default:
  814. if (vsf[0] == 1) {
  815. /* Really downsample */
  816. for (y = 0; y < 8; y += 2, yc += numfields) {
  817. xd = yc * width / 2;
  818. for (x = 0; x < width / 2; x++, xd++) {
  819. raw1[xd] = 127; //(chr1[y][x] + chr1[y + 1][x]) >> 1;
  820. raw2[xd] = 127; //(chr2[y][x] + chr2[y + 1][x]) >> 1;
  821. }
  822. }
  823. } else {
  824. /* Just copy */
  825. for (y = 0; y < 8; y++, yc += numfields) {
  826. xd = yc * width / 2;
  827. for (x = 0; x < width / 2; x++, xd++) {
  828. raw1[xd] = 127; //chr1[y][x];
  829. raw2[xd] = 127; //chr2[y][x];
  830. }
  831. }
  832. }
  833. break;
  834. }
  835. }
  836. (void) jpeg_finish_decompress (&dinfo);
  837. if (field == 0 && numfields > 1)
  838. jpeg_skip_ff (&dinfo);
  839. }
  840. jpeg_destroy_decompress (&dinfo);
  841. return 0;
  842. ERR_EXIT:
  843. jpeg_destroy_decompress (&dinfo);
  844. return -1;
  845. }
  846. /*******************************************************************
  847. * *
  848. * encode_jpeg_data: Compress raw YCbCr data (output JPEG *
  849. * may be interlaced *
  850. * *
  851. *******************************************************************/
  852. /*
  853. * jpeg_data: Buffer to hold output jpeg
  854. * len: Length of buffer
  855. * itype: 0: Not interlaced
  856. * 1: Interlaced, Top field first
  857. * 2: Interlaced, Bottom field first
  858. * ctype Chroma format for decompression.
  859. * Currently only Y4M_CHROMA_{420JPEG,422} are available
  860. */
  861. int encode_jpeg_raw(unsigned char *jpeg_data, int len, int quality,
  862. int itype, int ctype, unsigned int width,
  863. unsigned int height, unsigned char *raw0,
  864. unsigned char *raw1, unsigned char *raw2)
  865. {
  866. int numfields, field, yl, yc, y, i;
  867. JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3],
  868. buf0[4], buf0[5], buf0[6], buf0[7],
  869. buf0[8], buf0[9], buf0[10], buf0[11],
  870. buf0[12], buf0[13], buf0[14], buf0[15]};
  871. JSAMPROW row1[8] = { buf1[0], buf1[1], buf1[2], buf1[3],
  872. buf1[4], buf1[5], buf1[6], buf1[7]};
  873. JSAMPROW row2[8] = { buf2[0], buf2[1], buf2[2], buf2[3],
  874. buf2[4], buf2[5], buf2[6], buf2[7]};
  875. JSAMPARRAY scanarray[3] = { row0, row1, row2};
  876. struct jpeg_compress_struct cinfo;
  877. struct my_error_mgr jerr;
  878. /* We set up the normal JPEG error routines, then override error_exit. */
  879. cinfo.err = jpeg_std_error (&jerr.pub);
  880. jerr.pub.error_exit = my_error_exit;
  881. /* Establish the setjmp return context for my_error_exit to use. */
  882. if (setjmp (jerr.setjmp_buffer)) {
  883. /* If we get here, the JPEG code has signaled an error. */
  884. jpeg_destroy_compress (&cinfo);
  885. return -1;
  886. }
  887. jpeg_create_compress (&cinfo);
  888. jpeg_buffer_dest(&cinfo, jpeg_data, len);
  889. /* Set some jpeg header fields */
  890. cinfo.input_components = 3;
  891. jpeg_set_defaults (&cinfo);
  892. jpeg_set_quality (&cinfo, quality, FALSE);
  893. cinfo.raw_data_in = TRUE;
  894. cinfo.in_color_space = JCS_YCbCr;
  895. cinfo.dct_method = JDCT_IFAST;
  896. cinfo.input_gamma = 1.0;
  897. cinfo.comp_info[0].h_samp_factor = 2;
  898. cinfo.comp_info[0].v_samp_factor = 1; /*1||2 */
  899. cinfo.comp_info[1].h_samp_factor = 1;
  900. cinfo.comp_info[1].v_samp_factor = 1;
  901. cinfo.comp_info[2].h_samp_factor = 1; /*1||2 */
  902. cinfo.comp_info[2].v_samp_factor = 1;
  903. if ((width > 4096) || (height > 4096)) {
  904. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Image dimensions (%dx%d) exceed lavtools' max (4096x4096)", width, height);
  905. goto ERR_EXIT;
  906. }
  907. if ((width % 16) || (height % 16)) {
  908. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Image dimensions (%dx%d) not multiples of 16", width, height);
  909. goto ERR_EXIT;
  910. }
  911. cinfo.image_width = width;
  912. switch (itype) {
  913. case Y4M_ILACE_TOP_FIRST:
  914. case Y4M_ILACE_BOTTOM_FIRST: /* Interlaced */
  915. numfields = 2;
  916. break;
  917. default:
  918. numfields = 1;
  919. if (height > 2048) {
  920. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Image height (%d) exceeds lavtools max for non-interlaced frames", height);
  921. goto ERR_EXIT;
  922. }
  923. }
  924. cinfo.image_height = height / numfields;
  925. yl = yc = 0; /* y luma, chroma */
  926. for (field = 0; field < numfields; field++) {
  927. jpeg_start_compress (&cinfo, FALSE);
  928. if (numfields == 2) {
  929. static const JOCTET marker0[40]={0};
  930. jpeg_write_marker(&cinfo, JPEG_APP0, marker0, 14);
  931. jpeg_write_marker(&cinfo, JPEG_APP0 + 1, marker0, 40);
  932. switch (itype) {
  933. case Y4M_ILACE_TOP_FIRST: /* Top field first */
  934. yl = yc = field;
  935. break;
  936. case Y4M_ILACE_BOTTOM_FIRST: /* Bottom field first */
  937. yl = yc = (1 - field);
  938. break;
  939. default:
  940. //MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Input is interlaced but no interlacing set");
  941. goto ERR_EXIT;
  942. }
  943. } else {
  944. yl = yc = 0;
  945. }
  946. while (cinfo.next_scanline < cinfo.image_height) {
  947. for (y = 0; y < 8 * cinfo.comp_info[0].v_samp_factor;
  948. yl += numfields, y++) {
  949. row0[y] = &raw0[yl * width];
  950. }
  951. for (y = 0; y < 8; y++) {
  952. row1[y] = &raw1[yc * width / 2];
  953. row2[y] = &raw2[yc * width / 2];
  954. if ((ctype == Y4M_CHROMA_422) || (y % 2))
  955. yc += numfields;
  956. }
  957. jpeg_write_raw_data (&cinfo, scanarray,
  958. 8 * cinfo.comp_info[0].v_samp_factor);
  959. }
  960. (void) jpeg_finish_compress (&cinfo);
  961. }
  962. /* FIXME */
  963. i = len - cinfo.dest->free_in_buffer;
  964. jpeg_destroy_compress (&cinfo);
  965. return i; /* Size of jpeg */
  966. ERR_EXIT:
  967. jpeg_destroy_compress (&cinfo);
  968. return -1;
  969. }