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.

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