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.

981 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. /* build all the huffman tables */
  150. build_huffman_codes(m->huff_size_dc_luminance,
  151. m->huff_code_dc_luminance,
  152. bits_dc_luminance,
  153. val_dc_luminance);
  154. build_huffman_codes(m->huff_size_dc_chrominance,
  155. m->huff_code_dc_chrominance,
  156. bits_dc_chrominance,
  157. val_dc_chrominance);
  158. build_huffman_codes(m->huff_size_ac_luminance,
  159. m->huff_code_ac_luminance,
  160. bits_ac_luminance,
  161. val_ac_luminance);
  162. build_huffman_codes(m->huff_size_ac_chrominance,
  163. m->huff_code_ac_chrominance,
  164. bits_ac_chrominance,
  165. val_ac_chrominance);
  166. s->mjpeg_ctx = m;
  167. return 0;
  168. }
  169. void mjpeg_close(MpegEncContext *s)
  170. {
  171. free(s->mjpeg_ctx);
  172. }
  173. static inline void put_marker(PutBitContext *p, int code)
  174. {
  175. put_bits(p, 8, 0xff);
  176. put_bits(p, 8, code);
  177. }
  178. /* table_class: 0 = DC coef, 1 = AC coefs */
  179. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  180. const UINT8 *bits_table, const UINT8 *value_table)
  181. {
  182. PutBitContext *p = &s->pb;
  183. int n, i;
  184. put_bits(p, 4, table_class);
  185. put_bits(p, 4, table_id);
  186. n = 0;
  187. for(i=1;i<=16;i++) {
  188. n += bits_table[i];
  189. put_bits(p, 8, bits_table[i]);
  190. }
  191. for(i=0;i<n;i++)
  192. put_bits(p, 8, value_table[i]);
  193. return n + 17;
  194. }
  195. static void jpeg_table_header(MpegEncContext *s)
  196. {
  197. PutBitContext *p = &s->pb;
  198. int i, j, size;
  199. UINT8 *ptr;
  200. /* quant matrixes */
  201. put_marker(p, DQT);
  202. put_bits(p, 16, 2 + 1 * (1 + 64));
  203. put_bits(p, 4, 0); /* 8 bit precision */
  204. put_bits(p, 4, 0); /* table 0 */
  205. for(i=0;i<64;i++) {
  206. j = zigzag_direct[i];
  207. put_bits(p, 8, s->intra_matrix[j]);
  208. }
  209. #if 0
  210. put_bits(p, 4, 0); /* 8 bit precision */
  211. put_bits(p, 4, 1); /* table 1 */
  212. for(i=0;i<64;i++) {
  213. j = zigzag_direct[i];
  214. put_bits(p, 8, s->chroma_intra_matrix[j]);
  215. }
  216. #endif
  217. /* huffman table */
  218. put_marker(p, DHT);
  219. flush_put_bits(p);
  220. ptr = p->buf_ptr;
  221. put_bits(p, 16, 0); /* patched later */
  222. size = 2;
  223. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  224. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  225. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  226. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  227. ptr[0] = size >> 8;
  228. ptr[1] = size;
  229. }
  230. void mjpeg_picture_header(MpegEncContext *s)
  231. {
  232. put_marker(&s->pb, SOI);
  233. jpeg_table_header(s);
  234. put_marker(&s->pb, SOF0);
  235. put_bits(&s->pb, 16, 17);
  236. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  237. put_bits(&s->pb, 16, s->height);
  238. put_bits(&s->pb, 16, s->width);
  239. put_bits(&s->pb, 8, 3); /* 3 components */
  240. /* Y component */
  241. put_bits(&s->pb, 8, 1); /* component number */
  242. put_bits(&s->pb, 4, 2); /* H factor */
  243. put_bits(&s->pb, 4, 2); /* V factor */
  244. put_bits(&s->pb, 8, 0); /* select matrix */
  245. /* Cb component */
  246. put_bits(&s->pb, 8, 2); /* component number */
  247. put_bits(&s->pb, 4, 1); /* H factor */
  248. put_bits(&s->pb, 4, 1); /* V factor */
  249. put_bits(&s->pb, 8, 0); /* select matrix */
  250. /* Cr component */
  251. put_bits(&s->pb, 8, 3); /* component number */
  252. put_bits(&s->pb, 4, 1); /* H factor */
  253. put_bits(&s->pb, 4, 1); /* V factor */
  254. put_bits(&s->pb, 8, 0); /* select matrix */
  255. /* scan header */
  256. put_marker(&s->pb, SOS);
  257. put_bits(&s->pb, 16, 12); /* length */
  258. put_bits(&s->pb, 8, 3); /* 3 components */
  259. /* Y component */
  260. put_bits(&s->pb, 8, 1); /* index */
  261. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  262. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  263. /* Cb component */
  264. put_bits(&s->pb, 8, 2); /* index */
  265. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  266. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  267. /* Cr component */
  268. put_bits(&s->pb, 8, 3); /* index */
  269. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  270. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  271. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  272. put_bits(&s->pb, 8, 63); /* Se (not used) */
  273. put_bits(&s->pb, 8, 0); /* (not used) */
  274. }
  275. void mjpeg_picture_trailer(MpegEncContext *s)
  276. {
  277. jflush_put_bits(&s->pb);
  278. put_marker(&s->pb, EOI);
  279. }
  280. static inline void encode_dc(MpegEncContext *s, int val,
  281. UINT8 *huff_size, UINT16 *huff_code)
  282. {
  283. int mant, nbits;
  284. if (val == 0) {
  285. jput_bits(&s->pb, huff_size[0], huff_code[0]);
  286. } else {
  287. mant = val;
  288. if (val < 0) {
  289. val = -val;
  290. mant--;
  291. }
  292. /* compute the log (XXX: optimize) */
  293. nbits = 0;
  294. while (val != 0) {
  295. val = val >> 1;
  296. nbits++;
  297. }
  298. jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  299. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  300. }
  301. }
  302. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  303. {
  304. int mant, nbits, code, i, j;
  305. int component, dc, run, last_index, val;
  306. MJpegContext *m = s->mjpeg_ctx;
  307. UINT8 *huff_size_ac;
  308. UINT16 *huff_code_ac;
  309. /* DC coef */
  310. component = (n <= 3 ? 0 : n - 4 + 1);
  311. dc = block[0]; /* overflow is impossible */
  312. val = dc - s->last_dc[component];
  313. if (n < 4) {
  314. encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  315. huff_size_ac = m->huff_size_ac_luminance;
  316. huff_code_ac = m->huff_code_ac_luminance;
  317. } else {
  318. encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  319. huff_size_ac = m->huff_size_ac_chrominance;
  320. huff_code_ac = m->huff_code_ac_chrominance;
  321. }
  322. s->last_dc[component] = dc;
  323. /* AC coefs */
  324. run = 0;
  325. last_index = s->block_last_index[n];
  326. for(i=1;i<=last_index;i++) {
  327. j = zigzag_direct[i];
  328. val = block[j];
  329. if (val == 0) {
  330. run++;
  331. } else {
  332. while (run >= 16) {
  333. jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  334. run -= 16;
  335. }
  336. mant = val;
  337. if (val < 0) {
  338. val = -val;
  339. mant--;
  340. }
  341. /* compute the log (XXX: optimize) */
  342. nbits = 0;
  343. while (val != 0) {
  344. val = val >> 1;
  345. nbits++;
  346. }
  347. code = (run << 4) | nbits;
  348. jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  349. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  350. run = 0;
  351. }
  352. }
  353. /* output EOB only if not already 64 values */
  354. if (last_index < 63 || run != 0)
  355. jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  356. }
  357. void mjpeg_encode_mb(MpegEncContext *s,
  358. DCTELEM block[6][64])
  359. {
  360. int i;
  361. for(i=0;i<6;i++) {
  362. encode_block(s, block[i], i);
  363. }
  364. }
  365. /******************************************/
  366. /* decoding */
  367. /* compressed picture size */
  368. #define PICTURE_BUFFER_SIZE 100000
  369. #define MAX_COMPONENTS 4
  370. typedef struct MJpegDecodeContext {
  371. GetBitContext gb;
  372. UINT32 header_state;
  373. int start_code; /* current start code */
  374. UINT8 *buf_ptr;
  375. int buffer_size;
  376. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  377. INT16 quant_matrixes[4][64];
  378. VLC vlcs[2][4];
  379. int org_width, org_height; /* size given at codec init */
  380. int first_picture; /* true if decoding first picture */
  381. int interlaced; /* true if interlaced */
  382. int bottom_field; /* true if bottom field */
  383. int width, height;
  384. int nb_components;
  385. int component_id[MAX_COMPONENTS];
  386. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  387. int v_count[MAX_COMPONENTS];
  388. int h_max, v_max; /* maximum h and v counts */
  389. int quant_index[4]; /* quant table index for each component */
  390. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  391. UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
  392. int linesize[MAX_COMPONENTS];
  393. DCTELEM block[64] __align8;
  394. UINT8 buffer[PICTURE_BUFFER_SIZE];
  395. } MJpegDecodeContext;
  396. static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  397. int nb_codes)
  398. {
  399. UINT8 huff_size[256];
  400. UINT16 huff_code[256];
  401. memset(huff_size, 0, sizeof(huff_size));
  402. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  403. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  404. }
  405. static int mjpeg_decode_init(AVCodecContext *avctx)
  406. {
  407. MJpegDecodeContext *s = avctx->priv_data;
  408. s->header_state = 0;
  409. s->mpeg_enc_ctx_allocated = 0;
  410. s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
  411. account FF 00 case */
  412. s->start_code = -1;
  413. s->buf_ptr = s->buffer;
  414. s->first_picture = 1;
  415. s->org_width = avctx->width;
  416. s->org_height = avctx->height;
  417. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  418. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  419. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  420. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  421. return 0;
  422. }
  423. /* quantize tables */
  424. static int mjpeg_decode_dqt(MJpegDecodeContext *s,
  425. UINT8 *buf, int buf_size)
  426. {
  427. int len, index, i, j;
  428. init_get_bits(&s->gb, buf, buf_size);
  429. len = get_bits(&s->gb, 16);
  430. len -= 2;
  431. while (len >= 65) {
  432. /* only 8 bit precision handled */
  433. if (get_bits(&s->gb, 4) != 0)
  434. return -1;
  435. index = get_bits(&s->gb, 4);
  436. if (index >= 4)
  437. return -1;
  438. dprintf("index=%d\n", index);
  439. /* read quant table */
  440. for(i=0;i<64;i++) {
  441. j = zigzag_direct[i];
  442. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  443. }
  444. len -= 65;
  445. }
  446. return 0;
  447. }
  448. /* decode huffman tables and build VLC decoders */
  449. static int mjpeg_decode_dht(MJpegDecodeContext *s,
  450. UINT8 *buf, int buf_size)
  451. {
  452. int len, index, i, class, n, v, code_max;
  453. UINT8 bits_table[17];
  454. UINT8 val_table[256];
  455. init_get_bits(&s->gb, buf, buf_size);
  456. len = get_bits(&s->gb, 16);
  457. len -= 2;
  458. while (len > 0) {
  459. if (len < 17)
  460. return -1;
  461. class = get_bits(&s->gb, 4);
  462. if (class >= 2)
  463. return -1;
  464. index = get_bits(&s->gb, 4);
  465. if (index >= 4)
  466. return -1;
  467. n = 0;
  468. for(i=1;i<=16;i++) {
  469. bits_table[i] = get_bits(&s->gb, 8);
  470. n += bits_table[i];
  471. }
  472. len -= 17;
  473. if (len < n || n > 256)
  474. return -1;
  475. code_max = 0;
  476. for(i=0;i<n;i++) {
  477. v = get_bits(&s->gb, 8);
  478. if (v > code_max)
  479. code_max = v;
  480. val_table[i] = v;
  481. }
  482. len -= n;
  483. /* build VLC and flush previous vlc if present */
  484. free_vlc(&s->vlcs[class][index]);
  485. dprintf("class=%d index=%d nb_codes=%d\n",
  486. class, index, code_max + 1);
  487. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  488. }
  489. return 0;
  490. }
  491. static int mjpeg_decode_sof0(MJpegDecodeContext *s,
  492. UINT8 *buf, int buf_size)
  493. {
  494. int len, nb_components, i, width, height;
  495. init_get_bits(&s->gb, buf, buf_size);
  496. /* XXX: verify len field validity */
  497. len = get_bits(&s->gb, 16);
  498. /* only 8 bits/component accepted */
  499. if (get_bits(&s->gb, 8) != 8)
  500. return -1;
  501. height = get_bits(&s->gb, 16);
  502. width = get_bits(&s->gb, 16);
  503. nb_components = get_bits(&s->gb, 8);
  504. if (nb_components <= 0 ||
  505. nb_components > MAX_COMPONENTS)
  506. return -1;
  507. s->nb_components = nb_components;
  508. s->h_max = 1;
  509. s->v_max = 1;
  510. for(i=0;i<nb_components;i++) {
  511. /* component id */
  512. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  513. s->h_count[i] = get_bits(&s->gb, 4);
  514. s->v_count[i] = get_bits(&s->gb, 4);
  515. /* compute hmax and vmax (only used in interleaved case) */
  516. if (s->h_count[i] > s->h_max)
  517. s->h_max = s->h_count[i];
  518. if (s->v_count[i] > s->v_max)
  519. s->v_max = s->v_count[i];
  520. s->quant_index[i] = get_bits(&s->gb, 8);
  521. if (s->quant_index[i] >= 4)
  522. return -1;
  523. dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
  524. }
  525. /* if different size, realloc/alloc picture */
  526. /* XXX: also check h_count and v_count */
  527. if (width != s->width || height != s->height) {
  528. for(i=0;i<MAX_COMPONENTS;i++) {
  529. free(s->current_picture[i]);
  530. s->current_picture[i] = NULL;
  531. }
  532. s->width = width;
  533. s->height = height;
  534. /* test interlaced mode */
  535. if (s->first_picture &&
  536. s->org_height != 0 &&
  537. s->height < ((s->org_height * 3) / 4)) {
  538. s->interlaced = 1;
  539. s->bottom_field = 0;
  540. }
  541. for(i=0;i<nb_components;i++) {
  542. int w, h, hh, vv;
  543. hh = s->h_max / s->h_count[i];
  544. vv = s->v_max / s->v_count[i];
  545. w = (s->width + 8 * hh - 1) / (8 * hh);
  546. h = (s->height + 8 * vv - 1) / (8 * vv);
  547. w = w * 8;
  548. h = h * 8;
  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. };