]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpeg.c
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
[ffmpeg] / libavcodec / mjpeg.c
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  * Support for external huffman table and various fixes (AVID workaround) by
20  *                                    Alex Beregszaszi <alex@naxine.org>
21  */
22 //#define DEBUG
23 #include "avcodec.h"
24 #include "dsputil.h"
25 #include "mpegvideo.h"
26 #include "common.h"
27
28 #include <string.h>
29 #include <stdio.h>
30
31 #ifdef USE_FASTMEMCPY
32 #include "fastmemcpy.h"
33 #endif
34
35 /* use two quantizer table (one for luminance and one for chrominance) */
36 /* not yet working */
37 #undef TWOMATRIXES
38
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];
44
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];
49 } MJpegContext;
50
51 /* JPEG marker codes */
52 typedef enum {
53     /* start of frame */
54     SOF0  = 0xc0,       /* baseline */
55     SOF1  = 0xc1,       /* extended sequential, huffman */
56     SOF2  = 0xc2,       /* progressive, huffman */
57     SOF3  = 0xc3,       /* lossless, huffman */
58
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 */
66
67     SOF13 = 0xcd,       /* differential sequential, arithmetic */
68     SOF14 = 0xce,       /* differential progressive, arithmetic */
69     SOF15 = 0xcf,       /* differential lossless, arithmetic */
70
71     DHT   = 0xc4,       /* define huffman tables */
72
73     DAC   = 0xcc,       /* define arithmetic-coding conditioning */
74
75     /* restart with modulo 8 count "m" */
76     RST0  = 0xd0,
77     RST1  = 0xd1,
78     RST2  = 0xd2,
79     RST3  = 0xd3,
80     RST4  = 0xd4,
81     RST5  = 0xd5,
82     RST6  = 0xd6,
83     RST7  = 0xd7,
84
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 */
93
94     APP0  = 0xe0,
95     APP1  = 0xe1,
96     APP2  = 0xe2,
97     APP3  = 0xe3,
98     APP4  = 0xe4,
99     APP5  = 0xe5,
100     APP6  = 0xe6,
101     APP7  = 0xe7,
102     APP8  = 0xe8,
103     APP9  = 0xe9,
104     APP10 = 0xea,
105     APP11 = 0xeb,
106     APP12 = 0xec,
107     APP13 = 0xed,
108     APP14 = 0xee,
109     APP15 = 0xef,
110
111     JPG0  = 0xf0,
112     JPG1  = 0xf1,
113     JPG2  = 0xf2,
114     JPG3  = 0xf3,
115     JPG4  = 0xf4,
116     JPG5  = 0xf5,
117     JPG6  = 0xf6,
118     JPG7  = 0xf7,
119     JPG8  = 0xf8,
120     JPG9  = 0xf9,
121     JPG10 = 0xfa,
122     JPG11 = 0xfb,
123     JPG12 = 0xfc,
124     JPG13 = 0xfd,
125
126     COM   = 0xfe,       /* comment */
127
128     TEM   = 0x01,       /* temporary private use for arithmetic coding */
129
130     /* 0x02 -> 0xbf reserved */
131 } JPEG_MARKER;
132
133 #if 0
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.
137  */
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
147 };
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
157 };
158 #endif
159
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 };
166
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 };
171
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,
195   0xf9, 0xfa 
196 };
197
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 };
200
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,
222   0xf9, 0xfa 
223 };
224
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)
228 {
229     int i, j, k,nb, code, sym;
230
231     code = 0;
232     k = 0;
233     for(i=1;i<=16;i++) {
234         nb = bits_table[i];
235         for(j=0;j<nb;j++) {
236             sym = val_table[k++];
237             huff_size[sym] = i;
238             huff_code[sym] = code;
239             code++;
240         }
241         code <<= 1;
242     }
243 }
244
245 int mjpeg_init(MpegEncContext *s)
246 {
247     MJpegContext *m;
248     
249     m = malloc(sizeof(MJpegContext));
250     if (!m)
251         return -1;
252     
253     s->min_qcoeff=-1023;
254     s->max_qcoeff= 1023;
255     s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
256
257     /* build all the huffman tables */
258     build_huffman_codes(m->huff_size_dc_luminance,
259                         m->huff_code_dc_luminance,
260                         bits_dc_luminance,
261                         val_dc_luminance);
262     build_huffman_codes(m->huff_size_dc_chrominance,
263                         m->huff_code_dc_chrominance,
264                         bits_dc_chrominance,
265                         val_dc_chrominance);
266     build_huffman_codes(m->huff_size_ac_luminance,
267                         m->huff_code_ac_luminance,
268                         bits_ac_luminance,
269                         val_ac_luminance);
270     build_huffman_codes(m->huff_size_ac_chrominance,
271                         m->huff_code_ac_chrominance,
272                         bits_ac_chrominance,
273                         val_ac_chrominance);
274     
275     s->mjpeg_ctx = m;
276     return 0;
277 }
278
279 void mjpeg_close(MpegEncContext *s)
280 {
281     free(s->mjpeg_ctx);
282 }
283
284 static inline void put_marker(PutBitContext *p, int code)
285 {
286     put_bits(p, 8, 0xff);
287     put_bits(p, 8, code);
288 }
289
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)
293 {
294     PutBitContext *p = &s->pb;
295     int n, i;
296
297     put_bits(p, 4, table_class);
298     put_bits(p, 4, table_id);
299
300     n = 0;
301     for(i=1;i<=16;i++) {
302         n += bits_table[i];
303         put_bits(p, 8, bits_table[i]);
304     }
305
306     for(i=0;i<n;i++)
307         put_bits(p, 8, value_table[i]);
308
309     return n + 17;
310 }
311
312 static void jpeg_table_header(MpegEncContext *s)
313 {
314     PutBitContext *p = &s->pb;
315     int i, j, size;
316     UINT8 *ptr;
317
318     /* quant matrixes */
319     put_marker(p, DQT);
320 #ifdef TWOMATRIXES
321     put_bits(p, 16, 2 + 2 * (1 + 64));
322 #else
323     put_bits(p, 16, 2 + 1 * (1 + 64));
324 #endif
325     put_bits(p, 4, 0); /* 8 bit precision */
326     put_bits(p, 4, 0); /* table 0 */
327     for(i=0;i<64;i++) {
328         j = zigzag_direct[i];
329         put_bits(p, 8, s->intra_matrix[j]);
330     }
331 #ifdef TWOMATRIXES
332     put_bits(p, 4, 0); /* 8 bit precision */
333     put_bits(p, 4, 1); /* table 1 */
334     for(i=0;i<64;i++) {
335         j = zigzag_direct[i];
336         put_bits(p, 8, s->chroma_intra_matrix[j]);
337     }
338 #endif
339
340     /* huffman table */
341     put_marker(p, DHT);
342     flush_put_bits(p);
343     ptr = pbBufPtr(p);
344     put_bits(p, 16, 0); /* patched later */
345     size = 2;
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);
348     
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);
351     ptr[0] = size >> 8;
352     ptr[1] = size;
353 }
354
355 static void jpeg_put_comments(MpegEncContext *s)
356 {
357     PutBitContext *p = &s->pb;
358     int size;
359     UINT8 *ptr;
360
361 #if 0
362     /* JFIF header */
363     put_marker(p, APP0);
364     put_bits(p, 16, 16);
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 */
369     put_bits(p, 16, 1);
370     put_bits(p, 8, 0); /* thumbnail width */
371     put_bits(p, 8, 0); /* thumbnail height */
372 #endif
373
374     /* comment */
375     put_marker(p, COM);
376     flush_put_bits(p);
377     ptr = pbBufPtr(p);
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;
382 #undef VERSION
383     ptr[0] = size >> 8;
384     ptr[1] = size;
385 }
386
387 void mjpeg_picture_header(MpegEncContext *s)
388 {
389     put_marker(&s->pb, SOI);
390
391     if (!s->mjpeg_data_only_frames)
392     {
393     jpeg_put_comments(s);    
394
395     if (s->mjpeg_write_tables) jpeg_table_header(s);
396
397     put_marker(&s->pb, SOF0);
398
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 */
404     
405     /* Y component */
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 */
410     
411     /* Cb component */
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 */
415 #ifdef TWOMATRIXES
416     put_bits(&s->pb, 8, 1); /* select matrix */
417 #else
418     put_bits(&s->pb, 8, 0); /* select matrix */
419 #endif
420
421     /* Cr component */
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 */
425 #ifdef TWOMATRIXES
426     put_bits(&s->pb, 8, 1); /* select matrix */
427 #else
428     put_bits(&s->pb, 8, 0); /* select matrix */
429 #endif
430     }
431
432     /* scan header */
433     put_marker(&s->pb, SOS);
434     put_bits(&s->pb, 16, 12); /* length */
435     put_bits(&s->pb, 8, 3); /* 3 components */
436     
437     /* Y component */
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 */
441     
442     /* Cb component */
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 */
446     
447     /* Cr component */
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 */
451
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) */
455 }
456
457 void mjpeg_picture_trailer(MpegEncContext *s)
458 {
459     jflush_put_bits(&s->pb);
460     put_marker(&s->pb, EOI);
461 }
462
463 static inline void encode_dc(MpegEncContext *s, int val, 
464                              UINT8 *huff_size, UINT16 *huff_code)
465 {
466     int mant, nbits;
467
468     if (val == 0) {
469         jput_bits(&s->pb, huff_size[0], huff_code[0]);
470     } else {
471         mant = val;
472         if (val < 0) {
473             val = -val;
474             mant--;
475         }
476         
477         /* compute the log (XXX: optimize) */
478         nbits = 0;
479         while (val != 0) {
480             val = val >> 1;
481             nbits++;
482         }
483             
484         jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
485         
486         jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
487     }
488 }
489
490 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
491 {
492     int mant, nbits, code, i, j;
493     int component, dc, run, last_index, val;
494     MJpegContext *m = s->mjpeg_ctx;
495     UINT8 *huff_size_ac;
496     UINT16 *huff_code_ac;
497     
498     /* DC coef */
499     component = (n <= 3 ? 0 : n - 4 + 1);
500     dc = block[0]; /* overflow is impossible */
501     val = dc - s->last_dc[component];
502     if (n < 4) {
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;
506     } else {
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;
510     }
511     s->last_dc[component] = dc;
512     
513     /* AC coefs */
514     
515     run = 0;
516     last_index = s->block_last_index[n];
517     for(i=1;i<=last_index;i++) {
518         j = zigzag_direct[i];
519         val = block[j];
520         if (val == 0) {
521             run++;
522         } else {
523             while (run >= 16) {
524                 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
525                 run -= 16;
526             }
527             mant = val;
528             if (val < 0) {
529                 val = -val;
530                 mant--;
531             }
532             
533             /* compute the log (XXX: optimize) */
534             nbits = 0;
535             while (val != 0) {
536                 val = val >> 1;
537                 nbits++;
538             }
539             code = (run << 4) | nbits;
540
541             jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
542         
543             jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
544             run = 0;
545         }
546     }
547
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]);
551 }
552
553 void mjpeg_encode_mb(MpegEncContext *s, 
554                      DCTELEM block[6][64])
555 {
556     int i;
557     for(i=0;i<6;i++) {
558         encode_block(s, block[i], i);
559     }
560 }
561
562 /******************************************/
563 /* decoding */
564
565 /* compressed picture size */
566 #define PICTURE_BUFFER_SIZE 100000
567
568 #define MAX_COMPONENTS 4
569
570 typedef struct MJpegDecodeContext {
571     AVCodecContext *avctx;
572     GetBitContext gb;
573     UINT32 header_state;
574     int start_code; /* current start code */
575     UINT8 *buf_ptr;
576     int buffer_size;
577     int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
578     INT16 quant_matrixes[4][64];
579     VLC vlcs[2][4];
580
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 */
585
586     int width, height;
587     int nb_components;
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]; 
598
599     int buggy_avid;
600     int restart_interval;
601     int restart_count;
602     int interleaved_rows;
603 } MJpegDecodeContext;
604
605 #define SKIP_REMAINING(gb, len) { \
606     dprintf("reamining %d bytes in marker\n", len); \
607     if (len) while (--len) \
608         skip_bits(gb, 8); \
609 }
610
611 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table, 
612                       int nb_codes)
613 {
614     UINT8 huff_size[256];
615     UINT16 huff_code[256];
616
617     memset(huff_size, 0, sizeof(huff_size));
618     build_huffman_codes(huff_size, huff_code, bits_table, val_table);
619     
620     init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
621 }
622
623 static int mjpeg_decode_init(AVCodecContext *avctx)
624 {
625     MJpegDecodeContext *s = avctx->priv_data;
626
627     s->avctx = avctx;
628
629     s->header_state = 0;
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 */
633     s->start_code = -1;
634     s->buf_ptr = s->buffer;
635     s->first_picture = 1;
636     s->org_width = avctx->width;
637     s->org_height = avctx->height;
638
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);
643     
644     if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
645     {
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 */
649     }
650     return 0;
651 }
652
653 /* quantize tables */
654 static int mjpeg_decode_dqt(MJpegDecodeContext *s,
655                             UINT8 *buf, int buf_size)
656 {
657     int len, index, i, j;
658     init_get_bits(&s->gb, buf, buf_size);
659     
660     len = get_bits(&s->gb, 16) - 2;
661
662     while (len >= 65) {
663         /* only 8 bit precision handled */
664         if (get_bits(&s->gb, 4) != 0)
665         {
666             dprintf("dqt: 16bit precision\n");
667             return -1;
668         }
669         index = get_bits(&s->gb, 4);
670         if (index >= 4)
671             return -1;
672         dprintf("index=%d\n", index);
673         /* read quant table */
674         for(i=0;i<64;i++) {
675             j = zigzag_direct[i];
676             s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
677         }
678         len -= 65;
679     }
680     
681     SKIP_REMAINING(&s->gb, len);
682
683     return 0;
684 }
685
686 /* decode huffman tables and build VLC decoders */
687 static int mjpeg_decode_dht(MJpegDecodeContext *s,
688                             UINT8 *buf, int buf_size)
689 {
690     int len, index, i, class, n, v, code_max;
691     UINT8 bits_table[17];
692     UINT8 val_table[256];
693     
694     init_get_bits(&s->gb, buf, buf_size);
695
696     len = get_bits(&s->gb, 16);
697     len -= 2;
698
699     while (len > 0) {
700         if (len < 17)
701             return -1;
702         class = get_bits(&s->gb, 4);
703         if (class >= 2)
704             return -1;
705         index = get_bits(&s->gb, 4);
706         if (index >= 4)
707             return -1;
708         n = 0;
709         for(i=1;i<=16;i++) {
710             bits_table[i] = get_bits(&s->gb, 8);
711             n += bits_table[i];
712         }
713         len -= 17;
714         if (len < n || n > 256)
715             return -1;
716
717         code_max = 0;
718         for(i=0;i<n;i++) {
719             v = get_bits(&s->gb, 8);
720             if (v > code_max)
721                 code_max = v;
722             val_table[i] = v;
723         }
724         len -= n;
725
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);
731     }
732     return 0;
733 }
734
735 static int mjpeg_decode_sof0(MJpegDecodeContext *s,
736                              UINT8 *buf, int buf_size)
737 {
738     int len, nb_components, i, width, height;
739
740     init_get_bits(&s->gb, buf, buf_size);
741
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)
746         return -1;
747     height = get_bits(&s->gb, 16);
748     width = get_bits(&s->gb, 16);
749     dprintf("sof0: picture: %dx%d\n", width, height);
750
751     nb_components = get_bits(&s->gb, 8);
752     if (nb_components <= 0 ||
753         nb_components > MAX_COMPONENTS)
754         return -1;
755     s->nb_components = nb_components;
756     s->h_max = 1;
757     s->v_max = 1;
758     for(i=0;i<nb_components;i++) {
759         /* component id */
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)
770             return -1;
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]);
773     }
774
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;
781         }
782         s->width = width;
783         s->height = height;
784         /* test interlaced mode */
785         if (s->first_picture &&
786             s->org_height != 0 &&
787             s->height < ((s->org_height * 3) / 4)) {
788             s->interlaced = 1;
789             s->bottom_field = 0;
790         }
791
792         for(i=0;i<nb_components;i++) {
793             int w, h;
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];
798             if (s->interlaced)
799                 w *= 2;
800             s->linesize[i] = w;
801             /* memory test is done in mjpeg_decode_sos() */
802             s->current_picture[i] = av_mallocz(w * h);
803         }
804         s->first_picture = 0;
805     }
806
807     if (len != (8+(3*nb_components)))
808     {
809         dprintf("decode_sof0: error, len(%d) mismatch\n", len);
810     }
811     
812     return 0;
813 }
814
815 static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
816 {
817     int code, diff;
818
819     code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
820     if (code < 0)
821     {
822         dprintf("decode_dc: bad vlc: %d:%d (%x)\n", 0, dc_index,
823             &s->vlcs[0][dc_index]);
824         return 0xffff;
825     }
826     if (code == 0) {
827         diff = 0;
828     } else {
829         diff = get_bits(&s->gb, code);
830         if ((diff & (1 << (code - 1))) == 0) 
831             diff = (-1 << code) | (diff + 1);
832     }
833     return diff;
834 }
835
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)
839 {
840     int nbits, code, i, j, level;
841     int run, val;
842     VLC *ac_vlc;
843     INT16 *quant_matrix;
844
845     /* DC coef */
846     val = decode_dc(s, dc_index);
847     if (val == 0xffff) {
848         dprintf("error dc\n");
849         return -1;
850     }
851     quant_matrix = s->quant_matrixes[quant_index];
852     val = val * quant_matrix[0] + s->last_dc[component];
853     s->last_dc[component] = val;
854     block[0] = val;
855     /* AC coefs */
856     ac_vlc = &s->vlcs[1][ac_index];
857     i = 1;
858     for(;;) {
859         code = get_vlc(&s->gb, ac_vlc);
860         if (code < 0) {
861             dprintf("error ac\n");
862             return -1;
863         }
864         /* EOB */
865         if (code == 0)
866             break;
867         if (code == 0xf0) {
868             i += 16;
869         } else {
870             run = code >> 4;
871             nbits = code & 0xf;
872             level = get_bits(&s->gb, nbits);
873             if ((level & (1 << (nbits - 1))) == 0) 
874                 level = (-1 << nbits) | (level + 1);
875             i += run;
876             if (i >= 64) {
877                 dprintf("error count: %d\n", i);
878                 return -1;
879             }
880             j = zigzag_direct[i];
881             block[j] = level * quant_matrix[j];
882             i++;
883             if (i >= 64)
884                 break;
885         }
886     }
887     return 0;
888 }
889
890 static int mjpeg_decode_sos(MJpegDecodeContext *s,
891                             UINT8 *buf, int buf_size)
892 {
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;
895     int comp_index[4];
896     int dc_index[4];
897     int ac_index[4];
898     int nb_blocks[4];
899     int h_count[4];
900     int v_count[4];
901     
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)
908     {
909         dprintf("decode_sos: components(%d) mismatch\n", nb_components);
910         return -1;
911     }
912     vmax = 0;
913     hmax = 0;
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])
920                 break;
921         if (index == s->nb_components)
922         {
923             dprintf("decode_sos: index(%d) out of components\n", index);
924             return -1;
925         }
926
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];
931
932         dc_index[i] = get_bits(&s->gb, 4);
933         ac_index[i] = get_bits(&s->gb, 4);
934
935         if (dc_index[i] < 0 || ac_index[i] < 0 ||
936             dc_index[i] >= 4 || ac_index[i] >= 4)
937             goto out_of_range;
938         switch(s->start_code)
939         {
940             case SOF0:
941                 if (dc_index[i] > 1 || ac_index[i] > 1)
942                     goto out_of_range;
943                 break;
944             case SOF1:
945             case SOF2:
946                 if (dc_index[i] > 3 || ac_index[i] > 3)
947                     goto out_of_range;
948                 break;
949             case SOF3:
950                 if (dc_index[i] > 3 || ac_index[i] != 0)
951                     goto out_of_range;
952                 break;  
953         }
954     }
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) */
958
959     for(i=0;i<nb_components;i++) 
960         s->last_dc[i] = 1024;
961
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);
966     } else {
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);
971         nb_blocks[0] = 1;
972         h_count[0] = 1;
973         v_count[0] = 1;
974     }
975
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++) {
979                 UINT8 *ptr;
980                 int x, y, c;
981                 n = nb_blocks[i];
982                 c = comp_index[i];
983                 h = h_count[i];
984                 v = v_count[i];
985                 x = 0;
986                 y = 0;
987                 if (s->restart_interval && !s->restart_count)
988                     s->restart_count = s->restart_interval;
989                 for(j=0;j<n;j++) {
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);
995                         ret = -1;
996                         goto the_end;
997                     }
998 //                  dprintf("mb: %d %d processed\n", mb_y, mb_x);
999                     ff_idct (s->block);
1000                     ptr = s->current_picture[c] + 
1001                         (s->linesize[c] * (v * mb_y + y) * 8) + 
1002                         (h * mb_x + x) * 8;
1003                     if (s->interlaced && s->bottom_field)
1004                         ptr += s->linesize[c] >> 1;
1005                     put_pixels_clamped(s->block, ptr, s->linesize[c]);
1006                     if (++x == h) {
1007                         x = 0;
1008                         y++;
1009                     }
1010                 }
1011             }
1012         }
1013     }
1014     ret = 0;
1015  the_end:
1016     emms_c();
1017     return ret;
1018  out_of_range:
1019     dprintf("decode_sos: ac/dc index out of range\n");
1020     return -1;
1021 }
1022
1023 static int mjpeg_decode_dri(MJpegDecodeContext *s,
1024                              UINT8 *buf, int buf_size)
1025 {
1026     init_get_bits(&s->gb, buf, buf_size);
1027
1028     if (get_bits(&s->gb, 16) != 4)
1029         return -1;
1030     s->restart_interval = get_bits(&s->gb, 16);
1031     printf("restart interval: %d\n", s->restart_interval);
1032
1033     return 0;
1034 }
1035
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)
1039 {
1040     int len, id;
1041
1042     init_get_bits(&s->gb, buf, buf_size);
1043
1044     /* XXX: verify len field validity */
1045     len = get_bits(&s->gb, 16);
1046     if (len < 5)
1047         return -1;
1048
1049     id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1050     len -= 6;
1051
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'))
1056     {
1057         /* structure:
1058             4bytes      AVI1
1059             1bytes      polarity
1060             1bytes      always zero
1061             4bytes      field_size
1062             4bytes      field_size_less_padding
1063         */
1064         s->buggy_avid = 1;
1065         if (s->first_picture)
1066             printf("mjpeg: workarounding buggy AVID\n");
1067         s->interleaved_rows = get_bits(&s->gb, 8);
1068 #if 0
1069         skip_bits(&s->gb, 8);
1070         skip_bits(&s->gb, 32);
1071         skip_bits(&s->gb, 32);
1072         len -= 10;
1073 #endif
1074         if (s->interleaved_rows)
1075             printf("mjpeg: interleaved rows: %d\n", s->interleaved_rows);
1076         goto out;
1077     }
1078     
1079     len -= 2;
1080     
1081     if (id == FOURCC('J','F','I','F'))
1082     {
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));
1086         goto out;
1087     }
1088     
1089     /* Apple MJPEG-A */
1090     if ((start_code == APP1) && (len > (0x28 - 8)))
1091     {
1092         id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1093         len -= 4;
1094         if (id == FOURCC('m','j','p','g')) /* Apple MJPEG-A */
1095         {
1096 #if 0
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 */
1105 #endif
1106             if (s->first_picture)
1107                 printf("mjpeg: Apple MJPEG-A header found\n");
1108         }
1109     }
1110
1111 out:
1112     /* should check for further values.. */
1113     SKIP_REMAINING(&s->gb, len);
1114
1115     return 0;
1116 }
1117 #undef FOURCC
1118
1119 static int mjpeg_decode_com(MJpegDecodeContext *s,
1120                              UINT8 *buf, int buf_size)
1121 {
1122     int len, i;
1123     UINT8 *cbuf;
1124
1125     init_get_bits(&s->gb, buf, buf_size);
1126
1127     /* XXX: verify len field validity */
1128     len = get_bits(&s->gb, 16)-2;
1129     cbuf = malloc(len+1);
1130
1131     for (i = 0; i < len; i++)
1132         cbuf[i] = get_bits(&s->gb, 8);
1133     if (cbuf[i-1] == '\n')
1134         cbuf[i-1] = 0;
1135     else
1136         cbuf[i] = 0;
1137
1138     printf("mjpeg comment: '%s'\n", cbuf);
1139
1140     /* buggy avid, it puts EOI only at every 10th frame */
1141     if (!strcmp(cbuf, "AVID"))
1142     {
1143         s->buggy_avid = 1;
1144         if (s->first_picture)
1145             printf("mjpeg: workarounding buggy AVID\n");
1146     }
1147     
1148     free(cbuf);
1149
1150     return 0;
1151 }
1152
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)
1157 {
1158     UINT8 *buf_ptr;
1159     unsigned int state, v;
1160     int val;
1161
1162     state = *header_state;
1163     buf_ptr = *pbuf_ptr;
1164     if (state) {
1165         /* get marker */
1166     found:
1167         if (buf_ptr < buf_end) {
1168             val = *buf_ptr++;
1169             state = 0;
1170         } else {
1171             val = -1;
1172         }
1173     } else {
1174         while (buf_ptr < buf_end) {
1175             v = *buf_ptr++;
1176             if (v == 0xff) {
1177                 state = 1;
1178                 goto found;
1179             }
1180         }
1181         val = -1;
1182     }
1183     *pbuf_ptr = buf_ptr;
1184     *header_state = state;
1185     return val;
1186 }
1187
1188 static int mjpeg_decode_frame(AVCodecContext *avctx, 
1189                               void *data, int *data_size,
1190                               UINT8 *buf, int buf_size)
1191 {
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;
1197
1198     *data_size = 0;
1199
1200     /* no supplementary picture */
1201     if (buf_size == 0)
1202         return 0;
1203
1204     buf_ptr = buf;
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;
1215             if (code > 0)
1216                 s->start_code = code;
1217         } else {
1218             memcpy(s->buf_ptr, buf_start, len);
1219             s->buf_ptr += 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) {
1223                 s->buf_ptr--;
1224             } else {
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) {
1232                 case SOI:
1233                     s->restart_interval = 0;
1234                     /* nothing to do on SOI */
1235                     break;
1236                 case DQT:
1237                     mjpeg_decode_dqt(s, s->buffer, input_size);
1238                     break;
1239                 case DHT:
1240                     mjpeg_decode_dht(s, s->buffer, input_size);
1241                     break;
1242                 case SOF0:
1243                     mjpeg_decode_sof0(s, s->buffer, input_size);
1244                     break;
1245                 case SOS:
1246                     mjpeg_decode_sos(s, s->buffer, input_size);
1247                     if (s->start_code == EOI || s->buggy_avid || s->restart_interval) {
1248                         int l;
1249                         if (s->interlaced) {
1250                             s->bottom_field ^= 1;
1251                             /* if not bottom field, do not output image yet */
1252                             if (s->bottom_field)
1253                                 goto the_end;
1254                         }
1255                         for(i=0;i<3;i++) {
1256                             picture->data[i] = s->current_picture[i];
1257                             l = s->linesize[i];
1258                             if (s->interlaced)
1259                                 l >>= 1;
1260                             picture->linesize[i] = l;
1261                         }
1262                         *data_size = sizeof(AVPicture);
1263                         avctx->height = s->height;
1264                         if (s->interlaced)
1265                             avctx->height *= 2;
1266                         avctx->width = s->width;
1267                         /* XXX: not complete test ! */
1268                         switch((s->h_count[0] << 4) | s->v_count[0]) {
1269                         case 0x11:
1270                             avctx->pix_fmt = PIX_FMT_YUV444P;
1271                             break;
1272                         case 0x21:
1273                             avctx->pix_fmt = PIX_FMT_YUV422P;
1274                             break;
1275                         default:
1276                         case 0x22:
1277                             avctx->pix_fmt = PIX_FMT_YUV420P;
1278                             break;
1279                         }
1280                         /* dummy quality */
1281                         /* XXX: infer it with matrix */
1282                         avctx->quality = 3; 
1283                         goto the_end;
1284                     }
1285                     break;
1286                 case DRI:
1287                     mjpeg_decode_dri(s, s->buffer, input_size);
1288                     break;
1289                 case SOF1:
1290                 case SOF2:
1291                 case SOF3:
1292                 case SOF5:
1293                 case SOF6:
1294                 case SOF7:
1295                 case SOF9:
1296                 case SOF10:
1297                 case SOF11:
1298                 case SOF13:
1299                 case SOF14:
1300                 case SOF15:
1301                 case JPG:
1302                     printf("mjpeg: unsupported coding type (%x)\n", start_code);
1303                     return -1;
1304                 }
1305 #if 1
1306                 if (start_code >= 0xd0 && start_code <= 0xd7)
1307                 {
1308                     dprintf("restart marker: %d\n", start_code&0x0f);
1309                 }
1310                 else if (s->first_picture)
1311                 {
1312                     /* APP fields */
1313                     if (start_code >= 0xe0 && start_code <= 0xef)
1314                         mjpeg_decode_app(s, s->buffer, input_size, start_code);
1315                     /* Comment */
1316                     else if (start_code == COM)
1317                         mjpeg_decode_com(s, s->buffer, input_size);
1318                 }
1319 #endif
1320             }
1321         }
1322     }
1323  the_end:
1324     return buf_ptr - buf;
1325 }
1326
1327 static int mjpeg_decode_end(AVCodecContext *avctx)
1328 {
1329     MJpegDecodeContext *s = avctx->priv_data;
1330     int i, j;
1331
1332     for(i=0;i<MAX_COMPONENTS;i++)
1333         free(s->current_picture[i]);
1334     for(i=0;i<2;i++) {
1335         for(j=0;j<4;j++)
1336             free_vlc(&s->vlcs[i][j]);
1337     }
1338     return 0;
1339 }
1340
1341 AVCodec mjpeg_decoder = {
1342     "mjpeg",
1343     CODEC_TYPE_VIDEO,
1344     CODEC_ID_MJPEG,
1345     sizeof(MJpegDecodeContext),
1346     mjpeg_decode_init,
1347     NULL,
1348     mjpeg_decode_end,
1349     mjpeg_decode_frame,
1350     0,
1351     NULL
1352 };