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.

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