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.

1851 lines
54KB

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