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.

1697 lines
47KB

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