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.

1349 lines
39KB

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