Add frame_size as a codec parameter requirement for Speex in
[ffmpeg-lucabe.git] / libavcodec / mjpegdec.c
blobad4187b121b16f878f721d2ee6ce5ef3ca5ee139
1 /*
2 * MJPEG decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
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
11 * This file is part of FFmpeg.
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.
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.
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
28 /**
29 * @file libavcodec/mjpegdec.c
30 * MJPEG decoder.
33 //#define DEBUG
34 #include <assert.h>
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "mjpeg.h"
39 #include "mjpegdec.h"
40 #include "jpeglsdec.h"
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)
46 uint8_t huff_size[256+16];
47 uint16_t huff_code[256+16];
49 assert(nb_codes <= 256);
51 memset(huff_size, 0, sizeof(huff_size));
52 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
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;
62 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
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);
76 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
78 MJpegDecodeContext *s = avctx->priv_data;
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;
90 build_basic_mjpeg_vlc(s);
92 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
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);
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");
109 return 0;
113 /* quantize tables */
114 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
116 int len, index, i, j;
118 len = get_bits(&s->gb, 16) - 2;
120 while (len >= 65) {
121 /* only 8 bit precision handled */
122 if (get_bits(&s->gb, 4) != 0)
124 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
125 return -1;
127 index = get_bits(&s->gb, 4);
128 if (index >= 4)
129 return -1;
130 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
131 /* read quant table */
132 for(i=0;i<64;i++) {
133 j = s->scantable.permutated[i];
134 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
137 //XXX FIXME finetune, and perhaps add dc too
138 s->qscale[index]= FFMAX(
139 s->quant_matrixes[index][s->scantable.permutated[1]],
140 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
141 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
142 len -= 65;
145 return 0;
148 /* decode huffman tables and build VLC decoders */
149 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
151 int len, index, i, class, n, v, code_max;
152 uint8_t bits_table[17];
153 uint8_t val_table[256];
155 len = get_bits(&s->gb, 16) - 2;
157 while (len > 0) {
158 if (len < 17)
159 return -1;
160 class = get_bits(&s->gb, 4);
161 if (class >= 2)
162 return -1;
163 index = get_bits(&s->gb, 4);
164 if (index >= 4)
165 return -1;
166 n = 0;
167 for(i=1;i<=16;i++) {
168 bits_table[i] = get_bits(&s->gb, 8);
169 n += bits_table[i];
171 len -= 17;
172 if (len < n || n > 256)
173 return -1;
175 code_max = 0;
176 for(i=0;i<n;i++) {
177 v = get_bits(&s->gb, 8);
178 if (v > code_max)
179 code_max = v;
180 val_table[i] = v;
182 len -= n;
184 /* build VLC and flush previous vlc if present */
185 free_vlc(&s->vlcs[class][index]);
186 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
187 class, index, code_max + 1);
188 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
189 return -1;
192 return 0;
195 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
197 int len, nb_components, i, width, height, pix_fmt_id;
199 /* XXX: verify len field validity */
200 len = get_bits(&s->gb, 16);
201 s->bits= get_bits(&s->gb, 8);
203 if(s->pegasus_rct) s->bits=9;
204 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
206 if (s->bits != 8 && !s->lossless){
207 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
208 return -1;
211 height = get_bits(&s->gb, 16);
212 width = get_bits(&s->gb, 16);
214 //HACK for odd_height.mov
215 if(s->interlaced && s->width == width && s->height == height + 1)
216 height= s->height;
218 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
219 if(avcodec_check_dimensions(s->avctx, width, height))
220 return -1;
222 nb_components = get_bits(&s->gb, 8);
223 if (nb_components <= 0 ||
224 nb_components > MAX_COMPONENTS)
225 return -1;
226 if (s->ls && !(s->bits <= 8 || nb_components == 1)){
227 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
228 return -1;
230 s->nb_components = nb_components;
231 s->h_max = 1;
232 s->v_max = 1;
233 for(i=0;i<nb_components;i++) {
234 /* component id */
235 s->component_id[i] = get_bits(&s->gb, 8) - 1;
236 s->h_count[i] = get_bits(&s->gb, 4);
237 s->v_count[i] = get_bits(&s->gb, 4);
238 /* compute hmax and vmax (only used in interleaved case) */
239 if (s->h_count[i] > s->h_max)
240 s->h_max = s->h_count[i];
241 if (s->v_count[i] > s->v_max)
242 s->v_max = s->v_count[i];
243 s->quant_index[i] = get_bits(&s->gb, 8);
244 if (s->quant_index[i] >= 4)
245 return -1;
246 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
247 s->v_count[i], s->component_id[i], s->quant_index[i]);
250 if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
251 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
252 return -1;
255 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
257 /* if different size, realloc/alloc picture */
258 /* XXX: also check h_count and v_count */
259 if (width != s->width || height != s->height) {
260 av_freep(&s->qscale_table);
262 s->width = width;
263 s->height = height;
264 s->interlaced = 0;
266 /* test interlaced mode */
267 if (s->first_picture &&
268 s->org_height != 0 &&
269 s->height < ((s->org_height * 3) / 4)) {
270 s->interlaced = 1;
271 s->bottom_field = s->interlace_polarity;
272 s->picture.interlaced_frame = 1;
273 s->picture.top_field_first = !s->interlace_polarity;
274 height *= 2;
277 avcodec_set_dimensions(s->avctx, width, height);
279 s->qscale_table= av_mallocz((s->width+15)/16);
281 s->first_picture = 0;
284 if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
285 return 0;
287 /* XXX: not complete test ! */
288 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
289 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
290 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
291 (s->h_count[3] << 4) | s->v_count[3];
292 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
293 if(!(pix_fmt_id & 0x10101010))
294 pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1;
295 if(!(pix_fmt_id & 0x01010101))
296 pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1;
298 switch(pix_fmt_id){
299 case 0x11111100:
300 if(s->rgb){
301 s->avctx->pix_fmt = PIX_FMT_RGB32;
302 }else
303 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
304 assert(s->nb_components==3);
305 break;
306 case 0x11000000:
307 s->avctx->pix_fmt = PIX_FMT_GRAY8;
308 break;
309 case 0x12111100:
310 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P;
311 break;
312 case 0x21111100:
313 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
314 break;
315 case 0x22111100:
316 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
317 break;
318 default:
319 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
320 return -1;
322 if(s->ls){
323 if(s->nb_components > 1)
324 s->avctx->pix_fmt = PIX_FMT_RGB24;
325 else if(s->bits <= 8)
326 s->avctx->pix_fmt = PIX_FMT_GRAY8;
327 else
328 s->avctx->pix_fmt = PIX_FMT_GRAY16;
331 if(s->picture.data[0])
332 s->avctx->release_buffer(s->avctx, &s->picture);
334 s->picture.reference= 0;
335 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
336 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
337 return -1;
339 s->picture.pict_type= FF_I_TYPE;
340 s->picture.key_frame= 1;
341 s->got_picture = 1;
343 for(i=0; i<3; i++){
344 s->linesize[i]= s->picture.linesize[i] << s->interlaced;
347 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
349 if (len != (8+(3*nb_components)))
351 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
354 /* totally blank picture as progressive JPEG will only add details to it */
355 if(s->progressive){
356 int bw = (width + s->h_max*8-1) / (s->h_max*8);
357 int bh = (height + s->v_max*8-1) / (s->v_max*8);
358 for(i=0; i<s->nb_components; i++) {
359 int size = bw * bh * s->h_count[i] * s->v_count[i];
360 av_freep(&s->blocks[i]);
361 av_freep(&s->last_nnz[i]);
362 s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
363 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
364 s->block_stride[i] = bw * s->h_count[i];
366 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
368 return 0;
371 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
373 int code;
374 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
375 if (code < 0)
377 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
378 &s->vlcs[0][dc_index]);
379 return 0xffff;
382 if(code)
383 return get_xbits(&s->gb, code);
384 else
385 return 0;
388 /* decode block and dequantize */
389 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
390 int component, int dc_index, int ac_index, int16_t *quant_matrix)
392 int code, i, j, level, val;
394 /* DC coef */
395 val = mjpeg_decode_dc(s, dc_index);
396 if (val == 0xffff) {
397 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
398 return -1;
400 val = val * quant_matrix[0] + s->last_dc[component];
401 s->last_dc[component] = val;
402 block[0] = val;
403 /* AC coefs */
404 i = 0;
405 {OPEN_READER(re, &s->gb)
406 for(;;) {
407 UPDATE_CACHE(re, &s->gb);
408 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
410 /* EOB */
411 if (code == 0x10)
412 break;
413 i += ((unsigned)code) >> 4;
414 if(code != 0x100){
415 code &= 0xf;
416 if(code > MIN_CACHE_BITS - 16){
417 UPDATE_CACHE(re, &s->gb)
420 int cache=GET_CACHE(re,&s->gb);
421 int sign=(~cache)>>31;
422 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
425 LAST_SKIP_BITS(re, &s->gb, code)
427 if (i >= 63) {
428 if(i == 63){
429 j = s->scantable.permutated[63];
430 block[j] = level * quant_matrix[j];
431 break;
433 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
434 return -1;
436 j = s->scantable.permutated[i];
437 block[j] = level * quant_matrix[j];
440 CLOSE_READER(re, &s->gb)}
442 return 0;
445 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component,
446 int dc_index, int16_t *quant_matrix, int Al)
448 int val;
449 s->dsp.clear_block(block);
450 val = mjpeg_decode_dc(s, dc_index);
451 if (val == 0xffff) {
452 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
453 return -1;
455 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
456 s->last_dc[component] = val;
457 block[0] = val;
458 return 0;
461 /* decode block and dequantize - progressive JPEG version */
462 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
463 int ac_index, int16_t *quant_matrix,
464 int ss, int se, int Al, int *EOBRUN)
466 int code, i, j, level, val, run;
468 if(*EOBRUN){
469 (*EOBRUN)--;
470 return 0;
472 {OPEN_READER(re, &s->gb)
473 for(i=ss;;i++) {
474 UPDATE_CACHE(re, &s->gb);
475 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
476 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
477 code -= 16;
478 if(code & 0xF) {
479 i += ((unsigned) code) >> 4;
480 code &= 0xf;
481 if(code > MIN_CACHE_BITS - 16){
482 UPDATE_CACHE(re, &s->gb)
485 int cache=GET_CACHE(re,&s->gb);
486 int sign=(~cache)>>31;
487 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
490 LAST_SKIP_BITS(re, &s->gb, code)
492 if (i >= se) {
493 if(i == se){
494 j = s->scantable.permutated[se];
495 block[j] = level * quant_matrix[j] << Al;
496 break;
498 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
499 return -1;
501 j = s->scantable.permutated[i];
502 block[j] = level * quant_matrix[j] << Al;
503 }else{
504 run = ((unsigned) code) >> 4;
505 if(run == 0xF){// ZRL - skip 15 coefficients
506 i += 15;
507 }else{
508 val = run;
509 run = (1 << run);
510 UPDATE_CACHE(re, &s->gb);
511 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);
512 if(val)
513 LAST_SKIP_BITS(re, &s->gb, val);
514 *EOBRUN = run - 1;
515 break;
519 CLOSE_READER(re, &s->gb)}
520 if(i > *last_nnz)
521 *last_nnz = i;
522 return 0;
525 #define REFINE_BIT(j) {\
526 UPDATE_CACHE(re, &s->gb);\
527 sign = block[j]>>15;\
528 block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\
529 LAST_SKIP_BITS(re, &s->gb, 1);\
532 #define ZERO_RUN \
533 for(;;i++) {\
534 if(i > last) {\
535 i += run;\
536 if(i > se) {\
537 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\
538 return -1;\
540 break;\
542 j = s->scantable.permutated[i];\
543 if(block[j])\
544 REFINE_BIT(j)\
545 else if(run-- == 0)\
546 break;\
549 /* decode block and dequantize - progressive JPEG refinement pass */
550 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
551 int ac_index, int16_t *quant_matrix,
552 int ss, int se, int Al, int *EOBRUN)
554 int code, i=ss, j, sign, val, run;
555 int last = FFMIN(se, *last_nnz);
557 OPEN_READER(re, &s->gb);
558 if(*EOBRUN)
559 (*EOBRUN)--;
560 else {
561 for(;;i++) {
562 UPDATE_CACHE(re, &s->gb);
563 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
564 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
565 code -= 16;
566 if(code & 0xF) {
567 run = ((unsigned) code) >> 4;
568 UPDATE_CACHE(re, &s->gb);
569 val = SHOW_UBITS(re, &s->gb, 1);
570 LAST_SKIP_BITS(re, &s->gb, 1);
571 ZERO_RUN;
572 j = s->scantable.permutated[i];
573 val--;
574 block[j] = ((quant_matrix[j]^val)-val) << Al;
575 if(i == se) {
576 if(i > *last_nnz)
577 *last_nnz = i;
578 CLOSE_READER(re, &s->gb)
579 return 0;
581 }else{
582 run = ((unsigned) code) >> 4;
583 if(run == 0xF){
584 ZERO_RUN;
585 }else{
586 val = run;
587 run = (1 << run);
588 if(val) {
589 UPDATE_CACHE(re, &s->gb);
590 run += SHOW_UBITS(re, &s->gb, val);
591 LAST_SKIP_BITS(re, &s->gb, val);
593 *EOBRUN = run - 1;
594 break;
599 if(i > *last_nnz)
600 *last_nnz = i;
603 for(;i<=last;i++) {
604 j = s->scantable.permutated[i];
605 if(block[j])
606 REFINE_BIT(j)
608 CLOSE_READER(re, &s->gb);
610 return 0;
612 #undef REFINE_BIT
613 #undef ZERO_RUN
615 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
616 int i, mb_x, mb_y;
617 uint16_t buffer[32768][4];
618 int left[3], top[3], topleft[3];
619 const int linesize= s->linesize[0];
620 const int mask= (1<<s->bits)-1;
622 if((unsigned)s->mb_width > 32768) //dynamic alloc
623 return -1;
625 for(i=0; i<3; i++){
626 buffer[0][i]= 1 << (s->bits + point_transform - 1);
628 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
629 const int modified_predictor= mb_y ? predictor : 1;
630 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
632 if (s->interlaced && s->bottom_field)
633 ptr += linesize >> 1;
635 for(i=0; i<3; i++){
636 top[i]= left[i]= topleft[i]= buffer[0][i];
638 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
639 if (s->restart_interval && !s->restart_count)
640 s->restart_count = s->restart_interval;
642 for(i=0;i<3;i++) {
643 int pred;
645 topleft[i]= top[i];
646 top[i]= buffer[mb_x][i];
648 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
650 left[i]=
651 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
654 if (s->restart_interval && !--s->restart_count) {
655 align_get_bits(&s->gb);
656 skip_bits(&s->gb, 16); /* skip RSTn */
660 if(s->rct){
661 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
662 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
663 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
664 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
666 }else if(s->pegasus_rct){
667 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
668 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
669 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
670 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
672 }else{
673 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
674 ptr[4*mb_x+0] = buffer[mb_x][0];
675 ptr[4*mb_x+1] = buffer[mb_x][1];
676 ptr[4*mb_x+2] = buffer[mb_x][2];
680 return 0;
683 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
684 int i, mb_x, mb_y;
685 const int nb_components=3;
687 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
688 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
689 if (s->restart_interval && !s->restart_count)
690 s->restart_count = s->restart_interval;
692 if(mb_x==0 || mb_y==0 || s->interlaced){
693 for(i=0;i<nb_components;i++) {
694 uint8_t *ptr;
695 int n, h, v, x, y, c, j, linesize;
696 n = s->nb_blocks[i];
697 c = s->comp_index[i];
698 h = s->h_scount[i];
699 v = s->v_scount[i];
700 x = 0;
701 y = 0;
702 linesize= s->linesize[c];
704 for(j=0; j<n; j++) {
705 int pred;
707 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
708 if(y==0 && mb_y==0){
709 if(x==0 && mb_x==0){
710 pred= 128 << point_transform;
711 }else{
712 pred= ptr[-1];
714 }else{
715 if(x==0 && mb_x==0){
716 pred= ptr[-linesize];
717 }else{
718 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
722 if (s->interlaced && s->bottom_field)
723 ptr += linesize >> 1;
724 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
726 if (++x == h) {
727 x = 0;
728 y++;
732 }else{
733 for(i=0;i<nb_components;i++) {
734 uint8_t *ptr;
735 int n, h, v, x, y, c, j, linesize;
736 n = s->nb_blocks[i];
737 c = s->comp_index[i];
738 h = s->h_scount[i];
739 v = s->v_scount[i];
740 x = 0;
741 y = 0;
742 linesize= s->linesize[c];
744 for(j=0; j<n; j++) {
745 int pred;
747 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
748 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
749 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
750 if (++x == h) {
751 x = 0;
752 y++;
757 if (s->restart_interval && !--s->restart_count) {
758 align_get_bits(&s->gb);
759 skip_bits(&s->gb, 16); /* skip RSTn */
763 return 0;
766 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al){
767 int i, mb_x, mb_y;
768 uint8_t* data[MAX_COMPONENTS];
769 int linesize[MAX_COMPONENTS];
771 for(i=0; i < nb_components; i++) {
772 int c = s->comp_index[i];
773 data[c] = s->picture.data[c];
774 linesize[c]=s->linesize[c];
775 s->coefs_finished[c] |= 1;
776 if(s->avctx->codec->id==CODEC_ID_AMV) {
777 //picture should be flipped upside-down for this codec
778 assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));
779 data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));
780 linesize[c] *= -1;
784 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
785 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
786 if (s->restart_interval && !s->restart_count)
787 s->restart_count = s->restart_interval;
789 for(i=0;i<nb_components;i++) {
790 uint8_t *ptr;
791 int n, h, v, x, y, c, j;
792 n = s->nb_blocks[i];
793 c = s->comp_index[i];
794 h = s->h_scount[i];
795 v = s->v_scount[i];
796 x = 0;
797 y = 0;
798 for(j=0;j<n;j++) {
799 ptr = data[c] +
800 (((linesize[c] * (v * mb_y + y) * 8) +
801 (h * mb_x + x) * 8) >> s->avctx->lowres);
802 if(s->interlaced && s->bottom_field)
803 ptr += linesize[c] >> 1;
804 if(!s->progressive) {
805 s->dsp.clear_block(s->block);
806 if(decode_block(s, s->block, i,
807 s->dc_index[i], s->ac_index[i],
808 s->quant_matrixes[ s->quant_index[c] ]) < 0) {
809 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
810 return -1;
812 s->dsp.idct_put(ptr, linesize[c], s->block);
813 } else {
814 int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
815 DCTELEM *block = s->blocks[c][block_idx];
816 if(Ah)
817 block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
818 else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
819 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
820 return -1;
823 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
824 //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);
825 if (++x == h) {
826 x = 0;
827 y++;
832 if (s->restart_interval && !--s->restart_count) {
833 align_get_bits(&s->gb);
834 skip_bits(&s->gb, 16); /* skip RSTn */
835 for (i=0; i<nb_components; i++) /* reset dc */
836 s->last_dc[i] = 1024;
840 return 0;
843 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){
844 int mb_x, mb_y;
845 int EOBRUN = 0;
846 int c = s->comp_index[0];
847 uint8_t* data = s->picture.data[c];
848 int linesize = s->linesize[c];
849 int last_scan = 0;
850 int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ];
852 if(!Al) {
853 s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss);
854 last_scan = !~s->coefs_finished[c];
857 if(s->interlaced && s->bottom_field)
858 data += linesize >> 1;
860 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
861 uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres);
862 int block_idx = mb_y * s->block_stride[c];
863 DCTELEM (*block)[64] = &s->blocks[c][block_idx];
864 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
865 for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
866 int ret;
867 if(Ah)
868 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
869 quant_matrix, ss, se, Al, &EOBRUN);
870 else
871 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
872 quant_matrix, ss, se, Al, &EOBRUN);
873 if(ret < 0) {
874 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
875 return -1;
877 if(last_scan) {
878 s->dsp.idct_put(ptr, linesize, *block);
879 ptr += 8 >> s->avctx->lowres;
883 return 0;
886 int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
888 int len, nb_components, i, h, v, predictor, point_transform;
889 int index, id;
890 const int block_size= s->lossless ? 1 : 8;
891 int ilv, prev_shift;
893 /* XXX: verify len field validity */
894 len = get_bits(&s->gb, 16);
895 nb_components = get_bits(&s->gb, 8);
896 if (len != 6+2*nb_components)
898 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
899 return -1;
901 for(i=0;i<nb_components;i++) {
902 id = get_bits(&s->gb, 8) - 1;
903 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
904 /* find component index */
905 for(index=0;index<s->nb_components;index++)
906 if (id == s->component_id[index])
907 break;
908 if (index == s->nb_components)
910 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
911 return -1;
914 s->comp_index[i] = index;
916 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
917 s->h_scount[i] = s->h_count[index];
918 s->v_scount[i] = s->v_count[index];
920 s->dc_index[i] = get_bits(&s->gb, 4);
921 s->ac_index[i] = get_bits(&s->gb, 4);
923 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
924 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
925 goto out_of_range;
926 if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table)
927 goto out_of_range;
930 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
931 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
932 prev_shift = get_bits(&s->gb, 4); /* Ah */
933 point_transform= get_bits(&s->gb, 4); /* Al */
935 for(i=0;i<nb_components;i++)
936 s->last_dc[i] = 1024;
938 if (nb_components > 1) {
939 /* interleaved stream */
940 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
941 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
942 } else if(!s->ls) { /* skip this for JPEG-LS */
943 h = s->h_max / s->h_scount[0];
944 v = s->v_max / s->v_scount[0];
945 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
946 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
947 s->nb_blocks[0] = 1;
948 s->h_scount[0] = 1;
949 s->v_scount[0] = 1;
952 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
953 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" : "",
954 predictor, point_transform, ilv, s->bits,
955 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
958 /* mjpeg-b can have padding bytes between sos and image data, skip them */
959 for (i = s->mjpb_skiptosod; i > 0; i--)
960 skip_bits(&s->gb, 8);
962 if(s->lossless){
963 if(CONFIG_JPEGLS_DECODER && s->ls){
964 // for(){
965 // reset_ls_coding_parameters(s, 0);
967 if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)
968 return -1;
969 }else{
970 if(s->rgb){
971 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
972 return -1;
973 }else{
974 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
975 return -1;
978 }else{
979 if(s->progressive && predictor) {
980 if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0)
981 return -1;
982 } else {
983 if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform) < 0)
984 return -1;
987 emms_c();
988 return 0;
989 out_of_range:
990 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
991 return -1;
994 static int mjpeg_decode_dri(MJpegDecodeContext *s)
996 if (get_bits(&s->gb, 16) != 4)
997 return -1;
998 s->restart_interval = get_bits(&s->gb, 16);
999 s->restart_count = 0;
1000 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
1002 return 0;
1005 static int mjpeg_decode_app(MJpegDecodeContext *s)
1007 int len, id, i;
1009 len = get_bits(&s->gb, 16);
1010 if (len < 5)
1011 return -1;
1012 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
1013 return -1;
1015 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1016 id = be2me_32(id);
1017 len -= 6;
1019 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1020 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1023 /* buggy AVID, it puts EOI only at every 10th frame */
1024 /* also this fourcc is used by non-avid files too, it holds some
1025 informations, but it's always present in AVID creates files */
1026 if (id == AV_RL32("AVI1"))
1028 /* structure:
1029 4bytes AVI1
1030 1bytes polarity
1031 1bytes always zero
1032 4bytes field_size
1033 4bytes field_size_less_padding
1035 s->buggy_avid = 1;
1036 // if (s->first_picture)
1037 // printf("mjpeg: workarounding buggy AVID\n");
1038 i = get_bits(&s->gb, 8);
1039 if (i==2) s->bottom_field= 1;
1040 else if(i==1) s->bottom_field= 0;
1041 #if 0
1042 skip_bits(&s->gb, 8);
1043 skip_bits(&s->gb, 32);
1044 skip_bits(&s->gb, 32);
1045 len -= 10;
1046 #endif
1047 // if (s->interlace_polarity)
1048 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
1049 goto out;
1052 // len -= 2;
1054 if (id == AV_RL32("JFIF"))
1056 int t_w, t_h, v1, v2;
1057 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1058 v1= get_bits(&s->gb, 8);
1059 v2= get_bits(&s->gb, 8);
1060 skip_bits(&s->gb, 8);
1062 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
1063 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
1065 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1066 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1067 v1, v2,
1068 s->avctx->sample_aspect_ratio.num,
1069 s->avctx->sample_aspect_ratio.den
1072 t_w = get_bits(&s->gb, 8);
1073 t_h = get_bits(&s->gb, 8);
1074 if (t_w && t_h)
1076 /* skip thumbnail */
1077 if (len-10-(t_w*t_h*3) > 0)
1078 len -= t_w*t_h*3;
1080 len -= 10;
1081 goto out;
1084 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e'))
1086 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1087 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1088 skip_bits(&s->gb, 16); /* version */
1089 skip_bits(&s->gb, 16); /* flags0 */
1090 skip_bits(&s->gb, 16); /* flags1 */
1091 skip_bits(&s->gb, 8); /* transform */
1092 len -= 7;
1093 goto out;
1096 if (id == AV_RL32("LJIF")){
1097 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1098 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
1099 skip_bits(&s->gb, 16); /* version ? */
1100 skip_bits(&s->gb, 16); /* unknwon always 0? */
1101 skip_bits(&s->gb, 16); /* unknwon always 0? */
1102 skip_bits(&s->gb, 16); /* unknwon always 0? */
1103 switch( get_bits(&s->gb, 8)){
1104 case 1:
1105 s->rgb= 1;
1106 s->pegasus_rct=0;
1107 break;
1108 case 2:
1109 s->rgb= 1;
1110 s->pegasus_rct=1;
1111 break;
1112 default:
1113 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1115 len -= 9;
1116 goto out;
1119 /* Apple MJPEG-A */
1120 if ((s->start_code == APP1) && (len > (0x28 - 8)))
1122 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1123 id = be2me_32(id);
1124 len -= 4;
1125 if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */
1127 #if 0
1128 skip_bits(&s->gb, 32); /* field size */
1129 skip_bits(&s->gb, 32); /* pad field size */
1130 skip_bits(&s->gb, 32); /* next off */
1131 skip_bits(&s->gb, 32); /* quant off */
1132 skip_bits(&s->gb, 32); /* huff off */
1133 skip_bits(&s->gb, 32); /* image off */
1134 skip_bits(&s->gb, 32); /* scan off */
1135 skip_bits(&s->gb, 32); /* data off */
1136 #endif
1137 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1138 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1142 out:
1143 /* slow but needed for extreme adobe jpegs */
1144 if (len < 0)
1145 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
1146 while(--len > 0)
1147 skip_bits(&s->gb, 8);
1149 return 0;
1152 static int mjpeg_decode_com(MJpegDecodeContext *s)
1154 int len = get_bits(&s->gb, 16);
1155 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
1156 char *cbuf = av_malloc(len - 1);
1157 if (cbuf) {
1158 int i;
1159 for (i = 0; i < len - 2; i++)
1160 cbuf[i] = get_bits(&s->gb, 8);
1161 if (i > 0 && cbuf[i-1] == '\n')
1162 cbuf[i-1] = 0;
1163 else
1164 cbuf[i] = 0;
1166 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1167 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1169 /* buggy avid, it puts EOI only at every 10th frame */
1170 if (!strcmp(cbuf, "AVID"))
1172 s->buggy_avid = 1;
1173 // if (s->first_picture)
1174 // printf("mjpeg: workarounding buggy AVID\n");
1176 else if(!strcmp(cbuf, "CS=ITU601")){
1177 s->cs_itu601= 1;
1180 av_free(cbuf);
1184 return 0;
1187 #if 0
1188 static int valid_marker_list[] =
1190 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
1191 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1192 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1193 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1194 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1195 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1196 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1197 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1198 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1199 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1200 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1201 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1202 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1203 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1204 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1205 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1206 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1208 #endif
1210 /* return the 8 bit start code value and update the search
1211 state. Return -1 if no start code found */
1212 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1214 const uint8_t *buf_ptr;
1215 unsigned int v, v2;
1216 int val;
1217 #ifdef DEBUG
1218 int skipped=0;
1219 #endif
1221 buf_ptr = *pbuf_ptr;
1222 while (buf_ptr < buf_end) {
1223 v = *buf_ptr++;
1224 v2 = *buf_ptr;
1225 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1226 val = *buf_ptr++;
1227 goto found;
1229 #ifdef DEBUG
1230 skipped++;
1231 #endif
1233 val = -1;
1234 found:
1235 dprintf(NULL, "find_marker skipped %d bytes\n", skipped);
1236 *pbuf_ptr = buf_ptr;
1237 return val;
1240 int ff_mjpeg_decode_frame(AVCodecContext *avctx,
1241 void *data, int *data_size,
1242 AVPacket *avpkt)
1244 const uint8_t *buf = avpkt->data;
1245 int buf_size = avpkt->size;
1246 MJpegDecodeContext *s = avctx->priv_data;
1247 const uint8_t *buf_end, *buf_ptr;
1248 int start_code;
1249 AVFrame *picture = data;
1251 s->got_picture = 0; // picture from previous image can not be reused
1252 buf_ptr = buf;
1253 buf_end = buf + buf_size;
1254 while (buf_ptr < buf_end) {
1255 /* find start next marker */
1256 start_code = find_marker(&buf_ptr, buf_end);
1258 /* EOF */
1259 if (start_code < 0) {
1260 goto the_end;
1261 } else {
1262 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr);
1264 if ((buf_end - buf_ptr) > s->buffer_size)
1266 av_free(s->buffer);
1267 s->buffer_size = buf_end-buf_ptr;
1268 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
1269 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
1270 s->buffer_size);
1273 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1274 if (start_code == SOS && !s->ls)
1276 const uint8_t *src = buf_ptr;
1277 uint8_t *dst = s->buffer;
1279 while (src<buf_end)
1281 uint8_t x = *(src++);
1283 *(dst++) = x;
1284 if (avctx->codec_id != CODEC_ID_THP)
1286 if (x == 0xff) {
1287 while (src < buf_end && x == 0xff)
1288 x = *(src++);
1290 if (x >= 0xd0 && x <= 0xd7)
1291 *(dst++) = x;
1292 else if (x)
1293 break;
1297 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
1299 av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1300 (buf_end - buf_ptr) - (dst - s->buffer));
1302 else if(start_code == SOS && s->ls){
1303 const uint8_t *src = buf_ptr;
1304 uint8_t *dst = s->buffer;
1305 int bit_count = 0;
1306 int t = 0, b = 0;
1307 PutBitContext pb;
1309 s->cur_scan++;
1311 /* find marker */
1312 while (src + t < buf_end){
1313 uint8_t x = src[t++];
1314 if (x == 0xff){
1315 while((src + t < buf_end) && x == 0xff)
1316 x = src[t++];
1317 if (x & 0x80) {
1318 t -= 2;
1319 break;
1323 bit_count = t * 8;
1325 init_put_bits(&pb, dst, t);
1327 /* unescape bitstream */
1328 while(b < t){
1329 uint8_t x = src[b++];
1330 put_bits(&pb, 8, x);
1331 if(x == 0xFF){
1332 x = src[b++];
1333 put_bits(&pb, 7, x);
1334 bit_count--;
1337 flush_put_bits(&pb);
1339 init_get_bits(&s->gb, dst, bit_count);
1341 else
1342 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
1344 s->start_code = start_code;
1345 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1346 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1349 /* process markers */
1350 if (start_code >= 0xd0 && start_code <= 0xd7) {
1351 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
1352 /* APP fields */
1353 } else if (start_code >= APP0 && start_code <= APP15) {
1354 mjpeg_decode_app(s);
1355 /* Comment */
1356 } else if (start_code == COM){
1357 mjpeg_decode_com(s);
1360 switch(start_code) {
1361 case SOI:
1362 s->restart_interval = 0;
1364 s->restart_count = 0;
1365 /* nothing to do on SOI */
1366 break;
1367 case DQT:
1368 ff_mjpeg_decode_dqt(s);
1369 break;
1370 case DHT:
1371 if(ff_mjpeg_decode_dht(s) < 0){
1372 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1373 return -1;
1375 break;
1376 case SOF0:
1377 s->lossless=0;
1378 s->ls=0;
1379 s->progressive=0;
1380 if (ff_mjpeg_decode_sof(s) < 0)
1381 return -1;
1382 break;
1383 case SOF2:
1384 s->lossless=0;
1385 s->ls=0;
1386 s->progressive=1;
1387 if (ff_mjpeg_decode_sof(s) < 0)
1388 return -1;
1389 break;
1390 case SOF3:
1391 s->lossless=1;
1392 s->ls=0;
1393 s->progressive=0;
1394 if (ff_mjpeg_decode_sof(s) < 0)
1395 return -1;
1396 break;
1397 case SOF48:
1398 s->lossless=1;
1399 s->ls=1;
1400 s->progressive=0;
1401 if (ff_mjpeg_decode_sof(s) < 0)
1402 return -1;
1403 break;
1404 case LSE:
1405 if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
1406 return -1;
1407 break;
1408 case EOI:
1409 s->cur_scan = 0;
1410 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1411 break;
1412 eoi_parser:
1413 if (!s->got_picture) {
1414 av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n");
1415 break;
1418 if (s->interlaced) {
1419 s->bottom_field ^= 1;
1420 /* if not bottom field, do not output image yet */
1421 if (s->bottom_field == !s->interlace_polarity)
1422 goto not_the_end;
1424 *picture = s->picture;
1425 *data_size = sizeof(AVFrame);
1427 if(!s->lossless){
1428 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
1429 picture->qstride= 0;
1430 picture->qscale_table= s->qscale_table;
1431 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
1432 if(avctx->debug & FF_DEBUG_QP)
1433 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
1434 picture->quality*= FF_QP2LAMBDA;
1437 goto the_end;
1439 break;
1440 case SOS:
1441 if (!s->got_picture) {
1442 av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n");
1443 break;
1445 ff_mjpeg_decode_sos(s);
1446 /* buggy avid puts EOI every 10-20th frame */
1447 /* if restart period is over process EOI */
1448 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1449 goto eoi_parser;
1450 break;
1451 case DRI:
1452 mjpeg_decode_dri(s);
1453 break;
1454 case SOF1:
1455 case SOF5:
1456 case SOF6:
1457 case SOF7:
1458 case SOF9:
1459 case SOF10:
1460 case SOF11:
1461 case SOF13:
1462 case SOF14:
1463 case SOF15:
1464 case JPG:
1465 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
1466 break;
1467 // default:
1468 // printf("mjpeg: unsupported marker (%x)\n", start_code);
1469 // break;
1472 not_the_end:
1473 /* eof process start code */
1474 buf_ptr += (get_bits_count(&s->gb)+7)/8;
1475 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
1476 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
1480 if (s->got_picture) {
1481 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1482 goto eoi_parser;
1484 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1485 return -1;
1486 the_end:
1487 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr);
1488 // return buf_end - buf_ptr;
1489 return buf_ptr - buf;
1492 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
1494 MJpegDecodeContext *s = avctx->priv_data;
1495 int i, j;
1497 av_free(s->buffer);
1498 av_free(s->qscale_table);
1500 for(i=0;i<2;i++) {
1501 for(j=0;j<4;j++)
1502 free_vlc(&s->vlcs[i][j]);
1504 for(i=0; i<MAX_COMPONENTS; i++) {
1505 av_freep(&s->blocks[i]);
1506 av_freep(&s->last_nnz[i]);
1508 return 0;
1511 AVCodec mjpeg_decoder = {
1512 "mjpeg",
1513 CODEC_TYPE_VIDEO,
1514 CODEC_ID_MJPEG,
1515 sizeof(MJpegDecodeContext),
1516 ff_mjpeg_decode_init,
1517 NULL,
1518 ff_mjpeg_decode_end,
1519 ff_mjpeg_decode_frame,
1520 CODEC_CAP_DR1,
1521 NULL,
1522 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1525 AVCodec thp_decoder = {
1526 "thp",
1527 CODEC_TYPE_VIDEO,
1528 CODEC_ID_THP,
1529 sizeof(MJpegDecodeContext),
1530 ff_mjpeg_decode_init,
1531 NULL,
1532 ff_mjpeg_decode_end,
1533 ff_mjpeg_decode_frame,
1534 CODEC_CAP_DR1,
1535 NULL,
1536 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),