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"
41 #include "jpegtables.h"
44 #include "jpeglsdec.h"
48 #include "bytestream.h"
51 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
52 const uint8_t *val_table, int nb_codes,
53 int use_static, int is_ac)
55 uint8_t huff_size[256] = { 0 };
56 uint16_t huff_code[256];
57 uint16_t huff_sym[256];
60 av_assert0(nb_codes <= 256);
62 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
64 for (i = 0; i < 256; i++)
65 huff_sym[i] = i + 16 * is_ac;
68 huff_sym[0] = 16 * 256;
70 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
71 huff_code, 2, 2, huff_sym, 2, 2, use_static);
74 static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
76 build_vlc(&s->vlcs[0][0], avpriv_mjpeg_bits_dc_luminance,
77 avpriv_mjpeg_val_dc, 12, 0, 0);
78 build_vlc(&s->vlcs[0][1], avpriv_mjpeg_bits_dc_chrominance,
79 avpriv_mjpeg_val_dc, 12, 0, 0);
80 build_vlc(&s->vlcs[1][0], avpriv_mjpeg_bits_ac_luminance,
81 avpriv_mjpeg_val_ac_luminance, 251, 0, 1);
82 build_vlc(&s->vlcs[1][1], avpriv_mjpeg_bits_ac_chrominance,
83 avpriv_mjpeg_val_ac_chrominance, 251, 0, 1);
84 build_vlc(&s->vlcs[2][0], avpriv_mjpeg_bits_ac_luminance,
85 avpriv_mjpeg_val_ac_luminance, 251, 0, 0);
86 build_vlc(&s->vlcs[2][1], avpriv_mjpeg_bits_ac_chrominance,
87 avpriv_mjpeg_val_ac_chrominance, 251, 0, 0);
90 static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
93 if (len > 14 && buf[12] == 1) /* 1 - NTSC */
94 s->interlace_polarity = 1;
95 if (len > 14 && buf[12] == 2) /* 2 - PAL */
96 s->interlace_polarity = 0;
97 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
98 av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 14 ? buf[12] : -1);
101 static void init_idct(AVCodecContext *avctx)
103 MJpegDecodeContext *s = avctx->priv_data;
105 ff_idctdsp_init(&s->idsp, avctx);
106 ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
110 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
112 MJpegDecodeContext *s = avctx->priv_data;
114 if (!s->picture_ptr) {
115 s->picture = av_frame_alloc();
117 return AVERROR(ENOMEM);
118 s->picture_ptr = s->picture;
122 ff_blockdsp_init(&s->bdsp, avctx);
123 ff_hpeldsp_init(&s->hdsp, avctx->flags);
128 s->first_picture = 1;
130 s->org_height = avctx->coded_height;
131 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
132 avctx->colorspace = AVCOL_SPC_BT470BG;
134 build_basic_mjpeg_vlc(s);
136 if (s->extern_huff) {
137 av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
138 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
139 if (ff_mjpeg_decode_dht(s)) {
140 av_log(avctx, AV_LOG_ERROR,
141 "error using external huffman table, switching back to internal\n");
142 build_basic_mjpeg_vlc(s);
145 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
146 s->interlace_polarity = 1; /* bottom field first */
147 av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
148 } else if (avctx->field_order == AV_FIELD_UNKNOWN) {
149 if (avctx->codec_tag == AV_RL32("MJPG"))
150 s->interlace_polarity = 1;
153 if ( avctx->extradata_size > 8
154 && AV_RL32(avctx->extradata) == 0x2C
155 && AV_RL32(avctx->extradata+4) == 0x18) {
156 parse_avid(s, avctx->extradata, avctx->extradata_size);
159 if (avctx->codec->id == AV_CODEC_ID_AMV)
166 /* quantize tables */
167 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
169 int len, index, i, j;
171 len = get_bits(&s->gb, 16) - 2;
173 if (8*len > get_bits_left(&s->gb)) {
174 av_log(s->avctx, AV_LOG_ERROR, "dqt: len %d is too large\n", len);
175 return AVERROR_INVALIDDATA;
179 int pr = get_bits(&s->gb, 4);
181 av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
182 return AVERROR_INVALIDDATA;
184 index = get_bits(&s->gb, 4);
187 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
188 /* read quant table */
189 for (i = 0; i < 64; i++) {
190 j = s->scantable.permutated[i];
191 s->quant_matrixes[index][j] = get_bits(&s->gb, pr ? 16 : 8);
194 // XXX FIXME finetune, and perhaps add dc too
195 s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
196 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
197 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
198 index, s->qscale[index]);
199 len -= 1 + 64 * (1+pr);
204 /* decode huffman tables and build VLC decoders */
205 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
207 int len, index, i, class, n, v, code_max;
208 uint8_t bits_table[17];
209 uint8_t val_table[256];
212 len = get_bits(&s->gb, 16) - 2;
214 if (8*len > get_bits_left(&s->gb)) {
215 av_log(s->avctx, AV_LOG_ERROR, "dht: len %d is too large\n", len);
216 return AVERROR_INVALIDDATA;
221 return AVERROR_INVALIDDATA;
222 class = get_bits(&s->gb, 4);
224 return AVERROR_INVALIDDATA;
225 index = get_bits(&s->gb, 4);
227 return AVERROR_INVALIDDATA;
229 for (i = 1; i <= 16; i++) {
230 bits_table[i] = get_bits(&s->gb, 8);
234 if (len < n || n > 256)
235 return AVERROR_INVALIDDATA;
238 for (i = 0; i < n; i++) {
239 v = get_bits(&s->gb, 8);
246 /* build VLC and flush previous vlc if present */
247 ff_free_vlc(&s->vlcs[class][index]);
248 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
249 class, index, code_max + 1);
250 if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
251 code_max + 1, 0, class > 0)) < 0)
255 ff_free_vlc(&s->vlcs[2][index]);
256 if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
257 code_max + 1, 0, 0)) < 0)
264 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
266 int len, nb_components, i, width, height, bits, ret;
268 int h_count[MAX_COMPONENTS] = { 0 };
269 int v_count[MAX_COMPONENTS] = { 0 };
272 memset(s->upscale_h, 0, sizeof(s->upscale_h));
273 memset(s->upscale_v, 0, sizeof(s->upscale_v));
275 /* XXX: verify len field validity */
276 len = get_bits(&s->gb, 16);
277 bits = get_bits(&s->gb, 8);
279 if (bits > 16 || bits < 1) {
280 av_log(s->avctx, AV_LOG_ERROR, "bits %d is invalid\n", bits);
281 return AVERROR_INVALIDDATA;
284 if (s->avctx->bits_per_raw_sample != bits) {
285 av_log(s->avctx, AV_LOG_INFO, "Changing bps to %d\n", bits);
286 s->avctx->bits_per_raw_sample = bits;
291 if (bits == 9 && !s->pegasus_rct)
292 s->rct = 1; // FIXME ugly
294 if(s->lossless && s->avctx->lowres){
295 av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
299 height = get_bits(&s->gb, 16);
300 width = get_bits(&s->gb, 16);
302 // HACK for odd_height.mov
303 if (s->interlaced && s->width == width && s->height == height + 1)
306 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
307 if (av_image_check_size(width, height, 0, s->avctx))
308 return AVERROR_INVALIDDATA;
310 nb_components = get_bits(&s->gb, 8);
311 if (nb_components <= 0 ||
312 nb_components > MAX_COMPONENTS)
314 if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
315 if (nb_components != s->nb_components) {
316 av_log(s->avctx, AV_LOG_ERROR,
317 "nb_components changing in interlaced picture\n");
318 return AVERROR_INVALIDDATA;
321 if (s->ls && !(bits <= 8 || nb_components == 1)) {
322 avpriv_report_missing_feature(s->avctx,
323 "JPEG-LS that is not <= 8 "
324 "bits/component or 16-bit gray");
325 return AVERROR_PATCHWELCOME;
327 s->nb_components = nb_components;
330 for (i = 0; i < nb_components; i++) {
332 s->component_id[i] = get_bits(&s->gb, 8) - 1;
333 h_count[i] = get_bits(&s->gb, 4);
334 v_count[i] = get_bits(&s->gb, 4);
335 /* compute hmax and vmax (only used in interleaved case) */
336 if (h_count[i] > s->h_max)
337 s->h_max = h_count[i];
338 if (v_count[i] > s->v_max)
339 s->v_max = v_count[i];
340 s->quant_index[i] = get_bits(&s->gb, 8);
341 if (s->quant_index[i] >= 4) {
342 av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
343 return AVERROR_INVALIDDATA;
345 if (!h_count[i] || !v_count[i]) {
346 av_log(s->avctx, AV_LOG_ERROR,
347 "Invalid sampling factor in component %d %d:%d\n",
348 i, h_count[i], v_count[i]);
349 return AVERROR_INVALIDDATA;
352 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
353 i, h_count[i], v_count[i],
354 s->component_id[i], s->quant_index[i]);
356 if ( nb_components == 4
357 && s->component_id[0] == 'C' - 1
358 && s->component_id[1] == 'M' - 1
359 && s->component_id[2] == 'Y' - 1
360 && s->component_id[3] == 'K' - 1)
361 s->adobe_transform = 0;
363 if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
364 avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
365 return AVERROR_PATCHWELCOME;
369 /* if different size, realloc/alloc picture */
370 if (width != s->width || height != s->height || bits != s->bits ||
371 memcmp(s->h_count, h_count, sizeof(h_count)) ||
372 memcmp(s->v_count, v_count, sizeof(v_count))) {
377 memcpy(s->h_count, h_count, sizeof(h_count));
378 memcpy(s->v_count, v_count, sizeof(v_count));
382 /* test interlaced mode */
383 if (s->first_picture &&
384 (s->multiscope != 2 || s->avctx->time_base.den >= 25 * s->avctx->time_base.num) &&
385 s->org_height != 0 &&
386 s->height < ((s->org_height * 3) / 4)) {
388 s->bottom_field = s->interlace_polarity;
389 s->picture_ptr->interlaced_frame = 1;
390 s->picture_ptr->top_field_first = !s->interlace_polarity;
394 ret = ff_set_dimensions(s->avctx, width, height);
398 s->first_picture = 0;
401 if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
402 if (s->progressive) {
403 avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
404 return AVERROR_INVALIDDATA;
407 if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4))
409 else if (!s->lossless)
411 /* XXX: not complete test ! */
412 pix_fmt_id = ((unsigned)s->h_count[0] << 28) | (s->v_count[0] << 24) |
413 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
414 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
415 (s->h_count[3] << 4) | s->v_count[3];
416 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
417 /* NOTE we do not allocate pictures large enough for the possible
418 * padding of h/v_count being 4 */
419 if (!(pix_fmt_id & 0xD0D0D0D0))
420 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
421 if (!(pix_fmt_id & 0x0D0D0D0D))
422 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
424 for (i = 0; i < 8; i++) {
425 int j = 6 + (i&1) - (i&6);
426 int is = (pix_fmt_id >> (4*i)) & 0xF;
427 int js = (pix_fmt_id >> (4*j)) & 0xF;
429 if (is == 1 && js != 2 && (i < 2 || i > 5))
430 js = (pix_fmt_id >> ( 8 + 4*(i&1))) & 0xF;
431 if (is == 1 && js != 2 && (i < 2 || i > 5))
432 js = (pix_fmt_id >> (16 + 4*(i&1))) & 0xF;
434 if (is == 1 && js == 2) {
435 if (i & 1) s->upscale_h[j/2] = 1;
436 else s->upscale_v[j/2] = 1;
440 switch (pix_fmt_id) {
443 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_BGR48;
445 if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
446 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_GBRP : AV_PIX_FMT_GBRP16;
448 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
449 else s->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
450 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
453 av_assert0(s->nb_components == 3);
457 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA64;
459 if (s->adobe_transform == 0 && s->bits <= 8) {
460 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
462 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_YUVA444P : AV_PIX_FMT_YUVA444P16;
463 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
466 av_assert0(s->nb_components == 4);
470 if (s->adobe_transform == 0 && s->bits <= 8) {
471 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
472 s->upscale_v[1] = s->upscale_v[2] = 1;
473 s->upscale_h[1] = s->upscale_h[2] = 1;
474 } else if (s->adobe_transform == 2 && s->bits <= 8) {
475 s->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
476 s->upscale_v[1] = s->upscale_v[2] = 1;
477 s->upscale_h[1] = s->upscale_h[2] = 1;
478 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
480 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
481 else s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16;
482 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
484 av_assert0(s->nb_components == 4);
490 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
493 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
498 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
501 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
513 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
515 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
522 if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
523 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
526 s->upscale_v[0] = s->upscale_v[1] = 1;
528 if (pix_fmt_id == 0x14111100)
529 s->upscale_v[1] = s->upscale_v[2] = 1;
530 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P;
533 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
537 if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
538 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
541 s->upscale_h[0] = s->upscale_h[1] = 1;
543 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
544 else s->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
545 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
551 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
552 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
553 s->upscale_h[1] = s->upscale_h[2] = 2;
557 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
560 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
565 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
566 else s->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
567 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
568 if (pix_fmt_id == 0x42111100) {
571 s->upscale_h[1] = s->upscale_h[2] = 1;
572 } else if (pix_fmt_id == 0x24111100) {
575 s->upscale_v[1] = s->upscale_v[2] = 1;
579 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV411P : AV_PIX_FMT_YUVJ411P;
582 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
586 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x bits:%d\n", pix_fmt_id, s->bits);
587 memset(s->upscale_h, 0, sizeof(s->upscale_h));
588 memset(s->upscale_v, 0, sizeof(s->upscale_v));
589 return AVERROR_PATCHWELCOME;
591 if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->avctx->lowres) {
592 av_log(s->avctx, AV_LOG_ERROR, "lowres not supported for weird subsampling\n");
593 return AVERROR_PATCHWELCOME;
596 memset(s->upscale_h, 0, sizeof(s->upscale_h));
597 memset(s->upscale_v, 0, sizeof(s->upscale_v));
598 if (s->nb_components == 3) {
599 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
600 } else if (s->nb_components != 1) {
601 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of components %d\n", s->nb_components);
602 return AVERROR_PATCHWELCOME;
603 } else if (s->palette_index && s->bits <= 8)
604 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
605 else if (s->bits <= 8)
606 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
608 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
611 s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
613 av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
617 av_frame_unref(s->picture_ptr);
618 if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0)
620 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
621 s->picture_ptr->key_frame = 1;
624 for (i = 0; i < 4; i++)
625 s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
627 ff_dlog(s->avctx, "%d %d %d %d %d %d\n",
628 s->width, s->height, s->linesize[0], s->linesize[1],
629 s->interlaced, s->avctx->height);
631 if (len != (8 + (3 * nb_components)))
632 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
635 if ((s->rgb && !s->lossless && !s->ls) ||
636 (!s->rgb && s->ls && s->nb_components > 1)) {
637 av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
638 return AVERROR_PATCHWELCOME;
641 /* totally blank picture as progressive JPEG will only add details to it */
642 if (s->progressive) {
643 int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
644 int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
645 for (i = 0; i < s->nb_components; i++) {
646 int size = bw * bh * s->h_count[i] * s->v_count[i];
647 av_freep(&s->blocks[i]);
648 av_freep(&s->last_nnz[i]);
649 s->blocks[i] = av_mallocz_array(size, sizeof(**s->blocks));
650 s->last_nnz[i] = av_mallocz_array(size, sizeof(**s->last_nnz));
651 if (!s->blocks[i] || !s->last_nnz[i])
652 return AVERROR(ENOMEM);
653 s->block_stride[i] = bw * s->h_count[i];
655 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
660 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
663 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
664 if (code < 0 || code > 16) {
665 av_log(s->avctx, AV_LOG_WARNING,
666 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
667 0, dc_index, &s->vlcs[0][dc_index]);
672 return get_xbits(&s->gb, code);
677 /* decode block and dequantize */
678 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
679 int dc_index, int ac_index, int16_t *quant_matrix)
681 int code, i, j, level, val;
684 val = mjpeg_decode_dc(s, dc_index);
685 if (val == 0xfffff) {
686 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
687 return AVERROR_INVALIDDATA;
689 val = val * quant_matrix[0] + s->last_dc[component];
690 val = FFMIN(val, 32767);
691 s->last_dc[component] = val;
695 {OPEN_READER(re, &s->gb);
697 UPDATE_CACHE(re, &s->gb);
698 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
700 i += ((unsigned)code) >> 4;
703 if (code > MIN_CACHE_BITS - 16)
704 UPDATE_CACHE(re, &s->gb);
707 int cache = GET_CACHE(re, &s->gb);
708 int sign = (~cache) >> 31;
709 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
712 LAST_SKIP_BITS(re, &s->gb, code);
715 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
716 return AVERROR_INVALIDDATA;
718 j = s->scantable.permutated[i];
719 block[j] = level * quant_matrix[j];
722 CLOSE_READER(re, &s->gb);}
727 static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
728 int component, int dc_index,
729 int16_t *quant_matrix, int Al)
732 s->bdsp.clear_block(block);
733 val = mjpeg_decode_dc(s, dc_index);
734 if (val == 0xfffff) {
735 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
736 return AVERROR_INVALIDDATA;
738 val = (val * (quant_matrix[0] << Al)) + s->last_dc[component];
739 s->last_dc[component] = val;
744 /* decode block and dequantize - progressive JPEG version */
745 static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
746 uint8_t *last_nnz, int ac_index,
747 int16_t *quant_matrix,
748 int ss, int se, int Al, int *EOBRUN)
750 int code, i, j, level, val, run;
758 OPEN_READER(re, &s->gb);
759 for (i = ss; ; i++) {
760 UPDATE_CACHE(re, &s->gb);
761 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
763 run = ((unsigned) code) >> 4;
767 if (code > MIN_CACHE_BITS - 16)
768 UPDATE_CACHE(re, &s->gb);
771 int cache = GET_CACHE(re, &s->gb);
772 int sign = (~cache) >> 31;
773 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
776 LAST_SKIP_BITS(re, &s->gb, code);
780 j = s->scantable.permutated[se];
781 block[j] = level * (quant_matrix[j] << Al);
784 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
785 return AVERROR_INVALIDDATA;
787 j = s->scantable.permutated[i];
788 block[j] = level * (quant_matrix[j] << Al);
790 if (run == 0xF) {// ZRL - skip 15 coefficients
793 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
794 return AVERROR_INVALIDDATA;
799 UPDATE_CACHE(re, &s->gb);
800 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
801 LAST_SKIP_BITS(re, &s->gb, run);
808 CLOSE_READER(re, &s->gb);
817 #define REFINE_BIT(j) { \
818 UPDATE_CACHE(re, &s->gb); \
819 sign = block[j] >> 15; \
820 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
821 ((quant_matrix[j] ^ sign) - sign) << Al; \
822 LAST_SKIP_BITS(re, &s->gb, 1); \
830 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
835 j = s->scantable.permutated[i]; \
838 else if (run-- == 0) \
842 /* decode block and dequantize - progressive JPEG refinement pass */
843 static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
845 int ac_index, int16_t *quant_matrix,
846 int ss, int se, int Al, int *EOBRUN)
848 int code, i = ss, j, sign, val, run;
849 int last = FFMIN(se, *last_nnz);
851 OPEN_READER(re, &s->gb);
856 UPDATE_CACHE(re, &s->gb);
857 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
860 run = ((unsigned) code) >> 4;
861 UPDATE_CACHE(re, &s->gb);
862 val = SHOW_UBITS(re, &s->gb, 1);
863 LAST_SKIP_BITS(re, &s->gb, 1);
865 j = s->scantable.permutated[i];
867 block[j] = ((quant_matrix[j] << Al) ^ val) - val;
871 CLOSE_READER(re, &s->gb);
875 run = ((unsigned) code) >> 4;
882 UPDATE_CACHE(re, &s->gb);
883 run += SHOW_UBITS(re, &s->gb, val);
884 LAST_SKIP_BITS(re, &s->gb, val);
896 for (; i <= last; i++) {
897 j = s->scantable.permutated[i];
901 CLOSE_READER(re, &s->gb);
908 static int handle_rstn(MJpegDecodeContext *s, int nb_components)
913 if (s->restart_interval) {
915 if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
916 align_get_bits(&s->gb);
917 for (i = 0; i < nb_components; i++) /* reset dc */
918 s->last_dc[i] = (4 << s->bits);
921 i = 8 + ((-get_bits_count(&s->gb)) & 7);
923 if (s->restart_count == 0) {
924 if( show_bits(&s->gb, i) == (1 << i) - 1
925 || show_bits(&s->gb, i) == 0xFF) {
926 int pos = get_bits_count(&s->gb);
927 align_get_bits(&s->gb);
928 while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
929 skip_bits(&s->gb, 8);
930 if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
931 for (i = 0; i < nb_components; i++) /* reset dc */
932 s->last_dc[i] = (4 << s->bits);
935 skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
942 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
945 uint16_t (*buffer)[4];
946 int left[4], top[4], topleft[4];
947 const int linesize = s->linesize[0];
948 const int mask = ((1 << s->bits) - 1) << point_transform;
952 if (s->nb_components != 3 && s->nb_components != 4)
953 return AVERROR_INVALIDDATA;
954 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
955 return AVERROR_INVALIDDATA;
958 s->restart_count = s->restart_interval;
960 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
961 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
962 buffer = s->ljpeg_buffer;
964 for (i = 0; i < 4; i++)
965 buffer[0][i] = 1 << (s->bits - 1);
967 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
968 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
970 if (s->interlaced && s->bottom_field)
971 ptr += linesize >> 1;
973 for (i = 0; i < 4; i++)
974 top[i] = left[i] = topleft[i] = buffer[0][i];
976 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
977 int modified_predictor = predictor;
979 if (s->restart_interval && !s->restart_count){
980 s->restart_count = s->restart_interval;
984 top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
986 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
987 modified_predictor = 1;
989 for (i=0;i<nb_components;i++) {
993 top[i] = buffer[mb_x][i];
995 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
997 dc = mjpeg_decode_dc(s, s->dc_index[i]);
1001 left[i] = buffer[mb_x][i] =
1002 mask & (pred + (dc * (1 << point_transform)));
1005 if (s->restart_interval && !--s->restart_count) {
1006 align_get_bits(&s->gb);
1007 skip_bits(&s->gb, 16); /* skip RSTn */
1010 if (s->rct && s->nb_components == 4) {
1011 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1012 ptr[4*mb_x + 2] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1013 ptr[4*mb_x + 1] = buffer[mb_x][1] + ptr[4*mb_x + 2];
1014 ptr[4*mb_x + 3] = buffer[mb_x][2] + ptr[4*mb_x + 2];
1015 ptr[4*mb_x + 0] = buffer[mb_x][3];
1017 } else if (s->nb_components == 4) {
1018 for(i=0; i<nb_components; i++) {
1019 int c= s->comp_index[i];
1021 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1022 ptr[4*mb_x+3-c] = buffer[mb_x][i];
1024 } else if(s->bits == 9) {
1025 return AVERROR_PATCHWELCOME;
1027 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1028 ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i];
1032 } else if (s->rct) {
1033 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1034 ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1035 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1036 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1038 } else if (s->pegasus_rct) {
1039 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1040 ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
1041 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1042 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1045 for(i=0; i<nb_components; i++) {
1046 int c= s->comp_index[i];
1048 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1049 ptr[3*mb_x+2-c] = buffer[mb_x][i];
1051 } else if(s->bits == 9) {
1052 return AVERROR_PATCHWELCOME;
1054 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1055 ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i];
1064 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
1065 int point_transform, int nb_components)
1067 int i, mb_x, mb_y, mask;
1068 int bits= (s->bits+7)&~7;
1069 int resync_mb_y = 0;
1070 int resync_mb_x = 0;
1072 point_transform += bits - s->bits;
1073 mask = ((1 << s->bits) - 1) << point_transform;
1075 av_assert0(nb_components>=1 && nb_components<=4);
1077 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1078 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1079 if (s->restart_interval && !s->restart_count){
1080 s->restart_count = s->restart_interval;
1085 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
1086 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1087 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1088 for (i = 0; i < nb_components; i++) {
1091 int n, h, v, x, y, c, j, linesize;
1092 n = s->nb_blocks[i];
1093 c = s->comp_index[i];
1098 linesize= s->linesize[c];
1100 if(bits>8) linesize /= 2;
1102 for(j=0; j<n; j++) {
1105 dc = mjpeg_decode_dc(s, s->dc_index[i]);
1108 if ( h * mb_x + x >= s->width
1109 || v * mb_y + y >= s->height) {
1111 } else if (bits<=8) {
1112 ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
1114 if(x==0 && leftcol){
1115 pred= 1 << (bits - 1);
1120 if(x==0 && leftcol){
1121 pred= ptr[-linesize];
1123 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1127 if (s->interlaced && s->bottom_field)
1128 ptr += linesize >> 1;
1130 *ptr= pred + (dc << point_transform);
1132 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1134 if(x==0 && leftcol){
1135 pred= 1 << (bits - 1);
1140 if(x==0 && leftcol){
1141 pred= ptr16[-linesize];
1143 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1147 if (s->interlaced && s->bottom_field)
1148 ptr16 += linesize >> 1;
1150 *ptr16= pred + (dc << point_transform);
1159 for (i = 0; i < nb_components; i++) {
1162 int n, h, v, x, y, c, j, linesize, dc;
1163 n = s->nb_blocks[i];
1164 c = s->comp_index[i];
1169 linesize = s->linesize[c];
1171 if(bits>8) linesize /= 2;
1173 for (j = 0; j < n; j++) {
1176 dc = mjpeg_decode_dc(s, s->dc_index[i]);
1179 if ( h * mb_x + x >= s->width
1180 || v * mb_y + y >= s->height) {
1182 } else if (bits<=8) {
1183 ptr = s->picture_ptr->data[c] +
1184 (linesize * (v * mb_y + y)) +
1185 (h * mb_x + x); //FIXME optimize this crap
1186 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1189 *ptr = pred + (dc << point_transform);
1191 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1192 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1195 *ptr16= pred + (dc << point_transform);
1205 if (s->restart_interval && !--s->restart_count) {
1206 align_get_bits(&s->gb);
1207 skip_bits(&s->gb, 16); /* skip RSTn */
1214 static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s,
1215 uint8_t *dst, const uint8_t *src,
1216 int linesize, int lowres)
1219 case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
1221 case 1: copy_block4(dst, src, linesize, linesize, 4);
1223 case 2: copy_block2(dst, src, linesize, linesize, 2);
1225 case 3: *dst = *src;
1230 static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1232 int block_x, block_y;
1233 int size = 8 >> s->avctx->lowres;
1235 for (block_y=0; block_y<size; block_y++)
1236 for (block_x=0; block_x<size; block_x++)
1237 *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits;
1239 for (block_y=0; block_y<size; block_y++)
1240 for (block_x=0; block_x<size; block_x++)
1241 *(ptr + block_x + block_y*linesize) <<= 8 - s->bits;
1245 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1246 int Al, const uint8_t *mb_bitmask,
1247 int mb_bitmask_size,
1248 const AVFrame *reference)
1250 int i, mb_x, mb_y, chroma_h_shift, chroma_v_shift, chroma_width, chroma_height;
1251 uint8_t *data[MAX_COMPONENTS];
1252 const uint8_t *reference_data[MAX_COMPONENTS];
1253 int linesize[MAX_COMPONENTS];
1254 GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1255 int bytes_per_pixel = 1 + (s->bits > 8);
1258 if (mb_bitmask_size != (s->mb_width * s->mb_height + 7)>>3) {
1259 av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1260 return AVERROR_INVALIDDATA;
1262 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1265 s->restart_count = 0;
1267 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &chroma_h_shift,
1269 chroma_width = AV_CEIL_RSHIFT(s->width, chroma_h_shift);
1270 chroma_height = AV_CEIL_RSHIFT(s->height, chroma_v_shift);
1272 for (i = 0; i < nb_components; i++) {
1273 int c = s->comp_index[i];
1274 data[c] = s->picture_ptr->data[c];
1275 reference_data[c] = reference ? reference->data[c] : NULL;
1276 linesize[c] = s->linesize[c];
1277 s->coefs_finished[c] |= 1;
1280 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1281 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1282 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1284 if (s->restart_interval && !s->restart_count)
1285 s->restart_count = s->restart_interval;
1287 if (get_bits_left(&s->gb) < 0) {
1288 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1289 -get_bits_left(&s->gb));
1290 return AVERROR_INVALIDDATA;
1292 for (i = 0; i < nb_components; i++) {
1294 int n, h, v, x, y, c, j;
1296 n = s->nb_blocks[i];
1297 c = s->comp_index[i];
1302 for (j = 0; j < n; j++) {
1303 block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1304 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1306 if (s->interlaced && s->bottom_field)
1307 block_offset += linesize[c] >> 1;
1308 if ( 8*(h * mb_x + x) < ((c == 1) || (c == 2) ? chroma_width : s->width)
1309 && 8*(v * mb_y + y) < ((c == 1) || (c == 2) ? chroma_height : s->height)) {
1310 ptr = data[c] + block_offset;
1313 if (!s->progressive) {
1316 mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1317 linesize[c], s->avctx->lowres);
1320 s->bdsp.clear_block(s->block);
1321 if (decode_block(s, s->block, i,
1322 s->dc_index[i], s->ac_index[i],
1323 s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1324 av_log(s->avctx, AV_LOG_ERROR,
1325 "error y=%d x=%d\n", mb_y, mb_x);
1326 return AVERROR_INVALIDDATA;
1329 s->idsp.idct_put(ptr, linesize[c], s->block);
1331 shift_output(s, ptr, linesize[c]);
1335 int block_idx = s->block_stride[c] * (v * mb_y + y) +
1337 int16_t *block = s->blocks[c][block_idx];
1339 block[0] += get_bits1(&s->gb) *
1340 s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1341 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1342 s->quant_matrixes[s->quant_sindex[i]],
1344 av_log(s->avctx, AV_LOG_ERROR,
1345 "error y=%d x=%d\n", mb_y, mb_x);
1346 return AVERROR_INVALIDDATA;
1349 ff_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1350 ff_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1351 mb_x, mb_y, x, y, c, s->bottom_field,
1352 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1360 handle_rstn(s, nb_components);
1366 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
1367 int se, int Ah, int Al)
1371 int c = s->comp_index[0];
1372 uint8_t *data = s->picture_ptr->data[c];
1373 int linesize = s->linesize[c];
1375 int16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1376 int bytes_per_pixel = 1 + (s->bits > 8);
1378 av_assert0(ss>=0 && Ah>=0 && Al>=0);
1379 if (se < ss || se > 63) {
1380 av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se);
1381 return AVERROR_INVALIDDATA;
1385 // s->coefs_finished is a bitmask for coefficients coded
1386 // ss and se are parameters telling start and end coefficients
1387 s->coefs_finished[c] |= (2ULL << se) - (1ULL << ss);
1388 last_scan = !~s->coefs_finished[c];
1391 if (s->interlaced && s->bottom_field)
1392 data += linesize >> 1;
1394 s->restart_count = 0;
1396 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1397 uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1398 int block_idx = mb_y * s->block_stride[c];
1399 int16_t (*block)[64] = &s->blocks[c][block_idx];
1400 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1401 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1403 if (s->restart_interval && !s->restart_count)
1404 s->restart_count = s->restart_interval;
1407 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1408 quant_matrix, ss, se, Al, &EOBRUN);
1410 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1411 quant_matrix, ss, se, Al, &EOBRUN);
1413 av_log(s->avctx, AV_LOG_ERROR,
1414 "error y=%d x=%d\n", mb_y, mb_x);
1415 return AVERROR_INVALIDDATA;
1419 s->idsp.idct_put(ptr, linesize, *block);
1421 shift_output(s, ptr, linesize);
1422 ptr += bytes_per_pixel*8 >> s->avctx->lowres;
1424 if (handle_rstn(s, 0))
1431 int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
1432 int mb_bitmask_size, const AVFrame *reference)
1434 int len, nb_components, i, h, v, predictor, point_transform;
1436 const int block_size = s->lossless ? 1 : 8;
1437 int ilv, prev_shift;
1439 if (!s->got_picture) {
1440 av_log(s->avctx, AV_LOG_WARNING,
1441 "Can not process SOS before SOF, skipping\n");
1445 av_assert0(s->picture_ptr->data[0]);
1446 /* XXX: verify len field validity */
1447 len = get_bits(&s->gb, 16);
1448 nb_components = get_bits(&s->gb, 8);
1449 if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1450 av_log(s->avctx, AV_LOG_ERROR,
1451 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1452 return AVERROR_PATCHWELCOME;
1454 if (len != 6 + 2 * nb_components) {
1455 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1456 return AVERROR_INVALIDDATA;
1458 for (i = 0; i < nb_components; i++) {
1459 id = get_bits(&s->gb, 8) - 1;
1460 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1461 /* find component index */
1462 for (index = 0; index < s->nb_components; index++)
1463 if (id == s->component_id[index])
1465 if (index == s->nb_components) {
1466 av_log(s->avctx, AV_LOG_ERROR,
1467 "decode_sos: index(%d) out of components\n", index);
1468 return AVERROR_INVALIDDATA;
1470 /* Metasoft MJPEG codec has Cb and Cr swapped */
1471 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1472 && nb_components == 3 && s->nb_components == 3 && i)
1475 s->quant_sindex[i] = s->quant_index[index];
1476 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1477 s->h_scount[i] = s->h_count[index];
1478 s->v_scount[i] = s->v_count[index];
1480 if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1482 if(nb_components == 1 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1483 index = (index+2)%3;
1485 s->comp_index[i] = index;
1487 s->dc_index[i] = get_bits(&s->gb, 4);
1488 s->ac_index[i] = get_bits(&s->gb, 4);
1490 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1491 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1493 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))
1497 predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1498 ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1499 if(s->avctx->codec_tag != AV_RL32("CJPG")){
1500 prev_shift = get_bits(&s->gb, 4); /* Ah */
1501 point_transform = get_bits(&s->gb, 4); /* Al */
1503 prev_shift = point_transform = 0;
1505 if (nb_components > 1) {
1506 /* interleaved stream */
1507 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1508 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1509 } else if (!s->ls) { /* skip this for JPEG-LS */
1510 h = s->h_max / s->h_scount[0];
1511 v = s->v_max / s->v_scount[0];
1512 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1513 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1514 s->nb_blocks[0] = 1;
1519 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1520 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1521 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1522 predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1523 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1526 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1527 for (i = s->mjpb_skiptosod; i > 0; i--)
1528 skip_bits(&s->gb, 8);
1531 for (i = 0; i < nb_components; i++)
1532 s->last_dc[i] = (4 << s->bits);
1535 av_assert0(s->picture_ptr == s->picture);
1536 if (CONFIG_JPEGLS_DECODER && s->ls) {
1538 // reset_ls_coding_parameters(s, 0);
1540 if ((ret = ff_jpegls_decode_picture(s, predictor,
1541 point_transform, ilv)) < 0)
1545 if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1548 if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1550 nb_components)) < 0)
1555 if (s->progressive && predictor) {
1556 av_assert0(s->picture_ptr == s->picture);
1557 if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1559 point_transform)) < 0)
1562 if ((ret = mjpeg_decode_scan(s, nb_components,
1563 prev_shift, point_transform,
1564 mb_bitmask, mb_bitmask_size, reference)) < 0)
1569 if (s->interlaced &&
1570 get_bits_left(&s->gb) > 32 &&
1571 show_bits(&s->gb, 8) == 0xFF) {
1572 GetBitContext bak = s->gb;
1573 align_get_bits(&bak);
1574 if (show_bits(&bak, 16) == 0xFFD1) {
1575 av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1577 skip_bits(&s->gb, 16);
1578 s->bottom_field ^= 1;
1587 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1588 return AVERROR_INVALIDDATA;
1591 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1593 if (get_bits(&s->gb, 16) != 4)
1594 return AVERROR_INVALIDDATA;
1595 s->restart_interval = get_bits(&s->gb, 16);
1596 s->restart_count = 0;
1597 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1598 s->restart_interval);
1603 static int mjpeg_decode_app(MJpegDecodeContext *s)
1607 len = get_bits(&s->gb, 16);
1609 return AVERROR_INVALIDDATA;
1610 if (8 * len > get_bits_left(&s->gb))
1611 return AVERROR_INVALIDDATA;
1613 id = get_bits_long(&s->gb, 32);
1616 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
1618 av_get_codec_tag_string(id_str, sizeof(id_str), av_bswap32(id));
1619 av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n", id_str, id, len);
1622 /* Buggy AVID, it puts EOI only at every 10th frame. */
1623 /* Also, this fourcc is used by non-avid files too, it holds some
1624 information, but it's always present in AVID-created files. */
1625 if (id == AV_RB32("AVI1")) {
1631 4bytes field_size_less_padding
1634 i = get_bits(&s->gb, 8); len--;
1635 av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1637 skip_bits(&s->gb, 8);
1638 skip_bits(&s->gb, 32);
1639 skip_bits(&s->gb, 32);
1647 if (id == AV_RB32("JFIF")) {
1648 int t_w, t_h, v1, v2;
1649 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1650 v1 = get_bits(&s->gb, 8);
1651 v2 = get_bits(&s->gb, 8);
1652 skip_bits(&s->gb, 8);
1654 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1655 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1656 if ( s->avctx->sample_aspect_ratio.num <= 0
1657 || s->avctx->sample_aspect_ratio.den <= 0) {
1658 s->avctx->sample_aspect_ratio.num = 0;
1659 s->avctx->sample_aspect_ratio.den = 1;
1662 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1663 av_log(s->avctx, AV_LOG_INFO,
1664 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1666 s->avctx->sample_aspect_ratio.num,
1667 s->avctx->sample_aspect_ratio.den);
1669 t_w = get_bits(&s->gb, 8);
1670 t_h = get_bits(&s->gb, 8);
1672 /* skip thumbnail */
1673 if (len -10 - (t_w * t_h * 3) > 0)
1674 len -= t_w * t_h * 3;
1680 if (id == AV_RB32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1681 skip_bits(&s->gb, 16); /* version */
1682 skip_bits(&s->gb, 16); /* flags0 */
1683 skip_bits(&s->gb, 16); /* flags1 */
1684 s->adobe_transform = get_bits(&s->gb, 8);
1685 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1686 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1691 if (id == AV_RB32("LJIF")) {
1693 int pegasus_rct = s->pegasus_rct;
1694 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1695 av_log(s->avctx, AV_LOG_INFO,
1696 "Pegasus lossless jpeg header found\n");
1697 skip_bits(&s->gb, 16); /* version ? */
1698 skip_bits(&s->gb, 16); /* unknown always 0? */
1699 skip_bits(&s->gb, 16); /* unknown always 0? */
1700 skip_bits(&s->gb, 16); /* unknown always 0? */
1701 switch (i=get_bits(&s->gb, 8)) {
1711 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1716 if (rgb != s->rgb || pegasus_rct != s->pegasus_rct) {
1717 av_log(s->avctx, AV_LOG_WARNING, "Mismatching LJIF tag\n");
1722 s->pegasus_rct = pegasus_rct;
1726 if (id == AV_RL32("colr") && len > 0) {
1727 s->colr = get_bits(&s->gb, 8);
1728 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1729 av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1733 if (id == AV_RL32("xfrm") && len > 0) {
1734 s->xfrm = get_bits(&s->gb, 8);
1735 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1736 av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1741 /* JPS extension by VRex */
1742 if (s->start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1743 int flags, layout, type;
1744 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1745 av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
1747 skip_bits(&s->gb, 32); len -= 4; /* JPS_ */
1748 skip_bits(&s->gb, 16); len -= 2; /* block length */
1749 skip_bits(&s->gb, 8); /* reserved */
1750 flags = get_bits(&s->gb, 8);
1751 layout = get_bits(&s->gb, 8);
1752 type = get_bits(&s->gb, 8);
1755 s->stereo3d = av_stereo3d_alloc();
1760 s->stereo3d->type = AV_STEREO3D_2D;
1761 } else if (type == 1) {
1764 s->stereo3d->type = AV_STEREO3D_LINES;
1767 s->stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
1770 s->stereo3d->type = AV_STEREO3D_TOPBOTTOM;
1773 if (!(flags & 0x04)) {
1774 s->stereo3d->flags = AV_STEREO3D_FLAG_INVERT;
1781 if (s->start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
1782 GetByteContext gbytes;
1783 int ret, le, ifd_offset, bytes_read;
1784 const uint8_t *aligned;
1786 skip_bits(&s->gb, 16); // skip padding
1789 // init byte wise reading
1790 aligned = align_get_bits(&s->gb);
1791 bytestream2_init(&gbytes, aligned, len);
1794 ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
1796 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
1798 bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
1800 // read 0th IFD and store the metadata
1801 // (return values > 0 indicate the presence of subimage metadata)
1802 ret = avpriv_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata);
1804 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n");
1808 bytes_read = bytestream2_tell(&gbytes);
1809 skip_bits(&s->gb, bytes_read << 3);
1816 if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1817 id = get_bits_long(&s->gb, 32);
1820 if (id == AV_RB32("mjpg")) {
1822 skip_bits(&s->gb, 32); /* field size */
1823 skip_bits(&s->gb, 32); /* pad field size */
1824 skip_bits(&s->gb, 32); /* next off */
1825 skip_bits(&s->gb, 32); /* quant off */
1826 skip_bits(&s->gb, 32); /* huff off */
1827 skip_bits(&s->gb, 32); /* image off */
1828 skip_bits(&s->gb, 32); /* scan off */
1829 skip_bits(&s->gb, 32); /* data off */
1831 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1832 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1837 /* slow but needed for extreme adobe jpegs */
1839 av_log(s->avctx, AV_LOG_ERROR,
1840 "mjpeg: error, decode_app parser read over the end\n");
1842 skip_bits(&s->gb, 8);
1847 static int mjpeg_decode_com(MJpegDecodeContext *s)
1849 int len = get_bits(&s->gb, 16);
1850 if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1851 char *cbuf = av_malloc(len - 1);
1854 for (i = 0; i < len - 2; i++)
1855 cbuf[i] = get_bits(&s->gb, 8);
1856 if (i > 0 && cbuf[i - 1] == '\n')
1861 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1862 av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
1864 /* buggy avid, it puts EOI only at every 10th frame */
1865 if (!strncmp(cbuf, "AVID", 4)) {
1866 parse_avid(s, cbuf, len);
1867 } else if (!strcmp(cbuf, "CS=ITU601"))
1869 else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
1870 (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1872 else if (!strcmp(cbuf, "MULTISCOPE II"))
1882 /* return the 8 bit start code value and update the search
1883 state. Return -1 if no start code found */
1884 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1886 const uint8_t *buf_ptr;
1891 buf_ptr = *pbuf_ptr;
1892 while (buf_end - buf_ptr > 1) {
1895 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1904 ff_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1905 *pbuf_ptr = buf_ptr;
1909 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1910 const uint8_t **buf_ptr, const uint8_t *buf_end,
1911 const uint8_t **unescaped_buf_ptr,
1912 int *unescaped_buf_size)
1915 start_code = find_marker(buf_ptr, buf_end);
1917 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1919 return AVERROR(ENOMEM);
1921 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1922 if (start_code == SOS && !s->ls) {
1923 const uint8_t *src = *buf_ptr;
1924 uint8_t *dst = s->buffer;
1926 while (src < buf_end) {
1927 uint8_t x = *(src++);
1930 if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1932 while (src < buf_end && x == 0xff)
1935 if (x >= 0xd0 && x <= 0xd7)
1942 *unescaped_buf_ptr = s->buffer;
1943 *unescaped_buf_size = dst - s->buffer;
1944 memset(s->buffer + *unescaped_buf_size, 0,
1945 AV_INPUT_BUFFER_PADDING_SIZE);
1947 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %"PTRDIFF_SPECIFIER" bytes\n",
1948 (buf_end - *buf_ptr) - (dst - s->buffer));
1949 } else if (start_code == SOS && s->ls) {
1950 const uint8_t *src = *buf_ptr;
1951 uint8_t *dst = s->buffer;
1957 while (src + t < buf_end) {
1958 uint8_t x = src[t++];
1960 while ((src + t < buf_end) && x == 0xff)
1969 init_put_bits(&pb, dst, t);
1971 /* unescape bitstream */
1973 uint8_t x = src[b++];
1974 put_bits(&pb, 8, x);
1975 if (x == 0xFF && b < t) {
1978 av_log(s->avctx, AV_LOG_WARNING, "Invalid escape sequence\n");
1981 put_bits(&pb, 7, x);
1985 flush_put_bits(&pb);
1987 *unescaped_buf_ptr = dst;
1988 *unescaped_buf_size = (bit_count + 7) >> 3;
1989 memset(s->buffer + *unescaped_buf_size, 0,
1990 AV_INPUT_BUFFER_PADDING_SIZE);
1992 *unescaped_buf_ptr = *buf_ptr;
1993 *unescaped_buf_size = buf_end - *buf_ptr;
1999 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
2002 AVFrame *frame = data;
2003 const uint8_t *buf = avpkt->data;
2004 int buf_size = avpkt->size;
2005 MJpegDecodeContext *s = avctx->priv_data;
2006 const uint8_t *buf_end, *buf_ptr;
2007 const uint8_t *unescaped_buf_ptr;
2009 int unescaped_buf_size;
2015 av_dict_free(&s->exif_metadata);
2016 av_freep(&s->stereo3d);
2017 s->adobe_transform = -1;
2020 buf_end = buf + buf_size;
2021 while (buf_ptr < buf_end) {
2022 /* find start next marker */
2023 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
2025 &unescaped_buf_size);
2027 if (start_code < 0) {
2029 } else if (unescaped_buf_size > INT_MAX / 8) {
2030 av_log(avctx, AV_LOG_ERROR,
2031 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
2032 start_code, unescaped_buf_size, buf_size);
2033 return AVERROR_INVALIDDATA;
2035 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
2036 start_code, buf_end - buf_ptr);
2038 ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
2041 av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
2045 s->start_code = start_code;
2046 if (s->avctx->debug & FF_DEBUG_STARTCODE)
2047 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
2049 /* process markers */
2050 if (start_code >= 0xd0 && start_code <= 0xd7)
2051 av_log(avctx, AV_LOG_DEBUG,
2052 "restart marker: %d\n", start_code & 0x0f);
2054 else if (start_code >= APP0 && start_code <= APP15)
2055 mjpeg_decode_app(s);
2057 else if (start_code == COM)
2058 mjpeg_decode_com(s);
2062 if (!CONFIG_JPEGLS_DECODER &&
2063 (start_code == SOF48 || start_code == LSE)) {
2064 av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
2065 return AVERROR(ENOSYS);
2068 if (avctx->skip_frame == AVDISCARD_ALL) {
2069 switch(start_code) {
2084 switch (start_code) {
2086 s->restart_interval = 0;
2087 s->restart_count = 0;
2088 /* nothing to do on SOI */
2091 ff_mjpeg_decode_dqt(s);
2094 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
2095 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
2104 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2111 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2115 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
2119 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2123 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
2127 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2131 if (!CONFIG_JPEGLS_DECODER ||
2132 (ret = ff_jpegls_decode_lse(s)) < 0)
2138 if (!s->got_picture) {
2139 av_log(avctx, AV_LOG_WARNING,
2140 "Found EOI before any SOF, ignoring\n");
2143 if (s->interlaced) {
2144 s->bottom_field ^= 1;
2145 /* if not bottom field, do not output image yet */
2146 if (s->bottom_field == !s->interlace_polarity)
2149 if (avctx->skip_frame == AVDISCARD_ALL) {
2151 goto the_end_no_picture;
2153 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2159 int qp = FFMAX3(s->qscale[0],
2162 int qpw = (s->width + 15) / 16;
2163 AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
2165 memset(qp_table_buf->data, qp, qpw);
2166 av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
2169 if(avctx->debug & FF_DEBUG_QP)
2170 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2176 if (avctx->skip_frame == AVDISCARD_ALL)
2179 if ((ret = ff_mjpeg_decode_sos(s, NULL, 0, NULL)) < 0 &&
2180 (avctx->err_recognition & AV_EF_EXPLODE))
2184 mjpeg_decode_dri(s);
2196 av_log(avctx, AV_LOG_ERROR,
2197 "mjpeg: unsupported coding type (%x)\n", start_code);
2202 /* eof process start code */
2203 buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
2204 av_log(avctx, AV_LOG_DEBUG,
2205 "marker parser used %d bytes (%d bits)\n",
2206 (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
2208 if (s->got_picture && s->cur_scan) {
2209 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2212 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2213 return AVERROR_INVALIDDATA;
2219 is16bit = av_pix_fmt_desc_get(s->avctx->pix_fmt)->comp[0].step > 1;
2221 if (AV_RB32(s->upscale_h)) {
2223 av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2224 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2225 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2226 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2227 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2228 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2229 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2230 avctx->pix_fmt == AV_PIX_FMT_YUV420P16||
2231 avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2232 avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2233 avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2234 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2236 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2237 for (p = 0; p<4; p++) {
2238 uint8_t *line = s->picture_ptr->data[p];
2241 if (!s->upscale_h[p])
2244 w = AV_CEIL_RSHIFT(w, hshift);
2245 h = AV_CEIL_RSHIFT(h, vshift);
2247 if (s->upscale_v[p])
2250 for (i = 0; i < h; i++) {
2251 if (s->upscale_h[p] == 1) {
2252 if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2253 else line[w - 1] = line[(w - 1) / 2];
2254 for (index = w - 2; index > 0; index--) {
2256 ((uint16_t*)line)[index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2258 line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2260 } else if (s->upscale_h[p] == 2) {
2262 ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 3];
2264 ((uint16_t*)line)[w - 2] = ((uint16_t*)line)[w - 1];
2266 line[w - 1] = line[(w - 1) / 3];
2268 line[w - 2] = line[w - 1];
2270 for (index = w - 3; index > 0; index--) {
2271 line[index] = (line[index / 3] + line[(index + 1) / 3] + line[(index + 2) / 3] + 1) / 3;
2274 line += s->linesize[p];
2278 if (AV_RB32(s->upscale_v)) {
2280 av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2281 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2282 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2283 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2284 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2285 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2286 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2287 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2288 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2289 avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2290 avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2291 avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2292 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2294 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2295 for (p = 0; p < 4; p++) {
2299 if (!s->upscale_v[p])
2302 w = AV_CEIL_RSHIFT(w, hshift);
2303 h = AV_CEIL_RSHIFT(h, vshift);
2305 dst = &((uint8_t *)s->picture_ptr->data[p])[(h - 1) * s->linesize[p]];
2306 for (i = h - 1; i; i--) {
2307 uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i / 2 * s->linesize[p]];
2308 uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) / 2 * s->linesize[p]];
2309 if (src1 == src2 || i == h - 1) {
2310 memcpy(dst, src1, w);
2312 for (index = 0; index < w; index++)
2313 dst[index] = (src1[index] + src2[index]) >> 1;
2315 dst -= s->linesize[p];
2321 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2322 for (index=0; index<4; index++) {
2323 uint8_t *dst = s->picture_ptr->data[index];
2324 int w = s->picture_ptr->width;
2325 int h = s->picture_ptr->height;
2326 if(index && index<3){
2327 w = AV_CEIL_RSHIFT(w, hshift);
2328 h = AV_CEIL_RSHIFT(h, vshift);
2331 uint8_t *dst2 = dst + s->picture_ptr->linesize[index]*(h-1);
2332 for (i=0; i<h/2; i++) {
2334 FFSWAP(int, dst[j], dst2[j]);
2335 dst += s->picture_ptr->linesize[index];
2336 dst2 -= s->picture_ptr->linesize[index];
2341 if (s->adobe_transform == 0 && s->avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2342 int w = s->picture_ptr->width;
2343 int h = s->picture_ptr->height;
2344 for (i=0; i<h; i++) {
2347 for (index=0; index<4; index++) {
2348 dst[index] = s->picture_ptr->data[index]
2349 + s->picture_ptr->linesize[index]*i;
2351 for (j=0; j<w; j++) {
2353 int r = dst[0][j] * k;
2354 int g = dst[1][j] * k;
2355 int b = dst[2][j] * k;
2356 dst[0][j] = g*257 >> 16;
2357 dst[1][j] = b*257 >> 16;
2358 dst[2][j] = r*257 >> 16;
2363 if (s->adobe_transform == 2 && s->avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2364 int w = s->picture_ptr->width;
2365 int h = s->picture_ptr->height;
2366 for (i=0; i<h; i++) {
2369 for (index=0; index<4; index++) {
2370 dst[index] = s->picture_ptr->data[index]
2371 + s->picture_ptr->linesize[index]*i;
2373 for (j=0; j<w; j++) {
2375 int r = (255 - dst[0][j]) * k;
2376 int g = (128 - dst[1][j]) * k;
2377 int b = (128 - dst[2][j]) * k;
2378 dst[0][j] = r*257 >> 16;
2379 dst[1][j] = (g*257 >> 16) + 128;
2380 dst[2][j] = (b*257 >> 16) + 128;
2387 AVStereo3D *stereo = av_stereo3d_create_side_data(data);
2389 stereo->type = s->stereo3d->type;
2390 stereo->flags = s->stereo3d->flags;
2392 av_freep(&s->stereo3d);
2395 av_dict_copy(avpriv_frame_get_metadatap(data), s->exif_metadata, 0);
2396 av_dict_free(&s->exif_metadata);
2399 av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n",
2401 // return buf_end - buf_ptr;
2402 return buf_ptr - buf;
2405 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
2407 MJpegDecodeContext *s = avctx->priv_data;
2410 if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) {
2411 av_log(avctx, AV_LOG_INFO, "Single field\n");
2415 av_frame_free(&s->picture);
2416 s->picture_ptr = NULL;
2417 } else if (s->picture_ptr)
2418 av_frame_unref(s->picture_ptr);
2420 av_freep(&s->buffer);
2421 av_freep(&s->stereo3d);
2422 av_freep(&s->ljpeg_buffer);
2423 s->ljpeg_buffer_size = 0;
2425 for (i = 0; i < 3; i++) {
2426 for (j = 0; j < 4; j++)
2427 ff_free_vlc(&s->vlcs[i][j]);
2429 for (i = 0; i < MAX_COMPONENTS; i++) {
2430 av_freep(&s->blocks[i]);
2431 av_freep(&s->last_nnz[i]);
2433 av_dict_free(&s->exif_metadata);
2437 static void decode_flush(AVCodecContext *avctx)
2439 MJpegDecodeContext *s = avctx->priv_data;
2443 #if CONFIG_MJPEG_DECODER
2444 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2445 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2446 static const AVOption options[] = {
2447 { "extern_huff", "Use external huffman table.",
2448 OFFSET(extern_huff), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
2452 static const AVClass mjpegdec_class = {
2453 .class_name = "MJPEG decoder",
2454 .item_name = av_default_item_name,
2456 .version = LIBAVUTIL_VERSION_INT,
2459 AVCodec ff_mjpeg_decoder = {
2461 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
2462 .type = AVMEDIA_TYPE_VIDEO,
2463 .id = AV_CODEC_ID_MJPEG,
2464 .priv_data_size = sizeof(MJpegDecodeContext),
2465 .init = ff_mjpeg_decode_init,
2466 .close = ff_mjpeg_decode_end,
2467 .decode = ff_mjpeg_decode_frame,
2468 .flush = decode_flush,
2469 .capabilities = AV_CODEC_CAP_DR1,
2471 .priv_class = &mjpegdec_class,
2472 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2473 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2476 #if CONFIG_THP_DECODER
2477 AVCodec ff_thp_decoder = {
2479 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
2480 .type = AVMEDIA_TYPE_VIDEO,
2481 .id = AV_CODEC_ID_THP,
2482 .priv_data_size = sizeof(MJpegDecodeContext),
2483 .init = ff_mjpeg_decode_init,
2484 .close = ff_mjpeg_decode_end,
2485 .decode = ff_mjpeg_decode_frame,
2486 .flush = decode_flush,
2487 .capabilities = AV_CODEC_CAP_DR1,
2489 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,