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.

1163 lines
34KB

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