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"
38 #include "copy_block.h"
42 #include "jpeglsdec.h"
45 #include "bytestream.h"
48 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
49 const uint8_t *val_table, int nb_codes,
50 int use_static, int is_ac)
52 uint8_t huff_size[256] = { 0 };
53 uint16_t huff_code[256];
54 uint16_t huff_sym[256];
57 av_assert0(nb_codes <= 256);
59 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
61 for (i = 0; i < 256; i++)
62 huff_sym[i] = i + 16 * is_ac;
65 huff_sym[0] = 16 * 256;
67 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
68 huff_code, 2, 2, huff_sym, 2, 2, use_static);
71 static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
73 build_vlc(&s->vlcs[0][0], avpriv_mjpeg_bits_dc_luminance,
74 avpriv_mjpeg_val_dc, 12, 0, 0);
75 build_vlc(&s->vlcs[0][1], avpriv_mjpeg_bits_dc_chrominance,
76 avpriv_mjpeg_val_dc, 12, 0, 0);
77 build_vlc(&s->vlcs[1][0], avpriv_mjpeg_bits_ac_luminance,
78 avpriv_mjpeg_val_ac_luminance, 251, 0, 1);
79 build_vlc(&s->vlcs[1][1], avpriv_mjpeg_bits_ac_chrominance,
80 avpriv_mjpeg_val_ac_chrominance, 251, 0, 1);
81 build_vlc(&s->vlcs[2][0], avpriv_mjpeg_bits_ac_luminance,
82 avpriv_mjpeg_val_ac_luminance, 251, 0, 0);
83 build_vlc(&s->vlcs[2][1], avpriv_mjpeg_bits_ac_chrominance,
84 avpriv_mjpeg_val_ac_chrominance, 251, 0, 0);
87 static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
90 if (len > 14 && buf[12] == 1) /* 1 - NTSC */
91 s->interlace_polarity = 1;
92 if (len > 14 && buf[12] == 2) /* 2 - PAL */
93 s->interlace_polarity = 0;
94 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
95 av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 14 ? buf[12] : -1);
98 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
100 MJpegDecodeContext *s = avctx->priv_data;
102 if (!s->picture_ptr) {
103 s->picture = av_frame_alloc();
105 return AVERROR(ENOMEM);
106 s->picture_ptr = s->picture;
110 ff_blockdsp_init(&s->bdsp, avctx);
111 ff_hpeldsp_init(&s->hdsp, avctx->flags);
112 ff_dsputil_init(&s->dsp, avctx);
113 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
117 s->first_picture = 1;
119 s->org_height = avctx->coded_height;
120 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
122 build_basic_mjpeg_vlc(s);
124 if (s->extern_huff) {
125 av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
126 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
127 if (ff_mjpeg_decode_dht(s)) {
128 av_log(avctx, AV_LOG_ERROR,
129 "error using external huffman table, switching back to internal\n");
130 build_basic_mjpeg_vlc(s);
133 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
134 s->interlace_polarity = 1; /* bottom field first */
135 av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
136 } else if (avctx->field_order == AV_FIELD_UNKNOWN) {
137 if (avctx->codec_tag == AV_RL32("MJPG"))
138 s->interlace_polarity = 1;
141 if ( avctx->extradata_size > 8
142 && AV_RL32(avctx->extradata) == 0x2C
143 && AV_RL32(avctx->extradata+4) == 0x18) {
144 parse_avid(s, avctx->extradata, avctx->extradata_size);
147 if (avctx->codec->id == AV_CODEC_ID_AMV)
154 /* quantize tables */
155 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
157 int len, index, i, j;
159 len = get_bits(&s->gb, 16) - 2;
162 int pr = get_bits(&s->gb, 4);
164 av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
165 return AVERROR_INVALIDDATA;
167 index = get_bits(&s->gb, 4);
170 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
171 /* read quant table */
172 for (i = 0; i < 64; i++) {
173 j = s->scantable.permutated[i];
174 s->quant_matrixes[index][j] = get_bits(&s->gb, pr ? 16 : 8);
177 // XXX FIXME finetune, and perhaps add dc too
178 s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
179 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
180 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
181 index, s->qscale[index]);
187 /* decode huffman tables and build VLC decoders */
188 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
190 int len, index, i, class, n, v, code_max;
191 uint8_t bits_table[17];
192 uint8_t val_table[256];
195 len = get_bits(&s->gb, 16) - 2;
199 return AVERROR_INVALIDDATA;
200 class = get_bits(&s->gb, 4);
202 return AVERROR_INVALIDDATA;
203 index = get_bits(&s->gb, 4);
205 return AVERROR_INVALIDDATA;
207 for (i = 1; i <= 16; i++) {
208 bits_table[i] = get_bits(&s->gb, 8);
212 if (len < n || n > 256)
213 return AVERROR_INVALIDDATA;
216 for (i = 0; i < n; i++) {
217 v = get_bits(&s->gb, 8);
224 /* build VLC and flush previous vlc if present */
225 ff_free_vlc(&s->vlcs[class][index]);
226 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
227 class, index, code_max + 1);
228 if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
229 code_max + 1, 0, class > 0)) < 0)
233 ff_free_vlc(&s->vlcs[2][index]);
234 if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
235 code_max + 1, 0, 0)) < 0)
242 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
244 int len, nb_components, i, width, height, pix_fmt_id, ret;
245 int h_count[MAX_COMPONENTS];
246 int v_count[MAX_COMPONENTS];
249 s->upscale_h = s->upscale_v = 0;
251 /* XXX: verify len field validity */
252 len = get_bits(&s->gb, 16);
253 s->avctx->bits_per_raw_sample =
254 s->bits = get_bits(&s->gb, 8);
258 if (s->bits == 9 && !s->pegasus_rct)
259 s->rct = 1; // FIXME ugly
261 if(s->lossless && s->avctx->lowres){
262 av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
266 height = get_bits(&s->gb, 16);
267 width = get_bits(&s->gb, 16);
269 if (s->avctx->codec_id == AV_CODEC_ID_AMV && (height&15))
270 avpriv_request_sample(s->avctx, "non mod 16 height AMV\n");
272 // HACK for odd_height.mov
273 if (s->interlaced && s->width == width && s->height == height + 1)
276 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
277 if (av_image_check_size(width, height, 0, s->avctx))
278 return AVERROR_INVALIDDATA;
280 nb_components = get_bits(&s->gb, 8);
281 if (nb_components <= 0 ||
282 nb_components > MAX_COMPONENTS)
284 if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
285 if (nb_components != s->nb_components) {
286 av_log(s->avctx, AV_LOG_ERROR,
287 "nb_components changing in interlaced picture\n");
288 return AVERROR_INVALIDDATA;
291 if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
292 avpriv_report_missing_feature(s->avctx,
293 "JPEG-LS that is not <= 8 "
294 "bits/component or 16-bit gray");
295 return AVERROR_PATCHWELCOME;
297 s->nb_components = nb_components;
300 memset(h_count, 0, sizeof(h_count));
301 memset(v_count, 0, sizeof(v_count));
302 for (i = 0; i < nb_components; i++) {
304 s->component_id[i] = get_bits(&s->gb, 8) - 1;
305 h_count[i] = get_bits(&s->gb, 4);
306 v_count[i] = get_bits(&s->gb, 4);
307 /* compute hmax and vmax (only used in interleaved case) */
308 if (h_count[i] > s->h_max)
309 s->h_max = h_count[i];
310 if (v_count[i] > s->v_max)
311 s->v_max = v_count[i];
312 s->quant_index[i] = get_bits(&s->gb, 8);
313 if (s->quant_index[i] >= 4) {
314 av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
315 return AVERROR_INVALIDDATA;
317 if (!h_count[i] || !v_count[i]) {
318 av_log(s->avctx, AV_LOG_ERROR,
319 "Invalid sampling factor in component %d %d:%d\n",
320 i, h_count[i], v_count[i]);
321 return AVERROR_INVALIDDATA;
324 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
325 i, h_count[i], v_count[i],
326 s->component_id[i], s->quant_index[i]);
329 if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
330 avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
331 return AVERROR_PATCHWELCOME;
335 /* if different size, realloc/alloc picture */
336 if ( width != s->width || height != s->height
337 || memcmp(s->h_count, h_count, sizeof(h_count))
338 || memcmp(s->v_count, v_count, sizeof(v_count))) {
342 memcpy(s->h_count, h_count, sizeof(h_count));
343 memcpy(s->v_count, v_count, sizeof(v_count));
347 /* test interlaced mode */
348 if (s->first_picture &&
349 s->org_height != 0 &&
350 s->height < ((s->org_height * 3) / 4)) {
352 s->bottom_field = s->interlace_polarity;
353 s->picture_ptr->interlaced_frame = 1;
354 s->picture_ptr->top_field_first = !s->interlace_polarity;
358 ret = ff_set_dimensions(s->avctx, width, height);
362 s->first_picture = 0;
365 if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
366 if (s->progressive) {
367 avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
368 return AVERROR_INVALIDDATA;
371 if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4))
373 else if (!s->lossless)
375 /* XXX: not complete test ! */
376 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
377 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
378 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
379 (s->h_count[3] << 4) | s->v_count[3];
380 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
381 /* NOTE we do not allocate pictures large enough for the possible
382 * padding of h/v_count being 4 */
383 if (!(pix_fmt_id & 0xD0D0D0D0))
384 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
385 if (!(pix_fmt_id & 0x0D0D0D0D))
386 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
388 switch (pix_fmt_id) {
391 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_BGR48;
393 if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
394 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_GBRP : AV_PIX_FMT_GBRP16;
396 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
397 else s->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
398 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
401 av_assert0(s->nb_components == 3);
405 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA64;
407 if (s->adobe_transform == 0 && s->bits <= 8) {
408 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
410 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_YUVA444P : AV_PIX_FMT_YUVA444P16;
411 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
414 av_assert0(s->nb_components == 4);
417 if (s->adobe_transform == 0 && s->bits <= 8) {
418 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
421 s->chroma_height = s->height;
422 } else if (s->adobe_transform == 2 && s->bits <= 8) {
423 s->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
426 s->chroma_height = s->height;
427 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
429 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
430 else s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16;
431 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
433 av_assert0(s->nb_components == 4);
437 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
440 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
442 s->upscale_h = 2*(pix_fmt_id == 0x22122100);
443 s->chroma_height = s->height;
447 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
450 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
451 s->upscale_v = 2*(pix_fmt_id == 0x22211200);
453 s->chroma_height = s->height;
456 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
459 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
462 s->chroma_height = s->height / 2;
474 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
476 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
481 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P;
484 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
485 s->upscale_h = 4 * (pix_fmt_id == 0x22211100) + 2 * (pix_fmt_id == 0x22112100);
486 s->chroma_height = s->height / 2;
489 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
490 else s->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
491 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
495 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
498 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
499 s->upscale_v = 2 << (pix_fmt_id == 0x22121100);
503 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
504 else s->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
505 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
506 if (pix_fmt_id == 0x42111100) {
508 s->chroma_height = s->height / 2;
512 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV411P : AV_PIX_FMT_YUVJ411P;
515 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
519 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
520 return AVERROR_PATCHWELCOME;
522 if ((s->upscale_h || s->upscale_v) && s->avctx->lowres) {
523 av_log(s->avctx, AV_LOG_ERROR, "lowres not supported for weird subsampling\n");
524 return AVERROR_PATCHWELCOME;
527 s->upscale_h = s->upscale_v = 0;
528 if (s->nb_components > 1)
529 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
530 else if (s->palette_index && s->bits <= 8)
531 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
532 else if (s->bits <= 8)
533 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
535 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
538 s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
540 av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
544 av_frame_unref(s->picture_ptr);
545 if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0)
547 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
548 s->picture_ptr->key_frame = 1;
551 for (i = 0; i < 4; i++)
552 s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
554 av_dlog(s->avctx, "%d %d %d %d %d %d\n",
555 s->width, s->height, s->linesize[0], s->linesize[1],
556 s->interlaced, s->avctx->height);
558 if (len != (8 + (3 * nb_components)))
559 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
562 if (s->rgb && !s->lossless && !s->ls) {
563 av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
564 return AVERROR_PATCHWELCOME;
567 /* totally blank picture as progressive JPEG will only add details to it */
568 if (s->progressive) {
569 int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
570 int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
571 for (i = 0; i < s->nb_components; i++) {
572 int size = bw * bh * s->h_count[i] * s->v_count[i];
573 av_freep(&s->blocks[i]);
574 av_freep(&s->last_nnz[i]);
575 s->blocks[i] = av_mallocz_array(size, sizeof(**s->blocks));
576 s->last_nnz[i] = av_mallocz_array(size, sizeof(**s->last_nnz));
577 if (!s->blocks[i] || !s->last_nnz[i])
578 return AVERROR(ENOMEM);
579 s->block_stride[i] = bw * s->h_count[i];
581 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
586 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
589 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
590 if (code < 0 || code > 16) {
591 av_log(s->avctx, AV_LOG_WARNING,
592 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
593 0, dc_index, &s->vlcs[0][dc_index]);
598 return get_xbits(&s->gb, code);
603 /* decode block and dequantize */
604 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
605 int dc_index, int ac_index, int16_t *quant_matrix)
607 int code, i, j, level, val;
610 val = mjpeg_decode_dc(s, dc_index);
611 if (val == 0xfffff) {
612 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
613 return AVERROR_INVALIDDATA;
615 val = val * quant_matrix[0] + s->last_dc[component];
616 s->last_dc[component] = val;
620 {OPEN_READER(re, &s->gb);
622 UPDATE_CACHE(re, &s->gb);
623 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
625 i += ((unsigned)code) >> 4;
628 if (code > MIN_CACHE_BITS - 16)
629 UPDATE_CACHE(re, &s->gb);
632 int cache = GET_CACHE(re, &s->gb);
633 int sign = (~cache) >> 31;
634 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
637 LAST_SKIP_BITS(re, &s->gb, code);
640 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
641 return AVERROR_INVALIDDATA;
643 j = s->scantable.permutated[i];
644 block[j] = level * quant_matrix[j];
647 CLOSE_READER(re, &s->gb);}
652 static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
653 int component, int dc_index,
654 int16_t *quant_matrix, int Al)
657 s->bdsp.clear_block(block);
658 val = mjpeg_decode_dc(s, dc_index);
659 if (val == 0xfffff) {
660 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
661 return AVERROR_INVALIDDATA;
663 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
664 s->last_dc[component] = val;
669 /* decode block and dequantize - progressive JPEG version */
670 static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
671 uint8_t *last_nnz, int ac_index,
672 int16_t *quant_matrix,
673 int ss, int se, int Al, int *EOBRUN)
675 int code, i, j, level, val, run;
683 OPEN_READER(re, &s->gb);
684 for (i = ss; ; i++) {
685 UPDATE_CACHE(re, &s->gb);
686 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
688 run = ((unsigned) code) >> 4;
692 if (code > MIN_CACHE_BITS - 16)
693 UPDATE_CACHE(re, &s->gb);
696 int cache = GET_CACHE(re, &s->gb);
697 int sign = (~cache) >> 31;
698 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
701 LAST_SKIP_BITS(re, &s->gb, code);
705 j = s->scantable.permutated[se];
706 block[j] = level * quant_matrix[j] << Al;
709 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
710 return AVERROR_INVALIDDATA;
712 j = s->scantable.permutated[i];
713 block[j] = level * quant_matrix[j] << Al;
715 if (run == 0xF) {// ZRL - skip 15 coefficients
718 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
719 return AVERROR_INVALIDDATA;
724 UPDATE_CACHE(re, &s->gb);
725 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
726 LAST_SKIP_BITS(re, &s->gb, run);
733 CLOSE_READER(re, &s->gb);
742 #define REFINE_BIT(j) { \
743 UPDATE_CACHE(re, &s->gb); \
744 sign = block[j] >> 15; \
745 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
746 ((quant_matrix[j] ^ sign) - sign) << Al; \
747 LAST_SKIP_BITS(re, &s->gb, 1); \
755 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
760 j = s->scantable.permutated[i]; \
763 else if (run-- == 0) \
767 /* decode block and dequantize - progressive JPEG refinement pass */
768 static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
770 int ac_index, int16_t *quant_matrix,
771 int ss, int se, int Al, int *EOBRUN)
773 int code, i = ss, j, sign, val, run;
774 int last = FFMIN(se, *last_nnz);
776 OPEN_READER(re, &s->gb);
781 UPDATE_CACHE(re, &s->gb);
782 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
785 run = ((unsigned) code) >> 4;
786 UPDATE_CACHE(re, &s->gb);
787 val = SHOW_UBITS(re, &s->gb, 1);
788 LAST_SKIP_BITS(re, &s->gb, 1);
790 j = s->scantable.permutated[i];
792 block[j] = ((quant_matrix[j]^val) - val) << Al;
796 CLOSE_READER(re, &s->gb);
800 run = ((unsigned) code) >> 4;
807 UPDATE_CACHE(re, &s->gb);
808 run += SHOW_UBITS(re, &s->gb, val);
809 LAST_SKIP_BITS(re, &s->gb, val);
821 for (; i <= last; i++) {
822 j = s->scantable.permutated[i];
826 CLOSE_READER(re, &s->gb);
833 static int handle_rstn(MJpegDecodeContext *s, int nb_components)
838 if (s->restart_interval) {
840 if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
841 align_get_bits(&s->gb);
842 for (i = 0; i < nb_components; i++) /* reset dc */
843 s->last_dc[i] = (4 << s->bits);
846 i = 8 + ((-get_bits_count(&s->gb)) & 7);
848 if (s->restart_count == 0) {
849 if( show_bits(&s->gb, i) == (1 << i) - 1
850 || show_bits(&s->gb, i) == 0xFF) {
851 int pos = get_bits_count(&s->gb);
852 align_get_bits(&s->gb);
853 while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
854 skip_bits(&s->gb, 8);
855 if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
856 for (i = 0; i < nb_components; i++) /* reset dc */
857 s->last_dc[i] = (4 << s->bits);
860 skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
867 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
870 uint16_t (*buffer)[4];
871 int left[4], top[4], topleft[4];
872 const int linesize = s->linesize[0];
873 const int mask = ((1 << s->bits) - 1) << point_transform;
877 if (s->nb_components != 3 && s->nb_components != 4)
878 return AVERROR_INVALIDDATA;
879 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
880 return AVERROR_INVALIDDATA;
883 s->restart_count = s->restart_interval;
885 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
886 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
887 buffer = s->ljpeg_buffer;
889 for (i = 0; i < 4; i++)
890 buffer[0][i] = 1 << (s->bits - 1);
892 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
893 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
895 if (s->interlaced && s->bottom_field)
896 ptr += linesize >> 1;
898 for (i = 0; i < 4; i++)
899 top[i] = left[i] = topleft[i] = buffer[0][i];
901 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
902 int modified_predictor = predictor;
904 if (s->restart_interval && !s->restart_count){
905 s->restart_count = s->restart_interval;
909 top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
911 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
912 modified_predictor = 1;
914 for (i=0;i<nb_components;i++) {
918 top[i] = buffer[mb_x][i];
920 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
922 dc = mjpeg_decode_dc(s, s->dc_index[i]);
926 left[i] = buffer[mb_x][i] =
927 mask & (pred + (dc << point_transform));
930 if (s->restart_interval && !--s->restart_count) {
931 align_get_bits(&s->gb);
932 skip_bits(&s->gb, 16); /* skip RSTn */
935 if (s->nb_components == 4) {
936 for(i=0; i<nb_components; i++) {
937 int c= s->comp_index[i];
939 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
940 ptr[4*mb_x+3-c] = buffer[mb_x][i];
942 } else if(s->bits == 9) {
943 return AVERROR_PATCHWELCOME;
945 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
946 ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i];
951 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
952 ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
953 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
954 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
956 } else if (s->pegasus_rct) {
957 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
958 ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
959 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
960 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
963 for(i=0; i<nb_components; i++) {
964 int c= s->comp_index[i];
966 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
967 ptr[3*mb_x+2-c] = buffer[mb_x][i];
969 } else if(s->bits == 9) {
970 return AVERROR_PATCHWELCOME;
972 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
973 ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i];
982 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
983 int point_transform, int nb_components)
985 int i, mb_x, mb_y, mask;
986 int bits= (s->bits+7)&~7;
990 point_transform += bits - s->bits;
991 mask = ((1 << s->bits) - 1) << point_transform;
993 av_assert0(nb_components>=1 && nb_components<=4);
995 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
996 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
997 if (s->restart_interval && !s->restart_count){
998 s->restart_count = s->restart_interval;
1003 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
1004 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1005 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1006 for (i = 0; i < nb_components; i++) {
1009 int n, h, v, x, y, c, j, linesize;
1010 n = s->nb_blocks[i];
1011 c = s->comp_index[i];
1016 linesize= s->linesize[c];
1018 if(bits>8) linesize /= 2;
1020 for(j=0; j<n; j++) {
1023 dc = mjpeg_decode_dc(s, s->dc_index[i]);
1027 ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
1029 if(x==0 && leftcol){
1030 pred= 1 << (bits - 1);
1035 if(x==0 && leftcol){
1036 pred= ptr[-linesize];
1038 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1042 if (s->interlaced && s->bottom_field)
1043 ptr += linesize >> 1;
1045 *ptr= pred + (dc << point_transform);
1047 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1049 if(x==0 && leftcol){
1050 pred= 1 << (bits - 1);
1055 if(x==0 && leftcol){
1056 pred= ptr16[-linesize];
1058 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1062 if (s->interlaced && s->bottom_field)
1063 ptr16 += linesize >> 1;
1065 *ptr16= pred + (dc << point_transform);
1074 for (i = 0; i < nb_components; i++) {
1077 int n, h, v, x, y, c, j, linesize, dc;
1078 n = s->nb_blocks[i];
1079 c = s->comp_index[i];
1084 linesize = s->linesize[c];
1086 if(bits>8) linesize /= 2;
1088 for (j = 0; j < n; j++) {
1091 dc = mjpeg_decode_dc(s, s->dc_index[i]);
1095 ptr = s->picture_ptr->data[c] +
1096 (linesize * (v * mb_y + y)) +
1097 (h * mb_x + x); //FIXME optimize this crap
1098 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1101 *ptr = pred + (dc << point_transform);
1103 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1104 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1107 *ptr16= pred + (dc << point_transform);
1117 if (s->restart_interval && !--s->restart_count) {
1118 align_get_bits(&s->gb);
1119 skip_bits(&s->gb, 16); /* skip RSTn */
1126 static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s,
1127 uint8_t *dst, const uint8_t *src,
1128 int linesize, int lowres)
1131 case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
1133 case 1: copy_block4(dst, src, linesize, linesize, 4);
1135 case 2: copy_block2(dst, src, linesize, linesize, 2);
1137 case 3: *dst = *src;
1142 static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1144 int block_x, block_y;
1145 int size = 8 >> s->avctx->lowres;
1147 for (block_y=0; block_y<size; block_y++)
1148 for (block_x=0; block_x<size; block_x++)
1149 *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits;
1151 for (block_y=0; block_y<size; block_y++)
1152 for (block_x=0; block_x<size; block_x++)
1153 *(ptr + block_x + block_y*linesize) <<= 8 - s->bits;
1157 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1158 int Al, const uint8_t *mb_bitmask,
1159 int mb_bitmask_size,
1160 const AVFrame *reference)
1163 uint8_t *data[MAX_COMPONENTS];
1164 const uint8_t *reference_data[MAX_COMPONENTS];
1165 int linesize[MAX_COMPONENTS];
1166 GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1167 int bytes_per_pixel = 1 + (s->bits > 8);
1170 if (mb_bitmask_size != (s->mb_width * s->mb_height + 7)>>3) {
1171 av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1172 return AVERROR_INVALIDDATA;
1174 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1177 s->restart_count = 0;
1179 for (i = 0; i < nb_components; i++) {
1180 int c = s->comp_index[i];
1181 data[c] = s->picture_ptr->data[c];
1182 reference_data[c] = reference ? reference->data[c] : NULL;
1183 linesize[c] = s->linesize[c];
1184 s->coefs_finished[c] |= 1;
1187 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1188 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1189 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1191 if (s->restart_interval && !s->restart_count)
1192 s->restart_count = s->restart_interval;
1194 if (get_bits_left(&s->gb) < 0) {
1195 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1196 -get_bits_left(&s->gb));
1197 return AVERROR_INVALIDDATA;
1199 for (i = 0; i < nb_components; i++) {
1201 int n, h, v, x, y, c, j;
1203 n = s->nb_blocks[i];
1204 c = s->comp_index[i];
1209 for (j = 0; j < n; j++) {
1210 block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1211 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1213 if (s->interlaced && s->bottom_field)
1214 block_offset += linesize[c] >> 1;
1215 ptr = data[c] + block_offset;
1216 if (!s->progressive) {
1218 mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1219 linesize[c], s->avctx->lowres);
1222 s->bdsp.clear_block(s->block);
1223 if (decode_block(s, s->block, i,
1224 s->dc_index[i], s->ac_index[i],
1225 s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1226 av_log(s->avctx, AV_LOG_ERROR,
1227 "error y=%d x=%d\n", mb_y, mb_x);
1228 return AVERROR_INVALIDDATA;
1230 s->dsp.idct_put(ptr, linesize[c], s->block);
1232 shift_output(s, ptr, linesize[c]);
1235 int block_idx = s->block_stride[c] * (v * mb_y + y) +
1237 int16_t *block = s->blocks[c][block_idx];
1239 block[0] += get_bits1(&s->gb) *
1240 s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1241 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1242 s->quant_matrixes[s->quant_sindex[i]],
1244 av_log(s->avctx, AV_LOG_ERROR,
1245 "error y=%d x=%d\n", mb_y, mb_x);
1246 return AVERROR_INVALIDDATA;
1249 av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1250 av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1251 mb_x, mb_y, x, y, c, s->bottom_field,
1252 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1260 handle_rstn(s, nb_components);
1266 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
1267 int se, int Ah, int Al)
1271 int c = s->comp_index[0];
1272 uint8_t *data = s->picture_ptr->data[c];
1273 int linesize = s->linesize[c];
1275 int16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1276 int bytes_per_pixel = 1 + (s->bits > 8);
1278 av_assert0(ss>=0 && Ah>=0 && Al>=0);
1279 if (se < ss || se > 63) {
1280 av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se);
1281 return AVERROR_INVALIDDATA;
1285 s->coefs_finished[c] |= (2ULL << se) - (1ULL << ss);
1286 last_scan = !~s->coefs_finished[c];
1289 if (s->interlaced && s->bottom_field)
1290 data += linesize >> 1;
1292 s->restart_count = 0;
1294 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1295 uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1296 int block_idx = mb_y * s->block_stride[c];
1297 int16_t (*block)[64] = &s->blocks[c][block_idx];
1298 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1299 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1301 if (s->restart_interval && !s->restart_count)
1302 s->restart_count = s->restart_interval;
1305 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1306 quant_matrix, ss, se, Al, &EOBRUN);
1308 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1309 quant_matrix, ss, se, Al, &EOBRUN);
1311 av_log(s->avctx, AV_LOG_ERROR,
1312 "error y=%d x=%d\n", mb_y, mb_x);
1313 return AVERROR_INVALIDDATA;
1317 s->dsp.idct_put(ptr, linesize, *block);
1319 shift_output(s, ptr, linesize);
1320 ptr += bytes_per_pixel*8 >> s->avctx->lowres;
1322 if (handle_rstn(s, 0))
1329 int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
1330 int mb_bitmask_size, const AVFrame *reference)
1332 int len, nb_components, i, h, v, predictor, point_transform;
1334 const int block_size = s->lossless ? 1 : 8;
1335 int ilv, prev_shift;
1337 if (!s->got_picture) {
1338 av_log(s->avctx, AV_LOG_WARNING,
1339 "Can not process SOS before SOF, skipping\n");
1343 av_assert0(s->picture_ptr->data[0]);
1344 /* XXX: verify len field validity */
1345 len = get_bits(&s->gb, 16);
1346 nb_components = get_bits(&s->gb, 8);
1347 if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1348 av_log(s->avctx, AV_LOG_ERROR,
1349 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1350 return AVERROR_PATCHWELCOME;
1352 if (len != 6 + 2 * nb_components) {
1353 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1354 return AVERROR_INVALIDDATA;
1356 for (i = 0; i < nb_components; i++) {
1357 id = get_bits(&s->gb, 8) - 1;
1358 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1359 /* find component index */
1360 for (index = 0; index < s->nb_components; index++)
1361 if (id == s->component_id[index])
1363 if (index == s->nb_components) {
1364 av_log(s->avctx, AV_LOG_ERROR,
1365 "decode_sos: index(%d) out of components\n", index);
1366 return AVERROR_INVALIDDATA;
1368 /* Metasoft MJPEG codec has Cb and Cr swapped */
1369 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1370 && nb_components == 3 && s->nb_components == 3 && i)
1373 s->quant_sindex[i] = s->quant_index[index];
1374 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1375 s->h_scount[i] = s->h_count[index];
1376 s->v_scount[i] = s->v_count[index];
1378 if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1380 if(nb_components == 1 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1381 index = (index+2)%3;
1383 s->comp_index[i] = index;
1385 s->dc_index[i] = get_bits(&s->gb, 4);
1386 s->ac_index[i] = get_bits(&s->gb, 4);
1388 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1389 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1391 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))
1395 predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1396 ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1397 if(s->avctx->codec_tag != AV_RL32("CJPG")){
1398 prev_shift = get_bits(&s->gb, 4); /* Ah */
1399 point_transform = get_bits(&s->gb, 4); /* Al */
1401 prev_shift = point_transform = 0;
1403 if (nb_components > 1) {
1404 /* interleaved stream */
1405 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1406 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1407 } else if (!s->ls) { /* skip this for JPEG-LS */
1408 h = s->h_max / s->h_scount[0];
1409 v = s->v_max / s->v_scount[0];
1410 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1411 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1412 s->nb_blocks[0] = 1;
1417 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1418 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1419 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1420 predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1421 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1424 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1425 for (i = s->mjpb_skiptosod; i > 0; i--)
1426 skip_bits(&s->gb, 8);
1429 for (i = 0; i < nb_components; i++)
1430 s->last_dc[i] = (4 << s->bits);
1433 av_assert0(s->picture_ptr == s->picture);
1434 if (CONFIG_JPEGLS_DECODER && s->ls) {
1436 // reset_ls_coding_parameters(s, 0);
1438 if ((ret = ff_jpegls_decode_picture(s, predictor,
1439 point_transform, ilv)) < 0)
1443 if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1446 if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1448 nb_components)) < 0)
1453 if (s->progressive && predictor) {
1454 av_assert0(s->picture_ptr == s->picture);
1455 if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1457 point_transform)) < 0)
1460 if ((ret = mjpeg_decode_scan(s, nb_components,
1461 prev_shift, point_transform,
1462 mb_bitmask, mb_bitmask_size, reference)) < 0)
1467 if (s->interlaced &&
1468 get_bits_left(&s->gb) > 32 &&
1469 show_bits(&s->gb, 8) == 0xFF) {
1470 GetBitContext bak = s->gb;
1471 align_get_bits(&bak);
1472 if (show_bits(&bak, 16) == 0xFFD1) {
1473 av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1475 skip_bits(&s->gb, 16);
1476 s->bottom_field ^= 1;
1485 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1486 return AVERROR_INVALIDDATA;
1489 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1491 if (get_bits(&s->gb, 16) != 4)
1492 return AVERROR_INVALIDDATA;
1493 s->restart_interval = get_bits(&s->gb, 16);
1494 s->restart_count = 0;
1495 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1496 s->restart_interval);
1501 static int mjpeg_decode_app(MJpegDecodeContext *s)
1505 len = get_bits(&s->gb, 16);
1507 return AVERROR_INVALIDDATA;
1508 if (8 * len > get_bits_left(&s->gb))
1509 return AVERROR_INVALIDDATA;
1511 id = get_bits_long(&s->gb, 32);
1514 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
1516 av_get_codec_tag_string(id_str, sizeof(id_str), av_bswap32(id));
1517 av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n", id_str, id, len);
1520 /* Buggy AVID, it puts EOI only at every 10th frame. */
1521 /* Also, this fourcc is used by non-avid files too, it holds some
1522 information, but it's always present in AVID-created files. */
1523 if (id == AV_RB32("AVI1")) {
1529 4bytes field_size_less_padding
1532 i = get_bits(&s->gb, 8); len--;
1533 av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1535 skip_bits(&s->gb, 8);
1536 skip_bits(&s->gb, 32);
1537 skip_bits(&s->gb, 32);
1545 if (id == AV_RB32("JFIF")) {
1546 int t_w, t_h, v1, v2;
1547 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1548 v1 = get_bits(&s->gb, 8);
1549 v2 = get_bits(&s->gb, 8);
1550 skip_bits(&s->gb, 8);
1552 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1553 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1554 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1556 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1557 av_log(s->avctx, AV_LOG_INFO,
1558 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1560 s->avctx->sample_aspect_ratio.num,
1561 s->avctx->sample_aspect_ratio.den);
1563 t_w = get_bits(&s->gb, 8);
1564 t_h = get_bits(&s->gb, 8);
1566 /* skip thumbnail */
1567 if (len -10 - (t_w * t_h * 3) > 0)
1568 len -= t_w * t_h * 3;
1574 if (id == AV_RB32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1575 skip_bits(&s->gb, 16); /* version */
1576 skip_bits(&s->gb, 16); /* flags0 */
1577 skip_bits(&s->gb, 16); /* flags1 */
1578 s->adobe_transform = get_bits(&s->gb, 8);
1579 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1580 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1585 if (id == AV_RB32("LJIF")) {
1586 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1587 av_log(s->avctx, AV_LOG_INFO,
1588 "Pegasus lossless jpeg header found\n");
1589 skip_bits(&s->gb, 16); /* version ? */
1590 skip_bits(&s->gb, 16); /* unknown always 0? */
1591 skip_bits(&s->gb, 16); /* unknown always 0? */
1592 skip_bits(&s->gb, 16); /* unknown always 0? */
1593 switch (i=get_bits(&s->gb, 8)) {
1603 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1608 if (id == AV_RL32("colr") && len > 0) {
1609 s->colr = get_bits(&s->gb, 8);
1610 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1611 av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1615 if (id == AV_RL32("xfrm") && len > 0) {
1616 s->xfrm = get_bits(&s->gb, 8);
1617 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1618 av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1623 /* JPS extension by VRex */
1624 if (s->start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1625 int flags, layout, type;
1626 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1627 av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
1629 skip_bits(&s->gb, 32); len -= 4; /* JPS_ */
1630 skip_bits(&s->gb, 16); len -= 2; /* block length */
1631 skip_bits(&s->gb, 8); /* reserved */
1632 flags = get_bits(&s->gb, 8);
1633 layout = get_bits(&s->gb, 8);
1634 type = get_bits(&s->gb, 8);
1637 s->stereo3d = av_stereo3d_alloc();
1642 s->stereo3d->type = AV_STEREO3D_2D;
1643 } else if (type == 1) {
1646 s->stereo3d->type = AV_STEREO3D_LINES;
1649 s->stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1652 s->stereo3d->type = AV_STEREO3D_TOPBOTTOM;
1655 if (!(flags & 0x04)) {
1656 s->stereo3d->flags = AV_STEREO3D_FLAG_INVERT;
1663 if (s->start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
1664 GetByteContext gbytes;
1665 int ret, le, ifd_offset, bytes_read;
1666 const uint8_t *aligned;
1668 skip_bits(&s->gb, 16); // skip padding
1671 // init byte wise reading
1672 aligned = align_get_bits(&s->gb);
1673 bytestream2_init(&gbytes, aligned, len);
1676 ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
1678 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
1680 bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
1682 // read 0th IFD and store the metadata
1683 // (return values > 0 indicate the presence of subimage metadata)
1684 ret = avpriv_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata);
1686 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n");
1690 bytes_read = bytestream2_tell(&gbytes);
1691 skip_bits(&s->gb, bytes_read << 3);
1698 if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1699 id = get_bits_long(&s->gb, 32);
1702 if (id == AV_RB32("mjpg")) {
1704 skip_bits(&s->gb, 32); /* field size */
1705 skip_bits(&s->gb, 32); /* pad field size */
1706 skip_bits(&s->gb, 32); /* next off */
1707 skip_bits(&s->gb, 32); /* quant off */
1708 skip_bits(&s->gb, 32); /* huff off */
1709 skip_bits(&s->gb, 32); /* image off */
1710 skip_bits(&s->gb, 32); /* scan off */
1711 skip_bits(&s->gb, 32); /* data off */
1713 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1714 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1719 /* slow but needed for extreme adobe jpegs */
1721 av_log(s->avctx, AV_LOG_ERROR,
1722 "mjpeg: error, decode_app parser read over the end\n");
1724 skip_bits(&s->gb, 8);
1729 static int mjpeg_decode_com(MJpegDecodeContext *s)
1731 int len = get_bits(&s->gb, 16);
1732 if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1733 char *cbuf = av_malloc(len - 1);
1736 for (i = 0; i < len - 2; i++)
1737 cbuf[i] = get_bits(&s->gb, 8);
1738 if (i > 0 && cbuf[i - 1] == '\n')
1743 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1744 av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
1746 /* buggy avid, it puts EOI only at every 10th frame */
1747 if (!strncmp(cbuf, "AVID", 4)) {
1748 parse_avid(s, cbuf, len);
1749 } else if (!strcmp(cbuf, "CS=ITU601"))
1751 else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
1752 (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1762 /* return the 8 bit start code value and update the search
1763 state. Return -1 if no start code found */
1764 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1766 const uint8_t *buf_ptr;
1771 buf_ptr = *pbuf_ptr;
1772 while (buf_end - buf_ptr > 1) {
1775 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1784 av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1785 *pbuf_ptr = buf_ptr;
1789 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1790 const uint8_t **buf_ptr, const uint8_t *buf_end,
1791 const uint8_t **unescaped_buf_ptr,
1792 int *unescaped_buf_size)
1795 start_code = find_marker(buf_ptr, buf_end);
1797 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1799 return AVERROR(ENOMEM);
1801 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1802 if (start_code == SOS && !s->ls) {
1803 const uint8_t *src = *buf_ptr;
1804 uint8_t *dst = s->buffer;
1806 while (src < buf_end) {
1807 uint8_t x = *(src++);
1810 if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1812 while (src < buf_end && x == 0xff)
1815 if (x >= 0xd0 && x <= 0xd7)
1822 *unescaped_buf_ptr = s->buffer;
1823 *unescaped_buf_size = dst - s->buffer;
1824 memset(s->buffer + *unescaped_buf_size, 0,
1825 FF_INPUT_BUFFER_PADDING_SIZE);
1827 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %"PTRDIFF_SPECIFIER" bytes\n",
1828 (buf_end - *buf_ptr) - (dst - s->buffer));
1829 } else if (start_code == SOS && s->ls) {
1830 const uint8_t *src = *buf_ptr;
1831 uint8_t *dst = s->buffer;
1837 while (src + t < buf_end) {
1838 uint8_t x = src[t++];
1840 while ((src + t < buf_end) && x == 0xff)
1849 init_put_bits(&pb, dst, t);
1851 /* unescape bitstream */
1853 uint8_t x = src[b++];
1854 put_bits(&pb, 8, x);
1857 put_bits(&pb, 7, x);
1861 flush_put_bits(&pb);
1863 *unescaped_buf_ptr = dst;
1864 *unescaped_buf_size = (bit_count + 7) >> 3;
1865 memset(s->buffer + *unescaped_buf_size, 0,
1866 FF_INPUT_BUFFER_PADDING_SIZE);
1868 *unescaped_buf_ptr = *buf_ptr;
1869 *unescaped_buf_size = buf_end - *buf_ptr;
1875 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1878 AVFrame *frame = data;
1879 const uint8_t *buf = avpkt->data;
1880 int buf_size = avpkt->size;
1881 MJpegDecodeContext *s = avctx->priv_data;
1882 const uint8_t *buf_end, *buf_ptr;
1883 const uint8_t *unescaped_buf_ptr;
1885 int unescaped_buf_size;
1890 av_dict_free(&s->exif_metadata);
1891 av_freep(&s->stereo3d);
1892 s->adobe_transform = -1;
1895 buf_end = buf + buf_size;
1896 while (buf_ptr < buf_end) {
1897 /* find start next marker */
1898 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1900 &unescaped_buf_size);
1902 if (start_code < 0) {
1904 } else if (unescaped_buf_size > INT_MAX / 8) {
1905 av_log(avctx, AV_LOG_ERROR,
1906 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1907 start_code, unescaped_buf_size, buf_size);
1908 return AVERROR_INVALIDDATA;
1910 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
1911 start_code, buf_end - buf_ptr);
1913 ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
1916 av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
1920 s->start_code = start_code;
1921 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1922 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1924 /* process markers */
1925 if (start_code >= 0xd0 && start_code <= 0xd7)
1926 av_log(avctx, AV_LOG_DEBUG,
1927 "restart marker: %d\n", start_code & 0x0f);
1929 else if (start_code >= APP0 && start_code <= APP15)
1930 mjpeg_decode_app(s);
1932 else if (start_code == COM)
1933 mjpeg_decode_com(s);
1937 if (!CONFIG_JPEGLS_DECODER &&
1938 (start_code == SOF48 || start_code == LSE)) {
1939 av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
1940 return AVERROR(ENOSYS);
1943 switch (start_code) {
1945 s->restart_interval = 0;
1946 s->restart_count = 0;
1947 /* nothing to do on SOI */
1950 ff_mjpeg_decode_dqt(s);
1953 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1954 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1963 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1970 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1977 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1984 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1988 if (!CONFIG_JPEGLS_DECODER ||
1989 (ret = ff_jpegls_decode_lse(s)) < 0)
1995 if (!s->got_picture) {
1996 av_log(avctx, AV_LOG_WARNING,
1997 "Found EOI before any SOF, ignoring\n");
2000 if (s->interlaced) {
2001 s->bottom_field ^= 1;
2002 /* if not bottom field, do not output image yet */
2003 if (s->bottom_field == !s->interlace_polarity)
2006 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2012 int qp = FFMAX3(s->qscale[0],
2015 int qpw = (s->width + 15) / 16;
2016 AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
2018 memset(qp_table_buf->data, qp, qpw);
2019 av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
2022 if(avctx->debug & FF_DEBUG_QP)
2023 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2029 if ((ret = ff_mjpeg_decode_sos(s, NULL, 0, NULL)) < 0 &&
2030 (avctx->err_recognition & AV_EF_EXPLODE))
2034 mjpeg_decode_dri(s);
2046 av_log(avctx, AV_LOG_ERROR,
2047 "mjpeg: unsupported coding type (%x)\n", start_code);
2051 /* eof process start code */
2052 buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
2053 av_log(avctx, AV_LOG_DEBUG,
2054 "marker parser used %d bytes (%d bits)\n",
2055 (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
2057 if (s->got_picture && s->cur_scan) {
2058 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2061 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2062 return AVERROR_INVALIDDATA;
2069 av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2070 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2071 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2072 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2073 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2074 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2075 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2076 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2078 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2079 for (p = 1; p<4; p++) {
2080 uint8_t *line = s->picture_ptr->data[p];
2082 if (!(s->upscale_h & (1<<p)))
2084 w = s->width >> hshift;
2085 for (i = 0; i < s->chroma_height; i++) {
2086 for (index = w - 1; index; index--)
2087 line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2088 line += s->linesize[p];
2094 av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2095 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2096 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2097 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2098 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2099 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2101 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2102 for (p = 1; p < 4; p++) {
2103 uint8_t *dst = &((uint8_t *)s->picture_ptr->data[p])[(s->height - 1) * s->linesize[p]];
2105 if (!(s->upscale_v & (1<<p)))
2107 w = s->width >> hshift;
2108 for (i = s->height - 1; i; i--) {
2109 uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i / 2 * s->linesize[p]];
2110 uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) / 2 * s->linesize[p]];
2112 memcpy(dst, src1, w);
2114 for (index = 0; index < w; index++)
2115 dst[index] = (src1[index] + src2[index]) >> 1;
2117 dst -= s->linesize[p];
2123 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2124 for (index=0; index<4; index++) {
2125 uint8_t *dst = s->picture_ptr->data[index];
2126 int w = s->picture_ptr->width;
2127 int h = s->picture_ptr->height;
2128 if(index && index<3){
2129 w = FF_CEIL_RSHIFT(w, hshift);
2130 h = FF_CEIL_RSHIFT(h, vshift);
2133 uint8_t *dst2 = dst + s->picture_ptr->linesize[index]*(h-1);
2134 for (i=0; i<h/2; i++) {
2136 FFSWAP(int, dst[j], dst2[j]);
2137 dst += s->picture_ptr->linesize[index];
2138 dst2 -= s->picture_ptr->linesize[index];
2143 if (s->adobe_transform == 0 && s->avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2144 int w = s->picture_ptr->width;
2145 int h = s->picture_ptr->height;
2146 for (i=0; i<h; i++) {
2149 for (index=0; index<4; index++) {
2150 dst[index] = s->picture_ptr->data[index]
2151 + s->picture_ptr->linesize[index]*i;
2153 for (j=0; j<w; j++) {
2155 int r = dst[0][j] * k;
2156 int g = dst[1][j] * k;
2157 int b = dst[2][j] * k;
2158 dst[0][j] = g*257 >> 16;
2159 dst[1][j] = b*257 >> 16;
2160 dst[2][j] = r*257 >> 16;
2165 if (s->adobe_transform == 2 && s->avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2166 int w = s->picture_ptr->width;
2167 int h = s->picture_ptr->height;
2168 for (i=0; i<h; i++) {
2171 for (index=0; index<4; index++) {
2172 dst[index] = s->picture_ptr->data[index]
2173 + s->picture_ptr->linesize[index]*i;
2175 for (j=0; j<w; j++) {
2177 int r = (255 - dst[0][j]) * k;
2178 int g = (128 - dst[1][j]) * k;
2179 int b = (128 - dst[2][j]) * k;
2180 dst[0][j] = r*257 >> 16;
2181 dst[1][j] = (g*257 >> 16) + 128;
2182 dst[2][j] = (b*257 >> 16) + 128;
2189 AVStereo3D *stereo = av_stereo3d_create_side_data(data);
2191 stereo->type = s->stereo3d->type;
2192 stereo->flags = s->stereo3d->flags;
2194 av_freep(&s->stereo3d);
2197 av_dict_copy(avpriv_frame_get_metadatap(data), s->exif_metadata, 0);
2198 av_dict_free(&s->exif_metadata);
2200 av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n",
2202 // return buf_end - buf_ptr;
2203 return buf_ptr - buf;
2206 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
2208 MJpegDecodeContext *s = avctx->priv_data;
2211 if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) {
2212 av_log(avctx, AV_LOG_INFO, "Single field\n");
2216 av_frame_free(&s->picture);
2217 s->picture_ptr = NULL;
2218 } else if (s->picture_ptr)
2219 av_frame_unref(s->picture_ptr);
2221 av_freep(&s->buffer);
2222 av_freep(&s->stereo3d);
2223 av_freep(&s->ljpeg_buffer);
2224 s->ljpeg_buffer_size = 0;
2226 for (i = 0; i < 3; i++) {
2227 for (j = 0; j < 4; j++)
2228 ff_free_vlc(&s->vlcs[i][j]);
2230 for (i = 0; i < MAX_COMPONENTS; i++) {
2231 av_freep(&s->blocks[i]);
2232 av_freep(&s->last_nnz[i]);
2234 av_dict_free(&s->exif_metadata);
2238 static void decode_flush(AVCodecContext *avctx)
2240 MJpegDecodeContext *s = avctx->priv_data;
2244 #if CONFIG_MJPEG_DECODER
2245 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2246 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2247 static const AVOption options[] = {
2248 { "extern_huff", "Use external huffman table.",
2249 OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
2253 static const AVClass mjpegdec_class = {
2254 .class_name = "MJPEG decoder",
2255 .item_name = av_default_item_name,
2257 .version = LIBAVUTIL_VERSION_INT,
2260 AVCodec ff_mjpeg_decoder = {
2262 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
2263 .type = AVMEDIA_TYPE_VIDEO,
2264 .id = AV_CODEC_ID_MJPEG,
2265 .priv_data_size = sizeof(MJpegDecodeContext),
2266 .init = ff_mjpeg_decode_init,
2267 .close = ff_mjpeg_decode_end,
2268 .decode = ff_mjpeg_decode_frame,
2269 .flush = decode_flush,
2270 .capabilities = CODEC_CAP_DR1,
2272 .priv_class = &mjpegdec_class,
2275 #if CONFIG_THP_DECODER
2276 AVCodec ff_thp_decoder = {
2278 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
2279 .type = AVMEDIA_TYPE_VIDEO,
2280 .id = AV_CODEC_ID_THP,
2281 .priv_data_size = sizeof(MJpegDecodeContext),
2282 .init = ff_mjpeg_decode_init,
2283 .close = ff_mjpeg_decode_end,
2284 .decode = ff_mjpeg_decode_frame,
2285 .flush = decode_flush,
2286 .capabilities = CODEC_CAP_DR1,