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.

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