2 * MJPEG encoder and decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Support for external huffman table, various fixes (AVID workaround),
20 * aspecting and various markers support
21 * by Alex Beregszaszi <alex@naxine.org>
26 #include "mpegvideo.h"
29 #include "fastmemcpy.h"
32 /* use two quantizer table (one for luminance and one for chrominance) */
36 typedef struct MJpegContext {
37 UINT8 huff_size_dc_luminance[12];
38 UINT16 huff_code_dc_luminance[12];
39 UINT8 huff_size_dc_chrominance[12];
40 UINT16 huff_code_dc_chrominance[12];
42 UINT8 huff_size_ac_luminance[256];
43 UINT16 huff_code_ac_luminance[256];
44 UINT8 huff_size_ac_chrominance[256];
45 UINT16 huff_code_ac_chrominance[256];
48 /* JPEG marker codes */
51 SOF0 = 0xc0, /* baseline */
52 SOF1 = 0xc1, /* extended sequential, huffman */
53 SOF2 = 0xc2, /* progressive, huffman */
54 SOF3 = 0xc3, /* lossless, huffman */
56 SOF5 = 0xc5, /* differential sequential, huffman */
57 SOF6 = 0xc6, /* differential progressive, huffman */
58 SOF7 = 0xc7, /* differential lossless, huffman */
59 JPG = 0xc8, /* reserved for JPEG extension */
60 SOF9 = 0xc9, /* extended sequential, arithmetic */
61 SOF10 = 0xca, /* progressive, arithmetic */
62 SOF11 = 0xcb, /* lossless, arithmetic */
64 SOF13 = 0xcd, /* differential sequential, arithmetic */
65 SOF14 = 0xce, /* differential progressive, arithmetic */
66 SOF15 = 0xcf, /* differential lossless, arithmetic */
68 DHT = 0xc4, /* define huffman tables */
70 DAC = 0xcc, /* define arithmetic-coding conditioning */
72 /* restart with modulo 8 count "m" */
82 SOI = 0xd8, /* start of image */
83 EOI = 0xd9, /* end of image */
84 SOS = 0xda, /* start of scan */
85 DQT = 0xdb, /* define quantization tables */
86 DNL = 0xdc, /* define number of lines */
87 DRI = 0xdd, /* define restart interval */
88 DHP = 0xde, /* define hierarchical progression */
89 EXP = 0xdf, /* expand reference components */
123 COM = 0xfe, /* comment */
125 TEM = 0x01, /* temporary private use for arithmetic coding */
127 /* 0x02 -> 0xbf reserved */
131 /* These are the sample quantization tables given in JPEG spec section K.1.
132 * The spec says that the values given produce "good" quality, and
133 * when divided by 2, "very good" quality.
135 static const unsigned char std_luminance_quant_tbl[64] = {
136 16, 11, 10, 16, 24, 40, 51, 61,
137 12, 12, 14, 19, 26, 58, 60, 55,
138 14, 13, 16, 24, 40, 57, 69, 56,
139 14, 17, 22, 29, 51, 87, 80, 62,
140 18, 22, 37, 56, 68, 109, 103, 77,
141 24, 35, 55, 64, 81, 104, 113, 92,
142 49, 64, 78, 87, 103, 121, 120, 101,
143 72, 92, 95, 98, 112, 100, 103, 99
145 static const unsigned char std_chrominance_quant_tbl[64] = {
146 17, 18, 24, 47, 99, 99, 99, 99,
147 18, 21, 26, 66, 99, 99, 99, 99,
148 24, 26, 56, 99, 99, 99, 99, 99,
149 47, 66, 99, 99, 99, 99, 99, 99,
150 99, 99, 99, 99, 99, 99, 99, 99,
151 99, 99, 99, 99, 99, 99, 99, 99,
152 99, 99, 99, 99, 99, 99, 99, 99,
153 99, 99, 99, 99, 99, 99, 99, 99
157 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
158 /* IMPORTANT: these are only valid for 8-bit data precision! */
159 static const UINT8 bits_dc_luminance[17] =
160 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
161 static const UINT8 val_dc_luminance[] =
162 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
164 static const UINT8 bits_dc_chrominance[17] =
165 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
166 static const UINT8 val_dc_chrominance[] =
167 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
169 static const UINT8 bits_ac_luminance[17] =
170 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
171 static const UINT8 val_ac_luminance[] =
172 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
173 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
174 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
175 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
176 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
177 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
178 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
179 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
180 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
181 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
182 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
183 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
184 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
185 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
186 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
187 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
188 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
189 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
190 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
191 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
195 static const UINT8 bits_ac_chrominance[17] =
196 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
198 static const UINT8 val_ac_chrominance[] =
199 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
200 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
201 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
202 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
203 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
204 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
205 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
206 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
207 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
208 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
209 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
210 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
211 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
212 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
213 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
214 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
215 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
216 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
217 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
218 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
222 /* isn't this function nicer than the one in the libjpeg ? */
223 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
224 const UINT8 *bits_table, const UINT8 *val_table)
226 int i, j, k,nb, code, sym;
233 sym = val_table[k++];
235 huff_code[sym] = code;
242 int mjpeg_init(MpegEncContext *s)
246 m = av_malloc(sizeof(MJpegContext));
252 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
254 /* build all the huffman tables */
255 build_huffman_codes(m->huff_size_dc_luminance,
256 m->huff_code_dc_luminance,
259 build_huffman_codes(m->huff_size_dc_chrominance,
260 m->huff_code_dc_chrominance,
263 build_huffman_codes(m->huff_size_ac_luminance,
264 m->huff_code_ac_luminance,
267 build_huffman_codes(m->huff_size_ac_chrominance,
268 m->huff_code_ac_chrominance,
276 void mjpeg_close(MpegEncContext *s)
278 av_free(s->mjpeg_ctx);
281 static inline void put_marker(PutBitContext *p, int code)
283 put_bits(p, 8, 0xff);
284 put_bits(p, 8, code);
287 /* table_class: 0 = DC coef, 1 = AC coefs */
288 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
289 const UINT8 *bits_table, const UINT8 *value_table)
291 PutBitContext *p = &s->pb;
294 put_bits(p, 4, table_class);
295 put_bits(p, 4, table_id);
300 put_bits(p, 8, bits_table[i]);
304 put_bits(p, 8, value_table[i]);
309 static void jpeg_table_header(MpegEncContext *s)
311 PutBitContext *p = &s->pb;
318 put_bits(p, 16, 2 + 2 * (1 + 64));
320 put_bits(p, 16, 2 + 1 * (1 + 64));
322 put_bits(p, 4, 0); /* 8 bit precision */
323 put_bits(p, 4, 0); /* table 0 */
325 j = s->intra_scantable.permutated[i];
326 put_bits(p, 8, s->intra_matrix[j]);
329 put_bits(p, 4, 0); /* 8 bit precision */
330 put_bits(p, 4, 1); /* table 1 */
332 j = s->intra_scantable.permutated[i];
333 put_bits(p, 8, s->chroma_intra_matrix[j]);
341 put_bits(p, 16, 0); /* patched later */
343 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
344 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
346 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
347 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
352 static void jpeg_put_comments(MpegEncContext *s)
354 PutBitContext *p = &s->pb;
358 if (s->aspect_ratio_info)
363 put_string(p, "JFIF"); /* this puts the trailing zero-byte too */
364 put_bits(p, 16, 0x0201); /* v 1.02 */
365 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
366 switch(s->aspect_ratio_info)
368 case FF_ASPECT_4_3_625:
369 case FF_ASPECT_4_3_525:
373 case FF_ASPECT_16_9_625:
374 case FF_ASPECT_16_9_525:
378 case FF_ASPECT_EXTENDED:
379 put_bits(p, 16, s->aspected_width);
380 put_bits(p, 16, s->aspected_height);
382 case FF_ASPECT_SQUARE:
384 put_bits(p, 16, 1); /* aspect: 1:1 */
388 put_bits(p, 8, 0); /* thumbnail width */
389 put_bits(p, 8, 0); /* thumbnail height */
397 put_bits(p, 16, 0); /* patched later */
398 #define MJPEG_VERSION "FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR
399 put_string(p, MJPEG_VERSION);
400 size = strlen(MJPEG_VERSION)+3;
407 void mjpeg_picture_header(MpegEncContext *s)
409 put_marker(&s->pb, SOI);
411 if (!s->mjpeg_data_only_frames)
413 jpeg_put_comments(s);
415 if (s->mjpeg_write_tables) jpeg_table_header(s);
417 put_marker(&s->pb, SOF0);
419 put_bits(&s->pb, 16, 17);
420 put_bits(&s->pb, 8, 8); /* 8 bits/component */
421 put_bits(&s->pb, 16, s->height);
422 put_bits(&s->pb, 16, s->width);
423 put_bits(&s->pb, 8, 3); /* 3 components */
426 put_bits(&s->pb, 8, 1); /* component number */
427 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
428 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
429 put_bits(&s->pb, 8, 0); /* select matrix */
432 put_bits(&s->pb, 8, 2); /* component number */
433 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
434 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
436 put_bits(&s->pb, 8, 1); /* select matrix */
438 put_bits(&s->pb, 8, 0); /* select matrix */
442 put_bits(&s->pb, 8, 3); /* component number */
443 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
444 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
446 put_bits(&s->pb, 8, 1); /* select matrix */
448 put_bits(&s->pb, 8, 0); /* select matrix */
453 put_marker(&s->pb, SOS);
454 put_bits(&s->pb, 16, 12); /* length */
455 put_bits(&s->pb, 8, 3); /* 3 components */
458 put_bits(&s->pb, 8, 1); /* index */
459 put_bits(&s->pb, 4, 0); /* DC huffman table index */
460 put_bits(&s->pb, 4, 0); /* AC huffman table index */
463 put_bits(&s->pb, 8, 2); /* index */
464 put_bits(&s->pb, 4, 1); /* DC huffman table index */
465 put_bits(&s->pb, 4, 1); /* AC huffman table index */
468 put_bits(&s->pb, 8, 3); /* index */
469 put_bits(&s->pb, 4, 1); /* DC huffman table index */
470 put_bits(&s->pb, 4, 1); /* AC huffman table index */
472 put_bits(&s->pb, 8, 0); /* Ss (not used) */
473 put_bits(&s->pb, 8, 63); /* Se (not used) */
474 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
477 void mjpeg_picture_trailer(MpegEncContext *s)
479 jflush_put_bits(&s->pb);
480 put_marker(&s->pb, EOI);
483 static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
484 UINT8 *huff_size, UINT16 *huff_code)
489 jput_bits(&s->pb, huff_size[0], huff_code[0]);
497 /* compute the log (XXX: optimize) */
504 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
506 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
510 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
512 int mant, nbits, code, i, j;
513 int component, dc, run, last_index, val;
514 MJpegContext *m = s->mjpeg_ctx;
516 UINT16 *huff_code_ac;
519 component = (n <= 3 ? 0 : n - 4 + 1);
520 dc = block[0]; /* overflow is impossible */
521 val = dc - s->last_dc[component];
523 mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
524 huff_size_ac = m->huff_size_ac_luminance;
525 huff_code_ac = m->huff_code_ac_luminance;
527 mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
528 huff_size_ac = m->huff_size_ac_chrominance;
529 huff_code_ac = m->huff_code_ac_chrominance;
531 s->last_dc[component] = dc;
536 last_index = s->block_last_index[n];
537 for(i=1;i<=last_index;i++) {
538 j = s->intra_scantable.permutated[i];
544 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
553 /* compute the log (XXX: optimize) */
559 code = (run << 4) | nbits;
561 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
563 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
568 /* output EOB only if not already 64 values */
569 if (last_index < 63 || run != 0)
570 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
573 void mjpeg_encode_mb(MpegEncContext *s,
574 DCTELEM block[6][64])
578 encode_block(s, block[i], i);
582 /******************************************/
585 /* compressed picture size */
586 #define PICTURE_BUFFER_SIZE 100000
588 #define MAX_COMPONENTS 4
590 typedef struct MJpegDecodeContext {
591 AVCodecContext *avctx;
594 int start_code; /* current start code */
597 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
598 INT16 quant_matrixes[4][64];
601 int org_width, org_height; /* size given at codec init */
602 int first_picture; /* true if decoding first picture */
603 int interlaced; /* true if interlaced */
604 int bottom_field; /* true if bottom field */
608 int component_id[MAX_COMPONENTS];
609 int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
610 int v_count[MAX_COMPONENTS];
611 int h_max, v_max; /* maximum h and v counts */
612 int quant_index[4]; /* quant table index for each component */
613 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
614 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
615 int linesize[MAX_COMPONENTS];
616 DCTELEM block[64] __align8;
617 UINT8 buffer[PICTURE_BUFFER_SIZE];
620 int restart_interval;
622 int interleaved_rows;
624 void (*idct_put)(UINT8 *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
625 } MJpegDecodeContext;
627 #define SKIP_REMAINING(gb, len) { \
628 dprintf("reamining %d bytes in marker\n", len); \
629 if (len) while (--len) \
633 static int mjpeg_decode_dht(MJpegDecodeContext *s, UINT8 *buf, int buf_size);
635 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
638 UINT8 huff_size[256];
639 UINT16 huff_code[256];
641 memset(huff_size, 0, sizeof(huff_size));
642 build_huffman_codes(huff_size, huff_code, bits_table, val_table);
644 init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
647 static int mjpeg_decode_init(AVCodecContext *avctx)
649 MJpegDecodeContext *s = avctx->priv_data;
654 /* ugly way to get the idct & scantable */
655 memset(&s2, 0, sizeof(MpegEncContext));
656 s2.flags= avctx->flags;
658 // s2->out_format = FMT_MJPEG;
661 if (MPV_common_init(&s2) < 0)
663 s->scantable= s2.intra_scantable;
664 s->idct_put= s2.idct_put;
668 s->mpeg_enc_ctx_allocated = 0;
669 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
670 account FF 00 case */
672 s->buf_ptr = s->buffer;
673 s->first_picture = 1;
674 s->org_width = avctx->width;
675 s->org_height = avctx->height;
677 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
678 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
679 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
680 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
682 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
684 printf("mjpeg: using external huffman table\n");
685 mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size);
686 /* should check for error - but dunno */
691 /* quantize tables */
692 static int mjpeg_decode_dqt(MJpegDecodeContext *s,
693 UINT8 *buf, int buf_size)
695 int len, index, i, j;
696 init_get_bits(&s->gb, buf, buf_size);
698 len = get_bits(&s->gb, 16) - 2;
701 /* only 8 bit precision handled */
702 if (get_bits(&s->gb, 4) != 0)
704 dprintf("dqt: 16bit precision\n");
707 index = get_bits(&s->gb, 4);
710 dprintf("index=%d\n", index);
711 /* read quant table */
713 j = s->scantable.permutated[i];
714 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
719 SKIP_REMAINING(&s->gb, len);
724 /* decode huffman tables and build VLC decoders */
725 static int mjpeg_decode_dht(MJpegDecodeContext *s,
726 UINT8 *buf, int buf_size)
728 int len, index, i, class, n, v, code_max;
729 UINT8 bits_table[17];
730 UINT8 val_table[256];
732 init_get_bits(&s->gb, buf, buf_size);
734 len = get_bits(&s->gb, 16);
740 class = get_bits(&s->gb, 4);
743 index = get_bits(&s->gb, 4);
748 bits_table[i] = get_bits(&s->gb, 8);
752 if (len < n || n > 256)
757 v = get_bits(&s->gb, 8);
764 /* build VLC and flush previous vlc if present */
765 free_vlc(&s->vlcs[class][index]);
766 dprintf("class=%d index=%d nb_codes=%d\n",
767 class, index, code_max + 1);
768 build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
773 static int mjpeg_decode_sof0(MJpegDecodeContext *s,
774 UINT8 *buf, int buf_size)
776 int len, nb_components, i, width, height;
778 init_get_bits(&s->gb, buf, buf_size);
780 /* XXX: verify len field validity */
781 len = get_bits(&s->gb, 16);
782 /* only 8 bits/component accepted */
783 if (get_bits(&s->gb, 8) != 8)
785 height = get_bits(&s->gb, 16);
786 width = get_bits(&s->gb, 16);
787 dprintf("sof0: picture: %dx%d\n", width, height);
789 nb_components = get_bits(&s->gb, 8);
790 if (nb_components <= 0 ||
791 nb_components > MAX_COMPONENTS)
793 s->nb_components = nb_components;
796 for(i=0;i<nb_components;i++) {
798 s->component_id[i] = get_bits(&s->gb, 8) - 1;
799 s->h_count[i] = get_bits(&s->gb, 4);
800 s->v_count[i] = get_bits(&s->gb, 4);
801 /* compute hmax and vmax (only used in interleaved case) */
802 if (s->h_count[i] > s->h_max)
803 s->h_max = s->h_count[i];
804 if (s->v_count[i] > s->v_max)
805 s->v_max = s->v_count[i];
806 s->quant_index[i] = get_bits(&s->gb, 8);
807 if (s->quant_index[i] >= 4)
809 dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
810 s->v_count[i], s->component_id[i], s->quant_index[i]);
813 /* if different size, realloc/alloc picture */
814 /* XXX: also check h_count and v_count */
815 if (width != s->width || height != s->height) {
816 for(i=0;i<MAX_COMPONENTS;i++)
817 av_freep(&s->current_picture[i]);
820 /* test interlaced mode */
821 if (s->first_picture &&
822 s->org_height != 0 &&
823 s->height < ((s->org_height * 3) / 4)) {
828 for(i=0;i<nb_components;i++) {
830 w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
831 h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
832 w = w * 8 * s->h_count[i];
833 h = h * 8 * s->v_count[i];
837 /* memory test is done in mjpeg_decode_sos() */
838 s->current_picture[i] = av_mallocz(w * h);
840 s->first_picture = 0;
843 if (len != (8+(3*nb_components)))
845 dprintf("decode_sof0: error, len(%d) mismatch\n", len);
851 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
855 code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
858 dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
859 &s->vlcs[0][dc_index]);
865 diff = get_bits(&s->gb, code);
866 if ((diff & (1 << (code - 1))) == 0)
867 diff = (-1 << code) | (diff + 1);
872 /* decode block and dequantize */
873 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
874 int component, int dc_index, int ac_index, int quant_index)
876 int nbits, code, i, j, level;
882 val = mjpeg_decode_dc(s, dc_index);
884 dprintf("error dc\n");
887 quant_matrix = s->quant_matrixes[quant_index];
888 val = val * quant_matrix[0] + s->last_dc[component];
889 s->last_dc[component] = val;
892 ac_vlc = &s->vlcs[1][ac_index];
895 code = get_vlc(&s->gb, ac_vlc);
897 dprintf("error ac\n");
908 level = get_bits(&s->gb, nbits);
909 if ((level & (1 << (nbits - 1))) == 0)
910 level = (-1 << nbits) | (level + 1);
913 dprintf("error count: %d\n", i);
916 j = s->scantable.permutated[i];
917 block[j] = level * quant_matrix[j];
926 static int mjpeg_decode_sos(MJpegDecodeContext *s,
927 UINT8 *buf, int buf_size)
929 int len, nb_components, i, j, n, h, v, ret;
930 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
938 init_get_bits(&s->gb, buf, buf_size);
939 /* XXX: verify len field validity */
940 len = get_bits(&s->gb, 16);
941 nb_components = get_bits(&s->gb, 8);
942 /* XXX: only interleaved scan accepted */
943 if (nb_components != 3)
945 dprintf("decode_sos: components(%d) mismatch\n", nb_components);
950 for(i=0;i<nb_components;i++) {
951 id = get_bits(&s->gb, 8) - 1;
952 dprintf("component: %d\n", id);
953 /* find component index */
954 for(index=0;index<s->nb_components;index++)
955 if (id == s->component_id[index])
957 if (index == s->nb_components)
959 dprintf("decode_sos: index(%d) out of components\n", index);
963 comp_index[i] = index;
964 nb_blocks[i] = s->h_count[index] * s->v_count[index];
965 h_count[i] = s->h_count[index];
966 v_count[i] = s->v_count[index];
968 dc_index[i] = get_bits(&s->gb, 4);
969 ac_index[i] = get_bits(&s->gb, 4);
971 if (dc_index[i] < 0 || ac_index[i] < 0 ||
972 dc_index[i] >= 4 || ac_index[i] >= 4)
974 switch(s->start_code)
977 if (dc_index[i] > 1 || ac_index[i] > 1)
982 if (dc_index[i] > 3 || ac_index[i] > 3)
986 if (dc_index[i] > 3 || ac_index[i] != 0)
991 skip_bits(&s->gb, 8); /* Ss */
992 skip_bits(&s->gb, 8); /* Se */
993 skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
995 for(i=0;i<nb_components;i++)
996 s->last_dc[i] = 1024;
998 if (nb_components > 1) {
999 /* interleaved stream */
1000 mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
1001 mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
1003 h = s->h_max / s->h_count[comp_index[0]];
1004 v = s->v_max / s->v_count[comp_index[0]];
1005 mb_width = (s->width + h * 8 - 1) / (h * 8);
1006 mb_height = (s->height + v * 8 - 1) / (v * 8);
1012 for(mb_y = 0; mb_y < mb_height; mb_y++) {
1013 for(mb_x = 0; mb_x < mb_width; mb_x++) {
1014 for(i=0;i<nb_components;i++) {
1023 if (s->restart_interval && !s->restart_count)
1024 s->restart_count = s->restart_interval;
1026 memset(s->block, 0, sizeof(s->block));
1027 if (decode_block(s, s->block, i,
1028 dc_index[i], ac_index[i],
1029 s->quant_index[c]) < 0) {
1030 dprintf("error y=%d x=%d\n", mb_y, mb_x);
1034 // dprintf("mb: %d %d processed\n", mb_y, mb_x);
1035 ptr = s->current_picture[c] +
1036 (s->linesize[c] * (v * mb_y + y) * 8) +
1038 if (s->interlaced && s->bottom_field)
1039 ptr += s->linesize[c] >> 1;
1040 s->idct_put(ptr, s->linesize[c], s->block);
1047 if (s->restart_interval && !--s->restart_count) {
1048 align_get_bits(&s->gb);
1049 skip_bits(&s->gb, 16); /* skip RSTn */
1050 for (j=0; j<nb_components; j++) /* reset dc */
1051 s->last_dc[j] = 1024;
1060 dprintf("decode_sos: ac/dc index out of range\n");
1064 static int mjpeg_decode_dri(MJpegDecodeContext *s,
1065 UINT8 *buf, int buf_size)
1067 init_get_bits(&s->gb, buf, buf_size);
1069 if (get_bits(&s->gb, 16) != 4)
1071 s->restart_interval = get_bits(&s->gb, 16);
1072 dprintf("restart interval: %d\n", s->restart_interval);
1077 #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d)
1078 static int mjpeg_decode_app(MJpegDecodeContext *s,
1079 UINT8 *buf, int buf_size, int start_code)
1083 init_get_bits(&s->gb, buf, buf_size);
1085 /* XXX: verify len field validity */
1086 len = get_bits(&s->gb, 16);
1090 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1093 /* buggy AVID, it puts EOI only at every 10th frame */
1094 /* also this fourcc is used by non-avid files too, it means
1095 interleaving, but it's always present in AVID files */
1096 if (id == FOURCC('A','V','I','1'))
1103 4bytes field_size_less_padding
1106 if (s->first_picture)
1107 printf("mjpeg: workarounding buggy AVID\n");
1108 s->interleaved_rows = get_bits(&s->gb, 8);
1110 skip_bits(&s->gb, 8);
1111 skip_bits(&s->gb, 32);
1112 skip_bits(&s->gb, 32);
1115 if (s->interleaved_rows)
1116 printf("mjpeg: interleaved rows: %d\n", s->interleaved_rows);
1122 if (id == FOURCC('J','F','I','F'))
1124 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1125 printf("mjpeg: JFIF header found (version: %x.%x)\n",
1126 get_bits(&s->gb, 8), get_bits(&s->gb, 8));
1127 if (get_bits(&s->gb, 8) == 0)
1129 s->avctx->aspect_ratio_info = FF_ASPECT_EXTENDED;
1130 s->avctx->aspected_width = get_bits(&s->gb, 16);
1131 s->avctx->aspected_height = get_bits(&s->gb, 16);
1135 skip_bits(&s->gb, 16);
1136 skip_bits(&s->gb, 16);
1138 skip_bits(&s->gb, 8);
1139 skip_bits(&s->gb, 8);
1144 if ((start_code == APP1) && (len > (0x28 - 8)))
1146 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1148 if (id == FOURCC('m','j','p','g')) /* Apple MJPEG-A */
1151 skip_bits(&s->gb, 32); /* field size */
1152 skip_bits(&s->gb, 32); /* pad field size */
1153 skip_bits(&s->gb, 32); /* next off */
1154 skip_bits(&s->gb, 32); /* quant off */
1155 skip_bits(&s->gb, 32); /* huff off */
1156 skip_bits(&s->gb, 32); /* image off */
1157 skip_bits(&s->gb, 32); /* scan off */
1158 skip_bits(&s->gb, 32); /* data off */
1160 if (s->first_picture)
1161 printf("mjpeg: Apple MJPEG-A header found\n");
1166 /* should check for further values.. */
1167 SKIP_REMAINING(&s->gb, len);
1173 static int mjpeg_decode_com(MJpegDecodeContext *s,
1174 UINT8 *buf, int buf_size)
1179 init_get_bits(&s->gb, buf, buf_size);
1181 /* XXX: verify len field validity */
1182 len = get_bits(&s->gb, 16)-2;
1183 cbuf = av_malloc(len+1);
1185 for (i = 0; i < len; i++)
1186 cbuf[i] = get_bits(&s->gb, 8);
1187 if (cbuf[i-1] == '\n')
1192 printf("mjpeg comment: '%s'\n", cbuf);
1194 /* buggy avid, it puts EOI only at every 10th frame */
1195 if (!strcmp(cbuf, "AVID"))
1198 if (s->first_picture)
1199 printf("mjpeg: workarounding buggy AVID\n");
1207 /* return the 8 bit start code value and update the search
1208 state. Return -1 if no start code found */
1209 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
1210 UINT32 *header_state)
1213 unsigned int state, v;
1216 state = *header_state;
1217 buf_ptr = *pbuf_ptr;
1222 if (buf_ptr < buf_end) {
1225 if ((val >= RST0) && (val <= RST7))
1231 while (buf_ptr < buf_end) {
1240 *pbuf_ptr = buf_ptr;
1241 *header_state = state;
1245 static int mjpeg_decode_frame(AVCodecContext *avctx,
1246 void *data, int *data_size,
1247 UINT8 *buf, int buf_size)
1249 MJpegDecodeContext *s = avctx->priv_data;
1250 UINT8 *buf_end, *buf_ptr, *buf_start;
1251 int len, code, input_size, i;
1252 AVPicture *picture = data;
1253 unsigned int start_code;
1257 /* no supplementary picture */
1262 buf_end = buf + buf_size;
1263 while (buf_ptr < buf_end) {
1264 buf_start = buf_ptr;
1265 /* find start next marker */
1266 code = find_marker(&buf_ptr, buf_end, &s->header_state);
1267 /* copy to buffer */
1268 len = buf_ptr - buf_start;
1269 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1270 /* data too big : flush */
1271 s->buf_ptr = s->buffer;
1273 s->start_code = code;
1275 memcpy(s->buf_ptr, buf_start, len);
1278 /* nothing to do: wait next marker */
1279 } else if (code == 0 || code == 0xff) {
1280 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
1281 /* valid marker code is between 00 and ff - alex */
1284 /* prepare data for next start code */
1285 input_size = s->buf_ptr - s->buffer;
1286 start_code = s->start_code;
1287 s->buf_ptr = s->buffer;
1288 s->start_code = code;
1289 dprintf("marker=%x\n", start_code);
1290 switch(start_code) {
1292 s->restart_interval = 0;
1293 /* nothing to do on SOI */
1296 mjpeg_decode_dqt(s, s->buffer, input_size);
1299 mjpeg_decode_dht(s, s->buffer, input_size);
1302 mjpeg_decode_sof0(s, s->buffer, input_size);
1305 mjpeg_decode_sos(s, s->buffer, input_size);
1306 if (s->start_code == EOI || s->buggy_avid || s->restart_interval) {
1308 if (s->interlaced) {
1309 s->bottom_field ^= 1;
1310 /* if not bottom field, do not output image yet */
1311 if (s->bottom_field)
1315 picture->data[i] = s->current_picture[i];
1319 picture->linesize[i] = l;
1321 *data_size = sizeof(AVPicture);
1322 avctx->height = s->height;
1325 avctx->width = s->width;
1326 /* XXX: not complete test ! */
1327 switch((s->h_count[0] << 4) | s->v_count[0]) {
1329 avctx->pix_fmt = PIX_FMT_YUV444P;
1332 avctx->pix_fmt = PIX_FMT_YUV422P;
1336 avctx->pix_fmt = PIX_FMT_YUV420P;
1340 /* XXX: infer it with matrix */
1346 mjpeg_decode_dri(s, s->buffer, input_size);
1361 printf("mjpeg: unsupported coding type (%x)\n", start_code);
1365 if (start_code >= 0xd0 && start_code <= 0xd7) {
1366 dprintf("restart marker: %d\n", start_code&0x0f);
1367 } else if (s->first_picture) {
1369 if (start_code >= 0xe0 && start_code <= 0xef)
1370 mjpeg_decode_app(s, s->buffer, input_size, start_code);
1372 else if (start_code == COM)
1373 mjpeg_decode_com(s, s->buffer, input_size);
1382 return buf_ptr - buf;
1385 static int mjpeg_decode_end(AVCodecContext *avctx)
1387 MJpegDecodeContext *s = avctx->priv_data;
1390 for(i=0;i<MAX_COMPONENTS;i++)
1391 av_free(s->current_picture[i]);
1394 free_vlc(&s->vlcs[i][j]);
1399 AVCodec mjpeg_decoder = {
1403 sizeof(MJpegDecodeContext),