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.

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