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
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
36 #include "libavutil/imgutils.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/opt.h"
43 #include "jpeglsdec.h"
46 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
47 int nb_codes, int use_static, int is_ac)
49 uint8_t huff_size[256];
50 uint16_t huff_code[256];
51 uint16_t huff_sym[256];
54 assert(nb_codes <= 256);
56 memset(huff_size, 0, sizeof(huff_size));
57 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
60 huff_sym[i]= i + 16*is_ac;
62 if(is_ac) huff_sym[0]= 16*256;
64 return init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, huff_sym, 2, 2, use_static);
67 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) {
68 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance,
69 ff_mjpeg_val_dc, 12, 0, 0);
70 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance,
71 ff_mjpeg_val_dc, 12, 0, 0);
72 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance,
73 ff_mjpeg_val_ac_luminance, 251, 0, 1);
74 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance,
75 ff_mjpeg_val_ac_chrominance, 251, 0, 1);
76 build_vlc(&s->vlcs[2][0], ff_mjpeg_bits_ac_luminance,
77 ff_mjpeg_val_ac_luminance, 251, 0, 0);
78 build_vlc(&s->vlcs[2][1], ff_mjpeg_bits_ac_chrominance,
79 ff_mjpeg_val_ac_chrominance, 251, 0, 0);
82 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
84 MJpegDecodeContext *s = avctx->priv_data;
87 s->picture_ptr = &s->picture;
88 avcodec_get_frame_defaults(&s->picture);
91 dsputil_init(&s->dsp, avctx);
92 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
97 s->org_height = avctx->coded_height;
98 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
100 build_basic_mjpeg_vlc(s);
102 #if FF_API_MJPEG_GLOBAL_OPTS
103 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
108 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
109 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
110 if (ff_mjpeg_decode_dht(s)) {
111 av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n");
112 build_basic_mjpeg_vlc(s);
115 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
116 s->interlace_polarity = 1; /* bottom field first */
117 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
119 if (avctx->codec->id == CODEC_ID_AMV)
126 /* quantize tables */
127 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
129 int len, index, i, j;
131 len = get_bits(&s->gb, 16) - 2;
134 /* only 8 bit precision handled */
135 if (get_bits(&s->gb, 4) != 0)
137 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
140 index = get_bits(&s->gb, 4);
143 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
144 /* read quant table */
146 j = s->scantable.permutated[i];
147 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
150 //XXX FIXME finetune, and perhaps add dc too
151 s->qscale[index]= FFMAX(
152 s->quant_matrixes[index][s->scantable.permutated[1]],
153 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
154 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
161 /* decode huffman tables and build VLC decoders */
162 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
164 int len, index, i, class, n, v, code_max;
165 uint8_t bits_table[17];
166 uint8_t val_table[256];
168 len = get_bits(&s->gb, 16) - 2;
173 class = get_bits(&s->gb, 4);
176 index = get_bits(&s->gb, 4);
181 bits_table[i] = get_bits(&s->gb, 8);
185 if (len < n || n > 256)
190 v = get_bits(&s->gb, 8);
197 /* build VLC and flush previous vlc if present */
198 free_vlc(&s->vlcs[class][index]);
199 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
200 class, index, code_max + 1);
201 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
206 free_vlc(&s->vlcs[2][index]);
207 if(build_vlc(&s->vlcs[2][index], bits_table, val_table, code_max + 1, 0, 0) < 0){
215 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
217 int len, nb_components, i, width, height, pix_fmt_id;
221 /* XXX: verify len field validity */
222 len = get_bits(&s->gb, 16);
223 s->bits= get_bits(&s->gb, 8);
225 if(s->pegasus_rct) s->bits=9;
226 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
228 if (s->bits != 8 && !s->lossless){
229 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
233 if(s->lossless && s->avctx->lowres){
234 av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
238 height = get_bits(&s->gb, 16);
239 width = get_bits(&s->gb, 16);
241 //HACK for odd_height.mov
242 if(s->interlaced && s->width == width && s->height == height + 1)
245 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
246 if(av_image_check_size(width, height, 0, s->avctx))
249 nb_components = get_bits(&s->gb, 8);
250 if (nb_components <= 0 ||
251 nb_components > MAX_COMPONENTS)
253 if (s->ls && !(s->bits <= 8 || nb_components == 1)){
254 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
257 s->nb_components = nb_components;
260 for(i=0;i<nb_components;i++) {
262 s->component_id[i] = get_bits(&s->gb, 8) - 1;
263 s->h_count[i] = get_bits(&s->gb, 4);
264 s->v_count[i] = get_bits(&s->gb, 4);
265 /* compute hmax and vmax (only used in interleaved case) */
266 if (s->h_count[i] > s->h_max)
267 s->h_max = s->h_count[i];
268 if (s->v_count[i] > s->v_max)
269 s->v_max = s->v_count[i];
270 s->quant_index[i] = get_bits(&s->gb, 8);
271 if (s->quant_index[i] >= 4)
273 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
274 s->v_count[i], s->component_id[i], s->quant_index[i]);
277 if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
278 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
282 if(s->v_max==1 && s->h_max==1 && s->lossless==1 && nb_components==3) s->rgb=1;
284 /* if different size, realloc/alloc picture */
285 /* XXX: also check h_count and v_count */
286 if (width != s->width || height != s->height) {
287 av_freep(&s->qscale_table);
293 /* test interlaced mode */
294 if (s->first_picture &&
295 s->org_height != 0 &&
296 s->height < ((s->org_height * 3) / 4)) {
298 s->bottom_field = s->interlace_polarity;
299 s->picture_ptr->interlaced_frame = 1;
300 s->picture_ptr->top_field_first = !s->interlace_polarity;
304 avcodec_set_dimensions(s->avctx, width, height);
306 s->qscale_table= av_mallocz((s->width+15)/16);
308 s->first_picture = 0;
311 if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
314 /* XXX: not complete test ! */
315 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
316 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
317 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
318 (s->h_count[3] << 4) | s->v_count[3];
319 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
320 //NOTE we do not allocate pictures large enough for the possible padding of h/v_count being 4
321 if(!(pix_fmt_id & 0xD0D0D0D0))
322 pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1;
323 if(!(pix_fmt_id & 0x0D0D0D0D))
324 pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1;
329 s->avctx->pix_fmt = PIX_FMT_BGR24;
331 if(s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A'){
332 s->avctx->pix_fmt = PIX_FMT_GBR24P;
334 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
335 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
338 assert(s->nb_components==3);
341 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
342 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
347 s->avctx->pix_fmt = PIX_FMT_GRAY8;
349 s->avctx->pix_fmt = PIX_FMT_GRAY16;
353 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P;
354 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
355 s->yuv421 = pix_fmt_id == 0x22211100;
358 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
359 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
362 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
363 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
366 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
370 if(s->nb_components > 1)
371 s->avctx->pix_fmt = PIX_FMT_RGB24;
372 else if(s->bits <= 8)
373 s->avctx->pix_fmt = PIX_FMT_GRAY8;
375 s->avctx->pix_fmt = PIX_FMT_GRAY16;
378 if(s->picture_ptr->data[0])
379 s->avctx->release_buffer(s->avctx, s->picture_ptr);
381 if(s->avctx->get_buffer(s->avctx, s->picture_ptr) < 0){
382 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
385 s->picture_ptr->pict_type= AV_PICTURE_TYPE_I;
386 s->picture_ptr->key_frame= 1;
390 s->linesize[i]= s->picture_ptr->linesize[i] << s->interlaced;
393 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
395 if (len != (8+(3*nb_components)))
397 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
400 /* totally blank picture as progressive JPEG will only add details to it */
402 int bw = (width + s->h_max*8-1) / (s->h_max*8);
403 int bh = (height + s->v_max*8-1) / (s->v_max*8);
404 for(i=0; i<s->nb_components; i++) {
405 int size = bw * bh * s->h_count[i] * s->v_count[i];
406 av_freep(&s->blocks[i]);
407 av_freep(&s->last_nnz[i]);
408 s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
409 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
410 s->block_stride[i] = bw * s->h_count[i];
412 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
417 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
420 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
423 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
424 &s->vlcs[0][dc_index]);
429 return get_xbits(&s->gb, code);
434 /* decode block and dequantize */
435 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
436 int component, int dc_index, int ac_index, int16_t *quant_matrix)
438 int code, i, j, level, val;
441 val = mjpeg_decode_dc(s, dc_index);
443 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
446 val = val * quant_matrix[0] + s->last_dc[component];
447 s->last_dc[component] = val;
451 {OPEN_READER(re, &s->gb);
453 UPDATE_CACHE(re, &s->gb);
454 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
456 i += ((unsigned)code) >> 4;
459 if(code > MIN_CACHE_BITS - 16){
460 UPDATE_CACHE(re, &s->gb);
463 int cache=GET_CACHE(re,&s->gb);
464 int sign=(~cache)>>31;
465 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
468 LAST_SKIP_BITS(re, &s->gb, code);
471 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
474 j = s->scantable.permutated[i];
475 block[j] = level * quant_matrix[j];
478 CLOSE_READER(re, &s->gb);}
483 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component,
484 int dc_index, int16_t *quant_matrix, int Al)
487 s->dsp.clear_block(block);
488 val = mjpeg_decode_dc(s, dc_index);
490 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
493 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
494 s->last_dc[component] = val;
499 /* decode block and dequantize - progressive JPEG version */
500 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
501 int ac_index, int16_t *quant_matrix,
502 int ss, int se, int Al, int *EOBRUN)
504 int code, i, j, level, val, run;
510 {OPEN_READER(re, &s->gb);
512 UPDATE_CACHE(re, &s->gb);
513 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
515 run = ((unsigned) code) >> 4;
519 if(code > MIN_CACHE_BITS - 16){
520 UPDATE_CACHE(re, &s->gb);
523 int cache=GET_CACHE(re,&s->gb);
524 int sign=(~cache)>>31;
525 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
528 LAST_SKIP_BITS(re, &s->gb, code);
532 j = s->scantable.permutated[se];
533 block[j] = level * quant_matrix[j] << Al;
536 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
539 j = s->scantable.permutated[i];
540 block[j] = level * quant_matrix[j] << Al;
542 if(run == 0xF){// ZRL - skip 15 coefficients
545 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
551 UPDATE_CACHE(re, &s->gb);
552 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
553 LAST_SKIP_BITS(re, &s->gb, run);
560 CLOSE_READER(re, &s->gb);}
566 #define REFINE_BIT(j) {\
567 UPDATE_CACHE(re, &s->gb);\
568 sign = block[j]>>15;\
569 block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\
570 LAST_SKIP_BITS(re, &s->gb, 1);\
578 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\
583 j = s->scantable.permutated[i];\
590 /* decode block and dequantize - progressive JPEG refinement pass */
591 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
592 int ac_index, int16_t *quant_matrix,
593 int ss, int se, int Al, int *EOBRUN)
595 int code, i=ss, j, sign, val, run;
596 int last = FFMIN(se, *last_nnz);
598 OPEN_READER(re, &s->gb);
603 UPDATE_CACHE(re, &s->gb);
604 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
607 run = ((unsigned) code) >> 4;
608 UPDATE_CACHE(re, &s->gb);
609 val = SHOW_UBITS(re, &s->gb, 1);
610 LAST_SKIP_BITS(re, &s->gb, 1);
612 j = s->scantable.permutated[i];
614 block[j] = ((quant_matrix[j]^val)-val) << Al;
618 CLOSE_READER(re, &s->gb);
622 run = ((unsigned) code) >> 4;
629 UPDATE_CACHE(re, &s->gb);
630 run += SHOW_UBITS(re, &s->gb, val);
631 LAST_SKIP_BITS(re, &s->gb, val);
644 j = s->scantable.permutated[i];
648 CLOSE_READER(re, &s->gb);
655 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform){
657 uint16_t (*buffer)[4];
658 int left[3], top[3], topleft[3];
659 const int linesize= s->linesize[0];
660 const int mask= (1<<s->bits)-1;
662 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
663 buffer= s->ljpeg_buffer;
666 buffer[0][i]= 1 << (s->bits - 1);
668 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
669 const int modified_predictor= mb_y ? predictor : 1;
670 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
672 if (s->interlaced && s->bottom_field)
673 ptr += linesize >> 1;
676 top[i]= left[i]= topleft[i]= buffer[0][i];
678 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
679 if (s->restart_interval && !s->restart_count)
680 s->restart_count = s->restart_interval;
682 for(i=0;i<nb_components;i++) {
686 top[i]= buffer[mb_x][i];
688 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
690 dc = mjpeg_decode_dc(s, s->dc_index[i]);
695 buffer[mb_x][i]= mask & (pred + (dc << point_transform));
698 if (s->restart_interval && !--s->restart_count) {
699 align_get_bits(&s->gb);
700 skip_bits(&s->gb, 16); /* skip RSTn */
705 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
706 ptr[3*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
707 ptr[3*mb_x+0] = buffer[mb_x][1] + ptr[3*mb_x+1];
708 ptr[3*mb_x+2] = buffer[mb_x][2] + ptr[3*mb_x+1];
710 }else if(s->pegasus_rct){
711 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
712 ptr[3*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
713 ptr[3*mb_x+0] = buffer[mb_x][1] + ptr[3*mb_x+1];
714 ptr[3*mb_x+2] = buffer[mb_x][2] + ptr[3*mb_x+1];
717 for(i=0;i<nb_components;i++) {
718 int c= s->comp_index[i];
719 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
720 ptr[3*mb_x+2-c] = buffer[mb_x][i];
728 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
730 const int nb_components=s->nb_components;
731 int bits= (s->bits+7)&~7;
733 point_transform += bits - s->bits;
735 av_assert0(nb_components==1 || nb_components==3);
737 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
738 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
739 if (s->restart_interval && !s->restart_count)
740 s->restart_count = s->restart_interval;
742 if(mb_x==0 || mb_y==0 || s->interlaced){
743 for(i=0;i<nb_components;i++) {
746 int n, h, v, x, y, c, j, linesize;
748 c = s->comp_index[i];
753 linesize= s->linesize[c];
755 if(bits>8) linesize /= 2;
760 dc = mjpeg_decode_dc(s, s->dc_index[i]);
764 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
767 pred= 1 << (bits - 1);
773 pred= ptr[-linesize];
775 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
779 if (s->interlaced && s->bottom_field)
780 ptr += linesize >> 1;
781 *ptr= pred + (dc << point_transform);
783 ptr16 = s->picture.data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x); //FIXME optimize this crap
786 pred= 1 << (bits - 1);
792 pred= ptr16[-linesize];
794 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
798 if (s->interlaced && s->bottom_field)
799 ptr16 += linesize >> 1;
800 *ptr16= pred + (dc << point_transform);
809 for(i=0;i<nb_components;i++) {
812 int n, h, v, x, y, c, j, linesize, dc;
814 c = s->comp_index[i];
819 linesize= s->linesize[c];
821 if(bits>8) linesize /= 2;
826 dc = mjpeg_decode_dc(s, s->dc_index[i]);
830 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
831 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
833 *ptr= pred + (dc << point_transform);
835 ptr16 = s->picture.data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x); //FIXME optimize this crap
836 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
838 *ptr16= pred + (dc << point_transform);
847 if (s->restart_interval && !--s->restart_count) {
848 align_get_bits(&s->gb);
849 skip_bits(&s->gb, 16); /* skip RSTn */
856 static av_always_inline void mjpeg_copy_block(uint8_t *dst, const uint8_t *src,
857 int linesize, int lowres)
860 case 0: copy_block8(dst, src, linesize, linesize, 8);
862 case 1: copy_block4(dst, src, linesize, linesize, 4);
864 case 2: copy_block2(dst, src, linesize, linesize, 2);
871 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al,
872 const uint8_t *mb_bitmask, const AVFrame *reference){
874 uint8_t* data[MAX_COMPONENTS];
875 const uint8_t *reference_data[MAX_COMPONENTS];
876 int linesize[MAX_COMPONENTS];
877 GetBitContext mb_bitmask_gb;
880 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width*s->mb_height);
883 if(s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {
884 av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n");
887 for(i=0; i < nb_components; i++) {
888 int c = s->comp_index[i];
889 data[c] = s->picture_ptr->data[c];
890 reference_data[c] = reference ? reference->data[c] : NULL;
891 linesize[c]=s->linesize[c];
892 s->coefs_finished[c] |= 1;
894 //picture should be flipped upside-down for this codec
895 int offset = (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));
897 reference_data[c] += offset;
902 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
903 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
904 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
906 if (s->restart_interval && !s->restart_count)
907 s->restart_count = s->restart_interval;
909 if(get_bits_count(&s->gb)>s->gb.size_in_bits){
910 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", get_bits_count(&s->gb) - s->gb.size_in_bits);
913 for(i=0;i<nb_components;i++) {
915 int n, h, v, x, y, c, j;
918 c = s->comp_index[i];
924 block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
925 (h * mb_x + x) * 8) >> s->avctx->lowres);
927 if(s->interlaced && s->bottom_field)
928 block_offset += linesize[c] >> 1;
929 ptr = data[c] + block_offset;
930 if(!s->progressive) {
932 mjpeg_copy_block(ptr, reference_data[c] + block_offset, linesize[c], s->avctx->lowres);
934 s->dsp.clear_block(s->block);
935 if(decode_block(s, s->block, i,
936 s->dc_index[i], s->ac_index[i],
937 s->quant_matrixes[ s->quant_index[c] ]) < 0) {
938 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
941 s->dsp.idct_put(ptr, linesize[c], s->block);
944 int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
945 DCTELEM *block = s->blocks[c][block_idx];
947 block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
948 else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
949 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
953 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
954 //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);
962 if (s->restart_interval) --s->restart_count;
963 i= 8+((-get_bits_count(&s->gb))&7);
964 if (s->restart_interval && show_bits(&s->gb, i) == (1<<i)-1){ /* skip RSTn */
965 int pos= get_bits_count(&s->gb);
966 align_get_bits(&s->gb);
967 while(get_bits_count(&s->gb) < s->gb.size_in_bits && show_bits(&s->gb, 8) == 0xFF)
968 skip_bits(&s->gb, 8);
969 if(get_bits_count(&s->gb) < s->gb.size_in_bits && (get_bits(&s->gb, 8)&0xF8) == 0xD0){
970 for (i=0; i<nb_components; i++) /* reset dc */
971 s->last_dc[i] = 1024;
973 skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
981 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){
984 int c = s->comp_index[0];
985 uint8_t* data = s->picture.data[c];
986 int linesize = s->linesize[c];
988 int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ];
991 s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss);
992 last_scan = !~s->coefs_finished[c];
995 if(s->interlaced && s->bottom_field)
996 data += linesize >> 1;
998 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
999 uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres);
1000 int block_idx = mb_y * s->block_stride[c];
1001 DCTELEM (*block)[64] = &s->blocks[c][block_idx];
1002 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1003 for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1006 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1007 quant_matrix, ss, se, Al, &EOBRUN);
1009 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1010 quant_matrix, ss, se, Al, &EOBRUN);
1012 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
1016 s->dsp.idct_put(ptr, linesize, *block);
1017 ptr += 8 >> s->avctx->lowres;
1024 int ff_mjpeg_decode_sos(MJpegDecodeContext *s,
1025 const uint8_t *mb_bitmask, const AVFrame *reference)
1027 int len, nb_components, i, h, v, predictor, point_transform;
1029 const int block_size= s->lossless ? 1 : 8;
1030 int ilv, prev_shift;
1032 /* XXX: verify len field validity */
1033 len = get_bits(&s->gb, 16);
1034 nb_components = get_bits(&s->gb, 8);
1035 if (nb_components == 0 || nb_components > MAX_COMPONENTS){
1036 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\n", nb_components);
1039 if (len != 6+2*nb_components)
1041 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1044 for(i=0;i<nb_components;i++) {
1045 id = get_bits(&s->gb, 8) - 1;
1046 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1047 /* find component index */
1048 for(index=0;index<s->nb_components;index++)
1049 if (id == s->component_id[index])
1051 if (index == s->nb_components)
1053 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
1056 /* Metasoft MJPEG codec has Cb and Cr swapped */
1057 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1058 && nb_components == 3 && s->nb_components == 3 && i)
1061 if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == PIX_FMT_GBR24P)
1064 s->comp_index[i] = index;
1066 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1067 s->h_scount[i] = s->h_count[index];
1068 s->v_scount[i] = s->v_count[index];
1070 s->dc_index[i] = get_bits(&s->gb, 4);
1071 s->ac_index[i] = get_bits(&s->gb, 4);
1073 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1074 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1076 if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table)
1080 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1081 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1082 if(s->avctx->codec_tag != AV_RL32("CJPG")){
1083 prev_shift = get_bits(&s->gb, 4); /* Ah */
1084 point_transform= get_bits(&s->gb, 4); /* Al */
1086 prev_shift= point_transform= 0;
1088 for(i=0;i<nb_components;i++)
1089 s->last_dc[i] = 1024;
1091 if (nb_components > 1) {
1092 /* interleaved stream */
1093 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1094 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1095 } else if(!s->ls) { /* skip this for JPEG-LS */
1096 h = s->h_max / s->h_scount[0];
1097 v = s->v_max / s->v_scount[0];
1098 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1099 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1100 s->nb_blocks[0] = 1;
1105 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1106 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1107 predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1108 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1111 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1112 for (i = s->mjpb_skiptosod; i > 0; i--)
1113 skip_bits(&s->gb, 8);
1116 av_assert0(s->picture_ptr == &s->picture);
1117 if(CONFIG_JPEGLS_DECODER && s->ls){
1119 // reset_ls_coding_parameters(s, 0);
1121 if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)
1125 if(ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform) < 0)
1128 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
1133 if(s->progressive && predictor) {
1134 av_assert0(s->picture_ptr == &s->picture);
1135 if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0)
1138 if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform,
1139 mb_bitmask, reference) < 0)
1144 uint8_t *line = s->picture_ptr->data[2];
1145 for (i = 0; i < s->height / 2; i++) {
1146 for (index = s->width - 1; index; index--)
1147 line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
1148 line += s->linesize[2];
1150 } else if (s->yuv442) {
1151 uint8_t *dst = &((uint8_t *)s->picture_ptr->data[2])[(s->height - 1) * s->linesize[2]];
1152 for (i = s->height - 1; i; i--) {
1153 uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[2])[i / 2 * s->linesize[2]];
1154 uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[2])[(i + 1) / 2 * s->linesize[2]];
1156 memcpy(dst, src1, s->width);
1158 for (index = 0; index < s->width; index++)
1159 dst[index] = (src1[index] + src2[index]) >> 1;
1161 dst -= s->linesize[2];
1167 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1171 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1173 if (get_bits(&s->gb, 16) != 4)
1175 s->restart_interval = get_bits(&s->gb, 16);
1176 s->restart_count = 0;
1177 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
1182 static int mjpeg_decode_app(MJpegDecodeContext *s)
1186 len = get_bits(&s->gb, 16);
1189 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
1192 id = get_bits_long(&s->gb, 32);
1193 id = av_be2ne32(id);
1196 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1197 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1200 /* Buggy AVID, it puts EOI only at every 10th frame. */
1201 /* Also, this fourcc is used by non-avid files too, it holds some
1202 information, but it's always present in AVID-created files. */
1203 if (id == AV_RL32("AVI1"))
1210 4bytes field_size_less_padding
1213 // if (s->first_picture)
1214 // printf("mjpeg: workarounding buggy AVID\n");
1215 i = get_bits(&s->gb, 8); len--;
1216 av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1218 skip_bits(&s->gb, 8);
1219 skip_bits(&s->gb, 32);
1220 skip_bits(&s->gb, 32);
1223 // if (s->interlace_polarity)
1224 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
1230 if (id == AV_RL32("JFIF"))
1232 int t_w, t_h, v1, v2;
1233 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1234 v1= get_bits(&s->gb, 8);
1235 v2= get_bits(&s->gb, 8);
1236 skip_bits(&s->gb, 8);
1238 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
1239 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
1241 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1242 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1244 s->avctx->sample_aspect_ratio.num,
1245 s->avctx->sample_aspect_ratio.den
1248 t_w = get_bits(&s->gb, 8);
1249 t_h = get_bits(&s->gb, 8);
1252 /* skip thumbnail */
1253 if (len-10-(t_w*t_h*3) > 0)
1260 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e'))
1262 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1263 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1264 skip_bits(&s->gb, 16); /* version */
1265 skip_bits(&s->gb, 16); /* flags0 */
1266 skip_bits(&s->gb, 16); /* flags1 */
1267 skip_bits(&s->gb, 8); /* transform */
1272 if (id == AV_RL32("LJIF")){
1273 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1274 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
1275 skip_bits(&s->gb, 16); /* version ? */
1276 skip_bits(&s->gb, 16); /* unknwon always 0? */
1277 skip_bits(&s->gb, 16); /* unknwon always 0? */
1278 skip_bits(&s->gb, 16); /* unknwon always 0? */
1279 switch( get_bits(&s->gb, 8)){
1289 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1296 if ((s->start_code == APP1) && (len > (0x28 - 8)))
1298 id = get_bits_long(&s->gb, 32);
1299 id = av_be2ne32(id);
1301 if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */
1304 skip_bits(&s->gb, 32); /* field size */
1305 skip_bits(&s->gb, 32); /* pad field size */
1306 skip_bits(&s->gb, 32); /* next off */
1307 skip_bits(&s->gb, 32); /* quant off */
1308 skip_bits(&s->gb, 32); /* huff off */
1309 skip_bits(&s->gb, 32); /* image off */
1310 skip_bits(&s->gb, 32); /* scan off */
1311 skip_bits(&s->gb, 32); /* data off */
1313 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1314 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1319 /* slow but needed for extreme adobe jpegs */
1321 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
1323 skip_bits(&s->gb, 8);
1328 static int mjpeg_decode_com(MJpegDecodeContext *s)
1330 int len = get_bits(&s->gb, 16);
1331 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
1332 char *cbuf = av_malloc(len - 1);
1335 for (i = 0; i < len - 2; i++)
1336 cbuf[i] = get_bits(&s->gb, 8);
1337 if (i > 0 && cbuf[i-1] == '\n')
1342 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1343 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1345 /* buggy avid, it puts EOI only at every 10th frame */
1346 if (!strcmp(cbuf, "AVID"))
1349 // if (s->first_picture)
1350 // printf("mjpeg: workarounding buggy AVID\n");
1352 else if(!strcmp(cbuf, "CS=ITU601")){
1355 else if((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1356 (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20))){
1367 /* return the 8 bit start code value and update the search
1368 state. Return -1 if no start code found */
1369 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1371 const uint8_t *buf_ptr;
1376 buf_ptr = *pbuf_ptr;
1377 while (buf_ptr < buf_end) {
1380 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1388 av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1389 *pbuf_ptr = buf_ptr;
1393 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1394 const uint8_t **buf_ptr, const uint8_t *buf_end,
1395 const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size)
1398 start_code = find_marker(buf_ptr, buf_end);
1400 if ((buf_end - *buf_ptr) > s->buffer_size)
1403 s->buffer_size = buf_end - *buf_ptr;
1404 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
1405 av_log(s->avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
1409 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1410 if (start_code == SOS && !s->ls)
1412 const uint8_t *src = *buf_ptr;
1413 uint8_t *dst = s->buffer;
1417 uint8_t x = *(src++);
1420 if (s->avctx->codec_id != CODEC_ID_THP)
1423 while (src < buf_end && x == 0xff)
1426 if (x >= 0xd0 && x <= 0xd7)
1433 *unescaped_buf_ptr = s->buffer;
1434 *unescaped_buf_size = dst - s->buffer;
1436 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1437 (buf_end - *buf_ptr) - (dst - s->buffer));
1439 else if(start_code == SOS && s->ls){
1440 const uint8_t *src = *buf_ptr;
1441 uint8_t *dst = s->buffer;
1449 while (src + t < buf_end){
1450 uint8_t x = src[t++];
1452 while((src + t < buf_end) && x == 0xff)
1462 init_put_bits(&pb, dst, t);
1464 /* unescape bitstream */
1466 uint8_t x = src[b++];
1467 put_bits(&pb, 8, x);
1470 put_bits(&pb, 7, x);
1474 flush_put_bits(&pb);
1476 *unescaped_buf_ptr = dst;
1477 *unescaped_buf_size = (bit_count + 7) >> 3;
1481 *unescaped_buf_ptr = *buf_ptr;
1482 *unescaped_buf_size = buf_end - *buf_ptr;
1488 int ff_mjpeg_decode_frame(AVCodecContext *avctx,
1489 void *data, int *data_size,
1492 const uint8_t *buf = avpkt->data;
1493 int buf_size = avpkt->size;
1494 MJpegDecodeContext *s = avctx->priv_data;
1495 const uint8_t *buf_end, *buf_ptr;
1496 const uint8_t *unescaped_buf_ptr;
1497 int unescaped_buf_size;
1499 AVFrame *picture = data;
1501 s->got_picture = 0; // picture from previous image can not be reused
1503 buf_end = buf + buf_size;
1504 while (buf_ptr < buf_end) {
1505 /* find start next marker */
1506 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1507 &unescaped_buf_ptr, &unescaped_buf_size);
1510 if (start_code < 0) {
1513 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr);
1515 init_get_bits(&s->gb, unescaped_buf_ptr, unescaped_buf_size*8);
1517 s->start_code = start_code;
1518 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1519 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1522 /* process markers */
1523 if (start_code >= 0xd0 && start_code <= 0xd7) {
1524 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
1526 } else if (start_code >= APP0 && start_code <= APP15) {
1527 mjpeg_decode_app(s);
1529 } else if (start_code == COM){
1530 mjpeg_decode_com(s);
1533 switch(start_code) {
1535 s->restart_interval = 0;
1537 s->restart_count = 0;
1538 /* nothing to do on SOI */
1541 ff_mjpeg_decode_dqt(s);
1544 if(ff_mjpeg_decode_dht(s) < 0){
1545 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1554 if (ff_mjpeg_decode_sof(s) < 0)
1561 if (ff_mjpeg_decode_sof(s) < 0)
1568 if (ff_mjpeg_decode_sof(s) < 0)
1575 if (ff_mjpeg_decode_sof(s) < 0)
1579 if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
1585 if (!s->got_picture) {
1586 av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n");
1589 if (s->interlaced) {
1590 s->bottom_field ^= 1;
1591 /* if not bottom field, do not output image yet */
1592 if (s->bottom_field == !s->interlace_polarity)
1595 *picture = *s->picture_ptr;
1596 *data_size = sizeof(AVFrame);
1599 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
1600 picture->qstride= 0;
1601 picture->qscale_table= s->qscale_table;
1602 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
1603 if(avctx->debug & FF_DEBUG_QP)
1604 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
1605 picture->quality*= FF_QP2LAMBDA;
1610 if (!s->got_picture) {
1611 av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n");
1614 if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&
1615 (avctx->err_recognition & AV_EF_EXPLODE))
1616 return AVERROR_INVALIDDATA;
1619 mjpeg_decode_dri(s);
1631 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
1634 // printf("mjpeg: unsupported marker (%x)\n", start_code);
1638 /* eof process start code */
1639 buf_ptr += (get_bits_count(&s->gb)+7)/8;
1640 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
1641 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
1645 if (s->got_picture) {
1646 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1649 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1652 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr);
1653 // return buf_end - buf_ptr;
1654 return buf_ptr - buf;
1657 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
1659 MJpegDecodeContext *s = avctx->priv_data;
1662 if (s->picture_ptr && s->picture_ptr->data[0])
1663 avctx->release_buffer(avctx, s->picture_ptr);
1666 av_free(s->qscale_table);
1667 av_freep(&s->ljpeg_buffer);
1668 s->ljpeg_buffer_size=0;
1672 free_vlc(&s->vlcs[i][j]);
1674 for(i=0; i<MAX_COMPONENTS; i++) {
1675 av_freep(&s->blocks[i]);
1676 av_freep(&s->last_nnz[i]);
1681 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1682 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1683 static const AVOption options[] = {
1684 { "extern_huff", "Use external huffman table.", OFFSET(extern_huff), AV_OPT_TYPE_INT, { 0 }, 0, 1, VD },
1688 static const AVClass mjpegdec_class = {
1689 .class_name = "MJPEG decoder",
1690 .item_name = av_default_item_name,
1692 .version = LIBAVUTIL_VERSION_INT,
1695 AVCodec ff_mjpeg_decoder = {
1697 .type = AVMEDIA_TYPE_VIDEO,
1698 .id = CODEC_ID_MJPEG,
1699 .priv_data_size = sizeof(MJpegDecodeContext),
1700 .init = ff_mjpeg_decode_init,
1701 .close = ff_mjpeg_decode_end,
1702 .decode = ff_mjpeg_decode_frame,
1703 .capabilities = CODEC_CAP_DR1,
1705 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1706 .priv_class = &mjpegdec_class,
1709 AVCodec ff_thp_decoder = {
1711 .type = AVMEDIA_TYPE_VIDEO,
1713 .priv_data_size = sizeof(MJpegDecodeContext),
1714 .init = ff_mjpeg_decode_init,
1715 .close = ff_mjpeg_decode_end,
1716 .decode = ff_mjpeg_decode_frame,
1717 .capabilities = CODEC_CAP_DR1,
1719 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),