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.

1453 lines
41KB

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