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.

925 lines
27KB

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