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.

979 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 = pbBufPtr(p);
  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. if (s->mjpeg_write_tables) 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, s->mjpeg_hsample[0]); /* H factor */
  243. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* 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, s->mjpeg_hsample[1]); /* H factor */
  248. put_bits(&s->pb, 4, s->mjpeg_vsample[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, s->mjpeg_hsample[2]); /* H factor */
  253. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* 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;
  543. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  544. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  545. w = w * 8 * s->h_count[i];
  546. h = h * 8 * s->v_count[i];
  547. if (s->interlaced)
  548. w *= 2;
  549. s->linesize[i] = w;
  550. /* memory test is done in mjpeg_decode_sos() */
  551. s->current_picture[i] = av_mallocz(w * h);
  552. }
  553. s->first_picture = 0;
  554. }
  555. return 0;
  556. }
  557. static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
  558. {
  559. VLC *dc_vlc;
  560. int code, diff;
  561. dc_vlc = &s->vlcs[0][dc_index];
  562. code = get_vlc(&s->gb, dc_vlc);
  563. if (code < 0)
  564. return 0xffff;
  565. if (code == 0) {
  566. diff = 0;
  567. } else {
  568. diff = get_bits(&s->gb, code);
  569. if ((diff & (1 << (code - 1))) == 0)
  570. diff = (-1 << code) | (diff + 1);
  571. }
  572. return diff;
  573. }
  574. /* decode block and dequantize */
  575. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  576. int component, int dc_index, int ac_index, int quant_index)
  577. {
  578. int nbits, code, i, j, level;
  579. int run, val;
  580. VLC *ac_vlc;
  581. INT16 *quant_matrix;
  582. quant_matrix = s->quant_matrixes[quant_index];
  583. /* DC coef */
  584. val = decode_dc(s, dc_index);
  585. if (val == 0xffff) {
  586. dprintf("error dc\n");
  587. return -1;
  588. }
  589. val = val * quant_matrix[0] + s->last_dc[component];
  590. s->last_dc[component] = val;
  591. block[0] = val;
  592. /* AC coefs */
  593. ac_vlc = &s->vlcs[1][ac_index];
  594. i = 1;
  595. for(;;) {
  596. code = get_vlc(&s->gb, ac_vlc);
  597. if (code < 0) {
  598. dprintf("error ac\n");
  599. return -1;
  600. }
  601. /* EOB */
  602. if (code == 0)
  603. break;
  604. if (code == 0xf0) {
  605. i += 16;
  606. } else {
  607. run = code >> 4;
  608. nbits = code & 0xf;
  609. level = get_bits(&s->gb, nbits);
  610. if ((level & (1 << (nbits - 1))) == 0)
  611. level = (-1 << nbits) | (level + 1);
  612. i += run;
  613. if (i >= 64) {
  614. dprintf("error count: %d\n", i);
  615. return -1;
  616. }
  617. j = zigzag_direct[i];
  618. block[j] = level * quant_matrix[j];
  619. i++;
  620. if (i >= 64)
  621. break;
  622. }
  623. }
  624. return 0;
  625. }
  626. static int mjpeg_decode_sos(MJpegDecodeContext *s,
  627. UINT8 *buf, int buf_size)
  628. {
  629. int len, nb_components, i, j, n, h, v, ret;
  630. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  631. int comp_index[4];
  632. int dc_index[4];
  633. int ac_index[4];
  634. int nb_blocks[4];
  635. int h_count[4];
  636. int v_count[4];
  637. init_get_bits(&s->gb, buf, buf_size);
  638. /* XXX: verify len field validity */
  639. len = get_bits(&s->gb, 16);
  640. nb_components = get_bits(&s->gb, 8);
  641. /* XXX: only interleaved scan accepted */
  642. if (nb_components != 3)
  643. return -1;
  644. vmax = 0;
  645. hmax = 0;
  646. for(i=0;i<nb_components;i++) {
  647. id = get_bits(&s->gb, 8) - 1;
  648. /* find component index */
  649. for(index=0;index<s->nb_components;index++)
  650. if (id == s->component_id[index])
  651. break;
  652. if (index == s->nb_components)
  653. return -1;
  654. comp_index[i] = index;
  655. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  656. h_count[i] = s->h_count[index];
  657. v_count[i] = s->v_count[index];
  658. dc_index[i] = get_bits(&s->gb, 4);
  659. if (dc_index[i] >= 4)
  660. return -1;
  661. ac_index[i] = get_bits(&s->gb, 4);
  662. if (ac_index[i] >= 4)
  663. return -1;
  664. }
  665. get_bits(&s->gb, 8); /* Ss */
  666. get_bits(&s->gb, 8); /* Se */
  667. get_bits(&s->gb, 8); /* not used */
  668. for(i=0;i<nb_components;i++)
  669. s->last_dc[i] = 1024;
  670. if (nb_components > 1) {
  671. /* interleaved stream */
  672. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  673. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  674. } else {
  675. h = s->h_max / s->h_count[comp_index[0]];
  676. v = s->v_max / s->v_count[comp_index[0]];
  677. mb_width = (s->width + h * 8 - 1) / (h * 8);
  678. mb_height = (s->height + v * 8 - 1) / (v * 8);
  679. nb_blocks[0] = 1;
  680. h_count[0] = 1;
  681. v_count[0] = 1;
  682. }
  683. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  684. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  685. for(i=0;i<nb_components;i++) {
  686. UINT8 *ptr;
  687. int x, y, c;
  688. n = nb_blocks[i];
  689. c = comp_index[i];
  690. h = h_count[i];
  691. v = v_count[i];
  692. x = 0;
  693. y = 0;
  694. for(j=0;j<n;j++) {
  695. memset(s->block, 0, sizeof(s->block));
  696. if (decode_block(s, s->block, i,
  697. dc_index[i], ac_index[i],
  698. s->quant_index[c]) < 0) {
  699. dprintf("error %d %d\n", mb_y, mb_x);
  700. ret = -1;
  701. goto the_end;
  702. }
  703. ff_idct (s->block);
  704. ptr = s->current_picture[c] +
  705. (s->linesize[c] * (v * mb_y + y) * 8) +
  706. (h * mb_x + x) * 8;
  707. if (s->interlaced && s->bottom_field)
  708. ptr += s->linesize[c] >> 1;
  709. put_pixels_clamped(s->block, ptr, s->linesize[c]);
  710. if (++x == h) {
  711. x = 0;
  712. y++;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. ret = 0;
  719. the_end:
  720. emms_c();
  721. return ret;
  722. }
  723. /* return the 8 bit start code value and update the search
  724. state. Return -1 if no start code found */
  725. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
  726. UINT32 *header_state)
  727. {
  728. UINT8 *buf_ptr;
  729. unsigned int state, v;
  730. int val;
  731. state = *header_state;
  732. buf_ptr = *pbuf_ptr;
  733. if (state) {
  734. /* get marker */
  735. found:
  736. if (buf_ptr < buf_end) {
  737. val = *buf_ptr++;
  738. state = 0;
  739. } else {
  740. val = -1;
  741. }
  742. } else {
  743. while (buf_ptr < buf_end) {
  744. v = *buf_ptr++;
  745. if (v == 0xff) {
  746. state = 1;
  747. goto found;
  748. }
  749. }
  750. val = -1;
  751. }
  752. *pbuf_ptr = buf_ptr;
  753. *header_state = state;
  754. return val;
  755. }
  756. static int mjpeg_decode_frame(AVCodecContext *avctx,
  757. void *data, int *data_size,
  758. UINT8 *buf, int buf_size)
  759. {
  760. MJpegDecodeContext *s = avctx->priv_data;
  761. UINT8 *buf_end, *buf_ptr, *buf_start;
  762. int len, code, start_code, input_size, i;
  763. AVPicture *picture = data;
  764. *data_size = 0;
  765. /* no supplementary picture */
  766. if (buf_size == 0)
  767. return 0;
  768. buf_ptr = buf;
  769. buf_end = buf + buf_size;
  770. while (buf_ptr < buf_end) {
  771. buf_start = buf_ptr;
  772. /* find start next marker */
  773. code = find_marker(&buf_ptr, buf_end, &s->header_state);
  774. /* copy to buffer */
  775. len = buf_ptr - buf_start;
  776. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  777. /* data too big : flush */
  778. s->buf_ptr = s->buffer;
  779. if (code > 0)
  780. s->start_code = code;
  781. } else {
  782. memcpy(s->buf_ptr, buf_start, len);
  783. s->buf_ptr += len;
  784. /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
  785. if (code == 0) {
  786. s->buf_ptr--;
  787. } else if (code > 0) {
  788. /* prepare data for next start code */
  789. input_size = s->buf_ptr - s->buffer;
  790. start_code = s->start_code;
  791. s->buf_ptr = s->buffer;
  792. s->start_code = code;
  793. dprintf("marker=%x\n", start_code);
  794. switch(start_code) {
  795. case SOI:
  796. /* nothing to do on SOI */
  797. break;
  798. case DQT:
  799. mjpeg_decode_dqt(s, s->buffer, input_size);
  800. break;
  801. case DHT:
  802. mjpeg_decode_dht(s, s->buffer, input_size);
  803. break;
  804. case SOF0:
  805. mjpeg_decode_sof0(s, s->buffer, input_size);
  806. break;
  807. case SOS:
  808. mjpeg_decode_sos(s, s->buffer, input_size);
  809. if (s->start_code == EOI) {
  810. int l;
  811. if (s->interlaced) {
  812. s->bottom_field ^= 1;
  813. /* if not bottom field, do not output image yet */
  814. if (s->bottom_field)
  815. goto the_end;
  816. }
  817. for(i=0;i<3;i++) {
  818. picture->data[i] = s->current_picture[i];
  819. l = s->linesize[i];
  820. if (s->interlaced)
  821. l >>= 1;
  822. picture->linesize[i] = l;
  823. }
  824. *data_size = sizeof(AVPicture);
  825. avctx->height = s->height;
  826. if (s->interlaced)
  827. avctx->height *= 2;
  828. avctx->width = s->width;
  829. /* XXX: not complete test ! */
  830. switch((s->h_count[0] << 4) | s->v_count[0]) {
  831. case 0x11:
  832. avctx->pix_fmt = PIX_FMT_YUV444P;
  833. break;
  834. case 0x21:
  835. avctx->pix_fmt = PIX_FMT_YUV422P;
  836. break;
  837. default:
  838. case 0x22:
  839. avctx->pix_fmt = PIX_FMT_YUV420P;
  840. break;
  841. }
  842. /* dummy quality */
  843. /* XXX: infer it with matrix */
  844. avctx->quality = 3;
  845. goto the_end;
  846. }
  847. break;
  848. }
  849. }
  850. }
  851. }
  852. the_end:
  853. return buf_ptr - buf;
  854. }
  855. static int mjpeg_decode_end(AVCodecContext *avctx)
  856. {
  857. MJpegDecodeContext *s = avctx->priv_data;
  858. int i, j;
  859. for(i=0;i<MAX_COMPONENTS;i++)
  860. free(s->current_picture[i]);
  861. for(i=0;i<2;i++) {
  862. for(j=0;j<4;j++)
  863. free_vlc(&s->vlcs[i][j]);
  864. }
  865. return 0;
  866. }
  867. AVCodec mjpeg_decoder = {
  868. "mjpeg",
  869. CODEC_TYPE_VIDEO,
  870. CODEC_ID_MJPEG,
  871. sizeof(MJpegDecodeContext),
  872. mjpeg_decode_init,
  873. NULL,
  874. mjpeg_decode_end,
  875. mjpeg_decode_frame,
  876. };