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.

990 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 <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, j, 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. j = zigzag_direct[i];
  208. put_bits(p, 8, s->intra_matrix[j]);
  209. }
  210. #if 0
  211. put_bits(p, 4, 0); /* 8 bit precision */
  212. put_bits(p, 4, 1); /* table 1 */
  213. for(i=0;i<64;i++) {
  214. j = zigzag_direct[i];
  215. put_bits(p, 8, s->chroma_intra_matrix[j]);
  216. }
  217. #endif
  218. /* huffman table */
  219. put_marker(p, DHT);
  220. flush_put_bits(p);
  221. ptr = p->buf_ptr;
  222. put_bits(p, 16, 0); /* patched later */
  223. size = 2;
  224. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  225. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  226. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  227. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  228. ptr[0] = size >> 8;
  229. ptr[1] = size;
  230. }
  231. void mjpeg_picture_header(MpegEncContext *s)
  232. {
  233. put_marker(&s->pb, SOI);
  234. jpeg_table_header(s);
  235. put_marker(&s->pb, SOF0);
  236. put_bits(&s->pb, 16, 17);
  237. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  238. put_bits(&s->pb, 16, s->height);
  239. put_bits(&s->pb, 16, s->width);
  240. put_bits(&s->pb, 8, 3); /* 3 components */
  241. /* Y component */
  242. put_bits(&s->pb, 8, 1); /* component number */
  243. put_bits(&s->pb, 4, 2); /* H factor */
  244. put_bits(&s->pb, 4, 2); /* V factor */
  245. put_bits(&s->pb, 8, 0); /* select matrix */
  246. /* Cb component */
  247. put_bits(&s->pb, 8, 2); /* component number */
  248. put_bits(&s->pb, 4, 1); /* H factor */
  249. put_bits(&s->pb, 4, 1); /* V factor */
  250. put_bits(&s->pb, 8, 0); /* select matrix */
  251. /* Cr component */
  252. put_bits(&s->pb, 8, 3); /* component number */
  253. put_bits(&s->pb, 4, 1); /* H factor */
  254. put_bits(&s->pb, 4, 1); /* V factor */
  255. put_bits(&s->pb, 8, 0); /* select matrix */
  256. /* scan header */
  257. put_marker(&s->pb, SOS);
  258. put_bits(&s->pb, 16, 12); /* length */
  259. put_bits(&s->pb, 8, 3); /* 3 components */
  260. /* Y component */
  261. put_bits(&s->pb, 8, 1); /* index */
  262. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  263. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  264. /* Cb component */
  265. put_bits(&s->pb, 8, 2); /* index */
  266. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  267. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  268. /* Cr component */
  269. put_bits(&s->pb, 8, 3); /* index */
  270. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  271. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  272. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  273. put_bits(&s->pb, 8, 63); /* Se (not used) */
  274. put_bits(&s->pb, 8, 0); /* (not used) */
  275. }
  276. void mjpeg_picture_trailer(MpegEncContext *s)
  277. {
  278. jflush_put_bits(&s->pb);
  279. put_marker(&s->pb, EOI);
  280. }
  281. static inline void encode_dc(MpegEncContext *s, int val,
  282. UINT8 *huff_size, UINT16 *huff_code)
  283. {
  284. int mant, nbits;
  285. if (val == 0) {
  286. jput_bits(&s->pb, huff_size[0], huff_code[0]);
  287. } else {
  288. mant = val;
  289. if (val < 0) {
  290. val = -val;
  291. mant--;
  292. }
  293. /* compute the log (XXX: optimize) */
  294. nbits = 0;
  295. while (val != 0) {
  296. val = val >> 1;
  297. nbits++;
  298. }
  299. jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  300. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  301. }
  302. }
  303. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  304. {
  305. int mant, nbits, code, i, j;
  306. int component, dc, run, last_index, val;
  307. MJpegContext *m = s->mjpeg_ctx;
  308. UINT8 *huff_size_ac;
  309. UINT16 *huff_code_ac;
  310. /* DC coef */
  311. component = (n <= 3 ? 0 : n - 4 + 1);
  312. dc = block[0]; /* overflow is impossible */
  313. val = dc - s->last_dc[component];
  314. if (n < 4) {
  315. encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  316. huff_size_ac = m->huff_size_ac_luminance;
  317. huff_code_ac = m->huff_code_ac_luminance;
  318. } else {
  319. encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  320. huff_size_ac = m->huff_size_ac_chrominance;
  321. huff_code_ac = m->huff_code_ac_chrominance;
  322. }
  323. s->last_dc[component] = dc;
  324. /* AC coefs */
  325. run = 0;
  326. last_index = s->block_last_index[n];
  327. for(i=1;i<=last_index;i++) {
  328. j = zigzag_direct[i];
  329. val = block[j];
  330. if (val == 0) {
  331. run++;
  332. } else {
  333. while (run >= 16) {
  334. jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  335. run -= 16;
  336. }
  337. mant = val;
  338. if (val < 0) {
  339. val = -val;
  340. mant--;
  341. }
  342. /* compute the log (XXX: optimize) */
  343. nbits = 0;
  344. while (val != 0) {
  345. val = val >> 1;
  346. nbits++;
  347. }
  348. code = (run << 4) | nbits;
  349. jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  350. jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  351. run = 0;
  352. }
  353. }
  354. /* output EOB only if not already 64 values */
  355. if (last_index < 63 || run != 0)
  356. jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  357. }
  358. void mjpeg_encode_mb(MpegEncContext *s,
  359. DCTELEM block[6][64])
  360. {
  361. int i;
  362. for(i=0;i<6;i++) {
  363. encode_block(s, block[i], i);
  364. }
  365. }
  366. /******************************************/
  367. /* decoding */
  368. //#define DEBUG
  369. #ifdef DEBUG
  370. #define dprintf(fmt,args...) printf(fmt, ## args)
  371. #else
  372. #define dprintf(fmt,args...)
  373. #endif
  374. /* compressed picture size */
  375. #define PICTURE_BUFFER_SIZE 100000
  376. #define MAX_COMPONENTS 4
  377. typedef struct MJpegDecodeContext {
  378. GetBitContext gb;
  379. UINT32 header_state;
  380. int start_code; /* current start code */
  381. UINT8 *buf_ptr;
  382. int buffer_size;
  383. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  384. INT16 quant_matrixes[4][64];
  385. VLC vlcs[2][4];
  386. int org_width, org_height; /* size given at codec init */
  387. int first_picture; /* true if decoding first picture */
  388. int interlaced; /* true if interlaced */
  389. int bottom_field; /* true if bottom field */
  390. int width, height;
  391. int nb_components;
  392. int component_id[MAX_COMPONENTS];
  393. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  394. int v_count[MAX_COMPONENTS];
  395. int h_max, v_max; /* maximum h and v counts */
  396. int quant_index[4]; /* quant table index for each component */
  397. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  398. UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
  399. int linesize[MAX_COMPONENTS];
  400. DCTELEM block[64] __align8;
  401. UINT8 buffer[PICTURE_BUFFER_SIZE];
  402. } MJpegDecodeContext;
  403. static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  404. int nb_codes)
  405. {
  406. UINT8 huff_size[256];
  407. UINT16 huff_code[256];
  408. memset(huff_size, 0, sizeof(huff_size));
  409. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  410. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  411. }
  412. static int mjpeg_decode_init(AVCodecContext *avctx)
  413. {
  414. MJpegDecodeContext *s = avctx->priv_data;
  415. s->header_state = 0;
  416. s->mpeg_enc_ctx_allocated = 0;
  417. s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
  418. account FF 00 case */
  419. s->start_code = -1;
  420. s->buf_ptr = s->buffer;
  421. s->first_picture = 1;
  422. s->org_width = avctx->width;
  423. s->org_height = avctx->height;
  424. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  425. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  426. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  427. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  428. return 0;
  429. }
  430. /* quantize tables */
  431. static int mjpeg_decode_dqt(MJpegDecodeContext *s,
  432. UINT8 *buf, int buf_size)
  433. {
  434. int len, index, i, j;
  435. init_get_bits(&s->gb, buf, buf_size);
  436. len = get_bits(&s->gb, 16);
  437. len -= 2;
  438. while (len >= 65) {
  439. /* only 8 bit precision handled */
  440. if (get_bits(&s->gb, 4) != 0)
  441. return -1;
  442. index = get_bits(&s->gb, 4);
  443. if (index >= 4)
  444. return -1;
  445. dprintf("index=%d\n", index);
  446. /* read quant table */
  447. for(i=0;i<64;i++) {
  448. j = zigzag_direct[i];
  449. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  450. }
  451. len -= 65;
  452. }
  453. return 0;
  454. }
  455. /* decode huffman tables and build VLC decoders */
  456. static int mjpeg_decode_dht(MJpegDecodeContext *s,
  457. UINT8 *buf, int buf_size)
  458. {
  459. int len, index, i, class, n, v, code_max;
  460. UINT8 bits_table[17];
  461. UINT8 val_table[256];
  462. init_get_bits(&s->gb, buf, buf_size);
  463. len = get_bits(&s->gb, 16);
  464. len -= 2;
  465. while (len > 0) {
  466. if (len < 17)
  467. return -1;
  468. class = get_bits(&s->gb, 4);
  469. if (class >= 2)
  470. return -1;
  471. index = get_bits(&s->gb, 4);
  472. if (index >= 4)
  473. return -1;
  474. n = 0;
  475. for(i=1;i<=16;i++) {
  476. bits_table[i] = get_bits(&s->gb, 8);
  477. n += bits_table[i];
  478. }
  479. len -= 17;
  480. if (len < n || n > 256)
  481. return -1;
  482. code_max = 0;
  483. for(i=0;i<n;i++) {
  484. v = get_bits(&s->gb, 8);
  485. if (v > code_max)
  486. code_max = v;
  487. val_table[i] = v;
  488. }
  489. len -= n;
  490. /* build VLC and flush previous vlc if present */
  491. free_vlc(&s->vlcs[class][index]);
  492. dprintf("class=%d index=%d nb_codes=%d\n",
  493. class, index, code_max + 1);
  494. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  495. }
  496. return 0;
  497. }
  498. static int mjpeg_decode_sof0(MJpegDecodeContext *s,
  499. UINT8 *buf, int buf_size)
  500. {
  501. int len, nb_components, i, width, height;
  502. init_get_bits(&s->gb, buf, buf_size);
  503. /* XXX: verify len field validity */
  504. len = get_bits(&s->gb, 16);
  505. /* only 8 bits/component accepted */
  506. if (get_bits(&s->gb, 8) != 8)
  507. return -1;
  508. height = get_bits(&s->gb, 16);
  509. width = get_bits(&s->gb, 16);
  510. nb_components = get_bits(&s->gb, 8);
  511. if (nb_components <= 0 ||
  512. nb_components > MAX_COMPONENTS)
  513. return -1;
  514. s->nb_components = nb_components;
  515. s->h_max = 1;
  516. s->v_max = 1;
  517. for(i=0;i<nb_components;i++) {
  518. /* component id */
  519. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  520. s->h_count[i] = get_bits(&s->gb, 4);
  521. s->v_count[i] = get_bits(&s->gb, 4);
  522. /* compute hmax and vmax (only used in interleaved case) */
  523. if (s->h_count[i] > s->h_max)
  524. s->h_max = s->h_count[i];
  525. if (s->v_count[i] > s->v_max)
  526. s->v_max = s->v_count[i];
  527. s->quant_index[i] = get_bits(&s->gb, 8);
  528. if (s->quant_index[i] >= 4)
  529. return -1;
  530. dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
  531. }
  532. /* if different size, realloc/alloc picture */
  533. /* XXX: also check h_count and v_count */
  534. if (width != s->width || height != s->height) {
  535. for(i=0;i<MAX_COMPONENTS;i++) {
  536. free(s->current_picture[i]);
  537. s->current_picture[i] = NULL;
  538. }
  539. s->width = width;
  540. s->height = height;
  541. /* test interlaced mode */
  542. if (s->first_picture &&
  543. s->org_height != 0 &&
  544. s->height < ((s->org_height * 3) / 4)) {
  545. s->interlaced = 1;
  546. s->bottom_field = 0;
  547. }
  548. for(i=0;i<nb_components;i++) {
  549. int w, h, hh, vv;
  550. hh = s->h_max / s->h_count[i];
  551. vv = s->v_max / s->v_count[i];
  552. w = (s->width + 8 * hh - 1) / (8 * hh);
  553. h = (s->height + 8 * vv - 1) / (8 * vv);
  554. w = w * 8;
  555. h = h * 8;
  556. if (s->interlaced)
  557. w *= 2;
  558. s->linesize[i] = w;
  559. /* memory test is done in mjpeg_decode_sos() */
  560. s->current_picture[i] = av_mallocz(w * h);
  561. }
  562. s->first_picture = 0;
  563. }
  564. return 0;
  565. }
  566. static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
  567. {
  568. VLC *dc_vlc;
  569. int code, diff;
  570. dc_vlc = &s->vlcs[0][dc_index];
  571. code = get_vlc(&s->gb, dc_vlc);
  572. if (code < 0)
  573. return 0xffff;
  574. if (code == 0) {
  575. diff = 0;
  576. } else {
  577. diff = get_bits(&s->gb, code);
  578. if ((diff & (1 << (code - 1))) == 0)
  579. diff = (-1 << code) | (diff + 1);
  580. }
  581. return diff;
  582. }
  583. /* decode block and dequantize */
  584. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  585. int component, int dc_index, int ac_index, int quant_index)
  586. {
  587. int nbits, code, i, j, level;
  588. int run, val;
  589. VLC *ac_vlc;
  590. INT16 *quant_matrix;
  591. quant_matrix = s->quant_matrixes[quant_index];
  592. /* DC coef */
  593. val = decode_dc(s, dc_index);
  594. if (val == 0xffff) {
  595. dprintf("error dc\n");
  596. return -1;
  597. }
  598. val = val * quant_matrix[0] + s->last_dc[component];
  599. s->last_dc[component] = val;
  600. block[0] = val;
  601. /* AC coefs */
  602. ac_vlc = &s->vlcs[1][ac_index];
  603. i = 1;
  604. for(;;) {
  605. code = get_vlc(&s->gb, ac_vlc);
  606. if (code < 0) {
  607. dprintf("error ac\n");
  608. return -1;
  609. }
  610. /* EOB */
  611. if (code == 0)
  612. break;
  613. if (code == 0xf0) {
  614. i += 16;
  615. } else {
  616. run = code >> 4;
  617. nbits = code & 0xf;
  618. level = get_bits(&s->gb, nbits);
  619. if ((level & (1 << (nbits - 1))) == 0)
  620. level = (-1 << nbits) | (level + 1);
  621. i += run;
  622. if (i >= 64) {
  623. dprintf("error count: %d\n", i);
  624. return -1;
  625. }
  626. j = zigzag_direct[i];
  627. block[j] = level * quant_matrix[j];
  628. i++;
  629. if (i >= 64)
  630. break;
  631. }
  632. }
  633. return 0;
  634. }
  635. static int mjpeg_decode_sos(MJpegDecodeContext *s,
  636. UINT8 *buf, int buf_size)
  637. {
  638. int len, nb_components, i, j, n, h, v, ret;
  639. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  640. int comp_index[4];
  641. int dc_index[4];
  642. int ac_index[4];
  643. int nb_blocks[4];
  644. int h_count[4];
  645. int v_count[4];
  646. init_get_bits(&s->gb, buf, buf_size);
  647. /* XXX: verify len field validity */
  648. len = get_bits(&s->gb, 16);
  649. nb_components = get_bits(&s->gb, 8);
  650. /* XXX: only interleaved scan accepted */
  651. if (nb_components != 3)
  652. return -1;
  653. vmax = 0;
  654. hmax = 0;
  655. for(i=0;i<nb_components;i++) {
  656. id = get_bits(&s->gb, 8) - 1;
  657. /* find component index */
  658. for(index=0;index<s->nb_components;index++)
  659. if (id == s->component_id[index])
  660. break;
  661. if (index == s->nb_components)
  662. return -1;
  663. comp_index[i] = index;
  664. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  665. h_count[i] = s->h_count[index];
  666. v_count[i] = s->v_count[index];
  667. dc_index[i] = get_bits(&s->gb, 4);
  668. if (dc_index[i] >= 4)
  669. return -1;
  670. ac_index[i] = get_bits(&s->gb, 4);
  671. if (ac_index[i] >= 4)
  672. return -1;
  673. }
  674. get_bits(&s->gb, 8); /* Ss */
  675. get_bits(&s->gb, 8); /* Se */
  676. get_bits(&s->gb, 8); /* not used */
  677. for(i=0;i<nb_components;i++)
  678. s->last_dc[i] = 1024;
  679. if (nb_components > 1) {
  680. /* interleaved stream */
  681. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  682. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  683. } else {
  684. h = s->h_max / s->h_count[comp_index[0]];
  685. v = s->v_max / s->v_count[comp_index[0]];
  686. mb_width = (s->width + h * 8 - 1) / (h * 8);
  687. mb_height = (s->height + v * 8 - 1) / (v * 8);
  688. nb_blocks[0] = 1;
  689. h_count[0] = 1;
  690. v_count[0] = 1;
  691. }
  692. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  693. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  694. for(i=0;i<nb_components;i++) {
  695. UINT8 *ptr;
  696. int x, y, c;
  697. n = nb_blocks[i];
  698. c = comp_index[i];
  699. h = h_count[i];
  700. v = v_count[i];
  701. x = 0;
  702. y = 0;
  703. for(j=0;j<n;j++) {
  704. memset(s->block, 0, sizeof(s->block));
  705. if (decode_block(s, s->block, i,
  706. dc_index[i], ac_index[i],
  707. s->quant_index[c]) < 0) {
  708. dprintf("error %d %d\n", mb_y, mb_x);
  709. ret = -1;
  710. goto the_end;
  711. }
  712. ff_idct (s->block);
  713. ptr = s->current_picture[c] +
  714. (s->linesize[c] * (v * mb_y + y) * 8) +
  715. (h * mb_x + x) * 8;
  716. if (s->interlaced && s->bottom_field)
  717. ptr += s->linesize[c] >> 1;
  718. put_pixels_clamped(s->block, ptr, s->linesize[c]);
  719. if (++x == h) {
  720. x = 0;
  721. y++;
  722. }
  723. }
  724. }
  725. }
  726. }
  727. ret = 0;
  728. the_end:
  729. emms_c();
  730. return ret;
  731. }
  732. /* return the 8 bit start code value and update the search
  733. state. Return -1 if no start code found */
  734. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
  735. UINT32 *header_state)
  736. {
  737. UINT8 *buf_ptr;
  738. unsigned int state, v;
  739. int val;
  740. state = *header_state;
  741. buf_ptr = *pbuf_ptr;
  742. if (state) {
  743. /* get marker */
  744. found:
  745. if (buf_ptr < buf_end) {
  746. val = *buf_ptr++;
  747. state = 0;
  748. } else {
  749. val = -1;
  750. }
  751. } else {
  752. while (buf_ptr < buf_end) {
  753. v = *buf_ptr++;
  754. if (v == 0xff) {
  755. state = 1;
  756. goto found;
  757. }
  758. }
  759. val = -1;
  760. }
  761. *pbuf_ptr = buf_ptr;
  762. *header_state = state;
  763. return val;
  764. }
  765. static int mjpeg_decode_frame(AVCodecContext *avctx,
  766. void *data, int *data_size,
  767. UINT8 *buf, int buf_size)
  768. {
  769. MJpegDecodeContext *s = avctx->priv_data;
  770. UINT8 *buf_end, *buf_ptr, *buf_start;
  771. int len, code, start_code, input_size, i;
  772. AVPicture *picture = data;
  773. /* no supplementary picture */
  774. if (buf_size == 0) {
  775. *data_size = 0;
  776. return 0;
  777. }
  778. buf_ptr = buf;
  779. buf_end = buf + buf_size;
  780. while (buf_ptr < buf_end) {
  781. buf_start = buf_ptr;
  782. /* find start next marker */
  783. code = find_marker(&buf_ptr, buf_end, &s->header_state);
  784. /* copy to buffer */
  785. len = buf_ptr - buf_start;
  786. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  787. /* data too big : flush */
  788. s->buf_ptr = s->buffer;
  789. if (code > 0)
  790. s->start_code = code;
  791. } else {
  792. memcpy(s->buf_ptr, buf_start, len);
  793. s->buf_ptr += len;
  794. /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
  795. if (code == 0) {
  796. s->buf_ptr--;
  797. } else if (code > 0) {
  798. /* prepare data for next start code */
  799. input_size = s->buf_ptr - s->buffer;
  800. start_code = s->start_code;
  801. s->buf_ptr = s->buffer;
  802. s->start_code = code;
  803. dprintf("marker=%x\n", start_code);
  804. switch(start_code) {
  805. case SOI:
  806. /* nothing to do on SOI */
  807. break;
  808. case DQT:
  809. mjpeg_decode_dqt(s, s->buffer, input_size);
  810. break;
  811. case DHT:
  812. mjpeg_decode_dht(s, s->buffer, input_size);
  813. break;
  814. case SOF0:
  815. mjpeg_decode_sof0(s, s->buffer, input_size);
  816. break;
  817. case SOS:
  818. mjpeg_decode_sos(s, s->buffer, input_size);
  819. if (s->start_code == EOI) {
  820. int l;
  821. if (s->interlaced) {
  822. s->bottom_field ^= 1;
  823. /* if not bottom field, do not output image yet */
  824. if (s->bottom_field)
  825. goto the_end;
  826. }
  827. for(i=0;i<3;i++) {
  828. picture->data[i] = s->current_picture[i];
  829. l = s->linesize[i];
  830. if (s->interlaced)
  831. l >>= 1;
  832. picture->linesize[i] = l;
  833. }
  834. *data_size = sizeof(AVPicture);
  835. avctx->height = s->height;
  836. if (s->interlaced)
  837. avctx->height *= 2;
  838. avctx->width = s->width;
  839. /* XXX: not complete test ! */
  840. switch((s->h_count[0] << 4) | s->v_count[0]) {
  841. case 0x11:
  842. avctx->pix_fmt = PIX_FMT_YUV444P;
  843. break;
  844. case 0x21:
  845. avctx->pix_fmt = PIX_FMT_YUV422P;
  846. break;
  847. default:
  848. case 0x22:
  849. avctx->pix_fmt = PIX_FMT_YUV420P;
  850. break;
  851. }
  852. /* dummy quality */
  853. /* XXX: infer it with matrix */
  854. avctx->quality = 3;
  855. goto the_end;
  856. }
  857. break;
  858. }
  859. }
  860. }
  861. }
  862. the_end:
  863. return buf_ptr - buf;
  864. }
  865. static int mjpeg_decode_end(AVCodecContext *avctx)
  866. {
  867. MJpegDecodeContext *s = avctx->priv_data;
  868. int i, j;
  869. for(i=0;i<MAX_COMPONENTS;i++)
  870. free(s->current_picture[i]);
  871. for(i=0;i<2;i++) {
  872. for(j=0;j<4;j++)
  873. free_vlc(&s->vlcs[i][j]);
  874. }
  875. return 0;
  876. }
  877. AVCodec mjpeg_decoder = {
  878. "mjpeg",
  879. CODEC_TYPE_VIDEO,
  880. CODEC_ID_MJPEG,
  881. sizeof(MJpegDecodeContext),
  882. mjpeg_decode_init,
  883. NULL,
  884. mjpeg_decode_end,
  885. mjpeg_decode_frame,
  886. };