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.

1699 lines
48KB

  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. #define MJPEG_VERSION "FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR
  357. put_string(p, MJPEG_VERSION);
  358. size = strlen(MJPEG_VERSION)+3;
  359. #undef MJPEG_VERSION
  360. ptr[0] = size >> 8;
  361. ptr[1] = size;
  362. }
  363. }
  364. void mjpeg_picture_header(MpegEncContext *s)
  365. {
  366. put_marker(&s->pb, SOI);
  367. if (!s->mjpeg_data_only_frames)
  368. {
  369. jpeg_put_comments(s);
  370. if (s->mjpeg_write_tables) jpeg_table_header(s);
  371. put_marker(&s->pb, SOF0);
  372. put_bits(&s->pb, 16, 17);
  373. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  374. put_bits(&s->pb, 16, s->height);
  375. put_bits(&s->pb, 16, s->width);
  376. put_bits(&s->pb, 8, 3); /* 3 components */
  377. /* Y component */
  378. put_bits(&s->pb, 8, 1); /* component number */
  379. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  380. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  381. put_bits(&s->pb, 8, 0); /* select matrix */
  382. /* Cb component */
  383. put_bits(&s->pb, 8, 2); /* component number */
  384. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  385. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  386. #ifdef TWOMATRIXES
  387. put_bits(&s->pb, 8, 1); /* select matrix */
  388. #else
  389. put_bits(&s->pb, 8, 0); /* select matrix */
  390. #endif
  391. /* Cr component */
  392. put_bits(&s->pb, 8, 3); /* component number */
  393. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  394. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  395. #ifdef TWOMATRIXES
  396. put_bits(&s->pb, 8, 1); /* select matrix */
  397. #else
  398. put_bits(&s->pb, 8, 0); /* select matrix */
  399. #endif
  400. }
  401. /* scan header */
  402. put_marker(&s->pb, SOS);
  403. put_bits(&s->pb, 16, 12); /* length */
  404. put_bits(&s->pb, 8, 3); /* 3 components */
  405. /* Y component */
  406. put_bits(&s->pb, 8, 1); /* index */
  407. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  408. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  409. /* Cb component */
  410. put_bits(&s->pb, 8, 2); /* index */
  411. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  412. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  413. /* Cr component */
  414. put_bits(&s->pb, 8, 3); /* index */
  415. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  416. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  417. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  418. put_bits(&s->pb, 8, 63); /* Se (not used) */
  419. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  420. }
  421. static void escape_FF(MpegEncContext *s, int start)
  422. {
  423. int size= get_bit_count(&s->pb) - start*8;
  424. int i, ff_count;
  425. uint8_t *buf= s->pb.buf + start;
  426. int align= (-(int)(buf))&3;
  427. assert((size&7) == 0);
  428. size >>= 3;
  429. ff_count=0;
  430. for(i=0; i<size && i<align; i++){
  431. if(buf[i]==0xFF) ff_count++;
  432. }
  433. for(; i<size-15; i+=16){
  434. int acc, v;
  435. v= *(uint32_t*)(&buf[i]);
  436. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  437. v= *(uint32_t*)(&buf[i+4]);
  438. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  439. v= *(uint32_t*)(&buf[i+8]);
  440. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  441. v= *(uint32_t*)(&buf[i+12]);
  442. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  443. acc>>=4;
  444. acc+= (acc>>16);
  445. acc+= (acc>>8);
  446. ff_count+= acc&0xFF;
  447. }
  448. for(; i<size; i++){
  449. if(buf[i]==0xFF) ff_count++;
  450. }
  451. if(ff_count==0) return;
  452. /* skip put bits */
  453. for(i=0; i<ff_count-3; i+=4)
  454. put_bits(&s->pb, 32, 0);
  455. put_bits(&s->pb, (ff_count-i)*8, 0);
  456. flush_put_bits(&s->pb);
  457. for(i=size-1; ff_count; i--){
  458. int v= buf[i];
  459. if(v==0xFF){
  460. //printf("%d %d\n", i, ff_count);
  461. buf[i+ff_count]= 0;
  462. ff_count--;
  463. }
  464. buf[i+ff_count]= v;
  465. }
  466. }
  467. void mjpeg_picture_trailer(MpegEncContext *s)
  468. {
  469. int pad= (-get_bit_count(&s->pb))&7;
  470. put_bits(&s->pb, pad,0xFF>>(8-pad));
  471. flush_put_bits(&s->pb);
  472. assert((s->header_bits&7)==0);
  473. escape_FF(s, s->header_bits>>3);
  474. put_marker(&s->pb, EOI);
  475. }
  476. static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
  477. uint8_t *huff_size, uint16_t *huff_code)
  478. {
  479. int mant, nbits;
  480. if (val == 0) {
  481. put_bits(&s->pb, huff_size[0], huff_code[0]);
  482. } else {
  483. mant = val;
  484. if (val < 0) {
  485. val = -val;
  486. mant--;
  487. }
  488. /* compute the log (XXX: optimize) */
  489. nbits = 0;
  490. while (val != 0) {
  491. val = val >> 1;
  492. nbits++;
  493. }
  494. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  495. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  496. }
  497. }
  498. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  499. {
  500. int mant, nbits, code, i, j;
  501. int component, dc, run, last_index, val;
  502. MJpegContext *m = s->mjpeg_ctx;
  503. uint8_t *huff_size_ac;
  504. uint16_t *huff_code_ac;
  505. /* DC coef */
  506. component = (n <= 3 ? 0 : n - 4 + 1);
  507. dc = block[0]; /* overflow is impossible */
  508. val = dc - s->last_dc[component];
  509. if (n < 4) {
  510. mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  511. huff_size_ac = m->huff_size_ac_luminance;
  512. huff_code_ac = m->huff_code_ac_luminance;
  513. } else {
  514. mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  515. huff_size_ac = m->huff_size_ac_chrominance;
  516. huff_code_ac = m->huff_code_ac_chrominance;
  517. }
  518. s->last_dc[component] = dc;
  519. /* AC coefs */
  520. run = 0;
  521. last_index = s->block_last_index[n];
  522. for(i=1;i<=last_index;i++) {
  523. j = s->intra_scantable.permutated[i];
  524. val = block[j];
  525. if (val == 0) {
  526. run++;
  527. } else {
  528. while (run >= 16) {
  529. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  530. run -= 16;
  531. }
  532. mant = val;
  533. if (val < 0) {
  534. val = -val;
  535. mant--;
  536. }
  537. /* compute the log (XXX: optimize) */
  538. nbits = 0;
  539. while (val != 0) {
  540. val = val >> 1;
  541. nbits++;
  542. }
  543. code = (run << 4) | nbits;
  544. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  545. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  546. run = 0;
  547. }
  548. }
  549. /* output EOB only if not already 64 values */
  550. if (last_index < 63 || run != 0)
  551. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  552. }
  553. void mjpeg_encode_mb(MpegEncContext *s,
  554. DCTELEM block[6][64])
  555. {
  556. int i;
  557. for(i=0;i<6;i++) {
  558. encode_block(s, block[i], i);
  559. }
  560. }
  561. /******************************************/
  562. /* decoding */
  563. #define MAX_COMPONENTS 4
  564. typedef struct MJpegDecodeContext {
  565. AVCodecContext *avctx;
  566. GetBitContext gb;
  567. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  568. int start_code; /* current start code */
  569. int buffer_size;
  570. uint8_t *buffer;
  571. int16_t quant_matrixes[4][64];
  572. VLC vlcs[2][4];
  573. int org_width, org_height; /* size given at codec init */
  574. int first_picture; /* true if decoding first picture */
  575. int interlaced; /* true if interlaced */
  576. int bottom_field; /* true if bottom field */
  577. int width, height;
  578. int nb_components;
  579. int component_id[MAX_COMPONENTS];
  580. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  581. int v_count[MAX_COMPONENTS];
  582. int h_max, v_max; /* maximum h and v counts */
  583. int quant_index[4]; /* quant table index for each component */
  584. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  585. uint8_t *current_picture[MAX_COMPONENTS]; /* picture structure */
  586. int linesize[MAX_COMPONENTS];
  587. DCTELEM block[64] __align8;
  588. ScanTable scantable;
  589. void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  590. int restart_interval;
  591. int restart_count;
  592. int buggy_avid;
  593. int interlace_polarity;
  594. } MJpegDecodeContext;
  595. static int mjpeg_decode_dht(MJpegDecodeContext *s);
  596. static void build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
  597. int nb_codes)
  598. {
  599. uint8_t huff_size[256];
  600. uint16_t huff_code[256];
  601. memset(huff_size, 0, sizeof(huff_size));
  602. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  603. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  604. }
  605. static int mjpeg_decode_init(AVCodecContext *avctx)
  606. {
  607. MJpegDecodeContext *s = avctx->priv_data;
  608. MpegEncContext s2;
  609. s->avctx = avctx;
  610. /* ugly way to get the idct & scantable FIXME */
  611. memset(&s2, 0, sizeof(MpegEncContext));
  612. s2.flags= avctx->flags;
  613. s2.avctx= avctx;
  614. // s2->out_format = FMT_MJPEG;
  615. s2.width = 8;
  616. s2.height = 8;
  617. if (MPV_common_init(&s2) < 0)
  618. return -1;
  619. s->scantable= s2.intra_scantable;
  620. s->idct_put= s2.dsp.idct_put;
  621. MPV_common_end(&s2);
  622. s->mpeg_enc_ctx_allocated = 0;
  623. s->buffer_size = 102400; /* smaller buffer should be enough,
  624. but photojpg files could ahive bigger sizes */
  625. s->buffer = av_malloc(s->buffer_size);
  626. if (!s->buffer)
  627. return -1;
  628. s->start_code = -1;
  629. s->first_picture = 1;
  630. s->org_width = avctx->width;
  631. s->org_height = avctx->height;
  632. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  633. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  634. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  635. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  636. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  637. {
  638. printf("mjpeg: using external huffman table\n");
  639. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
  640. mjpeg_decode_dht(s);
  641. /* should check for error - but dunno */
  642. }
  643. return 0;
  644. }
  645. /* quantize tables */
  646. static int mjpeg_decode_dqt(MJpegDecodeContext *s)
  647. {
  648. int len, index, i, j;
  649. len = get_bits(&s->gb, 16) - 2;
  650. while (len >= 65) {
  651. /* only 8 bit precision handled */
  652. if (get_bits(&s->gb, 4) != 0)
  653. {
  654. dprintf("dqt: 16bit precision\n");
  655. return -1;
  656. }
  657. index = get_bits(&s->gb, 4);
  658. if (index >= 4)
  659. return -1;
  660. dprintf("index=%d\n", index);
  661. /* read quant table */
  662. for(i=0;i<64;i++) {
  663. j = s->scantable.permutated[i];
  664. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  665. }
  666. len -= 65;
  667. }
  668. return 0;
  669. }
  670. /* decode huffman tables and build VLC decoders */
  671. static int mjpeg_decode_dht(MJpegDecodeContext *s)
  672. {
  673. int len, index, i, class, n, v, code_max;
  674. uint8_t bits_table[17];
  675. uint8_t val_table[256];
  676. len = get_bits(&s->gb, 16) - 2;
  677. while (len > 0) {
  678. if (len < 17)
  679. return -1;
  680. class = get_bits(&s->gb, 4);
  681. if (class >= 2)
  682. return -1;
  683. index = get_bits(&s->gb, 4);
  684. if (index >= 4)
  685. return -1;
  686. n = 0;
  687. for(i=1;i<=16;i++) {
  688. bits_table[i] = get_bits(&s->gb, 8);
  689. n += bits_table[i];
  690. }
  691. len -= 17;
  692. if (len < n || n > 256)
  693. return -1;
  694. code_max = 0;
  695. for(i=0;i<n;i++) {
  696. v = get_bits(&s->gb, 8);
  697. if (v > code_max)
  698. code_max = v;
  699. val_table[i] = v;
  700. }
  701. len -= n;
  702. /* build VLC and flush previous vlc if present */
  703. free_vlc(&s->vlcs[class][index]);
  704. dprintf("class=%d index=%d nb_codes=%d\n",
  705. class, index, code_max + 1);
  706. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  707. }
  708. return 0;
  709. }
  710. static int mjpeg_decode_sof0(MJpegDecodeContext *s)
  711. {
  712. int len, nb_components, i, width, height;
  713. /* XXX: verify len field validity */
  714. len = get_bits(&s->gb, 16);
  715. /* only 8 bits/component accepted */
  716. if (get_bits(&s->gb, 8) != 8)
  717. return -1;
  718. height = get_bits(&s->gb, 16);
  719. width = get_bits(&s->gb, 16);
  720. dprintf("sof0: picture: %dx%d\n", width, height);
  721. nb_components = get_bits(&s->gb, 8);
  722. if (nb_components <= 0 ||
  723. nb_components > MAX_COMPONENTS)
  724. return -1;
  725. s->nb_components = nb_components;
  726. s->h_max = 1;
  727. s->v_max = 1;
  728. for(i=0;i<nb_components;i++) {
  729. /* component id */
  730. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  731. s->h_count[i] = get_bits(&s->gb, 4);
  732. s->v_count[i] = get_bits(&s->gb, 4);
  733. /* compute hmax and vmax (only used in interleaved case) */
  734. if (s->h_count[i] > s->h_max)
  735. s->h_max = s->h_count[i];
  736. if (s->v_count[i] > s->v_max)
  737. s->v_max = s->v_count[i];
  738. s->quant_index[i] = get_bits(&s->gb, 8);
  739. if (s->quant_index[i] >= 4)
  740. return -1;
  741. dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  742. s->v_count[i], s->component_id[i], s->quant_index[i]);
  743. }
  744. /* if different size, realloc/alloc picture */
  745. /* XXX: also check h_count and v_count */
  746. if (width != s->width || height != s->height) {
  747. for(i=0;i<MAX_COMPONENTS;i++)
  748. av_freep(&s->current_picture[i]);
  749. s->width = width;
  750. s->height = height;
  751. /* test interlaced mode */
  752. if (s->first_picture &&
  753. s->org_height != 0 &&
  754. s->height < ((s->org_height * 3) / 4)) {
  755. s->interlaced = 1;
  756. // s->bottom_field = (s->interlace_polarity) ? 1 : 0;
  757. s->bottom_field = 0;
  758. }
  759. for(i=0;i<nb_components;i++) {
  760. int w, h;
  761. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  762. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  763. w = w * 8 * s->h_count[i];
  764. h = h * 8 * s->v_count[i];
  765. if (s->interlaced)
  766. w *= 2;
  767. s->linesize[i] = w;
  768. s->current_picture[i] = av_mallocz(w * h);
  769. if (!s->current_picture[i])
  770. {
  771. dprintf("error: no picture buffers allocated\n");
  772. return -1;
  773. }
  774. }
  775. s->first_picture = 0;
  776. }
  777. if (len != (8+(3*nb_components)))
  778. {
  779. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  780. }
  781. return 0;
  782. }
  783. static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
  784. {
  785. int code, diff;
  786. #if 1
  787. code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
  788. #else
  789. code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
  790. #endif
  791. if (code < 0)
  792. {
  793. dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
  794. &s->vlcs[0][dc_index]);
  795. return 0xffff;
  796. }
  797. if (code == 0) {
  798. diff = 0;
  799. } else {
  800. diff = get_bits(&s->gb, code);
  801. if ((diff & (1 << (code - 1))) == 0)
  802. diff = (-1 << code) | (diff + 1);
  803. }
  804. return diff;
  805. }
  806. /* decode block and dequantize */
  807. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  808. int component, int dc_index, int ac_index, int quant_index)
  809. {
  810. int nbits, code, i, j, level;
  811. int run, val;
  812. VLC *ac_vlc;
  813. int16_t *quant_matrix;
  814. /* DC coef */
  815. val = mjpeg_decode_dc(s, dc_index);
  816. if (val == 0xffff) {
  817. dprintf("error dc\n");
  818. return -1;
  819. }
  820. quant_matrix = s->quant_matrixes[quant_index];
  821. val = val * quant_matrix[0] + s->last_dc[component];
  822. s->last_dc[component] = val;
  823. block[0] = val;
  824. /* AC coefs */
  825. ac_vlc = &s->vlcs[1][ac_index];
  826. i = 1;
  827. for(;;) {
  828. #if 1
  829. code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);
  830. #else
  831. code = get_vlc(&s->gb, ac_vlc);
  832. #endif
  833. if (code < 0) {
  834. dprintf("error ac\n");
  835. return -1;
  836. }
  837. /* EOB */
  838. if (code == 0)
  839. break;
  840. if (code == 0xf0) {
  841. i += 16;
  842. } else {
  843. run = code >> 4;
  844. nbits = code & 0xf;
  845. level = get_bits(&s->gb, nbits);
  846. if ((level & (1 << (nbits - 1))) == 0)
  847. level = (-1 << nbits) | (level + 1);
  848. i += run;
  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 int mjpeg_decode_sos(MJpegDecodeContext *s)
  863. {
  864. int len, nb_components, i, j, n, h, v, ret;
  865. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  866. int comp_index[4];
  867. int dc_index[4];
  868. int ac_index[4];
  869. int nb_blocks[4];
  870. int h_count[4];
  871. int v_count[4];
  872. /* XXX: verify len field validity */
  873. len = get_bits(&s->gb, 16);
  874. nb_components = get_bits(&s->gb, 8);
  875. if (len != 6+2*nb_components)
  876. {
  877. dprintf("decode_sos: invalid len (%d)\n", len);
  878. return -1;
  879. }
  880. /* XXX: only interleaved scan accepted */
  881. if (nb_components != 3)
  882. {
  883. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  884. return -1;
  885. }
  886. vmax = 0;
  887. hmax = 0;
  888. for(i=0;i<nb_components;i++) {
  889. id = get_bits(&s->gb, 8) - 1;
  890. dprintf("component: %d\n", id);
  891. /* find component index */
  892. for(index=0;index<s->nb_components;index++)
  893. if (id == s->component_id[index])
  894. break;
  895. if (index == s->nb_components)
  896. {
  897. dprintf("decode_sos: index(%d) out of components\n", index);
  898. return -1;
  899. }
  900. comp_index[i] = index;
  901. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  902. h_count[i] = s->h_count[index];
  903. v_count[i] = s->v_count[index];
  904. dc_index[i] = get_bits(&s->gb, 4);
  905. ac_index[i] = get_bits(&s->gb, 4);
  906. if (dc_index[i] < 0 || ac_index[i] < 0 ||
  907. dc_index[i] >= 4 || ac_index[i] >= 4)
  908. goto out_of_range;
  909. switch(s->start_code)
  910. {
  911. case SOF0:
  912. if (dc_index[i] > 1 || ac_index[i] > 1)
  913. goto out_of_range;
  914. break;
  915. case SOF1:
  916. case SOF2:
  917. if (dc_index[i] > 3 || ac_index[i] > 3)
  918. goto out_of_range;
  919. break;
  920. case SOF3:
  921. if (dc_index[i] > 3 || ac_index[i] != 0)
  922. goto out_of_range;
  923. break;
  924. }
  925. }
  926. skip_bits(&s->gb, 8); /* Ss */
  927. skip_bits(&s->gb, 8); /* Se */
  928. skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
  929. for(i=0;i<nb_components;i++)
  930. s->last_dc[i] = 1024;
  931. if (nb_components > 1) {
  932. /* interleaved stream */
  933. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  934. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  935. } else {
  936. h = s->h_max / s->h_count[comp_index[0]];
  937. v = s->v_max / s->v_count[comp_index[0]];
  938. mb_width = (s->width + h * 8 - 1) / (h * 8);
  939. mb_height = (s->height + v * 8 - 1) / (v * 8);
  940. nb_blocks[0] = 1;
  941. h_count[0] = 1;
  942. v_count[0] = 1;
  943. }
  944. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  945. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  946. for(i=0;i<nb_components;i++) {
  947. uint8_t *ptr;
  948. int x, y, c;
  949. n = nb_blocks[i];
  950. c = comp_index[i];
  951. h = h_count[i];
  952. v = v_count[i];
  953. x = 0;
  954. y = 0;
  955. if (s->restart_interval && !s->restart_count)
  956. s->restart_count = s->restart_interval;
  957. for(j=0;j<n;j++) {
  958. memset(s->block, 0, sizeof(s->block));
  959. if (decode_block(s, s->block, i,
  960. dc_index[i], ac_index[i],
  961. s->quant_index[c]) < 0) {
  962. dprintf("error y=%d x=%d\n", mb_y, mb_x);
  963. ret = -1;
  964. goto the_end;
  965. }
  966. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  967. ptr = s->current_picture[c] +
  968. (s->linesize[c] * (v * mb_y + y) * 8) +
  969. (h * mb_x + x) * 8;
  970. if (s->interlaced && s->bottom_field)
  971. ptr += s->linesize[c] >> 1;
  972. s->idct_put(ptr, s->linesize[c], s->block);
  973. if (++x == h) {
  974. x = 0;
  975. y++;
  976. }
  977. }
  978. }
  979. /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
  980. if (s->restart_interval && (s->restart_interval < 1350) &&
  981. !--s->restart_count) {
  982. align_get_bits(&s->gb);
  983. skip_bits(&s->gb, 16); /* skip RSTn */
  984. for (j=0; j<nb_components; j++) /* reset dc */
  985. s->last_dc[j] = 1024;
  986. }
  987. }
  988. }
  989. ret = 0;
  990. the_end:
  991. emms_c();
  992. return ret;
  993. out_of_range:
  994. dprintf("decode_sos: ac/dc index out of range\n");
  995. return -1;
  996. }
  997. static int mjpeg_decode_dri(MJpegDecodeContext *s)
  998. {
  999. if (get_bits(&s->gb, 16) != 4)
  1000. return -1;
  1001. s->restart_interval = get_bits(&s->gb, 16);
  1002. dprintf("restart interval: %d\n", s->restart_interval);
  1003. return 0;
  1004. }
  1005. static int mjpeg_decode_app(MJpegDecodeContext *s)
  1006. {
  1007. int len, id;
  1008. /* XXX: verify len field validity */
  1009. len = get_bits(&s->gb, 16);
  1010. if (len < 5)
  1011. return -1;
  1012. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1013. id = be2me_32(id);
  1014. len -= 6;
  1015. /* buggy AVID, it puts EOI only at every 10th frame */
  1016. /* also this fourcc is used by non-avid files too, it holds some
  1017. informations, but it's always present in AVID creates files */
  1018. if (id == ff_get_fourcc("AVI1"))
  1019. {
  1020. /* structure:
  1021. 4bytes AVI1
  1022. 1bytes polarity
  1023. 1bytes always zero
  1024. 4bytes field_size
  1025. 4bytes field_size_less_padding
  1026. */
  1027. s->buggy_avid = 1;
  1028. // if (s->first_picture)
  1029. // printf("mjpeg: workarounding buggy AVID\n");
  1030. s->interlace_polarity = get_bits(&s->gb, 8);
  1031. #if 0
  1032. skip_bits(&s->gb, 8);
  1033. skip_bits(&s->gb, 32);
  1034. skip_bits(&s->gb, 32);
  1035. len -= 10;
  1036. #endif
  1037. // if (s->interlace_polarity)
  1038. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  1039. goto out;
  1040. }
  1041. // len -= 2;
  1042. if (id == ff_get_fourcc("JFIF"))
  1043. {
  1044. int t_w, t_h;
  1045. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  1046. printf("mjpeg: JFIF header found (version: %x.%x)\n",
  1047. get_bits(&s->gb, 8), get_bits(&s->gb, 8));
  1048. if (get_bits(&s->gb, 8) == 0)
  1049. {
  1050. int x_density, y_density;
  1051. x_density = get_bits(&s->gb, 16);
  1052. y_density = get_bits(&s->gb, 16);
  1053. dprintf("x/y density: %d (%f), %d (%f)\n", x_density,
  1054. (float)x_density, y_density, (float)y_density);
  1055. #if 0
  1056. //MN: needs to be checked
  1057. if(x_density)
  1058. // s->avctx->aspect_ratio= s->width*y_density/((float)s->height*x_density);
  1059. s->avctx->aspect_ratio = (float)x_density/y_density;
  1060. /* it's better, but every JFIF I have seen stores 1:1 */
  1061. else
  1062. s->avctx->aspect_ratio= 0.0;
  1063. #endif
  1064. }
  1065. else
  1066. {
  1067. skip_bits(&s->gb, 16);
  1068. skip_bits(&s->gb, 16);
  1069. }
  1070. t_w = get_bits(&s->gb, 8);
  1071. t_h = get_bits(&s->gb, 8);
  1072. if (t_w && t_h)
  1073. {
  1074. /* skip thumbnail */
  1075. if (len-10-(t_w*t_h*3) > 0)
  1076. len -= t_w*t_h*3;
  1077. }
  1078. len -= 10;
  1079. goto out;
  1080. }
  1081. if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
  1082. {
  1083. printf("mjpeg: Adobe header found\n");
  1084. skip_bits(&s->gb, 16); /* version */
  1085. skip_bits(&s->gb, 16); /* flags0 */
  1086. skip_bits(&s->gb, 16); /* flags1 */
  1087. skip_bits(&s->gb, 8); /* transform */
  1088. len -= 7;
  1089. goto out;
  1090. }
  1091. /* Apple MJPEG-A */
  1092. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1093. {
  1094. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1095. id = be2me_32(id);
  1096. len -= 4;
  1097. if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
  1098. {
  1099. #if 0
  1100. skip_bits(&s->gb, 32); /* field size */
  1101. skip_bits(&s->gb, 32); /* pad field size */
  1102. skip_bits(&s->gb, 32); /* next off */
  1103. skip_bits(&s->gb, 32); /* quant off */
  1104. skip_bits(&s->gb, 32); /* huff off */
  1105. skip_bits(&s->gb, 32); /* image off */
  1106. skip_bits(&s->gb, 32); /* scan off */
  1107. skip_bits(&s->gb, 32); /* data off */
  1108. #endif
  1109. if (s->first_picture)
  1110. printf("mjpeg: Apple MJPEG-A header found\n");
  1111. }
  1112. }
  1113. out:
  1114. /* slow but needed for extreme adobe jpegs */
  1115. if (len < 0)
  1116. printf("mjpeg: error, decode_app parser read over the end\n");
  1117. while(--len > 0)
  1118. skip_bits(&s->gb, 8);
  1119. return 0;
  1120. }
  1121. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1122. {
  1123. /* XXX: verify len field validity */
  1124. unsigned int len = get_bits(&s->gb, 16);
  1125. if (len >= 2 && len < 32768) {
  1126. /* XXX: any better upper bound */
  1127. uint8_t *cbuf = av_malloc(len - 1);
  1128. if (cbuf) {
  1129. int i;
  1130. for (i = 0; i < len - 2; i++)
  1131. cbuf[i] = get_bits(&s->gb, 8);
  1132. if (i > 0 && cbuf[i-1] == '\n')
  1133. cbuf[i-1] = 0;
  1134. else
  1135. cbuf[i] = 0;
  1136. printf("mjpeg comment: '%s'\n", cbuf);
  1137. /* buggy avid, it puts EOI only at every 10th frame */
  1138. if (!strcmp(cbuf, "AVID"))
  1139. {
  1140. s->buggy_avid = 1;
  1141. // if (s->first_picture)
  1142. // printf("mjpeg: workarounding buggy AVID\n");
  1143. }
  1144. av_free(cbuf);
  1145. }
  1146. }
  1147. return 0;
  1148. }
  1149. #if 0
  1150. static int valid_marker_list[] =
  1151. {
  1152. /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
  1153. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1154. /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1155. /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1156. /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1157. /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1158. /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1159. /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1160. /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1161. /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1162. /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1163. /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1164. /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1165. /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1166. /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1167. /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1168. /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  1169. }
  1170. #endif
  1171. /* return the 8 bit start code value and update the search
  1172. state. Return -1 if no start code found */
  1173. static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
  1174. {
  1175. uint8_t *buf_ptr;
  1176. unsigned int v, v2;
  1177. int val;
  1178. #ifdef DEBUG
  1179. int skipped=0;
  1180. #endif
  1181. buf_ptr = *pbuf_ptr;
  1182. while (buf_ptr < buf_end) {
  1183. v = *buf_ptr++;
  1184. v2 = *buf_ptr;
  1185. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe)) {
  1186. val = *buf_ptr++;
  1187. goto found;
  1188. }
  1189. #ifdef DEBUG
  1190. skipped++;
  1191. #endif
  1192. }
  1193. val = -1;
  1194. found:
  1195. #ifdef DEBUG
  1196. dprintf("find_marker skipped %d bytes\n", skipped);
  1197. #endif
  1198. *pbuf_ptr = buf_ptr;
  1199. return val;
  1200. }
  1201. static int mjpeg_decode_frame(AVCodecContext *avctx,
  1202. void *data, int *data_size,
  1203. uint8_t *buf, int buf_size)
  1204. {
  1205. MJpegDecodeContext *s = avctx->priv_data;
  1206. uint8_t *buf_end, *buf_ptr;
  1207. int i, start_code;
  1208. AVPicture *picture = data;
  1209. *data_size = 0;
  1210. /* no supplementary picture */
  1211. if (buf_size == 0)
  1212. return 0;
  1213. buf_ptr = buf;
  1214. buf_end = buf + buf_size;
  1215. while (buf_ptr < buf_end) {
  1216. /* find start next marker */
  1217. start_code = find_marker(&buf_ptr, buf_end);
  1218. {
  1219. /* EOF */
  1220. if (start_code < 0) {
  1221. goto the_end;
  1222. } else {
  1223. dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
  1224. if ((buf_end - buf_ptr) > s->buffer_size)
  1225. {
  1226. av_free(s->buffer);
  1227. s->buffer_size = buf_end-buf_ptr;
  1228. s->buffer = av_malloc(s->buffer_size);
  1229. dprintf("buffer too small, expanding to %d bytes\n",
  1230. s->buffer_size);
  1231. }
  1232. /* unescape buffer of SOS */
  1233. if (start_code == SOS)
  1234. {
  1235. uint8_t *src = buf_ptr;
  1236. uint8_t *dst = s->buffer;
  1237. while (src<buf_end)
  1238. {
  1239. uint8_t x = *(src++);
  1240. *(dst++) = x;
  1241. if (x == 0xff)
  1242. {
  1243. while(*src == 0xff) src++;
  1244. x = *(src++);
  1245. if (x >= 0xd0 && x <= 0xd7)
  1246. *(dst++) = x;
  1247. else if (x)
  1248. break;
  1249. }
  1250. }
  1251. init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
  1252. dprintf("escaping removed %d bytes\n",
  1253. (buf_end - buf_ptr) - (dst - s->buffer));
  1254. }
  1255. else
  1256. init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
  1257. s->start_code = start_code;
  1258. /* process markers */
  1259. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1260. dprintf("restart marker: %d\n", start_code&0x0f);
  1261. } else if (s->first_picture) {
  1262. /* APP fields */
  1263. if (start_code >= 0xe0 && start_code <= 0xef)
  1264. mjpeg_decode_app(s);
  1265. /* Comment */
  1266. else if (start_code == COM)
  1267. mjpeg_decode_com(s);
  1268. }
  1269. switch(start_code) {
  1270. case SOI:
  1271. s->restart_interval = 0;
  1272. /* nothing to do on SOI */
  1273. break;
  1274. case DQT:
  1275. mjpeg_decode_dqt(s);
  1276. break;
  1277. case DHT:
  1278. mjpeg_decode_dht(s);
  1279. break;
  1280. case SOF0:
  1281. if (mjpeg_decode_sof0(s) < 0)
  1282. return -1;
  1283. break;
  1284. case EOI:
  1285. eoi_parser:
  1286. {
  1287. if (s->interlaced) {
  1288. s->bottom_field ^= 1;
  1289. /* if not bottom field, do not output image yet */
  1290. if (s->bottom_field)
  1291. goto not_the_end;
  1292. }
  1293. for(i=0;i<3;i++) {
  1294. picture->data[i] = s->current_picture[i];
  1295. picture->linesize[i] = (s->interlaced) ?
  1296. s->linesize[i] >> 1 : s->linesize[i];
  1297. }
  1298. *data_size = sizeof(AVPicture);
  1299. avctx->height = s->height;
  1300. if (s->interlaced)
  1301. avctx->height *= 2;
  1302. avctx->width = s->width;
  1303. /* XXX: not complete test ! */
  1304. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1305. case 0x11:
  1306. avctx->pix_fmt = PIX_FMT_YUV444P;
  1307. break;
  1308. case 0x21:
  1309. avctx->pix_fmt = PIX_FMT_YUV422P;
  1310. break;
  1311. default:
  1312. case 0x22:
  1313. avctx->pix_fmt = PIX_FMT_YUV420P;
  1314. break;
  1315. }
  1316. /* dummy quality */
  1317. /* XXX: infer it with matrix */
  1318. // avctx->quality = 3;
  1319. goto the_end;
  1320. }
  1321. break;
  1322. case SOS:
  1323. mjpeg_decode_sos(s);
  1324. /* buggy avid puts EOI every 10-20th frame */
  1325. /* if restart period is over process EOI */
  1326. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1327. goto eoi_parser;
  1328. break;
  1329. case DRI:
  1330. mjpeg_decode_dri(s);
  1331. break;
  1332. case SOF1:
  1333. case SOF2:
  1334. case SOF3:
  1335. case SOF5:
  1336. case SOF6:
  1337. case SOF7:
  1338. case SOF9:
  1339. case SOF10:
  1340. case SOF11:
  1341. case SOF13:
  1342. case SOF14:
  1343. case SOF15:
  1344. case JPG:
  1345. printf("mjpeg: unsupported coding type (%x)\n", start_code);
  1346. break;
  1347. // default:
  1348. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1349. // break;
  1350. }
  1351. not_the_end:
  1352. /* eof process start code */
  1353. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1354. dprintf("marker parser used %d bytes (%d bits)\n",
  1355. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1356. }
  1357. }
  1358. }
  1359. the_end:
  1360. dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
  1361. // return buf_end - buf_ptr;
  1362. return buf_ptr - buf;
  1363. }
  1364. static int mjpegb_decode_frame(AVCodecContext *avctx,
  1365. void *data, int *data_size,
  1366. uint8_t *buf, int buf_size)
  1367. {
  1368. MJpegDecodeContext *s = avctx->priv_data;
  1369. uint8_t *buf_end, *buf_ptr;
  1370. int i;
  1371. AVPicture *picture = data;
  1372. GetBitContext hgb; /* for the header */
  1373. uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
  1374. uint32_t field_size;
  1375. *data_size = 0;
  1376. /* no supplementary picture */
  1377. if (buf_size == 0)
  1378. return 0;
  1379. buf_ptr = buf;
  1380. buf_end = buf + buf_size;
  1381. read_header:
  1382. /* reset on every SOI */
  1383. s->restart_interval = 0;
  1384. init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
  1385. skip_bits(&hgb, 32); /* reserved zeros */
  1386. if (get_bits(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
  1387. {
  1388. dprintf("not mjpeg-b (bad fourcc)\n");
  1389. return 0;
  1390. }
  1391. field_size = get_bits(&hgb, 32); /* field size */
  1392. dprintf("field size: 0x%x\n", field_size);
  1393. skip_bits(&hgb, 32); /* padded field size */
  1394. second_field_offs = get_bits(&hgb, 32);
  1395. dprintf("second field offs: 0x%x\n", second_field_offs);
  1396. if (second_field_offs)
  1397. s->interlaced = 1;
  1398. dqt_offs = get_bits(&hgb, 32);
  1399. dprintf("dqt offs: 0x%x\n", dqt_offs);
  1400. if (dqt_offs)
  1401. {
  1402. init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
  1403. s->start_code = DQT;
  1404. mjpeg_decode_dqt(s);
  1405. }
  1406. dht_offs = get_bits(&hgb, 32);
  1407. dprintf("dht offs: 0x%x\n", dht_offs);
  1408. if (dht_offs)
  1409. {
  1410. init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
  1411. s->start_code = DHT;
  1412. mjpeg_decode_dht(s);
  1413. }
  1414. sof_offs = get_bits(&hgb, 32);
  1415. dprintf("sof offs: 0x%x\n", sof_offs);
  1416. if (sof_offs)
  1417. {
  1418. init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
  1419. s->start_code = SOF0;
  1420. if (mjpeg_decode_sof0(s) < 0)
  1421. return -1;
  1422. }
  1423. sos_offs = get_bits(&hgb, 32);
  1424. dprintf("sos offs: 0x%x\n", sos_offs);
  1425. if (sos_offs)
  1426. {
  1427. // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
  1428. init_get_bits(&s->gb, buf+sos_offs, field_size*8);
  1429. s->start_code = SOS;
  1430. mjpeg_decode_sos(s);
  1431. }
  1432. skip_bits(&hgb, 32); /* start of data offset */
  1433. if (s->interlaced) {
  1434. s->bottom_field ^= 1;
  1435. /* if not bottom field, do not output image yet */
  1436. if (s->bottom_field && second_field_offs)
  1437. {
  1438. buf_ptr = buf + second_field_offs;
  1439. second_field_offs = 0;
  1440. goto read_header;
  1441. }
  1442. }
  1443. for(i=0;i<3;i++) {
  1444. picture->data[i] = s->current_picture[i];
  1445. picture->linesize[i] = (s->interlaced) ?
  1446. s->linesize[i] >> 1 : s->linesize[i];
  1447. }
  1448. *data_size = sizeof(AVPicture);
  1449. avctx->height = s->height;
  1450. if (s->interlaced)
  1451. avctx->height *= 2;
  1452. avctx->width = s->width;
  1453. /* XXX: not complete test ! */
  1454. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1455. case 0x11:
  1456. avctx->pix_fmt = PIX_FMT_YUV444P;
  1457. break;
  1458. case 0x21:
  1459. avctx->pix_fmt = PIX_FMT_YUV422P;
  1460. break;
  1461. default:
  1462. case 0x22:
  1463. avctx->pix_fmt = PIX_FMT_YUV420P;
  1464. break;
  1465. }
  1466. /* dummy quality */
  1467. /* XXX: infer it with matrix */
  1468. // avctx->quality = 3;
  1469. return buf_ptr - buf;
  1470. }
  1471. static int mjpeg_decode_end(AVCodecContext *avctx)
  1472. {
  1473. MJpegDecodeContext *s = avctx->priv_data;
  1474. int i, j;
  1475. av_free(s->buffer);
  1476. for(i=0;i<MAX_COMPONENTS;i++)
  1477. av_free(s->current_picture[i]);
  1478. for(i=0;i<2;i++) {
  1479. for(j=0;j<4;j++)
  1480. free_vlc(&s->vlcs[i][j]);
  1481. }
  1482. return 0;
  1483. }
  1484. AVCodec mjpeg_decoder = {
  1485. "mjpeg",
  1486. CODEC_TYPE_VIDEO,
  1487. CODEC_ID_MJPEG,
  1488. sizeof(MJpegDecodeContext),
  1489. mjpeg_decode_init,
  1490. NULL,
  1491. mjpeg_decode_end,
  1492. mjpeg_decode_frame,
  1493. 0,
  1494. NULL
  1495. };
  1496. AVCodec mjpegb_decoder = {
  1497. "mjpegb",
  1498. CODEC_TYPE_VIDEO,
  1499. CODEC_ID_MJPEGB,
  1500. sizeof(MJpegDecodeContext),
  1501. mjpeg_decode_init,
  1502. NULL,
  1503. mjpeg_decode_end,
  1504. mjpegb_decode_frame,
  1505. 0,
  1506. NULL
  1507. };