]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpegdec.c
Commit functions used by both AMRNB and SIPR
[ffmpeg] / libavcodec / mjpegdec.c
1 /*
2  * MJPEG decoder
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003 Alex Beregszaszi
5  * Copyright (c) 2003-2004 Michael Niedermayer
6  *
7  * Support for external huffman table, various fixes (AVID workaround),
8  * aspecting, new decode_frame mechanism and apple mjpeg-b support
9  *                                  by Alex Beregszaszi
10  *
11  * This file is part of FFmpeg.
12  *
13  * FFmpeg is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * FFmpeg is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27
28 /**
29  * @file libavcodec/mjpegdec.c
30  * MJPEG decoder.
31  */
32
33 //#define DEBUG
34 #include <assert.h>
35
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "mjpeg.h"
39 #include "mjpegdec.h"
40 #include "jpeglsdec.h"
41
42
43 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
44                       int nb_codes, int use_static, int is_ac)
45 {
46     uint8_t huff_size[256+16];
47     uint16_t huff_code[256+16];
48
49     assert(nb_codes <= 256);
50
51     memset(huff_size, 0, sizeof(huff_size));
52     ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
53
54     if(is_ac){
55         memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes);
56         memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes);
57         memset(huff_size, 0, sizeof(uint8_t)*16);
58         memset(huff_code, 0, sizeof(uint16_t)*16);
59         nb_codes += 16;
60     }
61
62     return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
63 }
64
65 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) {
66     build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance,
67               ff_mjpeg_val_dc, 12, 0, 0);
68     build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance,
69               ff_mjpeg_val_dc, 12, 0, 0);
70     build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance,
71               ff_mjpeg_val_ac_luminance, 251, 0, 1);
72     build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance,
73               ff_mjpeg_val_ac_chrominance, 251, 0, 1);
74 }
75
76 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
77 {
78     MJpegDecodeContext *s = avctx->priv_data;
79
80     s->avctx = avctx;
81     dsputil_init(&s->dsp, avctx);
82     ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
83     s->buffer_size = 0;
84     s->buffer = NULL;
85     s->start_code = -1;
86     s->first_picture = 1;
87     s->org_height = avctx->coded_height;
88     avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
89
90     build_basic_mjpeg_vlc(s);
91
92     if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
93     {
94         av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
95         init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
96         if (ff_mjpeg_decode_dht(s)) {
97             av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n");
98             build_basic_mjpeg_vlc(s);
99         }
100     }
101     if (avctx->extradata_size > 9 &&
102         AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) {
103         if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */
104             s->interlace_polarity = 1; /* bottom field first */
105             av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
106         }
107     }
108     if (avctx->codec->id == CODEC_ID_AMV)
109         s->flipped = 1;
110
111     return 0;
112 }
113
114
115 /* quantize tables */
116 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
117 {
118     int len, index, i, j;
119
120     len = get_bits(&s->gb, 16) - 2;
121
122     while (len >= 65) {
123         /* only 8 bit precision handled */
124         if (get_bits(&s->gb, 4) != 0)
125         {
126             av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
127             return -1;
128         }
129         index = get_bits(&s->gb, 4);
130         if (index >= 4)
131             return -1;
132         av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
133         /* read quant table */
134         for(i=0;i<64;i++) {
135             j = s->scantable.permutated[i];
136             s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
137         }
138
139         //XXX FIXME finetune, and perhaps add dc too
140         s->qscale[index]= FFMAX(
141             s->quant_matrixes[index][s->scantable.permutated[1]],
142             s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
143         av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
144         len -= 65;
145     }
146
147     return 0;
148 }
149
150 /* decode huffman tables and build VLC decoders */
151 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
152 {
153     int len, index, i, class, n, v, code_max;
154     uint8_t bits_table[17];
155     uint8_t val_table[256];
156
157     len = get_bits(&s->gb, 16) - 2;
158
159     while (len > 0) {
160         if (len < 17)
161             return -1;
162         class = get_bits(&s->gb, 4);
163         if (class >= 2)
164             return -1;
165         index = get_bits(&s->gb, 4);
166         if (index >= 4)
167             return -1;
168         n = 0;
169         for(i=1;i<=16;i++) {
170             bits_table[i] = get_bits(&s->gb, 8);
171             n += bits_table[i];
172         }
173         len -= 17;
174         if (len < n || n > 256)
175             return -1;
176
177         code_max = 0;
178         for(i=0;i<n;i++) {
179             v = get_bits(&s->gb, 8);
180             if (v > code_max)
181                 code_max = v;
182             val_table[i] = v;
183         }
184         len -= n;
185
186         /* build VLC and flush previous vlc if present */
187         free_vlc(&s->vlcs[class][index]);
188         av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
189                class, index, code_max + 1);
190         if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
191             return -1;
192         }
193     }
194     return 0;
195 }
196
197 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
198 {
199     int len, nb_components, i, width, height, pix_fmt_id;
200
201     /* XXX: verify len field validity */
202     len = get_bits(&s->gb, 16);
203     s->bits= get_bits(&s->gb, 8);
204
205     if(s->pegasus_rct) s->bits=9;
206     if(s->bits==9 && !s->pegasus_rct) s->rct=1;    //FIXME ugly
207
208     if (s->bits != 8 && !s->lossless){
209         av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
210         return -1;
211     }
212
213     height = get_bits(&s->gb, 16);
214     width = get_bits(&s->gb, 16);
215
216     //HACK for odd_height.mov
217     if(s->interlaced && s->width == width && s->height == height + 1)
218         height= s->height;
219
220     av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
221     if(avcodec_check_dimensions(s->avctx, width, height))
222         return -1;
223
224     nb_components = get_bits(&s->gb, 8);
225     if (nb_components <= 0 ||
226         nb_components > MAX_COMPONENTS)
227         return -1;
228     if (s->ls && !(s->bits <= 8 || nb_components == 1)){
229         av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
230         return -1;
231     }
232     s->nb_components = nb_components;
233     s->h_max = 1;
234     s->v_max = 1;
235     for(i=0;i<nb_components;i++) {
236         /* component id */
237         s->component_id[i] = get_bits(&s->gb, 8) - 1;
238         s->h_count[i] = get_bits(&s->gb, 4);
239         s->v_count[i] = get_bits(&s->gb, 4);
240         /* compute hmax and vmax (only used in interleaved case) */
241         if (s->h_count[i] > s->h_max)
242             s->h_max = s->h_count[i];
243         if (s->v_count[i] > s->v_max)
244             s->v_max = s->v_count[i];
245         s->quant_index[i] = get_bits(&s->gb, 8);
246         if (s->quant_index[i] >= 4)
247             return -1;
248         av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
249                s->v_count[i], s->component_id[i], s->quant_index[i]);
250     }
251
252     if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
253         av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
254         return -1;
255     }
256
257     if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
258
259     /* if different size, realloc/alloc picture */
260     /* XXX: also check h_count and v_count */
261     if (width != s->width || height != s->height) {
262         av_freep(&s->qscale_table);
263
264         s->width = width;
265         s->height = height;
266         s->interlaced = 0;
267
268         /* test interlaced mode */
269         if (s->first_picture &&
270             s->org_height != 0 &&
271             s->height < ((s->org_height * 3) / 4)) {
272             s->interlaced = 1;
273             s->bottom_field = s->interlace_polarity;
274             s->picture.interlaced_frame = 1;
275             s->picture.top_field_first = !s->interlace_polarity;
276             height *= 2;
277         }
278
279         avcodec_set_dimensions(s->avctx, width, height);
280
281         s->qscale_table= av_mallocz((s->width+15)/16);
282
283         s->first_picture = 0;
284     }
285
286     if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
287         return 0;
288
289     /* XXX: not complete test ! */
290     pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
291                  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
292                  (s->h_count[2] << 12) | (s->v_count[2] <<  8) |
293                  (s->h_count[3] <<  4) |  s->v_count[3];
294     av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
295     //NOTE we do not allocate pictures large enough for the possible padding of h/v_count being 4
296     if(!(pix_fmt_id & 0xD0D0D0D0))
297         pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1;
298     if(!(pix_fmt_id & 0x0D0D0D0D))
299         pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1;
300
301     switch(pix_fmt_id){
302     case 0x11111100:
303         if(s->rgb){
304             s->avctx->pix_fmt = PIX_FMT_BGRA;
305         }else
306             s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
307         assert(s->nb_components==3);
308         break;
309     case 0x11000000:
310         s->avctx->pix_fmt = PIX_FMT_GRAY8;
311         break;
312     case 0x12111100:
313         s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P;
314         break;
315     case 0x21111100:
316         s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
317         break;
318     case 0x22111100:
319         s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
320         break;
321     default:
322         av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
323         return -1;
324     }
325     if(s->ls){
326         if(s->nb_components > 1)
327             s->avctx->pix_fmt = PIX_FMT_RGB24;
328         else if(s->bits <= 8)
329             s->avctx->pix_fmt = PIX_FMT_GRAY8;
330         else
331             s->avctx->pix_fmt = PIX_FMT_GRAY16;
332     }
333
334     if(s->picture.data[0])
335         s->avctx->release_buffer(s->avctx, &s->picture);
336
337     s->picture.reference= 0;
338     if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
339         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
340         return -1;
341     }
342     s->picture.pict_type= FF_I_TYPE;
343     s->picture.key_frame= 1;
344     s->got_picture = 1;
345
346     for(i=0; i<3; i++){
347         s->linesize[i]= s->picture.linesize[i] << s->interlaced;
348     }
349
350 //    printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
351
352     if (len != (8+(3*nb_components)))
353     {
354         av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
355     }
356
357     /* totally blank picture as progressive JPEG will only add details to it */
358     if(s->progressive){
359         int bw = (width  + s->h_max*8-1) / (s->h_max*8);
360         int bh = (height + s->v_max*8-1) / (s->v_max*8);
361         for(i=0; i<s->nb_components; i++) {
362             int size = bw * bh * s->h_count[i] * s->v_count[i];
363             av_freep(&s->blocks[i]);
364             av_freep(&s->last_nnz[i]);
365             s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
366             s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
367             s->block_stride[i] = bw * s->h_count[i];
368         }
369         memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
370     }
371     return 0;
372 }
373
374 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
375 {
376     int code;
377     code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
378     if (code < 0)
379     {
380         av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
381                &s->vlcs[0][dc_index]);
382         return 0xffff;
383     }
384
385     if(code)
386         return get_xbits(&s->gb, code);
387     else
388         return 0;
389 }
390
391 /* decode block and dequantize */
392 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
393                         int component, int dc_index, int ac_index, int16_t *quant_matrix)
394 {
395     int code, i, j, level, val;
396
397     /* DC coef */
398     val = mjpeg_decode_dc(s, dc_index);
399     if (val == 0xffff) {
400         av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
401         return -1;
402     }
403     val = val * quant_matrix[0] + s->last_dc[component];
404     s->last_dc[component] = val;
405     block[0] = val;
406     /* AC coefs */
407     i = 0;
408     {OPEN_READER(re, &s->gb)
409     for(;;) {
410         UPDATE_CACHE(re, &s->gb);
411         GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
412
413         /* EOB */
414         if (code == 0x10)
415             break;
416         i += ((unsigned)code) >> 4;
417         if(code != 0x100){
418             code &= 0xf;
419             if(code > MIN_CACHE_BITS - 16){
420                 UPDATE_CACHE(re, &s->gb)
421             }
422             {
423                 int cache=GET_CACHE(re,&s->gb);
424                 int sign=(~cache)>>31;
425                 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
426             }
427
428             LAST_SKIP_BITS(re, &s->gb, code)
429
430             if (i >= 63) {
431                 if(i == 63){
432                     j = s->scantable.permutated[63];
433                     block[j] = level * quant_matrix[j];
434                     break;
435                 }
436                 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
437                 return -1;
438             }
439             j = s->scantable.permutated[i];
440             block[j] = level * quant_matrix[j];
441         }
442     }
443     CLOSE_READER(re, &s->gb)}
444
445     return 0;
446 }
447
448 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component,
449                                  int dc_index, int16_t *quant_matrix, int Al)
450 {
451     int val;
452     s->dsp.clear_block(block);
453     val = mjpeg_decode_dc(s, dc_index);
454     if (val == 0xffff) {
455         av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
456         return -1;
457     }
458     val = (val * quant_matrix[0] << Al) + s->last_dc[component];
459     s->last_dc[component] = val;
460     block[0] = val;
461     return 0;
462 }
463
464 /* decode block and dequantize - progressive JPEG version */
465 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
466                                     int ac_index, int16_t *quant_matrix,
467                                     int ss, int se, int Al, int *EOBRUN)
468 {
469     int code, i, j, level, val, run;
470
471     if(*EOBRUN){
472         (*EOBRUN)--;
473         return 0;
474     }
475     {OPEN_READER(re, &s->gb)
476     for(i=ss;;i++) {
477         UPDATE_CACHE(re, &s->gb);
478         GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
479         /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
480         code -= 16;
481         if(code & 0xF) {
482             i += ((unsigned) code) >> 4;
483             code &= 0xf;
484             if(code > MIN_CACHE_BITS - 16){
485                 UPDATE_CACHE(re, &s->gb)
486             }
487             {
488                 int cache=GET_CACHE(re,&s->gb);
489                 int sign=(~cache)>>31;
490                 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
491             }
492
493             LAST_SKIP_BITS(re, &s->gb, code)
494
495             if (i >= se) {
496                 if(i == se){
497                     j = s->scantable.permutated[se];
498                     block[j] = level * quant_matrix[j] << Al;
499                     break;
500                 }
501                 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
502                 return -1;
503             }
504             j = s->scantable.permutated[i];
505             block[j] = level * quant_matrix[j] << Al;
506         }else{
507             run = ((unsigned) code) >> 4;
508             if(run == 0xF){// ZRL - skip 15 coefficients
509                 i += 15;
510             }else{
511                 val = run;
512                 run = (1 << run);
513                 UPDATE_CACHE(re, &s->gb);
514                 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);
515                 if(val)
516                     LAST_SKIP_BITS(re, &s->gb, val);
517                 *EOBRUN = run - 1;
518                 break;
519             }
520         }
521     }
522     CLOSE_READER(re, &s->gb)}
523     if(i > *last_nnz)
524         *last_nnz = i;
525     return 0;
526 }
527
528 #define REFINE_BIT(j) {\
529     UPDATE_CACHE(re, &s->gb);\
530     sign = block[j]>>15;\
531     block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\
532     LAST_SKIP_BITS(re, &s->gb, 1);\
533 }
534
535 #define ZERO_RUN \
536 for(;;i++) {\
537     if(i > last) {\
538         i += run;\
539         if(i > se) {\
540             av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\
541             return -1;\
542         }\
543         break;\
544     }\
545     j = s->scantable.permutated[i];\
546     if(block[j])\
547         REFINE_BIT(j)\
548     else if(run-- == 0)\
549         break;\
550 }
551
552 /* decode block and dequantize - progressive JPEG refinement pass */
553 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
554                         int ac_index, int16_t *quant_matrix,
555                         int ss, int se, int Al, int *EOBRUN)
556 {
557     int code, i=ss, j, sign, val, run;
558     int last = FFMIN(se, *last_nnz);
559
560     OPEN_READER(re, &s->gb);
561     if(*EOBRUN)
562         (*EOBRUN)--;
563     else {
564         for(;;i++) {
565             UPDATE_CACHE(re, &s->gb);
566             GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
567             /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
568             code -= 16;
569             if(code & 0xF) {
570                 run = ((unsigned) code) >> 4;
571                 UPDATE_CACHE(re, &s->gb);
572                 val = SHOW_UBITS(re, &s->gb, 1);
573                 LAST_SKIP_BITS(re, &s->gb, 1);
574                 ZERO_RUN;
575                 j = s->scantable.permutated[i];
576                 val--;
577                 block[j] = ((quant_matrix[j]^val)-val) << Al;
578                 if(i == se) {
579                     if(i > *last_nnz)
580                         *last_nnz = i;
581                     CLOSE_READER(re, &s->gb)
582                     return 0;
583                 }
584             }else{
585                 run = ((unsigned) code) >> 4;
586                 if(run == 0xF){
587                     ZERO_RUN;
588                 }else{
589                     val = run;
590                     run = (1 << run);
591                     if(val) {
592                         UPDATE_CACHE(re, &s->gb);
593                         run += SHOW_UBITS(re, &s->gb, val);
594                         LAST_SKIP_BITS(re, &s->gb, val);
595                     }
596                     *EOBRUN = run - 1;
597                     break;
598                 }
599             }
600         }
601
602         if(i > *last_nnz)
603             *last_nnz = i;
604     }
605
606     for(;i<=last;i++) {
607         j = s->scantable.permutated[i];
608         if(block[j])
609             REFINE_BIT(j)
610     }
611     CLOSE_READER(re, &s->gb);
612
613     return 0;
614 }
615 #undef REFINE_BIT
616 #undef ZERO_RUN
617
618 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
619     int i, mb_x, mb_y;
620     uint16_t (*buffer)[4];
621     int left[3], top[3], topleft[3];
622     const int linesize= s->linesize[0];
623     const int mask= (1<<s->bits)-1;
624
625     av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
626     buffer= s->ljpeg_buffer;
627
628     for(i=0; i<3; i++){
629         buffer[0][i]= 1 << (s->bits + point_transform - 1);
630     }
631     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
632         const int modified_predictor= mb_y ? predictor : 1;
633         uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
634
635         if (s->interlaced && s->bottom_field)
636             ptr += linesize >> 1;
637
638         for(i=0; i<3; i++){
639             top[i]= left[i]= topleft[i]= buffer[0][i];
640         }
641         for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
642             if (s->restart_interval && !s->restart_count)
643                 s->restart_count = s->restart_interval;
644
645             for(i=0;i<3;i++) {
646                 int pred;
647
648                 topleft[i]= top[i];
649                 top[i]= buffer[mb_x][i];
650
651                 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
652
653                 left[i]=
654                 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
655             }
656
657             if (s->restart_interval && !--s->restart_count) {
658                 align_get_bits(&s->gb);
659                 skip_bits(&s->gb, 16); /* skip RSTn */
660             }
661         }
662
663         if(s->rct){
664             for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
665                 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
666                 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
667                 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
668             }
669         }else if(s->pegasus_rct){
670             for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
671                 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
672                 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
673                 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
674             }
675         }else{
676             for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
677                 ptr[4*mb_x+0] = buffer[mb_x][2];
678                 ptr[4*mb_x+1] = buffer[mb_x][1];
679                 ptr[4*mb_x+2] = buffer[mb_x][0];
680             }
681         }
682     }
683     return 0;
684 }
685
686 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
687     int i, mb_x, mb_y;
688     const int nb_components=3;
689
690     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
691         for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
692             if (s->restart_interval && !s->restart_count)
693                 s->restart_count = s->restart_interval;
694
695             if(mb_x==0 || mb_y==0 || s->interlaced){
696                 for(i=0;i<nb_components;i++) {
697                     uint8_t *ptr;
698                     int n, h, v, x, y, c, j, linesize;
699                     n = s->nb_blocks[i];
700                     c = s->comp_index[i];
701                     h = s->h_scount[i];
702                     v = s->v_scount[i];
703                     x = 0;
704                     y = 0;
705                     linesize= s->linesize[c];
706
707                     for(j=0; j<n; j++) {
708                         int pred;
709
710                         ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
711                         if(y==0 && mb_y==0){
712                             if(x==0 && mb_x==0){
713                                 pred= 128 << point_transform;
714                             }else{
715                                 pred= ptr[-1];
716                             }
717                         }else{
718                             if(x==0 && mb_x==0){
719                                 pred= ptr[-linesize];
720                             }else{
721                                 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
722                             }
723                         }
724
725                         if (s->interlaced && s->bottom_field)
726                             ptr += linesize >> 1;
727                         *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
728
729                         if (++x == h) {
730                             x = 0;
731                             y++;
732                         }
733                     }
734                 }
735             }else{
736                 for(i=0;i<nb_components;i++) {
737                     uint8_t *ptr;
738                     int n, h, v, x, y, c, j, linesize;
739                     n = s->nb_blocks[i];
740                     c = s->comp_index[i];
741                     h = s->h_scount[i];
742                     v = s->v_scount[i];
743                     x = 0;
744                     y = 0;
745                     linesize= s->linesize[c];
746
747                     for(j=0; j<n; j++) {
748                         int pred;
749
750                         ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
751                         PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
752                         *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
753                         if (++x == h) {
754                             x = 0;
755                             y++;
756                         }
757                     }
758                 }
759             }
760             if (s->restart_interval && !--s->restart_count) {
761                 align_get_bits(&s->gb);
762                 skip_bits(&s->gb, 16); /* skip RSTn */
763             }
764         }
765     }
766     return 0;
767 }
768
769 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al){
770     int i, mb_x, mb_y;
771     uint8_t* data[MAX_COMPONENTS];
772     int linesize[MAX_COMPONENTS];
773
774     if(s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {
775         av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n");
776         s->flipped = 0;
777     }
778     for(i=0; i < nb_components; i++) {
779         int c = s->comp_index[i];
780         data[c] = s->picture.data[c];
781         linesize[c]=s->linesize[c];
782         s->coefs_finished[c] |= 1;
783         if(s->flipped) {
784             //picture should be flipped upside-down for this codec
785             data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));
786             linesize[c] *= -1;
787         }
788     }
789
790     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
791         for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
792             if (s->restart_interval && !s->restart_count)
793                 s->restart_count = s->restart_interval;
794
795             for(i=0;i<nb_components;i++) {
796                 uint8_t *ptr;
797                 int n, h, v, x, y, c, j;
798                 n = s->nb_blocks[i];
799                 c = s->comp_index[i];
800                 h = s->h_scount[i];
801                 v = s->v_scount[i];
802                 x = 0;
803                 y = 0;
804                 for(j=0;j<n;j++) {
805                     ptr = data[c] +
806                         (((linesize[c] * (v * mb_y + y) * 8) +
807                         (h * mb_x + x) * 8) >> s->avctx->lowres);
808                     if(s->interlaced && s->bottom_field)
809                         ptr += linesize[c] >> 1;
810                     if(!s->progressive) {
811                         s->dsp.clear_block(s->block);
812                         if(decode_block(s, s->block, i,
813                                      s->dc_index[i], s->ac_index[i],
814                                      s->quant_matrixes[ s->quant_index[c] ]) < 0) {
815                             av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
816                             return -1;
817                         }
818                         s->dsp.idct_put(ptr, linesize[c], s->block);
819                     } else {
820                         int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
821                         DCTELEM *block = s->blocks[c][block_idx];
822                         if(Ah)
823                             block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
824                         else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
825                             av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
826                             return -1;
827                         }
828                     }
829 //                    av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
830 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8);
831                     if (++x == h) {
832                         x = 0;
833                         y++;
834                     }
835                 }
836             }
837
838             if (s->restart_interval && !--s->restart_count) {
839                 align_get_bits(&s->gb);
840                 skip_bits(&s->gb, 16); /* skip RSTn */
841                 for (i=0; i<nb_components; i++) /* reset dc */
842                     s->last_dc[i] = 1024;
843             }
844         }
845     }
846     return 0;
847 }
848
849 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){
850     int mb_x, mb_y;
851     int EOBRUN = 0;
852     int c = s->comp_index[0];
853     uint8_t* data = s->picture.data[c];
854     int linesize = s->linesize[c];
855     int last_scan = 0;
856     int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ];
857
858     if(!Al) {
859         s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss);
860         last_scan = !~s->coefs_finished[c];
861     }
862
863     if(s->interlaced && s->bottom_field)
864         data += linesize >> 1;
865
866     for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
867         uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres);
868         int block_idx = mb_y * s->block_stride[c];
869         DCTELEM (*block)[64] = &s->blocks[c][block_idx];
870         uint8_t *last_nnz = &s->last_nnz[c][block_idx];
871         for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
872             int ret;
873             if(Ah)
874                 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
875                                               quant_matrix, ss, se, Al, &EOBRUN);
876             else
877                 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
878                                                quant_matrix, ss, se, Al, &EOBRUN);
879             if(ret < 0) {
880                 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
881                 return -1;
882             }
883             if(last_scan) {
884                 s->dsp.idct_put(ptr, linesize, *block);
885                 ptr += 8 >> s->avctx->lowres;
886             }
887         }
888     }
889     return 0;
890 }
891
892 int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
893 {
894     int len, nb_components, i, h, v, predictor, point_transform;
895     int index, id;
896     const int block_size= s->lossless ? 1 : 8;
897     int ilv, prev_shift;
898
899     /* XXX: verify len field validity */
900     len = get_bits(&s->gb, 16);
901     nb_components = get_bits(&s->gb, 8);
902     if (len != 6+2*nb_components)
903     {
904         av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
905         return -1;
906     }
907     for(i=0;i<nb_components;i++) {
908         id = get_bits(&s->gb, 8) - 1;
909         av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
910         /* find component index */
911         for(index=0;index<s->nb_components;index++)
912             if (id == s->component_id[index])
913                 break;
914         if (index == s->nb_components)
915         {
916             av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
917             return -1;
918         }
919
920         s->comp_index[i] = index;
921
922         s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
923         s->h_scount[i] = s->h_count[index];
924         s->v_scount[i] = s->v_count[index];
925
926         s->dc_index[i] = get_bits(&s->gb, 4);
927         s->ac_index[i] = get_bits(&s->gb, 4);
928
929         if (s->dc_index[i] <  0 || s->ac_index[i] < 0 ||
930             s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
931             goto out_of_range;
932         if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table)
933             goto out_of_range;
934     }
935
936     predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
937     ilv= get_bits(&s->gb, 8);    /* JPEG Se / JPEG-LS ILV */
938     prev_shift = get_bits(&s->gb, 4); /* Ah */
939     point_transform= get_bits(&s->gb, 4); /* Al */
940
941     for(i=0;i<nb_components;i++)
942         s->last_dc[i] = 1024;
943
944     if (nb_components > 1) {
945         /* interleaved stream */
946         s->mb_width  = (s->width  + s->h_max * block_size - 1) / (s->h_max * block_size);
947         s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
948     } else if(!s->ls) { /* skip this for JPEG-LS */
949         h = s->h_max / s->h_scount[0];
950         v = s->v_max / s->v_scount[0];
951         s->mb_width  = (s->width  + h * block_size - 1) / (h * block_size);
952         s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
953         s->nb_blocks[0] = 1;
954         s->h_scount[0] = 1;
955         s->v_scount[0] = 1;
956     }
957
958     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
959         av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "",
960                predictor, point_transform, ilv, s->bits,
961                s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
962
963
964     /* mjpeg-b can have padding bytes between sos and image data, skip them */
965     for (i = s->mjpb_skiptosod; i > 0; i--)
966         skip_bits(&s->gb, 8);
967
968     if(s->lossless){
969         if(CONFIG_JPEGLS_DECODER && s->ls){
970 //            for(){
971 //            reset_ls_coding_parameters(s, 0);
972
973             if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)
974                 return -1;
975         }else{
976             if(s->rgb){
977                 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
978                     return -1;
979             }else{
980                 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
981                     return -1;
982             }
983         }
984     }else{
985         if(s->progressive && predictor) {
986             if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0)
987                 return -1;
988         } else {
989             if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform) < 0)
990                 return -1;
991         }
992     }
993     emms_c();
994     return 0;
995  out_of_range:
996     av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
997     return -1;
998 }
999
1000 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1001 {
1002     if (get_bits(&s->gb, 16) != 4)
1003         return -1;
1004     s->restart_interval = get_bits(&s->gb, 16);
1005     s->restart_count = 0;
1006     av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
1007
1008     return 0;
1009 }
1010
1011 static int mjpeg_decode_app(MJpegDecodeContext *s)
1012 {
1013     int len, id, i;
1014
1015     len = get_bits(&s->gb, 16);
1016     if (len < 5)
1017         return -1;
1018     if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
1019         return -1;
1020
1021     id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1022     id = be2me_32(id);
1023     len -= 6;
1024
1025     if(s->avctx->debug & FF_DEBUG_STARTCODE){
1026         av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1027     }
1028
1029     /* buggy AVID, it puts EOI only at every 10th frame */
1030     /* also this fourcc is used by non-avid files too, it holds some
1031        informations, but it's always present in AVID creates files */
1032     if (id == AV_RL32("AVI1"))
1033     {
1034         /* structure:
1035             4bytes      AVI1
1036             1bytes      polarity
1037             1bytes      always zero
1038             4bytes      field_size
1039             4bytes      field_size_less_padding
1040         */
1041             s->buggy_avid = 1;
1042 //        if (s->first_picture)
1043 //            printf("mjpeg: workarounding buggy AVID\n");
1044         i = get_bits(&s->gb, 8);
1045         if     (i==2) s->bottom_field= 1;
1046         else if(i==1) s->bottom_field= 0;
1047 #if 0
1048         skip_bits(&s->gb, 8);
1049         skip_bits(&s->gb, 32);
1050         skip_bits(&s->gb, 32);
1051         len -= 10;
1052 #endif
1053 //        if (s->interlace_polarity)
1054 //            printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
1055         goto out;
1056     }
1057
1058 //    len -= 2;
1059
1060     if (id == AV_RL32("JFIF"))
1061     {
1062         int t_w, t_h, v1, v2;
1063         skip_bits(&s->gb, 8); /* the trailing zero-byte */
1064         v1= get_bits(&s->gb, 8);
1065         v2= get_bits(&s->gb, 8);
1066         skip_bits(&s->gb, 8);
1067
1068         s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
1069         s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
1070
1071         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1072             av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1073                 v1, v2,
1074                 s->avctx->sample_aspect_ratio.num,
1075                 s->avctx->sample_aspect_ratio.den
1076             );
1077
1078         t_w = get_bits(&s->gb, 8);
1079         t_h = get_bits(&s->gb, 8);
1080         if (t_w && t_h)
1081         {
1082             /* skip thumbnail */
1083             if (len-10-(t_w*t_h*3) > 0)
1084                 len -= t_w*t_h*3;
1085         }
1086         len -= 10;
1087         goto out;
1088     }
1089
1090     if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e'))
1091     {
1092         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1093             av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1094         skip_bits(&s->gb, 16); /* version */
1095         skip_bits(&s->gb, 16); /* flags0 */
1096         skip_bits(&s->gb, 16); /* flags1 */
1097         skip_bits(&s->gb, 8);  /* transform */
1098         len -= 7;
1099         goto out;
1100     }
1101
1102     if (id == AV_RL32("LJIF")){
1103         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1104             av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
1105         skip_bits(&s->gb, 16); /* version ? */
1106         skip_bits(&s->gb, 16); /* unknwon always 0? */
1107         skip_bits(&s->gb, 16); /* unknwon always 0? */
1108         skip_bits(&s->gb, 16); /* unknwon always 0? */
1109         switch( get_bits(&s->gb, 8)){
1110         case 1:
1111             s->rgb= 1;
1112             s->pegasus_rct=0;
1113             break;
1114         case 2:
1115             s->rgb= 1;
1116             s->pegasus_rct=1;
1117             break;
1118         default:
1119             av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1120         }
1121         len -= 9;
1122         goto out;
1123     }
1124
1125     /* Apple MJPEG-A */
1126     if ((s->start_code == APP1) && (len > (0x28 - 8)))
1127     {
1128         id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1129         id = be2me_32(id);
1130         len -= 4;
1131         if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */
1132         {
1133 #if 0
1134             skip_bits(&s->gb, 32); /* field size */
1135             skip_bits(&s->gb, 32); /* pad field size */
1136             skip_bits(&s->gb, 32); /* next off */
1137             skip_bits(&s->gb, 32); /* quant off */
1138             skip_bits(&s->gb, 32); /* huff off */
1139             skip_bits(&s->gb, 32); /* image off */
1140             skip_bits(&s->gb, 32); /* scan off */
1141             skip_bits(&s->gb, 32); /* data off */
1142 #endif
1143             if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1144                 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1145         }
1146     }
1147
1148 out:
1149     /* slow but needed for extreme adobe jpegs */
1150     if (len < 0)
1151         av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
1152     while(--len > 0)
1153         skip_bits(&s->gb, 8);
1154
1155     return 0;
1156 }
1157
1158 static int mjpeg_decode_com(MJpegDecodeContext *s)
1159 {
1160     int len = get_bits(&s->gb, 16);
1161     if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
1162         char *cbuf = av_malloc(len - 1);
1163         if (cbuf) {
1164             int i;
1165             for (i = 0; i < len - 2; i++)
1166                 cbuf[i] = get_bits(&s->gb, 8);
1167             if (i > 0 && cbuf[i-1] == '\n')
1168                 cbuf[i-1] = 0;
1169             else
1170                 cbuf[i] = 0;
1171
1172             if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1173                 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1174
1175             /* buggy avid, it puts EOI only at every 10th frame */
1176             if (!strcmp(cbuf, "AVID"))
1177             {
1178                 s->buggy_avid = 1;
1179                 //        if (s->first_picture)
1180                 //            printf("mjpeg: workarounding buggy AVID\n");
1181             }
1182             else if(!strcmp(cbuf, "CS=ITU601")){
1183                 s->cs_itu601= 1;
1184             }
1185             else if((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1186                     (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20))){
1187                 s->flipped = 1;
1188             }
1189
1190             av_free(cbuf);
1191         }
1192     }
1193
1194     return 0;
1195 }
1196
1197 #if 0
1198 static int valid_marker_list[] =
1199 {
1200         /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
1201 /* 0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1202 /* 1 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1203 /* 2 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1204 /* 3 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1205 /* 4 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1206 /* 5 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1207 /* 6 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1208 /* 7 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1209 /* 8 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1210 /* 9 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1211 /* a */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1212 /* b */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1213 /* c */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1214 /* d */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1215 /* e */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1216 /* f */    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1217 }
1218 #endif
1219
1220 /* return the 8 bit start code value and update the search
1221    state. Return -1 if no start code found */
1222 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1223 {
1224     const uint8_t *buf_ptr;
1225     unsigned int v, v2;
1226     int val;
1227 #ifdef DEBUG
1228     int skipped=0;
1229 #endif
1230
1231     buf_ptr = *pbuf_ptr;
1232     while (buf_ptr < buf_end) {
1233         v = *buf_ptr++;
1234         v2 = *buf_ptr;
1235         if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1236             val = *buf_ptr++;
1237             goto found;
1238         }
1239 #ifdef DEBUG
1240         skipped++;
1241 #endif
1242     }
1243     val = -1;
1244 found:
1245     dprintf(NULL, "find_marker skipped %d bytes\n", skipped);
1246     *pbuf_ptr = buf_ptr;
1247     return val;
1248 }
1249
1250 int ff_mjpeg_decode_frame(AVCodecContext *avctx,
1251                               void *data, int *data_size,
1252                               AVPacket *avpkt)
1253 {
1254     const uint8_t *buf = avpkt->data;
1255     int buf_size = avpkt->size;
1256     MJpegDecodeContext *s = avctx->priv_data;
1257     const uint8_t *buf_end, *buf_ptr;
1258     int start_code;
1259     AVFrame *picture = data;
1260
1261     s->got_picture = 0; // picture from previous image can not be reused
1262     buf_ptr = buf;
1263     buf_end = buf + buf_size;
1264     while (buf_ptr < buf_end) {
1265         /* find start next marker */
1266         start_code = find_marker(&buf_ptr, buf_end);
1267         {
1268             /* EOF */
1269             if (start_code < 0) {
1270                 goto the_end;
1271             } else {
1272                 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr);
1273
1274                 if ((buf_end - buf_ptr) > s->buffer_size)
1275                 {
1276                     av_free(s->buffer);
1277                     s->buffer_size = buf_end-buf_ptr;
1278                     s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
1279                     av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
1280                         s->buffer_size);
1281                 }
1282
1283                 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1284                 if (start_code == SOS && !s->ls)
1285                 {
1286                     const uint8_t *src = buf_ptr;
1287                     uint8_t *dst = s->buffer;
1288
1289                     while (src<buf_end)
1290                     {
1291                         uint8_t x = *(src++);
1292
1293                         *(dst++) = x;
1294                         if (avctx->codec_id != CODEC_ID_THP)
1295                         {
1296                             if (x == 0xff) {
1297                                 while (src < buf_end && x == 0xff)
1298                                     x = *(src++);
1299
1300                                 if (x >= 0xd0 && x <= 0xd7)
1301                                     *(dst++) = x;
1302                                 else if (x)
1303                                     break;
1304                             }
1305                         }
1306                     }
1307                     init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
1308
1309                     av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1310                            (buf_end - buf_ptr) - (dst - s->buffer));
1311                 }
1312                 else if(start_code == SOS && s->ls){
1313                     const uint8_t *src = buf_ptr;
1314                     uint8_t *dst = s->buffer;
1315                     int bit_count = 0;
1316                     int t = 0, b = 0;
1317                     PutBitContext pb;
1318
1319                     s->cur_scan++;
1320
1321                     /* find marker */
1322                     while (src + t < buf_end){
1323                         uint8_t x = src[t++];
1324                         if (x == 0xff){
1325                             while((src + t < buf_end) && x == 0xff)
1326                                 x = src[t++];
1327                             if (x & 0x80) {
1328                                 t -= 2;
1329                                 break;
1330                             }
1331                         }
1332                     }
1333                     bit_count = t * 8;
1334
1335                     init_put_bits(&pb, dst, t);
1336
1337                     /* unescape bitstream */
1338                     while(b < t){
1339                         uint8_t x = src[b++];
1340                         put_bits(&pb, 8, x);
1341                         if(x == 0xFF){
1342                             x = src[b++];
1343                             put_bits(&pb, 7, x);
1344                             bit_count--;
1345                         }
1346                     }
1347                     flush_put_bits(&pb);
1348
1349                     init_get_bits(&s->gb, dst, bit_count);
1350                 }
1351                 else
1352                     init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
1353
1354                 s->start_code = start_code;
1355                 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1356                     av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1357                 }
1358
1359                 /* process markers */
1360                 if (start_code >= 0xd0 && start_code <= 0xd7) {
1361                     av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
1362                     /* APP fields */
1363                 } else if (start_code >= APP0 && start_code <= APP15) {
1364                     mjpeg_decode_app(s);
1365                     /* Comment */
1366                 } else if (start_code == COM){
1367                     mjpeg_decode_com(s);
1368                 }
1369
1370                 switch(start_code) {
1371                 case SOI:
1372                     s->restart_interval = 0;
1373
1374                     s->restart_count = 0;
1375                     /* nothing to do on SOI */
1376                     break;
1377                 case DQT:
1378                     ff_mjpeg_decode_dqt(s);
1379                     break;
1380                 case DHT:
1381                     if(ff_mjpeg_decode_dht(s) < 0){
1382                         av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1383                         return -1;
1384                     }
1385                     break;
1386                 case SOF0:
1387                 case SOF1:
1388                     s->lossless=0;
1389                     s->ls=0;
1390                     s->progressive=0;
1391                     if (ff_mjpeg_decode_sof(s) < 0)
1392                         return -1;
1393                     break;
1394                 case SOF2:
1395                     s->lossless=0;
1396                     s->ls=0;
1397                     s->progressive=1;
1398                     if (ff_mjpeg_decode_sof(s) < 0)
1399                         return -1;
1400                     break;
1401                 case SOF3:
1402                     s->lossless=1;
1403                     s->ls=0;
1404                     s->progressive=0;
1405                     if (ff_mjpeg_decode_sof(s) < 0)
1406                         return -1;
1407                     break;
1408                 case SOF48:
1409                     s->lossless=1;
1410                     s->ls=1;
1411                     s->progressive=0;
1412                     if (ff_mjpeg_decode_sof(s) < 0)
1413                         return -1;
1414                     break;
1415                 case LSE:
1416                     if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
1417                         return -1;
1418                     break;
1419                 case EOI:
1420                     s->cur_scan = 0;
1421                     if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1422                         break;
1423 eoi_parser:
1424                     if (!s->got_picture) {
1425                         av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n");
1426                         break;
1427                     }
1428                     {
1429                         if (s->interlaced) {
1430                             s->bottom_field ^= 1;
1431                             /* if not bottom field, do not output image yet */
1432                             if (s->bottom_field == !s->interlace_polarity)
1433                                 goto not_the_end;
1434                         }
1435                         *picture = s->picture;
1436                         *data_size = sizeof(AVFrame);
1437
1438                         if(!s->lossless){
1439                             picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
1440                             picture->qstride= 0;
1441                             picture->qscale_table= s->qscale_table;
1442                             memset(picture->qscale_table, picture->quality, (s->width+15)/16);
1443                             if(avctx->debug & FF_DEBUG_QP)
1444                                 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
1445                             picture->quality*= FF_QP2LAMBDA;
1446                         }
1447
1448                         goto the_end;
1449                     }
1450                     break;
1451                 case SOS:
1452                     if (!s->got_picture) {
1453                         av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n");
1454                         break;
1455                     }
1456                     ff_mjpeg_decode_sos(s);
1457                     /* buggy avid puts EOI every 10-20th frame */
1458                     /* if restart period is over process EOI */
1459                     if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1460                         goto eoi_parser;
1461                     break;
1462                 case DRI:
1463                     mjpeg_decode_dri(s);
1464                     break;
1465                 case SOF5:
1466                 case SOF6:
1467                 case SOF7:
1468                 case SOF9:
1469                 case SOF10:
1470                 case SOF11:
1471                 case SOF13:
1472                 case SOF14:
1473                 case SOF15:
1474                 case JPG:
1475                     av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
1476                     break;
1477 //                default:
1478 //                    printf("mjpeg: unsupported marker (%x)\n", start_code);
1479 //                    break;
1480                 }
1481
1482 not_the_end:
1483                 /* eof process start code */
1484                 buf_ptr += (get_bits_count(&s->gb)+7)/8;
1485                 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
1486                        (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
1487             }
1488         }
1489     }
1490     if (s->got_picture) {
1491         av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1492         goto eoi_parser;
1493     }
1494     av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1495     return -1;
1496 the_end:
1497     av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr);
1498 //    return buf_end - buf_ptr;
1499     return buf_ptr - buf;
1500 }
1501
1502 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
1503 {
1504     MJpegDecodeContext *s = avctx->priv_data;
1505     int i, j;
1506
1507     if (s->picture.data[0])
1508         avctx->release_buffer(avctx, &s->picture);
1509
1510     av_free(s->buffer);
1511     av_free(s->qscale_table);
1512     av_freep(&s->ljpeg_buffer);
1513     s->ljpeg_buffer_size=0;
1514
1515     for(i=0;i<2;i++) {
1516         for(j=0;j<4;j++)
1517             free_vlc(&s->vlcs[i][j]);
1518     }
1519     for(i=0; i<MAX_COMPONENTS; i++) {
1520         av_freep(&s->blocks[i]);
1521         av_freep(&s->last_nnz[i]);
1522     }
1523     return 0;
1524 }
1525
1526 AVCodec mjpeg_decoder = {
1527     "mjpeg",
1528     CODEC_TYPE_VIDEO,
1529     CODEC_ID_MJPEG,
1530     sizeof(MJpegDecodeContext),
1531     ff_mjpeg_decode_init,
1532     NULL,
1533     ff_mjpeg_decode_end,
1534     ff_mjpeg_decode_frame,
1535     CODEC_CAP_DR1,
1536     NULL,
1537     .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1538 };
1539
1540 AVCodec thp_decoder = {
1541     "thp",
1542     CODEC_TYPE_VIDEO,
1543     CODEC_ID_THP,
1544     sizeof(MJpegDecodeContext),
1545     ff_mjpeg_decode_init,
1546     NULL,
1547     ff_mjpeg_decode_end,
1548     ff_mjpeg_decode_frame,
1549     CODEC_CAP_DR1,
1550     NULL,
1551     .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
1552 };