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 Libav.
13 * Libav 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 * Libav 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 Libav; 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/opt.h"
42 #include "jpeglsdec.h"
45 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
46 int nb_codes, int use_static, int is_ac)
48 uint8_t huff_size[256];
49 uint16_t huff_code[256];
50 uint16_t huff_sym[256];
53 assert(nb_codes <= 256);
55 memset(huff_size, 0, sizeof(huff_size));
56 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
59 huff_sym[i]= i + 16*is_ac;
61 if(is_ac) huff_sym[0]= 16*256;
63 return init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, huff_sym, 2, 2, use_static);
66 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) {
67 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance,
68 ff_mjpeg_val_dc, 12, 0, 0);
69 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance,
70 ff_mjpeg_val_dc, 12, 0, 0);
71 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance,
72 ff_mjpeg_val_ac_luminance, 251, 0, 1);
73 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance,
74 ff_mjpeg_val_ac_chrominance, 251, 0, 1);
75 build_vlc(&s->vlcs[2][0], ff_mjpeg_bits_ac_luminance,
76 ff_mjpeg_val_ac_luminance, 251, 0, 0);
77 build_vlc(&s->vlcs[2][1], ff_mjpeg_bits_ac_chrominance,
78 ff_mjpeg_val_ac_chrominance, 251, 0, 0);
81 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
83 MJpegDecodeContext *s = avctx->priv_data;
86 s->picture_ptr = &s->picture;
89 dsputil_init(&s->dsp, avctx);
90 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
95 s->org_height = avctx->coded_height;
96 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
98 build_basic_mjpeg_vlc(s);
100 #if FF_API_MJPEG_GLOBAL_OPTS
101 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
106 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
107 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
108 if (ff_mjpeg_decode_dht(s)) {
109 av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table\n");
110 return AVERROR_INVALIDDATA;
113 if (avctx->extradata_size > 9 &&
114 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) {
115 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */
116 s->interlace_polarity = 1; /* bottom field first */
117 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
120 if (avctx->codec->id == CODEC_ID_AMV)
127 /* quantize tables */
128 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
130 int len, index, i, j;
132 len = get_bits(&s->gb, 16) - 2;
135 /* only 8 bit precision handled */
136 if (get_bits(&s->gb, 4) != 0)
138 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
141 index = get_bits(&s->gb, 4);
144 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
145 /* read quant table */
147 j = s->scantable.permutated[i];
148 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
151 //XXX FIXME finetune, and perhaps add dc too
152 s->qscale[index]= FFMAX(
153 s->quant_matrixes[index][s->scantable.permutated[1]],
154 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
155 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
162 /* decode huffman tables and build VLC decoders */
163 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
165 int len, index, i, class, n, v, code_max;
166 uint8_t bits_table[17];
167 uint8_t val_table[256];
169 len = get_bits(&s->gb, 16) - 2;
174 class = get_bits(&s->gb, 4);
177 index = get_bits(&s->gb, 4);
182 bits_table[i] = get_bits(&s->gb, 8);
186 if (len < n || n > 256)
191 v = get_bits(&s->gb, 8);
198 /* build VLC and flush previous vlc if present */
199 free_vlc(&s->vlcs[class][index]);
200 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
201 class, index, code_max + 1);
202 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
207 free_vlc(&s->vlcs[2][index]);
208 if(build_vlc(&s->vlcs[2][index], bits_table, val_table, code_max + 1, 0, 0) < 0){
216 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
218 int len, nb_components, i, width, height, pix_fmt_id;
220 /* XXX: verify len field validity */
221 len = get_bits(&s->gb, 16);
222 s->bits= get_bits(&s->gb, 8);
224 if(s->pegasus_rct) s->bits=9;
225 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
227 if (s->bits != 8 && !s->lossless){
228 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
232 height = get_bits(&s->gb, 16);
233 width = get_bits(&s->gb, 16);
235 //HACK for odd_height.mov
236 if(s->interlaced && s->width == width && s->height == height + 1)
239 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
240 if(av_image_check_size(width, height, 0, s->avctx))
243 nb_components = get_bits(&s->gb, 8);
244 if (nb_components <= 0 ||
245 nb_components > MAX_COMPONENTS)
247 if (s->ls && !(s->bits <= 8 || nb_components == 1)){
248 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
251 s->nb_components = nb_components;
254 for(i=0;i<nb_components;i++) {
256 s->component_id[i] = get_bits(&s->gb, 8) - 1;
257 s->h_count[i] = get_bits(&s->gb, 4);
258 s->v_count[i] = get_bits(&s->gb, 4);
259 /* compute hmax and vmax (only used in interleaved case) */
260 if (s->h_count[i] > s->h_max)
261 s->h_max = s->h_count[i];
262 if (s->v_count[i] > s->v_max)
263 s->v_max = s->v_count[i];
264 s->quant_index[i] = get_bits(&s->gb, 8);
265 if (s->quant_index[i] >= 4)
267 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
268 s->v_count[i], s->component_id[i], s->quant_index[i]);
271 if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
272 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
276 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
278 /* if different size, realloc/alloc picture */
279 /* XXX: also check h_count and v_count */
280 if (width != s->width || height != s->height) {
281 av_freep(&s->qscale_table);
287 /* test interlaced mode */
288 if (s->first_picture &&
289 s->org_height != 0 &&
290 s->height < ((s->org_height * 3) / 4)) {
292 s->bottom_field = s->interlace_polarity;
293 s->picture_ptr->interlaced_frame = 1;
294 s->picture_ptr->top_field_first = !s->interlace_polarity;
298 avcodec_set_dimensions(s->avctx, width, height);
300 s->qscale_table= av_mallocz((s->width+15)/16);
302 s->first_picture = 0;
305 if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
308 /* XXX: not complete test ! */
309 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
310 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
311 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
312 (s->h_count[3] << 4) | s->v_count[3];
313 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
314 //NOTE we do not allocate pictures large enough for the possible padding of h/v_count being 4
315 if(!(pix_fmt_id & 0xD0D0D0D0))
316 pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1;
317 if(!(pix_fmt_id & 0x0D0D0D0D))
318 pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1;
323 s->avctx->pix_fmt = PIX_FMT_BGRA;
325 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
326 assert(s->nb_components==3);
329 s->avctx->pix_fmt = PIX_FMT_GRAY8;
332 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P;
335 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
338 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
341 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
345 if(s->nb_components > 1)
346 s->avctx->pix_fmt = PIX_FMT_RGB24;
347 else if(s->bits <= 8)
348 s->avctx->pix_fmt = PIX_FMT_GRAY8;
350 s->avctx->pix_fmt = PIX_FMT_GRAY16;
353 if(s->picture_ptr->data[0])
354 s->avctx->release_buffer(s->avctx, s->picture_ptr);
356 if(s->avctx->get_buffer(s->avctx, s->picture_ptr) < 0){
357 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
360 s->picture_ptr->pict_type= AV_PICTURE_TYPE_I;
361 s->picture_ptr->key_frame= 1;
365 s->linesize[i]= s->picture_ptr->linesize[i] << s->interlaced;
368 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
370 if (len != (8+(3*nb_components)))
372 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
375 /* totally blank picture as progressive JPEG will only add details to it */
377 int bw = (width + s->h_max*8-1) / (s->h_max*8);
378 int bh = (height + s->v_max*8-1) / (s->v_max*8);
379 for(i=0; i<s->nb_components; i++) {
380 int size = bw * bh * s->h_count[i] * s->v_count[i];
381 av_freep(&s->blocks[i]);
382 av_freep(&s->last_nnz[i]);
383 s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
384 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
385 s->block_stride[i] = bw * s->h_count[i];
387 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
392 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
395 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
398 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
399 &s->vlcs[0][dc_index]);
404 return get_xbits(&s->gb, code);
409 /* decode block and dequantize */
410 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
411 int component, int dc_index, int ac_index, int16_t *quant_matrix)
413 int code, i, j, level, val;
416 val = mjpeg_decode_dc(s, dc_index);
418 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
421 val = val * quant_matrix[0] + s->last_dc[component];
422 s->last_dc[component] = val;
426 {OPEN_READER(re, &s->gb);
428 UPDATE_CACHE(re, &s->gb);
429 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
431 i += ((unsigned)code) >> 4;
434 if(code > MIN_CACHE_BITS - 16){
435 UPDATE_CACHE(re, &s->gb);
438 int cache=GET_CACHE(re,&s->gb);
439 int sign=(~cache)>>31;
440 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
443 LAST_SKIP_BITS(re, &s->gb, code);
446 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
449 j = s->scantable.permutated[i];
450 block[j] = level * quant_matrix[j];
453 CLOSE_READER(re, &s->gb);}
458 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component,
459 int dc_index, int16_t *quant_matrix, int Al)
462 s->dsp.clear_block(block);
463 val = mjpeg_decode_dc(s, dc_index);
465 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
468 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
469 s->last_dc[component] = val;
474 /* decode block and dequantize - progressive JPEG version */
475 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
476 int ac_index, int16_t *quant_matrix,
477 int ss, int se, int Al, int *EOBRUN)
479 int code, i, j, level, val, run;
485 {OPEN_READER(re, &s->gb);
487 UPDATE_CACHE(re, &s->gb);
488 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
490 run = ((unsigned) code) >> 4;
494 if(code > MIN_CACHE_BITS - 16){
495 UPDATE_CACHE(re, &s->gb);
498 int cache=GET_CACHE(re,&s->gb);
499 int sign=(~cache)>>31;
500 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
503 LAST_SKIP_BITS(re, &s->gb, code);
507 j = s->scantable.permutated[se];
508 block[j] = level * quant_matrix[j] << Al;
511 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
514 j = s->scantable.permutated[i];
515 block[j] = level * quant_matrix[j] << Al;
517 if(run == 0xF){// ZRL - skip 15 coefficients
520 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
526 UPDATE_CACHE(re, &s->gb);
527 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
528 LAST_SKIP_BITS(re, &s->gb, run);
535 CLOSE_READER(re, &s->gb);}
541 #define REFINE_BIT(j) {\
542 UPDATE_CACHE(re, &s->gb);\
543 sign = block[j]>>15;\
544 block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\
545 LAST_SKIP_BITS(re, &s->gb, 1);\
553 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\
558 j = s->scantable.permutated[i];\
565 /* decode block and dequantize - progressive JPEG refinement pass */
566 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz,
567 int ac_index, int16_t *quant_matrix,
568 int ss, int se, int Al, int *EOBRUN)
570 int code, i=ss, j, sign, val, run;
571 int last = FFMIN(se, *last_nnz);
573 OPEN_READER(re, &s->gb);
578 UPDATE_CACHE(re, &s->gb);
579 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
582 run = ((unsigned) code) >> 4;
583 UPDATE_CACHE(re, &s->gb);
584 val = SHOW_UBITS(re, &s->gb, 1);
585 LAST_SKIP_BITS(re, &s->gb, 1);
587 j = s->scantable.permutated[i];
589 block[j] = ((quant_matrix[j]^val)-val) << Al;
593 CLOSE_READER(re, &s->gb);
597 run = ((unsigned) code) >> 4;
604 UPDATE_CACHE(re, &s->gb);
605 run += SHOW_UBITS(re, &s->gb, val);
606 LAST_SKIP_BITS(re, &s->gb, val);
619 j = s->scantable.permutated[i];
623 CLOSE_READER(re, &s->gb);
630 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
632 uint16_t (*buffer)[4];
633 int left[3], top[3], topleft[3];
634 const int linesize= s->linesize[0];
635 const int mask= (1<<s->bits)-1;
637 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
638 buffer= s->ljpeg_buffer;
641 buffer[0][i]= 1 << (s->bits + point_transform - 1);
643 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
644 const int modified_predictor= mb_y ? predictor : 1;
645 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
647 if (s->interlaced && s->bottom_field)
648 ptr += linesize >> 1;
651 top[i]= left[i]= topleft[i]= buffer[0][i];
653 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
654 if (s->restart_interval && !s->restart_count)
655 s->restart_count = s->restart_interval;
661 top[i]= buffer[mb_x][i];
663 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
666 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
669 if (s->restart_interval && !--s->restart_count) {
670 align_get_bits(&s->gb);
671 skip_bits(&s->gb, 16); /* skip RSTn */
676 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
677 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
678 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
679 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
681 }else if(s->pegasus_rct){
682 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
683 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
684 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
685 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
688 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
689 ptr[4*mb_x+0] = buffer[mb_x][2];
690 ptr[4*mb_x+1] = buffer[mb_x][1];
691 ptr[4*mb_x+2] = buffer[mb_x][0];
698 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
700 const int nb_components=3;
702 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
703 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
704 if (s->restart_interval && !s->restart_count)
705 s->restart_count = s->restart_interval;
707 if(mb_x==0 || mb_y==0 || s->interlaced){
708 for(i=0;i<nb_components;i++) {
710 int n, h, v, x, y, c, j, linesize;
712 c = s->comp_index[i];
717 linesize= s->linesize[c];
722 ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
725 pred= 128 << point_transform;
731 pred= ptr[-linesize];
733 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
737 if (s->interlaced && s->bottom_field)
738 ptr += linesize >> 1;
739 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
748 for(i=0;i<nb_components;i++) {
750 int n, h, v, x, y, c, j, linesize;
752 c = s->comp_index[i];
757 linesize= s->linesize[c];
762 ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
763 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
764 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
772 if (s->restart_interval && !--s->restart_count) {
773 align_get_bits(&s->gb);
774 skip_bits(&s->gb, 16); /* skip RSTn */
781 static av_always_inline void mjpeg_copy_block(uint8_t *dst, const uint8_t *src,
782 int linesize, int lowres)
785 case 0: copy_block8(dst, src, linesize, linesize, 8);
787 case 1: copy_block4(dst, src, linesize, linesize, 4);
789 case 2: copy_block2(dst, src, linesize, linesize, 2);
796 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al,
797 const uint8_t *mb_bitmask, const AVFrame *reference){
799 uint8_t* data[MAX_COMPONENTS];
800 const uint8_t *reference_data[MAX_COMPONENTS];
801 int linesize[MAX_COMPONENTS];
802 GetBitContext mb_bitmask_gb;
805 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width*s->mb_height);
808 if(s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {
809 av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n");
812 for(i=0; i < nb_components; i++) {
813 int c = s->comp_index[i];
814 data[c] = s->picture_ptr->data[c];
815 reference_data[c] = reference ? reference->data[c] : NULL;
816 linesize[c]=s->linesize[c];
817 s->coefs_finished[c] |= 1;
819 //picture should be flipped upside-down for this codec
820 int offset = (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));
822 reference_data[c] += offset;
827 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
828 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
829 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
831 if (s->restart_interval && !s->restart_count)
832 s->restart_count = s->restart_interval;
834 if(get_bits_count(&s->gb)>s->gb.size_in_bits){
835 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", get_bits_count(&s->gb) - s->gb.size_in_bits);
838 for(i=0;i<nb_components;i++) {
840 int n, h, v, x, y, c, j;
843 c = s->comp_index[i];
849 block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
850 (h * mb_x + x) * 8) >> s->avctx->lowres);
852 if(s->interlaced && s->bottom_field)
853 block_offset += linesize[c] >> 1;
854 ptr = data[c] + block_offset;
855 if(!s->progressive) {
857 mjpeg_copy_block(ptr, reference_data[c] + block_offset, linesize[c], s->avctx->lowres);
859 s->dsp.clear_block(s->block);
860 if(decode_block(s, s->block, i,
861 s->dc_index[i], s->ac_index[i],
862 s->quant_matrixes[ s->quant_index[c] ]) < 0) {
863 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
866 s->dsp.idct_put(ptr, linesize[c], s->block);
869 int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
870 DCTELEM *block = s->blocks[c][block_idx];
872 block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
873 else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
874 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
878 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
879 //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);
887 if (s->restart_interval && show_bits(&s->gb, 8) == 0xFF){ /* skip RSTn */
889 align_get_bits(&s->gb);
890 while(show_bits(&s->gb, 8) == 0xFF)
891 skip_bits(&s->gb, 8);
892 skip_bits(&s->gb, 8);
893 for (i=0; i<nb_components; i++) /* reset dc */
894 s->last_dc[i] = 1024;
901 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al,
902 const uint8_t *mb_bitmask, const AVFrame *reference){
905 int c = s->comp_index[0];
906 uint8_t* data = s->picture_ptr->data[c];
907 const uint8_t *reference_data = reference ? reference->data[c] : NULL;
908 int linesize = s->linesize[c];
910 int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ];
911 GetBitContext mb_bitmask_gb;
914 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width*s->mb_height);
918 s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss);
919 last_scan = !~s->coefs_finished[c];
922 if(s->interlaced && s->bottom_field) {
923 int offset = linesize >> 1;
925 reference_data += offset;
928 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
929 int block_offset = (mb_y*linesize*8 >> s->avctx->lowres);
930 uint8_t *ptr = data + block_offset;
931 int block_idx = mb_y * s->block_stride[c];
932 DCTELEM (*block)[64] = &s->blocks[c][block_idx];
933 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
934 for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
935 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
940 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
941 quant_matrix, ss, se, Al, &EOBRUN);
943 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
944 quant_matrix, ss, se, Al, &EOBRUN);
946 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
953 mjpeg_copy_block(ptr, reference_data + block_offset, linesize, s->avctx->lowres);
955 s->dsp.idct_put(ptr, linesize, *block);
956 ptr += 8 >> s->avctx->lowres;
964 int ff_mjpeg_decode_sos(MJpegDecodeContext *s,
965 const uint8_t *mb_bitmask, const AVFrame *reference)
967 int len, nb_components, i, h, v, predictor, point_transform;
969 const int block_size= s->lossless ? 1 : 8;
972 /* XXX: verify len field validity */
973 len = get_bits(&s->gb, 16);
974 nb_components = get_bits(&s->gb, 8);
975 if (nb_components == 0 || nb_components > MAX_COMPONENTS){
976 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\n", nb_components);
979 if (len != 6+2*nb_components)
981 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
984 for(i=0;i<nb_components;i++) {
985 id = get_bits(&s->gb, 8) - 1;
986 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
987 /* find component index */
988 for(index=0;index<s->nb_components;index++)
989 if (id == s->component_id[index])
991 if (index == s->nb_components)
993 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
996 /* Metasoft MJPEG codec has Cb and Cr swapped */
997 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
998 && nb_components == 3 && s->nb_components == 3 && i)
1001 s->comp_index[i] = index;
1003 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1004 s->h_scount[i] = s->h_count[index];
1005 s->v_scount[i] = s->v_count[index];
1007 s->dc_index[i] = get_bits(&s->gb, 4);
1008 s->ac_index[i] = get_bits(&s->gb, 4);
1010 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1011 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1013 if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table)
1017 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1018 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1019 prev_shift = get_bits(&s->gb, 4); /* Ah */
1020 point_transform= get_bits(&s->gb, 4); /* Al */
1022 for(i=0;i<nb_components;i++)
1023 s->last_dc[i] = 1024;
1025 if (nb_components > 1) {
1026 /* interleaved stream */
1027 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1028 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1029 } else if(!s->ls) { /* skip this for JPEG-LS */
1030 h = s->h_max / s->h_scount[0];
1031 v = s->v_max / s->v_scount[0];
1032 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1033 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1034 s->nb_blocks[0] = 1;
1039 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1040 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1041 predictor, point_transform, ilv, s->bits,
1042 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
1045 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1046 for (i = s->mjpb_skiptosod; i > 0; i--)
1047 skip_bits(&s->gb, 8);
1050 if(CONFIG_JPEGLS_DECODER && s->ls){
1052 // reset_ls_coding_parameters(s, 0);
1054 if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)
1058 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
1061 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
1066 if(s->progressive && predictor) {
1067 if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform,
1068 mb_bitmask, reference) < 0)
1071 if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform,
1072 mb_bitmask, reference) < 0)
1079 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1083 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1085 if (get_bits(&s->gb, 16) != 4)
1087 s->restart_interval = get_bits(&s->gb, 16);
1088 s->restart_count = 0;
1089 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
1094 static int mjpeg_decode_app(MJpegDecodeContext *s)
1098 len = get_bits(&s->gb, 16);
1101 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
1104 id = get_bits_long(&s->gb, 32);
1105 id = av_be2ne32(id);
1108 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1109 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1112 /* buggy AVID, it puts EOI only at every 10th frame */
1113 /* also this fourcc is used by non-avid files too, it holds some
1114 informations, but it's always present in AVID creates files */
1115 if (id == AV_RL32("AVI1"))
1122 4bytes field_size_less_padding
1125 // if (s->first_picture)
1126 // printf("mjpeg: workarounding buggy AVID\n");
1127 i = get_bits(&s->gb, 8);
1128 if (i==2) s->bottom_field= 1;
1129 else if(i==1) s->bottom_field= 0;
1131 skip_bits(&s->gb, 8);
1132 skip_bits(&s->gb, 32);
1133 skip_bits(&s->gb, 32);
1136 // if (s->interlace_polarity)
1137 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
1143 if (id == AV_RL32("JFIF"))
1145 int t_w, t_h, v1, v2;
1146 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1147 v1= get_bits(&s->gb, 8);
1148 v2= get_bits(&s->gb, 8);
1149 skip_bits(&s->gb, 8);
1151 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
1152 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
1154 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1155 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1157 s->avctx->sample_aspect_ratio.num,
1158 s->avctx->sample_aspect_ratio.den
1161 t_w = get_bits(&s->gb, 8);
1162 t_h = get_bits(&s->gb, 8);
1165 /* skip thumbnail */
1166 if (len-10-(t_w*t_h*3) > 0)
1173 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e'))
1175 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1176 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1177 skip_bits(&s->gb, 16); /* version */
1178 skip_bits(&s->gb, 16); /* flags0 */
1179 skip_bits(&s->gb, 16); /* flags1 */
1180 skip_bits(&s->gb, 8); /* transform */
1185 if (id == AV_RL32("LJIF")){
1186 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1187 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
1188 skip_bits(&s->gb, 16); /* version ? */
1189 skip_bits(&s->gb, 16); /* unknwon always 0? */
1190 skip_bits(&s->gb, 16); /* unknwon always 0? */
1191 skip_bits(&s->gb, 16); /* unknwon always 0? */
1192 switch( get_bits(&s->gb, 8)){
1202 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1209 if ((s->start_code == APP1) && (len > (0x28 - 8)))
1211 id = get_bits_long(&s->gb, 32);
1212 id = av_be2ne32(id);
1214 if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */
1217 skip_bits(&s->gb, 32); /* field size */
1218 skip_bits(&s->gb, 32); /* pad field size */
1219 skip_bits(&s->gb, 32); /* next off */
1220 skip_bits(&s->gb, 32); /* quant off */
1221 skip_bits(&s->gb, 32); /* huff off */
1222 skip_bits(&s->gb, 32); /* image off */
1223 skip_bits(&s->gb, 32); /* scan off */
1224 skip_bits(&s->gb, 32); /* data off */
1226 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1227 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1232 /* slow but needed for extreme adobe jpegs */
1234 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
1236 skip_bits(&s->gb, 8);
1241 static int mjpeg_decode_com(MJpegDecodeContext *s)
1243 int len = get_bits(&s->gb, 16);
1244 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
1245 char *cbuf = av_malloc(len - 1);
1248 for (i = 0; i < len - 2; i++)
1249 cbuf[i] = get_bits(&s->gb, 8);
1250 if (i > 0 && cbuf[i-1] == '\n')
1255 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1256 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1258 /* buggy avid, it puts EOI only at every 10th frame */
1259 if (!strcmp(cbuf, "AVID"))
1262 // if (s->first_picture)
1263 // printf("mjpeg: workarounding buggy AVID\n");
1265 else if(!strcmp(cbuf, "CS=ITU601")){
1268 else if((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1269 (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20))){
1280 /* return the 8 bit start code value and update the search
1281 state. Return -1 if no start code found */
1282 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1284 const uint8_t *buf_ptr;
1291 buf_ptr = *pbuf_ptr;
1292 while (buf_ptr < buf_end) {
1295 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1305 av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1306 *pbuf_ptr = buf_ptr;
1310 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1311 const uint8_t **buf_ptr, const uint8_t *buf_end,
1312 const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size)
1315 start_code = find_marker(buf_ptr, buf_end);
1317 if ((buf_end - *buf_ptr) > s->buffer_size)
1320 s->buffer_size = buf_end - *buf_ptr;
1321 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
1322 av_log(s->avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
1326 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1327 if (start_code == SOS && !s->ls)
1329 const uint8_t *src = *buf_ptr;
1330 uint8_t *dst = s->buffer;
1334 uint8_t x = *(src++);
1337 if (s->avctx->codec_id != CODEC_ID_THP)
1340 while (src < buf_end && x == 0xff)
1343 if (x >= 0xd0 && x <= 0xd7)
1350 *unescaped_buf_ptr = s->buffer;
1351 *unescaped_buf_size = dst - s->buffer;
1353 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1354 (buf_end - *buf_ptr) - (dst - s->buffer));
1356 else if(start_code == SOS && s->ls){
1357 const uint8_t *src = *buf_ptr;
1358 uint8_t *dst = s->buffer;
1366 while (src + t < buf_end){
1367 uint8_t x = src[t++];
1369 while((src + t < buf_end) && x == 0xff)
1379 init_put_bits(&pb, dst, t);
1381 /* unescape bitstream */
1383 uint8_t x = src[b++];
1384 put_bits(&pb, 8, x);
1387 put_bits(&pb, 7, x);
1391 flush_put_bits(&pb);
1393 *unescaped_buf_ptr = dst;
1394 *unescaped_buf_size = (bit_count + 7) >> 3;
1398 *unescaped_buf_ptr = *buf_ptr;
1399 *unescaped_buf_size = buf_end - *buf_ptr;
1405 int ff_mjpeg_decode_frame(AVCodecContext *avctx,
1406 void *data, int *data_size,
1409 const uint8_t *buf = avpkt->data;
1410 int buf_size = avpkt->size;
1411 MJpegDecodeContext *s = avctx->priv_data;
1412 const uint8_t *buf_end, *buf_ptr;
1413 const uint8_t *unescaped_buf_ptr;
1414 int unescaped_buf_size;
1416 AVFrame *picture = data;
1418 s->got_picture = 0; // picture from previous image can not be reused
1420 buf_end = buf + buf_size;
1421 while (buf_ptr < buf_end) {
1422 /* find start next marker */
1423 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1424 &unescaped_buf_ptr, &unescaped_buf_size);
1427 if (start_code < 0) {
1430 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr);
1432 init_get_bits(&s->gb, unescaped_buf_ptr, unescaped_buf_size*8);
1434 s->start_code = start_code;
1435 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1436 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1439 /* process markers */
1440 if (start_code >= 0xd0 && start_code <= 0xd7) {
1441 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
1443 } else if (start_code >= APP0 && start_code <= APP15) {
1444 mjpeg_decode_app(s);
1446 } else if (start_code == COM){
1447 mjpeg_decode_com(s);
1450 switch(start_code) {
1452 s->restart_interval = 0;
1454 s->restart_count = 0;
1455 /* nothing to do on SOI */
1458 ff_mjpeg_decode_dqt(s);
1461 if(ff_mjpeg_decode_dht(s) < 0){
1462 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1471 if (ff_mjpeg_decode_sof(s) < 0)
1478 if (ff_mjpeg_decode_sof(s) < 0)
1485 if (ff_mjpeg_decode_sof(s) < 0)
1492 if (ff_mjpeg_decode_sof(s) < 0)
1496 if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
1501 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1504 if (!s->got_picture) {
1505 av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n");
1508 if (s->interlaced) {
1509 s->bottom_field ^= 1;
1510 /* if not bottom field, do not output image yet */
1511 if (s->bottom_field == !s->interlace_polarity)
1514 *picture = *s->picture_ptr;
1515 *data_size = sizeof(AVFrame);
1518 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
1519 picture->qstride= 0;
1520 picture->qscale_table= s->qscale_table;
1521 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
1522 if(avctx->debug & FF_DEBUG_QP)
1523 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
1524 picture->quality*= FF_QP2LAMBDA;
1529 if (!s->got_picture) {
1530 av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n");
1533 if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&
1534 avctx->error_recognition >= FF_ER_EXPLODE)
1535 return AVERROR_INVALIDDATA;
1536 /* buggy avid puts EOI every 10-20th frame */
1537 /* if restart period is over process EOI */
1538 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1542 mjpeg_decode_dri(s);
1554 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
1557 // printf("mjpeg: unsupported marker (%x)\n", start_code);
1562 /* eof process start code */
1563 buf_ptr += (get_bits_count(&s->gb)+7)/8;
1564 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
1565 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
1569 if (s->got_picture) {
1570 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1573 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1576 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr);
1577 // return buf_end - buf_ptr;
1578 return buf_ptr - buf;
1581 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
1583 MJpegDecodeContext *s = avctx->priv_data;
1586 if (s->picture_ptr && s->picture_ptr->data[0])
1587 avctx->release_buffer(avctx, s->picture_ptr);
1590 av_free(s->qscale_table);
1591 av_freep(&s->ljpeg_buffer);
1592 s->ljpeg_buffer_size=0;
1596 free_vlc(&s->vlcs[i][j]);
1598 for(i=0; i<MAX_COMPONENTS; i++) {
1599 av_freep(&s->blocks[i]);
1600 av_freep(&s->last_nnz[i]);
1605 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1606 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1607 static const AVOption options[] = {
1608 { "extern_huff", "Use external huffman table.", OFFSET(extern_huff), FF_OPT_TYPE_INT, { 0 }, 0, 1, VD },
1612 static const AVClass mjpegdec_class = {
1613 .class_name = "MJPEG decoder",
1614 .item_name = av_default_item_name,
1616 .version = LIBAVUTIL_VERSION_INT,
1619 AVCodec ff_mjpeg_decoder = {
1621 .type = AVMEDIA_TYPE_VIDEO,
1622 .id = CODEC_ID_MJPEG,
1623 .priv_data_size = sizeof(MJpegDecodeContext),
1624 .init = ff_mjpeg_decode_init,
1625 .close = ff_mjpeg_decode_end,
1626 .decode = ff_mjpeg_decode_frame,
1627 .capabilities = CODEC_CAP_DR1,
1629 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1630 .priv_class = &mjpegdec_class,
1633 AVCodec ff_thp_decoder = {
1635 .type = AVMEDIA_TYPE_VIDEO,
1637 .priv_data_size = sizeof(MJpegDecodeContext),
1638 .init = ff_mjpeg_decode_init,
1639 .close = ff_mjpeg_decode_end,
1640 .decode = ff_mjpeg_decode_frame,
1641 .capabilities = CODEC_CAP_DR1,
1643 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),