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.

1544 lines
43KB

  1. /*
  2. * MJPEG encoder and decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Support for external huffman table, various fixes (AVID workaround),
  20. * aspecting and new decode_frame mechanism
  21. * by Alex Beregszaszi <alex@naxine.org>
  22. */
  23. //#define DEBUG
  24. #include "avcodec.h"
  25. #include "dsputil.h"
  26. #include "mpegvideo.h"
  27. /* use two quantizer tables (one for luminance and one for chrominance) */
  28. /* not yet working */
  29. #undef TWOMATRIXES
  30. typedef struct MJpegContext {
  31. UINT8 huff_size_dc_luminance[12];
  32. UINT16 huff_code_dc_luminance[12];
  33. UINT8 huff_size_dc_chrominance[12];
  34. UINT16 huff_code_dc_chrominance[12];
  35. UINT8 huff_size_ac_luminance[256];
  36. UINT16 huff_code_ac_luminance[256];
  37. UINT8 huff_size_ac_chrominance[256];
  38. UINT16 huff_code_ac_chrominance[256];
  39. } MJpegContext;
  40. /* JPEG marker codes */
  41. typedef enum {
  42. /* start of frame */
  43. SOF0 = 0xc0, /* baseline */
  44. SOF1 = 0xc1, /* extended sequential, huffman */
  45. SOF2 = 0xc2, /* progressive, huffman */
  46. SOF3 = 0xc3, /* lossless, huffman */
  47. SOF5 = 0xc5, /* differential sequential, huffman */
  48. SOF6 = 0xc6, /* differential progressive, huffman */
  49. SOF7 = 0xc7, /* differential lossless, huffman */
  50. JPG = 0xc8, /* reserved for JPEG extension */
  51. SOF9 = 0xc9, /* extended sequential, arithmetic */
  52. SOF10 = 0xca, /* progressive, arithmetic */
  53. SOF11 = 0xcb, /* lossless, arithmetic */
  54. SOF13 = 0xcd, /* differential sequential, arithmetic */
  55. SOF14 = 0xce, /* differential progressive, arithmetic */
  56. SOF15 = 0xcf, /* differential lossless, arithmetic */
  57. DHT = 0xc4, /* define huffman tables */
  58. DAC = 0xcc, /* define arithmetic-coding conditioning */
  59. /* restart with modulo 8 count "m" */
  60. RST0 = 0xd0,
  61. RST1 = 0xd1,
  62. RST2 = 0xd2,
  63. RST3 = 0xd3,
  64. RST4 = 0xd4,
  65. RST5 = 0xd5,
  66. RST6 = 0xd6,
  67. RST7 = 0xd7,
  68. SOI = 0xd8, /* start of image */
  69. EOI = 0xd9, /* end of image */
  70. SOS = 0xda, /* start of scan */
  71. DQT = 0xdb, /* define quantization tables */
  72. DNL = 0xdc, /* define number of lines */
  73. DRI = 0xdd, /* define restart interval */
  74. DHP = 0xde, /* define hierarchical progression */
  75. EXP = 0xdf, /* expand reference components */
  76. APP0 = 0xe0,
  77. APP1 = 0xe1,
  78. APP2 = 0xe2,
  79. APP3 = 0xe3,
  80. APP4 = 0xe4,
  81. APP5 = 0xe5,
  82. APP6 = 0xe6,
  83. APP7 = 0xe7,
  84. APP8 = 0xe8,
  85. APP9 = 0xe9,
  86. APP10 = 0xea,
  87. APP11 = 0xeb,
  88. APP12 = 0xec,
  89. APP13 = 0xed,
  90. APP14 = 0xee,
  91. APP15 = 0xef,
  92. JPG0 = 0xf0,
  93. JPG1 = 0xf1,
  94. JPG2 = 0xf2,
  95. JPG3 = 0xf3,
  96. JPG4 = 0xf4,
  97. JPG5 = 0xf5,
  98. JPG6 = 0xf6,
  99. JPG7 = 0xf7,
  100. JPG8 = 0xf8,
  101. JPG9 = 0xf9,
  102. JPG10 = 0xfa,
  103. JPG11 = 0xfb,
  104. JPG12 = 0xfc,
  105. JPG13 = 0xfd,
  106. COM = 0xfe, /* comment */
  107. TEM = 0x01, /* temporary private use for arithmetic coding */
  108. /* 0x02 -> 0xbf reserved */
  109. } JPEG_MARKER;
  110. #if 0
  111. /* These are the sample quantization tables given in JPEG spec section K.1.
  112. * The spec says that the values given produce "good" quality, and
  113. * when divided by 2, "very good" quality.
  114. */
  115. static const unsigned char std_luminance_quant_tbl[64] = {
  116. 16, 11, 10, 16, 24, 40, 51, 61,
  117. 12, 12, 14, 19, 26, 58, 60, 55,
  118. 14, 13, 16, 24, 40, 57, 69, 56,
  119. 14, 17, 22, 29, 51, 87, 80, 62,
  120. 18, 22, 37, 56, 68, 109, 103, 77,
  121. 24, 35, 55, 64, 81, 104, 113, 92,
  122. 49, 64, 78, 87, 103, 121, 120, 101,
  123. 72, 92, 95, 98, 112, 100, 103, 99
  124. };
  125. static const unsigned char std_chrominance_quant_tbl[64] = {
  126. 17, 18, 24, 47, 99, 99, 99, 99,
  127. 18, 21, 26, 66, 99, 99, 99, 99,
  128. 24, 26, 56, 99, 99, 99, 99, 99,
  129. 47, 66, 99, 99, 99, 99, 99, 99,
  130. 99, 99, 99, 99, 99, 99, 99, 99,
  131. 99, 99, 99, 99, 99, 99, 99, 99,
  132. 99, 99, 99, 99, 99, 99, 99, 99,
  133. 99, 99, 99, 99, 99, 99, 99, 99
  134. };
  135. #endif
  136. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  137. /* IMPORTANT: these are only valid for 8-bit data precision! */
  138. static const UINT8 bits_dc_luminance[17] =
  139. { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  140. static const UINT8 val_dc_luminance[] =
  141. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  142. static const UINT8 bits_dc_chrominance[17] =
  143. { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  144. static const UINT8 val_dc_chrominance[] =
  145. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  146. static const UINT8 bits_ac_luminance[17] =
  147. { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  148. static const UINT8 val_ac_luminance[] =
  149. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  150. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  151. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  152. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  153. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  154. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  155. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  156. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  157. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  158. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  159. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  160. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  161. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  162. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  163. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  164. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  165. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  166. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  167. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  168. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  169. 0xf9, 0xfa
  170. };
  171. static const UINT8 bits_ac_chrominance[17] =
  172. { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  173. static const UINT8 val_ac_chrominance[] =
  174. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  175. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  176. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  177. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  178. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  179. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  180. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  181. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  182. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  183. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  184. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  185. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  186. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  187. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  188. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  189. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  190. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  191. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  192. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  193. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  194. 0xf9, 0xfa
  195. };
  196. /* isn't this function nicer than the one in the libjpeg ? */
  197. static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
  198. const UINT8 *bits_table, const UINT8 *val_table)
  199. {
  200. int i, j, k,nb, code, sym;
  201. code = 0;
  202. k = 0;
  203. for(i=1;i<=16;i++) {
  204. nb = bits_table[i];
  205. for(j=0;j<nb;j++) {
  206. sym = val_table[k++];
  207. huff_size[sym] = i;
  208. huff_code[sym] = code;
  209. code++;
  210. }
  211. code <<= 1;
  212. }
  213. }
  214. int mjpeg_init(MpegEncContext *s)
  215. {
  216. MJpegContext *m;
  217. m = av_malloc(sizeof(MJpegContext));
  218. if (!m)
  219. return -1;
  220. s->min_qcoeff=-1023;
  221. s->max_qcoeff= 1023;
  222. s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
  223. /* build all the huffman tables */
  224. build_huffman_codes(m->huff_size_dc_luminance,
  225. m->huff_code_dc_luminance,
  226. bits_dc_luminance,
  227. val_dc_luminance);
  228. build_huffman_codes(m->huff_size_dc_chrominance,
  229. m->huff_code_dc_chrominance,
  230. bits_dc_chrominance,
  231. val_dc_chrominance);
  232. build_huffman_codes(m->huff_size_ac_luminance,
  233. m->huff_code_ac_luminance,
  234. bits_ac_luminance,
  235. val_ac_luminance);
  236. build_huffman_codes(m->huff_size_ac_chrominance,
  237. m->huff_code_ac_chrominance,
  238. bits_ac_chrominance,
  239. val_ac_chrominance);
  240. s->mjpeg_ctx = m;
  241. return 0;
  242. }
  243. void mjpeg_close(MpegEncContext *s)
  244. {
  245. av_free(s->mjpeg_ctx);
  246. }
  247. static inline void put_marker(PutBitContext *p, int code)
  248. {
  249. put_bits(p, 8, 0xff);
  250. put_bits(p, 8, code);
  251. }
  252. /* table_class: 0 = DC coef, 1 = AC coefs */
  253. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  254. const UINT8 *bits_table, const UINT8 *value_table)
  255. {
  256. PutBitContext *p = &s->pb;
  257. int n, i;
  258. put_bits(p, 4, table_class);
  259. put_bits(p, 4, table_id);
  260. n = 0;
  261. for(i=1;i<=16;i++) {
  262. n += bits_table[i];
  263. put_bits(p, 8, bits_table[i]);
  264. }
  265. for(i=0;i<n;i++)
  266. put_bits(p, 8, value_table[i]);
  267. return n + 17;
  268. }
  269. static void jpeg_table_header(MpegEncContext *s)
  270. {
  271. PutBitContext *p = &s->pb;
  272. int i, j, size;
  273. UINT8 *ptr;
  274. /* quant matrixes */
  275. put_marker(p, DQT);
  276. #ifdef TWOMATRIXES
  277. put_bits(p, 16, 2 + 2 * (1 + 64));
  278. #else
  279. put_bits(p, 16, 2 + 1 * (1 + 64));
  280. #endif
  281. put_bits(p, 4, 0); /* 8 bit precision */
  282. put_bits(p, 4, 0); /* table 0 */
  283. for(i=0;i<64;i++) {
  284. j = s->intra_scantable.permutated[i];
  285. put_bits(p, 8, s->intra_matrix[j]);
  286. }
  287. #ifdef TWOMATRIXES
  288. put_bits(p, 4, 0); /* 8 bit precision */
  289. put_bits(p, 4, 1); /* table 1 */
  290. for(i=0;i<64;i++) {
  291. j = s->intra_scantable.permutated[i];
  292. put_bits(p, 8, s->chroma_intra_matrix[j]);
  293. }
  294. #endif
  295. /* huffman table */
  296. put_marker(p, DHT);
  297. flush_put_bits(p);
  298. ptr = pbBufPtr(p);
  299. put_bits(p, 16, 0); /* patched later */
  300. size = 2;
  301. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  302. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  303. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  304. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  305. ptr[0] = size >> 8;
  306. ptr[1] = size;
  307. }
  308. static void jpeg_put_comments(MpegEncContext *s)
  309. {
  310. PutBitContext *p = &s->pb;
  311. int size;
  312. UINT8 *ptr;
  313. if (s->aspect_ratio_info)
  314. {
  315. /* JFIF header */
  316. put_marker(p, APP0);
  317. put_bits(p, 16, 16);
  318. put_string(p, "JFIF"); /* this puts the trailing zero-byte too */
  319. put_bits(p, 16, 0x0201); /* v 1.02 */
  320. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  321. switch(s->aspect_ratio_info)
  322. {
  323. case FF_ASPECT_4_3_625:
  324. case FF_ASPECT_4_3_525:
  325. put_bits(p, 16, 4);
  326. put_bits(p, 16, 3);
  327. break;
  328. case FF_ASPECT_16_9_625:
  329. case FF_ASPECT_16_9_525:
  330. put_bits(p, 16, 16);
  331. put_bits(p, 16, 9);
  332. break;
  333. case FF_ASPECT_EXTENDED:
  334. put_bits(p, 16, s->aspected_width);
  335. put_bits(p, 16, s->aspected_height);
  336. break;
  337. case FF_ASPECT_SQUARE:
  338. default:
  339. put_bits(p, 16, 1); /* aspect: 1:1 */
  340. put_bits(p, 16, 1);
  341. break;
  342. }
  343. put_bits(p, 8, 0); /* thumbnail width */
  344. put_bits(p, 8, 0); /* thumbnail height */
  345. }
  346. /* comment */
  347. if(!ff_bit_exact){
  348. put_marker(p, COM);
  349. flush_put_bits(p);
  350. ptr = pbBufPtr(p);
  351. put_bits(p, 16, 0); /* patched later */
  352. #define MJPEG_VERSION "FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR
  353. put_string(p, MJPEG_VERSION);
  354. size = strlen(MJPEG_VERSION)+3;
  355. #undef MJPEG_VERSION
  356. ptr[0] = size >> 8;
  357. ptr[1] = size;
  358. }
  359. }
  360. void mjpeg_picture_header(MpegEncContext *s)
  361. {
  362. put_marker(&s->pb, SOI);
  363. if (!s->mjpeg_data_only_frames)
  364. {
  365. jpeg_put_comments(s);
  366. if (s->mjpeg_write_tables) jpeg_table_header(s);
  367. put_marker(&s->pb, SOF0);
  368. put_bits(&s->pb, 16, 17);
  369. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  370. put_bits(&s->pb, 16, s->height);
  371. put_bits(&s->pb, 16, s->width);
  372. put_bits(&s->pb, 8, 3); /* 3 components */
  373. /* Y component */
  374. put_bits(&s->pb, 8, 1); /* component number */
  375. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  376. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  377. put_bits(&s->pb, 8, 0); /* select matrix */
  378. /* Cb component */
  379. put_bits(&s->pb, 8, 2); /* component number */
  380. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  381. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  382. #ifdef TWOMATRIXES
  383. put_bits(&s->pb, 8, 1); /* select matrix */
  384. #else
  385. put_bits(&s->pb, 8, 0); /* select matrix */
  386. #endif
  387. /* Cr component */
  388. put_bits(&s->pb, 8, 3); /* component number */
  389. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  390. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  391. #ifdef TWOMATRIXES
  392. put_bits(&s->pb, 8, 1); /* select matrix */
  393. #else
  394. put_bits(&s->pb, 8, 0); /* select matrix */
  395. #endif
  396. }
  397. /* scan header */
  398. put_marker(&s->pb, SOS);
  399. put_bits(&s->pb, 16, 12); /* length */
  400. put_bits(&s->pb, 8, 3); /* 3 components */
  401. /* Y component */
  402. put_bits(&s->pb, 8, 1); /* index */
  403. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  404. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  405. /* Cb component */
  406. put_bits(&s->pb, 8, 2); /* index */
  407. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  408. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  409. /* Cr component */
  410. put_bits(&s->pb, 8, 3); /* index */
  411. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  412. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  413. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  414. put_bits(&s->pb, 8, 63); /* Se (not used) */
  415. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  416. }
  417. static void escape_FF(MpegEncContext *s)
  418. {
  419. int size= get_bit_count(&s->pb) - s->header_bits;
  420. int i, ff_count;
  421. uint8_t *buf= s->pb.buf + (s->header_bits>>3);
  422. int align= (-(int)(buf))&3;
  423. assert((size&7) == 0);
  424. size >>= 3;
  425. ff_count=0;
  426. for(i=0; i<size && i<align; i++){
  427. if(buf[i]==0xFF) ff_count++;
  428. }
  429. for(; i<size-15; i+=16){
  430. int acc, v;
  431. v= *(uint32_t*)(&buf[i]);
  432. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  433. v= *(uint32_t*)(&buf[i+4]);
  434. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  435. v= *(uint32_t*)(&buf[i+8]);
  436. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  437. v= *(uint32_t*)(&buf[i+12]);
  438. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  439. acc>>=4;
  440. acc+= (acc>>16);
  441. acc+= (acc>>8);
  442. ff_count+= acc&0xFF;
  443. }
  444. for(; i<size; i++){
  445. if(buf[i]==0xFF) ff_count++;
  446. }
  447. if(ff_count==0) return;
  448. /* skip put bits */
  449. for(i=0; i<ff_count-3; i+=4)
  450. put_bits(&s->pb, 32, 0);
  451. put_bits(&s->pb, (ff_count-i)*8, 0);
  452. flush_put_bits(&s->pb);
  453. for(i=size-1; ff_count; i--){
  454. int v= buf[i];
  455. if(v==0xFF){
  456. //printf("%d %d\n", i, ff_count);
  457. buf[i+ff_count]= 0;
  458. ff_count--;
  459. }
  460. buf[i+ff_count]= v;
  461. }
  462. }
  463. void mjpeg_picture_trailer(MpegEncContext *s)
  464. {
  465. int pad= (-get_bit_count(&s->pb))&7;
  466. put_bits(&s->pb, pad,0xFF>>(8-pad));
  467. flush_put_bits(&s->pb);
  468. escape_FF(s);
  469. put_marker(&s->pb, EOI);
  470. }
  471. static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
  472. UINT8 *huff_size, UINT16 *huff_code)
  473. {
  474. int mant, nbits;
  475. if (val == 0) {
  476. put_bits(&s->pb, huff_size[0], huff_code[0]);
  477. } else {
  478. mant = val;
  479. if (val < 0) {
  480. val = -val;
  481. mant--;
  482. }
  483. /* compute the log (XXX: optimize) */
  484. nbits = 0;
  485. while (val != 0) {
  486. val = val >> 1;
  487. nbits++;
  488. }
  489. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  490. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  491. }
  492. }
  493. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  494. {
  495. int mant, nbits, code, i, j;
  496. int component, dc, run, last_index, val;
  497. MJpegContext *m = s->mjpeg_ctx;
  498. UINT8 *huff_size_ac;
  499. UINT16 *huff_code_ac;
  500. /* DC coef */
  501. component = (n <= 3 ? 0 : n - 4 + 1);
  502. dc = block[0]; /* overflow is impossible */
  503. val = dc - s->last_dc[component];
  504. if (n < 4) {
  505. mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  506. huff_size_ac = m->huff_size_ac_luminance;
  507. huff_code_ac = m->huff_code_ac_luminance;
  508. } else {
  509. mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  510. huff_size_ac = m->huff_size_ac_chrominance;
  511. huff_code_ac = m->huff_code_ac_chrominance;
  512. }
  513. s->last_dc[component] = dc;
  514. /* AC coefs */
  515. run = 0;
  516. last_index = s->block_last_index[n];
  517. for(i=1;i<=last_index;i++) {
  518. j = s->intra_scantable.permutated[i];
  519. val = block[j];
  520. if (val == 0) {
  521. run++;
  522. } else {
  523. while (run >= 16) {
  524. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  525. run -= 16;
  526. }
  527. mant = val;
  528. if (val < 0) {
  529. val = -val;
  530. mant--;
  531. }
  532. /* compute the log (XXX: optimize) */
  533. nbits = 0;
  534. while (val != 0) {
  535. val = val >> 1;
  536. nbits++;
  537. }
  538. code = (run << 4) | nbits;
  539. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  540. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  541. run = 0;
  542. }
  543. }
  544. /* output EOB only if not already 64 values */
  545. if (last_index < 63 || run != 0)
  546. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  547. }
  548. void mjpeg_encode_mb(MpegEncContext *s,
  549. DCTELEM block[6][64])
  550. {
  551. int i;
  552. for(i=0;i<6;i++) {
  553. encode_block(s, block[i], i);
  554. }
  555. }
  556. /******************************************/
  557. /* decoding */
  558. #define MAX_COMPONENTS 4
  559. typedef struct MJpegDecodeContext {
  560. AVCodecContext *avctx;
  561. GetBitContext gb;
  562. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  563. int start_code; /* current start code */
  564. int buffer_size;
  565. UINT8 *buffer;
  566. INT16 quant_matrixes[4][64];
  567. VLC vlcs[2][4];
  568. int org_width, org_height; /* size given at codec init */
  569. int first_picture; /* true if decoding first picture */
  570. int interlaced; /* true if interlaced */
  571. int bottom_field; /* true if bottom field */
  572. int width, height;
  573. int nb_components;
  574. int component_id[MAX_COMPONENTS];
  575. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  576. int v_count[MAX_COMPONENTS];
  577. int h_max, v_max; /* maximum h and v counts */
  578. int quant_index[4]; /* quant table index for each component */
  579. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  580. UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
  581. int linesize[MAX_COMPONENTS];
  582. DCTELEM block[64] __align8;
  583. ScanTable scantable;
  584. void (*idct_put)(UINT8 *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  585. int restart_interval;
  586. int restart_count;
  587. int buggy_avid;
  588. int interlace_polarity;
  589. } MJpegDecodeContext;
  590. static int mjpeg_decode_dht(MJpegDecodeContext *s);
  591. static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  592. int nb_codes)
  593. {
  594. UINT8 huff_size[256];
  595. UINT16 huff_code[256];
  596. memset(huff_size, 0, sizeof(huff_size));
  597. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  598. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  599. }
  600. static int mjpeg_decode_init(AVCodecContext *avctx)
  601. {
  602. MJpegDecodeContext *s = avctx->priv_data;
  603. MpegEncContext s2;
  604. s->avctx = avctx;
  605. /* ugly way to get the idct & scantable */
  606. memset(&s2, 0, sizeof(MpegEncContext));
  607. s2.flags= avctx->flags;
  608. s2.avctx= avctx;
  609. // s2->out_format = FMT_MJPEG;
  610. s2.width = 8;
  611. s2.height = 8;
  612. if (MPV_common_init(&s2) < 0)
  613. return -1;
  614. s->scantable= s2.intra_scantable;
  615. s->idct_put= s2.idct_put;
  616. MPV_common_end(&s2);
  617. s->mpeg_enc_ctx_allocated = 0;
  618. s->buffer_size = 102400; /* smaller buffer should be enough,
  619. but photojpg files could ahive bigger sizes */
  620. s->buffer = av_malloc(s->buffer_size);
  621. s->start_code = -1;
  622. s->first_picture = 1;
  623. s->org_width = avctx->width;
  624. s->org_height = avctx->height;
  625. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  626. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  627. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  628. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  629. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  630. {
  631. printf("mjpeg: using external huffman table\n");
  632. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
  633. mjpeg_decode_dht(s);
  634. /* should check for error - but dunno */
  635. }
  636. return 0;
  637. }
  638. /* quantize tables */
  639. static int mjpeg_decode_dqt(MJpegDecodeContext *s)
  640. {
  641. int len, index, i, j;
  642. len = get_bits(&s->gb, 16) - 2;
  643. while (len >= 65) {
  644. /* only 8 bit precision handled */
  645. if (get_bits(&s->gb, 4) != 0)
  646. {
  647. dprintf("dqt: 16bit precision\n");
  648. return -1;
  649. }
  650. index = get_bits(&s->gb, 4);
  651. if (index >= 4)
  652. return -1;
  653. dprintf("index=%d\n", index);
  654. /* read quant table */
  655. for(i=0;i<64;i++) {
  656. j = s->scantable.permutated[i];
  657. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  658. }
  659. len -= 65;
  660. }
  661. return 0;
  662. }
  663. /* decode huffman tables and build VLC decoders */
  664. static int mjpeg_decode_dht(MJpegDecodeContext *s)
  665. {
  666. int len, index, i, class, n, v, code_max;
  667. UINT8 bits_table[17];
  668. UINT8 val_table[256];
  669. len = get_bits(&s->gb, 16) - 2;
  670. while (len > 0) {
  671. if (len < 17)
  672. return -1;
  673. class = get_bits(&s->gb, 4);
  674. if (class >= 2)
  675. return -1;
  676. index = get_bits(&s->gb, 4);
  677. if (index >= 4)
  678. return -1;
  679. n = 0;
  680. for(i=1;i<=16;i++) {
  681. bits_table[i] = get_bits(&s->gb, 8);
  682. n += bits_table[i];
  683. }
  684. len -= 17;
  685. if (len < n || n > 256)
  686. return -1;
  687. code_max = 0;
  688. for(i=0;i<n;i++) {
  689. v = get_bits(&s->gb, 8);
  690. if (v > code_max)
  691. code_max = v;
  692. val_table[i] = v;
  693. }
  694. len -= n;
  695. /* build VLC and flush previous vlc if present */
  696. free_vlc(&s->vlcs[class][index]);
  697. dprintf("class=%d index=%d nb_codes=%d\n",
  698. class, index, code_max + 1);
  699. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  700. }
  701. return 0;
  702. }
  703. static int mjpeg_decode_sof0(MJpegDecodeContext *s)
  704. {
  705. int len, nb_components, i, width, height;
  706. /* XXX: verify len field validity */
  707. len = get_bits(&s->gb, 16);
  708. /* only 8 bits/component accepted */
  709. if (get_bits(&s->gb, 8) != 8)
  710. return -1;
  711. height = get_bits(&s->gb, 16);
  712. width = get_bits(&s->gb, 16);
  713. dprintf("sof0: picture: %dx%d\n", width, height);
  714. nb_components = get_bits(&s->gb, 8);
  715. if (nb_components <= 0 ||
  716. nb_components > MAX_COMPONENTS)
  717. return -1;
  718. s->nb_components = nb_components;
  719. s->h_max = 1;
  720. s->v_max = 1;
  721. for(i=0;i<nb_components;i++) {
  722. /* component id */
  723. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  724. s->h_count[i] = get_bits(&s->gb, 4);
  725. s->v_count[i] = get_bits(&s->gb, 4);
  726. /* compute hmax and vmax (only used in interleaved case) */
  727. if (s->h_count[i] > s->h_max)
  728. s->h_max = s->h_count[i];
  729. if (s->v_count[i] > s->v_max)
  730. s->v_max = s->v_count[i];
  731. s->quant_index[i] = get_bits(&s->gb, 8);
  732. if (s->quant_index[i] >= 4)
  733. return -1;
  734. dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  735. s->v_count[i], s->component_id[i], s->quant_index[i]);
  736. }
  737. /* if different size, realloc/alloc picture */
  738. /* XXX: also check h_count and v_count */
  739. if (width != s->width || height != s->height) {
  740. for(i=0;i<MAX_COMPONENTS;i++)
  741. av_freep(&s->current_picture[i]);
  742. s->width = width;
  743. s->height = height;
  744. /* test interlaced mode */
  745. if (s->first_picture &&
  746. s->org_height != 0 &&
  747. s->height < ((s->org_height * 3) / 4)) {
  748. s->interlaced = 1;
  749. // s->bottom_field = (s->interlace_polarity) ? 1 : 0;
  750. s->bottom_field = 0;
  751. }
  752. for(i=0;i<nb_components;i++) {
  753. int w, h;
  754. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  755. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  756. w = w * 8 * s->h_count[i];
  757. h = h * 8 * s->v_count[i];
  758. if (s->interlaced)
  759. w *= 2;
  760. s->linesize[i] = w;
  761. /* memory test is done in mjpeg_decode_sos() */
  762. s->current_picture[i] = av_mallocz(w * h);
  763. }
  764. s->first_picture = 0;
  765. }
  766. if (len != (8+(3*nb_components)))
  767. {
  768. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  769. }
  770. return 0;
  771. }
  772. static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
  773. {
  774. int code, diff;
  775. #if 1
  776. code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
  777. #else
  778. code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
  779. #endif
  780. if (code < 0)
  781. {
  782. dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
  783. &s->vlcs[0][dc_index]);
  784. return 0xffff;
  785. }
  786. if (code == 0) {
  787. diff = 0;
  788. } else {
  789. diff = get_bits(&s->gb, code);
  790. if ((diff & (1 << (code - 1))) == 0)
  791. diff = (-1 << code) | (diff + 1);
  792. }
  793. return diff;
  794. }
  795. /* decode block and dequantize */
  796. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  797. int component, int dc_index, int ac_index, int quant_index)
  798. {
  799. int nbits, code, i, j, level;
  800. int run, val;
  801. VLC *ac_vlc;
  802. INT16 *quant_matrix;
  803. /* DC coef */
  804. val = mjpeg_decode_dc(s, dc_index);
  805. if (val == 0xffff) {
  806. dprintf("error dc\n");
  807. return -1;
  808. }
  809. quant_matrix = s->quant_matrixes[quant_index];
  810. val = val * quant_matrix[0] + s->last_dc[component];
  811. s->last_dc[component] = val;
  812. block[0] = val;
  813. /* AC coefs */
  814. ac_vlc = &s->vlcs[1][ac_index];
  815. i = 1;
  816. for(;;) {
  817. #if 1
  818. code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);
  819. #else
  820. code = get_vlc(&s->gb, ac_vlc);
  821. #endif
  822. if (code < 0) {
  823. dprintf("error ac\n");
  824. return -1;
  825. }
  826. /* EOB */
  827. if (code == 0)
  828. break;
  829. if (code == 0xf0) {
  830. i += 16;
  831. } else {
  832. run = code >> 4;
  833. nbits = code & 0xf;
  834. level = get_bits(&s->gb, nbits);
  835. if ((level & (1 << (nbits - 1))) == 0)
  836. level = (-1 << nbits) | (level + 1);
  837. i += run;
  838. if (i >= 64) {
  839. dprintf("error count: %d\n", i);
  840. return -1;
  841. }
  842. j = s->scantable.permutated[i];
  843. block[j] = level * quant_matrix[j];
  844. i++;
  845. if (i >= 64)
  846. break;
  847. }
  848. }
  849. return 0;
  850. }
  851. static int mjpeg_decode_sos(MJpegDecodeContext *s)
  852. {
  853. int len, nb_components, i, j, n, h, v, ret;
  854. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  855. int comp_index[4];
  856. int dc_index[4];
  857. int ac_index[4];
  858. int nb_blocks[4];
  859. int h_count[4];
  860. int v_count[4];
  861. /* XXX: verify len field validity */
  862. len = get_bits(&s->gb, 16);
  863. nb_components = get_bits(&s->gb, 8);
  864. if (len != 6+2*nb_components)
  865. {
  866. dprintf("decode_sos: invalid len (%d)\n", len);
  867. return -1;
  868. }
  869. /* XXX: only interleaved scan accepted */
  870. if (nb_components != 3)
  871. {
  872. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  873. return -1;
  874. }
  875. vmax = 0;
  876. hmax = 0;
  877. for(i=0;i<nb_components;i++) {
  878. id = get_bits(&s->gb, 8) - 1;
  879. dprintf("component: %d\n", id);
  880. /* find component index */
  881. for(index=0;index<s->nb_components;index++)
  882. if (id == s->component_id[index])
  883. break;
  884. if (index == s->nb_components)
  885. {
  886. dprintf("decode_sos: index(%d) out of components\n", index);
  887. return -1;
  888. }
  889. comp_index[i] = index;
  890. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  891. h_count[i] = s->h_count[index];
  892. v_count[i] = s->v_count[index];
  893. dc_index[i] = get_bits(&s->gb, 4);
  894. ac_index[i] = get_bits(&s->gb, 4);
  895. if (dc_index[i] < 0 || ac_index[i] < 0 ||
  896. dc_index[i] >= 4 || ac_index[i] >= 4)
  897. goto out_of_range;
  898. switch(s->start_code)
  899. {
  900. case SOF0:
  901. if (dc_index[i] > 1 || ac_index[i] > 1)
  902. goto out_of_range;
  903. break;
  904. case SOF1:
  905. case SOF2:
  906. if (dc_index[i] > 3 || ac_index[i] > 3)
  907. goto out_of_range;
  908. break;
  909. case SOF3:
  910. if (dc_index[i] > 3 || ac_index[i] != 0)
  911. goto out_of_range;
  912. break;
  913. }
  914. }
  915. skip_bits(&s->gb, 8); /* Ss */
  916. skip_bits(&s->gb, 8); /* Se */
  917. skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
  918. for(i=0;i<nb_components;i++)
  919. s->last_dc[i] = 1024;
  920. if (nb_components > 1) {
  921. /* interleaved stream */
  922. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  923. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  924. } else {
  925. h = s->h_max / s->h_count[comp_index[0]];
  926. v = s->v_max / s->v_count[comp_index[0]];
  927. mb_width = (s->width + h * 8 - 1) / (h * 8);
  928. mb_height = (s->height + v * 8 - 1) / (v * 8);
  929. nb_blocks[0] = 1;
  930. h_count[0] = 1;
  931. v_count[0] = 1;
  932. }
  933. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  934. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  935. for(i=0;i<nb_components;i++) {
  936. UINT8 *ptr;
  937. int x, y, c;
  938. n = nb_blocks[i];
  939. c = comp_index[i];
  940. h = h_count[i];
  941. v = v_count[i];
  942. x = 0;
  943. y = 0;
  944. if (s->restart_interval && !s->restart_count)
  945. s->restart_count = s->restart_interval;
  946. for(j=0;j<n;j++) {
  947. memset(s->block, 0, sizeof(s->block));
  948. if (decode_block(s, s->block, i,
  949. dc_index[i], ac_index[i],
  950. s->quant_index[c]) < 0) {
  951. dprintf("error y=%d x=%d\n", mb_y, mb_x);
  952. ret = -1;
  953. goto the_end;
  954. }
  955. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  956. ptr = s->current_picture[c] +
  957. (s->linesize[c] * (v * mb_y + y) * 8) +
  958. (h * mb_x + x) * 8;
  959. if (s->interlaced && s->bottom_field)
  960. ptr += s->linesize[c] >> 1;
  961. s->idct_put(ptr, s->linesize[c], s->block);
  962. if (++x == h) {
  963. x = 0;
  964. y++;
  965. }
  966. }
  967. }
  968. /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
  969. if ((s->restart_interval < 1350) && !--s->restart_count) {
  970. align_get_bits(&s->gb);
  971. skip_bits(&s->gb, 16); /* skip RSTn */
  972. for (j=0; j<nb_components; j++) /* reset dc */
  973. s->last_dc[j] = 1024;
  974. }
  975. }
  976. }
  977. ret = 0;
  978. the_end:
  979. emms_c();
  980. return ret;
  981. out_of_range:
  982. dprintf("decode_sos: ac/dc index out of range\n");
  983. return -1;
  984. }
  985. static int mjpeg_decode_dri(MJpegDecodeContext *s)
  986. {
  987. if (get_bits(&s->gb, 16) != 4)
  988. return -1;
  989. s->restart_interval = get_bits(&s->gb, 16);
  990. dprintf("restart interval: %d\n", s->restart_interval);
  991. return 0;
  992. }
  993. static int mjpeg_decode_app(MJpegDecodeContext *s)
  994. {
  995. int len, id;
  996. /* XXX: verify len field validity */
  997. len = get_bits(&s->gb, 16);
  998. if (len < 5)
  999. return -1;
  1000. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1001. id = be2me_32(id);
  1002. len -= 6;
  1003. /* buggy AVID, it puts EOI only at every 10th frame */
  1004. /* also this fourcc is used by non-avid files too, it holds some
  1005. informations, but it's always present in AVID creates files */
  1006. if (id == ff_get_fourcc("AVI1"))
  1007. {
  1008. /* structure:
  1009. 4bytes AVI1
  1010. 1bytes polarity
  1011. 1bytes always zero
  1012. 4bytes field_size
  1013. 4bytes field_size_less_padding
  1014. */
  1015. s->buggy_avid = 1;
  1016. // if (s->first_picture)
  1017. // printf("mjpeg: workarounding buggy AVID\n");
  1018. s->interlace_polarity = get_bits(&s->gb, 8);
  1019. #if 0
  1020. skip_bits(&s->gb, 8);
  1021. skip_bits(&s->gb, 32);
  1022. skip_bits(&s->gb, 32);
  1023. len -= 10;
  1024. #endif
  1025. // if (s->interlace_polarity)
  1026. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  1027. goto out;
  1028. }
  1029. // len -= 2;
  1030. if (id == ff_get_fourcc("JFIF"))
  1031. {
  1032. int t_w, t_h;
  1033. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  1034. printf("mjpeg: JFIF header found (version: %x.%x)\n",
  1035. get_bits(&s->gb, 8), get_bits(&s->gb, 8));
  1036. if (get_bits(&s->gb, 8) == 0)
  1037. {
  1038. s->avctx->aspect_ratio_info = FF_ASPECT_EXTENDED;
  1039. s->avctx->aspected_width = get_bits(&s->gb, 16);
  1040. s->avctx->aspected_height = get_bits(&s->gb, 16);
  1041. }
  1042. else
  1043. {
  1044. skip_bits(&s->gb, 16);
  1045. skip_bits(&s->gb, 16);
  1046. }
  1047. t_w = get_bits(&s->gb, 8);
  1048. t_h = get_bits(&s->gb, 8);
  1049. if (t_w && t_h)
  1050. {
  1051. /* skip thumbnail */
  1052. if (len-10-(t_w*t_h*3) > 0)
  1053. len -= t_w*t_h*3;
  1054. }
  1055. len -= 10;
  1056. goto out;
  1057. }
  1058. if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
  1059. {
  1060. printf("mjpeg: Adobe header found\n");
  1061. skip_bits(&s->gb, 16); /* version */
  1062. skip_bits(&s->gb, 16); /* flags0 */
  1063. skip_bits(&s->gb, 16); /* flags1 */
  1064. skip_bits(&s->gb, 8); /* transform */
  1065. len -= 7;
  1066. goto out;
  1067. }
  1068. /* Apple MJPEG-A */
  1069. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1070. {
  1071. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1072. id = be2me_32(id);
  1073. len -= 4;
  1074. if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
  1075. {
  1076. #if 0
  1077. skip_bits(&s->gb, 32); /* field size */
  1078. skip_bits(&s->gb, 32); /* pad field size */
  1079. skip_bits(&s->gb, 32); /* next off */
  1080. skip_bits(&s->gb, 32); /* quant off */
  1081. skip_bits(&s->gb, 32); /* huff off */
  1082. skip_bits(&s->gb, 32); /* image off */
  1083. skip_bits(&s->gb, 32); /* scan off */
  1084. skip_bits(&s->gb, 32); /* data off */
  1085. #endif
  1086. if (s->first_picture)
  1087. printf("mjpeg: Apple MJPEG-A header found\n");
  1088. }
  1089. }
  1090. out:
  1091. /* slow but needed for extreme adobe jpegs */
  1092. if (len < 0)
  1093. printf("mjpeg: error, decode_app parser read over the end\n");
  1094. while(--len > 0)
  1095. skip_bits(&s->gb, 8);
  1096. return 0;
  1097. }
  1098. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1099. {
  1100. int len, i;
  1101. UINT8 *cbuf;
  1102. /* XXX: verify len field validity */
  1103. len = get_bits(&s->gb, 16)-2;
  1104. cbuf = av_malloc(len+1);
  1105. for (i = 0; i < len; i++)
  1106. cbuf[i] = get_bits(&s->gb, 8);
  1107. if (cbuf[i-1] == '\n')
  1108. cbuf[i-1] = 0;
  1109. else
  1110. cbuf[i] = 0;
  1111. printf("mjpeg comment: '%s'\n", cbuf);
  1112. /* buggy avid, it puts EOI only at every 10th frame */
  1113. if (!strcmp(cbuf, "AVID"))
  1114. {
  1115. s->buggy_avid = 1;
  1116. // if (s->first_picture)
  1117. // printf("mjpeg: workarounding buggy AVID\n");
  1118. }
  1119. av_free(cbuf);
  1120. return 0;
  1121. }
  1122. #if 0
  1123. static int valid_marker_list[] =
  1124. {
  1125. /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
  1126. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1127. /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1128. /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1129. /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1130. /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1131. /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1132. /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1133. /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1134. /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1135. /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1136. /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1137. /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1138. /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1139. /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1140. /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1141. /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  1142. }
  1143. #endif
  1144. /* return the 8 bit start code value and update the search
  1145. state. Return -1 if no start code found */
  1146. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end)
  1147. {
  1148. UINT8 *buf_ptr;
  1149. unsigned int v, v2;
  1150. int val;
  1151. #ifdef DEBUG
  1152. int skipped=0;
  1153. #endif
  1154. buf_ptr = *pbuf_ptr;
  1155. while (buf_ptr < buf_end) {
  1156. v = *buf_ptr++;
  1157. v2 = *buf_ptr;
  1158. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe)) {
  1159. val = *buf_ptr++;
  1160. goto found;
  1161. }
  1162. #ifdef DEBUG
  1163. skipped++;
  1164. #endif
  1165. }
  1166. val = -1;
  1167. found:
  1168. #ifdef DEBUG
  1169. dprintf("find_marker skipped %d bytes\n", skipped);
  1170. #endif
  1171. *pbuf_ptr = buf_ptr;
  1172. return val;
  1173. }
  1174. static int mjpeg_decode_frame(AVCodecContext *avctx,
  1175. void *data, int *data_size,
  1176. UINT8 *buf, int buf_size)
  1177. {
  1178. MJpegDecodeContext *s = avctx->priv_data;
  1179. UINT8 *buf_end, *buf_ptr;
  1180. int i, start_code;
  1181. AVPicture *picture = data;
  1182. *data_size = 0;
  1183. /* no supplementary picture */
  1184. if (buf_size == 0)
  1185. return 0;
  1186. buf_ptr = buf;
  1187. buf_end = buf + buf_size;
  1188. while (buf_ptr < buf_end) {
  1189. /* find start next marker */
  1190. start_code = find_marker(&buf_ptr, buf_end);
  1191. {
  1192. /* EOF */
  1193. if (start_code < 0) {
  1194. goto the_end;
  1195. } else {
  1196. dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
  1197. if ((buf_end - buf_ptr) > s->buffer_size)
  1198. {
  1199. av_free(s->buffer);
  1200. s->buffer_size = buf_end-buf_ptr;
  1201. s->buffer = av_malloc(s->buffer_size);
  1202. dprintf("buffer too small, expanding to %d bytes\n",
  1203. s->buffer_size);
  1204. }
  1205. /* unescape buffer of SOS */
  1206. if (start_code == SOS)
  1207. {
  1208. UINT8 *src = buf_ptr;
  1209. UINT8 *dst = s->buffer;
  1210. while (src<buf_end)
  1211. {
  1212. UINT8 x = *(src++);
  1213. #if 0
  1214. if (x == 0xff && *src == 0xff)
  1215. break;
  1216. #endif
  1217. *(dst++) = x;
  1218. if (x == 0xff)
  1219. {
  1220. while(*src == 0xff) src++;
  1221. x = *(src++);
  1222. if (x >= 0xd0 && x <= 0xd7)
  1223. *(dst++) = x;
  1224. else if (x)
  1225. break;
  1226. }
  1227. }
  1228. init_get_bits(&s->gb, s->buffer, dst - s->buffer);
  1229. dprintf("escaping removed %d bytes\n",
  1230. (buf_end - buf_ptr) - (dst - s->buffer));
  1231. }
  1232. else
  1233. init_get_bits(&s->gb, buf_ptr, buf_end - buf_ptr);
  1234. s->start_code = start_code;
  1235. /* process markers */
  1236. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1237. dprintf("restart marker: %d\n", start_code&0x0f);
  1238. } else if (s->first_picture) {
  1239. /* APP fields */
  1240. if (start_code >= 0xe0 && start_code <= 0xef)
  1241. mjpeg_decode_app(s);
  1242. /* Comment */
  1243. else if (start_code == COM)
  1244. mjpeg_decode_com(s);
  1245. }
  1246. switch(start_code) {
  1247. case SOI:
  1248. s->restart_interval = 0;
  1249. /* nothing to do on SOI */
  1250. break;
  1251. case DQT:
  1252. mjpeg_decode_dqt(s);
  1253. break;
  1254. case DHT:
  1255. mjpeg_decode_dht(s);
  1256. break;
  1257. case SOF0:
  1258. mjpeg_decode_sof0(s);
  1259. break;
  1260. case EOI:
  1261. eoi_parser:
  1262. {
  1263. int l;
  1264. if (s->interlaced) {
  1265. s->bottom_field ^= 1;
  1266. /* if not bottom field, do not output image yet */
  1267. if (s->bottom_field)
  1268. goto not_the_end;
  1269. }
  1270. for(i=0;i<3;i++) {
  1271. picture->data[i] = s->current_picture[i];
  1272. #if 1
  1273. l = s->linesize[i];
  1274. if (s->interlaced)
  1275. l >>= 1;
  1276. picture->linesize[i] = l;
  1277. #else
  1278. picture->linesize[i] = (s->interlaced) ?
  1279. s->linesize[i] >> 1 : s->linesize[i];
  1280. #endif
  1281. }
  1282. *data_size = sizeof(AVPicture);
  1283. avctx->height = s->height;
  1284. if (s->interlaced)
  1285. avctx->height *= 2;
  1286. avctx->width = s->width;
  1287. /* XXX: not complete test ! */
  1288. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1289. case 0x11:
  1290. avctx->pix_fmt = PIX_FMT_YUV444P;
  1291. break;
  1292. case 0x21:
  1293. avctx->pix_fmt = PIX_FMT_YUV422P;
  1294. break;
  1295. default:
  1296. case 0x22:
  1297. avctx->pix_fmt = PIX_FMT_YUV420P;
  1298. break;
  1299. }
  1300. /* dummy quality */
  1301. /* XXX: infer it with matrix */
  1302. avctx->quality = 3;
  1303. goto the_end;
  1304. }
  1305. break;
  1306. case SOS:
  1307. mjpeg_decode_sos(s);
  1308. /* buggy avid puts EOI every 10-20th frame */
  1309. /* if restart period is over process EOI */
  1310. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1311. goto eoi_parser;
  1312. break;
  1313. case DRI:
  1314. mjpeg_decode_dri(s);
  1315. break;
  1316. case SOF1:
  1317. case SOF2:
  1318. case SOF3:
  1319. case SOF5:
  1320. case SOF6:
  1321. case SOF7:
  1322. case SOF9:
  1323. case SOF10:
  1324. case SOF11:
  1325. case SOF13:
  1326. case SOF14:
  1327. case SOF15:
  1328. case JPG:
  1329. printf("mjpeg: unsupported coding type (%x)\n", start_code);
  1330. break;
  1331. // default:
  1332. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1333. // break;
  1334. }
  1335. not_the_end:
  1336. /* eof process start code */
  1337. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1338. dprintf("marker parser used %d bytes (%d bits)\n",
  1339. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1340. }
  1341. }
  1342. }
  1343. the_end:
  1344. dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
  1345. // return buf_end - buf_ptr;
  1346. return buf_ptr - buf;
  1347. }
  1348. static int mjpeg_decode_end(AVCodecContext *avctx)
  1349. {
  1350. MJpegDecodeContext *s = avctx->priv_data;
  1351. int i, j;
  1352. av_free(s->buffer);
  1353. for(i=0;i<MAX_COMPONENTS;i++)
  1354. av_free(s->current_picture[i]);
  1355. for(i=0;i<2;i++) {
  1356. for(j=0;j<4;j++)
  1357. free_vlc(&s->vlcs[i][j]);
  1358. }
  1359. return 0;
  1360. }
  1361. AVCodec mjpeg_decoder = {
  1362. "mjpeg",
  1363. CODEC_TYPE_VIDEO,
  1364. CODEC_ID_MJPEG,
  1365. sizeof(MJpegDecodeContext),
  1366. mjpeg_decode_init,
  1367. NULL,
  1368. mjpeg_decode_end,
  1369. mjpeg_decode_frame,
  1370. 0,
  1371. NULL
  1372. };