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.

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