FFmpeg
Loading...
Searching...
No Matches
mjpegdec.c
Go to the documentation of this file.
1/*
2 * MJPEG decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
6 *
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
9 * by Alex Beregszaszi
10 *
11 * This file is part of FFmpeg.
12 *
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.
17 *
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.
22 *
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
26 */
27
28/**
29 * @file
30 * MJPEG decoder.
31 */
32
33#include "config_components.h"
34
36#include "libavutil/imgutils.h"
37#include "libavutil/avassert.h"
38#include "libavutil/mem.h"
39#include "libavutil/opt.h"
40#include "avcodec.h"
41#include "blockdsp.h"
42#include "codec_internal.h"
43#include "copy_block.h"
44#include "decode.h"
45#include "exif.h"
46#include "exif_internal.h"
47#include "gain_map.h"
48#include "hwaccel_internal.h"
49#include "hwconfig.h"
50#include "idctdsp.h"
51#include "internal.h"
52#include "jpegtables.h"
53#include "mjpeg.h"
54#include "mjpegdec.h"
55#include "jpeglsdec.h"
56#include "profiles.h"
57#include "put_bits.h"
58
59
61 const uint8_t **pbuf_ptr, size_t *pbuf_size);
62
64{
65 static const struct {
66 int class;
67 int index;
68 const uint8_t *bits;
69 const uint8_t *values;
70 int length;
71 } ht[] = {
73 ff_mjpeg_val_dc, 12 },
75 ff_mjpeg_val_dc, 12 },
84 };
85 int i, ret;
86
87 for (i = 0; i < FF_ARRAY_ELEMS(ht); i++) {
88 ff_vlc_free(&s->vlcs[ht[i].class][ht[i].index]);
89 ret = ff_mjpeg_build_vlc(&s->vlcs[ht[i].class][ht[i].index],
90 ht[i].bits, ht[i].values,
91 ht[i].class == 1, s->avctx);
92 if (ret < 0)
93 return ret;
94
95 if (ht[i].class < 2) {
96 memcpy(s->raw_huffman_lengths[ht[i].class][ht[i].index],
97 ht[i].bits + 1, 16);
98 memcpy(s->raw_huffman_values[ht[i].class][ht[i].index],
99 ht[i].values, ht[i].length);
100 }
101 }
102
103 return 0;
104}
105
106static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
107{
108 if (len > 12 && buf[12] == 1) /* 1 - NTSC */
109 s->interlace_polarity = 1;
110 if (len > 12 && buf[12] == 2) /* 2 - PAL */
111 s->interlace_polarity = 0;
112 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
113 av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 12 ? buf[12] : -1);
114}
115
116static void init_idct(AVCodecContext *avctx)
117{
119
120 ff_idctdsp_init(&s->idsp, avctx);
121 ff_permute_scantable(s->permutated_scantable, ff_zigzag_direct,
122 s->idsp.idct_permutation);
123}
124
126{
128 int ret;
129
130 if (!s->picture_ptr) {
131 s->picture = av_frame_alloc();
132 if (!s->picture)
133 return AVERROR(ENOMEM);
134 s->picture_ptr = s->picture;
135 }
136
137 s->avctx = avctx;
138 ff_blockdsp_init(&s->bdsp);
139 init_idct(avctx);
140 s->buffer_size = 0;
141 s->buffer = NULL;
142 s->first_picture = 1;
143 s->got_picture = 0;
144 s->orig_height = avctx->coded_height;
147 s->hwaccel_pix_fmt = s->hwaccel_sw_pix_fmt = AV_PIX_FMT_NONE;
148
149 if ((ret = init_default_huffman_tables(s)) < 0)
150 return ret;
151
152#if FF_API_MJPEG_EXTERN_HUFF
153 if (s->extern_huff && avctx->extradata) {
154 av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
155 bytestream2_init(&s->gB, avctx->extradata, avctx->extradata_size);
156 if (ff_mjpeg_decode_dht(s)) {
157 av_log(avctx, AV_LOG_ERROR,
158 "error using external huffman table, switching back to internal\n");
159 if ((ret = init_default_huffman_tables(s)) < 0)
160 return ret;
161 }
162 }
163#endif
164 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
165 s->interlace_polarity = 1; /* bottom field first */
166 av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
167 } else if (avctx->field_order == AV_FIELD_UNKNOWN) {
168 if (avctx->codec_tag == AV_RL32("MJPG"))
169 s->interlace_polarity = 1;
170 }
171
172 if (avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
173 if (avctx->extradata_size >= 4)
174 s->smv_frames_per_jpeg = AV_RL32(avctx->extradata);
175
176 if (s->smv_frames_per_jpeg <= 0) {
177 av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
178 return AVERROR_INVALIDDATA;
179 }
180
181 s->smv_frame = av_frame_alloc();
182 if (!s->smv_frame)
183 return AVERROR(ENOMEM);
184 } else if (avctx->extradata_size > 8
185 && AV_RL32(avctx->extradata) == 0x2C
186 && AV_RL32(avctx->extradata + 4) == 0x18) {
187 parse_avid(s, avctx->extradata, avctx->extradata_size);
188 }
189
190 if (avctx->codec->id == AV_CODEC_ID_AMV)
191 s->flipped = 1;
192
193 return 0;
194}
195
196
197static int mjpeg_parse_len(MJpegDecodeContext *s, int *plen, const char *name)
198{
199 int len = bytestream2_get_be16u(&s->gB);
200 if (len < 2 || bytestream2_get_bytes_left(&s->gB) < (len - 2)) {
201 av_log(s->avctx, AV_LOG_ERROR, "%s: invalid len %d\n", name, len);
202 return AVERROR_INVALIDDATA;
203 }
204 *plen = len - 2;
205 return 0;
206}
207
208/* quantize tables */
210{
211 int len, index, i;
212
213 int ret = mjpeg_parse_len(s, &len, "dqt");
214 if (ret < 0)
215 return ret;
216
217 while (len >= 65) {
218 uint8_t b = bytestream2_get_byteu(&s->gB);
219 int pr = b >> 4;
220 if (pr > 1) {
221 av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
222 return AVERROR_INVALIDDATA;
223 }
224 if (len < (1 + 64 * (1 + pr)))
225 return AVERROR_INVALIDDATA;
226 index = b & 0x0F;
227 if (index >= 4)
228 return AVERROR_INVALIDDATA;
229 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
230 /* read quant table */
231 for (i = 0; i < 64; i++) {
232 s->quant_matrixes[index][i] = pr ? bytestream2_get_be16u(&s->gB) : bytestream2_get_byteu(&s->gB);
233 if (s->quant_matrixes[index][i] == 0) {
234 int log_level = s->avctx->err_recognition & AV_EF_EXPLODE ? AV_LOG_ERROR : AV_LOG_WARNING;
235 av_log(s->avctx, log_level, "dqt: 0 quant value\n");
236 if (s->avctx->err_recognition & AV_EF_EXPLODE)
237 return AVERROR_INVALIDDATA;
238 }
239 }
240
241 // XXX FIXME fine-tune, and perhaps add dc too
242 s->qscale[index] = FFMAX(s->quant_matrixes[index][1],
243 s->quant_matrixes[index][8]) >> 1;
244 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
245 index, s->qscale[index]);
246 len -= 1 + 64 * (1 + pr);
247 }
248 return 0;
249}
250
251/* decode huffman tables and build VLC decoders */
253{
254 int len, index, i, class, n, v;
255 uint8_t bits_table[17];
256 uint8_t val_table[256];
257 int ret = 0;
258
259 ret = mjpeg_parse_len(s, &len, "dht");
260 if (ret < 0)
261 return ret;
262
263 while (len > 0) {
264 if (len < 17)
265 return AVERROR_INVALIDDATA;
266 uint8_t b = bytestream2_get_byteu(&s->gB);
267 class = b >> 4;
268 if (class >= 2)
269 return AVERROR_INVALIDDATA;
270 index = b & 0x0F;
271 if (index >= 4)
272 return AVERROR_INVALIDDATA;
273 n = 0;
274 for (i = 1; i <= 16; i++) {
275 bits_table[i] = bytestream2_get_byteu(&s->gB);
276 n += bits_table[i];
277 }
278 len -= 17;
279 if (len < n || n > 256)
280 return AVERROR_INVALIDDATA;
281
282 for (i = 0; i < n; i++) {
283 v = bytestream2_get_byteu(&s->gB);
284 val_table[i] = v;
285 }
286 len -= n;
287
288 /* build VLC and flush previous vlc if present */
289 ff_vlc_free(&s->vlcs[class][index]);
290 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
291 class, index, n);
292 if ((ret = ff_mjpeg_build_vlc(&s->vlcs[class][index], bits_table,
293 val_table, class > 0, s->avctx)) < 0)
294 return ret;
295
296 if (class > 0) {
297 ff_vlc_free(&s->vlcs[2][index]);
298 if ((ret = ff_mjpeg_build_vlc(&s->vlcs[2][index], bits_table,
299 val_table, 0, s->avctx)) < 0)
300 return ret;
301 }
302
303 for (i = 0; i < 16; i++)
304 s->raw_huffman_lengths[class][index][i] = bits_table[i + 1];
305 for (i = 0; i < 256; i++)
306 s->raw_huffman_values[class][index][i] = val_table[i];
307 }
308 return 0;
309}
310
312{
313 int len, nb_components, i, width, height, bits, ret, size_change;
314 unsigned pix_fmt_id;
315 int h_count[MAX_COMPONENTS] = { 0 };
316 int v_count[MAX_COMPONENTS] = { 0 };
317
318 s->cur_scan = 0;
319 memset(s->upscale_h, 0, sizeof(s->upscale_h));
320 memset(s->upscale_v, 0, sizeof(s->upscale_v));
321
322 ret = mjpeg_parse_len(s, &len, "sof");
323 if (ret < 0)
324 return ret;
325 if (len < 6)
326 return AVERROR_INVALIDDATA;
327 bits = bytestream2_get_byteu(&s->gB);
328
329 if (bits > 16 || bits < 1) {
330 av_log(s->avctx, AV_LOG_ERROR, "bits %d is invalid\n", bits);
331 return AVERROR_INVALIDDATA;
332 }
333
334 if (s->avctx->bits_per_raw_sample != bits) {
335 av_log(s->avctx, s->avctx->bits_per_raw_sample > 0 ? AV_LOG_INFO : AV_LOG_DEBUG, "Changing bps from %d to %d\n", s->avctx->bits_per_raw_sample, bits);
336 s->avctx->bits_per_raw_sample = bits;
337 init_idct(s->avctx);
338 }
339 if (s->pegasus_rct)
340 bits = 9;
341 if (bits == 9 && !s->pegasus_rct)
342 s->rct = 1; // FIXME ugly
343
344 if (s->lossless && s->avctx->lowres) {
345 av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
346 return AVERROR(ENOSYS);
347 }
348
349 height = bytestream2_get_be16u(&s->gB);
350 width = bytestream2_get_be16u(&s->gB);
351
352 // HACK for odd_height.mov
353 if (s->interlaced && s->width == width && s->height == height + 1)
354 height = s->height;
355
356 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
357 if (av_image_check_size(width, height, 0, s->avctx) < 0)
358 return AVERROR_INVALIDDATA;
359
360 if (!s->progressive && !s->ls) {
361 // A valid frame requires at least 1 bit for DC + 1 bit for AC for each 8x8 block.
362 if (s->buf_size && (width + 7) / 8 * ((height + 7) / 8) > s->buf_size * 4LL)
363 return AVERROR_INVALIDDATA;
364 }
365
366 nb_components = bytestream2_get_byteu(&s->gB);
367 if (nb_components <= 0 ||
368 nb_components > MAX_COMPONENTS)
369 return AVERROR_INVALIDDATA;
370 if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
371 if (nb_components != s->nb_components) {
372 av_log(s->avctx, AV_LOG_ERROR,
373 "nb_components changing in interlaced picture\n");
374 return AVERROR_INVALIDDATA;
375 }
376 }
377 if (s->ls && !(bits <= 8 || nb_components == 1)) {
379 "JPEG-LS that is not <= 8 "
380 "bits/component or 16-bit gray");
382 }
383 len -= 6;
384 if (len != 3 * nb_components) {
385 av_log(s->avctx, AV_LOG_ERROR, "decode_sof0: error, len(%d) mismatch %d components\n", len, nb_components);
386 return AVERROR_INVALIDDATA;
387 }
388
389 s->nb_components = nb_components;
390 s->nb_seq_component_scans = 0;
391 s->h_max = 1;
392 s->v_max = 1;
393 for (i = 0; i < nb_components; i++) {
394 /* component id */
395 s->component_id[i] = bytestream2_get_byteu(&s->gB);
396 uint8_t b = bytestream2_get_byteu(&s->gB);
397 h_count[i] = b >> 4;
398 v_count[i] = b & 0x0F;
399 /* compute hmax and vmax (only used in interleaved case) */
400 if (h_count[i] > s->h_max)
401 s->h_max = h_count[i];
402 if (v_count[i] > s->v_max)
403 s->v_max = v_count[i];
404 s->quant_index[i] = bytestream2_get_byteu(&s->gB);
405 if (s->quant_index[i] >= 4) {
406 av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
407 return AVERROR_INVALIDDATA;
408 }
409 if (!h_count[i] || !v_count[i]) {
410 av_log(s->avctx, AV_LOG_ERROR,
411 "Invalid sampling factor in component %d %d:%d\n",
412 i, h_count[i], v_count[i]);
413 return AVERROR_INVALIDDATA;
414 }
415
416 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
417 i, h_count[i], v_count[i],
418 s->component_id[i], s->quant_index[i]);
419 }
420 if ( nb_components == 4
421 && s->component_id[0] == 'C'
422 && s->component_id[1] == 'M'
423 && s->component_id[2] == 'Y'
424 && s->component_id[3] == 'K')
425 s->adobe_transform = 0;
426
427 if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
428 avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
430 }
431
432 /* if different size, realloc/alloc picture */
433 if (width != s->width || height != s->height || bits != s->bits ||
434 memcmp(s->h_count, h_count, sizeof(h_count)) ||
435 memcmp(s->v_count, v_count, sizeof(v_count))) {
436 size_change = 1;
437
438 s->width = width;
439 s->height = height;
440 s->bits = bits;
441 memcpy(s->h_count, h_count, sizeof(h_count));
442 memcpy(s->v_count, v_count, sizeof(v_count));
443 s->interlaced = 0;
444 s->got_picture = 0;
445
446 /* test interlaced mode */
447 if (s->first_picture &&
448 (s->multiscope != 2 || s->avctx->pkt_timebase.den >= 25 * s->avctx->pkt_timebase.num) &&
449 s->orig_height != 0 &&
450 s->height < ((s->orig_height * 3) / 4)) {
451 s->interlaced = 1;
452 s->bottom_field = s->interlace_polarity;
453 s->picture_ptr->flags |= AV_FRAME_FLAG_INTERLACED;
454 s->picture_ptr->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !s->interlace_polarity;
455 height *= 2;
456 }
457
458 ret = ff_set_dimensions(s->avctx, width, height);
459 if (ret < 0)
460 return ret;
461
462 if (s->avctx->codec_id != AV_CODEC_ID_SMVJPEG &&
463 (s->avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
464 s->avctx->codec_tag == MKTAG('A', 'V', 'D', 'J')) &&
465 s->orig_height < height)
466 s->avctx->height = AV_CEIL_RSHIFT(s->orig_height, s->avctx->lowres);
467
468 if (s->bayer) {
469 if (nb_components == 2) {
470 /* Bayer images embedded in DNGs can contain 2 interleaved components and the
471 * width stored in their SOF3 markers is the width of each one. We only output
472 * a single component, therefore we need to adjust the output image width. We
473 * handle the deinterleaving (but not the debayering) in this file. */
474 s->avctx->width *= 2;
475 }
476 /* They can also contain 1 component, which is double the width and half the height
477 * of the final image (rows are interleaved). We don't handle the decoding in this
478 * file, but leave that to the TIFF/DNG decoder. */
479 }
480
481 s->first_picture = 0;
482 } else {
483 size_change = 0;
484 }
485
486 if (s->avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
487 s->avctx->height = s->avctx->coded_height / s->smv_frames_per_jpeg;
488 if (s->avctx->height <= 0)
489 return AVERROR_INVALIDDATA;
490 }
491 if (s->bayer && s->progressive) {
492 avpriv_request_sample(s->avctx, "progressively coded bayer picture");
493 return AVERROR_INVALIDDATA;
494 }
495
496 if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
497 if (s->progressive) {
498 avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
499 return AVERROR_INVALIDDATA;
500 }
501 } else {
502 if (s->v_max == 1 && s->h_max == 1 && s->lossless == 1 && (nb_components == 3 || nb_components == 4))
503 s->rgb = 1;
504 else if (!s->lossless)
505 s->rgb = 0;
506 /* XXX: not complete test ! */
507 pix_fmt_id = ((unsigned)s->h_count[0] << 28) | (s->v_count[0] << 24) |
508 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
509 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
510 (s->h_count[3] << 4) | s->v_count[3];
511 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
512 /* NOTE we do not allocate pictures large enough for the possible
513 * padding of h/v_count being 4 */
514 if (!(pix_fmt_id & 0xD0D0D0D0))
515 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
516 if (!(pix_fmt_id & 0x0D0D0D0D))
517 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
518
519 for (i = 0; i < 8; i++) {
520 int j = 6 + (i & 1) - (i & 6);
521 int is = (pix_fmt_id >> (4 * i)) & 0xF;
522 int js = (pix_fmt_id >> (4 * j)) & 0xF;
523
524 if (is == 1 && js != 2 && (i < 2 || i > 5))
525 js = (pix_fmt_id >> ( 8 + 4 * (i & 1))) & 0xF;
526 if (is == 1 && js != 2 && (i < 2 || i > 5))
527 js = (pix_fmt_id >> (16 + 4 * (i & 1))) & 0xF;
528
529 if (is == 1 && js == 2) {
530 if (i & 1) s->upscale_h[j / 2] = 1;
531 else s->upscale_v[j / 2] = 1;
532 }
533 }
534
535 if (s->bayer) {
536 if (pix_fmt_id != 0x11110000 && pix_fmt_id != 0x11000000)
537 goto unk_pixfmt;
538 }
539
540 switch (pix_fmt_id) {
541 case 0x11110000: /* for bayer-encoded huffman lossless JPEGs embedded in DNGs */
542 if (!s->bayer)
543 goto unk_pixfmt;
544 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
545 break;
546 case 0x11111100:
547 if (s->rgb)
548 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_BGR48;
549 else {
550 if ( s->adobe_transform == 0
551 || s->component_id[0] == 'R' && s->component_id[1] == 'G' && s->component_id[2] == 'B') {
552 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_GBRP : AV_PIX_FMT_GBRP16;
553 } else {
554 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
555 else s->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
556 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
557 }
558 }
559 av_assert0(s->nb_components == 3);
560 break;
561 case 0x11111111:
562 if (s->rgb)
563 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA64;
564 else {
565 if (s->adobe_transform == 0 && s->bits <= 8) {
566 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
567 } else {
568 s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_YUVA444P : AV_PIX_FMT_YUVA444P16;
569 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
570 }
571 }
572 av_assert0(s->nb_components == 4);
573 break;
574 case 0x11412100:
575 if (s->bits > 8)
576 goto unk_pixfmt;
577 if (s->component_id[0] == 'R' && s->component_id[1] == 'G' && s->component_id[2] == 'B') {
578 s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
579 s->upscale_h[0] = 4;
580 s->upscale_h[1] = 0;
581 s->upscale_h[2] = 1;
582 } else {
583 goto unk_pixfmt;
584 }
585 break;
586 case 0x22111122:
587 case 0x22111111:
588 if (s->adobe_transform == 0 && s->bits <= 8) {
589 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
590 s->upscale_v[1] = s->upscale_v[2] = 1;
591 s->upscale_h[1] = s->upscale_h[2] = 1;
592 } else if (s->adobe_transform == 2 && s->bits <= 8) {
593 s->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
594 s->upscale_v[1] = s->upscale_v[2] = 1;
595 s->upscale_h[1] = s->upscale_h[2] = 1;
596 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
597 } else {
598 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
599 else s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16;
600 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
601 }
602 av_assert0(s->nb_components == 4);
603 break;
604 case 0x12121100:
605 case 0x22122100:
606 case 0x21211100:
607 case 0x21112100:
608 case 0x22211200:
609 case 0x22221100:
610 case 0x22112200:
611 case 0x11222200:
612 if (s->bits > 8)
613 goto unk_pixfmt;
614 if (s->adobe_transform == 0 || s->component_id[0] == 'R' &&
615 s->component_id[1] == 'G' && s->component_id[2] == 'B') {
616 s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
617 } else {
618 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
619 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
620 }
621 break;
622 case 0x11000000:
623 case 0x13000000:
624 case 0x14000000:
625 case 0x31000000:
626 case 0x33000000:
627 case 0x34000000:
628 case 0x41000000:
629 case 0x43000000:
630 case 0x44000000:
631 if (s->bits <= 8)
632 s->avctx->pix_fmt = s->force_pal8 ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
633 else
634 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
635 break;
636 case 0x12111100:
637 case 0x14121200:
638 case 0x14111100:
639 case 0x22211100:
640 case 0x22112100:
641 if (s->component_id[0] == 'R' && s->component_id[1] == 'G' && s->component_id[2] == 'B') {
642 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
643 else
644 goto unk_pixfmt;
645 s->upscale_v[1] = s->upscale_v[2] = 1;
646 } else {
647 if (pix_fmt_id == 0x14111100)
648 s->upscale_v[1] = s->upscale_v[2] = 1;
649 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P;
650 else
651 goto unk_pixfmt;
652 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
653 }
654 break;
655 case 0x21111100:
656 if (s->component_id[0] == 'R' && s->component_id[1] == 'G' && s->component_id[2] == 'B') {
657 if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
658 else
659 goto unk_pixfmt;
660 s->upscale_h[1] = s->upscale_h[2] = 1;
661 } else {
662 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
663 else s->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
664 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
665 }
666 break;
667 case 0x11311100:
668 if (s->bits > 8)
669 goto unk_pixfmt;
670 if (s->component_id[0] == 'R' && s->component_id[1] == 'G' && s->component_id[2] == 'B')
671 s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
672 else
673 goto unk_pixfmt;
674 s->upscale_h[0] = s->upscale_h[2] = 2;
675 break;
676 case 0x31111100:
677 if (s->bits > 8)
678 goto unk_pixfmt;
679 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
680 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
681 s->upscale_h[1] = s->upscale_h[2] = 2;
682 break;
683 case 0x22121100:
684 case 0x22111200:
685 case 0x41211100:
686 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
687 else
688 goto unk_pixfmt;
689 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
690 break;
691 case 0x22111100:
692 case 0x23111100:
693 case 0x42111100:
694 case 0x24111100:
695 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
696 else s->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
697 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
698 if (pix_fmt_id == 0x42111100) {
699 if (s->bits > 8)
700 goto unk_pixfmt;
701 s->upscale_h[1] = s->upscale_h[2] = 1;
702 } else if (pix_fmt_id == 0x24111100) {
703 if (s->bits > 8)
704 goto unk_pixfmt;
705 s->upscale_v[1] = s->upscale_v[2] = 1;
706 } else if (pix_fmt_id == 0x23111100) {
707 if (s->bits > 8)
708 goto unk_pixfmt;
709 s->upscale_v[1] = s->upscale_v[2] = 2;
710 }
711 break;
712 case 0x41111100:
713 if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV411P : AV_PIX_FMT_YUVJ411P;
714 else
715 goto unk_pixfmt;
716 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
717 break;
718 default:
719 unk_pixfmt:
720 avpriv_report_missing_feature(s->avctx, "Pixel format 0x%x bits:%d", pix_fmt_id, s->bits);
721 memset(s->upscale_h, 0, sizeof(s->upscale_h));
722 memset(s->upscale_v, 0, sizeof(s->upscale_v));
724 }
725 if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->avctx->lowres) {
726 avpriv_report_missing_feature(s->avctx, "Lowres for weird subsampling");
728 }
729 if (s->ls) {
730 memset(s->upscale_h, 0, sizeof(s->upscale_h));
731 memset(s->upscale_v, 0, sizeof(s->upscale_v));
732 if (s->nb_components == 3) {
733 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
734 } else if (s->nb_components != 1) {
735 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of components %d\n", s->nb_components);
737 } else if ((s->palette_index || s->force_pal8) && s->bits <= 8)
738 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
739 else if (s->bits <= 8)
740 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
741 else
742 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
743 }
744
745 s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
746 if (!s->pix_desc) {
747 av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
748 return AVERROR_BUG;
749 }
750
751 if (s->avctx->pix_fmt == s->hwaccel_sw_pix_fmt && !size_change) {
752 s->avctx->pix_fmt = s->hwaccel_pix_fmt;
753 } else {
754 enum AVPixelFormat pix_fmts[] = {
755#if CONFIG_MJPEG_NVDEC_HWACCEL
757#endif
758#if CONFIG_MJPEG_VAAPI_HWACCEL
760#endif
761 s->avctx->pix_fmt,
763 };
764 av_refstruct_unref(&s->hwaccel_picture_private);
765 s->hwaccel_pix_fmt = ff_get_format(s->avctx, pix_fmts);
766 if (s->hwaccel_pix_fmt < 0)
767 return AVERROR(EINVAL);
768
769 s->hwaccel_sw_pix_fmt = s->avctx->pix_fmt;
770 s->avctx->pix_fmt = s->hwaccel_pix_fmt;
771 }
772
773 if (s->avctx->skip_frame == AVDISCARD_ALL) {
774 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
775 s->picture_ptr->flags |= AV_FRAME_FLAG_KEY;
776 s->got_picture = 1;
777 return 0;
778 }
779
780 av_frame_unref(s->picture_ptr);
781 ret = ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF);
782 if (ret < 0)
783 return ret;
784 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
785 s->picture_ptr->flags |= AV_FRAME_FLAG_KEY;
786 s->got_picture = 1;
787
788 // Lets clear the palette to avoid leaving uninitialized values in it
789 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
790 memset(s->picture_ptr->data[1], 0, 1024);
791
792 for (i = 0; i < 4; i++)
793 s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
794
795 ff_dlog(s->avctx, "%d %d %d %d %d %d\n",
796 s->width, s->height, s->linesize[0], s->linesize[1],
797 s->interlaced, s->avctx->height);
798
799 }
800
801 if ((s->rgb && !s->lossless && !s->ls) ||
802 (!s->rgb && s->ls && s->nb_components > 1) ||
803 (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 && !s->ls)
804 ) {
805 av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
807 }
808
809 /* totally blank picture as progressive JPEG will only add details to it */
810 if (s->progressive) {
811 int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
812 int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
813 for (i = 0; i < s->nb_components; i++) {
814 int size = bw * bh * s->h_count[i] * s->v_count[i];
815 av_freep(&s->blocks[i]);
816 av_freep(&s->last_nnz[i]);
817 s->blocks[i] = av_calloc(size, sizeof(**s->blocks));
818 s->last_nnz[i] = av_calloc(size, sizeof(**s->last_nnz));
819 if (!s->blocks[i] || !s->last_nnz[i])
820 return AVERROR(ENOMEM);
821 s->block_stride[i] = bw * s->h_count[i];
822 }
823 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
824 }
825
826 if (s->avctx->hwaccel) {
827 const FFHWAccel *hwaccel = ffhwaccel(s->avctx->hwaccel);
828 if (!s->raw_image_buffer) {
829 av_log(s->avctx, AV_LOG_ERROR, "SOF without SOI\n");
830 return AVERROR_INVALIDDATA;
831 }
832
833 /* A previous frame may have been aborted before its end_frame call,
834 * leaving the private data behind. */
835 av_refstruct_unref(&s->hwaccel_picture_private);
836
837 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &s->hwaccel_picture_private);
838 if (ret < 0)
839 return ret;
840
841 ret = hwaccel->start_frame(s->avctx, NULL, s->raw_image_buffer,
842 s->raw_image_buffer_size);
843 if (ret < 0) {
844 av_refstruct_unref(&s->hwaccel_picture_private);
845 return ret;
846 }
847 }
848
849 return 0;
850}
851
852static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index, int *val)
853{
854 int code;
855 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
856 if (code < 0 || code > 16) {
857 av_log(s->avctx, AV_LOG_ERROR,
858 "mjpeg_decode_dc: bad vlc: %d\n", dc_index);
859 return AVERROR_INVALIDDATA;
860 }
861
862 *val = code ? get_xbits(&s->gb, code) : 0;
863 return 0;
864}
865
866/* decode block and dequantize */
867static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
868 int dc_index, int ac_index, uint16_t *quant_matrix)
869{
870 int code, i, j, level, val;
871
872 /* DC coef */
873 int ret = mjpeg_decode_dc(s, dc_index, &val);
874 if (ret < 0)
875 return ret;
876
877 val = val * (unsigned)quant_matrix[0] + s->last_dc[component];
878 s->last_dc[component] = val;
879 block[0] = av_clip_int16(val);
880 /* AC coefs */
881 i = 0;
882 {
883 OPEN_READER(re, &s->gb);
884 do {
885 UPDATE_CACHE(re, &s->gb);
886 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
887
888 i += ((unsigned)code) >> 4;
889 code &= 0xf;
890 if (code) {
891 // GET_VLC updates the cache if parsing reaches the second stage.
892 // So we have at least MIN_CACHE_BITS - 9 > 15 bits left here
893 // and don't need to refill the cache.
894 {
895 int cache = GET_CACHE(re, &s->gb);
896 int sign = (~cache) >> 31;
897 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
898 }
899
900 LAST_SKIP_BITS(re, &s->gb, code);
901
902 if (i > 63) {
903 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
904 return AVERROR_INVALIDDATA;
905 }
906 j = s->permutated_scantable[i];
907 block[j] = level * quant_matrix[i];
908 }
909 } while (i < 63);
910 CLOSE_READER(re, &s->gb);
911 }
912
913 return 0;
914}
915
917 int component, int dc_index,
918 uint16_t *quant_matrix, int Al)
919{
920 unsigned val;
921 s->bdsp.clear_block(block);
922 int ret = mjpeg_decode_dc(s, dc_index, &val);
923 if (ret < 0)
924 return ret;
925
926 val = (val * (quant_matrix[0] << Al)) + s->last_dc[component];
927 s->last_dc[component] = val;
928 block[0] = val;
929 return 0;
930}
931
932/* decode block and dequantize - progressive JPEG version */
934 uint8_t *last_nnz, int ac_index,
935 uint16_t *quant_matrix,
936 int Ss, int Se, int Al, int *EOBRUN)
937{
938 int code, i, j, val, run;
939 unsigned level;
940
941 if (*EOBRUN) {
942 (*EOBRUN)--;
943 return 0;
944 }
945
946 {
947 OPEN_READER(re, &s->gb);
948 for (i = Ss; ; i++) {
949 UPDATE_CACHE(re, &s->gb);
950 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
951
952 run = ((unsigned) code) >> 4;
953 code &= 0xF;
954 if (code) {
955 i += run;
956
957 {
958 int cache = GET_CACHE(re, &s->gb);
959 int sign = (~cache) >> 31;
960 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
961 }
962
963 LAST_SKIP_BITS(re, &s->gb, code);
964
965 if (i >= Se) {
966 if (i == Se) {
967 j = s->permutated_scantable[Se];
968 block[j] = level * (quant_matrix[Se] << Al);
969 break;
970 }
971 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
972 return AVERROR_INVALIDDATA;
973 }
974 j = s->permutated_scantable[i];
975 block[j] = level * (quant_matrix[i] << Al);
976 } else {
977 if (run == 0xF) { // ZRL - skip 15 coefficients
978 i += 15;
979 if (i >= Se) {
980 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
981 return AVERROR_INVALIDDATA;
982 }
983 } else {
984 val = (1 << run);
985 if (run) {
986 // Given that GET_VLC reloads internally, we always
987 // have at least 16 bits in the cache here.
988 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
989 LAST_SKIP_BITS(re, &s->gb, run);
990 }
991 *EOBRUN = val - 1;
992 break;
993 }
994 }
995 }
996 CLOSE_READER(re, &s->gb);
997 }
998
999 if (i > *last_nnz)
1000 *last_nnz = i;
1001
1002 return 0;
1003}
1004
1005#define REFINE_BIT(j) { \
1006 UPDATE_CACHE(re, &s->gb); \
1007 sign = block[j] >> 15; \
1008 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
1009 ((quant_matrix[i] ^ sign) - sign) << Al; \
1010 LAST_SKIP_BITS(re, &s->gb, 1); \
1011}
1012
1013#define ZERO_RUN \
1014for (; ; i++) { \
1015 if (i > last) { \
1016 i += run; \
1017 if (i > Se) { \
1018 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
1019 return -1; \
1020 } \
1021 break; \
1022 } \
1023 j = s->permutated_scantable[i]; \
1024 if (block[j]) \
1025 REFINE_BIT(j) \
1026 else if (run-- == 0) \
1027 break; \
1028}
1029
1030/* decode block and dequantize - progressive JPEG refinement pass */
1032 uint8_t *last_nnz,
1033 int ac_index, uint16_t *quant_matrix,
1034 int Ss, int Se, int Al, int *EOBRUN)
1035{
1036 int code, i = Ss, j, sign, val, run;
1037 int last = FFMIN(Se, *last_nnz);
1038
1039 OPEN_READER(re, &s->gb);
1040 if (*EOBRUN) {
1041 (*EOBRUN)--;
1042 } else {
1043 for (; ; i++) {
1044 UPDATE_CACHE(re, &s->gb);
1045 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
1046
1047 if (code & 0xF) {
1048 run = ((unsigned) code) >> 4;
1049 val = SHOW_UBITS(re, &s->gb, 1);
1050 LAST_SKIP_BITS(re, &s->gb, 1);
1051 ZERO_RUN;
1052 j = s->permutated_scantable[i];
1053 val--;
1054 block[j] = ((quant_matrix[i] << Al) ^ val) - val;
1055 if (i == Se) {
1056 if (i > *last_nnz)
1057 *last_nnz = i;
1058 CLOSE_READER(re, &s->gb);
1059 return 0;
1060 }
1061 } else {
1062 run = ((unsigned) code) >> 4;
1063 if (run == 0xF) {
1064 ZERO_RUN;
1065 } else {
1066 val = run;
1067 run = (1 << run);
1068 if (val) {
1069 // Given that GET_VLC reloads internally, we always
1070 // have at least 16 bits in the cache here.
1071 run += SHOW_UBITS(re, &s->gb, val);
1072 LAST_SKIP_BITS(re, &s->gb, val);
1073 }
1074 *EOBRUN = run - 1;
1075 break;
1076 }
1077 }
1078 }
1079
1080 if (i > *last_nnz)
1081 *last_nnz = i;
1082 }
1083
1084 for (; i <= last; i++) {
1085 j = s->permutated_scantable[i];
1086 if (block[j])
1087 REFINE_BIT(j)
1088 }
1089 CLOSE_READER(re, &s->gb);
1090
1091 return 0;
1092}
1093#undef REFINE_BIT
1094#undef ZERO_RUN
1095
1096/* Handles 1 to 4 components */
1098{
1099 int nb_components = s->nb_components_sos;
1100 int predictor = s->Ss;
1101 int point_transform = s->Al;
1102 int i, mb_x, mb_y;
1103 uint16_t (*buffer)[4];
1104 int left[4], top[4], topleft[4];
1105 const int linesize = s->linesize[0];
1106 const int mask = ((1 << s->bits) - 1) << point_transform;
1107 int resync_mb_y = 0;
1108 int resync_mb_x = 0;
1109 int ret;
1110
1111 if (!s->bayer && s->nb_components < 3)
1112 return AVERROR_INVALIDDATA;
1113 if (s->bayer && s->nb_components > 2)
1114 return AVERROR_INVALIDDATA;
1115 if (s->nb_components <= 0 || s->nb_components > 4)
1116 return AVERROR_INVALIDDATA;
1117 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
1118 return AVERROR_INVALIDDATA;
1119 if (s->bayer) {
1120 if (s->rct || s->pegasus_rct)
1121 return AVERROR_INVALIDDATA;
1122 }
1123 if ((s->rct || s->pegasus_rct) && nb_components != s->nb_components)
1124 return AVERROR_INVALIDDATA;
1125
1126 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
1127 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
1128 if (!s->ljpeg_buffer)
1129 return AVERROR(ENOMEM);
1130
1131 buffer = s->ljpeg_buffer;
1132
1133 for (i = 0; i < 4; i++)
1134 buffer[0][i] = 1 << (s->bits - 1);
1135
1136 s->restart_count = -1;
1137
1138 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1139 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
1140
1141 if (s->interlaced && s->bottom_field)
1142 ptr += linesize >> 1;
1143
1144 for (i = 0; i < 4; i++)
1145 top[i] = left[i] = topleft[i] = buffer[0][i];
1146
1147 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1148 int modified_predictor = predictor;
1149 int restart;
1150
1151 ret = ff_mjpeg_handle_restart(s, &restart);
1152 if (ret < 0)
1153 return ret;
1154 if (restart) {
1155 resync_mb_x = mb_x;
1156 resync_mb_y = mb_y;
1157 for (i = 0; i < 4; i++)
1158 top[i] = left[i] = topleft[i] = 1 << (s->bits - 1);
1159 }
1160
1161 if (get_bits_left(&s->gb) < 1) {
1162 av_log(s->avctx, AV_LOG_ERROR, "bitstream end in rgb_scan\n");
1163 return AVERROR_INVALIDDATA;
1164 }
1165
1166 if (mb_y == resync_mb_y || mb_y == resync_mb_y + 1 && mb_x < resync_mb_x || !mb_x)
1167 modified_predictor = 1;
1168
1169 for (i = 0; i < nb_components; i++) {
1170 int pred, dc;
1171
1172 topleft[i] = top[i];
1173 top[i] = buffer[mb_x][i];
1174
1175 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
1176
1177 ret = mjpeg_decode_dc(s, s->dc_index[i], &dc);
1178 if (ret < 0)
1179 return ret;
1180
1181 left[i] = buffer[mb_x][i] =
1182 mask & (pred + (unsigned)(dc * (1 << point_transform)));
1183 }
1184 }
1185 if (s->rct && s->nb_components == 4) {
1186 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1187 ptr[4 * mb_x + 2] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1188 ptr[4 * mb_x + 1] = buffer[mb_x][1] + ptr[4 * mb_x + 2];
1189 ptr[4 * mb_x + 3] = buffer[mb_x][2] + ptr[4 * mb_x + 2];
1190 ptr[4 * mb_x + 0] = buffer[mb_x][3];
1191 }
1192 } else if (s->nb_components == 4) {
1193 for (i = 0; i < nb_components; i++) {
1194 int c = s->comp_index[i];
1195 if (s->bits <= 8) {
1196 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1197 ptr[4 * mb_x + 3 - c] = buffer[mb_x][i];
1198 }
1199 } else if (s->bits == 9) {
1200 return AVERROR_PATCHWELCOME;
1201 } else {
1202 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1203 ((uint16_t*)ptr)[4 * mb_x + c] = buffer[mb_x][i];
1204 }
1205 }
1206 }
1207 } else if (s->rct) {
1208 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1209 ptr[3 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1210 ptr[3 * mb_x + 0] = buffer[mb_x][1] + ptr[3 * mb_x + 1];
1211 ptr[3 * mb_x + 2] = buffer[mb_x][2] + ptr[3 * mb_x + 1];
1212 }
1213 } else if (s->pegasus_rct) {
1214 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1215 ptr[3 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
1216 ptr[3 * mb_x + 0] = buffer[mb_x][1] + ptr[3 * mb_x + 1];
1217 ptr[3 * mb_x + 2] = buffer[mb_x][2] + ptr[3 * mb_x + 1];
1218 }
1219 } else if (s->bayer) {
1220 if (s->bits <= 8)
1221 return AVERROR_PATCHWELCOME;
1222 if (nb_components == 1) {
1223 /* Leave decoding to the TIFF/DNG decoder (see comment in ff_mjpeg_decode_sof) */
1224 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
1225 ((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
1226 } else if (nb_components == 2) {
1227 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1228 ((uint16_t*)ptr)[2 * mb_x + 0] = buffer[mb_x][0];
1229 ((uint16_t*)ptr)[2 * mb_x + 1] = buffer[mb_x][1];
1230 }
1231 }
1232 } else {
1233 for (i = 0; i < nb_components; i++) {
1234 int c = s->comp_index[i];
1235 if (s->bits <= 8) {
1236 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1237 ptr[3 * mb_x + 2 - c] = buffer[mb_x][i];
1238 }
1239 } else if (s->bits == 9) {
1240 return AVERROR_PATCHWELCOME;
1241 } else {
1242 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1243 ((uint16_t*)ptr)[3 * mb_x + 2 - c] = buffer[mb_x][i];
1244 }
1245 }
1246 }
1247 }
1248 }
1249 return 0;
1250}
1251
1253{
1254 int predictor = s->Ss;
1255 int point_transform = s->Al;
1256 int nb_components = s->nb_components_sos;
1257 int i, mb_x, mb_y, mask;
1258 int bits = (s->bits + 7) & ~7;
1259 int resync_mb_y = 0;
1260 int resync_mb_x = 0;
1261 int ret;
1262
1263 point_transform += bits - s->bits;
1264 mask = ((1 << s->bits) - 1) << point_transform;
1265
1266 av_assert0(nb_components >= 1 && nb_components <= 4);
1267
1268 s->restart_count = -1;
1269
1270 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1271 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1272 int restart;
1273 ret = ff_mjpeg_handle_restart(s, &restart);
1274 if (ret < 0)
1275 return ret;
1276 if (restart) {
1277 resync_mb_x = mb_x;
1278 resync_mb_y = mb_y;
1279 }
1280
1281 if (get_bits_left(&s->gb) < 1) {
1282 av_log(s->avctx, AV_LOG_ERROR, "bitstream end in yuv_scan\n");
1283 return AVERROR_INVALIDDATA;
1284 }
1285
1286 if (!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y + 1 && mb_x < resync_mb_x || s->interlaced) {
1287 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y + 1 && mb_x < resync_mb_x;
1288 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1289 for (i = 0; i < nb_components; i++) {
1290 uint8_t *ptr;
1291 uint16_t *ptr16;
1292 int n, h, v, x, y, c, j, linesize;
1293 n = s->nb_blocks[i];
1294 c = s->comp_index[i];
1295 h = s->h_scount[i];
1296 v = s->v_scount[i];
1297 x = 0;
1298 y = 0;
1299 linesize = s->linesize[c];
1300
1301 if (bits > 8) linesize /= 2;
1302
1303 for (j = 0; j < n; j++) {
1304 int pred, dc;
1305
1306 ret = mjpeg_decode_dc(s, s->dc_index[i], &dc);
1307 if (ret < 0)
1308 return ret;
1309
1310 if ( h * mb_x + x >= s->width
1311 || v * mb_y + y >= s->height) {
1312 // Nothing to do
1313 } else if (bits <= 8) {
1314 ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); // FIXME optimize this crap
1315 if (y == 0 && toprow) {
1316 if (x == 0 && leftcol) {
1317 pred = 1 << (bits - 1);
1318 } else {
1319 pred = ptr[-1];
1320 }
1321 } else {
1322 if (x == 0 && leftcol) {
1323 pred = ptr[-linesize];
1324 } else {
1325 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
1326 }
1327 }
1328
1329 if (s->interlaced && s->bottom_field)
1330 ptr += linesize >> 1;
1331 pred &= mask;
1332 *ptr = pred + ((unsigned)dc << point_transform);
1333 } else {
1334 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2 * (linesize * (v * mb_y + y)) + 2 * (h * mb_x + x)); // FIXME optimize this crap
1335 if (y == 0 && toprow) {
1336 if (x == 0 && leftcol) {
1337 pred = 1 << (bits - 1);
1338 } else {
1339 pred = ptr16[-1];
1340 }
1341 } else {
1342 if (x == 0 && leftcol) {
1343 pred = ptr16[-linesize];
1344 } else {
1345 PREDICT(pred, ptr16[-linesize - 1], ptr16[-linesize], ptr16[-1], predictor);
1346 }
1347 }
1348
1349 if (s->interlaced && s->bottom_field)
1350 ptr16 += linesize >> 1;
1351 pred &= mask;
1352 *ptr16 = pred + ((unsigned)dc << point_transform);
1353 }
1354 if (++x == h) {
1355 x = 0;
1356 y++;
1357 }
1358 }
1359 }
1360 } else {
1361 for (i = 0; i < nb_components; i++) {
1362 uint8_t *ptr;
1363 uint16_t *ptr16;
1364 int n, h, v, x, y, c, j, linesize, dc;
1365 n = s->nb_blocks[i];
1366 c = s->comp_index[i];
1367 h = s->h_scount[i];
1368 v = s->v_scount[i];
1369 x = 0;
1370 y = 0;
1371 linesize = s->linesize[c];
1372
1373 if (bits > 8) linesize /= 2;
1374
1375 for (j = 0; j < n; j++) {
1376 int pred;
1377
1378 ret = mjpeg_decode_dc(s, s->dc_index[i], &dc);
1379 if (ret < 0)
1380 return ret;
1381
1382 if ( h * mb_x + x >= s->width
1383 || v * mb_y + y >= s->height) {
1384 // Nothing to do
1385 } else if (bits <= 8) {
1386 ptr = s->picture_ptr->data[c] +
1387 (linesize * (v * mb_y + y)) +
1388 (h * mb_x + x); // FIXME optimize this crap
1389 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
1390
1391 pred &= mask;
1392 *ptr = pred + ((unsigned)dc << point_transform);
1393 } else {
1394 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2 * (linesize * (v * mb_y + y)) + 2 * (h * mb_x + x)); // FIXME optimize this crap
1395 PREDICT(pred, ptr16[-linesize - 1], ptr16[-linesize], ptr16[-1], predictor);
1396
1397 pred &= mask;
1398 *ptr16 = pred + ((unsigned)dc << point_transform);
1399 }
1400
1401 if (++x == h) {
1402 x = 0;
1403 y++;
1404 }
1405 }
1406 }
1407 }
1408 }
1409 }
1410 return 0;
1411}
1412
1414 uint8_t *dst, const uint8_t *src,
1415 int linesize, int lowres)
1416{
1417 switch (lowres) {
1418 case 0: s->copy_block(dst, src, linesize, 8);
1419 break;
1420 case 1: copy_block4(dst, src, linesize, linesize, 4);
1421 break;
1422 case 2: copy_block2(dst, src, linesize, linesize, 2);
1423 break;
1424 case 3: *dst = *src;
1425 break;
1426 }
1427}
1428
1429static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1430{
1431 int block_x, block_y;
1432 int size = 8 >> s->avctx->lowres;
1433 if (s->bits > 8) {
1434 for (block_y = 0; block_y < size; block_y++)
1435 for (block_x = 0; block_x < size; block_x++)
1436 *(uint16_t*)(ptr + 2 * block_x + block_y * linesize) <<= 16 - s->bits;
1437 } else {
1438 for (block_y = 0; block_y < size; block_y++)
1439 for (block_x = 0; block_x < size; block_x++)
1440 *(ptr + block_x + block_y * linesize) <<= 8 - s->bits;
1441 }
1442}
1443
1445{
1446 int nb_components = s->nb_components_sos;
1447 int Ah = s->Ah;
1448 int Al = s->Al;
1449 const uint8_t *mb_bitmask = NULL;
1450 const AVFrame *reference = NULL;
1451 int i, mb_x, mb_y, chroma_h_shift, chroma_v_shift, chroma_width, chroma_height;
1452 uint8_t *data[MAX_COMPONENTS];
1453 const uint8_t *reference_data[MAX_COMPONENTS];
1454 int linesize[MAX_COMPONENTS];
1455 GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1456 int bytes_per_pixel = 1 + (s->bits > 8);
1457 int field_pos = -1;
1458 int ret;
1459
1460 if (s->avctx->codec_id == AV_CODEC_ID_MXPEG) {
1461 mb_bitmask = s->mb_bitmask;
1462 reference = s->reference;
1463 }
1464
1465 if (mb_bitmask) {
1466 if (s->mb_bitmask_size != (s->mb_width * s->mb_height + 7) >> 3) {
1467 av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1468 return AVERROR_INVALIDDATA;
1469 }
1470 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1471 }
1472
1473 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &chroma_h_shift,
1474 &chroma_v_shift);
1475 chroma_width = AV_CEIL_RSHIFT(s->width, chroma_h_shift);
1476 chroma_height = AV_CEIL_RSHIFT(s->height, chroma_v_shift);
1477
1478 for (i = 0; i < nb_components; i++) {
1479 int c = s->comp_index[i];
1480 data[c] = s->picture_ptr->data[c];
1481 reference_data[c] = reference ? reference->data[c] : NULL;
1482 linesize[c] = s->linesize[c];
1483 s->coefs_finished[c] |= 1;
1484 }
1485
1486next_field:
1487 s->restart_count = -1;
1488
1489 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1490 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1491 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1492 int restart;
1493
1494 ret = ff_mjpeg_handle_restart(s, &restart);
1495 if (ret < 0)
1496 return ret;
1497 if (restart) {
1498 for (i = 0; i < nb_components; i++)
1499 s->last_dc[i] = (4 << s->bits);
1500 }
1501
1502 if (get_bits_left(&s->gb) < 0) {
1503 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1504 -get_bits_left(&s->gb));
1505 return AVERROR_INVALIDDATA;
1506 }
1507 for (i = 0; i < nb_components; i++) {
1508 uint8_t *ptr;
1509 int n, h, v, x, y, c, j;
1510 int block_offset;
1511 n = s->nb_blocks[i];
1512 c = s->comp_index[i];
1513 h = s->h_scount[i];
1514 v = s->v_scount[i];
1515 x = 0;
1516 y = 0;
1517 for (j = 0; j < n; j++) {
1518 block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1519 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1520
1521 if (s->interlaced && s->bottom_field)
1522 block_offset += linesize[c] >> 1;
1523 if ( 8 * (h * mb_x + x) < ((c == 1) || (c == 2) ? chroma_width : s->width)
1524 && 8 * (v * mb_y + y) < ((c == 1) || (c == 2) ? chroma_height : s->height)) {
1525 ptr = data[c] + block_offset;
1526 } else
1527 ptr = NULL;
1528 if (!s->progressive) {
1529 if (copy_mb) {
1530 if (ptr)
1531 mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1532 linesize[c], s->avctx->lowres);
1533
1534 } else {
1535 s->bdsp.clear_block(s->block);
1536 if (decode_block(s, s->block, i,
1537 s->dc_index[i], s->ac_index[i],
1538 s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1539 av_log(s->avctx, AV_LOG_ERROR,
1540 "error y=%d x=%d\n", mb_y, mb_x);
1541 return AVERROR_INVALIDDATA;
1542 }
1543 if (ptr && linesize[c]) {
1544 s->idsp.idct_put(ptr, linesize[c], s->block);
1545 if (s->bits & 7)
1546 shift_output(s, ptr, linesize[c]);
1547 }
1548 }
1549 } else {
1550 int block_idx = s->block_stride[c] * (v * mb_y + y) +
1551 (h * mb_x + x);
1552 int16_t *block = s->blocks[c][block_idx];
1553 if (Ah)
1554 block[0] += get_bits1(&s->gb) *
1555 s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1556 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1557 s->quant_matrixes[s->quant_sindex[i]],
1558 Al) < 0) {
1559 av_log(s->avctx, AV_LOG_ERROR,
1560 "error y=%d x=%d\n", mb_y, mb_x);
1561 return AVERROR_INVALIDDATA;
1562 }
1563 }
1564 ff_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1565 ff_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1566 mb_x, mb_y, x, y, c, s->bottom_field,
1567 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1568 if (++x == h) {
1569 x = 0;
1570 y++;
1571 }
1572 }
1573 }
1574 }
1575 }
1576
1577 if (s->interlaced &&
1578 bytestream2_get_bytes_left(&s->gB) > 2 &&
1579 bytestream2_tell(&s->gB) > 2 &&
1580 bytestream2_tell(&s->gB) != field_pos &&
1581 s->gB.buffer[-2] == 0xFF &&
1582 s->gB.buffer[-1] == 0xD1) {
1583 av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1584 field_pos = bytestream2_tell(&s->gB);
1585 s->bottom_field ^= 1;
1586
1587 goto next_field;
1588 }
1589
1590 return 0;
1591}
1592
1594{
1595 int Ss = s->Ss;
1596 int Se = s->Se;
1597 int Ah = s->Ah;
1598 int Al = s->Al;
1599 int mb_x, mb_y;
1600 int EOBRUN = 0;
1601 int c = s->comp_index[0];
1602 uint16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1603
1604 av_assert0(Ss >= 0 && Ah >= 0 && Al >= 0);
1605 if (Se < Ss || Se > 63) {
1606 av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", Ss, Se);
1607 return AVERROR_INVALIDDATA;
1608 }
1609
1610 // s->coefs_finished is a bitmask for coefficients coded
1611 // Ss and Se are parameters telling start and end coefficients
1612 s->coefs_finished[c] |= (2ULL << Se) - (1ULL << Ss);
1613
1614 s->restart_count = -1;
1615
1616 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1617 int block_idx = mb_y * s->block_stride[c];
1618 int16_t (*block)[64] = &s->blocks[c][block_idx];
1619 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1620 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1621 int ret;
1622 int restart;
1623 ret = ff_mjpeg_handle_restart(s, &restart);
1624 if (ret < 0)
1625 return ret;
1626 if (restart)
1627 EOBRUN = 0;
1628
1629 if (Ah)
1630 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1631 quant_matrix, Ss, Se, Al, &EOBRUN);
1632 else
1633 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1634 quant_matrix, Ss, Se, Al, &EOBRUN);
1635
1636 if (ret >= 0 && get_bits_left(&s->gb) < 0)
1638 if (ret < 0) {
1639 av_log(s->avctx, AV_LOG_ERROR,
1640 "error y=%d x=%d\n", mb_y, mb_x);
1641 return AVERROR_INVALIDDATA;
1642 }
1643 }
1644 }
1645 return 0;
1646}
1647
1649{
1650 int mb_x, mb_y;
1651 int c;
1652 const int bytes_per_pixel = 1 + (s->bits > 8);
1653 const int block_size = s->lossless ? 1 : 8;
1654
1655 for (c = 0; c < s->nb_components; c++) {
1656 uint8_t *data = s->picture_ptr->data[c];
1657 int linesize = s->linesize[c];
1658 int h = s->h_max / s->h_count[c];
1659 int v = s->v_max / s->v_count[c];
1660 int mb_width = (s->width + h * block_size - 1) / (h * block_size);
1661 int mb_height = (s->height + v * block_size - 1) / (v * block_size);
1662
1663 if (~s->coefs_finished[c])
1664 av_log(s->avctx, AV_LOG_WARNING, "component %d is incomplete\n", c);
1665
1666 if (s->interlaced && s->bottom_field)
1667 data += linesize >> 1;
1668
1669 for (mb_y = 0; mb_y < mb_height; mb_y++) {
1670 uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1671 int block_idx = mb_y * s->block_stride[c];
1672 int16_t (*block)[64] = &s->blocks[c][block_idx];
1673 for (mb_x = 0; mb_x < mb_width; mb_x++, block++) {
1674 s->idsp.idct_put(ptr, linesize, *block);
1675 if (s->bits & 7)
1676 shift_output(s, ptr, linesize);
1677 ptr += bytes_per_pixel * 8 >> s->avctx->lowres;
1678 }
1679 }
1680 }
1681}
1682
1684{
1685 int len, i, h, v;
1686 int index, id, ret;
1687 const int block_size = s->lossless ? 1 : 8;
1688
1689 if (!s->got_picture) {
1690 av_log(s->avctx, AV_LOG_WARNING,
1691 "Can not process SOS before SOF, skipping\n");
1692 return AVERROR_INVALIDDATA;
1693 }
1694
1695 ret = mjpeg_parse_len(s, &len, "sos");
1696 if (ret < 0)
1697 return ret;
1698 if (len < 1)
1699 return AVERROR_INVALIDDATA;
1700 s->nb_components_sos = bytestream2_get_byteu(&s->gB);
1701 if (s->nb_components_sos == 0 || s->nb_components_sos > MAX_COMPONENTS) {
1703 "decode_sos: nb_components (%d)",
1704 s->nb_components_sos);
1705 return AVERROR_PATCHWELCOME;
1706 }
1707 if (len != 4 + 2 * s->nb_components_sos) {
1708 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: len(%d) mismatch %d components\n", len, s->nb_components_sos);
1709 return AVERROR_INVALIDDATA;
1710 }
1711 for (i = 0; i < s->nb_components_sos; i++) {
1712 id = bytestream2_get_byteu(&s->gB);
1713 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1714 /* find component index */
1715 for (index = 0; index < s->nb_components; index++)
1716 if (id == s->component_id[index])
1717 break;
1718 if (index == s->nb_components) {
1719 av_log(s->avctx, AV_LOG_ERROR,
1720 "decode_sos: index(%d) out of components\n", index);
1721 return AVERROR_INVALIDDATA;
1722 }
1723 /* Metasoft MJPEG codec has Cb and Cr swapped */
1724 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1725 && s->nb_components_sos == 3 && s->nb_components == 3 && i)
1726 index = 3 - i;
1727
1728 s->quant_sindex[i] = s->quant_index[index];
1729 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1730 s->h_scount[i] = s->h_count[index];
1731 s->v_scount[i] = s->v_count[index];
1732
1733 s->comp_index[i] = index;
1734
1735 uint8_t b = bytestream2_get_byteu(&s->gB);
1736 s->dc_index[i] = b >> 4;
1737 s->ac_index[i] = b & 0x0F;
1738
1739 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1740 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1741 goto out_of_range;
1742 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))
1743 goto out_of_range;
1744 }
1745
1746 s->Ss = bytestream2_get_byteu(&s->gB); /* JPEG Ss / lossless JPEG predictor / JPEG-LS NEAR */
1747 s->Se = bytestream2_get_byteu(&s->gB); /* JPEG Se / JPEG-LS ILV */
1748 uint8_t b = bytestream2_get_byteu(&s->gB);
1749 s->Ah = b >> 4; /* Ah */
1750 s->Al = b & 0x0F; /* Al */
1751
1752 if (s->nb_components_sos > 1) {
1753 /* interleaved stream */
1754 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1755 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1756 } else if (!s->ls) { /* skip this for JPEG-LS */
1757 h = s->h_max / s->h_scount[0];
1758 v = s->v_max / s->v_scount[0];
1759 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1760 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1761 s->nb_blocks[0] = 1;
1762 s->h_scount[0] = 1;
1763 s->v_scount[0] = 1;
1764 }
1765
1766 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1767 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1768 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1769 s->Ss, s->Al, s->Se, s->bits, s->mjpb_skiptosod,
1770 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), s->nb_components_sos);
1771
1772
1773 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1774 if (s->mjpb_skiptosod)
1775 bytestream2_skip(&s->gB, s->mjpb_skiptosod);
1776
1777 if (!s->progressive && !s->lossless &&
1778 s->avctx->codec_id != AV_CODEC_ID_MXPEG) {
1779 s->nb_seq_component_scans += s->nb_components_sos;
1780 if (s->nb_seq_component_scans > s->nb_components) {
1781 av_log(s->avctx, AV_LOG_ERROR,
1782 "too many scans for a sequential image\n");
1783 return AVERROR_INVALIDDATA;
1784 }
1785 }
1786
1787 if (s->avctx->hwaccel) {
1788 const uint8_t *buf_ptr;
1789 size_t buf_size;
1790
1791 mjpeg_find_raw_scan_data(s, &buf_ptr, &buf_size);
1792
1793 ret = FF_HW_CALL(s->avctx, decode_slice, buf_ptr, buf_size);
1794 if (ret < 0)
1795 return ret;
1796
1797 } else {
1798 if (s->lossless) {
1799 av_assert0(s->picture_ptr == s->picture);
1800 if (CONFIG_JPEGLS_DECODER && s->ls) {
1801 if ((ret = ff_jpegls_decode_picture(s)) < 0)
1802 return ret;
1803 } else {
1804 if (s->rgb || s->bayer) {
1805 if ((ret = ljpeg_decode_rgb_scan(s)) < 0)
1806 return ret;
1807 } else {
1808 if ((ret = ljpeg_decode_yuv_scan(s)) < 0)
1809 return ret;
1810 }
1811 }
1812 } else {
1813 if (s->progressive && s->Ss) {
1814 av_assert0(s->picture_ptr == s->picture);
1816 return ret;
1817 } else {
1818 if ((ret = mjpeg_decode_scan(s)) < 0)
1819 return ret;
1820 }
1821 }
1822 }
1823
1824 if (s->avctx->codec_id == AV_CODEC_ID_MEDIA100 ||
1825 s->avctx->codec_id == AV_CODEC_ID_MJPEGB ||
1826 s->avctx->codec_id == AV_CODEC_ID_THP) {
1827 /* Add the amount of bits read from the unescaped image data buffer
1828 * into the GetByteContext. */
1829 bytestream2_skipu(&s->gB, (get_bits_count(&s->gb) + 7) / 8);
1830 }
1831
1832 return 0;
1833 out_of_range:
1834 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1835 return AVERROR_INVALIDDATA;
1836}
1837
1839{
1840 if (bytestream2_get_be16u(&s->gB) != 4)
1841 return AVERROR_INVALIDDATA;
1842 s->restart_interval = bytestream2_get_be16u(&s->gB);
1843 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1844 s->restart_interval);
1845
1846 return 0;
1847}
1848
1850{
1851 int len, id, i;
1852
1853 int ret = mjpeg_parse_len(s, &len, "app");
1854 if (ret < 0)
1855 return AVERROR_INVALIDDATA;
1856
1857 if (len < 4) {
1858 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1859 return AVERROR_INVALIDDATA;
1860 av_log(s->avctx, AV_LOG_VERBOSE, "skipping APPx stub (len=%" PRId32 ")\n", len);
1861 goto out;
1862 }
1863
1864 id = bytestream2_get_be32u(&s->gB);
1865 len -= 4;
1866
1867 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1868 av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n",
1869 av_fourcc2str(av_bswap32(id)), id, len);
1870
1871 /* This fourcc is used by non-avid files too, it holds some
1872 information, but it's always present in AVID-created files. */
1873 if (id == AV_RB32("AVI1")) {
1874 /* structure:
1875 4bytes AVI1
1876 1bytes polarity
1877 1bytes always zero
1878 4bytes field_size
1879 4bytes field_size_less_padding
1880 */
1881 if (len < 1)
1882 goto out;
1883 i = bytestream2_get_byteu(&s->gB); len--;
1884 av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1885 goto out;
1886 }
1887
1888 if (id == AV_RB32("JFIF")) {
1889 int t_w, t_h, v1, v2;
1890 if (len < 8)
1891 goto out;
1892 bytestream2_skipu(&s->gB, 1); /* the trailing zero-byte */
1893 v1 = bytestream2_get_byteu(&s->gB);
1894 v2 = bytestream2_get_byteu(&s->gB);
1895 bytestream2_skipu(&s->gB, 1);
1896
1897 s->avctx->sample_aspect_ratio.num = bytestream2_get_be16u(&s->gB);
1898 s->avctx->sample_aspect_ratio.den = bytestream2_get_be16u(&s->gB);
1899 if ( s->avctx->sample_aspect_ratio.num <= 0
1900 || s->avctx->sample_aspect_ratio.den <= 0) {
1901 s->avctx->sample_aspect_ratio.num = 0;
1902 s->avctx->sample_aspect_ratio.den = 1;
1903 }
1904
1905 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1906 av_log(s->avctx, AV_LOG_INFO,
1907 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1908 v1, v2,
1909 s->avctx->sample_aspect_ratio.num,
1910 s->avctx->sample_aspect_ratio.den);
1911
1912 len -= 8;
1913 if (len >= 2) {
1914 t_w = bytestream2_get_byteu(&s->gB);
1915 t_h = bytestream2_get_byteu(&s->gB);
1916 if (t_w && t_h) {
1917 /* skip thumbnail */
1918 if (len - 10 - (t_w * t_h * 3) > 0)
1919 len -= t_w * t_h * 3;
1920 }
1921 len -= 2;
1922 }
1923 goto out;
1924 }
1925
1926 if ( id == AV_RB32("Adob")
1927 && len >= 8
1928 && bytestream2_peek_byteu(&s->gB) == 'e'
1929 && bytestream2_peek_be32u(&s->gB) != AV_RB32("e_CM")) {
1930 bytestream2_skipu(&s->gB, 1); /* 'e' */
1931 bytestream2_skipu(&s->gB, 2); /* version */
1932 bytestream2_skipu(&s->gB, 2); /* flags0 */
1933 bytestream2_skipu(&s->gB, 2); /* flags1 */
1934 s->adobe_transform = bytestream2_get_byteu(&s->gB);
1935 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1936 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1937 len -= 8;
1938 goto out;
1939 }
1940
1941 if (id == AV_RB32("LJIF")) {
1942 int rgb = s->rgb;
1943 int pegasus_rct = s->pegasus_rct;
1944 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1945 av_log(s->avctx, AV_LOG_INFO,
1946 "Pegasus lossless jpeg header found\n");
1947 if (len < 9)
1948 goto out;
1949 bytestream2_skipu(&s->gB, 2); /* version ? */
1950 bytestream2_skipu(&s->gB, 2); /* unknown always 0? */
1951 bytestream2_skipu(&s->gB, 2); /* unknown always 0? */
1952 bytestream2_skipu(&s->gB, 2); /* unknown always 0? */
1953 switch (i = bytestream2_get_byteu(&s->gB)) {
1954 case 1:
1955 rgb = 1;
1956 pegasus_rct = 0;
1957 break;
1958 case 2:
1959 rgb = 1;
1960 pegasus_rct = 1;
1961 break;
1962 default:
1963 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1964 }
1965
1966 len -= 9;
1967 if (s->bayer)
1968 goto out;
1969 if (s->got_picture)
1970 if (rgb != s->rgb || pegasus_rct != s->pegasus_rct) {
1971 av_log(s->avctx, AV_LOG_WARNING, "Mismatching LJIF tag\n");
1972 goto out;
1973 }
1974
1975 s->rgb = rgb;
1976 s->pegasus_rct = pegasus_rct;
1977
1978 goto out;
1979 }
1980 if (id == AV_RL32("colr") && len > 0) {
1981 s->colr = bytestream2_get_byteu(&s->gB);
1982 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1983 av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1984 len--;
1985 goto out;
1986 }
1987 if (id == AV_RL32("xfrm") && len > 0) {
1988 s->xfrm = bytestream2_get_byteu(&s->gB);
1989 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1990 av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1991 len--;
1992 goto out;
1993 }
1994
1995 /* JPS extension by VRex */
1996 if (start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1997 int flags, layout, type;
1998 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1999 av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
2000
2001 bytestream2_skipu(&s->gB, 4); len -= 4; /* JPS_ */
2002 bytestream2_skipu(&s->gB, 2); len -= 2; /* block length */
2003 bytestream2_skipu(&s->gB, 1); /* reserved */
2004 flags = bytestream2_get_byteu(&s->gB);
2005 layout = bytestream2_get_byteu(&s->gB);
2006 type = bytestream2_get_byteu(&s->gB);
2007 len -= 4;
2008
2009 av_freep(&s->stereo3d);
2010 s->stereo3d = av_stereo3d_alloc();
2011 if (!s->stereo3d) {
2012 goto out;
2013 }
2014 if (type == 0) {
2015 s->stereo3d->type = AV_STEREO3D_2D;
2016 } else if (type == 1) {
2017 switch (layout) {
2018 case 0x01:
2019 s->stereo3d->type = AV_STEREO3D_LINES;
2020 break;
2021 case 0x02:
2022 s->stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
2023 break;
2024 case 0x03:
2025 s->stereo3d->type = AV_STEREO3D_TOPBOTTOM;
2026 break;
2027 }
2028 if (!(flags & 0x04)) {
2029 s->stereo3d->flags = AV_STEREO3D_FLAG_INVERT;
2030 }
2031 }
2032 goto out;
2033 }
2034
2035 /* EXIF metadata */
2036 if (start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
2037 int ret;
2038
2039 bytestream2_skipu(&s->gB, 2); // skip padding
2040 len -= 2;
2041
2042 if (s->exif_metadata.entries) {
2043 av_log(s->avctx, AV_LOG_WARNING, "multiple EXIF\n");
2044 goto out;
2045 }
2046
2047 ret = av_exif_parse_buffer(s->avctx, s->gB.buffer, len, &s->exif_metadata, AV_EXIF_TIFF_HEADER);
2048 if (ret < 0) {
2049 av_log(s->avctx, AV_LOG_WARNING, "unable to parse EXIF buffer\n");
2050 goto out;
2051 }
2052
2053 bytestream2_skipu(&s->gB, ret);
2054 len -= ret;
2055
2056 goto out;
2057 }
2058
2059 /* Apple MJPEG-A */
2060 if ((start_code == APP1) && (len > (0x28 - 8))) {
2061 id = bytestream2_get_be32u(&s->gB);
2062 len -= 4;
2063 /* Apple MJPEG-A */
2064 if (id == AV_RB32("mjpg")) {
2065 /* structure:
2066 4bytes field size
2067 4bytes pad field size
2068 4bytes next off
2069 4bytes quant off
2070 4bytes huff off
2071 4bytes image off
2072 4bytes scan off
2073 4bytes data off
2074 */
2075 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2076 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
2077 }
2078 }
2079
2080 if (start_code == APP2 && id == AV_RB32("ICC_") && len >= 10) {
2081 int id2;
2082 unsigned seqno;
2083 unsigned nummarkers;
2084
2085 id = bytestream2_get_be32u(&s->gB);
2086 id2 = bytestream2_get_be24u(&s->gB);
2087 len -= 7;
2088 if (id != AV_RB32("PROF") || id2 != AV_RB24("ILE")) {
2089 av_log(s->avctx, AV_LOG_WARNING, "Invalid ICC_PROFILE header in APP2\n");
2090 goto out;
2091 }
2092
2093 bytestream2_skipu(&s->gB, 1);
2094 seqno = bytestream2_get_byteu(&s->gB);
2095 len -= 2;
2096 if (seqno == 0) {
2097 av_log(s->avctx, AV_LOG_WARNING, "Invalid sequence number in APP2\n");
2098 goto out;
2099 }
2100
2101 nummarkers = bytestream2_get_byteu(&s->gB);
2102 len -= 1;
2103 if (nummarkers == 0) {
2104 av_log(s->avctx, AV_LOG_WARNING, "Invalid number of markers coded in APP2\n");
2105 goto out;
2106 } else if (s->iccnum != 0 && nummarkers != s->iccnum) {
2107 av_log(s->avctx, AV_LOG_WARNING, "Mismatch in coded number of ICC markers between markers\n");
2108 goto out;
2109 } else if (seqno > nummarkers) {
2110 av_log(s->avctx, AV_LOG_WARNING, "Mismatching sequence number and coded number of ICC markers\n");
2111 goto out;
2112 }
2113
2114 /* Allocate if this is the first APP2 we've seen. */
2115 if (s->iccnum == 0) {
2116 if (!FF_ALLOCZ_TYPED_ARRAY(s->iccentries, nummarkers)) {
2117 av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data arrays\n");
2118 return AVERROR(ENOMEM);
2119 }
2120 s->iccnum = nummarkers;
2121 }
2122
2123 if (s->iccentries[seqno - 1].data) {
2124 av_log(s->avctx, AV_LOG_WARNING, "Duplicate ICC sequence number\n");
2125 goto out;
2126 }
2127
2128 s->iccentries[seqno - 1].length = len;
2129 s->iccentries[seqno - 1].data = av_malloc(len);
2130 if (!s->iccentries[seqno - 1].data) {
2131 av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data buffer\n");
2132 return AVERROR(ENOMEM);
2133 }
2134
2135 bytestream2_get_bufferu(&s->gB, s->iccentries[seqno - 1].data, len);
2136 len = 0;
2137 s->iccread++;
2138
2139 if (s->iccread > s->iccnum)
2140 av_log(s->avctx, AV_LOG_WARNING, "Read more ICC markers than are supposed to be coded\n");
2141 }
2142
2143 if (start_code == APP2 && id == AV_RB32("urn:")) {
2144 bytestream2_seek(&s->gB, -4, SEEK_CUR); /* include `urn:` in the string */
2145 len += 4;
2146
2147 if (len >= sizeof(AV_ISO21496_IDENTIFIER) &&
2148 !memcmp(s->gB.buffer, AV_ISO21496_IDENTIFIER, sizeof(AV_ISO21496_IDENTIFIER)))
2149 {
2151 len -= sizeof(AV_ISO21496_IDENTIFIER);
2152
2153 /* Discard gain map version stubs from base layer */
2154 if (len <= sizeof(uint16_t[2]))
2155 goto out;
2156 else if (s->gain_map) {
2157 av_log(s->avctx, AV_LOG_WARNING, "Multiple ISO 21496 gain map headers\n");
2158 goto out;
2159 }
2160
2161 s->gain_map = av_gain_map_params_alloc(&s->gain_map_size);
2162 if (!s->gain_map)
2163 return AVERROR(ENOMEM);
2164
2165 ret = av_gain_map_params_from_iso21496(s->gain_map, s->gB.buffer, len);
2166 if (ret < 0) {
2167 av_log(s->avctx, AV_LOG_WARNING, "Failed to parse ISO 21496 gain map header\n");
2168 av_freep(&s->gain_map);
2169 s->gain_map_size = 0;
2170 goto out;
2171 }
2172 }
2173 }
2174
2175out:
2176 /* slow but needed for extreme adobe jpegs */
2177 if (len < 0)
2178 av_log(s->avctx, AV_LOG_ERROR,
2179 "mjpeg: error, decode_app parser read over the end\n");
2180 if (len > 0)
2181 bytestream2_skipu(&s->gB, len);
2182
2183 return 0;
2184}
2185
2187{
2188 int len;
2189 int ret = mjpeg_parse_len(s, &len, "com");
2190 if (ret < 0)
2191 return ret;
2192 if (!len)
2193 return 0;
2194
2195 int i;
2196 char *cbuf = av_malloc(len + 1);
2197 if (!cbuf)
2198 return AVERROR(ENOMEM);
2199
2200 for (i = 0; i < len; i++)
2201 cbuf[i] = bytestream2_get_byteu(&s->gB);
2202 if (cbuf[i - 1] == '\n')
2203 cbuf[i - 1] = 0;
2204 else
2205 cbuf[i] = 0;
2206
2207 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2208 av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
2209
2210 /* buggy avid, it puts EOI only at every 10th frame */
2211 if (!strncmp(cbuf, "AVID", 4)) {
2212 parse_avid(s, cbuf, len);
2213 } else if (!strcmp(cbuf, "CS=ITU601"))
2214 s->cs_itu601 = 1;
2215 else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
2216 (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
2217 s->flipped = 1;
2218 else if (!strcmp(cbuf, "MULTISCOPE II")) {
2219 s->avctx->sample_aspect_ratio = (AVRational) { 1, 2 };
2220 s->multiscope = 2;
2221 }
2222
2223 av_free(cbuf);
2224
2225 return 0;
2226}
2227
2228/* return the 8 bit start code value and update the search
2229 state. Return -1 if no start code found */
2230int ff_mjpeg_find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
2231{
2232 const uint8_t *buf_ptr;
2233 int val;
2234
2235 buf_ptr = *pbuf_ptr;
2236 while ((buf_ptr = memchr(buf_ptr, 0xff, buf_end - buf_ptr))) {
2237 buf_ptr++;
2238 while (buf_ptr < buf_end) {
2239 val = *buf_ptr++;
2240 if (val != 0xff) {
2241 if ((val >= SOF0) && (val <= COM))
2242 goto found;
2243 break;
2244 }
2245 }
2246 }
2247 buf_ptr = buf_end;
2248 val = -1;
2249found:
2250 ff_dlog(NULL, "find_marker skipped %td bytes\n",
2251 (buf_ptr - *pbuf_ptr) - (val < 0 ? 0 : 2));
2252 *pbuf_ptr = buf_ptr;
2253 return val;
2254}
2255
2257 const uint8_t **pbuf_ptr, size_t *pbuf_size)
2258{
2259 const uint8_t *buf_ptr = s->gB.buffer;
2260 const uint8_t *buf_end = buf_ptr + bytestream2_get_bytes_left(&s->gB);
2261
2262 /* Find size of image data buffer (including restart markers).
2263 * No unescaping is performed. */
2264 const uint8_t *ptr = buf_ptr;
2265 while ((ptr = memchr(ptr, 0xff, buf_end - ptr))) {
2266 ptr++;
2267 if (ptr < buf_end) {
2268 uint8_t x = *ptr++;
2269 /* Discard multiple optional 0xFF fill bytes. */
2270 while (x == 0xff && ptr < buf_end)
2271 x = *ptr++;
2272 if (x && (x < RST0 || x > RST7)) {
2273 /* Non-restart marker */
2274 ptr -= 2;
2275 goto found_hw;
2276 }
2277 }
2278 }
2279 ptr = buf_end;
2280found_hw:
2281 *pbuf_ptr = buf_ptr;
2282 *pbuf_size = ptr - buf_ptr;
2283 bytestream2_skipu(&s->gB, *pbuf_size);
2284}
2285
2287{
2288 const uint8_t *buf_ptr = s->gB.buffer;
2289 const uint8_t *buf_end = buf_ptr + bytestream2_get_bytes_left(&s->gB);
2290 const uint8_t *unescaped_buf_ptr;
2291 size_t unescaped_buf_size;
2292
2293 if (s->avctx->codec_id == AV_CODEC_ID_MEDIA100 ||
2294 s->avctx->codec_id == AV_CODEC_ID_MJPEGB ||
2295 s->avctx->codec_id == AV_CODEC_ID_THP) {
2296 /* The image data buffer is already unescaped. The only way to
2297 * find the size of the buffer is by fully decoding it. */
2298 unescaped_buf_ptr = buf_ptr;
2299 unescaped_buf_size = buf_end - buf_ptr;
2300 goto the_end;
2301 }
2302
2303 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - buf_ptr);
2304 if (!s->buffer)
2305 return AVERROR(ENOMEM);
2306
2307 /* unescape buffer of SOS, use special treatment for JPEG-LS */
2308 if (!s->ls) {
2309 const uint8_t *src = buf_ptr;
2310 const uint8_t *ptr = src;
2311 uint8_t *dst = s->buffer;
2312 PutByteContext pb;
2313
2314 bytestream2_init_writer(&pb, dst, buf_end - src);
2315
2316 while ((ptr = memchr(ptr, 0xff, buf_end - ptr))) {
2317 ptr++;
2318 if (ptr < buf_end) {
2319 /* Copy verbatim data. */
2320 ptrdiff_t length = (ptr - 1) - src;
2321 if (length > 0)
2322 bytestream2_put_bufferu(&pb, src, length);
2323
2324 uint8_t x = *ptr++;
2325 /* Discard multiple optional 0xFF fill bytes. */
2326 while (x == 0xff && ptr < buf_end)
2327 x = *ptr++;
2328
2329 src = ptr;
2330 if (x == 0) {
2331 /* Stuffed zero byte */
2332 bytestream2_put_byteu(&pb, 0xff);
2333 } else if (x >= RST0 && x <= RST7) {
2334 /* Restart marker */
2335 goto found;
2336 } else {
2337 /* Non-restart marker */
2338 ptr -= 2;
2339 goto found;
2340 }
2341 }
2342 }
2343 /* Copy remaining verbatim data. */
2344 ptr = buf_end;
2345 ptrdiff_t length = ptr - src;
2346 if (length > 0)
2347 bytestream2_put_bufferu(&pb, src, length);
2348
2349found:
2350 unescaped_buf_ptr = s->buffer;
2351 unescaped_buf_size = bytestream2_tell_p(&pb);
2352 memset(s->buffer + unescaped_buf_size, 0,
2354
2355 bytestream2_skipu(&s->gB, ptr - buf_ptr);
2356
2357 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
2358 (buf_end - buf_ptr) - (unescaped_buf_size));
2359 } else {
2360 const uint8_t *src = buf_ptr;
2361 const uint8_t *ptr = src;
2362 uint8_t *dst = s->buffer;
2363 PutBitContext pb;
2364
2365 init_put_bits(&pb, dst, buf_end - src);
2366
2367 while ((ptr = memchr(ptr, 0xff, buf_end - ptr))) {
2368 ptr++;
2369 if (ptr < buf_end) {
2370 /* Copy verbatim data. */
2371 ptrdiff_t length = (ptr - 1) - src;
2372 if (length > 0)
2373 ff_copy_bits(&pb, src, length * 8);
2374
2375 uint8_t x = *ptr++;
2376 /* Discard multiple optional 0xFF fill bytes. */
2377 while (x == 0xff && ptr < buf_end)
2378 x = *ptr++;
2379
2380 src = ptr;
2381 if (!(x & 0x80)) {
2382 /* Stuffed zero bit */
2383 put_bits(&pb, 15, 0x7f80 | x);
2384 } else if (x >= RST0 && x <= RST7) {
2385 /* Restart marker */
2386 goto found_ls;
2387 } else {
2388 /* Non-restart marker */
2389 ptr -= 2;
2390 goto found_ls;
2391 }
2392 }
2393 }
2394 /* Copy remaining verbatim data. */
2395 ptr = buf_end;
2396 ptrdiff_t length = ptr - src;
2397 if (length > 0)
2398 ff_copy_bits(&pb, src, length * 8);
2399
2400found_ls:
2401 flush_put_bits(&pb);
2402
2403 unescaped_buf_ptr = dst;
2404 unescaped_buf_size = put_bytes_output(&pb);
2405 memset(s->buffer + unescaped_buf_size, 0,
2407
2408 bytestream2_skipu(&s->gB, ptr - buf_ptr);
2409 }
2410
2411the_end:
2412 return init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
2413}
2414
2416{
2417 int i;
2418
2419 if (s->iccentries) {
2420 for (i = 0; i < s->iccnum; i++)
2421 av_freep(&s->iccentries[i].data);
2422 av_freep(&s->iccentries);
2423 }
2424
2425 s->iccread = 0;
2426 s->iccnum = 0;
2427}
2428
2430 int *got_frame, const AVPacket *avpkt,
2431 const uint8_t *buf, const int buf_size)
2432{
2433 MJpegDecodeContext *s = avctx->priv_data;
2434 const uint8_t *buf_end, *buf_ptr;
2435 int hshift, vshift;
2436 int start_code;
2437 int index;
2438 int ret = 0;
2439 int is16bit;
2440
2441 s->force_pal8 = 0;
2442 s->total_ls_decoded_height = 0;
2443
2444 s->buf_size = buf_size;
2445 s->raw_image_buffer = NULL;
2446 s->raw_image_buffer_size = 0;
2447
2448 av_exif_free(&s->exif_metadata);
2449 av_freep(&s->stereo3d);
2450 av_freep(&s->gain_map);
2451 s->adobe_transform = -1;
2452
2453 if (s->iccnum != 0)
2455
2456redo_for_pal8:
2457 buf_ptr = buf;
2458 buf_end = buf + buf_size;
2459 while (buf_ptr < buf_end) {
2460 /* find start next marker */
2461 start_code = ff_mjpeg_find_marker(&buf_ptr, buf_end);
2462 /* EOF */
2463 if (start_code < 0)
2464 break;
2465
2466 ptrdiff_t bytes_left = buf_end - buf_ptr;
2467 if (bytes_left > INT_MAX / 8) {
2468 av_log(avctx, AV_LOG_ERROR,
2469 "MJPEG packet 0x%x too big (%td/%d), corrupt data?\n",
2470 start_code, bytes_left, buf_size);
2471 return AVERROR_INVALIDDATA;
2472 }
2473 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
2474 start_code, buf_end - buf_ptr);
2475
2476 bytestream2_init(&s->gB, buf_ptr, bytes_left);
2477
2478 if (avctx->debug & FF_DEBUG_STARTCODE)
2479 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
2480
2481 /* process markers */
2482 if (start_code >= RST0 && start_code <= RST7) {
2483 av_log(avctx, AV_LOG_DEBUG,
2484 "restart marker: %d\n", start_code & 0x0f);
2485 /* APP fields */
2486 } else if (start_code >= APP0 && start_code <= APP15) {
2487 if ((ret = mjpeg_decode_app(s, start_code)) < 0)
2488 av_log(avctx, AV_LOG_ERROR, "unable to decode APP fields: %s\n",
2489 av_err2str(ret));
2490 /* Comment */
2491 } else if (start_code == COM) {
2493 if (ret < 0)
2494 return ret;
2495 } else if (start_code == DQT) {
2497 if (ret < 0)
2498 return ret;
2499 }
2500
2501 ret = -1;
2502
2503 if (!CONFIG_JPEGLS_DECODER &&
2504 (start_code == SOF55 || start_code == LSE)) {
2505 av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
2506 return AVERROR(ENOSYS);
2507 }
2508
2509 if (avctx->skip_frame == AVDISCARD_ALL) {
2510 switch (start_code) {
2511 case SOF0:
2512 case SOF1:
2513 case SOF2:
2514 case SOF3:
2515 case SOF55:
2516 break;
2517 default:
2518 goto skip;
2519 }
2520 }
2521
2522 switch (start_code) {
2523 case SOI:
2524 s->restart_interval = 0;
2525 s->raw_image_buffer = buf_ptr;
2526 s->raw_image_buffer_size = buf_end - buf_ptr;
2527 /* nothing to do on SOI */
2528 break;
2529 case DHT:
2530 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
2531 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
2532 goto fail;
2533 }
2534 break;
2535 case SOF0:
2536 case SOF1:
2537 if (start_code == SOF0)
2539 else
2541 s->lossless = 0;
2542 s->ls = 0;
2543 s->progressive = 0;
2544 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2545 goto fail;
2546 break;
2547 case SOF2:
2549 s->lossless = 0;
2550 s->ls = 0;
2551 s->progressive = 1;
2552 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2553 goto fail;
2554 break;
2555 case SOF3:
2557 s->lossless = 1;
2558 s->ls = 0;
2559 s->progressive = 0;
2560 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2561 goto fail;
2562 break;
2563 case SOF55:
2565 s->lossless = 1;
2566 s->ls = 1;
2567 s->progressive = 0;
2568 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2569 goto fail;
2570 break;
2571 case LSE:
2572 if (!CONFIG_JPEGLS_DECODER ||
2573 (ret = ff_jpegls_decode_lse(s)) < 0)
2574 goto fail;
2575 if (ret == 1)
2576 goto redo_for_pal8;
2577 break;
2578 case EOI:
2579eoi_parser:
2580 if (!avctx->hwaccel &&
2581 s->progressive && s->cur_scan && s->got_picture)
2583 s->cur_scan = 0;
2584 if (!s->got_picture) {
2585 av_log(avctx, AV_LOG_WARNING,
2586 "Found EOI before any SOF, ignoring\n");
2587 break;
2588 }
2589 if (s->interlaced) {
2590 s->bottom_field ^= 1;
2591 /* if not bottom field, do not output image yet */
2592 if (s->bottom_field == !s->interlace_polarity)
2593 break;
2594 }
2595 if (avctx->hwaccel) {
2596 ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
2597 if (ret < 0)
2598 return ret;
2599
2600 av_refstruct_unref(&s->hwaccel_picture_private);
2601 }
2602 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2603 return ret;
2604 if (s->lossless)
2605 frame->flags |= AV_FRAME_FLAG_LOSSLESS;
2606 *got_frame = 1;
2607 s->got_picture = 0;
2608
2609 if (!s->lossless && avctx->debug & FF_DEBUG_QP) {
2610 int qp = FFMAX3(s->qscale[0],
2611 s->qscale[1],
2612 s->qscale[2]);
2613
2614 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2615 }
2616
2617 goto the_end;
2618 case SOS:
2619 s->cur_scan++;
2620
2621 if ((ret = ff_mjpeg_decode_sos(s)) < 0 &&
2622 (avctx->err_recognition & AV_EF_EXPLODE))
2623 goto fail;
2624 break;
2625 case DRI:
2626 if ((ret = mjpeg_decode_dri(s)) < 0)
2627 return ret;
2628 break;
2629 case SOF5:
2630 case SOF6:
2631 case SOF7:
2632 case SOF9:
2633 case SOF10:
2634 case SOF11:
2635 case SOF13:
2636 case SOF14:
2637 case SOF15:
2638 case JPG:
2639 av_log(avctx, AV_LOG_ERROR,
2640 "mjpeg: unsupported coding type (%x)\n", start_code);
2641 break;
2642 }
2643
2644 if (avctx->skip_frame == AVDISCARD_ALL) {
2645 switch (start_code) {
2646 case SOF0:
2647 case SOF1:
2648 case SOF2:
2649 case SOF3:
2650 case SOF55:
2651 s->got_picture = 0;
2652 goto the_end_no_picture;
2653 }
2654 }
2655
2656skip:
2657 /* eof process start code */
2658 buf_ptr += bytestream2_tell(&s->gB);
2659 av_log(avctx, AV_LOG_DEBUG,
2660 "marker parser used %d bytes\n",
2661 bytestream2_tell(&s->gB));
2662 }
2663 if (s->got_picture && s->cur_scan) {
2664 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2665 goto eoi_parser;
2666 }
2667 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2668 return AVERROR_INVALIDDATA;
2669fail:
2670 s->got_picture = 0;
2671 return ret;
2672the_end:
2673
2674 is16bit = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].step > 1;
2675
2676 if (AV_RB32(s->upscale_h)) {
2677 int p;
2679 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2680 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2681 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2682 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2683 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2684 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2685 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2686 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2687 avctx->pix_fmt == AV_PIX_FMT_YUV420P16||
2688 avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2689 avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2690 avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2691 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2692 );
2693 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &hshift, &vshift);
2694 if (ret)
2695 return ret;
2696
2697 av_assert0(s->nb_components == av_pix_fmt_count_planes(s->picture_ptr->format));
2698 for (p = 0; p < s->nb_components; p++) {
2699 uint8_t *line = s->picture_ptr->data[p];
2700 int w = s->width;
2701 int h = s->height;
2702 if (!s->upscale_h[p])
2703 continue;
2704 if (p == 1 || p == 2) {
2705 w = AV_CEIL_RSHIFT(w, hshift);
2706 h = AV_CEIL_RSHIFT(h, vshift);
2707 }
2708 if (s->upscale_v[p] == 1)
2709 h = (h + 1) >> 1;
2710 av_assert0(w > 0);
2711 for (int i = 0; i < h; i++) {
2712 if (s->upscale_h[p] == 1) {
2713 if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2714 else line[w - 1] = line[(w - 1) / 2];
2715 for (index = w - 2; index > 0; index--) {
2716 if (is16bit)
2717 ((uint16_t*)line)[index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2718 else
2719 line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2720 }
2721 } else if (s->upscale_h[p] == 2) {
2722 if (is16bit) {
2723 ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 3];
2724 if (w > 1)
2725 ((uint16_t*)line)[w - 2] = ((uint16_t*)line)[w - 1];
2726 } else {
2727 line[w - 1] = line[(w - 1) / 3];
2728 if (w > 1)
2729 line[w - 2] = line[w - 1];
2730 }
2731 for (index = w - 3; index > 0; index--) {
2732 line[index] = (line[index / 3] + line[(index + 1) / 3] + line[(index + 2) / 3] + 1) / 3;
2733 }
2734 } else if (s->upscale_h[p] == 4) {
2735 if (is16bit) {
2736 uint16_t *line16 = (uint16_t *) line;
2737 line16[w - 1] = line16[(w - 1) >> 2];
2738 if (w > 1)
2739 line16[w - 2] = (line16[(w - 1) >> 2] * 3 + line16[(w - 2) >> 2]) >> 2;
2740 if (w > 2)
2741 line16[w - 3] = (line16[(w - 1) >> 2] + line16[(w - 2) >> 2]) >> 1;
2742 } else {
2743 line[w - 1] = line[(w - 1) >> 2];
2744 if (w > 1)
2745 line[w - 2] = (line[(w - 1) >> 2] * 3 + line[(w - 2) >> 2]) >> 2;
2746 if (w > 2)
2747 line[w - 3] = (line[(w - 1) >> 2] + line[(w - 2) >> 2]) >> 1;
2748 }
2749 for (index = w - 4; index > 0; index--)
2750 line[index] = (line[(index + 3) >> 2] + line[(index + 2) >> 2]
2751 + line[(index + 1) >> 2] + line[index >> 2]) >> 2;
2752 }
2753 line += s->linesize[p];
2754 }
2755 }
2756 }
2757 if (AV_RB32(s->upscale_v)) {
2758 int p;
2760 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2761 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2762 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2763 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2764 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2765 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2766 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2767 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2768 avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2769 avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2770 avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2771 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2772 );
2773 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &hshift, &vshift);
2774 if (ret)
2775 return ret;
2776
2777 av_assert0(s->nb_components == av_pix_fmt_count_planes(s->picture_ptr->format));
2778 for (p = 0; p < s->nb_components; p++) {
2779 uint8_t *dst;
2780 int w = s->width;
2781 int h = s->height;
2782 if (!s->upscale_v[p])
2783 continue;
2784 if (p == 1 || p == 2) {
2785 w = AV_CEIL_RSHIFT(w, hshift);
2786 h = AV_CEIL_RSHIFT(h, vshift);
2787 }
2788 dst = &((uint8_t *)s->picture_ptr->data[p])[(h - 1) * s->linesize[p]];
2789 for (int i = h - 1; i; i--) {
2790 uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2791 uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2792 if (s->upscale_v[p] != 2 && (src1 == src2 || i == h - 1)) {
2793 memcpy(dst, src1, w);
2794 } else {
2795 for (index = 0; index < w; index++)
2796 dst[index] = (src1[index] + src2[index]) >> 1;
2797 }
2798 dst -= s->linesize[p];
2799 }
2800 }
2801 }
2802 if (avctx->codec_id != AV_CODEC_ID_SMVJPEG &&
2803 (avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
2804 avctx->codec_tag == MKTAG('A', 'V', 'D', 'J')) &&
2805 avctx->coded_height > s->orig_height) {
2806 frame->height = AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres);
2807 frame->crop_top = frame->height - avctx->height;
2808 }
2809
2810 if (s->flipped && !s->rgb && !s->bayer) {
2811 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &hshift, &vshift);
2812 if (ret)
2813 return ret;
2814
2815 av_assert0(s->nb_components == av_pix_fmt_count_planes(frame->format));
2816 for (index = 0; index < s->nb_components; index++) {
2817 int h = frame->height;
2818 if (index && index < 3)
2819 h = AV_CEIL_RSHIFT(h, vshift);
2820 if (frame->data[index]) {
2821 frame->data[index] += (h - 1) * frame->linesize[index];
2822 frame->linesize[index] *= -1;
2823 }
2824 }
2825 }
2826
2827 if (avctx->pix_fmt == AV_PIX_FMT_GBRP) {
2828 av_assert0(s->nb_components == 3);
2829 FFSWAP(uint8_t *, frame->data[0], frame->data[2]);
2830 FFSWAP(uint8_t *, frame->data[0], frame->data[1]);
2831 FFSWAP(int, frame->linesize[0], frame->linesize[2]);
2832 FFSWAP(int, frame->linesize[0], frame->linesize[1]);
2833 }
2834
2835 if (s->adobe_transform == 0 && avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2836 int w = s->picture_ptr->width;
2837 int h = s->picture_ptr->height;
2838 av_assert0(s->nb_components == 4);
2839 for (int i = 0; i < h; i++) {
2840 int j;
2841 uint8_t *dst[4];
2842 for (index = 0; index < 4; index++) {
2843 dst[index] = s->picture_ptr->data[index]
2844 + s->picture_ptr->linesize[index]*i;
2845 }
2846 for (j = 0; j < w; j++) {
2847 int k = dst[3][j];
2848 int r = dst[0][j] * k;
2849 int g = dst[1][j] * k;
2850 int b = dst[2][j] * k;
2851 dst[0][j] = g * 257 >> 16;
2852 dst[1][j] = b * 257 >> 16;
2853 dst[2][j] = r * 257 >> 16;
2854 }
2855 memset(dst[3], 255, w);
2856 }
2857 }
2858 if (s->adobe_transform == 2 && avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2859 int w = s->picture_ptr->width;
2860 int h = s->picture_ptr->height;
2861 av_assert0(s->nb_components == 4);
2862 for (int i = 0; i < h; i++) {
2863 int j;
2864 uint8_t *dst[4];
2865 for (index = 0; index < 4; index++) {
2866 dst[index] = s->picture_ptr->data[index]
2867 + s->picture_ptr->linesize[index]*i;
2868 }
2869 for (j = 0; j < w; j++) {
2870 int k = dst[3][j];
2871 int r = (255 - dst[0][j]) * k;
2872 int g = (128 - dst[1][j]) * k;
2873 int b = (128 - dst[2][j]) * k;
2874 dst[0][j] = r * 257 >> 16;
2875 dst[1][j] = (g * 257 >> 16) + 128;
2876 dst[2][j] = (b * 257 >> 16) + 128;
2877 }
2878 memset(dst[3], 255, w);
2879 }
2880 }
2881
2882 if (s->stereo3d) {
2884 if (stereo) {
2885 stereo->type = s->stereo3d->type;
2886 stereo->flags = s->stereo3d->flags;
2887 }
2888 av_freep(&s->stereo3d);
2889 }
2890
2891 if (s->gain_map) {
2892 AVFrameSideData *sd;
2893 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_GAIN_MAP_PARAMS, s->gain_map_size, &sd);
2894 if (ret < 0)
2895 return ret;
2896
2897 if (sd)
2898 memcpy(sd->data, s->gain_map, s->gain_map_size);
2899 av_freep(&s->gain_map);
2900 s->gain_map_size = 0;
2901 }
2902
2903 if (s->iccnum != 0 && s->iccnum == s->iccread) {
2904 AVFrameSideData *sd;
2905 size_t offset = 0;
2906 int total_size = 0;
2907
2908 /* Sum size of all parts. */
2909 for (int i = 0; i < s->iccnum; i++)
2910 total_size += s->iccentries[i].length;
2911
2912 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_ICC_PROFILE, total_size, &sd);
2913 if (ret < 0) {
2914 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame side data\n");
2915 return ret;
2916 }
2917
2918 if (sd) {
2919 /* Reassemble the parts, which are now in-order. */
2920 for (int i = 0; i < s->iccnum; i++) {
2921 memcpy(sd->data + offset, s->iccentries[i].data, s->iccentries[i].length);
2922 offset += s->iccentries[i].length;
2923 }
2924 }
2925 }
2926
2927 if (s->exif_metadata.entries) {
2928 ret = ff_decode_exif_attach_ifd(avctx, frame, &s->exif_metadata);
2929 av_exif_free(&s->exif_metadata);
2930 if (ret < 0)
2931 av_log(avctx, AV_LOG_WARNING, "couldn't attach EXIF metadata\n");
2932 }
2933
2934the_end_no_picture:
2935 av_log(avctx, AV_LOG_DEBUG, "decode frame unused %td bytes\n",
2936 buf_end - buf_ptr);
2937 return buf_ptr - buf;
2938}
2939
2941 AVPacket *avpkt)
2942{
2943 return ff_mjpeg_decode_frame_from_buf(avctx, frame, got_frame,
2944 avpkt, avpkt->data, avpkt->size);
2945}
2946
2947
2948/* mxpeg may call the following function (with a blank MJpegDecodeContext)
2949 * even without having called ff_mjpeg_decode_init(). */
2951{
2952 MJpegDecodeContext *s = avctx->priv_data;
2953 int i, j;
2954
2955 if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_num) {
2956 av_log(avctx, AV_LOG_INFO, "Single field\n");
2957 }
2958
2959 av_refstruct_unref(&s->hwaccel_picture_private);
2960 av_frame_free(&s->picture);
2961 s->picture_ptr = NULL;
2962
2963 av_frame_free(&s->smv_frame);
2964
2965 av_freep(&s->buffer);
2966 av_freep(&s->stereo3d);
2967 av_freep(&s->ljpeg_buffer);
2968 s->ljpeg_buffer_size = 0;
2969 av_freep(&s->gain_map);
2970 s->gain_map_size = 0;
2971
2972 for (i = 0; i < 3; i++) {
2973 for (j = 0; j < 4; j++)
2974 ff_vlc_free(&s->vlcs[i][j]);
2975 }
2976 for (i = 0; i < MAX_COMPONENTS; i++) {
2977 av_freep(&s->blocks[i]);
2978 av_freep(&s->last_nnz[i]);
2979 }
2980 av_exif_free(&s->exif_metadata);
2981
2983
2984 av_freep(&s->jls_state);
2985
2986 return 0;
2987}
2988
2990{
2991 MJpegDecodeContext *s = avctx->priv_data;
2992 s->got_picture = 0;
2993
2994 av_refstruct_unref(&s->hwaccel_picture_private);
2995
2996 s->smv_next_frame = 0;
2997 av_frame_unref(s->smv_frame);
2998}
2999
3000#if CONFIG_MJPEG_DECODER
3001#if FF_API_MJPEG_EXTERN_HUFF
3002#define OFFSET(x) offsetof(MJpegDecodeContext, x)
3003#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
3004static const AVOption options[] = {
3005 { "extern_huff", "Use external huffman table.",
3006 OFFSET(extern_huff), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD | AV_OPT_FLAG_DEPRECATED },
3007 { NULL },
3008};
3009#endif
3010
3011static const AVClass mjpegdec_class = {
3012 .class_name = "MJPEG decoder",
3013 .item_name = av_default_item_name,
3014#if FF_API_MJPEG_EXTERN_HUFF
3015 .option = options,
3016#endif
3017 .version = LIBAVUTIL_VERSION_INT,
3018};
3019
3020const FFCodec ff_mjpeg_decoder = {
3021 .p.name = "mjpeg",
3022 CODEC_LONG_NAME("MJPEG (Motion JPEG)"),
3023 .p.type = AVMEDIA_TYPE_VIDEO,
3024 .p.id = AV_CODEC_ID_MJPEG,
3025 .priv_data_size = sizeof(MJpegDecodeContext),
3029 .flush = decode_flush,
3030 .p.capabilities = AV_CODEC_CAP_DR1,
3031 .p.max_lowres = 3,
3032 .p.priv_class = &mjpegdec_class,
3034 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3037 .hw_configs = (const AVCodecHWConfigInternal *const []) {
3038#if CONFIG_MJPEG_NVDEC_HWACCEL
3039 HWACCEL_NVDEC(mjpeg),
3040#endif
3041#if CONFIG_MJPEG_VAAPI_HWACCEL
3042 HWACCEL_VAAPI(mjpeg),
3043#endif
3044 NULL
3045 },
3046};
3047#endif
3048#if CONFIG_THP_DECODER
3049const FFCodec ff_thp_decoder = {
3050 .p.name = "thp",
3051 CODEC_LONG_NAME("Nintendo Gamecube THP video"),
3052 .p.type = AVMEDIA_TYPE_VIDEO,
3053 .p.id = AV_CODEC_ID_THP,
3054 .priv_data_size = sizeof(MJpegDecodeContext),
3058 .flush = decode_flush,
3059 .p.capabilities = AV_CODEC_CAP_DR1,
3060 .p.max_lowres = 3,
3061 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
3062};
3063#endif
3064
3065#if CONFIG_SMVJPEG_DECODER
3066// SMV JPEG just stacks several output frames into one JPEG picture
3067// we handle that by setting up the cropping parameters appropriately
3068static void smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
3069{
3070 MJpegDecodeContext *s = avctx->priv_data;
3071
3072 av_assert0((s->smv_next_frame + 1) * avctx->height <= avctx->coded_height);
3073
3074 frame->width = avctx->coded_width;
3075 frame->height = avctx->coded_height;
3076 frame->crop_top = FFMIN(s->smv_next_frame * avctx->height, frame->height);
3077 frame->crop_bottom = frame->height - (s->smv_next_frame + 1) * avctx->height;
3078
3079 if (s->smv_frame->pts != AV_NOPTS_VALUE)
3080 s->smv_frame->pts += s->smv_frame->duration;
3081 s->smv_next_frame = (s->smv_next_frame + 1) % s->smv_frames_per_jpeg;
3082
3083 if (s->smv_next_frame == 0)
3084 av_frame_unref(s->smv_frame);
3085}
3086
3087static int smvjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
3088{
3089 MJpegDecodeContext *s = avctx->priv_data;
3090 AVPacket *const pkt = avctx->internal->in_pkt;
3091 int got_frame = 0;
3092 int ret;
3093
3094 if (s->smv_next_frame > 0)
3095 goto return_frame;
3096
3097 ret = ff_decode_get_packet(avctx, pkt);
3098 if (ret < 0)
3099 return ret;
3100
3101 av_frame_unref(s->smv_frame);
3102
3103 ret = ff_mjpeg_decode_frame(avctx, s->smv_frame, &got_frame, pkt);
3104 s->smv_frame->pkt_dts = pkt->dts;
3106 if (ret < 0)
3107 return ret;
3108
3109 if (!got_frame)
3110 return AVERROR(EAGAIN);
3111
3112 // packet duration covers all the frames in the packet
3113 s->smv_frame->duration /= s->smv_frames_per_jpeg;
3114
3116 av_assert0(s->smv_frame->buf[0]);
3117 ret = av_frame_ref(frame, s->smv_frame);
3118 if (ret < 0)
3119 return ret;
3120
3121 smv_process_frame(avctx, frame);
3122 return 0;
3123}
3124
3126 .p.name = "smvjpeg",
3127 CODEC_LONG_NAME("SMV JPEG"),
3128 .p.type = AVMEDIA_TYPE_VIDEO,
3129 .p.id = AV_CODEC_ID_SMVJPEG,
3130 .priv_data_size = sizeof(MJpegDecodeContext),
3133 FF_CODEC_RECEIVE_FRAME_CB(smvjpeg_receive_frame),
3134 .flush = decode_flush,
3135 .p.capabilities = AV_CODEC_CAP_DR1,
3136 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
3138};
3139#endif
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static double val(void *priv, double ch)
Definition aeval.c:77
static av_cold void decode_flush(AVCodecContext *avctx)
Definition agm.c:1253
const FFCodec ff_mjpeg_decoder
const FFCodec ff_thp_decoder
const FFCodec ff_smvjpeg_decoder
#define VD
Definition amfdec.c:807
static FILE * out
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_DEBUG_STARTCODE
Definition avcodec.h:1406
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1399
#define FF_DEBUG_QP
Definition avcodec.h:1403
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition bitstream.c:49
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
static av_always_inline int bytestream2_tell_p(const PutByteContext *p)
Definition bytestream.h:197
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition bytestream.h:301
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define is(width, name, range_min, range_max, subs,...)
Definition cbs_h264.c:78
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static void copy_mb(CinepakEncContext *s, uint8_t *a_data[4], int a_linesize[4], uint8_t *b_data[4], int b_linesize[4])
Definition cinepakenc.c:506
#define FF_CODEC_RECEIVE_FRAME_CB(func)
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters if all frames are skipped due to the skip_frame setting...
#define FF_CODEC_CAP_ICC_PROFILES
Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip_int16
Definition common.h:115
static void copy_block2(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
Definition copy_block.h:27
static void copy_block4(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
Definition copy_block.h:37
#define NULL
Definition coverity.c:32
#define EOI
End Of Image marker.
Definition cpia.c:43
static int16_t block[64]
Definition dct.c:125
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1814
int ff_decode_exif_attach_ifd(AVCodecContext *avctx, AVFrame *frame, const AVExifMetadata *ifd)
Definition decode.c:2544
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition decode.c:255
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition decode.c:1248
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
Allocate a hwaccel frame private data if the provided avctx uses a hwaccel method that needs it.
Definition decode.c:2383
int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame, enum AVFrameSideDataType type, size_t size, AVFrameSideData **psd)
Wrapper around av_frame_new_side_data, which rejects side data overridden by the demuxer.
Definition decode.c:2231
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
#define AV_PROFILE_MJPEG_JPEG_LS
Definition defs.h:191
#define AV_PROFILE_MJPEG_HUFFMAN_EXTENDED_SEQUENTIAL_DCT
Definition defs.h:188
#define AV_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT
Definition defs.h:187
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:231
@ AV_FIELD_UNKNOWN
Definition defs.h:228
#define AV_PROFILE_MJPEG_HUFFMAN_LOSSLESS
Definition defs.h:190
#define AV_PROFILE_MJPEG_HUFFMAN_PROGRESSIVE_DCT
Definition defs.h:189
static AVPacket * pkt
static AVFrame * frame
enum AVCodecID id
Definition dts2pts.c:612
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int av_exif_parse_buffer(void *logctx, const uint8_t *buf, size_t size, AVExifMetadata *ifd, enum AVExifHeaderMode header_mode)
Decodes the EXIF data provided in the buffer and writes it into the struct *ifd.
Definition exif.c:902
void av_exif_free(AVExifMetadata *ifd)
Frees all resources associated with the given EXIF metadata struct.
Definition exif.c:679
EXIF metadata parser.
@ AV_EXIF_TIFF_HEADER
The TIFF header starts with 0x49492a00, or 0x4d4d002a.
Definition exif.h:62
EXIF metadata parser - internal functions.
static void predictor(uint8_t *src, ptrdiff_t size)
Definition exrenc.c:170
static const uint8_t bits[8]
Definition fastaudio.c:100
#define OFFSET(x)
static const char * hwaccel
Definition ffplay.c:406
int ret
Definition ffplay.c:391
static int lowres
Definition ffplay.c:362
static int decode_slice(AVCodecContext *c, void *arg)
Definition ffv1dec.c:451
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition get_bits.h:573
#define GET_CACHE(name, gb)
Definition get_bits.h:251
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
#define CLOSE_READER(name, gb)
Definition get_bits.h:189
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
#define SHOW_UBITS(name, gb, num)
Definition get_bits.h:247
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
#define OPEN_READER(name, gb)
Definition get_bits.h:182
#define UPDATE_CACHE(name, gb)
Definition get_bits.h:213
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
#define LAST_SKIP_BITS(name, gb, num)
Definition get_bits.h:235
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
Definition get_bits.h:294
#define fail
Definition test.h:479
#define AV_OPT_FLAG_DEPRECATED
Set if option is deprecated, users should refer to AVOption.help text for more information.
Definition opt.h:385
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_MXPEG
Definition codec_id.h:196
@ AV_CODEC_ID_MJPEGB
Definition codec_id.h:58
@ AV_CODEC_ID_SMVJPEG
Definition codec_id.h:259
@ AV_CODEC_ID_MEDIA100
Definition codec_id.h:313
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
@ AV_CODEC_ID_THP
Definition codec_id.h:150
@ AV_CODEC_ID_AMV
Definition codec_id.h:157
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:248
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition utils.c:53
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:702
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition frame.h:707
#define AV_FRAME_FLAG_LOSSLESS
A decoder can use this flag to mark frames which were originally encoded losslessly.
Definition frame.h:715
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:694
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:500
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:282
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition frame.h:144
@ AV_FRAME_DATA_GAIN_MAP_PARAMS
Parameters describing how to combine this gain map with its base image to form an alternate rendition...
Definition frame.h:314
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition log.h:204
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:661
#define av_fourcc2str(fourcc)
Definition avutil.h:323
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
AVStereo3D * av_stereo3d_alloc(void)
Allocate an AVStereo3D structure and set its fields to default values.
Definition stereo3d.c:34
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition stereo3d.h:194
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition stereo3d.c:54
@ AV_STEREO3D_LINES
Views are packed per line, as if interlaced.
Definition stereo3d.h:126
@ AV_STEREO3D_2D
Video is not stereoscopic (and metadata has to be there).
Definition stereo3d.h:52
@ AV_STEREO3D_TOPBOTTOM
Views are on top of each other.
Definition stereo3d.h:76
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition stereo3d.h:64
int index
Definition gxfenc.c:90
const pixel * src2
#define FF_HW_SIMPLE_CALL(avctx, function)
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define FF_HW_CALL(avctx, function,...)
#define HWACCEL_NVDEC(codec)
Definition hwconfig.h:68
#define HWACCEL_VAAPI(codec)
Definition hwconfig.h:72
misc image utilities
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_RB24(x)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
Decode LSE block with initialization parameters.
Definition jpeglsdec.c:52
int ff_jpegls_decode_picture(MJpegDecodeContext *s)
Definition jpeglsdec.c:357
JPEG-LS decoder.
enum MPFType type
Definition jpegmpfenc.c:55
FF_VISIBILITY_PUSH_HIDDEN const uint8_t ff_mjpeg_bits_dc_luminance[]
Definition jpegtabs.h:32
const uint8_t ff_mjpeg_val_ac_chrominance[]
Definition jpegtabs.h:69
const uint8_t ff_mjpeg_bits_ac_luminance[]
Definition jpegtabs.h:40
const uint8_t ff_mjpeg_bits_dc_chrominance[]
Definition jpegtabs.h:37
const uint8_t ff_mjpeg_bits_ac_chrominance[]
Definition jpegtabs.h:66
const uint8_t ff_mjpeg_val_dc[]
Definition jpegtabs.h:34
const uint8_t ff_mjpeg_val_ac_luminance[]
Definition jpegtabs.h:42
unsigned offset
Definition libaomenc.c:812
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition blockdsp.c:58
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
Serialization of AVGainMapParams into the metadata blobs that image formats carry it in,...
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition idctdsp.c:228
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition idctdsp.c:30
common internal api header.
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
AVGainMapParams * av_gain_map_params_alloc(size_t *size)
Allocate an AVGainMapParams structure and initialize it to default values.
Definition gain_map.c:224
int av_gain_map_params_from_iso21496(AVGainMapParams *restrict p, const uint8_t *data, size_t size)
Definition gain_map.c:111
#define AV_ISO21496_IDENTIFIER
Definition gain_map.h:28
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:98
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition internal.h:81
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMAX3(a, b, c)
Definition macros.h:48
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
#define NEG_USR32(a, s)
Definition mathops.h:180
uint64_t layout
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
MJPEG encoder and decoder.
#define PREDICT(ret, topleft, top, left, predictor)
Definition mjpeg.h:118
@ RST0
Definition mjpeg.h:61
@ APP15
Definition mjpeg.h:94
@ JPG
Definition mjpeg.h:47
@ DQT
Definition mjpeg.h:73
@ SOF5
Definition mjpeg.h:44
@ SOF2
Definition mjpeg.h:41
@ DRI
Definition mjpeg.h:75
@ SOF9
Definition mjpeg.h:48
@ DHT
Definition mjpeg.h:56
@ SOF13
Definition mjpeg.h:52
@ APP1
Definition mjpeg.h:80
@ SOF1
Definition mjpeg.h:40
@ SOF6
Definition mjpeg.h:45
@ APP2
Definition mjpeg.h:81
@ SOS
Definition mjpeg.h:72
@ SOF7
Definition mjpeg.h:46
@ APP0
Definition mjpeg.h:79
@ SOF11
Definition mjpeg.h:50
@ SOI
Definition mjpeg.h:70
@ SOF14
Definition mjpeg.h:53
@ SOF10
Definition mjpeg.h:49
@ LSE
JPEG-LS extension parameters.
Definition mjpeg.h:104
@ RST7
Definition mjpeg.h:68
@ APP3
Definition mjpeg.h:82
@ COM
Definition mjpeg.h:111
@ SOF55
JPEG-LS.
Definition mjpeg.h:103
@ SOF3
Definition mjpeg.h:42
@ SOF0
Definition mjpeg.h:39
@ SOF15
Definition mjpeg.h:54
static av_cold void decode_flush(AVCodecContext *avctx)
Definition mjpegdec.c:2989
static void mjpeg_find_raw_scan_data(MJpegDecodeContext *s, const uint8_t **pbuf_ptr, size_t *pbuf_size)
Definition mjpegdec.c:2256
static int mjpeg_decode_app(MJpegDecodeContext *s, int start_code)
Definition mjpegdec.c:1849
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s)
Definition mjpegdec.c:1252
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s)
Definition mjpegdec.c:1097
static void reset_icc_profile(MJpegDecodeContext *s)
Definition mjpegdec.c:2415
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition mjpegdec.c:125
int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
Definition mjpegdec.c:1683
static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
Definition mjpegdec.c:106
static int mjpeg_decode_com(MJpegDecodeContext *s)
Definition mjpegdec.c:2186
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition mjpegdec.c:209
int ff_mjpeg_unescape_sos(MJpegDecodeContext *s)
Definition mjpegdec.c:2286
static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s, uint8_t *dst, const uint8_t *src, int linesize, int lowres)
Definition mjpegdec.c:1413
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition mjpegdec.c:2950
int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx, AVFrame *frame, int *got_frame, const AVPacket *avpkt, const uint8_t *buf, const int buf_size)
Definition mjpegdec.c:2429
#define ZERO_RUN
Definition mjpegdec.c:1013
static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
Definition mjpegdec.c:1429
static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block, uint8_t *last_nnz, int ac_index, uint16_t *quant_matrix, int Ss, int Se, int Al, int *EOBRUN)
Definition mjpegdec.c:933
static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block, uint8_t *last_nnz, int ac_index, uint16_t *quant_matrix, int Ss, int Se, int Al, int *EOBRUN)
Definition mjpegdec.c:1031
int ff_mjpeg_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition mjpegdec.c:2940
static int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index, int *val)
Definition mjpegdec.c:852
static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s)
Definition mjpegdec.c:1593
#define REFINE_BIT(j)
Definition mjpegdec.c:1005
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition mjpegdec.c:1838
static int mjpeg_parse_len(MJpegDecodeContext *s, int *plen, const char *name)
Definition mjpegdec.c:197
static int mjpeg_decode_scan(MJpegDecodeContext *s)
Definition mjpegdec.c:1444
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition mjpegdec.c:311
static void mjpeg_idct_scan_progressive_ac(MJpegDecodeContext *s)
Definition mjpegdec.c:1648
static int init_default_huffman_tables(MJpegDecodeContext *s)
Definition mjpegdec.c:63
int ff_mjpeg_find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
Definition mjpegdec.c:2230
static int decode_block(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, int ac_index, uint16_t *quant_matrix)
Definition mjpegdec.c:867
static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, uint16_t *quant_matrix, int Al)
Definition mjpegdec.c:916
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition mjpegdec.c:252
MJPEG decoder.
static int ff_mjpeg_handle_restart(MJpegDecodeContext *s, int *restart)
Definition mjpegdec.h:222
int ff_mjpeg_build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, int is_ac, void *logctx)
#define MAX_COMPONENTS
Definition mjpegdec.h:48
const char data[16]
Definition mxf.c:155
uint8_t interlaced
Definition mxfenc.c:2478
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3524
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3512
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:559
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition pixfmt.h:814
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
#define AV_PIX_FMT_BGR48
Definition pixfmt.h:539
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:604
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:538
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition pixfmt.h:105
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition pixfmt.h:126
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:560
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:531
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:606
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:570
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:561
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:717
const AVProfile ff_mjpeg_profiles[]
Definition profiles.c:205
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
const char * name
Definition qsvenc.c:142
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
#define av_bswap32
Definition bswap.h:47
#define FF_ARRAY_ELEMS(a)
static const float pred[4]
Definition siprdata.h:259
const uint8_t * code
Definition spdifenc.c:433
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int debug
debug
Definition avcodec.h:1398
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1893
enum AVFieldOrder field_order
Field order.
Definition avcodec.h:698
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1429
const struct AVCodec * codec
Definition avcodec.h:452
int profile
profile
Definition avcodec.h:1646
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:672
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:692
enum AVCodecID codec_id
Definition avcodec.h:453
int extradata_size
Definition avcodec.h:527
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:620
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition avcodec.h:1712
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1677
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1422
AVPacket * in_pkt
This packet is used to hold the packet given to decoders implementing the .decode API; it is unused b...
Definition internal.h:83
enum AVCodecID id
Definition codec.h:189
int step
Number of elements between 2 horizontally consecutive pixels.
Definition pixdesc.h:40
Structure to hold side data for an AVFrame.
Definition frame.h:334
uint8_t * data
Definition frame.h:336
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stereo 3D type: this structure describes how two videos are packed within a single video surface,...
Definition stereo3d.h:203
enum AVStereo3DType type
How views are packed within the video.
Definition stereo3d.h:207
int flags
Additional information about the frame packing.
Definition stereo3d.h:212
Definition rpzaenc.c:60
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define av_free(p)
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
#define src1
Definition h264pred.c:141
#define src
Definition vp8dsp.c:248
static char buffer[20]
Definition seek.c:33
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
int size
const char * g
Definition vf_curves.c:128
static const uint8_t start_code[]
void ff_vlc_free(VLC *vlc)
Definition vlc.c:580
int len
#define init_idct(tx, nm, bpp)
static double c[64]
static int return_frame(AVFilterContext *ctx, int is_second)