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 void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
614 UINT8 huff_size[256];
615 UINT16 huff_code[256];
617 memset(huff_size, 0, sizeof(huff_size));
618 build_huffman_codes(huff_size, huff_code, bits_table, val_table);
620 init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
623 static int mjpeg_decode_init(AVCodecContext *avctx)
625 MJpegDecodeContext *s = avctx->priv_data;
630 s->mpeg_enc_ctx_allocated = 0;
631 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
632 account FF 00 case */
634 s->buf_ptr = s->buffer;
635 s->first_picture = 1;
636 s->org_width = avctx->width;
637 s->org_height = avctx->height;
639 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
640 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
641 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
642 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
644 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
646 printf("mjpeg: using external huffman table\n");
647 mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size);
648 /* should check for error - but dunno */
653 /* quantize tables */
654 static int mjpeg_decode_dqt(MJpegDecodeContext *s,
655 UINT8 *buf, int buf_size)
657 int len, index, i, j;
658 init_get_bits(&s->gb, buf, buf_size);
660 len = get_bits(&s->gb, 16) - 2;
663 /* only 8 bit precision handled */
664 if (get_bits(&s->gb, 4) != 0)
666 dprintf("dqt: 16bit precision\n");
669 index = get_bits(&s->gb, 4);
672 dprintf("index=%d\n", index);
673 /* read quant table */
675 j = zigzag_direct[i];
676 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
681 SKIP_REMAINING(&s->gb, len);
686 /* decode huffman tables and build VLC decoders */
687 static int mjpeg_decode_dht(MJpegDecodeContext *s,
688 UINT8 *buf, int buf_size)
690 int len, index, i, class, n, v, code_max;
691 UINT8 bits_table[17];
692 UINT8 val_table[256];
694 init_get_bits(&s->gb, buf, buf_size);
696 len = get_bits(&s->gb, 16);
702 class = get_bits(&s->gb, 4);
705 index = get_bits(&s->gb, 4);
710 bits_table[i] = get_bits(&s->gb, 8);
714 if (len < n || n > 256)
719 v = get_bits(&s->gb, 8);
726 /* build VLC and flush previous vlc if present */
727 free_vlc(&s->vlcs[class][index]);
728 dprintf("class=%d index=%d nb_codes=%d\n",
729 class, index, code_max + 1);
730 build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
735 static int mjpeg_decode_sof0(MJpegDecodeContext *s,
736 UINT8 *buf, int buf_size)
738 int len, nb_components, i, width, height;
740 init_get_bits(&s->gb, buf, buf_size);
742 /* XXX: verify len field validity */
743 len = get_bits(&s->gb, 16);
744 /* only 8 bits/component accepted */
745 if (get_bits(&s->gb, 8) != 8)
747 height = get_bits(&s->gb, 16);
748 width = get_bits(&s->gb, 16);
749 dprintf("sof0: picture: %dx%d\n", width, height);
751 nb_components = get_bits(&s->gb, 8);
752 if (nb_components <= 0 ||
753 nb_components > MAX_COMPONENTS)
755 s->nb_components = nb_components;
758 for(i=0;i<nb_components;i++) {
760 s->component_id[i] = get_bits(&s->gb, 8) - 1;
761 s->h_count[i] = get_bits(&s->gb, 4);
762 s->v_count[i] = get_bits(&s->gb, 4);
763 /* compute hmax and vmax (only used in interleaved case) */
764 if (s->h_count[i] > s->h_max)
765 s->h_max = s->h_count[i];
766 if (s->v_count[i] > s->v_max)
767 s->v_max = s->v_count[i];
768 s->quant_index[i] = get_bits(&s->gb, 8);
769 if (s->quant_index[i] >= 4)
771 dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
772 s->v_count[i], s->component_id[i], s->quant_index[i]);
775 /* if different size, realloc/alloc picture */
776 /* XXX: also check h_count and v_count */
777 if (width != s->width || height != s->height) {
778 for(i=0;i<MAX_COMPONENTS;i++) {
779 free(s->current_picture[i]);
780 s->current_picture[i] = NULL;
784 /* test interlaced mode */
785 if (s->first_picture &&
786 s->org_height != 0 &&
787 s->height < ((s->org_height * 3) / 4)) {
792 for(i=0;i<nb_components;i++) {
794 w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
795 h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
796 w = w * 8 * s->h_count[i];
797 h = h * 8 * s->v_count[i];
801 /* memory test is done in mjpeg_decode_sos() */
802 s->current_picture[i] = av_mallocz(w * h);
804 s->first_picture = 0;
807 if (len != (8+(3*nb_components)))
809 dprintf("decode_sof0: error, len(%d) mismatch\n", len);
815 static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
819 code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
822 dprintf("decode_dc: bad vlc: %d:%d (%x)\n", 0, dc_index,
823 &s->vlcs[0][dc_index]);
829 diff = get_bits(&s->gb, code);
830 if ((diff & (1 << (code - 1))) == 0)
831 diff = (-1 << code) | (diff + 1);
836 /* decode block and dequantize */
837 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
838 int component, int dc_index, int ac_index, int quant_index)
840 int nbits, code, i, j, level;
846 val = decode_dc(s, dc_index);
848 dprintf("error dc\n");
851 quant_matrix = s->quant_matrixes[quant_index];
852 val = val * quant_matrix[0] + s->last_dc[component];
853 s->last_dc[component] = val;
856 ac_vlc = &s->vlcs[1][ac_index];
859 code = get_vlc(&s->gb, ac_vlc);
861 dprintf("error ac\n");
872 level = get_bits(&s->gb, nbits);
873 if ((level & (1 << (nbits - 1))) == 0)
874 level = (-1 << nbits) | (level + 1);
877 dprintf("error count: %d\n", i);
880 j = zigzag_direct[i];
881 block[j] = level * quant_matrix[j];
890 static int mjpeg_decode_sos(MJpegDecodeContext *s,
891 UINT8 *buf, int buf_size)
893 int len, nb_components, i, j, n, h, v, ret;
894 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
902 init_get_bits(&s->gb, buf, buf_size);
903 /* XXX: verify len field validity */
904 len = get_bits(&s->gb, 16);
905 nb_components = get_bits(&s->gb, 8);
906 /* XXX: only interleaved scan accepted */
907 if (nb_components != 3)
909 dprintf("decode_sos: components(%d) mismatch\n", nb_components);
914 for(i=0;i<nb_components;i++) {
915 id = get_bits(&s->gb, 8) - 1;
916 dprintf("component: %d\n", id);
917 /* find component index */
918 for(index=0;index<s->nb_components;index++)
919 if (id == s->component_id[index])
921 if (index == s->nb_components)
923 dprintf("decode_sos: index(%d) out of components\n", index);
927 comp_index[i] = index;
928 nb_blocks[i] = s->h_count[index] * s->v_count[index];
929 h_count[i] = s->h_count[index];
930 v_count[i] = s->v_count[index];
932 dc_index[i] = get_bits(&s->gb, 4);
933 ac_index[i] = get_bits(&s->gb, 4);
935 if (dc_index[i] < 0 || ac_index[i] < 0 ||
936 dc_index[i] >= 4 || ac_index[i] >= 4)
938 switch(s->start_code)
941 if (dc_index[i] > 1 || ac_index[i] > 1)
946 if (dc_index[i] > 3 || ac_index[i] > 3)
950 if (dc_index[i] > 3 || ac_index[i] != 0)
955 skip_bits(&s->gb, 8); /* Ss */
956 skip_bits(&s->gb, 8); /* Se */
957 skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
959 for(i=0;i<nb_components;i++)
960 s->last_dc[i] = 1024;
962 if (nb_components > 1) {
963 /* interleaved stream */
964 mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
965 mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
967 h = s->h_max / s->h_count[comp_index[0]];
968 v = s->v_max / s->v_count[comp_index[0]];
969 mb_width = (s->width + h * 8 - 1) / (h * 8);
970 mb_height = (s->height + v * 8 - 1) / (v * 8);
976 for(mb_y = 0; mb_y < mb_height; mb_y++) {
977 for(mb_x = 0; mb_x < mb_width; mb_x++) {
978 for(i=0;i<nb_components;i++) {
987 if (s->restart_interval && !s->restart_count)
988 s->restart_count = s->restart_interval;
990 memset(s->block, 0, sizeof(s->block));
991 if (decode_block(s, s->block, i,
992 dc_index[i], ac_index[i],
993 s->quant_index[c]) < 0) {
994 dprintf("error %d %d\n", mb_y, mb_x);
998 // dprintf("mb: %d %d processed\n", mb_y, mb_x);
1000 ptr = s->current_picture[c] +
1001 (s->linesize[c] * (v * mb_y + y) * 8) +
1003 if (s->interlaced && s->bottom_field)
1004 ptr += s->linesize[c] >> 1;
1005 put_pixels_clamped(s->block, ptr, s->linesize[c]);
1019 dprintf("decode_sos: ac/dc index out of range\n");
1023 static int mjpeg_decode_dri(MJpegDecodeContext *s,
1024 UINT8 *buf, int buf_size)
1026 init_get_bits(&s->gb, buf, buf_size);
1028 if (get_bits(&s->gb, 16) != 4)
1030 s->restart_interval = get_bits(&s->gb, 16);
1031 printf("restart interval: %d\n", s->restart_interval);
1036 #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d)
1037 static int mjpeg_decode_app(MJpegDecodeContext *s,
1038 UINT8 *buf, int buf_size, int start_code)
1042 init_get_bits(&s->gb, buf, buf_size);
1044 /* XXX: verify len field validity */
1045 len = get_bits(&s->gb, 16);
1049 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1052 /* buggy AVID, it puts EOI only at every 10th frame */
1053 /* also this fourcc is used by non-avid files too, it means
1054 interleaving, but it's always present in AVID files */
1055 if (id == FOURCC('A','V','I','1'))
1062 4bytes field_size_less_padding
1065 if (s->first_picture)
1066 printf("mjpeg: workarounding buggy AVID\n");
1067 s->interleaved_rows = get_bits(&s->gb, 8);
1069 skip_bits(&s->gb, 8);
1070 skip_bits(&s->gb, 32);
1071 skip_bits(&s->gb, 32);
1074 if (s->interleaved_rows)
1075 printf("mjpeg: interleaved rows: %d\n", s->interleaved_rows);
1081 if (id == FOURCC('J','F','I','F'))
1083 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1084 printf("mjpeg: JFIF header found (version: %x.%x)\n",
1085 get_bits(&s->gb, 8), get_bits(&s->gb, 8));
1090 if ((start_code == APP1) && (len > (0x28 - 8)))
1092 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1094 if (id == FOURCC('m','j','p','g')) /* Apple MJPEG-A */
1097 skip_bits(&s->gb, 32); /* field size */
1098 skip_bits(&s->gb, 32); /* pad field size */
1099 skip_bits(&s->gb, 32); /* next off */
1100 skip_bits(&s->gb, 32); /* quant off */
1101 skip_bits(&s->gb, 32); /* huff off */
1102 skip_bits(&s->gb, 32); /* image off */
1103 skip_bits(&s->gb, 32); /* scan off */
1104 skip_bits(&s->gb, 32); /* data off */
1106 if (s->first_picture)
1107 printf("mjpeg: Apple MJPEG-A header found\n");
1112 /* should check for further values.. */
1113 SKIP_REMAINING(&s->gb, len);
1119 static int mjpeg_decode_com(MJpegDecodeContext *s,
1120 UINT8 *buf, int buf_size)
1125 init_get_bits(&s->gb, buf, buf_size);
1127 /* XXX: verify len field validity */
1128 len = get_bits(&s->gb, 16)-2;
1129 cbuf = malloc(len+1);
1131 for (i = 0; i < len; i++)
1132 cbuf[i] = get_bits(&s->gb, 8);
1133 if (cbuf[i-1] == '\n')
1138 printf("mjpeg comment: '%s'\n", cbuf);
1140 /* buggy avid, it puts EOI only at every 10th frame */
1141 if (!strcmp(cbuf, "AVID"))
1144 if (s->first_picture)
1145 printf("mjpeg: workarounding buggy AVID\n");
1153 /* return the 8 bit start code value and update the search
1154 state. Return -1 if no start code found */
1155 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
1156 UINT32 *header_state)
1159 unsigned int state, v;
1162 state = *header_state;
1163 buf_ptr = *pbuf_ptr;
1167 if (buf_ptr < buf_end) {
1174 while (buf_ptr < buf_end) {
1183 *pbuf_ptr = buf_ptr;
1184 *header_state = state;
1188 static int mjpeg_decode_frame(AVCodecContext *avctx,
1189 void *data, int *data_size,
1190 UINT8 *buf, int buf_size)
1192 MJpegDecodeContext *s = avctx->priv_data;
1193 UINT8 *buf_end, *buf_ptr, *buf_start;
1194 int len, code, input_size, i;
1195 AVPicture *picture = data;
1196 unsigned int start_code;
1200 /* no supplementary picture */
1205 buf_end = buf + buf_size;
1206 while (buf_ptr < buf_end) {
1207 buf_start = buf_ptr;
1208 /* find start next marker */
1209 code = find_marker(&buf_ptr, buf_end, &s->header_state);
1210 /* copy to buffer */
1211 len = buf_ptr - buf_start;
1212 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1213 /* data too big : flush */
1214 s->buf_ptr = s->buffer;
1216 s->start_code = code;
1218 memcpy(s->buf_ptr, buf_start, len);
1220 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
1221 /* valid marker code is between 00 and ff - alex */
1222 if (code <= 0 || code >= 0xff) {
1225 /* prepare data for next start code */
1226 input_size = s->buf_ptr - s->buffer;
1227 start_code = s->start_code;
1228 s->buf_ptr = s->buffer;
1229 s->start_code = code;
1230 dprintf("marker=%x\n", start_code);
1231 switch(start_code) {
1233 s->restart_interval = 0;
1234 /* nothing to do on SOI */
1237 mjpeg_decode_dqt(s, s->buffer, input_size);
1240 mjpeg_decode_dht(s, s->buffer, input_size);
1243 mjpeg_decode_sof0(s, s->buffer, input_size);
1246 mjpeg_decode_sos(s, s->buffer, input_size);
1247 if (s->start_code == EOI || s->buggy_avid || s->restart_interval) {
1249 if (s->interlaced) {
1250 s->bottom_field ^= 1;
1251 /* if not bottom field, do not output image yet */
1252 if (s->bottom_field)
1256 picture->data[i] = s->current_picture[i];
1260 picture->linesize[i] = l;
1262 *data_size = sizeof(AVPicture);
1263 avctx->height = s->height;
1266 avctx->width = s->width;
1267 /* XXX: not complete test ! */
1268 switch((s->h_count[0] << 4) | s->v_count[0]) {
1270 avctx->pix_fmt = PIX_FMT_YUV444P;
1273 avctx->pix_fmt = PIX_FMT_YUV422P;
1277 avctx->pix_fmt = PIX_FMT_YUV420P;
1281 /* XXX: infer it with matrix */
1287 mjpeg_decode_dri(s, s->buffer, input_size);
1302 printf("mjpeg: unsupported coding type (%x)\n", start_code);
1306 if (start_code >= 0xd0 && start_code <= 0xd7)
1308 dprintf("restart marker: %d\n", start_code&0x0f);
1310 else if (s->first_picture)
1313 if (start_code >= 0xe0 && start_code <= 0xef)
1314 mjpeg_decode_app(s, s->buffer, input_size, start_code);
1316 else if (start_code == COM)
1317 mjpeg_decode_com(s, s->buffer, input_size);
1324 return buf_ptr - buf;
1327 static int mjpeg_decode_end(AVCodecContext *avctx)
1329 MJpegDecodeContext *s = avctx->priv_data;
1332 for(i=0;i<MAX_COMPONENTS;i++)
1333 free(s->current_picture[i]);
1336 free_vlc(&s->vlcs[i][j]);
1341 AVCodec mjpeg_decoder = {
1345 sizeof(MJpegDecodeContext),