3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
11 * This file is part of Libav.
13 * Libav is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * Libav is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with Libav; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "libavutil/imgutils.h"
36 #include "libavutil/opt.h"
41 #include "jpeglsdec.h"
44 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
45 const uint8_t *val_table, int nb_codes,
46 int use_static, int is_ac)
48 uint8_t huff_size[256] = { 0 };
49 uint16_t huff_code[256];
50 uint16_t huff_sym[256];
53 assert(nb_codes <= 256);
55 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
57 for (i = 0; i < 256; i++)
58 huff_sym[i] = i + 16 * is_ac;
61 huff_sym[0] = 16 * 256;
63 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
64 huff_code, 2, 2, huff_sym, 2, 2, use_static);
67 static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
69 build_vlc(&s->vlcs[0][0], avpriv_mjpeg_bits_dc_luminance,
70 avpriv_mjpeg_val_dc, 12, 0, 0);
71 build_vlc(&s->vlcs[0][1], avpriv_mjpeg_bits_dc_chrominance,
72 avpriv_mjpeg_val_dc, 12, 0, 0);
73 build_vlc(&s->vlcs[1][0], avpriv_mjpeg_bits_ac_luminance,
74 avpriv_mjpeg_val_ac_luminance, 251, 0, 1);
75 build_vlc(&s->vlcs[1][1], avpriv_mjpeg_bits_ac_chrominance,
76 avpriv_mjpeg_val_ac_chrominance, 251, 0, 1);
77 build_vlc(&s->vlcs[2][0], avpriv_mjpeg_bits_ac_luminance,
78 avpriv_mjpeg_val_ac_luminance, 251, 0, 0);
79 build_vlc(&s->vlcs[2][1], avpriv_mjpeg_bits_ac_chrominance,
80 avpriv_mjpeg_val_ac_chrominance, 251, 0, 0);
83 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
85 MJpegDecodeContext *s = avctx->priv_data;
87 if (!s->picture_ptr) {
88 s->picture = av_frame_alloc();
90 return AVERROR(ENOMEM);
91 s->picture_ptr = s->picture;
95 ff_hpeldsp_init(&s->hdsp, avctx->flags);
96 ff_dsputil_init(&s->dsp, avctx);
97 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
101 s->first_picture = 1;
102 s->org_height = avctx->coded_height;
103 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
105 build_basic_mjpeg_vlc(s);
107 if (s->extern_huff) {
109 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
110 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
111 if ((ret = ff_mjpeg_decode_dht(s))) {
112 av_log(avctx, AV_LOG_ERROR,
113 "mjpeg: error using external huffman table\n");
117 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
118 s->interlace_polarity = 1; /* bottom field first */
119 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
121 if (avctx->codec->id == AV_CODEC_ID_AMV)
128 /* quantize tables */
129 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
131 int len, index, i, j;
133 len = get_bits(&s->gb, 16) - 2;
136 /* only 8 bit precision handled */
137 if (get_bits(&s->gb, 4) != 0) {
138 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
141 index = get_bits(&s->gb, 4);
144 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
145 /* read quant table */
146 for (i = 0; i < 64; i++) {
147 j = s->scantable.permutated[i];
148 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
151 // XXX FIXME finetune, and perhaps add dc too
152 s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
153 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
154 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
155 index, s->qscale[index]);
161 /* decode huffman tables and build VLC decoders */
162 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
164 int len, index, i, class, n, v, code_max;
165 uint8_t bits_table[17];
166 uint8_t val_table[256];
169 len = get_bits(&s->gb, 16) - 2;
173 return AVERROR_INVALIDDATA;
174 class = get_bits(&s->gb, 4);
176 return AVERROR_INVALIDDATA;
177 index = get_bits(&s->gb, 4);
179 return AVERROR_INVALIDDATA;
181 for (i = 1; i <= 16; i++) {
182 bits_table[i] = get_bits(&s->gb, 8);
186 if (len < n || n > 256)
187 return AVERROR_INVALIDDATA;
190 for (i = 0; i < n; i++) {
191 v = get_bits(&s->gb, 8);
198 /* build VLC and flush previous vlc if present */
199 ff_free_vlc(&s->vlcs[class][index]);
200 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
201 class, index, code_max + 1);
202 if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
203 code_max + 1, 0, class > 0)) < 0)
207 ff_free_vlc(&s->vlcs[2][index]);
208 if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
209 code_max + 1, 0, 0)) < 0)
216 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
218 int len, nb_components, i, width, height, pix_fmt_id, ret;
220 /* XXX: verify len field validity */
221 len = get_bits(&s->gb, 16);
222 s->bits = get_bits(&s->gb, 8);
226 if (s->bits == 9 && !s->pegasus_rct)
227 s->rct = 1; // FIXME ugly
229 if (s->bits != 8 && !s->lossless) {
230 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
234 height = get_bits(&s->gb, 16);
235 width = get_bits(&s->gb, 16);
237 // HACK for odd_height.mov
238 if (s->interlaced && s->width == width && s->height == height + 1)
241 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
242 if (av_image_check_size(width, height, 0, s->avctx))
243 return AVERROR_INVALIDDATA;
245 nb_components = get_bits(&s->gb, 8);
246 if (nb_components <= 0 ||
247 nb_components > MAX_COMPONENTS)
249 if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
250 if (nb_components != s->nb_components) {
251 av_log(s->avctx, AV_LOG_ERROR,
252 "nb_components changing in interlaced picture\n");
253 return AVERROR_INVALIDDATA;
256 if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
257 avpriv_report_missing_feature(s->avctx,
258 "JPEG-LS that is not <= 8 "
259 "bits/component or 16-bit gray");
260 return AVERROR_PATCHWELCOME;
262 s->nb_components = nb_components;
265 for (i = 0; i < nb_components; i++) {
267 s->component_id[i] = get_bits(&s->gb, 8) - 1;
268 s->h_count[i] = get_bits(&s->gb, 4);
269 s->v_count[i] = get_bits(&s->gb, 4);
270 /* compute hmax and vmax (only used in interleaved case) */
271 if (s->h_count[i] > s->h_max)
272 s->h_max = s->h_count[i];
273 if (s->v_count[i] > s->v_max)
274 s->v_max = s->v_count[i];
275 s->quant_index[i] = get_bits(&s->gb, 8);
276 if (s->quant_index[i] >= 4)
277 return AVERROR_INVALIDDATA;
278 if (!s->h_count[i] || !s->v_count[i]) {
279 av_log(s->avctx, AV_LOG_ERROR,
280 "Invalid sampling factor in component %d %d:%d\n",
281 i, s->h_count[i], s->v_count[i]);
282 return AVERROR_INVALIDDATA;
285 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
286 i, s->h_count[i], s->v_count[i],
287 s->component_id[i], s->quant_index[i]);
290 if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
291 avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
292 return AVERROR_PATCHWELCOME;
295 if (s->v_max == 1 && s->h_max == 1 && s->lossless == 1)
298 /* if different size, realloc/alloc picture */
299 /* XXX: also check h_count and v_count */
300 if (width != s->width || height != s->height) {
305 /* test interlaced mode */
306 if (s->first_picture &&
307 s->org_height != 0 &&
308 s->height < ((s->org_height * 3) / 4)) {
310 s->bottom_field = s->interlace_polarity;
311 s->picture_ptr->interlaced_frame = 1;
312 s->picture_ptr->top_field_first = !s->interlace_polarity;
316 ret = ff_set_dimensions(s->avctx, width, height);
320 s->first_picture = 0;
323 if (!(s->interlaced && (s->bottom_field == !s->interlace_polarity))) {
324 /* XXX: not complete test ! */
325 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
326 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
327 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
328 (s->h_count[3] << 4) | s->v_count[3];
329 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
330 /* NOTE we do not allocate pictures large enough for the possible
331 * padding of h/v_count being 4 */
332 if (!(pix_fmt_id & 0xD0D0D0D0))
333 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
334 if (!(pix_fmt_id & 0x0D0D0D0D))
335 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
337 switch (pix_fmt_id) {
340 s->avctx->pix_fmt = AV_PIX_FMT_BGRA;
342 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
343 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
345 assert(s->nb_components == 3);
348 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
351 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P;
352 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
355 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
356 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
359 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
360 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
363 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
364 return AVERROR_PATCHWELCOME;
367 if (s->nb_components > 1)
368 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
369 else if (s->bits <= 8)
370 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
372 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
375 s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
377 av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
381 av_frame_unref(s->picture_ptr);
382 if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0) {
383 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
386 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
387 s->picture_ptr->key_frame = 1;
390 for (i = 0; i < 3; i++)
391 s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
393 av_dlog(s->avctx, "%d %d %d %d %d %d\n",
394 s->width, s->height, s->linesize[0], s->linesize[1],
395 s->interlaced, s->avctx->height);
397 if (len != (8 + (3 * nb_components)))
398 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
401 /* totally blank picture as progressive JPEG will only add details to it */
402 if (s->progressive) {
403 int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
404 int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
405 for (i = 0; i < s->nb_components; i++) {
406 int size = bw * bh * s->h_count[i] * s->v_count[i];
407 av_freep(&s->blocks[i]);
408 av_freep(&s->last_nnz[i]);
409 s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
410 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
411 s->block_stride[i] = bw * s->h_count[i];
413 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
418 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
421 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
423 av_log(s->avctx, AV_LOG_WARNING,
424 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
425 0, dc_index, &s->vlcs[0][dc_index]);
430 return get_xbits(&s->gb, code);
435 /* decode block and dequantize */
436 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
437 int dc_index, int ac_index, int16_t *quant_matrix)
439 int code, i, j, level, val;
442 val = mjpeg_decode_dc(s, dc_index);
444 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
445 return AVERROR_INVALIDDATA;
447 val = val * quant_matrix[0] + s->last_dc[component];
448 s->last_dc[component] = val;
452 {OPEN_READER(re, &s->gb);
454 UPDATE_CACHE(re, &s->gb);
455 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
457 i += ((unsigned)code) >> 4;
460 if (code > MIN_CACHE_BITS - 16)
461 UPDATE_CACHE(re, &s->gb);
464 int cache = GET_CACHE(re, &s->gb);
465 int sign = (~cache) >> 31;
466 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
469 LAST_SKIP_BITS(re, &s->gb, code);
472 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
473 return AVERROR_INVALIDDATA;
475 j = s->scantable.permutated[i];
476 block[j] = level * quant_matrix[j];
479 CLOSE_READER(re, &s->gb);}
484 static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
485 int component, int dc_index,
486 int16_t *quant_matrix, int Al)
489 s->dsp.clear_block(block);
490 val = mjpeg_decode_dc(s, dc_index);
492 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
493 return AVERROR_INVALIDDATA;
495 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
496 s->last_dc[component] = val;
501 /* decode block and dequantize - progressive JPEG version */
502 static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
503 uint8_t *last_nnz, int ac_index,
504 int16_t *quant_matrix,
505 int ss, int se, int Al, int *EOBRUN)
507 int code, i, j, level, val, run;
515 OPEN_READER(re, &s->gb);
516 for (i = ss; ; i++) {
517 UPDATE_CACHE(re, &s->gb);
518 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
520 run = ((unsigned) code) >> 4;
524 if (code > MIN_CACHE_BITS - 16)
525 UPDATE_CACHE(re, &s->gb);
528 int cache = GET_CACHE(re, &s->gb);
529 int sign = (~cache) >> 31;
530 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
533 LAST_SKIP_BITS(re, &s->gb, code);
537 j = s->scantable.permutated[se];
538 block[j] = level * quant_matrix[j] << Al;
541 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
542 return AVERROR_INVALIDDATA;
544 j = s->scantable.permutated[i];
545 block[j] = level * quant_matrix[j] << Al;
547 if (run == 0xF) {// ZRL - skip 15 coefficients
550 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
551 return AVERROR_INVALIDDATA;
556 UPDATE_CACHE(re, &s->gb);
557 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
558 LAST_SKIP_BITS(re, &s->gb, run);
565 CLOSE_READER(re, &s->gb);
574 #define REFINE_BIT(j) { \
575 UPDATE_CACHE(re, &s->gb); \
576 sign = block[j] >> 15; \
577 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
578 ((quant_matrix[j] ^ sign) - sign) << Al; \
579 LAST_SKIP_BITS(re, &s->gb, 1); \
587 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
592 j = s->scantable.permutated[i]; \
595 else if (run-- == 0) \
599 /* decode block and dequantize - progressive JPEG refinement pass */
600 static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
602 int ac_index, int16_t *quant_matrix,
603 int ss, int se, int Al, int *EOBRUN)
605 int code, i = ss, j, sign, val, run;
606 int last = FFMIN(se, *last_nnz);
608 OPEN_READER(re, &s->gb);
613 UPDATE_CACHE(re, &s->gb);
614 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
617 run = ((unsigned) code) >> 4;
618 UPDATE_CACHE(re, &s->gb);
619 val = SHOW_UBITS(re, &s->gb, 1);
620 LAST_SKIP_BITS(re, &s->gb, 1);
622 j = s->scantable.permutated[i];
624 block[j] = ((quant_matrix[j]^val) - val) << Al;
628 CLOSE_READER(re, &s->gb);
632 run = ((unsigned) code) >> 4;
639 UPDATE_CACHE(re, &s->gb);
640 run += SHOW_UBITS(re, &s->gb, val);
641 LAST_SKIP_BITS(re, &s->gb, val);
653 for (; i <= last; i++) {
654 j = s->scantable.permutated[i];
658 CLOSE_READER(re, &s->gb);
665 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor,
669 uint16_t (*buffer)[4];
670 int left[3], top[3], topleft[3];
671 const int linesize = s->linesize[0];
672 const int mask = (1 << s->bits) - 1;
674 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
675 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
676 buffer = s->ljpeg_buffer;
678 for (i = 0; i < 3; i++)
679 buffer[0][i] = 1 << (s->bits + point_transform - 1);
681 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
682 const int modified_predictor = mb_y ? predictor : 1;
683 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
685 if (s->interlaced && s->bottom_field)
686 ptr += linesize >> 1;
688 for (i = 0; i < 3; i++)
689 top[i] = left[i] = topleft[i] = buffer[0][i];
691 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
692 if (s->restart_interval && !s->restart_count)
693 s->restart_count = s->restart_interval;
695 for (i = 0; i < 3; i++) {
699 top[i] = buffer[mb_x][i];
701 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
703 left[i] = buffer[mb_x][i] =
704 mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
707 if (s->restart_interval && !--s->restart_count) {
708 align_get_bits(&s->gb);
709 skip_bits(&s->gb, 16); /* skip RSTn */
714 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
715 ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
716 ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
717 ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
719 } else if (s->pegasus_rct) {
720 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
721 ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
722 ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
723 ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
726 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
727 ptr[4 * mb_x + 0] = buffer[mb_x][2];
728 ptr[4 * mb_x + 1] = buffer[mb_x][1];
729 ptr[4 * mb_x + 2] = buffer[mb_x][0];
736 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
737 int point_transform, int nb_components)
741 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
742 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
743 if (s->restart_interval && !s->restart_count)
744 s->restart_count = s->restart_interval;
746 if (mb_x == 0 || mb_y == 0 || s->interlaced) {
747 for (i = 0; i < nb_components; i++) {
749 int n, h, v, x, y, c, j, linesize;
751 c = s->comp_index[i];
756 linesize = s->linesize[c];
758 for (j = 0; j < n; j++) {
760 // FIXME optimize this crap
761 ptr = s->picture_ptr->data[c] +
762 (linesize * (v * mb_y + y)) +
764 if (y == 0 && mb_y == 0) {
765 if (x == 0 && mb_x == 0)
766 pred = 128 << point_transform;
770 if (x == 0 && mb_x == 0)
771 pred = ptr[-linesize];
773 PREDICT(pred, ptr[-linesize - 1],
774 ptr[-linesize], ptr[-1], predictor);
777 if (s->interlaced && s->bottom_field)
778 ptr += linesize >> 1;
779 *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
788 for (i = 0; i < nb_components; i++) {
790 int n, h, v, x, y, c, j, linesize;
792 c = s->comp_index[i];
797 linesize = s->linesize[c];
799 for (j = 0; j < n; j++) {
802 // FIXME optimize this crap
803 ptr = s->picture_ptr->data[c] +
804 (linesize * (v * mb_y + y)) +
806 PREDICT(pred, ptr[-linesize - 1],
807 ptr[-linesize], ptr[-1], predictor);
808 *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
816 if (s->restart_interval && !--s->restart_count) {
817 align_get_bits(&s->gb);
818 skip_bits(&s->gb, 16); /* skip RSTn */
825 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
826 int Al, const uint8_t *mb_bitmask,
827 const AVFrame *reference)
830 uint8_t *data[MAX_COMPONENTS];
831 const uint8_t *reference_data[MAX_COMPONENTS];
832 int linesize[MAX_COMPONENTS];
833 GetBitContext mb_bitmask_gb;
836 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
838 for (i = 0; i < nb_components; i++) {
839 int c = s->comp_index[i];
840 data[c] = s->picture_ptr->data[c];
841 reference_data[c] = reference ? reference->data[c] : NULL;
842 linesize[c] = s->linesize[c];
843 s->coefs_finished[c] |= 1;
846 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
847 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
848 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
850 if (s->restart_interval && !s->restart_count)
851 s->restart_count = s->restart_interval;
853 if (get_bits_left(&s->gb) < 0) {
854 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
855 -get_bits_left(&s->gb));
856 return AVERROR_INVALIDDATA;
858 for (i = 0; i < nb_components; i++) {
860 int n, h, v, x, y, c, j;
863 c = s->comp_index[i];
868 for (j = 0; j < n; j++) {
869 block_offset = ((linesize[c] * (v * mb_y + y) * 8) +
872 if (s->interlaced && s->bottom_field)
873 block_offset += linesize[c] >> 1;
874 ptr = data[c] + block_offset;
875 if (!s->progressive) {
877 s->hdsp.put_pixels_tab[1][0](ptr,
878 reference_data[c] + block_offset,
881 s->dsp.clear_block(s->block);
882 if (decode_block(s, s->block, i,
883 s->dc_index[i], s->ac_index[i],
884 s->quant_matrixes[s->quant_index[c]]) < 0) {
885 av_log(s->avctx, AV_LOG_ERROR,
886 "error y=%d x=%d\n", mb_y, mb_x);
887 return AVERROR_INVALIDDATA;
889 s->dsp.idct_put(ptr, linesize[c], s->block);
892 int block_idx = s->block_stride[c] * (v * mb_y + y) +
894 int16_t *block = s->blocks[c][block_idx];
896 block[0] += get_bits1(&s->gb) *
897 s->quant_matrixes[s->quant_index[c]][0] << Al;
898 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
899 s->quant_matrixes[s->quant_index[c]],
901 av_log(s->avctx, AV_LOG_ERROR,
902 "error y=%d x=%d\n", mb_y, mb_x);
903 return AVERROR_INVALIDDATA;
906 av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
907 av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
908 mb_x, mb_y, x, y, c, s->bottom_field,
909 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
917 if (s->restart_interval) {
919 i = 8 + ((-get_bits_count(&s->gb)) & 7);
921 if (show_bits(&s->gb, i) == (1 << i) - 1) {
922 int pos = get_bits_count(&s->gb);
923 align_get_bits(&s->gb);
924 while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
925 skip_bits(&s->gb, 8);
926 if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
927 for (i = 0; i < nb_components; i++) /* reset dc */
928 s->last_dc[i] = 1024;
930 skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
938 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
939 int se, int Ah, int Al,
940 const uint8_t *mb_bitmask,
941 const AVFrame *reference)
945 int c = s->comp_index[0];
946 uint8_t *data = s->picture_ptr->data[c];
947 const uint8_t *reference_data = reference ? reference->data[c] : NULL;
948 int linesize = s->linesize[c];
950 int16_t *quant_matrix = s->quant_matrixes[s->quant_index[c]];
951 GetBitContext mb_bitmask_gb;
953 if (ss < 0 || ss >= 64 ||
954 se < ss || se >= 64 ||
956 return AVERROR_INVALIDDATA;
959 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
962 s->coefs_finished[c] |= (1LL << (se + 1)) - (1LL << ss);
963 last_scan = !~s->coefs_finished[c];
966 if (s->interlaced && s->bottom_field) {
967 int offset = linesize >> 1;
969 reference_data += offset;
972 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
973 int block_offset = mb_y * linesize * 8;
974 uint8_t *ptr = data + block_offset;
975 int block_idx = mb_y * s->block_stride[c];
976 int16_t (*block)[64] = &s->blocks[c][block_idx];
977 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
978 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
979 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
984 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
985 quant_matrix, ss, se, Al, &EOBRUN);
987 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
988 quant_matrix, ss, se, Al, &EOBRUN);
990 av_log(s->avctx, AV_LOG_ERROR,
991 "error y=%d x=%d\n", mb_y, mb_x);
992 return AVERROR_INVALIDDATA;
998 s->hdsp.put_pixels_tab[1][0](ptr,
999 reference_data + block_offset,
1002 s->dsp.idct_put(ptr, linesize, *block);
1011 int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
1012 const AVFrame *reference)
1014 int len, nb_components, i, h, v, predictor, point_transform;
1016 const int block_size = s->lossless ? 1 : 8;
1017 int ilv, prev_shift;
1019 /* XXX: verify len field validity */
1020 len = get_bits(&s->gb, 16);
1021 nb_components = get_bits(&s->gb, 8);
1022 if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1023 av_log(s->avctx, AV_LOG_ERROR,
1024 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1025 return AVERROR_PATCHWELCOME;
1027 if (len != 6 + 2 * nb_components) {
1028 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1029 return AVERROR_INVALIDDATA;
1031 for (i = 0; i < nb_components; i++) {
1032 id = get_bits(&s->gb, 8) - 1;
1033 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1034 /* find component index */
1035 for (index = 0; index < s->nb_components; index++)
1036 if (id == s->component_id[index])
1038 if (index == s->nb_components) {
1039 av_log(s->avctx, AV_LOG_ERROR,
1040 "decode_sos: index(%d) out of components\n", index);
1041 return AVERROR_INVALIDDATA;
1043 /* Metasoft MJPEG codec has Cb and Cr swapped */
1044 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1045 && nb_components == 3 && s->nb_components == 3 && i)
1048 s->comp_index[i] = index;
1050 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1051 s->h_scount[i] = s->h_count[index];
1052 s->v_scount[i] = s->v_count[index];
1054 s->dc_index[i] = get_bits(&s->gb, 4);
1055 s->ac_index[i] = get_bits(&s->gb, 4);
1057 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1058 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1060 if (!s->vlcs[0][s->dc_index[i]].table ||
1061 !s->vlcs[1][s->ac_index[i]].table)
1065 predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1066 ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1067 prev_shift = get_bits(&s->gb, 4); /* Ah */
1068 point_transform = get_bits(&s->gb, 4); /* Al */
1070 if (nb_components > 1) {
1071 /* interleaved stream */
1072 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1073 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1074 } else if (!s->ls) { /* skip this for JPEG-LS */
1075 h = s->h_max / s->h_scount[0];
1076 v = s->v_max / s->v_scount[0];
1077 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1078 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1079 s->nb_blocks[0] = 1;
1084 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1085 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n",
1086 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1087 predictor, point_transform, ilv, s->bits,
1088 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
1091 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1092 for (i = s->mjpb_skiptosod; i > 0; i--)
1093 skip_bits(&s->gb, 8);
1096 for (i = 0; i < nb_components; i++)
1097 s->last_dc[i] = 1024;
1100 if (CONFIG_JPEGLS_DECODER && s->ls) {
1102 // reset_ls_coding_parameters(s, 0);
1104 if ((ret = ff_jpegls_decode_picture(s, predictor,
1105 point_transform, ilv)) < 0)
1109 if ((ret = ljpeg_decode_rgb_scan(s, predictor,
1110 point_transform)) < 0)
1113 if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1115 nb_components)) < 0)
1120 if (s->progressive && predictor) {
1121 if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1128 if ((ret = mjpeg_decode_scan(s, nb_components,
1129 prev_shift, point_transform,
1130 mb_bitmask, reference)) < 0)
1135 if (s->interlaced &&
1136 get_bits_left(&s->gb) > 32 &&
1137 show_bits(&s->gb, 8) == 0xFF) {
1138 GetBitContext bak = s->gb;
1139 align_get_bits(&bak);
1140 if (show_bits(&bak, 16) == 0xFFD1) {
1141 av_dlog(s->avctx, "AVRn interlaced picture marker found\n");
1143 skip_bits(&s->gb, 16);
1144 s->bottom_field ^= 1;
1153 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1154 return AVERROR_INVALIDDATA;
1157 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1159 if (get_bits(&s->gb, 16) != 4)
1160 return AVERROR_INVALIDDATA;
1161 s->restart_interval = get_bits(&s->gb, 16);
1162 s->restart_count = 0;
1163 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1164 s->restart_interval);
1169 static int mjpeg_decode_app(MJpegDecodeContext *s)
1173 len = get_bits(&s->gb, 16);
1175 return AVERROR_INVALIDDATA;
1176 if (8 * len > get_bits_left(&s->gb))
1177 return AVERROR_INVALIDDATA;
1179 id = get_bits_long(&s->gb, 32);
1180 id = av_be2ne32(id);
1183 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1184 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1186 /* Buggy AVID, it puts EOI only at every 10th frame. */
1187 /* Also, this fourcc is used by non-avid files too, it holds some
1188 information, but it's always present in AVID-created files. */
1189 if (id == AV_RL32("AVI1")) {
1195 4bytes field_size_less_padding
1198 i = get_bits(&s->gb, 8);
1200 s->bottom_field = 1;
1202 s->bottom_field = 0;
1204 skip_bits(&s->gb, 8);
1205 skip_bits(&s->gb, 32);
1206 skip_bits(&s->gb, 32);
1214 if (id == AV_RL32("JFIF")) {
1215 int t_w, t_h, v1, v2;
1216 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1217 v1 = get_bits(&s->gb, 8);
1218 v2 = get_bits(&s->gb, 8);
1219 skip_bits(&s->gb, 8);
1221 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1222 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1224 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1225 av_log(s->avctx, AV_LOG_INFO,
1226 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1228 s->avctx->sample_aspect_ratio.num,
1229 s->avctx->sample_aspect_ratio.den);
1231 t_w = get_bits(&s->gb, 8);
1232 t_h = get_bits(&s->gb, 8);
1234 /* skip thumbnail */
1235 if (len -10 - (t_w * t_h * 3) > 0)
1236 len -= t_w * t_h * 3;
1242 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1243 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1244 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1245 skip_bits(&s->gb, 16); /* version */
1246 skip_bits(&s->gb, 16); /* flags0 */
1247 skip_bits(&s->gb, 16); /* flags1 */
1248 skip_bits(&s->gb, 8); /* transform */
1253 if (id == AV_RL32("LJIF")) {
1254 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1255 av_log(s->avctx, AV_LOG_INFO,
1256 "Pegasus lossless jpeg header found\n");
1257 skip_bits(&s->gb, 16); /* version ? */
1258 skip_bits(&s->gb, 16); /* unknwon always 0? */
1259 skip_bits(&s->gb, 16); /* unknwon always 0? */
1260 skip_bits(&s->gb, 16); /* unknwon always 0? */
1261 switch (get_bits(&s->gb, 8)) {
1271 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1278 if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1279 id = get_bits_long(&s->gb, 32);
1280 id = av_be2ne32(id);
1283 if (id == AV_RL32("mjpg")) {
1285 skip_bits(&s->gb, 32); /* field size */
1286 skip_bits(&s->gb, 32); /* pad field size */
1287 skip_bits(&s->gb, 32); /* next off */
1288 skip_bits(&s->gb, 32); /* quant off */
1289 skip_bits(&s->gb, 32); /* huff off */
1290 skip_bits(&s->gb, 32); /* image off */
1291 skip_bits(&s->gb, 32); /* scan off */
1292 skip_bits(&s->gb, 32); /* data off */
1294 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1295 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1300 /* slow but needed for extreme adobe jpegs */
1302 av_log(s->avctx, AV_LOG_ERROR,
1303 "mjpeg: error, decode_app parser read over the end\n");
1305 skip_bits(&s->gb, 8);
1310 static int mjpeg_decode_com(MJpegDecodeContext *s)
1312 int len = get_bits(&s->gb, 16);
1313 if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1314 char *cbuf = av_malloc(len - 1);
1317 for (i = 0; i < len - 2; i++)
1318 cbuf[i] = get_bits(&s->gb, 8);
1319 if (i > 0 && cbuf[i - 1] == '\n')
1324 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1325 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1327 /* buggy avid, it puts EOI only at every 10th frame */
1328 if (!strcmp(cbuf, "AVID")) {
1330 } else if (!strcmp(cbuf, "CS=ITU601"))
1332 else if ((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1333 (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1343 /* return the 8 bit start code value and update the search
1344 state. Return -1 if no start code found */
1345 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1347 const uint8_t *buf_ptr;
1354 buf_ptr = *pbuf_ptr;
1355 while (buf_ptr < buf_end) {
1358 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1368 av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1369 *pbuf_ptr = buf_ptr;
1373 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1374 const uint8_t **buf_ptr, const uint8_t *buf_end,
1375 const uint8_t **unescaped_buf_ptr,
1376 int *unescaped_buf_size)
1379 start_code = find_marker(buf_ptr, buf_end);
1381 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1383 return AVERROR(ENOMEM);
1385 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1386 if (start_code == SOS && !s->ls) {
1387 const uint8_t *src = *buf_ptr;
1388 uint8_t *dst = s->buffer;
1390 while (src < buf_end) {
1391 uint8_t x = *(src++);
1394 if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1396 while (src < buf_end && x == 0xff)
1399 if (x >= 0xd0 && x <= 0xd7)
1406 *unescaped_buf_ptr = s->buffer;
1407 *unescaped_buf_size = dst - s->buffer;
1408 memset(s->buffer + *unescaped_buf_size, 0,
1409 FF_INPUT_BUFFER_PADDING_SIZE);
1411 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1412 (buf_end - *buf_ptr) - (dst - s->buffer));
1413 } else if (start_code == SOS && s->ls) {
1414 const uint8_t *src = *buf_ptr;
1415 uint8_t *dst = s->buffer;
1423 while (src + t < buf_end) {
1424 uint8_t x = src[t++];
1426 while ((src + t < buf_end) && x == 0xff)
1435 init_put_bits(&pb, dst, t);
1437 /* unescape bitstream */
1439 uint8_t x = src[b++];
1440 put_bits(&pb, 8, x);
1443 put_bits(&pb, 7, x);
1447 flush_put_bits(&pb);
1449 *unescaped_buf_ptr = dst;
1450 *unescaped_buf_size = (bit_count + 7) >> 3;
1451 memset(s->buffer + *unescaped_buf_size, 0,
1452 FF_INPUT_BUFFER_PADDING_SIZE);
1454 *unescaped_buf_ptr = *buf_ptr;
1455 *unescaped_buf_size = buf_end - *buf_ptr;
1461 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1464 AVFrame *frame = data;
1465 const uint8_t *buf = avpkt->data;
1466 int buf_size = avpkt->size;
1467 MJpegDecodeContext *s = avctx->priv_data;
1468 const uint8_t *buf_end, *buf_ptr;
1469 const uint8_t *unescaped_buf_ptr;
1470 int unescaped_buf_size;
1474 s->got_picture = 0; // picture from previous image can not be reused
1476 buf_end = buf + buf_size;
1477 while (buf_ptr < buf_end) {
1478 /* find start next marker */
1479 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1481 &unescaped_buf_size);
1483 if (start_code < 0) {
1485 } else if (unescaped_buf_size > INT_MAX / 8) {
1486 av_log(avctx, AV_LOG_ERROR,
1487 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1488 start_code, unescaped_buf_size, buf_size);
1489 return AVERROR_INVALIDDATA;
1492 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
1493 start_code, buf_end - buf_ptr);
1495 ret = init_get_bits(&s->gb, unescaped_buf_ptr,
1496 unescaped_buf_size * 8);
1500 s->start_code = start_code;
1501 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1502 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1504 /* process markers */
1505 if (start_code >= 0xd0 && start_code <= 0xd7)
1506 av_log(avctx, AV_LOG_DEBUG,
1507 "restart marker: %d\n", start_code & 0x0f);
1509 else if (start_code >= APP0 && start_code <= APP15)
1510 mjpeg_decode_app(s);
1512 else if (start_code == COM)
1513 mjpeg_decode_com(s);
1515 if (!CONFIG_JPEGLS_DECODER &&
1516 (start_code == SOF48 || start_code == LSE)) {
1517 av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
1518 return AVERROR(ENOSYS);
1521 switch (start_code) {
1523 s->restart_interval = 0;
1524 s->restart_count = 0;
1525 /* nothing to do on SOI */
1528 ff_mjpeg_decode_dqt(s);
1531 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1532 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1541 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1548 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1555 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1562 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1566 if (!CONFIG_JPEGLS_DECODER ||
1567 (ret = ff_jpegls_decode_lse(s)) < 0)
1572 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1575 if (!s->got_picture) {
1576 av_log(avctx, AV_LOG_WARNING,
1577 "Found EOI before any SOF, ignoring\n");
1580 if (s->interlaced) {
1581 s->bottom_field ^= 1;
1582 /* if not bottom field, do not output image yet */
1583 if (s->bottom_field == !s->interlace_polarity)
1586 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
1590 for (i = 0; frame->data[i]; i++) {
1591 int h = frame->height >> ((i == 1 || i == 2) ?
1592 s->pix_desc->log2_chroma_h : 0);
1593 frame->data[i] += frame->linesize[i] * (h - 1);
1594 frame->linesize[i] *= -1;
1600 avctx->debug & FF_DEBUG_QP) {
1601 av_log(avctx, AV_LOG_DEBUG,
1602 "QP: %d\n", FFMAX3(s->qscale[0],
1609 if (!s->got_picture) {
1610 av_log(avctx, AV_LOG_WARNING,
1611 "Can not process SOS before SOF, skipping\n");
1614 if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&
1615 (avctx->err_recognition & AV_EF_EXPLODE))
1617 /* buggy avid puts EOI every 10-20th frame */
1618 /* if restart period is over process EOI */
1619 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1623 mjpeg_decode_dri(s);
1635 av_log(avctx, AV_LOG_ERROR,
1636 "mjpeg: unsupported coding type (%x)\n", start_code);
1641 /* eof process start code */
1642 buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
1643 av_log(avctx, AV_LOG_DEBUG,
1644 "marker parser used %d bytes (%d bits)\n",
1645 (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
1647 if (s->got_picture) {
1648 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1651 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1652 return AVERROR_INVALIDDATA;
1654 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n",
1656 // return buf_end - buf_ptr;
1657 return buf_ptr - buf;
1660 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
1662 MJpegDecodeContext *s = avctx->priv_data;
1666 av_frame_free(&s->picture);
1667 s->picture_ptr = NULL;
1668 } else if (s->picture_ptr)
1669 av_frame_unref(s->picture_ptr);
1672 av_freep(&s->ljpeg_buffer);
1673 s->ljpeg_buffer_size = 0;
1675 for (i = 0; i < 3; i++) {
1676 for (j = 0; j < 4; j++)
1677 ff_free_vlc(&s->vlcs[i][j]);
1679 for (i = 0; i < MAX_COMPONENTS; i++) {
1680 av_freep(&s->blocks[i]);
1681 av_freep(&s->last_nnz[i]);
1686 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1687 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1688 static const AVOption options[] = {
1689 { "extern_huff", "Use external huffman table.",
1690 OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1694 static const AVClass mjpegdec_class = {
1695 .class_name = "MJPEG decoder",
1696 .item_name = av_default_item_name,
1698 .version = LIBAVUTIL_VERSION_INT,
1701 AVCodec ff_mjpeg_decoder = {
1703 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1704 .type = AVMEDIA_TYPE_VIDEO,
1705 .id = AV_CODEC_ID_MJPEG,
1706 .priv_data_size = sizeof(MJpegDecodeContext),
1707 .init = ff_mjpeg_decode_init,
1708 .close = ff_mjpeg_decode_end,
1709 .decode = ff_mjpeg_decode_frame,
1710 .capabilities = CODEC_CAP_DR1,
1711 .priv_class = &mjpegdec_class,
1714 AVCodec ff_thp_decoder = {
1716 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
1717 .type = AVMEDIA_TYPE_VIDEO,
1718 .id = AV_CODEC_ID_THP,
1719 .priv_data_size = sizeof(MJpegDecodeContext),
1720 .init = ff_mjpeg_decode_init,
1721 .close = ff_mjpeg_decode_end,
1722 .decode = ff_mjpeg_decode_frame,
1723 .capabilities = CODEC_CAP_DR1,