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
33 #include "libavutil/imgutils.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/opt.h"
37 #include "copy_block.h"
41 #include "jpeglsdec.h"
44 #include "bytestream.h"
47 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
48 const uint8_t *val_table, int nb_codes,
49 int use_static, int is_ac)
51 uint8_t huff_size[256] = { 0 };
52 uint16_t huff_code[256];
53 uint16_t huff_sym[256];
56 av_assert0(nb_codes <= 256);
58 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
60 for (i = 0; i < 256; i++)
61 huff_sym[i] = i + 16 * is_ac;
64 huff_sym[0] = 16 * 256;
66 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
67 huff_code, 2, 2, huff_sym, 2, 2, use_static);
70 static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
72 build_vlc(&s->vlcs[0][0], avpriv_mjpeg_bits_dc_luminance,
73 avpriv_mjpeg_val_dc, 12, 0, 0);
74 build_vlc(&s->vlcs[0][1], avpriv_mjpeg_bits_dc_chrominance,
75 avpriv_mjpeg_val_dc, 12, 0, 0);
76 build_vlc(&s->vlcs[1][0], avpriv_mjpeg_bits_ac_luminance,
77 avpriv_mjpeg_val_ac_luminance, 251, 0, 1);
78 build_vlc(&s->vlcs[1][1], avpriv_mjpeg_bits_ac_chrominance,
79 avpriv_mjpeg_val_ac_chrominance, 251, 0, 1);
80 build_vlc(&s->vlcs[2][0], avpriv_mjpeg_bits_ac_luminance,
81 avpriv_mjpeg_val_ac_luminance, 251, 0, 0);
82 build_vlc(&s->vlcs[2][1], avpriv_mjpeg_bits_ac_chrominance,
83 avpriv_mjpeg_val_ac_chrominance, 251, 0, 0);
86 static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
89 if (len > 14 && buf[12] == 1) /* 1 - NTSC */
90 s->interlace_polarity = 1;
91 if (len > 14 && buf[12] == 2) /* 2 - PAL */
92 s->interlace_polarity = 0;
93 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
94 av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 14 ? buf[12] : -1);
97 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
99 MJpegDecodeContext *s = avctx->priv_data;
101 if (!s->picture_ptr) {
102 s->picture = av_frame_alloc();
104 return AVERROR(ENOMEM);
105 s->picture_ptr = s->picture;
109 ff_hpeldsp_init(&s->hdsp, avctx->flags);
110 ff_dsputil_init(&s->dsp, avctx);
111 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
115 s->first_picture = 1;
117 s->org_height = avctx->coded_height;
118 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
120 build_basic_mjpeg_vlc(s);
122 if (s->extern_huff) {
123 av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
124 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
125 if (ff_mjpeg_decode_dht(s)) {
126 av_log(avctx, AV_LOG_ERROR,
127 "error using external huffman table, switching back to internal\n");
128 build_basic_mjpeg_vlc(s);
131 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
132 s->interlace_polarity = 1; /* bottom field first */
133 av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
134 } else if (avctx->field_order == AV_FIELD_UNKNOWN) {
135 if (avctx->codec_tag == AV_RL32("MJPG"))
136 s->interlace_polarity = 1;
139 if ( avctx->extradata_size > 8
140 && AV_RL32(avctx->extradata) == 0x2C
141 && AV_RL32(avctx->extradata+4) == 0x18) {
142 parse_avid(s, avctx->extradata, avctx->extradata_size);
145 if (avctx->codec->id == AV_CODEC_ID_AMV)
152 /* quantize tables */
153 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
155 int len, index, i, j;
157 len = get_bits(&s->gb, 16) - 2;
160 int pr = get_bits(&s->gb, 4);
162 av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
163 return AVERROR_INVALIDDATA;
165 index = get_bits(&s->gb, 4);
168 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
169 /* read quant table */
170 for (i = 0; i < 64; i++) {
171 j = s->scantable.permutated[i];
172 s->quant_matrixes[index][j] = get_bits(&s->gb, pr ? 16 : 8);
175 // XXX FIXME finetune, and perhaps add dc too
176 s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
177 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
178 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
179 index, s->qscale[index]);
185 /* decode huffman tables and build VLC decoders */
186 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
188 int len, index, i, class, n, v, code_max;
189 uint8_t bits_table[17];
190 uint8_t val_table[256];
193 len = get_bits(&s->gb, 16) - 2;
197 return AVERROR_INVALIDDATA;
198 class = get_bits(&s->gb, 4);
200 return AVERROR_INVALIDDATA;
201 index = get_bits(&s->gb, 4);
203 return AVERROR_INVALIDDATA;
205 for (i = 1; i <= 16; i++) {
206 bits_table[i] = get_bits(&s->gb, 8);
210 if (len < n || n > 256)
211 return AVERROR_INVALIDDATA;
214 for (i = 0; i < n; i++) {
215 v = get_bits(&s->gb, 8);
222 /* build VLC and flush previous vlc if present */
223 ff_free_vlc(&s->vlcs[class][index]);
224 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
225 class, index, code_max + 1);
226 if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
227 code_max + 1, 0, class > 0)) < 0)
231 ff_free_vlc(&s->vlcs[2][index]);
232 if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
233 code_max + 1, 0, 0)) < 0)
240 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
242 int len, nb_components, i, width, height, pix_fmt_id, ret;
243 int h_count[MAX_COMPONENTS];
244 int v_count[MAX_COMPONENTS];
247 s->upscale_h = s->upscale_v = 0;
249 /* XXX: verify len field validity */
250 len = get_bits(&s->gb, 16);
251 s->avctx->bits_per_raw_sample =
252 s->bits = get_bits(&s->gb, 8);
256 if (s->bits == 9 && !s->pegasus_rct)
257 s->rct = 1; // FIXME ugly
259 if(s->lossless && s->avctx->lowres){
260 av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
264 height = get_bits(&s->gb, 16);
265 width = get_bits(&s->gb, 16);
267 if (s->avctx->codec_id == AV_CODEC_ID_AMV && (height&15))
268 avpriv_request_sample(s->avctx, "non mod 16 height AMV\n");
270 // HACK for odd_height.mov
271 if (s->interlaced && s->width == width && s->height == height + 1)
274 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
275 if (av_image_check_size(width, height, 0, s->avctx))
276 return AVERROR_INVALIDDATA;
278 nb_components = get_bits(&s->gb, 8);
279 if (nb_components <= 0 ||
280 nb_components > MAX_COMPONENTS)
282 if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
283 if (nb_components != s->nb_components) {
284 av_log(s->avctx, AV_LOG_ERROR,
285 "nb_components changing in interlaced picture\n");
286 return AVERROR_INVALIDDATA;
289 if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
290 avpriv_report_missing_feature(s->avctx,
291 "JPEG-LS that is not <= 8 "
292 "bits/component or 16-bit gray");
293 return AVERROR_PATCHWELCOME;
295 s->nb_components = nb_components;
298 memset(h_count, 0, sizeof(h_count));
299 memset(v_count, 0, sizeof(v_count));
300 for (i = 0; i < nb_components; i++) {
302 s->component_id[i] = get_bits(&s->gb, 8) - 1;
303 h_count[i] = get_bits(&s->gb, 4);
304 v_count[i] = get_bits(&s->gb, 4);
305 /* compute hmax and vmax (only used in interleaved case) */
306 if (h_count[i] > s->h_max)
307 s->h_max = h_count[i];
308 if (v_count[i] > s->v_max)
309 s->v_max = v_count[i];
310 s->quant_index[i] = get_bits(&s->gb, 8);
311 if (s->quant_index[i] >= 4) {
312 av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
313 return AVERROR_INVALIDDATA;
315 if (!h_count[i] || !v_count[i]) {
316 av_log(s->avctx, AV_LOG_ERROR,
317 "Invalid sampling factor in component %d %d:%d\n",
318 i, h_count[i], v_count[i]);
319 return AVERROR_INVALIDDATA;
322 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
323 i, h_count[i], v_count[i],
324 s->component_id[i], s->quant_index[i]);
327 if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
328 avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
329 return AVERROR_PATCHWELCOME;
333 /* if different size, realloc/alloc picture */
334 if ( width != s->width || height != s->height
335 || memcmp(s->h_count, h_count, sizeof(h_count))
336 || memcmp(s->v_count, v_count, sizeof(v_count))) {
340 memcpy(s->h_count, h_count, sizeof(h_count));
341 memcpy(s->v_count, v_count, sizeof(v_count));
345 /* test interlaced mode */
346 if (s->first_picture &&
347 s->org_height != 0 &&
348 s->height < ((s->org_height * 3) / 4)) {
350 s->bottom_field = s->interlace_polarity;
351 s->picture_ptr->interlaced_frame = 1;
352 s->picture_ptr->top_field_first = !s->interlace_polarity;
356 ret = ff_set_dimensions(s->avctx, width, height);
360 s->first_picture = 0;
363 if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
364 if (s->progressive) {
365 avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
366 return AVERROR_INVALIDDATA;
369 if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4))
371 else if (!s->lossless)
373 /* XXX: not complete test ! */
374 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
375 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
376 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
377 (s->h_count[3] << 4) | s->v_count[3];
378 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
379 /* NOTE we do not allocate pictures large enough for the possible
380 * padding of h/v_count being 4 */
381 if (!(pix_fmt_id & 0xD0D0D0D0))
382 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
383 if (!(pix_fmt_id & 0x0D0D0D0D))
384 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
386 switch (pix_fmt_id) {
389 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_BGR48;
391 if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
392 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_GBRP : AV_PIX_FMT_GBRP16;
394 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
395 else s->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
396 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
399 av_assert0(s->nb_components == 3);
403 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA64;
405 if (s->adobe_transform == 0 && s->bits <= 8) {
406 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
408 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_YUVA444P : AV_PIX_FMT_YUVA444P16;
409 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
412 av_assert0(s->nb_components == 4);
415 if (s->adobe_transform == 0 && s->bits <= 8) {
416 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
419 s->chroma_height = s->height;
420 } else if (s->adobe_transform == 2 && s->bits <= 8) {
421 s->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
424 s->chroma_height = s->height;
425 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
427 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
428 else s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16;
429 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
431 av_assert0(s->nb_components == 4);
435 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
438 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
440 s->upscale_h = 2*(pix_fmt_id == 0x22122100);
441 s->chroma_height = s->height;
445 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
448 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
449 s->upscale_v = 2*(pix_fmt_id == 0x22211200);
451 s->chroma_height = s->height;
454 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
457 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
460 s->chroma_height = s->height / 2;
472 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
474 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
479 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P;
482 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
483 s->upscale_h = 4 * (pix_fmt_id == 0x22211100) + 2 * (pix_fmt_id == 0x22112100);
484 s->chroma_height = s->height / 2;
487 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
488 else s->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
489 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
493 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
496 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
497 s->upscale_v = 2 << (pix_fmt_id == 0x22121100);
501 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
502 else s->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
503 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
504 if (pix_fmt_id == 0x42111100) {
506 s->chroma_height = s->height / 2;
510 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV411P : AV_PIX_FMT_YUVJ411P;
513 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
517 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
518 return AVERROR_PATCHWELCOME;
520 if ((s->upscale_h || s->upscale_v) && s->avctx->lowres) {
521 av_log(s->avctx, AV_LOG_ERROR, "lowres not supported for weird subsampling\n");
522 return AVERROR_PATCHWELCOME;
525 s->upscale_h = s->upscale_v = 0;
526 if (s->nb_components > 1)
527 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
528 else if (s->palette_index && s->bits <= 8)
529 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
530 else if (s->bits <= 8)
531 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
533 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
536 s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
538 av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
542 av_frame_unref(s->picture_ptr);
543 if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0)
545 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
546 s->picture_ptr->key_frame = 1;
549 for (i = 0; i < 4; i++)
550 s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
552 av_dlog(s->avctx, "%d %d %d %d %d %d\n",
553 s->width, s->height, s->linesize[0], s->linesize[1],
554 s->interlaced, s->avctx->height);
556 if (len != (8 + (3 * nb_components)))
557 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
560 if (s->rgb && !s->lossless && !s->ls) {
561 av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
562 return AVERROR_PATCHWELCOME;
565 /* totally blank picture as progressive JPEG will only add details to it */
566 if (s->progressive) {
567 int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
568 int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
569 for (i = 0; i < s->nb_components; i++) {
570 int size = bw * bh * s->h_count[i] * s->v_count[i];
571 av_freep(&s->blocks[i]);
572 av_freep(&s->last_nnz[i]);
573 s->blocks[i] = av_mallocz_array(size, sizeof(**s->blocks));
574 s->last_nnz[i] = av_mallocz_array(size, sizeof(**s->last_nnz));
575 if (!s->blocks[i] || !s->last_nnz[i])
576 return AVERROR(ENOMEM);
577 s->block_stride[i] = bw * s->h_count[i];
579 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
584 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
587 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
588 if (code < 0 || code > 16) {
589 av_log(s->avctx, AV_LOG_WARNING,
590 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
591 0, dc_index, &s->vlcs[0][dc_index]);
596 return get_xbits(&s->gb, code);
601 /* decode block and dequantize */
602 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
603 int dc_index, int ac_index, int16_t *quant_matrix)
605 int code, i, j, level, val;
608 val = mjpeg_decode_dc(s, dc_index);
609 if (val == 0xfffff) {
610 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
611 return AVERROR_INVALIDDATA;
613 val = val * quant_matrix[0] + s->last_dc[component];
614 s->last_dc[component] = val;
618 {OPEN_READER(re, &s->gb);
620 UPDATE_CACHE(re, &s->gb);
621 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
623 i += ((unsigned)code) >> 4;
626 if (code > MIN_CACHE_BITS - 16)
627 UPDATE_CACHE(re, &s->gb);
630 int cache = GET_CACHE(re, &s->gb);
631 int sign = (~cache) >> 31;
632 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
635 LAST_SKIP_BITS(re, &s->gb, code);
638 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
639 return AVERROR_INVALIDDATA;
641 j = s->scantable.permutated[i];
642 block[j] = level * quant_matrix[j];
645 CLOSE_READER(re, &s->gb);}
650 static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
651 int component, int dc_index,
652 int16_t *quant_matrix, int Al)
655 s->dsp.clear_block(block);
656 val = mjpeg_decode_dc(s, dc_index);
657 if (val == 0xfffff) {
658 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
659 return AVERROR_INVALIDDATA;
661 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
662 s->last_dc[component] = val;
667 /* decode block and dequantize - progressive JPEG version */
668 static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
669 uint8_t *last_nnz, int ac_index,
670 int16_t *quant_matrix,
671 int ss, int se, int Al, int *EOBRUN)
673 int code, i, j, level, val, run;
681 OPEN_READER(re, &s->gb);
682 for (i = ss; ; i++) {
683 UPDATE_CACHE(re, &s->gb);
684 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
686 run = ((unsigned) code) >> 4;
690 if (code > MIN_CACHE_BITS - 16)
691 UPDATE_CACHE(re, &s->gb);
694 int cache = GET_CACHE(re, &s->gb);
695 int sign = (~cache) >> 31;
696 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
699 LAST_SKIP_BITS(re, &s->gb, code);
703 j = s->scantable.permutated[se];
704 block[j] = level * quant_matrix[j] << Al;
707 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
708 return AVERROR_INVALIDDATA;
710 j = s->scantable.permutated[i];
711 block[j] = level * quant_matrix[j] << Al;
713 if (run == 0xF) {// ZRL - skip 15 coefficients
716 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
717 return AVERROR_INVALIDDATA;
722 UPDATE_CACHE(re, &s->gb);
723 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
724 LAST_SKIP_BITS(re, &s->gb, run);
731 CLOSE_READER(re, &s->gb);
740 #define REFINE_BIT(j) { \
741 UPDATE_CACHE(re, &s->gb); \
742 sign = block[j] >> 15; \
743 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
744 ((quant_matrix[j] ^ sign) - sign) << Al; \
745 LAST_SKIP_BITS(re, &s->gb, 1); \
753 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
758 j = s->scantable.permutated[i]; \
761 else if (run-- == 0) \
765 /* decode block and dequantize - progressive JPEG refinement pass */
766 static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
768 int ac_index, int16_t *quant_matrix,
769 int ss, int se, int Al, int *EOBRUN)
771 int code, i = ss, j, sign, val, run;
772 int last = FFMIN(se, *last_nnz);
774 OPEN_READER(re, &s->gb);
779 UPDATE_CACHE(re, &s->gb);
780 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
783 run = ((unsigned) code) >> 4;
784 UPDATE_CACHE(re, &s->gb);
785 val = SHOW_UBITS(re, &s->gb, 1);
786 LAST_SKIP_BITS(re, &s->gb, 1);
788 j = s->scantable.permutated[i];
790 block[j] = ((quant_matrix[j]^val) - val) << Al;
794 CLOSE_READER(re, &s->gb);
798 run = ((unsigned) code) >> 4;
805 UPDATE_CACHE(re, &s->gb);
806 run += SHOW_UBITS(re, &s->gb, val);
807 LAST_SKIP_BITS(re, &s->gb, val);
819 for (; i <= last; i++) {
820 j = s->scantable.permutated[i];
824 CLOSE_READER(re, &s->gb);
831 static int handle_rstn(MJpegDecodeContext *s, int nb_components)
836 if (s->restart_interval) {
838 if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
839 align_get_bits(&s->gb);
840 for (i = 0; i < nb_components; i++) /* reset dc */
841 s->last_dc[i] = (4 << s->bits);
844 i = 8 + ((-get_bits_count(&s->gb)) & 7);
846 if (s->restart_count == 0) {
847 if( show_bits(&s->gb, i) == (1 << i) - 1
848 || show_bits(&s->gb, i) == 0xFF) {
849 int pos = get_bits_count(&s->gb);
850 align_get_bits(&s->gb);
851 while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
852 skip_bits(&s->gb, 8);
853 if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
854 for (i = 0; i < nb_components; i++) /* reset dc */
855 s->last_dc[i] = (4 << s->bits);
858 skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
865 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
868 uint16_t (*buffer)[4];
869 int left[4], top[4], topleft[4];
870 const int linesize = s->linesize[0];
871 const int mask = ((1 << s->bits) - 1) << point_transform;
875 if (s->nb_components != 3 && s->nb_components != 4)
876 return AVERROR_INVALIDDATA;
877 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
878 return AVERROR_INVALIDDATA;
881 s->restart_count = s->restart_interval;
883 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
884 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
885 buffer = s->ljpeg_buffer;
887 for (i = 0; i < 4; i++)
888 buffer[0][i] = 1 << (s->bits - 1);
890 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
891 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
893 if (s->interlaced && s->bottom_field)
894 ptr += linesize >> 1;
896 for (i = 0; i < 4; i++)
897 top[i] = left[i] = topleft[i] = buffer[0][i];
899 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
900 int modified_predictor = predictor;
902 if (s->restart_interval && !s->restart_count){
903 s->restart_count = s->restart_interval;
907 top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
909 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
910 modified_predictor = 1;
912 for (i=0;i<nb_components;i++) {
916 top[i] = buffer[mb_x][i];
918 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
920 dc = mjpeg_decode_dc(s, s->dc_index[i]);
924 left[i] = buffer[mb_x][i] =
925 mask & (pred + (dc << point_transform));
928 if (s->restart_interval && !--s->restart_count) {
929 align_get_bits(&s->gb);
930 skip_bits(&s->gb, 16); /* skip RSTn */
933 if (s->nb_components == 4) {
934 for(i=0; i<nb_components; i++) {
935 int c= s->comp_index[i];
937 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
938 ptr[4*mb_x+3-c] = buffer[mb_x][i];
940 } else if(s->bits == 9) {
941 return AVERROR_PATCHWELCOME;
943 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
944 ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i];
949 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
950 ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
951 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
952 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
954 } else if (s->pegasus_rct) {
955 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
956 ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
957 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
958 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
961 for(i=0; i<nb_components; i++) {
962 int c= s->comp_index[i];
964 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
965 ptr[3*mb_x+2-c] = buffer[mb_x][i];
967 } else if(s->bits == 9) {
968 return AVERROR_PATCHWELCOME;
970 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
971 ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i];
980 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
981 int point_transform, int nb_components)
983 int i, mb_x, mb_y, mask;
984 int bits= (s->bits+7)&~7;
988 point_transform += bits - s->bits;
989 mask = ((1 << s->bits) - 1) << point_transform;
991 av_assert0(nb_components>=1 && nb_components<=4);
993 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
994 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
995 if (s->restart_interval && !s->restart_count){
996 s->restart_count = s->restart_interval;
1001 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
1002 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1003 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1004 for (i = 0; i < nb_components; i++) {
1007 int n, h, v, x, y, c, j, linesize;
1008 n = s->nb_blocks[i];
1009 c = s->comp_index[i];
1014 linesize= s->linesize[c];
1016 if(bits>8) linesize /= 2;
1018 for(j=0; j<n; j++) {
1021 dc = mjpeg_decode_dc(s, s->dc_index[i]);
1025 ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
1027 if(x==0 && leftcol){
1028 pred= 1 << (bits - 1);
1033 if(x==0 && leftcol){
1034 pred= ptr[-linesize];
1036 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1040 if (s->interlaced && s->bottom_field)
1041 ptr += linesize >> 1;
1043 *ptr= pred + (dc << point_transform);
1045 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1047 if(x==0 && leftcol){
1048 pred= 1 << (bits - 1);
1053 if(x==0 && leftcol){
1054 pred= ptr16[-linesize];
1056 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1060 if (s->interlaced && s->bottom_field)
1061 ptr16 += linesize >> 1;
1063 *ptr16= pred + (dc << point_transform);
1072 for (i = 0; i < nb_components; i++) {
1075 int n, h, v, x, y, c, j, linesize, dc;
1076 n = s->nb_blocks[i];
1077 c = s->comp_index[i];
1082 linesize = s->linesize[c];
1084 if(bits>8) linesize /= 2;
1086 for (j = 0; j < n; j++) {
1089 dc = mjpeg_decode_dc(s, s->dc_index[i]);
1093 ptr = s->picture_ptr->data[c] +
1094 (linesize * (v * mb_y + y)) +
1095 (h * mb_x + x); //FIXME optimize this crap
1096 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1099 *ptr = pred + (dc << point_transform);
1101 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1102 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1105 *ptr16= pred + (dc << point_transform);
1115 if (s->restart_interval && !--s->restart_count) {
1116 align_get_bits(&s->gb);
1117 skip_bits(&s->gb, 16); /* skip RSTn */
1124 static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s,
1125 uint8_t *dst, const uint8_t *src,
1126 int linesize, int lowres)
1129 case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
1131 case 1: copy_block4(dst, src, linesize, linesize, 4);
1133 case 2: copy_block2(dst, src, linesize, linesize, 2);
1135 case 3: *dst = *src;
1140 static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1142 int block_x, block_y;
1143 int size = 8 >> s->avctx->lowres;
1145 for (block_y=0; block_y<size; block_y++)
1146 for (block_x=0; block_x<size; block_x++)
1147 *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits;
1149 for (block_y=0; block_y<size; block_y++)
1150 for (block_x=0; block_x<size; block_x++)
1151 *(ptr + block_x + block_y*linesize) <<= 8 - s->bits;
1155 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1156 int Al, const uint8_t *mb_bitmask,
1157 int mb_bitmask_size,
1158 const AVFrame *reference)
1161 uint8_t *data[MAX_COMPONENTS];
1162 const uint8_t *reference_data[MAX_COMPONENTS];
1163 int linesize[MAX_COMPONENTS];
1164 GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1165 int bytes_per_pixel = 1 + (s->bits > 8);
1168 if (mb_bitmask_size != (s->mb_width * s->mb_height + 7)>>3) {
1169 av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1170 return AVERROR_INVALIDDATA;
1172 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1175 s->restart_count = 0;
1177 for (i = 0; i < nb_components; i++) {
1178 int c = s->comp_index[i];
1179 data[c] = s->picture_ptr->data[c];
1180 reference_data[c] = reference ? reference->data[c] : NULL;
1181 linesize[c] = s->linesize[c];
1182 s->coefs_finished[c] |= 1;
1185 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1186 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1187 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1189 if (s->restart_interval && !s->restart_count)
1190 s->restart_count = s->restart_interval;
1192 if (get_bits_left(&s->gb) < 0) {
1193 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1194 -get_bits_left(&s->gb));
1195 return AVERROR_INVALIDDATA;
1197 for (i = 0; i < nb_components; i++) {
1199 int n, h, v, x, y, c, j;
1201 n = s->nb_blocks[i];
1202 c = s->comp_index[i];
1207 for (j = 0; j < n; j++) {
1208 block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1209 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1211 if (s->interlaced && s->bottom_field)
1212 block_offset += linesize[c] >> 1;
1213 ptr = data[c] + block_offset;
1214 if (!s->progressive) {
1216 mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1217 linesize[c], s->avctx->lowres);
1220 s->dsp.clear_block(s->block);
1221 if (decode_block(s, s->block, i,
1222 s->dc_index[i], s->ac_index[i],
1223 s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1224 av_log(s->avctx, AV_LOG_ERROR,
1225 "error y=%d x=%d\n", mb_y, mb_x);
1226 return AVERROR_INVALIDDATA;
1228 s->dsp.idct_put(ptr, linesize[c], s->block);
1230 shift_output(s, ptr, linesize[c]);
1233 int block_idx = s->block_stride[c] * (v * mb_y + y) +
1235 int16_t *block = s->blocks[c][block_idx];
1237 block[0] += get_bits1(&s->gb) *
1238 s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1239 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1240 s->quant_matrixes[s->quant_sindex[i]],
1242 av_log(s->avctx, AV_LOG_ERROR,
1243 "error y=%d x=%d\n", mb_y, mb_x);
1244 return AVERROR_INVALIDDATA;
1247 av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1248 av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1249 mb_x, mb_y, x, y, c, s->bottom_field,
1250 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1258 handle_rstn(s, nb_components);
1264 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
1265 int se, int Ah, int Al)
1269 int c = s->comp_index[0];
1270 uint8_t *data = s->picture_ptr->data[c];
1271 int linesize = s->linesize[c];
1273 int16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1274 int bytes_per_pixel = 1 + (s->bits > 8);
1276 av_assert0(ss>=0 && Ah>=0 && Al>=0);
1277 if (se < ss || se > 63) {
1278 av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se);
1279 return AVERROR_INVALIDDATA;
1283 s->coefs_finished[c] |= (2ULL << se) - (1ULL << ss);
1284 last_scan = !~s->coefs_finished[c];
1287 if (s->interlaced && s->bottom_field)
1288 data += linesize >> 1;
1290 s->restart_count = 0;
1292 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1293 uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1294 int block_idx = mb_y * s->block_stride[c];
1295 int16_t (*block)[64] = &s->blocks[c][block_idx];
1296 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1297 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1299 if (s->restart_interval && !s->restart_count)
1300 s->restart_count = s->restart_interval;
1303 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1304 quant_matrix, ss, se, Al, &EOBRUN);
1306 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1307 quant_matrix, ss, se, Al, &EOBRUN);
1309 av_log(s->avctx, AV_LOG_ERROR,
1310 "error y=%d x=%d\n", mb_y, mb_x);
1311 return AVERROR_INVALIDDATA;
1315 s->dsp.idct_put(ptr, linesize, *block);
1317 shift_output(s, ptr, linesize);
1318 ptr += bytes_per_pixel*8 >> s->avctx->lowres;
1320 if (handle_rstn(s, 0))
1327 int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
1328 int mb_bitmask_size, const AVFrame *reference)
1330 int len, nb_components, i, h, v, predictor, point_transform;
1332 const int block_size = s->lossless ? 1 : 8;
1333 int ilv, prev_shift;
1335 if (!s->got_picture) {
1336 av_log(s->avctx, AV_LOG_WARNING,
1337 "Can not process SOS before SOF, skipping\n");
1341 av_assert0(s->picture_ptr->data[0]);
1342 /* XXX: verify len field validity */
1343 len = get_bits(&s->gb, 16);
1344 nb_components = get_bits(&s->gb, 8);
1345 if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1346 av_log(s->avctx, AV_LOG_ERROR,
1347 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1348 return AVERROR_PATCHWELCOME;
1350 if (len != 6 + 2 * nb_components) {
1351 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1352 return AVERROR_INVALIDDATA;
1354 for (i = 0; i < nb_components; i++) {
1355 id = get_bits(&s->gb, 8) - 1;
1356 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1357 /* find component index */
1358 for (index = 0; index < s->nb_components; index++)
1359 if (id == s->component_id[index])
1361 if (index == s->nb_components) {
1362 av_log(s->avctx, AV_LOG_ERROR,
1363 "decode_sos: index(%d) out of components\n", index);
1364 return AVERROR_INVALIDDATA;
1366 /* Metasoft MJPEG codec has Cb and Cr swapped */
1367 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1368 && nb_components == 3 && s->nb_components == 3 && i)
1371 s->quant_sindex[i] = s->quant_index[index];
1372 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1373 s->h_scount[i] = s->h_count[index];
1374 s->v_scount[i] = s->v_count[index];
1376 if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1378 if(nb_components == 1 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1379 index = (index+2)%3;
1381 s->comp_index[i] = index;
1383 s->dc_index[i] = get_bits(&s->gb, 4);
1384 s->ac_index[i] = get_bits(&s->gb, 4);
1386 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1387 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1389 if (!s->vlcs[0][s->dc_index[i]].table || !(s->progressive ? s->vlcs[2][s->ac_index[0]].table : s->vlcs[1][s->ac_index[i]].table))
1393 predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1394 ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1395 if(s->avctx->codec_tag != AV_RL32("CJPG")){
1396 prev_shift = get_bits(&s->gb, 4); /* Ah */
1397 point_transform = get_bits(&s->gb, 4); /* Al */
1399 prev_shift = point_transform = 0;
1401 if (nb_components > 1) {
1402 /* interleaved stream */
1403 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1404 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1405 } else if (!s->ls) { /* skip this for JPEG-LS */
1406 h = s->h_max / s->h_scount[0];
1407 v = s->v_max / s->v_scount[0];
1408 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1409 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1410 s->nb_blocks[0] = 1;
1415 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1416 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1417 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1418 predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1419 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1422 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1423 for (i = s->mjpb_skiptosod; i > 0; i--)
1424 skip_bits(&s->gb, 8);
1427 for (i = 0; i < nb_components; i++)
1428 s->last_dc[i] = (4 << s->bits);
1431 av_assert0(s->picture_ptr == s->picture);
1432 if (CONFIG_JPEGLS_DECODER && s->ls) {
1434 // reset_ls_coding_parameters(s, 0);
1436 if ((ret = ff_jpegls_decode_picture(s, predictor,
1437 point_transform, ilv)) < 0)
1441 if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1444 if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1446 nb_components)) < 0)
1451 if (s->progressive && predictor) {
1452 av_assert0(s->picture_ptr == s->picture);
1453 if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1455 point_transform)) < 0)
1458 if ((ret = mjpeg_decode_scan(s, nb_components,
1459 prev_shift, point_transform,
1460 mb_bitmask, mb_bitmask_size, reference)) < 0)
1465 if (s->interlaced &&
1466 get_bits_left(&s->gb) > 32 &&
1467 show_bits(&s->gb, 8) == 0xFF) {
1468 GetBitContext bak = s->gb;
1469 align_get_bits(&bak);
1470 if (show_bits(&bak, 16) == 0xFFD1) {
1471 av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1473 skip_bits(&s->gb, 16);
1474 s->bottom_field ^= 1;
1483 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1484 return AVERROR_INVALIDDATA;
1487 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1489 if (get_bits(&s->gb, 16) != 4)
1490 return AVERROR_INVALIDDATA;
1491 s->restart_interval = get_bits(&s->gb, 16);
1492 s->restart_count = 0;
1493 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1494 s->restart_interval);
1499 static int mjpeg_decode_app(MJpegDecodeContext *s)
1503 len = get_bits(&s->gb, 16);
1505 return AVERROR_INVALIDDATA;
1506 if (8 * len > get_bits_left(&s->gb))
1507 return AVERROR_INVALIDDATA;
1509 id = get_bits_long(&s->gb, 32);
1512 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
1514 av_get_codec_tag_string(id_str, sizeof(id_str), av_bswap32(id));
1515 av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n", id_str, id, len);
1518 /* Buggy AVID, it puts EOI only at every 10th frame. */
1519 /* Also, this fourcc is used by non-avid files too, it holds some
1520 information, but it's always present in AVID-created files. */
1521 if (id == AV_RB32("AVI1")) {
1527 4bytes field_size_less_padding
1530 i = get_bits(&s->gb, 8); len--;
1531 av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1533 skip_bits(&s->gb, 8);
1534 skip_bits(&s->gb, 32);
1535 skip_bits(&s->gb, 32);
1543 if (id == AV_RB32("JFIF")) {
1544 int t_w, t_h, v1, v2;
1545 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1546 v1 = get_bits(&s->gb, 8);
1547 v2 = get_bits(&s->gb, 8);
1548 skip_bits(&s->gb, 8);
1550 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1551 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1553 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1554 av_log(s->avctx, AV_LOG_INFO,
1555 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1557 s->avctx->sample_aspect_ratio.num,
1558 s->avctx->sample_aspect_ratio.den);
1560 t_w = get_bits(&s->gb, 8);
1561 t_h = get_bits(&s->gb, 8);
1563 /* skip thumbnail */
1564 if (len -10 - (t_w * t_h * 3) > 0)
1565 len -= t_w * t_h * 3;
1571 if (id == AV_RB32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1572 skip_bits(&s->gb, 16); /* version */
1573 skip_bits(&s->gb, 16); /* flags0 */
1574 skip_bits(&s->gb, 16); /* flags1 */
1575 s->adobe_transform = get_bits(&s->gb, 8);
1576 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1577 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1582 if (id == AV_RB32("LJIF")) {
1583 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1584 av_log(s->avctx, AV_LOG_INFO,
1585 "Pegasus lossless jpeg header found\n");
1586 skip_bits(&s->gb, 16); /* version ? */
1587 skip_bits(&s->gb, 16); /* unknown always 0? */
1588 skip_bits(&s->gb, 16); /* unknown always 0? */
1589 skip_bits(&s->gb, 16); /* unknown always 0? */
1590 switch (i=get_bits(&s->gb, 8)) {
1600 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1605 if (id == AV_RL32("colr") && len > 0) {
1606 s->colr = get_bits(&s->gb, 8);
1607 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1608 av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1612 if (id == AV_RL32("xfrm") && len > 0) {
1613 s->xfrm = get_bits(&s->gb, 8);
1614 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1615 av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1620 /* JPS extension by VRex */
1621 if (s->start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1622 int flags, layout, type;
1623 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1624 av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
1626 skip_bits(&s->gb, 32); len -= 4; /* JPS_ */
1627 skip_bits(&s->gb, 16); len -= 2; /* block length */
1628 skip_bits(&s->gb, 8); /* reserved */
1629 flags = get_bits(&s->gb, 8);
1630 layout = get_bits(&s->gb, 8);
1631 type = get_bits(&s->gb, 8);
1634 s->stereo3d = av_stereo3d_alloc();
1639 s->stereo3d->type = AV_STEREO3D_2D;
1640 } else if (type == 1) {
1643 s->stereo3d->type = AV_STEREO3D_LINES;
1646 s->stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1649 s->stereo3d->type = AV_STEREO3D_TOPBOTTOM;
1652 if (!(flags & 0x04)) {
1653 s->stereo3d->flags = AV_STEREO3D_FLAG_INVERT;
1660 if (s->start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
1661 GetByteContext gbytes;
1662 int ret, le, ifd_offset, bytes_read;
1663 const uint8_t *aligned;
1665 skip_bits(&s->gb, 16); // skip padding
1668 // init byte wise reading
1669 aligned = align_get_bits(&s->gb);
1670 bytestream2_init(&gbytes, aligned, len);
1673 ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
1675 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
1677 bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
1679 // read 0th IFD and store the metadata
1680 // (return values > 0 indicate the presence of subimage metadata)
1681 ret = avpriv_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata);
1683 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n");
1687 bytes_read = bytestream2_tell(&gbytes);
1688 skip_bits(&s->gb, bytes_read << 3);
1695 if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1696 id = get_bits_long(&s->gb, 32);
1699 if (id == AV_RB32("mjpg")) {
1701 skip_bits(&s->gb, 32); /* field size */
1702 skip_bits(&s->gb, 32); /* pad field size */
1703 skip_bits(&s->gb, 32); /* next off */
1704 skip_bits(&s->gb, 32); /* quant off */
1705 skip_bits(&s->gb, 32); /* huff off */
1706 skip_bits(&s->gb, 32); /* image off */
1707 skip_bits(&s->gb, 32); /* scan off */
1708 skip_bits(&s->gb, 32); /* data off */
1710 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1711 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1716 /* slow but needed for extreme adobe jpegs */
1718 av_log(s->avctx, AV_LOG_ERROR,
1719 "mjpeg: error, decode_app parser read over the end\n");
1721 skip_bits(&s->gb, 8);
1726 static int mjpeg_decode_com(MJpegDecodeContext *s)
1728 int len = get_bits(&s->gb, 16);
1729 if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1730 char *cbuf = av_malloc(len - 1);
1733 for (i = 0; i < len - 2; i++)
1734 cbuf[i] = get_bits(&s->gb, 8);
1735 if (i > 0 && cbuf[i - 1] == '\n')
1740 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1741 av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
1743 /* buggy avid, it puts EOI only at every 10th frame */
1744 if (!strncmp(cbuf, "AVID", 4)) {
1745 parse_avid(s, cbuf, len);
1746 } else if (!strcmp(cbuf, "CS=ITU601"))
1748 else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
1749 (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1759 /* return the 8 bit start code value and update the search
1760 state. Return -1 if no start code found */
1761 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1763 const uint8_t *buf_ptr;
1768 buf_ptr = *pbuf_ptr;
1769 while (buf_end - buf_ptr > 1) {
1772 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1781 av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1782 *pbuf_ptr = buf_ptr;
1786 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1787 const uint8_t **buf_ptr, const uint8_t *buf_end,
1788 const uint8_t **unescaped_buf_ptr,
1789 int *unescaped_buf_size)
1792 start_code = find_marker(buf_ptr, buf_end);
1794 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1796 return AVERROR(ENOMEM);
1798 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1799 if (start_code == SOS && !s->ls) {
1800 const uint8_t *src = *buf_ptr;
1801 uint8_t *dst = s->buffer;
1803 while (src < buf_end) {
1804 uint8_t x = *(src++);
1807 if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1809 while (src < buf_end && x == 0xff)
1812 if (x >= 0xd0 && x <= 0xd7)
1819 *unescaped_buf_ptr = s->buffer;
1820 *unescaped_buf_size = dst - s->buffer;
1821 memset(s->buffer + *unescaped_buf_size, 0,
1822 FF_INPUT_BUFFER_PADDING_SIZE);
1824 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %"PTRDIFF_SPECIFIER" bytes\n",
1825 (buf_end - *buf_ptr) - (dst - s->buffer));
1826 } else if (start_code == SOS && s->ls) {
1827 const uint8_t *src = *buf_ptr;
1828 uint8_t *dst = s->buffer;
1834 while (src + t < buf_end) {
1835 uint8_t x = src[t++];
1837 while ((src + t < buf_end) && x == 0xff)
1846 init_put_bits(&pb, dst, t);
1848 /* unescape bitstream */
1850 uint8_t x = src[b++];
1851 put_bits(&pb, 8, x);
1854 put_bits(&pb, 7, x);
1858 flush_put_bits(&pb);
1860 *unescaped_buf_ptr = dst;
1861 *unescaped_buf_size = (bit_count + 7) >> 3;
1862 memset(s->buffer + *unescaped_buf_size, 0,
1863 FF_INPUT_BUFFER_PADDING_SIZE);
1865 *unescaped_buf_ptr = *buf_ptr;
1866 *unescaped_buf_size = buf_end - *buf_ptr;
1872 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1875 AVFrame *frame = data;
1876 const uint8_t *buf = avpkt->data;
1877 int buf_size = avpkt->size;
1878 MJpegDecodeContext *s = avctx->priv_data;
1879 const uint8_t *buf_end, *buf_ptr;
1880 const uint8_t *unescaped_buf_ptr;
1882 int unescaped_buf_size;
1887 av_dict_free(&s->exif_metadata);
1888 av_freep(&s->stereo3d);
1889 s->adobe_transform = -1;
1892 buf_end = buf + buf_size;
1893 while (buf_ptr < buf_end) {
1894 /* find start next marker */
1895 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1897 &unescaped_buf_size);
1899 if (start_code < 0) {
1901 } else if (unescaped_buf_size > INT_MAX / 8) {
1902 av_log(avctx, AV_LOG_ERROR,
1903 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1904 start_code, unescaped_buf_size, buf_size);
1905 return AVERROR_INVALIDDATA;
1907 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
1908 start_code, buf_end - buf_ptr);
1910 ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
1913 av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
1917 s->start_code = start_code;
1918 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1919 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1921 /* process markers */
1922 if (start_code >= 0xd0 && start_code <= 0xd7)
1923 av_log(avctx, AV_LOG_DEBUG,
1924 "restart marker: %d\n", start_code & 0x0f);
1926 else if (start_code >= APP0 && start_code <= APP15)
1927 mjpeg_decode_app(s);
1929 else if (start_code == COM)
1930 mjpeg_decode_com(s);
1934 if (!CONFIG_JPEGLS_DECODER &&
1935 (start_code == SOF48 || start_code == LSE)) {
1936 av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
1937 return AVERROR(ENOSYS);
1940 switch (start_code) {
1942 s->restart_interval = 0;
1943 s->restart_count = 0;
1944 /* nothing to do on SOI */
1947 ff_mjpeg_decode_dqt(s);
1950 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1951 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1960 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1967 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1974 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1981 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1985 if (!CONFIG_JPEGLS_DECODER ||
1986 (ret = ff_jpegls_decode_lse(s)) < 0)
1992 if (!s->got_picture) {
1993 av_log(avctx, AV_LOG_WARNING,
1994 "Found EOI before any SOF, ignoring\n");
1997 if (s->interlaced) {
1998 s->bottom_field ^= 1;
1999 /* if not bottom field, do not output image yet */
2000 if (s->bottom_field == !s->interlace_polarity)
2003 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2009 int qp = FFMAX3(s->qscale[0],
2012 int qpw = (s->width + 15) / 16;
2013 AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
2015 memset(qp_table_buf->data, qp, qpw);
2016 av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
2019 if(avctx->debug & FF_DEBUG_QP)
2020 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2026 if ((ret = ff_mjpeg_decode_sos(s, NULL, 0, NULL)) < 0 &&
2027 (avctx->err_recognition & AV_EF_EXPLODE))
2031 mjpeg_decode_dri(s);
2043 av_log(avctx, AV_LOG_ERROR,
2044 "mjpeg: unsupported coding type (%x)\n", start_code);
2048 /* eof process start code */
2049 buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
2050 av_log(avctx, AV_LOG_DEBUG,
2051 "marker parser used %d bytes (%d bits)\n",
2052 (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
2054 if (s->got_picture && s->cur_scan) {
2055 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2058 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2059 return AVERROR_INVALIDDATA;
2066 av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2067 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2068 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2069 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2070 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2071 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2072 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2073 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2075 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2076 for (p = 1; p<4; p++) {
2077 uint8_t *line = s->picture_ptr->data[p];
2079 if (!(s->upscale_h & (1<<p)))
2081 w = s->width >> hshift;
2082 for (i = 0; i < s->chroma_height; i++) {
2083 for (index = w - 1; index; index--)
2084 line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2085 line += s->linesize[p];
2091 av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2092 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2093 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2094 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2095 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2096 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2098 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2099 for (p = 1; p < 4; p++) {
2100 uint8_t *dst = &((uint8_t *)s->picture_ptr->data[p])[(s->height - 1) * s->linesize[p]];
2102 if (!(s->upscale_v & (1<<p)))
2104 w = s->width >> hshift;
2105 for (i = s->height - 1; i; i--) {
2106 uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i / 2 * s->linesize[p]];
2107 uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) / 2 * s->linesize[p]];
2109 memcpy(dst, src1, w);
2111 for (index = 0; index < w; index++)
2112 dst[index] = (src1[index] + src2[index]) >> 1;
2114 dst -= s->linesize[p];
2120 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2121 for (index=0; index<4; index++) {
2122 uint8_t *dst = s->picture_ptr->data[index];
2123 int w = s->picture_ptr->width;
2124 int h = s->picture_ptr->height;
2125 if(index && index<3){
2126 w = FF_CEIL_RSHIFT(w, hshift);
2127 h = FF_CEIL_RSHIFT(h, vshift);
2130 uint8_t *dst2 = dst + s->picture_ptr->linesize[index]*(h-1);
2131 for (i=0; i<h/2; i++) {
2133 FFSWAP(int, dst[j], dst2[j]);
2134 dst += s->picture_ptr->linesize[index];
2135 dst2 -= s->picture_ptr->linesize[index];
2140 if (s->adobe_transform == 0 && s->avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2141 int w = s->picture_ptr->width;
2142 int h = s->picture_ptr->height;
2143 for (i=0; i<h; i++) {
2146 for (index=0; index<4; index++) {
2147 dst[index] = s->picture_ptr->data[index]
2148 + s->picture_ptr->linesize[index]*i;
2150 for (j=0; j<w; j++) {
2152 int r = dst[0][j] * k;
2153 int g = dst[1][j] * k;
2154 int b = dst[2][j] * k;
2155 dst[0][j] = g*257 >> 16;
2156 dst[1][j] = b*257 >> 16;
2157 dst[2][j] = r*257 >> 16;
2162 if (s->adobe_transform == 2 && s->avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2163 int w = s->picture_ptr->width;
2164 int h = s->picture_ptr->height;
2165 for (i=0; i<h; i++) {
2168 for (index=0; index<4; index++) {
2169 dst[index] = s->picture_ptr->data[index]
2170 + s->picture_ptr->linesize[index]*i;
2172 for (j=0; j<w; j++) {
2174 int r = (255 - dst[0][j]) * k;
2175 int g = (128 - dst[1][j]) * k;
2176 int b = (128 - dst[2][j]) * k;
2177 dst[0][j] = r*257 >> 16;
2178 dst[1][j] = (g*257 >> 16) + 128;
2179 dst[2][j] = (b*257 >> 16) + 128;
2186 AVStereo3D *stereo = av_stereo3d_create_side_data(data);
2188 stereo->type = s->stereo3d->type;
2189 stereo->flags = s->stereo3d->flags;
2191 av_freep(&s->stereo3d);
2194 av_dict_copy(avpriv_frame_get_metadatap(data), s->exif_metadata, 0);
2195 av_dict_free(&s->exif_metadata);
2197 av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n",
2199 // return buf_end - buf_ptr;
2200 return buf_ptr - buf;
2203 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
2205 MJpegDecodeContext *s = avctx->priv_data;
2208 if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) {
2209 av_log(avctx, AV_LOG_INFO, "Single field\n");
2213 av_frame_free(&s->picture);
2214 s->picture_ptr = NULL;
2215 } else if (s->picture_ptr)
2216 av_frame_unref(s->picture_ptr);
2218 av_freep(&s->buffer);
2219 av_freep(&s->stereo3d);
2220 av_freep(&s->ljpeg_buffer);
2221 s->ljpeg_buffer_size = 0;
2223 for (i = 0; i < 3; i++) {
2224 for (j = 0; j < 4; j++)
2225 ff_free_vlc(&s->vlcs[i][j]);
2227 for (i = 0; i < MAX_COMPONENTS; i++) {
2228 av_freep(&s->blocks[i]);
2229 av_freep(&s->last_nnz[i]);
2231 av_dict_free(&s->exif_metadata);
2235 static void decode_flush(AVCodecContext *avctx)
2237 MJpegDecodeContext *s = avctx->priv_data;
2241 #if CONFIG_MJPEG_DECODER
2242 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2243 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2244 static const AVOption options[] = {
2245 { "extern_huff", "Use external huffman table.",
2246 OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
2250 static const AVClass mjpegdec_class = {
2251 .class_name = "MJPEG decoder",
2252 .item_name = av_default_item_name,
2254 .version = LIBAVUTIL_VERSION_INT,
2257 AVCodec ff_mjpeg_decoder = {
2259 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
2260 .type = AVMEDIA_TYPE_VIDEO,
2261 .id = AV_CODEC_ID_MJPEG,
2262 .priv_data_size = sizeof(MJpegDecodeContext),
2263 .init = ff_mjpeg_decode_init,
2264 .close = ff_mjpeg_decode_end,
2265 .decode = ff_mjpeg_decode_frame,
2266 .flush = decode_flush,
2267 .capabilities = CODEC_CAP_DR1,
2269 .priv_class = &mjpegdec_class,
2272 #if CONFIG_THP_DECODER
2273 AVCodec ff_thp_decoder = {
2275 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
2276 .type = AVMEDIA_TYPE_VIDEO,
2277 .id = AV_CODEC_ID_THP,
2278 .priv_data_size = sizeof(MJpegDecodeContext),
2279 .init = ff_mjpeg_decode_init,
2280 .close = ff_mjpeg_decode_end,
2281 .decode = ff_mjpeg_decode_frame,
2282 .flush = decode_flush,
2283 .capabilities = CODEC_CAP_DR1,