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.

1162 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. /* build all the huffman tables */
  222. build_huffman_codes(m->huff_size_dc_luminance,
  223. m->huff_code_dc_luminance,
  224. bits_dc_luminance,
  225. val_dc_luminance);
  226. build_huffman_codes(m->huff_size_dc_chrominance,
  227. m->huff_code_dc_chrominance,
  228. bits_dc_chrominance,
  229. val_dc_chrominance);
  230. build_huffman_codes(m->huff_size_ac_luminance,
  231. m->huff_code_ac_luminance,
  232. bits_ac_luminance,
  233. val_ac_luminance);
  234. build_huffman_codes(m->huff_size_ac_chrominance,
  235. m->huff_code_ac_chrominance,
  236. bits_ac_chrominance,
  237. val_ac_chrominance);
  238. s->mjpeg_ctx = m;
  239. return 0;
  240. }
  241. void mjpeg_close(MpegEncContext *s)
  242. {
  243. free(s->mjpeg_ctx);
  244. }
  245. static inline void put_marker(PutBitContext *p, int code)
  246. {
  247. put_bits(p, 8, 0xff);
  248. put_bits(p, 8, code);
  249. }
  250. /* table_class: 0 = DC coef, 1 = AC coefs */
  251. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  252. const UINT8 *bits_table, const UINT8 *value_table)
  253. {
  254. PutBitContext *p = &s->pb;
  255. int n, i;
  256. put_bits(p, 4, table_class);
  257. put_bits(p, 4, table_id);
  258. n = 0;
  259. for(i=1;i<=16;i++) {
  260. n += bits_table[i];
  261. put_bits(p, 8, bits_table[i]);
  262. }
  263. for(i=0;i<n;i++)
  264. put_bits(p, 8, value_table[i]);
  265. return n + 17;
  266. }
  267. static void jpeg_table_header(MpegEncContext *s)
  268. {
  269. PutBitContext *p = &s->pb;
  270. int i, j, size;
  271. UINT8 *ptr;
  272. /* quant matrixes */
  273. put_marker(p, DQT);
  274. put_bits(p, 16, 2 + 1 * (1 + 64));
  275. put_bits(p, 4, 0); /* 8 bit precision */
  276. put_bits(p, 4, 0); /* table 0 */
  277. for(i=0;i<64;i++) {
  278. j = zigzag_direct[i];
  279. put_bits(p, 8, s->intra_matrix[j]);
  280. }
  281. #if 0
  282. put_bits(p, 4, 0); /* 8 bit precision */
  283. put_bits(p, 4, 1); /* table 1 */
  284. for(i=0;i<64;i++) {
  285. j = zigzag_direct[i];
  286. put_bits(p, 8, s->chroma_intra_matrix[j]);
  287. }
  288. #endif
  289. /* huffman table */
  290. put_marker(p, DHT);
  291. flush_put_bits(p);
  292. ptr = pbBufPtr(p);
  293. put_bits(p, 16, 0); /* patched later */
  294. size = 2;
  295. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  296. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  297. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  298. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  299. ptr[0] = size >> 8;
  300. ptr[1] = size;
  301. }
  302. void mjpeg_picture_header(MpegEncContext *s)
  303. {
  304. put_marker(&s->pb, SOI);
  305. if (s->mjpeg_write_tables) jpeg_table_header(s);
  306. put_marker(&s->pb, SOF0);
  307. put_bits(&s->pb, 16, 17);
  308. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  309. put_bits(&s->pb, 16, s->height);
  310. put_bits(&s->pb, 16, s->width);
  311. put_bits(&s->pb, 8, 3); /* 3 components */
  312. /* Y component */
  313. put_bits(&s->pb, 8, 1); /* component number */
  314. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  315. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  316. put_bits(&s->pb, 8, 0); /* select matrix */
  317. /* Cb component */
  318. put_bits(&s->pb, 8, 2); /* component number */
  319. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  320. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  321. put_bits(&s->pb, 8, 0); /* select matrix */
  322. /* Cr component */
  323. put_bits(&s->pb, 8, 3); /* component number */
  324. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  325. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  326. put_bits(&s->pb, 8, 0); /* select matrix */
  327. /* scan header */
  328. put_marker(&s->pb, SOS);
  329. put_bits(&s->pb, 16, 12); /* length */
  330. put_bits(&s->pb, 8, 3); /* 3 components */
  331. /* Y component */
  332. put_bits(&s->pb, 8, 1); /* index */
  333. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  334. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  335. /* Cb component */
  336. put_bits(&s->pb, 8, 2); /* index */
  337. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  338. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  339. /* Cr component */
  340. put_bits(&s->pb, 8, 3); /* index */
  341. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  342. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  343. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  344. put_bits(&s->pb, 8, 63); /* Se (not used) */
  345. put_bits(&s->pb, 8, 0); /* (not used) */
  346. }
  347. void mjpeg_picture_trailer(MpegEncContext *s)
  348. {
  349. jflush_put_bits(&s->pb);
  350. put_marker(&s->pb, EOI);
  351. }
  352. static inline void encode_dc(MpegEncContext *s, int val,
  353. UINT8 *huff_size, UINT16 *huff_code)
  354. {
  355. int mant, nbits;
  356. if (val == 0) {
  357. jput_bits(&s->pb, huff_size[0], huff_code[0]);
  358. } else {
  359. mant = val;
  360. if (val < 0) {
  361. val = -val;
  362. mant--;
  363. }
  364. /* compute the log (XXX: optimize) */
  365. nbits = 0;
  366. while (val != 0) {
  367. val = val >> 1;
  368. nbits++;
  369. }
  370. jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  371. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  372. }
  373. }
  374. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  375. {
  376. int mant, nbits, code, i, j;
  377. int component, dc, run, last_index, val;
  378. MJpegContext *m = s->mjpeg_ctx;
  379. UINT8 *huff_size_ac;
  380. UINT16 *huff_code_ac;
  381. /* DC coef */
  382. component = (n <= 3 ? 0 : n - 4 + 1);
  383. dc = block[0]; /* overflow is impossible */
  384. val = dc - s->last_dc[component];
  385. if (n < 4) {
  386. encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  387. huff_size_ac = m->huff_size_ac_luminance;
  388. huff_code_ac = m->huff_code_ac_luminance;
  389. } else {
  390. encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  391. huff_size_ac = m->huff_size_ac_chrominance;
  392. huff_code_ac = m->huff_code_ac_chrominance;
  393. }
  394. s->last_dc[component] = dc;
  395. /* AC coefs */
  396. run = 0;
  397. last_index = s->block_last_index[n];
  398. for(i=1;i<=last_index;i++) {
  399. j = zigzag_direct[i];
  400. val = block[j];
  401. if (val == 0) {
  402. run++;
  403. } else {
  404. while (run >= 16) {
  405. jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  406. run -= 16;
  407. }
  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. code = (run << 4) | nbits;
  420. jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  421. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  422. run = 0;
  423. }
  424. }
  425. /* output EOB only if not already 64 values */
  426. if (last_index < 63 || run != 0)
  427. jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  428. }
  429. void mjpeg_encode_mb(MpegEncContext *s,
  430. DCTELEM block[6][64])
  431. {
  432. int i;
  433. for(i=0;i<6;i++) {
  434. encode_block(s, block[i], i);
  435. }
  436. }
  437. /******************************************/
  438. /* decoding */
  439. /* compressed picture size */
  440. #define PICTURE_BUFFER_SIZE 100000
  441. #define MAX_COMPONENTS 4
  442. typedef struct MJpegDecodeContext {
  443. GetBitContext gb;
  444. UINT32 header_state;
  445. int start_code; /* current start code */
  446. UINT8 *buf_ptr;
  447. int buffer_size;
  448. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  449. INT16 quant_matrixes[4][64];
  450. VLC vlcs[2][4];
  451. int org_width, org_height; /* size given at codec init */
  452. int first_picture; /* true if decoding first picture */
  453. int interlaced; /* true if interlaced */
  454. int bottom_field; /* true if bottom field */
  455. int width, height;
  456. int nb_components;
  457. int component_id[MAX_COMPONENTS];
  458. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  459. int v_count[MAX_COMPONENTS];
  460. int h_max, v_max; /* maximum h and v counts */
  461. int quant_index[4]; /* quant table index for each component */
  462. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  463. UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
  464. int linesize[MAX_COMPONENTS];
  465. DCTELEM block[64] __align8;
  466. UINT8 buffer[PICTURE_BUFFER_SIZE];
  467. int buggy_avid;
  468. } MJpegDecodeContext;
  469. static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  470. int nb_codes)
  471. {
  472. UINT8 huff_size[256];
  473. UINT16 huff_code[256];
  474. memset(huff_size, 0, sizeof(huff_size));
  475. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  476. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  477. }
  478. static int mjpeg_decode_init(AVCodecContext *avctx)
  479. {
  480. MJpegDecodeContext *s = avctx->priv_data;
  481. s->header_state = 0;
  482. s->mpeg_enc_ctx_allocated = 0;
  483. s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
  484. account FF 00 case */
  485. s->start_code = -1;
  486. s->buf_ptr = s->buffer;
  487. s->first_picture = 1;
  488. s->org_width = avctx->width;
  489. s->org_height = avctx->height;
  490. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  491. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  492. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  493. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  494. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  495. {
  496. printf("mjpeg: using external huffman table\n");
  497. mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size);
  498. /* should check for error - but dunno */
  499. }
  500. return 0;
  501. }
  502. /* quantize tables */
  503. static int mjpeg_decode_dqt(MJpegDecodeContext *s,
  504. UINT8 *buf, int buf_size)
  505. {
  506. int len, index, i, j;
  507. init_get_bits(&s->gb, buf, buf_size);
  508. len = get_bits(&s->gb, 16);
  509. len -= 2;
  510. while (len >= 65) {
  511. /* only 8 bit precision handled */
  512. if (get_bits(&s->gb, 4) != 0)
  513. {
  514. dprintf("dqt: 16bit precision\n");
  515. return -1;
  516. }
  517. index = get_bits(&s->gb, 4);
  518. if (index >= 4)
  519. return -1;
  520. dprintf("index=%d\n", index);
  521. /* read quant table */
  522. for(i=0;i<64;i++) {
  523. j = zigzag_direct[i];
  524. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  525. }
  526. len -= 65;
  527. }
  528. return 0;
  529. }
  530. /* decode huffman tables and build VLC decoders */
  531. static int mjpeg_decode_dht(MJpegDecodeContext *s,
  532. UINT8 *buf, int buf_size)
  533. {
  534. int len, index, i, class, n, v, code_max;
  535. UINT8 bits_table[17];
  536. UINT8 val_table[256];
  537. init_get_bits(&s->gb, buf, buf_size);
  538. len = get_bits(&s->gb, 16);
  539. len -= 2;
  540. while (len > 0) {
  541. if (len < 17)
  542. return -1;
  543. class = get_bits(&s->gb, 4);
  544. if (class >= 2)
  545. return -1;
  546. index = get_bits(&s->gb, 4);
  547. if (index >= 4)
  548. return -1;
  549. n = 0;
  550. for(i=1;i<=16;i++) {
  551. bits_table[i] = get_bits(&s->gb, 8);
  552. n += bits_table[i];
  553. }
  554. len -= 17;
  555. if (len < n || n > 256)
  556. return -1;
  557. code_max = 0;
  558. for(i=0;i<n;i++) {
  559. v = get_bits(&s->gb, 8);
  560. if (v > code_max)
  561. code_max = v;
  562. val_table[i] = v;
  563. }
  564. len -= n;
  565. /* build VLC and flush previous vlc if present */
  566. free_vlc(&s->vlcs[class][index]);
  567. dprintf("class=%d index=%d nb_codes=%d\n",
  568. class, index, code_max + 1);
  569. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  570. }
  571. return 0;
  572. }
  573. static int mjpeg_decode_sof0(MJpegDecodeContext *s,
  574. UINT8 *buf, int buf_size)
  575. {
  576. int len, nb_components, i, width, height;
  577. init_get_bits(&s->gb, buf, buf_size);
  578. /* XXX: verify len field validity */
  579. len = get_bits(&s->gb, 16);
  580. /* only 8 bits/component accepted */
  581. if (get_bits(&s->gb, 8) != 8)
  582. return -1;
  583. height = get_bits(&s->gb, 16);
  584. width = get_bits(&s->gb, 16);
  585. nb_components = get_bits(&s->gb, 8);
  586. if (nb_components <= 0 ||
  587. nb_components > MAX_COMPONENTS)
  588. return -1;
  589. s->nb_components = nb_components;
  590. s->h_max = 1;
  591. s->v_max = 1;
  592. for(i=0;i<nb_components;i++) {
  593. /* component id */
  594. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  595. s->h_count[i] = get_bits(&s->gb, 4);
  596. s->v_count[i] = get_bits(&s->gb, 4);
  597. /* compute hmax and vmax (only used in interleaved case) */
  598. if (s->h_count[i] > s->h_max)
  599. s->h_max = s->h_count[i];
  600. if (s->v_count[i] > s->v_max)
  601. s->v_max = s->v_count[i];
  602. s->quant_index[i] = get_bits(&s->gb, 8);
  603. if (s->quant_index[i] >= 4)
  604. return -1;
  605. dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
  606. }
  607. /* if different size, realloc/alloc picture */
  608. /* XXX: also check h_count and v_count */
  609. if (width != s->width || height != s->height) {
  610. for(i=0;i<MAX_COMPONENTS;i++) {
  611. free(s->current_picture[i]);
  612. s->current_picture[i] = NULL;
  613. }
  614. s->width = width;
  615. s->height = height;
  616. /* test interlaced mode */
  617. if (s->first_picture &&
  618. s->org_height != 0 &&
  619. s->height < ((s->org_height * 3) / 4)) {
  620. s->interlaced = 1;
  621. s->bottom_field = 0;
  622. }
  623. for(i=0;i<nb_components;i++) {
  624. int w, h;
  625. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  626. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  627. w = w * 8 * s->h_count[i];
  628. h = h * 8 * s->v_count[i];
  629. if (s->interlaced)
  630. w *= 2;
  631. s->linesize[i] = w;
  632. /* memory test is done in mjpeg_decode_sos() */
  633. s->current_picture[i] = av_mallocz(w * h);
  634. }
  635. s->first_picture = 0;
  636. }
  637. if (len != (8+(3*nb_components)))
  638. {
  639. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  640. }
  641. return 0;
  642. }
  643. static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
  644. {
  645. int code, diff;
  646. code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
  647. if (code < 0)
  648. {
  649. dprintf("decode_dc: bad vlc: %d:%d\n", 0, dc_index);
  650. return 0xffff;
  651. }
  652. if (code == 0) {
  653. diff = 0;
  654. // dprintf("decode_dc: bad code\n");
  655. } else {
  656. diff = get_bits(&s->gb, code);
  657. if ((diff & (1 << (code - 1))) == 0)
  658. diff = (-1 << code) | (diff + 1);
  659. }
  660. return diff;
  661. }
  662. /* decode block and dequantize */
  663. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  664. int component, int dc_index, int ac_index, int quant_index)
  665. {
  666. int nbits, code, i, j, level;
  667. int run, val;
  668. VLC *ac_vlc;
  669. INT16 *quant_matrix;
  670. /* DC coef */
  671. val = decode_dc(s, dc_index);
  672. if (val == 0xffff) {
  673. dprintf("error dc\n");
  674. return -1;
  675. }
  676. quant_matrix = s->quant_matrixes[quant_index];
  677. val = val * quant_matrix[0] + s->last_dc[component];
  678. s->last_dc[component] = val;
  679. block[0] = val;
  680. /* AC coefs */
  681. ac_vlc = &s->vlcs[1][ac_index];
  682. i = 1;
  683. for(;;) {
  684. code = get_vlc(&s->gb, ac_vlc);
  685. if (code < 0) {
  686. dprintf("error ac\n");
  687. return -1;
  688. }
  689. /* EOB */
  690. if (code == 0)
  691. break;
  692. if (code == 0xf0) {
  693. i += 16;
  694. } else {
  695. run = code >> 4;
  696. nbits = code & 0xf;
  697. level = get_bits(&s->gb, nbits);
  698. if ((level & (1 << (nbits - 1))) == 0)
  699. level = (-1 << nbits) | (level + 1);
  700. i += run;
  701. if (i >= 64) {
  702. dprintf("error count: %d\n", i);
  703. return -1;
  704. }
  705. j = zigzag_direct[i];
  706. block[j] = level * quant_matrix[j];
  707. i++;
  708. if (i >= 64)
  709. break;
  710. }
  711. }
  712. return 0;
  713. }
  714. static int mjpeg_decode_sos(MJpegDecodeContext *s,
  715. UINT8 *buf, int buf_size)
  716. {
  717. int len, nb_components, i, j, n, h, v, ret;
  718. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  719. int comp_index[4];
  720. int dc_index[4];
  721. int ac_index[4];
  722. int nb_blocks[4];
  723. int h_count[4];
  724. int v_count[4];
  725. init_get_bits(&s->gb, buf, buf_size);
  726. /* XXX: verify len field validity */
  727. len = get_bits(&s->gb, 16);
  728. nb_components = get_bits(&s->gb, 8);
  729. /* XXX: only interleaved scan accepted */
  730. if (nb_components != 3)
  731. {
  732. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  733. return -1;
  734. }
  735. vmax = 0;
  736. hmax = 0;
  737. for(i=0;i<nb_components;i++) {
  738. id = get_bits(&s->gb, 8) - 1;
  739. /* find component index */
  740. for(index=0;index<s->nb_components;index++)
  741. if (id == s->component_id[index])
  742. break;
  743. if (index == s->nb_components)
  744. {
  745. dprintf("decode_sos: index out of components\n");
  746. return -1;
  747. }
  748. comp_index[i] = index;
  749. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  750. h_count[i] = s->h_count[index];
  751. v_count[i] = s->v_count[index];
  752. dc_index[i] = get_bits(&s->gb, 4);
  753. ac_index[i] = get_bits(&s->gb, 4);
  754. if (dc_index[i] < 0 || ac_index[i] < 0 ||
  755. dc_index[i] >= 4 || ac_index[i] >= 4)
  756. goto out_of_range;
  757. switch(s->start_code)
  758. {
  759. case SOF0:
  760. if (dc_index[i] > 1 || ac_index[i] > 1)
  761. goto out_of_range;
  762. break;
  763. case SOF1:
  764. case SOF2:
  765. if (dc_index[i] > 3 || ac_index[i] > 3)
  766. goto out_of_range;
  767. break;
  768. case SOF3:
  769. if (dc_index[i] > 3 || ac_index[i] != 0)
  770. goto out_of_range;
  771. break;
  772. }
  773. }
  774. get_bits(&s->gb, 8); /* Ss */
  775. get_bits(&s->gb, 8); /* Se */
  776. get_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
  777. for(i=0;i<nb_components;i++)
  778. s->last_dc[i] = 1024;
  779. if (nb_components > 1) {
  780. /* interleaved stream */
  781. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  782. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  783. } else {
  784. h = s->h_max / s->h_count[comp_index[0]];
  785. v = s->v_max / s->v_count[comp_index[0]];
  786. mb_width = (s->width + h * 8 - 1) / (h * 8);
  787. mb_height = (s->height + v * 8 - 1) / (v * 8);
  788. nb_blocks[0] = 1;
  789. h_count[0] = 1;
  790. v_count[0] = 1;
  791. }
  792. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  793. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  794. for(i=0;i<nb_components;i++) {
  795. UINT8 *ptr;
  796. int x, y, c;
  797. n = nb_blocks[i];
  798. c = comp_index[i];
  799. h = h_count[i];
  800. v = v_count[i];
  801. x = 0;
  802. y = 0;
  803. for(j=0;j<n;j++) {
  804. memset(s->block, 0, sizeof(s->block));
  805. if (decode_block(s, s->block, i,
  806. dc_index[i], ac_index[i],
  807. s->quant_index[c]) < 0) {
  808. dprintf("error %d %d\n", mb_y, mb_x);
  809. ret = -1;
  810. goto the_end;
  811. }
  812. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  813. ff_idct (s->block);
  814. ptr = s->current_picture[c] +
  815. (s->linesize[c] * (v * mb_y + y) * 8) +
  816. (h * mb_x + x) * 8;
  817. if (s->interlaced && s->bottom_field)
  818. ptr += s->linesize[c] >> 1;
  819. put_pixels_clamped(s->block, ptr, s->linesize[c]);
  820. if (++x == h) {
  821. x = 0;
  822. y++;
  823. }
  824. }
  825. }
  826. }
  827. }
  828. ret = 0;
  829. the_end:
  830. emms_c();
  831. return ret;
  832. out_of_range:
  833. dprintf("decode_sos: ac/dc index out of range\n");
  834. return -1;
  835. }
  836. #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d)
  837. static int mjpeg_decode_app(MJpegDecodeContext *s,
  838. UINT8 *buf, int buf_size)
  839. {
  840. int len, id;
  841. init_get_bits(&s->gb, buf, buf_size);
  842. /* XXX: verify len field validity */
  843. len = get_bits(&s->gb, 16)-2;
  844. if (len < 5)
  845. return -1;
  846. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  847. if (get_bits(&s->gb, 8) != 0)
  848. return -1;
  849. /* buggy avid, it puts EOI only at every 10th frame */
  850. if (id == FOURCC('A','V','I','1'))
  851. {
  852. s->buggy_avid = 1;
  853. if (s->first_picture)
  854. printf("mjpeg: workarounding buggy AVID\n");
  855. }
  856. /* if id == JFIF, we can extract some infos (aspect ratio), others
  857. are useless */
  858. /* should check for further values.. */
  859. return 0;
  860. }
  861. #undef FOURCC
  862. /* return the 8 bit start code value and update the search
  863. state. Return -1 if no start code found */
  864. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
  865. UINT32 *header_state)
  866. {
  867. UINT8 *buf_ptr;
  868. unsigned int state, v;
  869. int val;
  870. state = *header_state;
  871. buf_ptr = *pbuf_ptr;
  872. if (state) {
  873. /* get marker */
  874. found:
  875. if (buf_ptr < buf_end) {
  876. val = *buf_ptr++;
  877. state = 0;
  878. } else {
  879. val = -1;
  880. }
  881. } else {
  882. while (buf_ptr < buf_end) {
  883. v = *buf_ptr++;
  884. if (v == 0xff) {
  885. state = 1;
  886. goto found;
  887. }
  888. }
  889. val = -1;
  890. }
  891. *pbuf_ptr = buf_ptr;
  892. *header_state = state;
  893. return val;
  894. }
  895. static int mjpeg_decode_frame(AVCodecContext *avctx,
  896. void *data, int *data_size,
  897. UINT8 *buf, int buf_size)
  898. {
  899. MJpegDecodeContext *s = avctx->priv_data;
  900. UINT8 *buf_end, *buf_ptr, *buf_start;
  901. int len, code, input_size, i;
  902. AVPicture *picture = data;
  903. unsigned int start_code;
  904. *data_size = 0;
  905. /* no supplementary picture */
  906. if (buf_size == 0)
  907. return 0;
  908. buf_ptr = buf;
  909. buf_end = buf + buf_size;
  910. while (buf_ptr < buf_end) {
  911. buf_start = buf_ptr;
  912. /* find start next marker */
  913. code = find_marker(&buf_ptr, buf_end, &s->header_state);
  914. /* copy to buffer */
  915. len = buf_ptr - buf_start;
  916. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  917. /* data too big : flush */
  918. s->buf_ptr = s->buffer;
  919. if (code > 0)
  920. s->start_code = code;
  921. } else {
  922. memcpy(s->buf_ptr, buf_start, len);
  923. s->buf_ptr += len;
  924. /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
  925. /* valid marker code is between 00 and ff - alex */
  926. if (code <= 0 || code >= 0xff) {
  927. s->buf_ptr--;
  928. } else {
  929. /* prepare data for next start code */
  930. input_size = s->buf_ptr - s->buffer;
  931. start_code = s->start_code;
  932. s->buf_ptr = s->buffer;
  933. s->start_code = code;
  934. dprintf("marker=%x\n", start_code);
  935. switch(start_code) {
  936. case SOI:
  937. /* nothing to do on SOI */
  938. break;
  939. case DQT:
  940. mjpeg_decode_dqt(s, s->buffer, input_size);
  941. break;
  942. case DHT:
  943. mjpeg_decode_dht(s, s->buffer, input_size);
  944. break;
  945. case SOF0:
  946. mjpeg_decode_sof0(s, s->buffer, input_size);
  947. break;
  948. case SOS:
  949. mjpeg_decode_sos(s, s->buffer, input_size);
  950. if (s->start_code == EOI || s->buggy_avid) {
  951. int l;
  952. if (s->interlaced) {
  953. s->bottom_field ^= 1;
  954. /* if not bottom field, do not output image yet */
  955. if (s->bottom_field)
  956. goto the_end;
  957. }
  958. for(i=0;i<3;i++) {
  959. picture->data[i] = s->current_picture[i];
  960. l = s->linesize[i];
  961. if (s->interlaced)
  962. l >>= 1;
  963. picture->linesize[i] = l;
  964. }
  965. *data_size = sizeof(AVPicture);
  966. avctx->height = s->height;
  967. if (s->interlaced)
  968. avctx->height *= 2;
  969. avctx->width = s->width;
  970. /* XXX: not complete test ! */
  971. switch((s->h_count[0] << 4) | s->v_count[0]) {
  972. case 0x11:
  973. avctx->pix_fmt = PIX_FMT_YUV444P;
  974. break;
  975. case 0x21:
  976. avctx->pix_fmt = PIX_FMT_YUV422P;
  977. break;
  978. default:
  979. case 0x22:
  980. avctx->pix_fmt = PIX_FMT_YUV420P;
  981. break;
  982. }
  983. /* dummy quality */
  984. /* XXX: infer it with matrix */
  985. avctx->quality = 3;
  986. goto the_end;
  987. }
  988. break;
  989. case APP0:
  990. mjpeg_decode_app(s, s->buffer, input_size);
  991. break;
  992. case SOF1:
  993. case SOF2:
  994. case SOF3:
  995. case SOF5:
  996. case SOF6:
  997. case SOF7:
  998. case SOF9:
  999. case SOF10:
  1000. case SOF11:
  1001. case SOF13:
  1002. case SOF14:
  1003. case SOF15:
  1004. case JPG:
  1005. printf("mjpeg: unsupported coding type (%x)\n", start_code);
  1006. return -1;
  1007. }
  1008. }
  1009. }
  1010. }
  1011. the_end:
  1012. return buf_ptr - buf;
  1013. }
  1014. static int mjpeg_decode_end(AVCodecContext *avctx)
  1015. {
  1016. MJpegDecodeContext *s = avctx->priv_data;
  1017. int i, j;
  1018. for(i=0;i<MAX_COMPONENTS;i++)
  1019. free(s->current_picture[i]);
  1020. for(i=0;i<2;i++) {
  1021. for(j=0;j<4;j++)
  1022. free_vlc(&s->vlcs[i][j]);
  1023. }
  1024. return 0;
  1025. }
  1026. AVCodec mjpeg_decoder = {
  1027. "mjpeg",
  1028. CODEC_TYPE_VIDEO,
  1029. CODEC_ID_MJPEG,
  1030. sizeof(MJpegDecodeContext),
  1031. mjpeg_decode_init,
  1032. NULL,
  1033. mjpeg_decode_end,
  1034. mjpeg_decode_frame,
  1035. };