2 * MJPEG encoder and decoder
3 * Copyright (c) 2000, 2001 Gerard Lantau.
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.
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.
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.
19 * Support for external huffman table and various fixes (AVID workaround) by
20 * Alex Beregszaszi <alex@naxine.org>
25 #include "mpegvideo.h"
32 #include "fastmemcpy.h"
35 /* use two quantizer table (one for luminance and one for chrominance) */
39 typedef struct MJpegContext {
40 UINT8 huff_size_dc_luminance[12];
41 UINT16 huff_code_dc_luminance[12];
42 UINT8 huff_size_dc_chrominance[12];
43 UINT16 huff_code_dc_chrominance[12];
45 UINT8 huff_size_ac_luminance[256];
46 UINT16 huff_code_ac_luminance[256];
47 UINT8 huff_size_ac_chrominance[256];
48 UINT16 huff_code_ac_chrominance[256];
51 /* JPEG marker codes */
54 SOF0 = 0xc0, /* baseline */
55 SOF1 = 0xc1, /* extended sequential, huffman */
56 SOF2 = 0xc2, /* progressive, huffman */
57 SOF3 = 0xc3, /* lossless, huffman */
59 SOF5 = 0xc5, /* differential sequential, huffman */
60 SOF6 = 0xc6, /* differential progressive, huffman */
61 SOF7 = 0xc7, /* differential lossless, huffman */
62 JPG = 0xc8, /* reserved for JPEG extension */
63 SOF9 = 0xc9, /* extended sequential, arithmetic */
64 SOF10 = 0xca, /* progressive, arithmetic */
65 SOF11 = 0xcb, /* lossless, arithmetic */
67 SOF13 = 0xcd, /* differential sequential, arithmetic */
68 SOF14 = 0xce, /* differential progressive, arithmetic */
69 SOF15 = 0xcf, /* differential lossless, arithmetic */
71 DHT = 0xc4, /* define huffman tables */
73 DAC = 0xcc, /* define arithmetic-coding conditioning */
75 /* restart with modulo 8 count "m" */
85 SOI = 0xd8, /* start of image */
86 EOI = 0xd9, /* end of image */
87 SOS = 0xda, /* start of scan */
88 DQT = 0xdb, /* define quantization tables */
89 DNL = 0xdc, /* define number of lines */
90 DRI = 0xdd, /* define restart interval */
91 DHP = 0xde, /* define hierarchical progression */
92 EXP = 0xdf, /* expand reference components */
126 COM = 0xfe, /* comment */
128 TEM = 0x01, /* temporary private use for arithmetic coding */
130 /* 0x02 -> 0xbf reserved */
134 /* These are the sample quantization tables given in JPEG spec section K.1.
135 * The spec says that the values given produce "good" quality, and
136 * when divided by 2, "very good" quality.
138 static const unsigned char std_luminance_quant_tbl[64] = {
139 16, 11, 10, 16, 24, 40, 51, 61,
140 12, 12, 14, 19, 26, 58, 60, 55,
141 14, 13, 16, 24, 40, 57, 69, 56,
142 14, 17, 22, 29, 51, 87, 80, 62,
143 18, 22, 37, 56, 68, 109, 103, 77,
144 24, 35, 55, 64, 81, 104, 113, 92,
145 49, 64, 78, 87, 103, 121, 120, 101,
146 72, 92, 95, 98, 112, 100, 103, 99
148 static const unsigned char std_chrominance_quant_tbl[64] = {
149 17, 18, 24, 47, 99, 99, 99, 99,
150 18, 21, 26, 66, 99, 99, 99, 99,
151 24, 26, 56, 99, 99, 99, 99, 99,
152 47, 66, 99, 99, 99, 99, 99, 99,
153 99, 99, 99, 99, 99, 99, 99, 99,
154 99, 99, 99, 99, 99, 99, 99, 99,
155 99, 99, 99, 99, 99, 99, 99, 99,
156 99, 99, 99, 99, 99, 99, 99, 99
160 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
161 /* IMPORTANT: these are only valid for 8-bit data precision! */
162 static const UINT8 bits_dc_luminance[17] =
163 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
164 static const UINT8 val_dc_luminance[] =
165 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
167 static const UINT8 bits_dc_chrominance[17] =
168 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
169 static const UINT8 val_dc_chrominance[] =
170 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
172 static const UINT8 bits_ac_luminance[17] =
173 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
174 static const UINT8 val_ac_luminance[] =
175 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
176 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
177 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
178 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
179 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
180 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
181 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
182 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
183 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
184 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
185 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
186 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
187 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
188 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
189 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
190 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
191 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
192 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
193 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
194 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
198 static const UINT8 bits_ac_chrominance[17] =
199 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
201 static const UINT8 val_ac_chrominance[] =
202 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
203 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
204 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
205 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
206 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
207 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
208 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
209 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
210 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
211 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
212 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
213 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
214 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
215 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
216 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
217 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
218 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
219 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
220 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
221 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
225 /* isn't this function nicer than the one in the libjpeg ? */
226 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
227 const UINT8 *bits_table, const UINT8 *val_table)
229 int i, j, k,nb, code, sym;
236 sym = val_table[k++];
238 huff_code[sym] = code;
245 int mjpeg_init(MpegEncContext *s)
249 m = malloc(sizeof(MJpegContext));
255 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
257 /* build all the huffman tables */
258 build_huffman_codes(m->huff_size_dc_luminance,
259 m->huff_code_dc_luminance,
262 build_huffman_codes(m->huff_size_dc_chrominance,
263 m->huff_code_dc_chrominance,
266 build_huffman_codes(m->huff_size_ac_luminance,
267 m->huff_code_ac_luminance,
270 build_huffman_codes(m->huff_size_ac_chrominance,
271 m->huff_code_ac_chrominance,
279 void mjpeg_close(MpegEncContext *s)
284 static inline void put_marker(PutBitContext *p, int code)
286 put_bits(p, 8, 0xff);
287 put_bits(p, 8, code);
290 /* table_class: 0 = DC coef, 1 = AC coefs */
291 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
292 const UINT8 *bits_table, const UINT8 *value_table)
294 PutBitContext *p = &s->pb;
297 put_bits(p, 4, table_class);
298 put_bits(p, 4, table_id);
303 put_bits(p, 8, bits_table[i]);
307 put_bits(p, 8, value_table[i]);
312 static void jpeg_table_header(MpegEncContext *s)
314 PutBitContext *p = &s->pb;
321 put_bits(p, 16, 2 + 2 * (1 + 64));
323 put_bits(p, 16, 2 + 1 * (1 + 64));
325 put_bits(p, 4, 0); /* 8 bit precision */
326 put_bits(p, 4, 0); /* table 0 */
328 j = zigzag_direct[i];
329 put_bits(p, 8, s->intra_matrix[j]);
332 put_bits(p, 4, 0); /* 8 bit precision */
333 put_bits(p, 4, 1); /* table 1 */
335 j = zigzag_direct[i];
336 put_bits(p, 8, s->chroma_intra_matrix[j]);
344 put_bits(p, 16, 0); /* patched later */
346 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
347 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
349 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
350 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
355 static void jpeg_put_comments(MpegEncContext *s)
357 PutBitContext *p = &s->pb;
365 put_string(p, "JFIF"); /* this puts the trailing zero-byte too */
366 put_bits(p, 16, 0x101);
367 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
368 put_bits(p, 16, 1); /* aspect: 1:1 */
370 put_bits(p, 8, 0); /* thumbnail width */
371 put_bits(p, 8, 0); /* thumbnail height */
378 put_bits(p, 16, 0); /* patched later */
379 #define VERSION "FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR
380 put_string(p, VERSION);
381 size = strlen(VERSION)+3;
387 void mjpeg_picture_header(MpegEncContext *s)
389 put_marker(&s->pb, SOI);
391 if (!s->mjpeg_data_only_frames)
393 jpeg_put_comments(s);
395 if (s->mjpeg_write_tables) jpeg_table_header(s);
397 put_marker(&s->pb, SOF0);
399 put_bits(&s->pb, 16, 17);
400 put_bits(&s->pb, 8, 8); /* 8 bits/component */
401 put_bits(&s->pb, 16, s->height);
402 put_bits(&s->pb, 16, s->width);
403 put_bits(&s->pb, 8, 3); /* 3 components */
406 put_bits(&s->pb, 8, 1); /* component number */
407 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
408 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
409 put_bits(&s->pb, 8, 0); /* select matrix */
412 put_bits(&s->pb, 8, 2); /* component number */
413 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
414 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
416 put_bits(&s->pb, 8, 1); /* select matrix */
418 put_bits(&s->pb, 8, 0); /* select matrix */
422 put_bits(&s->pb, 8, 3); /* component number */
423 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
424 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
426 put_bits(&s->pb, 8, 1); /* select matrix */
428 put_bits(&s->pb, 8, 0); /* select matrix */
433 put_marker(&s->pb, SOS);
434 put_bits(&s->pb, 16, 12); /* length */
435 put_bits(&s->pb, 8, 3); /* 3 components */
438 put_bits(&s->pb, 8, 1); /* index */
439 put_bits(&s->pb, 4, 0); /* DC huffman table index */
440 put_bits(&s->pb, 4, 0); /* AC huffman table index */
443 put_bits(&s->pb, 8, 2); /* index */
444 put_bits(&s->pb, 4, 1); /* DC huffman table index */
445 put_bits(&s->pb, 4, 1); /* AC huffman table index */
448 put_bits(&s->pb, 8, 3); /* index */
449 put_bits(&s->pb, 4, 1); /* DC huffman table index */
450 put_bits(&s->pb, 4, 1); /* AC huffman table index */
452 put_bits(&s->pb, 8, 0); /* Ss (not used) */
453 put_bits(&s->pb, 8, 63); /* Se (not used) */
454 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
457 void mjpeg_picture_trailer(MpegEncContext *s)
459 jflush_put_bits(&s->pb);
460 put_marker(&s->pb, EOI);
463 static inline void encode_dc(MpegEncContext *s, int val,
464 UINT8 *huff_size, UINT16 *huff_code)
469 jput_bits(&s->pb, huff_size[0], huff_code[0]);
477 /* compute the log (XXX: optimize) */
484 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
486 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
490 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
492 int mant, nbits, code, i, j;
493 int component, dc, run, last_index, val;
494 MJpegContext *m = s->mjpeg_ctx;
496 UINT16 *huff_code_ac;
499 component = (n <= 3 ? 0 : n - 4 + 1);
500 dc = block[0]; /* overflow is impossible */
501 val = dc - s->last_dc[component];
503 encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
504 huff_size_ac = m->huff_size_ac_luminance;
505 huff_code_ac = m->huff_code_ac_luminance;
507 encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
508 huff_size_ac = m->huff_size_ac_chrominance;
509 huff_code_ac = m->huff_code_ac_chrominance;
511 s->last_dc[component] = dc;
516 last_index = s->block_last_index[n];
517 for(i=1;i<=last_index;i++) {
518 j = zigzag_direct[i];
524 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
533 /* compute the log (XXX: optimize) */
539 code = (run << 4) | nbits;
541 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
543 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
548 /* output EOB only if not already 64 values */
549 if (last_index < 63 || run != 0)
550 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
553 void mjpeg_encode_mb(MpegEncContext *s,
554 DCTELEM block[6][64])
558 encode_block(s, block[i], i);
562 /******************************************/
565 /* compressed picture size */
566 #define PICTURE_BUFFER_SIZE 100000
568 #define MAX_COMPONENTS 4
570 typedef struct MJpegDecodeContext {
571 AVCodecContext *avctx;
574 int start_code; /* current start code */
577 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
578 INT16 quant_matrixes[4][64];
581 int org_width, org_height; /* size given at codec init */
582 int first_picture; /* true if decoding first picture */
583 int interlaced; /* true if interlaced */
584 int bottom_field; /* true if bottom field */
588 int component_id[MAX_COMPONENTS];
589 int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
590 int v_count[MAX_COMPONENTS];
591 int h_max, v_max; /* maximum h and v counts */
592 int quant_index[4]; /* quant table index for each component */
593 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
594 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
595 int linesize[MAX_COMPONENTS];
596 DCTELEM block[64] __align8;
597 UINT8 buffer[PICTURE_BUFFER_SIZE];
600 int restart_interval;
602 int interleaved_rows;
603 } MJpegDecodeContext;
605 #define SKIP_REMAINING(gb, len) { \
606 dprintf("reamining %d bytes in marker\n", len); \
607 if (len) while (--len) \
611 static int mjpeg_decode_dht(MJpegDecodeContext *s, UINT8 *buf, int buf_size);
613 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
616 UINT8 huff_size[256];
617 UINT16 huff_code[256];
619 memset(huff_size, 0, sizeof(huff_size));
620 build_huffman_codes(huff_size, huff_code, bits_table, val_table);
622 init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
625 static int mjpeg_decode_init(AVCodecContext *avctx)
627 MJpegDecodeContext *s = avctx->priv_data;
632 s->mpeg_enc_ctx_allocated = 0;
633 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
634 account FF 00 case */
636 s->buf_ptr = s->buffer;
637 s->first_picture = 1;
638 s->org_width = avctx->width;
639 s->org_height = avctx->height;
641 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
642 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
643 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
644 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
646 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
648 printf("mjpeg: using external huffman table\n");
649 mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size);
650 /* should check for error - but dunno */
655 /* quantize tables */
656 static int mjpeg_decode_dqt(MJpegDecodeContext *s,
657 UINT8 *buf, int buf_size)
659 int len, index, i, j;
660 init_get_bits(&s->gb, buf, buf_size);
662 len = get_bits(&s->gb, 16) - 2;
665 /* only 8 bit precision handled */
666 if (get_bits(&s->gb, 4) != 0)
668 dprintf("dqt: 16bit precision\n");
671 index = get_bits(&s->gb, 4);
674 dprintf("index=%d\n", index);
675 /* read quant table */
677 j = zigzag_direct[i];
678 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
683 SKIP_REMAINING(&s->gb, len);
688 /* decode huffman tables and build VLC decoders */
689 static int mjpeg_decode_dht(MJpegDecodeContext *s,
690 UINT8 *buf, int buf_size)
692 int len, index, i, class, n, v, code_max;
693 UINT8 bits_table[17];
694 UINT8 val_table[256];
696 init_get_bits(&s->gb, buf, buf_size);
698 len = get_bits(&s->gb, 16);
704 class = get_bits(&s->gb, 4);
707 index = get_bits(&s->gb, 4);
712 bits_table[i] = get_bits(&s->gb, 8);
716 if (len < n || n > 256)
721 v = get_bits(&s->gb, 8);
728 /* build VLC and flush previous vlc if present */
729 free_vlc(&s->vlcs[class][index]);
730 dprintf("class=%d index=%d nb_codes=%d\n",
731 class, index, code_max + 1);
732 build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
737 static int mjpeg_decode_sof0(MJpegDecodeContext *s,
738 UINT8 *buf, int buf_size)
740 int len, nb_components, i, width, height;
742 init_get_bits(&s->gb, buf, buf_size);
744 /* XXX: verify len field validity */
745 len = get_bits(&s->gb, 16);
746 /* only 8 bits/component accepted */
747 if (get_bits(&s->gb, 8) != 8)
749 height = get_bits(&s->gb, 16);
750 width = get_bits(&s->gb, 16);
751 dprintf("sof0: picture: %dx%d\n", width, height);
753 nb_components = get_bits(&s->gb, 8);
754 if (nb_components <= 0 ||
755 nb_components > MAX_COMPONENTS)
757 s->nb_components = nb_components;
760 for(i=0;i<nb_components;i++) {
762 s->component_id[i] = get_bits(&s->gb, 8) - 1;
763 s->h_count[i] = get_bits(&s->gb, 4);
764 s->v_count[i] = get_bits(&s->gb, 4);
765 /* compute hmax and vmax (only used in interleaved case) */
766 if (s->h_count[i] > s->h_max)
767 s->h_max = s->h_count[i];
768 if (s->v_count[i] > s->v_max)
769 s->v_max = s->v_count[i];
770 s->quant_index[i] = get_bits(&s->gb, 8);
771 if (s->quant_index[i] >= 4)
773 dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
774 s->v_count[i], s->component_id[i], s->quant_index[i]);
777 /* if different size, realloc/alloc picture */
778 /* XXX: also check h_count and v_count */
779 if (width != s->width || height != s->height) {
780 for(i=0;i<MAX_COMPONENTS;i++) {
781 free(s->current_picture[i]);
782 s->current_picture[i] = NULL;
786 /* test interlaced mode */
787 if (s->first_picture &&
788 s->org_height != 0 &&
789 s->height < ((s->org_height * 3) / 4)) {
794 for(i=0;i<nb_components;i++) {
796 w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
797 h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
798 w = w * 8 * s->h_count[i];
799 h = h * 8 * s->v_count[i];
803 /* memory test is done in mjpeg_decode_sos() */
804 s->current_picture[i] = av_mallocz(w * h);
806 s->first_picture = 0;
809 if (len != (8+(3*nb_components)))
811 dprintf("decode_sof0: error, len(%d) mismatch\n", len);
817 static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
821 code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
824 dprintf("decode_dc: bad vlc: %d:%d (%x)\n", 0, dc_index,
825 &s->vlcs[0][dc_index]);
831 diff = get_bits(&s->gb, code);
832 if ((diff & (1 << (code - 1))) == 0)
833 diff = (-1 << code) | (diff + 1);
838 /* decode block and dequantize */
839 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
840 int component, int dc_index, int ac_index, int quant_index)
842 int nbits, code, i, j, level;
848 val = decode_dc(s, dc_index);
850 dprintf("error dc\n");
853 quant_matrix = s->quant_matrixes[quant_index];
854 val = val * quant_matrix[0] + s->last_dc[component];
855 s->last_dc[component] = val;
858 ac_vlc = &s->vlcs[1][ac_index];
861 code = get_vlc(&s->gb, ac_vlc);
863 dprintf("error ac\n");
874 level = get_bits(&s->gb, nbits);
875 if ((level & (1 << (nbits - 1))) == 0)
876 level = (-1 << nbits) | (level + 1);
879 dprintf("error count: %d\n", i);
882 j = zigzag_direct[i];
883 block[j] = level * quant_matrix[j];
892 static int mjpeg_decode_sos(MJpegDecodeContext *s,
893 UINT8 *buf, int buf_size)
895 int len, nb_components, i, j, n, h, v, ret;
896 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
904 init_get_bits(&s->gb, buf, buf_size);
905 /* XXX: verify len field validity */
906 len = get_bits(&s->gb, 16);
907 nb_components = get_bits(&s->gb, 8);
908 /* XXX: only interleaved scan accepted */
909 if (nb_components != 3)
911 dprintf("decode_sos: components(%d) mismatch\n", nb_components);
916 for(i=0;i<nb_components;i++) {
917 id = get_bits(&s->gb, 8) - 1;
918 dprintf("component: %d\n", id);
919 /* find component index */
920 for(index=0;index<s->nb_components;index++)
921 if (id == s->component_id[index])
923 if (index == s->nb_components)
925 dprintf("decode_sos: index(%d) out of components\n", index);
929 comp_index[i] = index;
930 nb_blocks[i] = s->h_count[index] * s->v_count[index];
931 h_count[i] = s->h_count[index];
932 v_count[i] = s->v_count[index];
934 dc_index[i] = get_bits(&s->gb, 4);
935 ac_index[i] = get_bits(&s->gb, 4);
937 if (dc_index[i] < 0 || ac_index[i] < 0 ||
938 dc_index[i] >= 4 || ac_index[i] >= 4)
940 switch(s->start_code)
943 if (dc_index[i] > 1 || ac_index[i] > 1)
948 if (dc_index[i] > 3 || ac_index[i] > 3)
952 if (dc_index[i] > 3 || ac_index[i] != 0)
957 skip_bits(&s->gb, 8); /* Ss */
958 skip_bits(&s->gb, 8); /* Se */
959 skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
961 for(i=0;i<nb_components;i++)
962 s->last_dc[i] = 1024;
964 if (nb_components > 1) {
965 /* interleaved stream */
966 mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
967 mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
969 h = s->h_max / s->h_count[comp_index[0]];
970 v = s->v_max / s->v_count[comp_index[0]];
971 mb_width = (s->width + h * 8 - 1) / (h * 8);
972 mb_height = (s->height + v * 8 - 1) / (v * 8);
978 for(mb_y = 0; mb_y < mb_height; mb_y++) {
979 for(mb_x = 0; mb_x < mb_width; mb_x++) {
980 for(i=0;i<nb_components;i++) {
989 if (s->restart_interval && !s->restart_count)
990 s->restart_count = s->restart_interval;
992 memset(s->block, 0, sizeof(s->block));
993 if (decode_block(s, s->block, i,
994 dc_index[i], ac_index[i],
995 s->quant_index[c]) < 0) {
996 dprintf("error %d %d\n", mb_y, mb_x);
1000 // dprintf("mb: %d %d processed\n", mb_y, mb_x);
1002 ptr = s->current_picture[c] +
1003 (s->linesize[c] * (v * mb_y + y) * 8) +
1005 if (s->interlaced && s->bottom_field)
1006 ptr += s->linesize[c] >> 1;
1007 put_pixels_clamped(s->block, ptr, s->linesize[c]);
1021 dprintf("decode_sos: ac/dc index out of range\n");
1025 static int mjpeg_decode_dri(MJpegDecodeContext *s,
1026 UINT8 *buf, int buf_size)
1028 init_get_bits(&s->gb, buf, buf_size);
1030 if (get_bits(&s->gb, 16) != 4)
1032 s->restart_interval = get_bits(&s->gb, 16);
1033 printf("restart interval: %d\n", s->restart_interval);
1038 #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d)
1039 static int mjpeg_decode_app(MJpegDecodeContext *s,
1040 UINT8 *buf, int buf_size, int start_code)
1044 init_get_bits(&s->gb, buf, buf_size);
1046 /* XXX: verify len field validity */
1047 len = get_bits(&s->gb, 16);
1051 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1054 /* buggy AVID, it puts EOI only at every 10th frame */
1055 /* also this fourcc is used by non-avid files too, it means
1056 interleaving, but it's always present in AVID files */
1057 if (id == FOURCC('A','V','I','1'))
1064 4bytes field_size_less_padding
1067 if (s->first_picture)
1068 printf("mjpeg: workarounding buggy AVID\n");
1069 s->interleaved_rows = get_bits(&s->gb, 8);
1071 skip_bits(&s->gb, 8);
1072 skip_bits(&s->gb, 32);
1073 skip_bits(&s->gb, 32);
1076 if (s->interleaved_rows)
1077 printf("mjpeg: interleaved rows: %d\n", s->interleaved_rows);
1083 if (id == FOURCC('J','F','I','F'))
1085 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1086 printf("mjpeg: JFIF header found (version: %x.%x)\n",
1087 get_bits(&s->gb, 8), get_bits(&s->gb, 8));
1092 if ((start_code == APP1) && (len > (0x28 - 8)))
1094 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1096 if (id == FOURCC('m','j','p','g')) /* Apple MJPEG-A */
1099 skip_bits(&s->gb, 32); /* field size */
1100 skip_bits(&s->gb, 32); /* pad field size */
1101 skip_bits(&s->gb, 32); /* next off */
1102 skip_bits(&s->gb, 32); /* quant off */
1103 skip_bits(&s->gb, 32); /* huff off */
1104 skip_bits(&s->gb, 32); /* image off */
1105 skip_bits(&s->gb, 32); /* scan off */
1106 skip_bits(&s->gb, 32); /* data off */
1108 if (s->first_picture)
1109 printf("mjpeg: Apple MJPEG-A header found\n");
1114 /* should check for further values.. */
1115 SKIP_REMAINING(&s->gb, len);
1121 static int mjpeg_decode_com(MJpegDecodeContext *s,
1122 UINT8 *buf, int buf_size)
1127 init_get_bits(&s->gb, buf, buf_size);
1129 /* XXX: verify len field validity */
1130 len = get_bits(&s->gb, 16)-2;
1131 cbuf = malloc(len+1);
1133 for (i = 0; i < len; i++)
1134 cbuf[i] = get_bits(&s->gb, 8);
1135 if (cbuf[i-1] == '\n')
1140 printf("mjpeg comment: '%s'\n", cbuf);
1142 /* buggy avid, it puts EOI only at every 10th frame */
1143 if (!strcmp(cbuf, "AVID"))
1146 if (s->first_picture)
1147 printf("mjpeg: workarounding buggy AVID\n");
1155 /* return the 8 bit start code value and update the search
1156 state. Return -1 if no start code found */
1157 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
1158 UINT32 *header_state)
1161 unsigned int state, v;
1164 state = *header_state;
1165 buf_ptr = *pbuf_ptr;
1169 if (buf_ptr < buf_end) {
1176 while (buf_ptr < buf_end) {
1185 *pbuf_ptr = buf_ptr;
1186 *header_state = state;
1190 static int mjpeg_decode_frame(AVCodecContext *avctx,
1191 void *data, int *data_size,
1192 UINT8 *buf, int buf_size)
1194 MJpegDecodeContext *s = avctx->priv_data;
1195 UINT8 *buf_end, *buf_ptr, *buf_start;
1196 int len, code, input_size, i;
1197 AVPicture *picture = data;
1198 unsigned int start_code;
1202 /* no supplementary picture */
1207 buf_end = buf + buf_size;
1208 while (buf_ptr < buf_end) {
1209 buf_start = buf_ptr;
1210 /* find start next marker */
1211 code = find_marker(&buf_ptr, buf_end, &s->header_state);
1212 /* copy to buffer */
1213 len = buf_ptr - buf_start;
1214 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1215 /* data too big : flush */
1216 s->buf_ptr = s->buffer;
1218 s->start_code = code;
1220 memcpy(s->buf_ptr, buf_start, len);
1222 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
1223 /* valid marker code is between 00 and ff - alex */
1224 if (code <= 0 || code >= 0xff) {
1227 /* prepare data for next start code */
1228 input_size = s->buf_ptr - s->buffer;
1229 start_code = s->start_code;
1230 s->buf_ptr = s->buffer;
1231 s->start_code = code;
1232 dprintf("marker=%x\n", start_code);
1233 switch(start_code) {
1235 s->restart_interval = 0;
1236 /* nothing to do on SOI */
1239 mjpeg_decode_dqt(s, s->buffer, input_size);
1242 mjpeg_decode_dht(s, s->buffer, input_size);
1245 mjpeg_decode_sof0(s, s->buffer, input_size);
1248 mjpeg_decode_sos(s, s->buffer, input_size);
1249 if (s->start_code == EOI || s->buggy_avid || s->restart_interval) {
1251 if (s->interlaced) {
1252 s->bottom_field ^= 1;
1253 /* if not bottom field, do not output image yet */
1254 if (s->bottom_field)
1258 picture->data[i] = s->current_picture[i];
1262 picture->linesize[i] = l;
1264 *data_size = sizeof(AVPicture);
1265 avctx->height = s->height;
1268 avctx->width = s->width;
1269 /* XXX: not complete test ! */
1270 switch((s->h_count[0] << 4) | s->v_count[0]) {
1272 avctx->pix_fmt = PIX_FMT_YUV444P;
1275 avctx->pix_fmt = PIX_FMT_YUV422P;
1279 avctx->pix_fmt = PIX_FMT_YUV420P;
1283 /* XXX: infer it with matrix */
1289 mjpeg_decode_dri(s, s->buffer, input_size);
1304 printf("mjpeg: unsupported coding type (%x)\n", start_code);
1308 if (start_code >= 0xd0 && start_code <= 0xd7)
1310 dprintf("restart marker: %d\n", start_code&0x0f);
1312 else if (s->first_picture)
1315 if (start_code >= 0xe0 && start_code <= 0xef)
1316 mjpeg_decode_app(s, s->buffer, input_size, start_code);
1318 else if (start_code == COM)
1319 mjpeg_decode_com(s, s->buffer, input_size);
1326 return buf_ptr - buf;
1329 static int mjpeg_decode_end(AVCodecContext *avctx)
1331 MJpegDecodeContext *s = avctx->priv_data;
1334 for(i=0;i<MAX_COMPONENTS;i++)
1335 free(s->current_picture[i]);
1338 free_vlc(&s->vlcs[i][j]);
1343 AVCodec mjpeg_decoder = {
1347 sizeof(MJpegDecodeContext),