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.

1350 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. jpeg_put_comments(s);
  346. if (s->mjpeg_write_tables) jpeg_table_header(s);
  347. put_marker(&s->pb, SOF0);
  348. put_bits(&s->pb, 16, 17);
  349. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  350. put_bits(&s->pb, 16, s->height);
  351. put_bits(&s->pb, 16, s->width);
  352. put_bits(&s->pb, 8, 3); /* 3 components */
  353. /* Y component */
  354. put_bits(&s->pb, 8, 1); /* component number */
  355. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  356. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  357. put_bits(&s->pb, 8, 0); /* select matrix */
  358. /* Cb component */
  359. put_bits(&s->pb, 8, 2); /* component number */
  360. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  361. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  362. #ifdef TWOMATRIXES
  363. put_bits(&s->pb, 8, 1); /* select matrix */
  364. #else
  365. put_bits(&s->pb, 8, 0); /* select matrix */
  366. #endif
  367. /* Cr component */
  368. put_bits(&s->pb, 8, 3); /* component number */
  369. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  370. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  371. #ifdef TWOMATRIXES
  372. put_bits(&s->pb, 8, 1); /* select matrix */
  373. #else
  374. put_bits(&s->pb, 8, 0); /* select matrix */
  375. #endif
  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 void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  528. int nb_codes)
  529. {
  530. UINT8 huff_size[256];
  531. UINT16 huff_code[256];
  532. memset(huff_size, 0, sizeof(huff_size));
  533. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  534. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  535. }
  536. static int mjpeg_decode_init(AVCodecContext *avctx)
  537. {
  538. MJpegDecodeContext *s = avctx->priv_data;
  539. s->avctx = avctx;
  540. s->header_state = 0;
  541. s->mpeg_enc_ctx_allocated = 0;
  542. s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
  543. account FF 00 case */
  544. s->start_code = -1;
  545. s->buf_ptr = s->buffer;
  546. s->first_picture = 1;
  547. s->org_width = avctx->width;
  548. s->org_height = avctx->height;
  549. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  550. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  551. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  552. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  553. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  554. {
  555. printf("mjpeg: using external huffman table\n");
  556. mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size);
  557. /* should check for error - but dunno */
  558. }
  559. return 0;
  560. }
  561. /* quantize tables */
  562. static int mjpeg_decode_dqt(MJpegDecodeContext *s,
  563. UINT8 *buf, int buf_size)
  564. {
  565. int len, index, i, j;
  566. init_get_bits(&s->gb, buf, buf_size);
  567. len = get_bits(&s->gb, 16) - 2;
  568. while (len >= 65) {
  569. /* only 8 bit precision handled */
  570. if (get_bits(&s->gb, 4) != 0)
  571. {
  572. dprintf("dqt: 16bit precision\n");
  573. return -1;
  574. }
  575. index = get_bits(&s->gb, 4);
  576. if (index >= 4)
  577. return -1;
  578. dprintf("index=%d\n", index);
  579. /* read quant table */
  580. for(i=0;i<64;i++) {
  581. j = zigzag_direct[i];
  582. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  583. }
  584. len -= 65;
  585. }
  586. SKIP_REMAINING(&s->gb, len);
  587. return 0;
  588. }
  589. /* decode huffman tables and build VLC decoders */
  590. static int mjpeg_decode_dht(MJpegDecodeContext *s,
  591. UINT8 *buf, int buf_size)
  592. {
  593. int len, index, i, class, n, v, code_max;
  594. UINT8 bits_table[17];
  595. UINT8 val_table[256];
  596. init_get_bits(&s->gb, buf, buf_size);
  597. len = get_bits(&s->gb, 16);
  598. len -= 2;
  599. while (len > 0) {
  600. if (len < 17)
  601. return -1;
  602. class = get_bits(&s->gb, 4);
  603. if (class >= 2)
  604. return -1;
  605. index = get_bits(&s->gb, 4);
  606. if (index >= 4)
  607. return -1;
  608. n = 0;
  609. for(i=1;i<=16;i++) {
  610. bits_table[i] = get_bits(&s->gb, 8);
  611. n += bits_table[i];
  612. }
  613. len -= 17;
  614. if (len < n || n > 256)
  615. return -1;
  616. code_max = 0;
  617. for(i=0;i<n;i++) {
  618. v = get_bits(&s->gb, 8);
  619. if (v > code_max)
  620. code_max = v;
  621. val_table[i] = v;
  622. }
  623. len -= n;
  624. /* build VLC and flush previous vlc if present */
  625. free_vlc(&s->vlcs[class][index]);
  626. dprintf("class=%d index=%d nb_codes=%d\n",
  627. class, index, code_max + 1);
  628. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  629. }
  630. return 0;
  631. }
  632. static int mjpeg_decode_sof0(MJpegDecodeContext *s,
  633. UINT8 *buf, int buf_size)
  634. {
  635. int len, nb_components, i, width, height;
  636. init_get_bits(&s->gb, buf, buf_size);
  637. /* XXX: verify len field validity */
  638. len = get_bits(&s->gb, 16);
  639. /* only 8 bits/component accepted */
  640. if (get_bits(&s->gb, 8) != 8)
  641. return -1;
  642. height = get_bits(&s->gb, 16);
  643. width = get_bits(&s->gb, 16);
  644. dprintf("sof0: picture: %dx%d\n", width, height);
  645. nb_components = get_bits(&s->gb, 8);
  646. if (nb_components <= 0 ||
  647. nb_components > MAX_COMPONENTS)
  648. return -1;
  649. s->nb_components = nb_components;
  650. s->h_max = 1;
  651. s->v_max = 1;
  652. for(i=0;i<nb_components;i++) {
  653. /* component id */
  654. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  655. s->h_count[i] = get_bits(&s->gb, 4);
  656. s->v_count[i] = get_bits(&s->gb, 4);
  657. /* compute hmax and vmax (only used in interleaved case) */
  658. if (s->h_count[i] > s->h_max)
  659. s->h_max = s->h_count[i];
  660. if (s->v_count[i] > s->v_max)
  661. s->v_max = s->v_count[i];
  662. s->quant_index[i] = get_bits(&s->gb, 8);
  663. if (s->quant_index[i] >= 4)
  664. return -1;
  665. dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  666. s->v_count[i], s->component_id[i], s->quant_index[i]);
  667. }
  668. /* if different size, realloc/alloc picture */
  669. /* XXX: also check h_count and v_count */
  670. if (width != s->width || height != s->height) {
  671. for(i=0;i<MAX_COMPONENTS;i++) {
  672. free(s->current_picture[i]);
  673. s->current_picture[i] = NULL;
  674. }
  675. s->width = width;
  676. s->height = height;
  677. /* test interlaced mode */
  678. if (s->first_picture &&
  679. s->org_height != 0 &&
  680. s->height < ((s->org_height * 3) / 4)) {
  681. s->interlaced = 1;
  682. s->bottom_field = 0;
  683. }
  684. for(i=0;i<nb_components;i++) {
  685. int w, h;
  686. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  687. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  688. w = w * 8 * s->h_count[i];
  689. h = h * 8 * s->v_count[i];
  690. if (s->interlaced)
  691. w *= 2;
  692. s->linesize[i] = w;
  693. /* memory test is done in mjpeg_decode_sos() */
  694. s->current_picture[i] = av_mallocz(w * h);
  695. }
  696. s->first_picture = 0;
  697. }
  698. if (len != (8+(3*nb_components)))
  699. {
  700. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  701. }
  702. return 0;
  703. }
  704. static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
  705. {
  706. int code, diff;
  707. code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
  708. if (code < 0)
  709. {
  710. dprintf("decode_dc: bad vlc: %d:%d (%x)\n", 0, dc_index,
  711. &s->vlcs[0][dc_index]);
  712. return 0xffff;
  713. }
  714. if (code == 0) {
  715. diff = 0;
  716. } else {
  717. diff = get_bits(&s->gb, code);
  718. if ((diff & (1 << (code - 1))) == 0)
  719. diff = (-1 << code) | (diff + 1);
  720. }
  721. return diff;
  722. }
  723. /* decode block and dequantize */
  724. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  725. int component, int dc_index, int ac_index, int quant_index)
  726. {
  727. int nbits, code, i, j, level;
  728. int run, val;
  729. VLC *ac_vlc;
  730. INT16 *quant_matrix;
  731. /* DC coef */
  732. val = decode_dc(s, dc_index);
  733. if (val == 0xffff) {
  734. dprintf("error dc\n");
  735. return -1;
  736. }
  737. quant_matrix = s->quant_matrixes[quant_index];
  738. val = val * quant_matrix[0] + s->last_dc[component];
  739. s->last_dc[component] = val;
  740. block[0] = val;
  741. /* AC coefs */
  742. ac_vlc = &s->vlcs[1][ac_index];
  743. i = 1;
  744. for(;;) {
  745. code = get_vlc(&s->gb, ac_vlc);
  746. if (code < 0) {
  747. dprintf("error ac\n");
  748. return -1;
  749. }
  750. /* EOB */
  751. if (code == 0)
  752. break;
  753. if (code == 0xf0) {
  754. i += 16;
  755. } else {
  756. run = code >> 4;
  757. nbits = code & 0xf;
  758. level = get_bits(&s->gb, nbits);
  759. if ((level & (1 << (nbits - 1))) == 0)
  760. level = (-1 << nbits) | (level + 1);
  761. i += run;
  762. if (i >= 64) {
  763. dprintf("error count: %d\n", i);
  764. return -1;
  765. }
  766. j = zigzag_direct[i];
  767. block[j] = level * quant_matrix[j];
  768. i++;
  769. if (i >= 64)
  770. break;
  771. }
  772. }
  773. return 0;
  774. }
  775. static int mjpeg_decode_sos(MJpegDecodeContext *s,
  776. UINT8 *buf, int buf_size)
  777. {
  778. int len, nb_components, i, j, n, h, v, ret;
  779. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  780. int comp_index[4];
  781. int dc_index[4];
  782. int ac_index[4];
  783. int nb_blocks[4];
  784. int h_count[4];
  785. int v_count[4];
  786. init_get_bits(&s->gb, buf, buf_size);
  787. /* XXX: verify len field validity */
  788. len = get_bits(&s->gb, 16);
  789. nb_components = get_bits(&s->gb, 8);
  790. /* XXX: only interleaved scan accepted */
  791. if (nb_components != 3)
  792. {
  793. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  794. return -1;
  795. }
  796. vmax = 0;
  797. hmax = 0;
  798. for(i=0;i<nb_components;i++) {
  799. id = get_bits(&s->gb, 8) - 1;
  800. dprintf("component: %d\n", id);
  801. /* find component index */
  802. for(index=0;index<s->nb_components;index++)
  803. if (id == s->component_id[index])
  804. break;
  805. if (index == s->nb_components)
  806. {
  807. dprintf("decode_sos: index(%d) out of components\n", index);
  808. return -1;
  809. }
  810. comp_index[i] = index;
  811. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  812. h_count[i] = s->h_count[index];
  813. v_count[i] = s->v_count[index];
  814. dc_index[i] = get_bits(&s->gb, 4);
  815. ac_index[i] = get_bits(&s->gb, 4);
  816. if (dc_index[i] < 0 || ac_index[i] < 0 ||
  817. dc_index[i] >= 4 || ac_index[i] >= 4)
  818. goto out_of_range;
  819. switch(s->start_code)
  820. {
  821. case SOF0:
  822. if (dc_index[i] > 1 || ac_index[i] > 1)
  823. goto out_of_range;
  824. break;
  825. case SOF1:
  826. case SOF2:
  827. if (dc_index[i] > 3 || ac_index[i] > 3)
  828. goto out_of_range;
  829. break;
  830. case SOF3:
  831. if (dc_index[i] > 3 || ac_index[i] != 0)
  832. goto out_of_range;
  833. break;
  834. }
  835. }
  836. skip_bits(&s->gb, 8); /* Ss */
  837. skip_bits(&s->gb, 8); /* Se */
  838. skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
  839. for(i=0;i<nb_components;i++)
  840. s->last_dc[i] = 1024;
  841. if (nb_components > 1) {
  842. /* interleaved stream */
  843. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  844. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  845. } else {
  846. h = s->h_max / s->h_count[comp_index[0]];
  847. v = s->v_max / s->v_count[comp_index[0]];
  848. mb_width = (s->width + h * 8 - 1) / (h * 8);
  849. mb_height = (s->height + v * 8 - 1) / (v * 8);
  850. nb_blocks[0] = 1;
  851. h_count[0] = 1;
  852. v_count[0] = 1;
  853. }
  854. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  855. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  856. for(i=0;i<nb_components;i++) {
  857. UINT8 *ptr;
  858. int x, y, c;
  859. n = nb_blocks[i];
  860. c = comp_index[i];
  861. h = h_count[i];
  862. v = v_count[i];
  863. x = 0;
  864. y = 0;
  865. if (s->restart_interval && !s->restart_count)
  866. s->restart_count = s->restart_interval;
  867. for(j=0;j<n;j++) {
  868. memset(s->block, 0, sizeof(s->block));
  869. if (decode_block(s, s->block, i,
  870. dc_index[i], ac_index[i],
  871. s->quant_index[c]) < 0) {
  872. dprintf("error %d %d\n", mb_y, mb_x);
  873. ret = -1;
  874. goto the_end;
  875. }
  876. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  877. ff_idct (s->block);
  878. ptr = s->current_picture[c] +
  879. (s->linesize[c] * (v * mb_y + y) * 8) +
  880. (h * mb_x + x) * 8;
  881. if (s->interlaced && s->bottom_field)
  882. ptr += s->linesize[c] >> 1;
  883. put_pixels_clamped(s->block, ptr, s->linesize[c]);
  884. if (++x == h) {
  885. x = 0;
  886. y++;
  887. }
  888. }
  889. }
  890. }
  891. }
  892. ret = 0;
  893. the_end:
  894. emms_c();
  895. return ret;
  896. out_of_range:
  897. dprintf("decode_sos: ac/dc index out of range\n");
  898. return -1;
  899. }
  900. static int mjpeg_decode_dri(MJpegDecodeContext *s,
  901. UINT8 *buf, int buf_size)
  902. {
  903. init_get_bits(&s->gb, buf, buf_size);
  904. if (get_bits(&s->gb, 16) != 4)
  905. return -1;
  906. s->restart_interval = get_bits(&s->gb, 16);
  907. printf("restart interval: %d\n", s->restart_interval);
  908. return 0;
  909. }
  910. #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d)
  911. static int mjpeg_decode_app(MJpegDecodeContext *s,
  912. UINT8 *buf, int buf_size, int start_code)
  913. {
  914. int len, id;
  915. init_get_bits(&s->gb, buf, buf_size);
  916. /* XXX: verify len field validity */
  917. len = get_bits(&s->gb, 16);
  918. if (len < 5)
  919. return -1;
  920. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  921. len -= 6;
  922. /* buggy AVID, it puts EOI only at every 10th frame */
  923. /* also this fourcc is used by non-avid files too, it means
  924. interleaving, but it's always present in AVID files */
  925. if (id == FOURCC('A','V','I','1'))
  926. {
  927. /* structure:
  928. 4bytes AVI1
  929. 1bytes polarity
  930. 1bytes always zero
  931. 4bytes field_size
  932. 4bytes field_size_less_padding
  933. */
  934. s->buggy_avid = 1;
  935. if (s->first_picture)
  936. printf("mjpeg: workarounding buggy AVID\n");
  937. s->interleaved_rows = get_bits(&s->gb, 8);
  938. #if 0
  939. skip_bits(&s->gb, 8);
  940. skip_bits(&s->gb, 32);
  941. skip_bits(&s->gb, 32);
  942. len -= 10;
  943. #endif
  944. if (s->interleaved_rows)
  945. printf("mjpeg: interleaved rows: %d\n", s->interleaved_rows);
  946. goto out;
  947. }
  948. len -= 2;
  949. if (id == FOURCC('J','F','I','F'))
  950. {
  951. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  952. printf("mjpeg: JFIF header found (version: %x.%x)\n",
  953. get_bits(&s->gb, 8), get_bits(&s->gb, 8));
  954. goto out;
  955. }
  956. /* Apple MJPEG-A */
  957. if ((start_code == APP1) && (len > (0x28 - 8)))
  958. {
  959. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  960. len -= 4;
  961. if (id == FOURCC('m','j','p','g')) /* Apple MJPEG-A */
  962. {
  963. #if 0
  964. skip_bits(&s->gb, 32); /* field size */
  965. skip_bits(&s->gb, 32); /* pad field size */
  966. skip_bits(&s->gb, 32); /* next off */
  967. skip_bits(&s->gb, 32); /* quant off */
  968. skip_bits(&s->gb, 32); /* huff off */
  969. skip_bits(&s->gb, 32); /* image off */
  970. skip_bits(&s->gb, 32); /* scan off */
  971. skip_bits(&s->gb, 32); /* data off */
  972. #endif
  973. if (s->first_picture)
  974. printf("mjpeg: Apple MJPEG-A header found\n");
  975. }
  976. }
  977. out:
  978. /* should check for further values.. */
  979. SKIP_REMAINING(&s->gb, len);
  980. return 0;
  981. }
  982. #undef FOURCC
  983. static int mjpeg_decode_com(MJpegDecodeContext *s,
  984. UINT8 *buf, int buf_size)
  985. {
  986. int len, i;
  987. UINT8 *cbuf;
  988. init_get_bits(&s->gb, buf, buf_size);
  989. /* XXX: verify len field validity */
  990. len = get_bits(&s->gb, 16)-2;
  991. cbuf = malloc(len+1);
  992. for (i = 0; i < len; i++)
  993. cbuf[i] = get_bits(&s->gb, 8);
  994. if (cbuf[i-1] == '\n')
  995. cbuf[i-1] = 0;
  996. else
  997. cbuf[i] = 0;
  998. printf("mjpeg comment: '%s'\n", cbuf);
  999. /* buggy avid, it puts EOI only at every 10th frame */
  1000. if (!strcmp(cbuf, "AVID"))
  1001. {
  1002. s->buggy_avid = 1;
  1003. if (s->first_picture)
  1004. printf("mjpeg: workarounding buggy AVID\n");
  1005. }
  1006. free(cbuf);
  1007. return 0;
  1008. }
  1009. /* return the 8 bit start code value and update the search
  1010. state. Return -1 if no start code found */
  1011. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
  1012. UINT32 *header_state)
  1013. {
  1014. UINT8 *buf_ptr;
  1015. unsigned int state, v;
  1016. int val;
  1017. state = *header_state;
  1018. buf_ptr = *pbuf_ptr;
  1019. if (state) {
  1020. /* get marker */
  1021. found:
  1022. if (buf_ptr < buf_end) {
  1023. val = *buf_ptr++;
  1024. state = 0;
  1025. } else {
  1026. val = -1;
  1027. }
  1028. } else {
  1029. while (buf_ptr < buf_end) {
  1030. v = *buf_ptr++;
  1031. if (v == 0xff) {
  1032. state = 1;
  1033. goto found;
  1034. }
  1035. }
  1036. val = -1;
  1037. }
  1038. *pbuf_ptr = buf_ptr;
  1039. *header_state = state;
  1040. return val;
  1041. }
  1042. static int mjpeg_decode_frame(AVCodecContext *avctx,
  1043. void *data, int *data_size,
  1044. UINT8 *buf, int buf_size)
  1045. {
  1046. MJpegDecodeContext *s = avctx->priv_data;
  1047. UINT8 *buf_end, *buf_ptr, *buf_start;
  1048. int len, code, input_size, i;
  1049. AVPicture *picture = data;
  1050. unsigned int start_code;
  1051. *data_size = 0;
  1052. /* no supplementary picture */
  1053. if (buf_size == 0)
  1054. return 0;
  1055. buf_ptr = buf;
  1056. buf_end = buf + buf_size;
  1057. while (buf_ptr < buf_end) {
  1058. buf_start = buf_ptr;
  1059. /* find start next marker */
  1060. code = find_marker(&buf_ptr, buf_end, &s->header_state);
  1061. /* copy to buffer */
  1062. len = buf_ptr - buf_start;
  1063. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1064. /* data too big : flush */
  1065. s->buf_ptr = s->buffer;
  1066. if (code > 0)
  1067. s->start_code = code;
  1068. } else {
  1069. memcpy(s->buf_ptr, buf_start, len);
  1070. s->buf_ptr += len;
  1071. /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
  1072. /* valid marker code is between 00 and ff - alex */
  1073. if (code <= 0 || code >= 0xff) {
  1074. s->buf_ptr--;
  1075. } else {
  1076. /* prepare data for next start code */
  1077. input_size = s->buf_ptr - s->buffer;
  1078. start_code = s->start_code;
  1079. s->buf_ptr = s->buffer;
  1080. s->start_code = code;
  1081. dprintf("marker=%x\n", start_code);
  1082. switch(start_code) {
  1083. case SOI:
  1084. s->restart_interval = 0;
  1085. /* nothing to do on SOI */
  1086. break;
  1087. case DQT:
  1088. mjpeg_decode_dqt(s, s->buffer, input_size);
  1089. break;
  1090. case DHT:
  1091. mjpeg_decode_dht(s, s->buffer, input_size);
  1092. break;
  1093. case SOF0:
  1094. mjpeg_decode_sof0(s, s->buffer, input_size);
  1095. break;
  1096. case SOS:
  1097. mjpeg_decode_sos(s, s->buffer, input_size);
  1098. if (s->start_code == EOI || s->buggy_avid || s->restart_interval) {
  1099. int l;
  1100. if (s->interlaced) {
  1101. s->bottom_field ^= 1;
  1102. /* if not bottom field, do not output image yet */
  1103. if (s->bottom_field)
  1104. goto the_end;
  1105. }
  1106. for(i=0;i<3;i++) {
  1107. picture->data[i] = s->current_picture[i];
  1108. l = s->linesize[i];
  1109. if (s->interlaced)
  1110. l >>= 1;
  1111. picture->linesize[i] = l;
  1112. }
  1113. *data_size = sizeof(AVPicture);
  1114. avctx->height = s->height;
  1115. if (s->interlaced)
  1116. avctx->height *= 2;
  1117. avctx->width = s->width;
  1118. /* XXX: not complete test ! */
  1119. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1120. case 0x11:
  1121. avctx->pix_fmt = PIX_FMT_YUV444P;
  1122. break;
  1123. case 0x21:
  1124. avctx->pix_fmt = PIX_FMT_YUV422P;
  1125. break;
  1126. default:
  1127. case 0x22:
  1128. avctx->pix_fmt = PIX_FMT_YUV420P;
  1129. break;
  1130. }
  1131. /* dummy quality */
  1132. /* XXX: infer it with matrix */
  1133. avctx->quality = 3;
  1134. goto the_end;
  1135. }
  1136. break;
  1137. case DRI:
  1138. mjpeg_decode_dri(s, s->buffer, input_size);
  1139. break;
  1140. case SOF1:
  1141. case SOF2:
  1142. case SOF3:
  1143. case SOF5:
  1144. case SOF6:
  1145. case SOF7:
  1146. case SOF9:
  1147. case SOF10:
  1148. case SOF11:
  1149. case SOF13:
  1150. case SOF14:
  1151. case SOF15:
  1152. case JPG:
  1153. printf("mjpeg: unsupported coding type (%x)\n", start_code);
  1154. return -1;
  1155. }
  1156. #if 1
  1157. if (start_code >= 0xd0 && start_code <= 0xd7)
  1158. {
  1159. dprintf("restart marker: %d\n", start_code&0x0f);
  1160. }
  1161. else if (s->first_picture)
  1162. {
  1163. /* APP fields */
  1164. if (start_code >= 0xe0 && start_code <= 0xef)
  1165. mjpeg_decode_app(s, s->buffer, input_size, start_code);
  1166. /* Comment */
  1167. else if (start_code == COM)
  1168. mjpeg_decode_com(s, s->buffer, input_size);
  1169. }
  1170. #endif
  1171. }
  1172. }
  1173. }
  1174. the_end:
  1175. return buf_ptr - buf;
  1176. }
  1177. static int mjpeg_decode_end(AVCodecContext *avctx)
  1178. {
  1179. MJpegDecodeContext *s = avctx->priv_data;
  1180. int i, j;
  1181. for(i=0;i<MAX_COMPONENTS;i++)
  1182. free(s->current_picture[i]);
  1183. for(i=0;i<2;i++) {
  1184. for(j=0;j<4;j++)
  1185. free_vlc(&s->vlcs[i][j]);
  1186. }
  1187. return 0;
  1188. }
  1189. AVCodec mjpeg_decoder = {
  1190. "mjpeg",
  1191. CODEC_TYPE_VIDEO,
  1192. CODEC_ID_MJPEG,
  1193. sizeof(MJpegDecodeContext),
  1194. mjpeg_decode_init,
  1195. NULL,
  1196. mjpeg_decode_end,
  1197. mjpeg_decode_frame,
  1198. 0,
  1199. NULL
  1200. };