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.

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