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.

982 lines
30KB

  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. //#define DEBUG
  20. #include "avcodec.h"
  21. #include "dsputil.h"
  22. #include "mpegvideo.h"
  23. typedef struct MJpegContext {
  24. UINT8 huff_size_dc_luminance[12];
  25. UINT16 huff_code_dc_luminance[12];
  26. UINT8 huff_size_dc_chrominance[12];
  27. UINT16 huff_code_dc_chrominance[12];
  28. UINT8 huff_size_ac_luminance[256];
  29. UINT16 huff_code_ac_luminance[256];
  30. UINT8 huff_size_ac_chrominance[256];
  31. UINT16 huff_code_ac_chrominance[256];
  32. } MJpegContext;
  33. #define SOF0 0xc0
  34. #define SOI 0xd8
  35. #define EOI 0xd9
  36. #define DQT 0xdb
  37. #define DHT 0xc4
  38. #define SOS 0xda
  39. #if 0
  40. /* These are the sample quantization tables given in JPEG spec section K.1.
  41. * The spec says that the values given produce "good" quality, and
  42. * when divided by 2, "very good" quality.
  43. */
  44. static const unsigned char std_luminance_quant_tbl[64] = {
  45. 16, 11, 10, 16, 24, 40, 51, 61,
  46. 12, 12, 14, 19, 26, 58, 60, 55,
  47. 14, 13, 16, 24, 40, 57, 69, 56,
  48. 14, 17, 22, 29, 51, 87, 80, 62,
  49. 18, 22, 37, 56, 68, 109, 103, 77,
  50. 24, 35, 55, 64, 81, 104, 113, 92,
  51. 49, 64, 78, 87, 103, 121, 120, 101,
  52. 72, 92, 95, 98, 112, 100, 103, 99
  53. };
  54. static const unsigned char std_chrominance_quant_tbl[64] = {
  55. 17, 18, 24, 47, 99, 99, 99, 99,
  56. 18, 21, 26, 66, 99, 99, 99, 99,
  57. 24, 26, 56, 99, 99, 99, 99, 99,
  58. 47, 66, 99, 99, 99, 99, 99, 99,
  59. 99, 99, 99, 99, 99, 99, 99, 99,
  60. 99, 99, 99, 99, 99, 99, 99, 99,
  61. 99, 99, 99, 99, 99, 99, 99, 99,
  62. 99, 99, 99, 99, 99, 99, 99, 99
  63. };
  64. #endif
  65. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  66. /* IMPORTANT: these are only valid for 8-bit data precision! */
  67. static const UINT8 bits_dc_luminance[17] =
  68. { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  69. static const UINT8 val_dc_luminance[] =
  70. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  71. static const UINT8 bits_dc_chrominance[17] =
  72. { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  73. static const UINT8 val_dc_chrominance[] =
  74. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  75. static const UINT8 bits_ac_luminance[17] =
  76. { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  77. static const UINT8 val_ac_luminance[] =
  78. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  79. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  80. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  81. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  82. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  83. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  84. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  85. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  86. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  87. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  88. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  89. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  90. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  91. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  92. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  93. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  94. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  95. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  96. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  97. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  98. 0xf9, 0xfa
  99. };
  100. static const UINT8 bits_ac_chrominance[17] =
  101. { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  102. static const UINT8 val_ac_chrominance[] =
  103. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  104. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  105. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  106. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  107. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  108. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  109. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  110. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  111. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  112. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  113. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  114. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  115. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  116. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  117. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  118. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  119. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  120. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  121. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  122. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  123. 0xf9, 0xfa
  124. };
  125. /* isn't this function nicer than the one in the libjpeg ? */
  126. static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
  127. const UINT8 *bits_table, const UINT8 *val_table)
  128. {
  129. int i, j, k,nb, code, sym;
  130. code = 0;
  131. k = 0;
  132. for(i=1;i<=16;i++) {
  133. nb = bits_table[i];
  134. for(j=0;j<nb;j++) {
  135. sym = val_table[k++];
  136. huff_size[sym] = i;
  137. huff_code[sym] = code;
  138. code++;
  139. }
  140. code <<= 1;
  141. }
  142. }
  143. int mjpeg_init(MpegEncContext *s)
  144. {
  145. MJpegContext *m;
  146. m = malloc(sizeof(MJpegContext));
  147. if (!m)
  148. return -1;
  149. s->min_qcoeff=-1023;
  150. s->max_qcoeff= 1023;
  151. /* build all the huffman tables */
  152. build_huffman_codes(m->huff_size_dc_luminance,
  153. m->huff_code_dc_luminance,
  154. bits_dc_luminance,
  155. val_dc_luminance);
  156. build_huffman_codes(m->huff_size_dc_chrominance,
  157. m->huff_code_dc_chrominance,
  158. bits_dc_chrominance,
  159. val_dc_chrominance);
  160. build_huffman_codes(m->huff_size_ac_luminance,
  161. m->huff_code_ac_luminance,
  162. bits_ac_luminance,
  163. val_ac_luminance);
  164. build_huffman_codes(m->huff_size_ac_chrominance,
  165. m->huff_code_ac_chrominance,
  166. bits_ac_chrominance,
  167. val_ac_chrominance);
  168. s->mjpeg_ctx = m;
  169. return 0;
  170. }
  171. void mjpeg_close(MpegEncContext *s)
  172. {
  173. free(s->mjpeg_ctx);
  174. }
  175. static inline void put_marker(PutBitContext *p, int code)
  176. {
  177. put_bits(p, 8, 0xff);
  178. put_bits(p, 8, code);
  179. }
  180. /* table_class: 0 = DC coef, 1 = AC coefs */
  181. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  182. const UINT8 *bits_table, const UINT8 *value_table)
  183. {
  184. PutBitContext *p = &s->pb;
  185. int n, i;
  186. put_bits(p, 4, table_class);
  187. put_bits(p, 4, table_id);
  188. n = 0;
  189. for(i=1;i<=16;i++) {
  190. n += bits_table[i];
  191. put_bits(p, 8, bits_table[i]);
  192. }
  193. for(i=0;i<n;i++)
  194. put_bits(p, 8, value_table[i]);
  195. return n + 17;
  196. }
  197. static void jpeg_table_header(MpegEncContext *s)
  198. {
  199. PutBitContext *p = &s->pb;
  200. int i, j, size;
  201. UINT8 *ptr;
  202. /* quant matrixes */
  203. put_marker(p, DQT);
  204. put_bits(p, 16, 2 + 1 * (1 + 64));
  205. put_bits(p, 4, 0); /* 8 bit precision */
  206. put_bits(p, 4, 0); /* table 0 */
  207. for(i=0;i<64;i++) {
  208. j = zigzag_direct[i];
  209. put_bits(p, 8, s->intra_matrix[j]);
  210. }
  211. #if 0
  212. put_bits(p, 4, 0); /* 8 bit precision */
  213. put_bits(p, 4, 1); /* table 1 */
  214. for(i=0;i<64;i++) {
  215. j = zigzag_direct[i];
  216. put_bits(p, 8, s->chroma_intra_matrix[j]);
  217. }
  218. #endif
  219. /* huffman table */
  220. put_marker(p, DHT);
  221. flush_put_bits(p);
  222. ptr = pbBufPtr(p);
  223. put_bits(p, 16, 0); /* patched later */
  224. size = 2;
  225. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  226. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  227. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  228. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  229. ptr[0] = size >> 8;
  230. ptr[1] = size;
  231. }
  232. void mjpeg_picture_header(MpegEncContext *s)
  233. {
  234. put_marker(&s->pb, SOI);
  235. if (s->mjpeg_write_tables) jpeg_table_header(s);
  236. put_marker(&s->pb, SOF0);
  237. put_bits(&s->pb, 16, 17);
  238. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  239. put_bits(&s->pb, 16, s->height);
  240. put_bits(&s->pb, 16, s->width);
  241. put_bits(&s->pb, 8, 3); /* 3 components */
  242. /* Y component */
  243. put_bits(&s->pb, 8, 1); /* component number */
  244. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  245. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  246. put_bits(&s->pb, 8, 0); /* select matrix */
  247. /* Cb component */
  248. put_bits(&s->pb, 8, 2); /* component number */
  249. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  250. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  251. put_bits(&s->pb, 8, 0); /* select matrix */
  252. /* Cr component */
  253. put_bits(&s->pb, 8, 3); /* component number */
  254. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  255. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  256. put_bits(&s->pb, 8, 0); /* select matrix */
  257. /* scan header */
  258. put_marker(&s->pb, SOS);
  259. put_bits(&s->pb, 16, 12); /* length */
  260. put_bits(&s->pb, 8, 3); /* 3 components */
  261. /* Y component */
  262. put_bits(&s->pb, 8, 1); /* index */
  263. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  264. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  265. /* Cb component */
  266. put_bits(&s->pb, 8, 2); /* index */
  267. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  268. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  269. /* Cr component */
  270. put_bits(&s->pb, 8, 3); /* index */
  271. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  272. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  273. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  274. put_bits(&s->pb, 8, 63); /* Se (not used) */
  275. put_bits(&s->pb, 8, 0); /* (not used) */
  276. }
  277. void mjpeg_picture_trailer(MpegEncContext *s)
  278. {
  279. jflush_put_bits(&s->pb);
  280. put_marker(&s->pb, EOI);
  281. }
  282. static inline void encode_dc(MpegEncContext *s, int val,
  283. UINT8 *huff_size, UINT16 *huff_code)
  284. {
  285. int mant, nbits;
  286. if (val == 0) {
  287. jput_bits(&s->pb, huff_size[0], huff_code[0]);
  288. } else {
  289. mant = val;
  290. if (val < 0) {
  291. val = -val;
  292. mant--;
  293. }
  294. /* compute the log (XXX: optimize) */
  295. nbits = 0;
  296. while (val != 0) {
  297. val = val >> 1;
  298. nbits++;
  299. }
  300. jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  301. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  302. }
  303. }
  304. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  305. {
  306. int mant, nbits, code, i, j;
  307. int component, dc, run, last_index, val;
  308. MJpegContext *m = s->mjpeg_ctx;
  309. UINT8 *huff_size_ac;
  310. UINT16 *huff_code_ac;
  311. /* DC coef */
  312. component = (n <= 3 ? 0 : n - 4 + 1);
  313. dc = block[0]; /* overflow is impossible */
  314. val = dc - s->last_dc[component];
  315. if (n < 4) {
  316. encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  317. huff_size_ac = m->huff_size_ac_luminance;
  318. huff_code_ac = m->huff_code_ac_luminance;
  319. } else {
  320. encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  321. huff_size_ac = m->huff_size_ac_chrominance;
  322. huff_code_ac = m->huff_code_ac_chrominance;
  323. }
  324. s->last_dc[component] = dc;
  325. /* AC coefs */
  326. run = 0;
  327. last_index = s->block_last_index[n];
  328. for(i=1;i<=last_index;i++) {
  329. j = zigzag_direct[i];
  330. val = block[j];
  331. if (val == 0) {
  332. run++;
  333. } else {
  334. while (run >= 16) {
  335. jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  336. run -= 16;
  337. }
  338. mant = val;
  339. if (val < 0) {
  340. val = -val;
  341. mant--;
  342. }
  343. /* compute the log (XXX: optimize) */
  344. nbits = 0;
  345. while (val != 0) {
  346. val = val >> 1;
  347. nbits++;
  348. }
  349. code = (run << 4) | nbits;
  350. jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  351. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  352. run = 0;
  353. }
  354. }
  355. /* output EOB only if not already 64 values */
  356. if (last_index < 63 || run != 0)
  357. jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  358. }
  359. void mjpeg_encode_mb(MpegEncContext *s,
  360. DCTELEM block[6][64])
  361. {
  362. int i;
  363. for(i=0;i<6;i++) {
  364. encode_block(s, block[i], i);
  365. }
  366. }
  367. /******************************************/
  368. /* decoding */
  369. /* compressed picture size */
  370. #define PICTURE_BUFFER_SIZE 100000
  371. #define MAX_COMPONENTS 4
  372. typedef struct MJpegDecodeContext {
  373. GetBitContext gb;
  374. UINT32 header_state;
  375. int start_code; /* current start code */
  376. UINT8 *buf_ptr;
  377. int buffer_size;
  378. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  379. INT16 quant_matrixes[4][64];
  380. VLC vlcs[2][4];
  381. int org_width, org_height; /* size given at codec init */
  382. int first_picture; /* true if decoding first picture */
  383. int interlaced; /* true if interlaced */
  384. int bottom_field; /* true if bottom field */
  385. int width, height;
  386. int nb_components;
  387. int component_id[MAX_COMPONENTS];
  388. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  389. int v_count[MAX_COMPONENTS];
  390. int h_max, v_max; /* maximum h and v counts */
  391. int quant_index[4]; /* quant table index for each component */
  392. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  393. UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
  394. int linesize[MAX_COMPONENTS];
  395. DCTELEM block[64] __align8;
  396. UINT8 buffer[PICTURE_BUFFER_SIZE];
  397. } MJpegDecodeContext;
  398. static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  399. int nb_codes)
  400. {
  401. UINT8 huff_size[256];
  402. UINT16 huff_code[256];
  403. memset(huff_size, 0, sizeof(huff_size));
  404. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  405. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  406. }
  407. static int mjpeg_decode_init(AVCodecContext *avctx)
  408. {
  409. MJpegDecodeContext *s = avctx->priv_data;
  410. s->header_state = 0;
  411. s->mpeg_enc_ctx_allocated = 0;
  412. s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
  413. account FF 00 case */
  414. s->start_code = -1;
  415. s->buf_ptr = s->buffer;
  416. s->first_picture = 1;
  417. s->org_width = avctx->width;
  418. s->org_height = avctx->height;
  419. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  420. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  421. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  422. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  423. return 0;
  424. }
  425. /* quantize tables */
  426. static int mjpeg_decode_dqt(MJpegDecodeContext *s,
  427. UINT8 *buf, int buf_size)
  428. {
  429. int len, index, i, j;
  430. init_get_bits(&s->gb, buf, buf_size);
  431. len = get_bits(&s->gb, 16);
  432. len -= 2;
  433. while (len >= 65) {
  434. /* only 8 bit precision handled */
  435. if (get_bits(&s->gb, 4) != 0)
  436. return -1;
  437. index = get_bits(&s->gb, 4);
  438. if (index >= 4)
  439. return -1;
  440. dprintf("index=%d\n", index);
  441. /* read quant table */
  442. for(i=0;i<64;i++) {
  443. j = zigzag_direct[i];
  444. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  445. }
  446. len -= 65;
  447. }
  448. return 0;
  449. }
  450. /* decode huffman tables and build VLC decoders */
  451. static int mjpeg_decode_dht(MJpegDecodeContext *s,
  452. UINT8 *buf, int buf_size)
  453. {
  454. int len, index, i, class, n, v, code_max;
  455. UINT8 bits_table[17];
  456. UINT8 val_table[256];
  457. init_get_bits(&s->gb, buf, buf_size);
  458. len = get_bits(&s->gb, 16);
  459. len -= 2;
  460. while (len > 0) {
  461. if (len < 17)
  462. return -1;
  463. class = get_bits(&s->gb, 4);
  464. if (class >= 2)
  465. return -1;
  466. index = get_bits(&s->gb, 4);
  467. if (index >= 4)
  468. return -1;
  469. n = 0;
  470. for(i=1;i<=16;i++) {
  471. bits_table[i] = get_bits(&s->gb, 8);
  472. n += bits_table[i];
  473. }
  474. len -= 17;
  475. if (len < n || n > 256)
  476. return -1;
  477. code_max = 0;
  478. for(i=0;i<n;i++) {
  479. v = get_bits(&s->gb, 8);
  480. if (v > code_max)
  481. code_max = v;
  482. val_table[i] = v;
  483. }
  484. len -= n;
  485. /* build VLC and flush previous vlc if present */
  486. free_vlc(&s->vlcs[class][index]);
  487. dprintf("class=%d index=%d nb_codes=%d\n",
  488. class, index, code_max + 1);
  489. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  490. }
  491. return 0;
  492. }
  493. static int mjpeg_decode_sof0(MJpegDecodeContext *s,
  494. UINT8 *buf, int buf_size)
  495. {
  496. int len, nb_components, i, width, height;
  497. init_get_bits(&s->gb, buf, buf_size);
  498. /* XXX: verify len field validity */
  499. len = get_bits(&s->gb, 16);
  500. /* only 8 bits/component accepted */
  501. if (get_bits(&s->gb, 8) != 8)
  502. return -1;
  503. height = get_bits(&s->gb, 16);
  504. width = get_bits(&s->gb, 16);
  505. nb_components = get_bits(&s->gb, 8);
  506. if (nb_components <= 0 ||
  507. nb_components > MAX_COMPONENTS)
  508. return -1;
  509. s->nb_components = nb_components;
  510. s->h_max = 1;
  511. s->v_max = 1;
  512. for(i=0;i<nb_components;i++) {
  513. /* component id */
  514. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  515. s->h_count[i] = get_bits(&s->gb, 4);
  516. s->v_count[i] = get_bits(&s->gb, 4);
  517. /* compute hmax and vmax (only used in interleaved case) */
  518. if (s->h_count[i] > s->h_max)
  519. s->h_max = s->h_count[i];
  520. if (s->v_count[i] > s->v_max)
  521. s->v_max = s->v_count[i];
  522. s->quant_index[i] = get_bits(&s->gb, 8);
  523. if (s->quant_index[i] >= 4)
  524. return -1;
  525. dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
  526. }
  527. /* if different size, realloc/alloc picture */
  528. /* XXX: also check h_count and v_count */
  529. if (width != s->width || height != s->height) {
  530. for(i=0;i<MAX_COMPONENTS;i++) {
  531. free(s->current_picture[i]);
  532. s->current_picture[i] = NULL;
  533. }
  534. s->width = width;
  535. s->height = height;
  536. /* test interlaced mode */
  537. if (s->first_picture &&
  538. s->org_height != 0 &&
  539. s->height < ((s->org_height * 3) / 4)) {
  540. s->interlaced = 1;
  541. s->bottom_field = 0;
  542. }
  543. for(i=0;i<nb_components;i++) {
  544. int w, h;
  545. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  546. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  547. w = w * 8 * s->h_count[i];
  548. h = h * 8 * s->v_count[i];
  549. if (s->interlaced)
  550. w *= 2;
  551. s->linesize[i] = w;
  552. /* memory test is done in mjpeg_decode_sos() */
  553. s->current_picture[i] = av_mallocz(w * h);
  554. }
  555. s->first_picture = 0;
  556. }
  557. return 0;
  558. }
  559. static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
  560. {
  561. VLC *dc_vlc;
  562. int code, diff;
  563. dc_vlc = &s->vlcs[0][dc_index];
  564. code = get_vlc(&s->gb, dc_vlc);
  565. if (code < 0)
  566. return 0xffff;
  567. if (code == 0) {
  568. diff = 0;
  569. } else {
  570. diff = get_bits(&s->gb, code);
  571. if ((diff & (1 << (code - 1))) == 0)
  572. diff = (-1 << code) | (diff + 1);
  573. }
  574. return diff;
  575. }
  576. /* decode block and dequantize */
  577. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  578. int component, int dc_index, int ac_index, int quant_index)
  579. {
  580. int nbits, code, i, j, level;
  581. int run, val;
  582. VLC *ac_vlc;
  583. INT16 *quant_matrix;
  584. quant_matrix = s->quant_matrixes[quant_index];
  585. /* DC coef */
  586. val = decode_dc(s, dc_index);
  587. if (val == 0xffff) {
  588. dprintf("error dc\n");
  589. return -1;
  590. }
  591. val = val * quant_matrix[0] + s->last_dc[component];
  592. s->last_dc[component] = val;
  593. block[0] = val;
  594. /* AC coefs */
  595. ac_vlc = &s->vlcs[1][ac_index];
  596. i = 1;
  597. for(;;) {
  598. code = get_vlc(&s->gb, ac_vlc);
  599. if (code < 0) {
  600. dprintf("error ac\n");
  601. return -1;
  602. }
  603. /* EOB */
  604. if (code == 0)
  605. break;
  606. if (code == 0xf0) {
  607. i += 16;
  608. } else {
  609. run = code >> 4;
  610. nbits = code & 0xf;
  611. level = get_bits(&s->gb, nbits);
  612. if ((level & (1 << (nbits - 1))) == 0)
  613. level = (-1 << nbits) | (level + 1);
  614. i += run;
  615. if (i >= 64) {
  616. dprintf("error count: %d\n", i);
  617. return -1;
  618. }
  619. j = zigzag_direct[i];
  620. block[j] = level * quant_matrix[j];
  621. i++;
  622. if (i >= 64)
  623. break;
  624. }
  625. }
  626. return 0;
  627. }
  628. static int mjpeg_decode_sos(MJpegDecodeContext *s,
  629. UINT8 *buf, int buf_size)
  630. {
  631. int len, nb_components, i, j, n, h, v, ret;
  632. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  633. int comp_index[4];
  634. int dc_index[4];
  635. int ac_index[4];
  636. int nb_blocks[4];
  637. int h_count[4];
  638. int v_count[4];
  639. init_get_bits(&s->gb, buf, buf_size);
  640. /* XXX: verify len field validity */
  641. len = get_bits(&s->gb, 16);
  642. nb_components = get_bits(&s->gb, 8);
  643. /* XXX: only interleaved scan accepted */
  644. if (nb_components != 3)
  645. return -1;
  646. vmax = 0;
  647. hmax = 0;
  648. for(i=0;i<nb_components;i++) {
  649. id = get_bits(&s->gb, 8) - 1;
  650. /* find component index */
  651. for(index=0;index<s->nb_components;index++)
  652. if (id == s->component_id[index])
  653. break;
  654. if (index == s->nb_components)
  655. return -1;
  656. comp_index[i] = index;
  657. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  658. h_count[i] = s->h_count[index];
  659. v_count[i] = s->v_count[index];
  660. dc_index[i] = get_bits(&s->gb, 4);
  661. if (dc_index[i] >= 4)
  662. return -1;
  663. ac_index[i] = get_bits(&s->gb, 4);
  664. if (ac_index[i] >= 4)
  665. return -1;
  666. }
  667. get_bits(&s->gb, 8); /* Ss */
  668. get_bits(&s->gb, 8); /* Se */
  669. get_bits(&s->gb, 8); /* not used */
  670. for(i=0;i<nb_components;i++)
  671. s->last_dc[i] = 1024;
  672. if (nb_components > 1) {
  673. /* interleaved stream */
  674. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  675. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  676. } else {
  677. h = s->h_max / s->h_count[comp_index[0]];
  678. v = s->v_max / s->v_count[comp_index[0]];
  679. mb_width = (s->width + h * 8 - 1) / (h * 8);
  680. mb_height = (s->height + v * 8 - 1) / (v * 8);
  681. nb_blocks[0] = 1;
  682. h_count[0] = 1;
  683. v_count[0] = 1;
  684. }
  685. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  686. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  687. for(i=0;i<nb_components;i++) {
  688. UINT8 *ptr;
  689. int x, y, c;
  690. n = nb_blocks[i];
  691. c = comp_index[i];
  692. h = h_count[i];
  693. v = v_count[i];
  694. x = 0;
  695. y = 0;
  696. for(j=0;j<n;j++) {
  697. memset(s->block, 0, sizeof(s->block));
  698. if (decode_block(s, s->block, i,
  699. dc_index[i], ac_index[i],
  700. s->quant_index[c]) < 0) {
  701. dprintf("error %d %d\n", mb_y, mb_x);
  702. ret = -1;
  703. goto the_end;
  704. }
  705. ff_idct (s->block);
  706. ptr = s->current_picture[c] +
  707. (s->linesize[c] * (v * mb_y + y) * 8) +
  708. (h * mb_x + x) * 8;
  709. if (s->interlaced && s->bottom_field)
  710. ptr += s->linesize[c] >> 1;
  711. put_pixels_clamped(s->block, ptr, s->linesize[c]);
  712. if (++x == h) {
  713. x = 0;
  714. y++;
  715. }
  716. }
  717. }
  718. }
  719. }
  720. ret = 0;
  721. the_end:
  722. emms_c();
  723. return ret;
  724. }
  725. /* return the 8 bit start code value and update the search
  726. state. Return -1 if no start code found */
  727. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
  728. UINT32 *header_state)
  729. {
  730. UINT8 *buf_ptr;
  731. unsigned int state, v;
  732. int val;
  733. state = *header_state;
  734. buf_ptr = *pbuf_ptr;
  735. if (state) {
  736. /* get marker */
  737. found:
  738. if (buf_ptr < buf_end) {
  739. val = *buf_ptr++;
  740. state = 0;
  741. } else {
  742. val = -1;
  743. }
  744. } else {
  745. while (buf_ptr < buf_end) {
  746. v = *buf_ptr++;
  747. if (v == 0xff) {
  748. state = 1;
  749. goto found;
  750. }
  751. }
  752. val = -1;
  753. }
  754. *pbuf_ptr = buf_ptr;
  755. *header_state = state;
  756. return val;
  757. }
  758. static int mjpeg_decode_frame(AVCodecContext *avctx,
  759. void *data, int *data_size,
  760. UINT8 *buf, int buf_size)
  761. {
  762. MJpegDecodeContext *s = avctx->priv_data;
  763. UINT8 *buf_end, *buf_ptr, *buf_start;
  764. int len, code, start_code, input_size, i;
  765. AVPicture *picture = data;
  766. *data_size = 0;
  767. /* no supplementary picture */
  768. if (buf_size == 0)
  769. return 0;
  770. buf_ptr = buf;
  771. buf_end = buf + buf_size;
  772. while (buf_ptr < buf_end) {
  773. buf_start = buf_ptr;
  774. /* find start next marker */
  775. code = find_marker(&buf_ptr, buf_end, &s->header_state);
  776. /* copy to buffer */
  777. len = buf_ptr - buf_start;
  778. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  779. /* data too big : flush */
  780. s->buf_ptr = s->buffer;
  781. if (code > 0)
  782. s->start_code = code;
  783. } else {
  784. memcpy(s->buf_ptr, buf_start, len);
  785. s->buf_ptr += len;
  786. /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
  787. if (code == 0) {
  788. s->buf_ptr--;
  789. } else if (code > 0) {
  790. /* prepare data for next start code */
  791. input_size = s->buf_ptr - s->buffer;
  792. start_code = s->start_code;
  793. s->buf_ptr = s->buffer;
  794. s->start_code = code;
  795. dprintf("marker=%x\n", start_code);
  796. switch(start_code) {
  797. case SOI:
  798. /* nothing to do on SOI */
  799. break;
  800. case DQT:
  801. mjpeg_decode_dqt(s, s->buffer, input_size);
  802. break;
  803. case DHT:
  804. mjpeg_decode_dht(s, s->buffer, input_size);
  805. break;
  806. case SOF0:
  807. mjpeg_decode_sof0(s, s->buffer, input_size);
  808. break;
  809. case SOS:
  810. mjpeg_decode_sos(s, s->buffer, input_size);
  811. if (s->start_code == EOI) {
  812. int l;
  813. if (s->interlaced) {
  814. s->bottom_field ^= 1;
  815. /* if not bottom field, do not output image yet */
  816. if (s->bottom_field)
  817. goto the_end;
  818. }
  819. for(i=0;i<3;i++) {
  820. picture->data[i] = s->current_picture[i];
  821. l = s->linesize[i];
  822. if (s->interlaced)
  823. l >>= 1;
  824. picture->linesize[i] = l;
  825. }
  826. *data_size = sizeof(AVPicture);
  827. avctx->height = s->height;
  828. if (s->interlaced)
  829. avctx->height *= 2;
  830. avctx->width = s->width;
  831. /* XXX: not complete test ! */
  832. switch((s->h_count[0] << 4) | s->v_count[0]) {
  833. case 0x11:
  834. avctx->pix_fmt = PIX_FMT_YUV444P;
  835. break;
  836. case 0x21:
  837. avctx->pix_fmt = PIX_FMT_YUV422P;
  838. break;
  839. default:
  840. case 0x22:
  841. avctx->pix_fmt = PIX_FMT_YUV420P;
  842. break;
  843. }
  844. /* dummy quality */
  845. /* XXX: infer it with matrix */
  846. avctx->quality = 3;
  847. goto the_end;
  848. }
  849. break;
  850. }
  851. }
  852. }
  853. }
  854. the_end:
  855. return buf_ptr - buf;
  856. }
  857. static int mjpeg_decode_end(AVCodecContext *avctx)
  858. {
  859. MJpegDecodeContext *s = avctx->priv_data;
  860. int i, j;
  861. for(i=0;i<MAX_COMPONENTS;i++)
  862. free(s->current_picture[i]);
  863. for(i=0;i<2;i++) {
  864. for(j=0;j<4;j++)
  865. free_vlc(&s->vlcs[i][j]);
  866. }
  867. return 0;
  868. }
  869. AVCodec mjpeg_decoder = {
  870. "mjpeg",
  871. CODEC_TYPE_VIDEO,
  872. CODEC_ID_MJPEG,
  873. sizeof(MJpegDecodeContext),
  874. mjpeg_decode_init,
  875. NULL,
  876. mjpeg_decode_end,
  877. mjpeg_decode_frame,
  878. };