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.

1894 lines
55KB

  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. #define PREDICT(ret, topleft, top, left, predictor)\
  252. switch(predictor){\
  253. case 1: ret= left; break;\
  254. case 2: ret= top; break;\
  255. case 3: ret= topleft; break;\
  256. case 4: ret= left + top - topleft; break;\
  257. case 5: ret= left + ((top - topleft)>>1); break;\
  258. case 6: ret= top + ((left - topleft)>>1); break;\
  259. case 7: ret= (left + top)>>1; break;\
  260. }
  261. static inline void put_marker(PutBitContext *p, int code)
  262. {
  263. put_bits(p, 8, 0xff);
  264. put_bits(p, 8, code);
  265. }
  266. /* table_class: 0 = DC coef, 1 = AC coefs */
  267. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  268. const uint8_t *bits_table, const uint8_t *value_table)
  269. {
  270. PutBitContext *p = &s->pb;
  271. int n, i;
  272. put_bits(p, 4, table_class);
  273. put_bits(p, 4, table_id);
  274. n = 0;
  275. for(i=1;i<=16;i++) {
  276. n += bits_table[i];
  277. put_bits(p, 8, bits_table[i]);
  278. }
  279. for(i=0;i<n;i++)
  280. put_bits(p, 8, value_table[i]);
  281. return n + 17;
  282. }
  283. static void jpeg_table_header(MpegEncContext *s)
  284. {
  285. PutBitContext *p = &s->pb;
  286. int i, j, size;
  287. uint8_t *ptr;
  288. /* quant matrixes */
  289. put_marker(p, DQT);
  290. #ifdef TWOMATRIXES
  291. put_bits(p, 16, 2 + 2 * (1 + 64));
  292. #else
  293. put_bits(p, 16, 2 + 1 * (1 + 64));
  294. #endif
  295. put_bits(p, 4, 0); /* 8 bit precision */
  296. put_bits(p, 4, 0); /* table 0 */
  297. for(i=0;i<64;i++) {
  298. j = s->intra_scantable.permutated[i];
  299. put_bits(p, 8, s->intra_matrix[j]);
  300. }
  301. #ifdef TWOMATRIXES
  302. put_bits(p, 4, 0); /* 8 bit precision */
  303. put_bits(p, 4, 1); /* table 1 */
  304. for(i=0;i<64;i++) {
  305. j = s->intra_scantable.permutated[i];
  306. put_bits(p, 8, s->chroma_intra_matrix[j]);
  307. }
  308. #endif
  309. /* huffman table */
  310. put_marker(p, DHT);
  311. flush_put_bits(p);
  312. ptr = pbBufPtr(p);
  313. put_bits(p, 16, 0); /* patched later */
  314. size = 2;
  315. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  316. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  317. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  318. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  319. ptr[0] = size >> 8;
  320. ptr[1] = size;
  321. }
  322. static void jpeg_put_comments(MpegEncContext *s)
  323. {
  324. PutBitContext *p = &s->pb;
  325. int size;
  326. uint8_t *ptr;
  327. if (s->aspect_ratio_info)
  328. {
  329. /* JFIF header */
  330. put_marker(p, APP0);
  331. put_bits(p, 16, 16);
  332. put_string(p, "JFIF"); /* this puts the trailing zero-byte too */
  333. put_bits(p, 16, 0x0201); /* v 1.02 */
  334. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  335. switch(s->aspect_ratio_info)
  336. {
  337. case FF_ASPECT_4_3_625:
  338. case FF_ASPECT_4_3_525:
  339. put_bits(p, 16, 4);
  340. put_bits(p, 16, 3);
  341. break;
  342. case FF_ASPECT_16_9_625:
  343. case FF_ASPECT_16_9_525:
  344. put_bits(p, 16, 16);
  345. put_bits(p, 16, 9);
  346. break;
  347. case FF_ASPECT_EXTENDED:
  348. put_bits(p, 16, s->aspected_width);
  349. put_bits(p, 16, s->aspected_height);
  350. break;
  351. case FF_ASPECT_SQUARE:
  352. default:
  353. put_bits(p, 16, 1); /* aspect: 1:1 */
  354. put_bits(p, 16, 1);
  355. break;
  356. }
  357. put_bits(p, 8, 0); /* thumbnail width */
  358. put_bits(p, 8, 0); /* thumbnail height */
  359. }
  360. /* comment */
  361. if(!(s->flags & CODEC_FLAG_BITEXACT)){
  362. put_marker(p, COM);
  363. flush_put_bits(p);
  364. ptr = pbBufPtr(p);
  365. put_bits(p, 16, 0); /* patched later */
  366. put_string(p, LIBAVCODEC_IDENT);
  367. size = strlen(LIBAVCODEC_IDENT)+3;
  368. ptr[0] = size >> 8;
  369. ptr[1] = size;
  370. }
  371. }
  372. void mjpeg_picture_header(MpegEncContext *s)
  373. {
  374. put_marker(&s->pb, SOI);
  375. if (!s->mjpeg_data_only_frames)
  376. {
  377. jpeg_put_comments(s);
  378. if (s->mjpeg_write_tables) jpeg_table_header(s);
  379. put_marker(&s->pb, SOF0);
  380. put_bits(&s->pb, 16, 17);
  381. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  382. put_bits(&s->pb, 16, s->height);
  383. put_bits(&s->pb, 16, s->width);
  384. put_bits(&s->pb, 8, 3); /* 3 components */
  385. /* Y component */
  386. put_bits(&s->pb, 8, 1); /* component number */
  387. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  388. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  389. put_bits(&s->pb, 8, 0); /* select matrix */
  390. /* Cb component */
  391. put_bits(&s->pb, 8, 2); /* component number */
  392. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  393. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  394. #ifdef TWOMATRIXES
  395. put_bits(&s->pb, 8, 1); /* select matrix */
  396. #else
  397. put_bits(&s->pb, 8, 0); /* select matrix */
  398. #endif
  399. /* Cr component */
  400. put_bits(&s->pb, 8, 3); /* component number */
  401. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  402. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  403. #ifdef TWOMATRIXES
  404. put_bits(&s->pb, 8, 1); /* select matrix */
  405. #else
  406. put_bits(&s->pb, 8, 0); /* select matrix */
  407. #endif
  408. }
  409. /* scan header */
  410. put_marker(&s->pb, SOS);
  411. put_bits(&s->pb, 16, 12); /* length */
  412. put_bits(&s->pb, 8, 3); /* 3 components */
  413. /* Y component */
  414. put_bits(&s->pb, 8, 1); /* index */
  415. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  416. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  417. /* Cb component */
  418. put_bits(&s->pb, 8, 2); /* index */
  419. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  420. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  421. /* Cr component */
  422. put_bits(&s->pb, 8, 3); /* index */
  423. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  424. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  425. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  426. put_bits(&s->pb, 8, 63); /* Se (not used) */
  427. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  428. }
  429. static void escape_FF(MpegEncContext *s, int start)
  430. {
  431. int size= get_bit_count(&s->pb) - start*8;
  432. int i, ff_count;
  433. uint8_t *buf= s->pb.buf + start;
  434. int align= (-(size_t)(buf))&3;
  435. assert((size&7) == 0);
  436. size >>= 3;
  437. ff_count=0;
  438. for(i=0; i<size && i<align; i++){
  439. if(buf[i]==0xFF) ff_count++;
  440. }
  441. for(; i<size-15; i+=16){
  442. int acc, v;
  443. v= *(uint32_t*)(&buf[i]);
  444. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  445. v= *(uint32_t*)(&buf[i+4]);
  446. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  447. v= *(uint32_t*)(&buf[i+8]);
  448. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  449. v= *(uint32_t*)(&buf[i+12]);
  450. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  451. acc>>=4;
  452. acc+= (acc>>16);
  453. acc+= (acc>>8);
  454. ff_count+= acc&0xFF;
  455. }
  456. for(; i<size; i++){
  457. if(buf[i]==0xFF) ff_count++;
  458. }
  459. if(ff_count==0) return;
  460. /* skip put bits */
  461. for(i=0; i<ff_count-3; i+=4)
  462. put_bits(&s->pb, 32, 0);
  463. put_bits(&s->pb, (ff_count-i)*8, 0);
  464. flush_put_bits(&s->pb);
  465. for(i=size-1; ff_count; i--){
  466. int v= buf[i];
  467. if(v==0xFF){
  468. //printf("%d %d\n", i, ff_count);
  469. buf[i+ff_count]= 0;
  470. ff_count--;
  471. }
  472. buf[i+ff_count]= v;
  473. }
  474. }
  475. void mjpeg_picture_trailer(MpegEncContext *s)
  476. {
  477. int pad= (-get_bit_count(&s->pb))&7;
  478. put_bits(&s->pb, pad,0xFF>>(8-pad));
  479. flush_put_bits(&s->pb);
  480. assert((s->header_bits&7)==0);
  481. escape_FF(s, s->header_bits>>3);
  482. put_marker(&s->pb, EOI);
  483. }
  484. static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
  485. uint8_t *huff_size, uint16_t *huff_code)
  486. {
  487. int mant, nbits;
  488. if (val == 0) {
  489. put_bits(&s->pb, huff_size[0], huff_code[0]);
  490. } else {
  491. mant = val;
  492. if (val < 0) {
  493. val = -val;
  494. mant--;
  495. }
  496. nbits= av_log2(val) + 1;
  497. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  498. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  499. }
  500. }
  501. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  502. {
  503. int mant, nbits, code, i, j;
  504. int component, dc, run, last_index, val;
  505. MJpegContext *m = s->mjpeg_ctx;
  506. uint8_t *huff_size_ac;
  507. uint16_t *huff_code_ac;
  508. /* DC coef */
  509. component = (n <= 3 ? 0 : n - 4 + 1);
  510. dc = block[0]; /* overflow is impossible */
  511. val = dc - s->last_dc[component];
  512. if (n < 4) {
  513. mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  514. huff_size_ac = m->huff_size_ac_luminance;
  515. huff_code_ac = m->huff_code_ac_luminance;
  516. } else {
  517. mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  518. huff_size_ac = m->huff_size_ac_chrominance;
  519. huff_code_ac = m->huff_code_ac_chrominance;
  520. }
  521. s->last_dc[component] = dc;
  522. /* AC coefs */
  523. run = 0;
  524. last_index = s->block_last_index[n];
  525. for(i=1;i<=last_index;i++) {
  526. j = s->intra_scantable.permutated[i];
  527. val = block[j];
  528. if (val == 0) {
  529. run++;
  530. } else {
  531. while (run >= 16) {
  532. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  533. run -= 16;
  534. }
  535. mant = val;
  536. if (val < 0) {
  537. val = -val;
  538. mant--;
  539. }
  540. nbits= av_log2(val) + 1;
  541. code = (run << 4) | nbits;
  542. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  543. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  544. run = 0;
  545. }
  546. }
  547. /* output EOB only if not already 64 values */
  548. if (last_index < 63 || run != 0)
  549. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  550. }
  551. void mjpeg_encode_mb(MpegEncContext *s,
  552. DCTELEM block[6][64])
  553. {
  554. int i;
  555. for(i=0;i<6;i++) {
  556. encode_block(s, block[i], i);
  557. }
  558. }
  559. /******************************************/
  560. /* decoding */
  561. #define MAX_COMPONENTS 4
  562. typedef struct MJpegDecodeContext {
  563. AVCodecContext *avctx;
  564. GetBitContext gb;
  565. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  566. int start_code; /* current start code */
  567. int buffer_size;
  568. uint8_t *buffer;
  569. int16_t quant_matrixes[4][64];
  570. VLC vlcs[2][4];
  571. int org_width, org_height; /* size given at codec init */
  572. int first_picture; /* true if decoding first picture */
  573. int interlaced; /* true if interlaced */
  574. int bottom_field; /* true if bottom field */
  575. int lossless;
  576. int rgb;
  577. int rct; /* pegasus reversible colorspace transform */
  578. int bits; /* bits per component */
  579. int width, height;
  580. int nb_components;
  581. int component_id[MAX_COMPONENTS];
  582. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  583. int v_count[MAX_COMPONENTS];
  584. int h_max, v_max; /* maximum h and v counts */
  585. int quant_index[4]; /* quant table index for each component */
  586. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  587. uint8_t *current_picture[MAX_COMPONENTS]; /* picture structure */
  588. int linesize[MAX_COMPONENTS];
  589. DCTELEM block[64] __align8;
  590. ScanTable scantable;
  591. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  592. int restart_interval;
  593. int restart_count;
  594. int buggy_avid;
  595. int interlace_polarity;
  596. } MJpegDecodeContext;
  597. static int mjpeg_decode_dht(MJpegDecodeContext *s);
  598. static void build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
  599. int nb_codes)
  600. {
  601. uint8_t huff_size[256];
  602. uint16_t huff_code[256];
  603. memset(huff_size, 0, sizeof(huff_size));
  604. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  605. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  606. }
  607. static int mjpeg_decode_init(AVCodecContext *avctx)
  608. {
  609. MJpegDecodeContext *s = avctx->priv_data;
  610. MpegEncContext s2;
  611. s->avctx = avctx;
  612. /* ugly way to get the idct & scantable FIXME */
  613. memset(&s2, 0, sizeof(MpegEncContext));
  614. s2.flags= avctx->flags;
  615. s2.avctx= avctx;
  616. // s2->out_format = FMT_MJPEG;
  617. s2.width = 8;
  618. s2.height = 8;
  619. if (MPV_common_init(&s2) < 0)
  620. return -1;
  621. s->scantable= s2.intra_scantable;
  622. s->idct_put= s2.dsp.idct_put;
  623. MPV_common_end(&s2);
  624. s->mpeg_enc_ctx_allocated = 0;
  625. s->buffer_size = 102400; /* smaller buffer should be enough,
  626. but photojpg files could ahive bigger sizes */
  627. s->buffer = av_malloc(s->buffer_size);
  628. if (!s->buffer)
  629. return -1;
  630. s->start_code = -1;
  631. s->first_picture = 1;
  632. s->org_width = avctx->width;
  633. s->org_height = avctx->height;
  634. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  635. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  636. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  637. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  638. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  639. {
  640. printf("mjpeg: using external huffman table\n");
  641. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
  642. mjpeg_decode_dht(s);
  643. /* should check for error - but dunno */
  644. }
  645. return 0;
  646. }
  647. /* quantize tables */
  648. static int mjpeg_decode_dqt(MJpegDecodeContext *s)
  649. {
  650. int len, index, i, j;
  651. len = get_bits(&s->gb, 16) - 2;
  652. while (len >= 65) {
  653. /* only 8 bit precision handled */
  654. if (get_bits(&s->gb, 4) != 0)
  655. {
  656. dprintf("dqt: 16bit precision\n");
  657. return -1;
  658. }
  659. index = get_bits(&s->gb, 4);
  660. if (index >= 4)
  661. return -1;
  662. dprintf("index=%d\n", index);
  663. /* read quant table */
  664. for(i=0;i<64;i++) {
  665. j = s->scantable.permutated[i];
  666. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  667. }
  668. len -= 65;
  669. }
  670. return 0;
  671. }
  672. /* decode huffman tables and build VLC decoders */
  673. static int mjpeg_decode_dht(MJpegDecodeContext *s)
  674. {
  675. int len, index, i, class, n, v, code_max;
  676. uint8_t bits_table[17];
  677. uint8_t val_table[256];
  678. len = get_bits(&s->gb, 16) - 2;
  679. while (len > 0) {
  680. if (len < 17)
  681. return -1;
  682. class = get_bits(&s->gb, 4);
  683. if (class >= 2)
  684. return -1;
  685. index = get_bits(&s->gb, 4);
  686. if (index >= 4)
  687. return -1;
  688. n = 0;
  689. for(i=1;i<=16;i++) {
  690. bits_table[i] = get_bits(&s->gb, 8);
  691. n += bits_table[i];
  692. }
  693. len -= 17;
  694. if (len < n || n > 256)
  695. return -1;
  696. code_max = 0;
  697. for(i=0;i<n;i++) {
  698. v = get_bits(&s->gb, 8);
  699. if (v > code_max)
  700. code_max = v;
  701. val_table[i] = v;
  702. }
  703. len -= n;
  704. /* build VLC and flush previous vlc if present */
  705. free_vlc(&s->vlcs[class][index]);
  706. dprintf("class=%d index=%d nb_codes=%d\n",
  707. class, index, code_max + 1);
  708. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  709. }
  710. return 0;
  711. }
  712. static int mjpeg_decode_sof(MJpegDecodeContext *s)
  713. {
  714. int len, nb_components, i, width, height;
  715. /* XXX: verify len field validity */
  716. len = get_bits(&s->gb, 16);
  717. s->bits= get_bits(&s->gb, 8);
  718. if(s->rct) s->bits=9;
  719. if (s->bits != 8 && !s->lossless){
  720. printf("only 8 bits/component accepted\n");
  721. return -1;
  722. }
  723. height = get_bits(&s->gb, 16);
  724. width = get_bits(&s->gb, 16);
  725. dprintf("sof0: picture: %dx%d\n", width, height);
  726. nb_components = get_bits(&s->gb, 8);
  727. if (nb_components <= 0 ||
  728. nb_components > MAX_COMPONENTS)
  729. return -1;
  730. s->nb_components = nb_components;
  731. s->h_max = 1;
  732. s->v_max = 1;
  733. for(i=0;i<nb_components;i++) {
  734. /* component id */
  735. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  736. s->h_count[i] = get_bits(&s->gb, 4);
  737. s->v_count[i] = get_bits(&s->gb, 4);
  738. /* compute hmax and vmax (only used in interleaved case) */
  739. if (s->h_count[i] > s->h_max)
  740. s->h_max = s->h_count[i];
  741. if (s->v_count[i] > s->v_max)
  742. s->v_max = s->v_count[i];
  743. s->quant_index[i] = get_bits(&s->gb, 8);
  744. if (s->quant_index[i] >= 4)
  745. return -1;
  746. dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  747. s->v_count[i], s->component_id[i], s->quant_index[i]);
  748. }
  749. if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
  750. /* if different size, realloc/alloc picture */
  751. /* XXX: also check h_count and v_count */
  752. if (width != s->width || height != s->height) {
  753. for(i=0;i<MAX_COMPONENTS;i++)
  754. av_freep(&s->current_picture[i]);
  755. s->width = width;
  756. s->height = height;
  757. /* test interlaced mode */
  758. if (s->first_picture &&
  759. s->org_height != 0 &&
  760. s->height < ((s->org_height * 3) / 4)) {
  761. s->interlaced = 1;
  762. // s->bottom_field = (s->interlace_polarity) ? 1 : 0;
  763. s->bottom_field = 0;
  764. }
  765. if(s->rgb){
  766. int w, h;
  767. w = s->width;
  768. h = s->height;
  769. if (s->interlaced)
  770. w *= 2;
  771. s->linesize[0] = 4*w;
  772. s->current_picture[0] = av_mallocz(4*w * h);
  773. s->current_picture[1] = s->current_picture[2] = NULL;
  774. }else{
  775. for(i=0;i<nb_components;i++) {
  776. int w, h;
  777. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  778. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  779. w = w * 8 * s->h_count[i];
  780. h = h * 8 * s->v_count[i];
  781. if (s->interlaced)
  782. w *= 2;
  783. s->linesize[i] = w;
  784. s->current_picture[i] = av_mallocz(w * h);
  785. if (!s->current_picture[i])
  786. {
  787. dprintf("error: no picture buffers allocated\n");
  788. return -1;
  789. }
  790. }
  791. }
  792. s->first_picture = 0;
  793. }
  794. if (len != (8+(3*nb_components)))
  795. {
  796. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  797. }
  798. return 0;
  799. }
  800. static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
  801. {
  802. int code;
  803. code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
  804. if (code < 0)
  805. {
  806. dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
  807. &s->vlcs[0][dc_index]);
  808. return 0xffff;
  809. }
  810. if(code)
  811. return get_xbits(&s->gb, code);
  812. else
  813. return 0;
  814. }
  815. /* decode block and dequantize */
  816. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  817. int component, int dc_index, int ac_index, int quant_index)
  818. {
  819. int code, i, j, level, val;
  820. VLC *ac_vlc;
  821. int16_t *quant_matrix;
  822. /* DC coef */
  823. val = mjpeg_decode_dc(s, dc_index);
  824. if (val == 0xffff) {
  825. dprintf("error dc\n");
  826. return -1;
  827. }
  828. quant_matrix = s->quant_matrixes[quant_index];
  829. val = val * quant_matrix[0] + s->last_dc[component];
  830. s->last_dc[component] = val;
  831. block[0] = val;
  832. /* AC coefs */
  833. ac_vlc = &s->vlcs[1][ac_index];
  834. i = 1;
  835. for(;;) {
  836. code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);
  837. if (code < 0) {
  838. dprintf("error ac\n");
  839. return -1;
  840. }
  841. /* EOB */
  842. if (code == 0)
  843. break;
  844. if (code == 0xf0) {
  845. i += 16;
  846. } else {
  847. level = get_xbits(&s->gb, code & 0xf);
  848. i += code >> 4;
  849. if (i >= 64) {
  850. dprintf("error count: %d\n", i);
  851. return -1;
  852. }
  853. j = s->scantable.permutated[i];
  854. block[j] = level * quant_matrix[j];
  855. i++;
  856. if (i >= 64)
  857. break;
  858. }
  859. }
  860. return 0;
  861. }
  862. static void decode_lossless(MJpegDecodeContext *s, int point_transform, int predictor){
  863. }
  864. static int mjpeg_decode_sos(MJpegDecodeContext *s)
  865. {
  866. int len, nb_components, i, j, n, h, v, ret, point_transform, predictor;
  867. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  868. int comp_index[4];
  869. int dc_index[4];
  870. int ac_index[4];
  871. int nb_blocks[4];
  872. int h_count[4];
  873. int v_count[4];
  874. const int block_size= s->lossless ? 1 : 8;
  875. /* XXX: verify len field validity */
  876. len = get_bits(&s->gb, 16);
  877. nb_components = get_bits(&s->gb, 8);
  878. if (len != 6+2*nb_components)
  879. {
  880. dprintf("decode_sos: invalid len (%d)\n", len);
  881. return -1;
  882. }
  883. /* XXX: only interleaved scan accepted */
  884. if (nb_components != 3)
  885. {
  886. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  887. return -1;
  888. }
  889. vmax = 0;
  890. hmax = 0;
  891. for(i=0;i<nb_components;i++) {
  892. id = get_bits(&s->gb, 8) - 1;
  893. dprintf("component: %d\n", id);
  894. /* find component index */
  895. for(index=0;index<s->nb_components;index++)
  896. if (id == s->component_id[index])
  897. break;
  898. if (index == s->nb_components)
  899. {
  900. dprintf("decode_sos: index(%d) out of components\n", index);
  901. return -1;
  902. }
  903. comp_index[i] = index;
  904. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  905. h_count[i] = s->h_count[index];
  906. v_count[i] = s->v_count[index];
  907. dc_index[i] = get_bits(&s->gb, 4);
  908. ac_index[i] = get_bits(&s->gb, 4);
  909. if (dc_index[i] < 0 || ac_index[i] < 0 ||
  910. dc_index[i] >= 4 || ac_index[i] >= 4)
  911. goto out_of_range;
  912. switch(s->start_code)
  913. {
  914. case SOF0:
  915. if (dc_index[i] > 1 || ac_index[i] > 1)
  916. goto out_of_range;
  917. break;
  918. case SOF1:
  919. case SOF2:
  920. if (dc_index[i] > 3 || ac_index[i] > 3)
  921. goto out_of_range;
  922. break;
  923. case SOF3:
  924. if (dc_index[i] > 3 || ac_index[i] != 0)
  925. goto out_of_range;
  926. break;
  927. }
  928. }
  929. predictor= get_bits(&s->gb, 8); /* lossless predictor or start of spectral (Ss) */
  930. skip_bits(&s->gb, 8); /* Se */
  931. skip_bits(&s->gb, 4); /* Ah */
  932. point_transform= get_bits(&s->gb, 4); /* Al */
  933. for(i=0;i<nb_components;i++)
  934. s->last_dc[i] = 1024;
  935. if (nb_components > 1) {
  936. /* interleaved stream */
  937. mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
  938. mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
  939. } else {
  940. h = s->h_max / s->h_count[comp_index[0]];
  941. v = s->v_max / s->v_count[comp_index[0]];
  942. mb_width = (s->width + h * block_size - 1) / (h * block_size);
  943. mb_height = (s->height + v * block_size - 1) / (v * block_size);
  944. nb_blocks[0] = 1;
  945. h_count[0] = 1;
  946. v_count[0] = 1;
  947. }
  948. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  949. printf("%s %s p:%d >>:%d\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "", predictor, point_transform);
  950. if(s->lossless){
  951. if(s->rgb){
  952. uint16_t buffer[2048][4];
  953. int left[3], top[3], topleft[3];
  954. const int linesize= s->linesize[0];
  955. const int mask= (1<<s->bits)-1;
  956. for(i=0; i<3; i++){
  957. buffer[0][i]= 1 << (s->bits + point_transform - 1);
  958. }
  959. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  960. const int modified_predictor= mb_y ? 1 : predictor;
  961. uint8_t *ptr = s->current_picture[0] + (linesize * mb_y);
  962. if (s->interlaced && s->bottom_field)
  963. ptr += linesize >> 1;
  964. for(i=0; i<3; i++){
  965. top[i]= left[i]= topleft[i]= buffer[0][i];
  966. }
  967. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  968. if (s->restart_interval && !s->restart_count)
  969. s->restart_count = s->restart_interval;
  970. for(i=0;i<3;i++) {
  971. int pred;
  972. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  973. topleft[i]= top[i];
  974. top[i]= buffer[mb_x][i];
  975. left[i]=
  976. buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, dc_index[i]) << point_transform));
  977. }
  978. if (s->restart_interval && !--s->restart_count) {
  979. align_get_bits(&s->gb);
  980. skip_bits(&s->gb, 16); /* skip RSTn */
  981. }
  982. }
  983. if(s->rct){
  984. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  985. ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
  986. ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
  987. ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
  988. }
  989. }else{
  990. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  991. ptr[4*mb_x+0] = buffer[mb_x][0];
  992. ptr[4*mb_x+1] = buffer[mb_x][1];
  993. ptr[4*mb_x+2] = buffer[mb_x][2];
  994. }
  995. }
  996. }
  997. }else{
  998. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  999. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  1000. if (s->restart_interval && !s->restart_count)
  1001. s->restart_count = s->restart_interval;
  1002. if(mb_x==0 || mb_y==0 || s->interlaced){
  1003. for(i=0;i<nb_components;i++) {
  1004. uint8_t *ptr;
  1005. int x, y, c, linesize;
  1006. n = nb_blocks[i];
  1007. c = comp_index[i];
  1008. h = h_count[i];
  1009. v = v_count[i];
  1010. x = 0;
  1011. y = 0;
  1012. linesize= s->linesize[c];
  1013. for(j=0; j<n; j++) {
  1014. int pred;
  1015. ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  1016. if(y==0 && mb_y==0){
  1017. if(x==0 && mb_x==0){
  1018. pred= 128 << point_transform;
  1019. }else{
  1020. pred= ptr[-1];
  1021. }
  1022. }else{
  1023. if(x==0 && mb_x==0){
  1024. pred= ptr[-linesize];
  1025. }else{
  1026. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  1027. }
  1028. }
  1029. if (s->interlaced && s->bottom_field)
  1030. ptr += linesize >> 1;
  1031. *ptr= pred + (mjpeg_decode_dc(s, dc_index[i]) << point_transform);
  1032. if (++x == h) {
  1033. x = 0;
  1034. y++;
  1035. }
  1036. }
  1037. }
  1038. }else{
  1039. for(i=0;i<nb_components;i++) {
  1040. uint8_t *ptr;
  1041. int x, y, c, linesize;
  1042. n = nb_blocks[i];
  1043. c = comp_index[i];
  1044. h = h_count[i];
  1045. v = v_count[i];
  1046. x = 0;
  1047. y = 0;
  1048. linesize= s->linesize[c];
  1049. for(j=0; j<n; j++) {
  1050. int pred;
  1051. ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  1052. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  1053. *ptr= pred + (mjpeg_decode_dc(s, dc_index[i]) << point_transform);
  1054. if (++x == h) {
  1055. x = 0;
  1056. y++;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. if (s->restart_interval && !--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. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1142. printf("APPx %8X\n", id);
  1143. }
  1144. /* buggy AVID, it puts EOI only at every 10th frame */
  1145. /* also this fourcc is used by non-avid files too, it holds some
  1146. informations, but it's always present in AVID creates files */
  1147. if (id == ff_get_fourcc("AVI1"))
  1148. {
  1149. /* structure:
  1150. 4bytes AVI1
  1151. 1bytes polarity
  1152. 1bytes always zero
  1153. 4bytes field_size
  1154. 4bytes field_size_less_padding
  1155. */
  1156. s->buggy_avid = 1;
  1157. // if (s->first_picture)
  1158. // printf("mjpeg: workarounding buggy AVID\n");
  1159. s->interlace_polarity = get_bits(&s->gb, 8);
  1160. #if 0
  1161. skip_bits(&s->gb, 8);
  1162. skip_bits(&s->gb, 32);
  1163. skip_bits(&s->gb, 32);
  1164. len -= 10;
  1165. #endif
  1166. // if (s->interlace_polarity)
  1167. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  1168. goto out;
  1169. }
  1170. // len -= 2;
  1171. if (id == ff_get_fourcc("JFIF"))
  1172. {
  1173. int t_w, t_h;
  1174. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  1175. printf("mjpeg: JFIF header found (version: %x.%x)\n",
  1176. get_bits(&s->gb, 8), get_bits(&s->gb, 8));
  1177. if (get_bits(&s->gb, 8) == 0)
  1178. {
  1179. int x_density, y_density;
  1180. x_density = get_bits(&s->gb, 16);
  1181. y_density = get_bits(&s->gb, 16);
  1182. dprintf("x/y density: %d (%f), %d (%f)\n", x_density,
  1183. (float)x_density, y_density, (float)y_density);
  1184. #if 0
  1185. //MN: needs to be checked
  1186. if(x_density)
  1187. // s->avctx->aspect_ratio= s->width*y_density/((float)s->height*x_density);
  1188. s->avctx->aspect_ratio = (float)x_density/y_density;
  1189. /* it's better, but every JFIF I have seen stores 1:1 */
  1190. else
  1191. s->avctx->aspect_ratio= 0.0;
  1192. #endif
  1193. }
  1194. else
  1195. {
  1196. skip_bits(&s->gb, 16);
  1197. skip_bits(&s->gb, 16);
  1198. }
  1199. t_w = get_bits(&s->gb, 8);
  1200. t_h = get_bits(&s->gb, 8);
  1201. if (t_w && t_h)
  1202. {
  1203. /* skip thumbnail */
  1204. if (len-10-(t_w*t_h*3) > 0)
  1205. len -= t_w*t_h*3;
  1206. }
  1207. len -= 10;
  1208. goto out;
  1209. }
  1210. if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
  1211. {
  1212. printf("mjpeg: Adobe header found\n");
  1213. skip_bits(&s->gb, 16); /* version */
  1214. skip_bits(&s->gb, 16); /* flags0 */
  1215. skip_bits(&s->gb, 16); /* flags1 */
  1216. skip_bits(&s->gb, 8); /* transform */
  1217. len -= 7;
  1218. goto out;
  1219. }
  1220. if (id == ff_get_fourcc("LJIF")){
  1221. printf("Pegasus lossless jpeg header found\n");
  1222. int i;
  1223. skip_bits(&s->gb, 16); /* version ? */
  1224. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1225. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1226. skip_bits(&s->gb, 16); /* unknwon always 0? */
  1227. switch( get_bits(&s->gb, 8)){
  1228. case 1:
  1229. s->rgb= 1;
  1230. s->rct=0;
  1231. break;
  1232. case 2:
  1233. s->rgb= 1;
  1234. s->rct=1;
  1235. break;
  1236. default:
  1237. printf("unknown colorspace\n");
  1238. }
  1239. len -= 9;
  1240. goto out;
  1241. }
  1242. /* Apple MJPEG-A */
  1243. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1244. {
  1245. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1246. id = be2me_32(id);
  1247. len -= 4;
  1248. if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
  1249. {
  1250. #if 0
  1251. skip_bits(&s->gb, 32); /* field size */
  1252. skip_bits(&s->gb, 32); /* pad field size */
  1253. skip_bits(&s->gb, 32); /* next off */
  1254. skip_bits(&s->gb, 32); /* quant off */
  1255. skip_bits(&s->gb, 32); /* huff off */
  1256. skip_bits(&s->gb, 32); /* image off */
  1257. skip_bits(&s->gb, 32); /* scan off */
  1258. skip_bits(&s->gb, 32); /* data off */
  1259. #endif
  1260. if (s->first_picture)
  1261. printf("mjpeg: Apple MJPEG-A header found\n");
  1262. }
  1263. }
  1264. out:
  1265. /* slow but needed for extreme adobe jpegs */
  1266. if (len < 0)
  1267. printf("mjpeg: error, decode_app parser read over the end\n");
  1268. while(--len > 0)
  1269. skip_bits(&s->gb, 8);
  1270. return 0;
  1271. }
  1272. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1273. {
  1274. /* XXX: verify len field validity */
  1275. unsigned int len = get_bits(&s->gb, 16);
  1276. if (len >= 2 && len < 32768) {
  1277. /* XXX: any better upper bound */
  1278. uint8_t *cbuf = av_malloc(len - 1);
  1279. if (cbuf) {
  1280. int i;
  1281. for (i = 0; i < len - 2; i++)
  1282. cbuf[i] = get_bits(&s->gb, 8);
  1283. if (i > 0 && cbuf[i-1] == '\n')
  1284. cbuf[i-1] = 0;
  1285. else
  1286. cbuf[i] = 0;
  1287. printf("mjpeg comment: '%s'\n", cbuf);
  1288. /* buggy avid, it puts EOI only at every 10th frame */
  1289. if (!strcmp(cbuf, "AVID"))
  1290. {
  1291. s->buggy_avid = 1;
  1292. // if (s->first_picture)
  1293. // printf("mjpeg: workarounding buggy AVID\n");
  1294. }
  1295. av_free(cbuf);
  1296. }
  1297. }
  1298. return 0;
  1299. }
  1300. #if 0
  1301. static int valid_marker_list[] =
  1302. {
  1303. /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
  1304. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1305. /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1306. /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1307. /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1308. /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1309. /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1310. /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1311. /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1312. /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1313. /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1314. /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1315. /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1316. /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1317. /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1318. /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1319. /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  1320. }
  1321. #endif
  1322. /* return the 8 bit start code value and update the search
  1323. state. Return -1 if no start code found */
  1324. static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
  1325. {
  1326. uint8_t *buf_ptr;
  1327. unsigned int v, v2;
  1328. int val;
  1329. #ifdef DEBUG
  1330. int skipped=0;
  1331. #endif
  1332. buf_ptr = *pbuf_ptr;
  1333. while (buf_ptr < buf_end) {
  1334. v = *buf_ptr++;
  1335. v2 = *buf_ptr;
  1336. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe)) {
  1337. val = *buf_ptr++;
  1338. goto found;
  1339. }
  1340. #ifdef DEBUG
  1341. skipped++;
  1342. #endif
  1343. }
  1344. val = -1;
  1345. found:
  1346. #ifdef DEBUG
  1347. dprintf("find_marker skipped %d bytes\n", skipped);
  1348. #endif
  1349. *pbuf_ptr = buf_ptr;
  1350. return val;
  1351. }
  1352. static int mjpeg_decode_frame(AVCodecContext *avctx,
  1353. void *data, int *data_size,
  1354. uint8_t *buf, int buf_size)
  1355. {
  1356. MJpegDecodeContext *s = avctx->priv_data;
  1357. uint8_t *buf_end, *buf_ptr;
  1358. int i, start_code;
  1359. AVPicture *picture = data;
  1360. *data_size = 0;
  1361. /* no supplementary picture */
  1362. if (buf_size == 0)
  1363. return 0;
  1364. buf_ptr = buf;
  1365. buf_end = buf + buf_size;
  1366. while (buf_ptr < buf_end) {
  1367. /* find start next marker */
  1368. start_code = find_marker(&buf_ptr, buf_end);
  1369. {
  1370. /* EOF */
  1371. if (start_code < 0) {
  1372. goto the_end;
  1373. } else {
  1374. dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
  1375. if ((buf_end - buf_ptr) > s->buffer_size)
  1376. {
  1377. av_free(s->buffer);
  1378. s->buffer_size = buf_end-buf_ptr;
  1379. s->buffer = av_malloc(s->buffer_size);
  1380. dprintf("buffer too small, expanding to %d bytes\n",
  1381. s->buffer_size);
  1382. }
  1383. /* unescape buffer of SOS */
  1384. if (start_code == SOS)
  1385. {
  1386. uint8_t *src = buf_ptr;
  1387. uint8_t *dst = s->buffer;
  1388. while (src<buf_end)
  1389. {
  1390. uint8_t x = *(src++);
  1391. *(dst++) = x;
  1392. if (x == 0xff)
  1393. {
  1394. while(*src == 0xff) src++;
  1395. x = *(src++);
  1396. if (x >= 0xd0 && x <= 0xd7)
  1397. *(dst++) = x;
  1398. else if (x)
  1399. break;
  1400. }
  1401. }
  1402. init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
  1403. dprintf("escaping removed %d bytes\n",
  1404. (buf_end - buf_ptr) - (dst - s->buffer));
  1405. }
  1406. else
  1407. init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
  1408. s->start_code = start_code;
  1409. if(s->avctx->debug & FF_DEBUG_STARTCODE){
  1410. printf("startcode: %X\n", start_code);
  1411. }
  1412. /* process markers */
  1413. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1414. dprintf("restart marker: %d\n", start_code&0x0f);
  1415. } else if (s->first_picture) {
  1416. /* APP fields */
  1417. if (start_code >= 0xe0 && start_code <= 0xef)
  1418. mjpeg_decode_app(s);
  1419. /* Comment */
  1420. else if (start_code == COM)
  1421. mjpeg_decode_com(s);
  1422. }
  1423. switch(start_code) {
  1424. case SOI:
  1425. s->restart_interval = 0;
  1426. /* nothing to do on SOI */
  1427. break;
  1428. case DQT:
  1429. mjpeg_decode_dqt(s);
  1430. break;
  1431. case DHT:
  1432. mjpeg_decode_dht(s);
  1433. break;
  1434. case SOF0:
  1435. s->lossless=0;
  1436. if (mjpeg_decode_sof(s) < 0)
  1437. return -1;
  1438. break;
  1439. case SOF3:
  1440. s->lossless=1;
  1441. if (mjpeg_decode_sof(s) < 0)
  1442. return -1;
  1443. break;
  1444. case EOI:
  1445. eoi_parser:
  1446. {
  1447. if (s->interlaced) {
  1448. s->bottom_field ^= 1;
  1449. /* if not bottom field, do not output image yet */
  1450. if (s->bottom_field)
  1451. goto not_the_end;
  1452. }
  1453. for(i=0;i<3;i++) {
  1454. picture->data[i] = s->current_picture[i];
  1455. picture->linesize[i] = (s->interlaced) ?
  1456. s->linesize[i] >> 1 : s->linesize[i];
  1457. }
  1458. *data_size = sizeof(AVPicture);
  1459. avctx->height = s->height;
  1460. if (s->interlaced)
  1461. avctx->height *= 2;
  1462. avctx->width = s->width;
  1463. /* XXX: not complete test ! */
  1464. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1465. case 0x11:
  1466. if(s->rgb){
  1467. #ifdef WORDS_BIGENDIAN
  1468. avctx->pix_fmt = PIX_FMT_ABGR32;
  1469. #else
  1470. avctx->pix_fmt = PIX_FMT_RGBA32;
  1471. #endif
  1472. }else
  1473. avctx->pix_fmt = PIX_FMT_YUV444P;
  1474. break;
  1475. case 0x21:
  1476. avctx->pix_fmt = PIX_FMT_YUV422P;
  1477. break;
  1478. default:
  1479. case 0x22:
  1480. avctx->pix_fmt = PIX_FMT_YUV420P;
  1481. break;
  1482. }
  1483. /* dummy quality */
  1484. /* XXX: infer it with matrix */
  1485. // avctx->quality = 3;
  1486. goto the_end;
  1487. }
  1488. break;
  1489. case SOS:
  1490. mjpeg_decode_sos(s);
  1491. /* buggy avid puts EOI every 10-20th frame */
  1492. /* if restart period is over process EOI */
  1493. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1494. goto eoi_parser;
  1495. break;
  1496. case DRI:
  1497. mjpeg_decode_dri(s);
  1498. break;
  1499. case SOF1:
  1500. case SOF2:
  1501. case SOF5:
  1502. case SOF6:
  1503. case SOF7:
  1504. case SOF9:
  1505. case SOF10:
  1506. case SOF11:
  1507. case SOF13:
  1508. case SOF14:
  1509. case SOF15:
  1510. case JPG:
  1511. printf("mjpeg: unsupported coding type (%x)\n", start_code);
  1512. break;
  1513. // default:
  1514. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1515. // break;
  1516. }
  1517. not_the_end:
  1518. /* eof process start code */
  1519. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1520. dprintf("marker parser used %d bytes (%d bits)\n",
  1521. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1522. }
  1523. }
  1524. }
  1525. the_end:
  1526. dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
  1527. // return buf_end - buf_ptr;
  1528. return buf_ptr - buf;
  1529. }
  1530. static int mjpegb_decode_frame(AVCodecContext *avctx,
  1531. void *data, int *data_size,
  1532. uint8_t *buf, int buf_size)
  1533. {
  1534. MJpegDecodeContext *s = avctx->priv_data;
  1535. uint8_t *buf_end, *buf_ptr;
  1536. int i;
  1537. AVPicture *picture = data;
  1538. GetBitContext hgb; /* for the header */
  1539. uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
  1540. uint32_t field_size;
  1541. *data_size = 0;
  1542. /* no supplementary picture */
  1543. if (buf_size == 0)
  1544. return 0;
  1545. buf_ptr = buf;
  1546. buf_end = buf + buf_size;
  1547. read_header:
  1548. /* reset on every SOI */
  1549. s->restart_interval = 0;
  1550. init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
  1551. skip_bits(&hgb, 32); /* reserved zeros */
  1552. if (get_bits(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
  1553. {
  1554. dprintf("not mjpeg-b (bad fourcc)\n");
  1555. return 0;
  1556. }
  1557. field_size = get_bits(&hgb, 32); /* field size */
  1558. dprintf("field size: 0x%x\n", field_size);
  1559. skip_bits(&hgb, 32); /* padded field size */
  1560. second_field_offs = get_bits(&hgb, 32);
  1561. dprintf("second field offs: 0x%x\n", second_field_offs);
  1562. if (second_field_offs)
  1563. s->interlaced = 1;
  1564. dqt_offs = get_bits(&hgb, 32);
  1565. dprintf("dqt offs: 0x%x\n", dqt_offs);
  1566. if (dqt_offs)
  1567. {
  1568. init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
  1569. s->start_code = DQT;
  1570. mjpeg_decode_dqt(s);
  1571. }
  1572. dht_offs = get_bits(&hgb, 32);
  1573. dprintf("dht offs: 0x%x\n", dht_offs);
  1574. if (dht_offs)
  1575. {
  1576. init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
  1577. s->start_code = DHT;
  1578. mjpeg_decode_dht(s);
  1579. }
  1580. sof_offs = get_bits(&hgb, 32);
  1581. dprintf("sof offs: 0x%x\n", sof_offs);
  1582. if (sof_offs)
  1583. {
  1584. init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
  1585. s->start_code = SOF0;
  1586. if (mjpeg_decode_sof(s) < 0)
  1587. return -1;
  1588. }
  1589. sos_offs = get_bits(&hgb, 32);
  1590. dprintf("sos offs: 0x%x\n", sos_offs);
  1591. if (sos_offs)
  1592. {
  1593. // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
  1594. init_get_bits(&s->gb, buf+sos_offs, field_size*8);
  1595. s->start_code = SOS;
  1596. mjpeg_decode_sos(s);
  1597. }
  1598. skip_bits(&hgb, 32); /* start of data offset */
  1599. if (s->interlaced) {
  1600. s->bottom_field ^= 1;
  1601. /* if not bottom field, do not output image yet */
  1602. if (s->bottom_field && second_field_offs)
  1603. {
  1604. buf_ptr = buf + second_field_offs;
  1605. second_field_offs = 0;
  1606. goto read_header;
  1607. }
  1608. }
  1609. for(i=0;i<3;i++) {
  1610. picture->data[i] = s->current_picture[i];
  1611. picture->linesize[i] = (s->interlaced) ?
  1612. s->linesize[i] >> 1 : s->linesize[i];
  1613. }
  1614. *data_size = sizeof(AVPicture);
  1615. avctx->height = s->height;
  1616. if (s->interlaced)
  1617. avctx->height *= 2;
  1618. avctx->width = s->width;
  1619. /* XXX: not complete test ! */
  1620. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1621. case 0x11:
  1622. avctx->pix_fmt = PIX_FMT_YUV444P;
  1623. break;
  1624. case 0x21:
  1625. avctx->pix_fmt = PIX_FMT_YUV422P;
  1626. break;
  1627. default:
  1628. case 0x22:
  1629. avctx->pix_fmt = PIX_FMT_YUV420P;
  1630. break;
  1631. }
  1632. /* dummy quality */
  1633. /* XXX: infer it with matrix */
  1634. // avctx->quality = 3;
  1635. return buf_ptr - buf;
  1636. }
  1637. static int mjpeg_decode_end(AVCodecContext *avctx)
  1638. {
  1639. MJpegDecodeContext *s = avctx->priv_data;
  1640. int i, j;
  1641. av_free(s->buffer);
  1642. for(i=0;i<MAX_COMPONENTS;i++)
  1643. av_free(s->current_picture[i]);
  1644. for(i=0;i<2;i++) {
  1645. for(j=0;j<4;j++)
  1646. free_vlc(&s->vlcs[i][j]);
  1647. }
  1648. return 0;
  1649. }
  1650. AVCodec mjpeg_decoder = {
  1651. "mjpeg",
  1652. CODEC_TYPE_VIDEO,
  1653. CODEC_ID_MJPEG,
  1654. sizeof(MJpegDecodeContext),
  1655. mjpeg_decode_init,
  1656. NULL,
  1657. mjpeg_decode_end,
  1658. mjpeg_decode_frame,
  1659. 0,
  1660. NULL
  1661. };
  1662. AVCodec mjpegb_decoder = {
  1663. "mjpegb",
  1664. CODEC_TYPE_VIDEO,
  1665. CODEC_ID_MJPEGB,
  1666. sizeof(MJpegDecodeContext),
  1667. mjpeg_decode_init,
  1668. NULL,
  1669. mjpeg_decode_end,
  1670. mjpegb_decode_frame,
  1671. 0,
  1672. NULL
  1673. };