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
1124 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
1125 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
1126 if (!s->ljpeg_buffer)
1127 return AVERROR(ENOMEM);
1128
1129 buffer = s->ljpeg_buffer;
1130
1131 for (i = 0; i < 4; i++)
1132 buffer[0][i] = 1 << (s->bits - 1);
1133
1134 s->restart_count = -1;
1135
1136 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1137 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
1138
1139 if (s->interlaced && s->bottom_field)
1140 ptr += linesize >> 1;
1141
1142 for (i = 0; i < 4; i++)
1143 top[i] = left[i] = topleft[i] = buffer[0][i];
1144
1145 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1146 int modified_predictor = predictor;
1147 int restart;
1148
1149 ret = ff_mjpeg_handle_restart(s, &restart);
1150 if (ret < 0)
1151 return ret;
1152 if (restart) {
1153 resync_mb_x = mb_x;
1154 resync_mb_y = mb_y;
1155 for (i = 0; i < 4; i++)
1156 top[i] = left[i] = topleft[i] = 1 << (s->bits - 1);
1157 }
1158
1159 if (get_bits_left(&s->gb) < 1) {
1160 av_log(s->avctx, AV_LOG_ERROR, "bitstream end in rgb_scan\n");
1161 return AVERROR_INVALIDDATA;
1162 }
1163
1164 if (mb_y == resync_mb_y || mb_y == resync_mb_y + 1 && mb_x < resync_mb_x || !mb_x)
1165 modified_predictor = 1;
1166
1167 for (i = 0; i < nb_components; i++) {
1168 int pred, dc;
1169
1170 topleft[i] = top[i];
1171 top[i] = buffer[mb_x][i];
1172
1173 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
1174
1175 ret = mjpeg_decode_dc(s, s->dc_index[i], &dc);
1176 if (ret < 0)
1177 return ret;
1178
1179 left[i] = buffer[mb_x][i] =
1180 mask & (pred + (unsigned)(dc * (1 << point_transform)));
1181 }
1182 }
1183 if (s->rct && s->nb_components == 4) {
1184 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1185 ptr[4 * mb_x + 2] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1186 ptr[4 * mb_x + 1] = buffer[mb_x][1] + ptr[4 * mb_x + 2];
1187 ptr[4 * mb_x + 3] = buffer[mb_x][2] + ptr[4 * mb_x + 2];
1188 ptr[4 * mb_x + 0] = buffer[mb_x][3];
1189 }
1190 } else if (s->nb_components == 4) {
1191 for (i = 0; i < nb_components; i++) {
1192 int c = s->comp_index[i];
1193 if (s->bits <= 8) {
1194 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1195 ptr[4 * mb_x + 3 - c] = buffer[mb_x][i];
1196 }
1197 } else if (s->bits == 9) {
1198 return AVERROR_PATCHWELCOME;
1199 } else {
1200 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1201 ((uint16_t*)ptr)[4 * mb_x + c] = buffer[mb_x][i];
1202 }
1203 }
1204 }
1205 } else if (s->rct) {
1206 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1207 ptr[3 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1208 ptr[3 * mb_x + 0] = buffer[mb_x][1] + ptr[3 * mb_x + 1];
1209 ptr[3 * mb_x + 2] = buffer[mb_x][2] + ptr[3 * mb_x + 1];
1210 }
1211 } else if (s->pegasus_rct) {
1212 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1213 ptr[3 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
1214 ptr[3 * mb_x + 0] = buffer[mb_x][1] + ptr[3 * mb_x + 1];
1215 ptr[3 * mb_x + 2] = buffer[mb_x][2] + ptr[3 * mb_x + 1];
1216 }
1217 } else if (s->bayer) {
1218 if (s->bits <= 8)
1219 return AVERROR_PATCHWELCOME;
1220 if (nb_components == 1) {
1221 /* Leave decoding to the TIFF/DNG decoder (see comment in ff_mjpeg_decode_sof) */
1222 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
1223 ((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
1224 } else if (nb_components == 2) {
1225 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1226 ((uint16_t*)ptr)[2 * mb_x + 0] = buffer[mb_x][0];
1227 ((uint16_t*)ptr)[2 * mb_x + 1] = buffer[mb_x][1];
1228 }
1229 }
1230 } else {
1231 for (i = 0; i < nb_components; i++) {
1232 int c = s->comp_index[i];
1233 if (s->bits <= 8) {
1234 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1235 ptr[3 * mb_x + 2 - c] = buffer[mb_x][i];
1236 }
1237 } else if (s->bits == 9) {
1238 return AVERROR_PATCHWELCOME;
1239 } else {
1240 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1241 ((uint16_t*)ptr)[3 * mb_x + 2 - c] = buffer[mb_x][i];
1242 }
1243 }
1244 }
1245 }
1246 }
1247 return 0;
1248}
1249
1251{
1252 int predictor = s->Ss;
1253 int point_transform = s->Al;
1254 int nb_components = s->nb_components_sos;
1255 int i, mb_x, mb_y, mask;
1256 int bits = (s->bits + 7) & ~7;
1257 int resync_mb_y = 0;
1258 int resync_mb_x = 0;
1259 int ret;
1260
1261 point_transform += bits - s->bits;
1262 mask = ((1 << s->bits) - 1) << point_transform;
1263
1264 av_assert0(nb_components >= 1 && nb_components <= 4);
1265
1266 s->restart_count = -1;
1267
1268 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1269 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1270 int restart;
1271 ret = ff_mjpeg_handle_restart(s, &restart);
1272 if (ret < 0)
1273 return ret;
1274 if (restart) {
1275 resync_mb_x = mb_x;
1276 resync_mb_y = mb_y;
1277 }
1278
1279 if (get_bits_left(&s->gb) < 1) {
1280 av_log(s->avctx, AV_LOG_ERROR, "bitstream end in yuv_scan\n");
1281 return AVERROR_INVALIDDATA;
1282 }
1283
1284 if (!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y + 1 && mb_x < resync_mb_x || s->interlaced) {
1285 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y + 1 && mb_x < resync_mb_x;
1286 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1287 for (i = 0; i < nb_components; i++) {
1288 uint8_t *ptr;
1289 uint16_t *ptr16;
1290 int n, h, v, x, y, c, j, linesize;
1291 n = s->nb_blocks[i];
1292 c = s->comp_index[i];
1293 h = s->h_scount[i];
1294 v = s->v_scount[i];
1295 x = 0;
1296 y = 0;
1297 linesize = s->linesize[c];
1298
1299 if (bits > 8) linesize /= 2;
1300
1301 for (j = 0; j < n; j++) {
1302 int pred, dc;
1303
1304 ret = mjpeg_decode_dc(s, s->dc_index[i], &dc);
1305 if (ret < 0)
1306 return ret;
1307
1308 if ( h * mb_x + x >= s->width
1309 || v * mb_y + y >= s->height) {
1310 // Nothing to do
1311 } else if (bits <= 8) {
1312 ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); // FIXME optimize this crap
1313 if (y == 0 && toprow) {
1314 if (x == 0 && leftcol) {
1315 pred = 1 << (bits - 1);
1316 } else {
1317 pred = ptr[-1];
1318 }
1319 } else {
1320 if (x == 0 && leftcol) {
1321 pred = ptr[-linesize];
1322 } else {
1323 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
1324 }
1325 }
1326
1327 if (s->interlaced && s->bottom_field)
1328 ptr += linesize >> 1;
1329 pred &= mask;
1330 *ptr = pred + ((unsigned)dc << point_transform);
1331 } else {
1332 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2 * (linesize * (v * mb_y + y)) + 2 * (h * mb_x + x)); // FIXME optimize this crap
1333 if (y == 0 && toprow) {
1334 if (x == 0 && leftcol) {
1335 pred = 1 << (bits - 1);
1336 } else {
1337 pred = ptr16[-1];
1338 }
1339 } else {
1340 if (x == 0 && leftcol) {
1341 pred = ptr16[-linesize];
1342 } else {
1343 PREDICT(pred, ptr16[-linesize - 1], ptr16[-linesize], ptr16[-1], predictor);
1344 }
1345 }
1346
1347 if (s->interlaced && s->bottom_field)
1348 ptr16 += linesize >> 1;
1349 pred &= mask;
1350 *ptr16 = pred + ((unsigned)dc << point_transform);
1351 }
1352 if (++x == h) {
1353 x = 0;
1354 y++;
1355 }
1356 }
1357 }
1358 } else {
1359 for (i = 0; i < nb_components; i++) {
1360 uint8_t *ptr;
1361 uint16_t *ptr16;
1362 int n, h, v, x, y, c, j, linesize, dc;
1363 n = s->nb_blocks[i];
1364 c = s->comp_index[i];
1365 h = s->h_scount[i];
1366 v = s->v_scount[i];
1367 x = 0;
1368 y = 0;
1369 linesize = s->linesize[c];
1370
1371 if (bits > 8) linesize /= 2;
1372
1373 for (j = 0; j < n; j++) {
1374 int pred;
1375
1376 ret = mjpeg_decode_dc(s, s->dc_index[i], &dc);
1377 if (ret < 0)
1378 return ret;
1379
1380 if ( h * mb_x + x >= s->width
1381 || v * mb_y + y >= s->height) {
1382 // Nothing to do
1383 } else if (bits <= 8) {
1384 ptr = s->picture_ptr->data[c] +
1385 (linesize * (v * mb_y + y)) +
1386 (h * mb_x + x); // FIXME optimize this crap
1387 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
1388
1389 pred &= mask;
1390 *ptr = pred + ((unsigned)dc << point_transform);
1391 } else {
1392 ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2 * (linesize * (v * mb_y + y)) + 2 * (h * mb_x + x)); // FIXME optimize this crap
1393 PREDICT(pred, ptr16[-linesize - 1], ptr16[-linesize], ptr16[-1], predictor);
1394
1395 pred &= mask;
1396 *ptr16 = pred + ((unsigned)dc << point_transform);
1397 }
1398
1399 if (++x == h) {
1400 x = 0;
1401 y++;
1402 }
1403 }
1404 }
1405 }
1406 }
1407 }
1408 return 0;
1409}
1410
1412 uint8_t *dst, const uint8_t *src,
1413 int linesize, int lowres)
1414{
1415 switch (lowres) {
1416 case 0: s->copy_block(dst, src, linesize, 8);
1417 break;
1418 case 1: copy_block4(dst, src, linesize, linesize, 4);
1419 break;
1420 case 2: copy_block2(dst, src, linesize, linesize, 2);
1421 break;
1422 case 3: *dst = *src;
1423 break;
1424 }
1425}
1426
1427static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1428{
1429 int block_x, block_y;
1430 int size = 8 >> s->avctx->lowres;
1431 if (s->bits > 8) {
1432 for (block_y = 0; block_y < size; block_y++)
1433 for (block_x = 0; block_x < size; block_x++)
1434 *(uint16_t*)(ptr + 2 * block_x + block_y * linesize) <<= 16 - s->bits;
1435 } else {
1436 for (block_y = 0; block_y < size; block_y++)
1437 for (block_x = 0; block_x < size; block_x++)
1438 *(ptr + block_x + block_y * linesize) <<= 8 - s->bits;
1439 }
1440}
1441
1443{
1444 int nb_components = s->nb_components_sos;
1445 int Ah = s->Ah;
1446 int Al = s->Al;
1447 const uint8_t *mb_bitmask = NULL;
1448 const AVFrame *reference = NULL;
1449 int i, mb_x, mb_y, chroma_h_shift, chroma_v_shift, chroma_width, chroma_height;
1450 uint8_t *data[MAX_COMPONENTS];
1451 const uint8_t *reference_data[MAX_COMPONENTS];
1452 int linesize[MAX_COMPONENTS];
1453 GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1454 int bytes_per_pixel = 1 + (s->bits > 8);
1455 int field_pos = -1;
1456 int ret;
1457
1458 if (s->avctx->codec_id == AV_CODEC_ID_MXPEG) {
1459 mb_bitmask = s->mb_bitmask;
1460 reference = s->reference;
1461 }
1462
1463 if (mb_bitmask) {
1464 if (s->mb_bitmask_size != (s->mb_width * s->mb_height + 7) >> 3) {
1465 av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1466 return AVERROR_INVALIDDATA;
1467 }
1468 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1469 }
1470
1471 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &chroma_h_shift,
1472 &chroma_v_shift);
1473 chroma_width = AV_CEIL_RSHIFT(s->width, chroma_h_shift);
1474 chroma_height = AV_CEIL_RSHIFT(s->height, chroma_v_shift);
1475
1476 for (i = 0; i < nb_components; i++) {
1477 int c = s->comp_index[i];
1478 data[c] = s->picture_ptr->data[c];
1479 reference_data[c] = reference ? reference->data[c] : NULL;
1480 linesize[c] = s->linesize[c];
1481 s->coefs_finished[c] |= 1;
1482 }
1483
1484next_field:
1485 s->restart_count = -1;
1486
1487 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1488 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1489 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1490 int restart;
1491
1492 ret = ff_mjpeg_handle_restart(s, &restart);
1493 if (ret < 0)
1494 return ret;
1495 if (restart) {
1496 for (i = 0; i < nb_components; i++)
1497 s->last_dc[i] = (4 << s->bits);
1498 }
1499
1500 if (get_bits_left(&s->gb) < 0) {
1501 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1502 -get_bits_left(&s->gb));
1503 return AVERROR_INVALIDDATA;
1504 }
1505 for (i = 0; i < nb_components; i++) {
1506 uint8_t *ptr;
1507 int n, h, v, x, y, c, j;
1508 int block_offset;
1509 n = s->nb_blocks[i];
1510 c = s->comp_index[i];
1511 h = s->h_scount[i];
1512 v = s->v_scount[i];
1513 x = 0;
1514 y = 0;
1515 for (j = 0; j < n; j++) {
1516 block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1517 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1518
1519 if (s->interlaced && s->bottom_field)
1520 block_offset += linesize[c] >> 1;
1521 if ( 8 * (h * mb_x + x) < ((c == 1) || (c == 2) ? chroma_width : s->width)
1522 && 8 * (v * mb_y + y) < ((c == 1) || (c == 2) ? chroma_height : s->height)) {
1523 ptr = data[c] + block_offset;
1524 } else
1525 ptr = NULL;
1526 if (!s->progressive) {
1527 if (copy_mb) {
1528 if (ptr)
1529 mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1530 linesize[c], s->avctx->lowres);
1531
1532 } else {
1533 s->bdsp.clear_block(s->block);
1534 if (decode_block(s, s->block, i,
1535 s->dc_index[i], s->ac_index[i],
1536 s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1537 av_log(s->avctx, AV_LOG_ERROR,
1538 "error y=%d x=%d\n", mb_y, mb_x);
1539 return AVERROR_INVALIDDATA;
1540 }
1541 if (ptr && linesize[c]) {
1542 s->idsp.idct_put(ptr, linesize[c], s->block);
1543 if (s->bits & 7)
1544 shift_output(s, ptr, linesize[c]);
1545 }
1546 }
1547 } else {
1548 int block_idx = s->block_stride[c] * (v * mb_y + y) +
1549 (h * mb_x + x);
1550 int16_t *block = s->blocks[c][block_idx];
1551 if (Ah)
1552 block[0] += get_bits1(&s->gb) *
1553 s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1554 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1555 s->quant_matrixes[s->quant_sindex[i]],
1556 Al) < 0) {
1557 av_log(s->avctx, AV_LOG_ERROR,
1558 "error y=%d x=%d\n", mb_y, mb_x);
1559 return AVERROR_INVALIDDATA;
1560 }
1561 }
1562 ff_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1563 ff_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1564 mb_x, mb_y, x, y, c, s->bottom_field,
1565 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1566 if (++x == h) {
1567 x = 0;
1568 y++;
1569 }
1570 }
1571 }
1572 }
1573 }
1574
1575 if (s->interlaced &&
1576 bytestream2_get_bytes_left(&s->gB) > 2 &&
1577 bytestream2_tell(&s->gB) > 2 &&
1578 bytestream2_tell(&s->gB) != field_pos &&
1579 s->gB.buffer[-2] == 0xFF &&
1580 s->gB.buffer[-1] == 0xD1) {
1581 av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1582 field_pos = bytestream2_tell(&s->gB);
1583 s->bottom_field ^= 1;
1584
1585 goto next_field;
1586 }
1587
1588 return 0;
1589}
1590
1592{
1593 int Ss = s->Ss;
1594 int Se = s->Se;
1595 int Ah = s->Ah;
1596 int Al = s->Al;
1597 int mb_x, mb_y;
1598 int EOBRUN = 0;
1599 int c = s->comp_index[0];
1600 uint16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1601
1602 av_assert0(Ss >= 0 && Ah >= 0 && Al >= 0);
1603 if (Se < Ss || Se > 63) {
1604 av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", Ss, Se);
1605 return AVERROR_INVALIDDATA;
1606 }
1607
1608 // s->coefs_finished is a bitmask for coefficients coded
1609 // Ss and Se are parameters telling start and end coefficients
1610 s->coefs_finished[c] |= (2ULL << Se) - (1ULL << Ss);
1611
1612 s->restart_count = -1;
1613
1614 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1615 int block_idx = mb_y * s->block_stride[c];
1616 int16_t (*block)[64] = &s->blocks[c][block_idx];
1617 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1618 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1619 int ret;
1620 int restart;
1621 ret = ff_mjpeg_handle_restart(s, &restart);
1622 if (ret < 0)
1623 return ret;
1624 if (restart)
1625 EOBRUN = 0;
1626
1627 if (Ah)
1628 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1629 quant_matrix, Ss, Se, Al, &EOBRUN);
1630 else
1631 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1632 quant_matrix, Ss, Se, Al, &EOBRUN);
1633
1634 if (ret >= 0 && get_bits_left(&s->gb) < 0)
1636 if (ret < 0) {
1637 av_log(s->avctx, AV_LOG_ERROR,
1638 "error y=%d x=%d\n", mb_y, mb_x);
1639 return AVERROR_INVALIDDATA;
1640 }
1641 }
1642 }
1643 return 0;
1644}
1645
1647{
1648 int mb_x, mb_y;
1649 int c;
1650 const int bytes_per_pixel = 1 + (s->bits > 8);
1651 const int block_size = s->lossless ? 1 : 8;
1652
1653 for (c = 0; c < s->nb_components; c++) {
1654 uint8_t *data = s->picture_ptr->data[c];
1655 int linesize = s->linesize[c];
1656 int h = s->h_max / s->h_count[c];
1657 int v = s->v_max / s->v_count[c];
1658 int mb_width = (s->width + h * block_size - 1) / (h * block_size);
1659 int mb_height = (s->height + v * block_size - 1) / (v * block_size);
1660
1661 if (~s->coefs_finished[c])
1662 av_log(s->avctx, AV_LOG_WARNING, "component %d is incomplete\n", c);
1663
1664 if (s->interlaced && s->bottom_field)
1665 data += linesize >> 1;
1666
1667 for (mb_y = 0; mb_y < mb_height; mb_y++) {
1668 uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1669 int block_idx = mb_y * s->block_stride[c];
1670 int16_t (*block)[64] = &s->blocks[c][block_idx];
1671 for (mb_x = 0; mb_x < mb_width; mb_x++, block++) {
1672 s->idsp.idct_put(ptr, linesize, *block);
1673 if (s->bits & 7)
1674 shift_output(s, ptr, linesize);
1675 ptr += bytes_per_pixel * 8 >> s->avctx->lowres;
1676 }
1677 }
1678 }
1679}
1680
1682{
1683 int len, i, h, v;
1684 int index, id, ret;
1685 const int block_size = s->lossless ? 1 : 8;
1686
1687 if (!s->got_picture) {
1688 av_log(s->avctx, AV_LOG_WARNING,
1689 "Can not process SOS before SOF, skipping\n");
1690 return AVERROR_INVALIDDATA;
1691 }
1692
1693 ret = mjpeg_parse_len(s, &len, "sos");
1694 if (ret < 0)
1695 return ret;
1696 if (len < 1)
1697 return AVERROR_INVALIDDATA;
1698 s->nb_components_sos = bytestream2_get_byteu(&s->gB);
1699 if (s->nb_components_sos == 0 || s->nb_components_sos > MAX_COMPONENTS) {
1701 "decode_sos: nb_components (%d)",
1702 s->nb_components_sos);
1703 return AVERROR_PATCHWELCOME;
1704 }
1705 if (len != 4 + 2 * s->nb_components_sos) {
1706 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: len(%d) mismatch %d components\n", len, s->nb_components_sos);
1707 return AVERROR_INVALIDDATA;
1708 }
1709 for (i = 0; i < s->nb_components_sos; i++) {
1710 id = bytestream2_get_byteu(&s->gB);
1711 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1712 /* find component index */
1713 for (index = 0; index < s->nb_components; index++)
1714 if (id == s->component_id[index])
1715 break;
1716 if (index == s->nb_components) {
1717 av_log(s->avctx, AV_LOG_ERROR,
1718 "decode_sos: index(%d) out of components\n", index);
1719 return AVERROR_INVALIDDATA;
1720 }
1721 /* Metasoft MJPEG codec has Cb and Cr swapped */
1722 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1723 && s->nb_components_sos == 3 && s->nb_components == 3 && i)
1724 index = 3 - i;
1725
1726 s->quant_sindex[i] = s->quant_index[index];
1727 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1728 s->h_scount[i] = s->h_count[index];
1729 s->v_scount[i] = s->v_count[index];
1730
1731 s->comp_index[i] = index;
1732
1733 uint8_t b = bytestream2_get_byteu(&s->gB);
1734 s->dc_index[i] = b >> 4;
1735 s->ac_index[i] = b & 0x0F;
1736
1737 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1738 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1739 goto out_of_range;
1740 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))
1741 goto out_of_range;
1742 }
1743
1744 s->Ss = bytestream2_get_byteu(&s->gB); /* JPEG Ss / lossless JPEG predictor / JPEG-LS NEAR */
1745 s->Se = bytestream2_get_byteu(&s->gB); /* JPEG Se / JPEG-LS ILV */
1746 uint8_t b = bytestream2_get_byteu(&s->gB);
1747 s->Ah = b >> 4; /* Ah */
1748 s->Al = b & 0x0F; /* Al */
1749
1750 if (s->nb_components_sos > 1) {
1751 /* interleaved stream */
1752 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1753 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1754 } else if (!s->ls) { /* skip this for JPEG-LS */
1755 h = s->h_max / s->h_scount[0];
1756 v = s->v_max / s->v_scount[0];
1757 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1758 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1759 s->nb_blocks[0] = 1;
1760 s->h_scount[0] = 1;
1761 s->v_scount[0] = 1;
1762 }
1763
1764 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1765 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1766 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1767 s->Ss, s->Al, s->Se, s->bits, s->mjpb_skiptosod,
1768 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), s->nb_components_sos);
1769
1770
1771 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1772 if (s->mjpb_skiptosod)
1773 bytestream2_skip(&s->gB, s->mjpb_skiptosod);
1774
1775 if (!s->progressive && !s->lossless &&
1776 s->avctx->codec_id != AV_CODEC_ID_MXPEG) {
1777 s->nb_seq_component_scans += s->nb_components_sos;
1778 if (s->nb_seq_component_scans > s->nb_components) {
1779 av_log(s->avctx, AV_LOG_ERROR,
1780 "too many scans for a sequential image\n");
1781 return AVERROR_INVALIDDATA;
1782 }
1783 }
1784
1785 if (s->avctx->hwaccel) {
1786 const uint8_t *buf_ptr;
1787 size_t buf_size;
1788
1789 mjpeg_find_raw_scan_data(s, &buf_ptr, &buf_size);
1790
1791 ret = FF_HW_CALL(s->avctx, decode_slice, buf_ptr, buf_size);
1792 if (ret < 0)
1793 return ret;
1794
1795 } else {
1796 if (s->lossless) {
1797 av_assert0(s->picture_ptr == s->picture);
1798 if (CONFIG_JPEGLS_DECODER && s->ls) {
1799 if ((ret = ff_jpegls_decode_picture(s)) < 0)
1800 return ret;
1801 } else {
1802 if (s->rgb || s->bayer) {
1803 if ((ret = ljpeg_decode_rgb_scan(s)) < 0)
1804 return ret;
1805 } else {
1806 if ((ret = ljpeg_decode_yuv_scan(s)) < 0)
1807 return ret;
1808 }
1809 }
1810 } else {
1811 if (s->progressive && s->Ss) {
1812 av_assert0(s->picture_ptr == s->picture);
1814 return ret;
1815 } else {
1816 if ((ret = mjpeg_decode_scan(s)) < 0)
1817 return ret;
1818 }
1819 }
1820 }
1821
1822 if (s->avctx->codec_id == AV_CODEC_ID_MEDIA100 ||
1823 s->avctx->codec_id == AV_CODEC_ID_MJPEGB ||
1824 s->avctx->codec_id == AV_CODEC_ID_THP) {
1825 /* Add the amount of bits read from the unescaped image data buffer
1826 * into the GetByteContext. */
1827 bytestream2_skipu(&s->gB, (get_bits_count(&s->gb) + 7) / 8);
1828 }
1829
1830 return 0;
1831 out_of_range:
1832 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1833 return AVERROR_INVALIDDATA;
1834}
1835
1837{
1838 if (bytestream2_get_be16u(&s->gB) != 4)
1839 return AVERROR_INVALIDDATA;
1840 s->restart_interval = bytestream2_get_be16u(&s->gB);
1841 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1842 s->restart_interval);
1843
1844 return 0;
1845}
1846
1848{
1849 int len, id, i;
1850
1851 int ret = mjpeg_parse_len(s, &len, "app");
1852 if (ret < 0)
1853 return AVERROR_INVALIDDATA;
1854
1855 if (len < 4) {
1856 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1857 return AVERROR_INVALIDDATA;
1858 av_log(s->avctx, AV_LOG_VERBOSE, "skipping APPx stub (len=%" PRId32 ")\n", len);
1859 goto out;
1860 }
1861
1862 id = bytestream2_get_be32u(&s->gB);
1863 len -= 4;
1864
1865 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1866 av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n",
1867 av_fourcc2str(av_bswap32(id)), id, len);
1868
1869 /* This fourcc is used by non-avid files too, it holds some
1870 information, but it's always present in AVID-created files. */
1871 if (id == AV_RB32("AVI1")) {
1872 /* structure:
1873 4bytes AVI1
1874 1bytes polarity
1875 1bytes always zero
1876 4bytes field_size
1877 4bytes field_size_less_padding
1878 */
1879 if (len < 1)
1880 goto out;
1881 i = bytestream2_get_byteu(&s->gB); len--;
1882 av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1883 goto out;
1884 }
1885
1886 if (id == AV_RB32("JFIF")) {
1887 int t_w, t_h, v1, v2;
1888 if (len < 8)
1889 goto out;
1890 bytestream2_skipu(&s->gB, 1); /* the trailing zero-byte */
1891 v1 = bytestream2_get_byteu(&s->gB);
1892 v2 = bytestream2_get_byteu(&s->gB);
1893 bytestream2_skipu(&s->gB, 1);
1894
1895 s->avctx->sample_aspect_ratio.num = bytestream2_get_be16u(&s->gB);
1896 s->avctx->sample_aspect_ratio.den = bytestream2_get_be16u(&s->gB);
1897 if ( s->avctx->sample_aspect_ratio.num <= 0
1898 || s->avctx->sample_aspect_ratio.den <= 0) {
1899 s->avctx->sample_aspect_ratio.num = 0;
1900 s->avctx->sample_aspect_ratio.den = 1;
1901 }
1902
1903 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1904 av_log(s->avctx, AV_LOG_INFO,
1905 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1906 v1, v2,
1907 s->avctx->sample_aspect_ratio.num,
1908 s->avctx->sample_aspect_ratio.den);
1909
1910 len -= 8;
1911 if (len >= 2) {
1912 t_w = bytestream2_get_byteu(&s->gB);
1913 t_h = bytestream2_get_byteu(&s->gB);
1914 if (t_w && t_h) {
1915 /* skip thumbnail */
1916 if (len - 10 - (t_w * t_h * 3) > 0)
1917 len -= t_w * t_h * 3;
1918 }
1919 len -= 2;
1920 }
1921 goto out;
1922 }
1923
1924 if ( id == AV_RB32("Adob")
1925 && len >= 8
1926 && bytestream2_peek_byteu(&s->gB) == 'e'
1927 && bytestream2_peek_be32u(&s->gB) != AV_RB32("e_CM")) {
1928 bytestream2_skipu(&s->gB, 1); /* 'e' */
1929 bytestream2_skipu(&s->gB, 2); /* version */
1930 bytestream2_skipu(&s->gB, 2); /* flags0 */
1931 bytestream2_skipu(&s->gB, 2); /* flags1 */
1932 s->adobe_transform = bytestream2_get_byteu(&s->gB);
1933 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1934 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1935 len -= 8;
1936 goto out;
1937 }
1938
1939 if (id == AV_RB32("LJIF")) {
1940 int rgb = s->rgb;
1941 int pegasus_rct = s->pegasus_rct;
1942 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1943 av_log(s->avctx, AV_LOG_INFO,
1944 "Pegasus lossless jpeg header found\n");
1945 if (len < 9)
1946 goto out;
1947 bytestream2_skipu(&s->gB, 2); /* version ? */
1948 bytestream2_skipu(&s->gB, 2); /* unknown always 0? */
1949 bytestream2_skipu(&s->gB, 2); /* unknown always 0? */
1950 bytestream2_skipu(&s->gB, 2); /* unknown always 0? */
1951 switch (i = bytestream2_get_byteu(&s->gB)) {
1952 case 1:
1953 rgb = 1;
1954 pegasus_rct = 0;
1955 break;
1956 case 2:
1957 rgb = 1;
1958 pegasus_rct = 1;
1959 break;
1960 default:
1961 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1962 }
1963
1964 len -= 9;
1965 if (s->bayer)
1966 goto out;
1967 if (s->got_picture)
1968 if (rgb != s->rgb || pegasus_rct != s->pegasus_rct) {
1969 av_log(s->avctx, AV_LOG_WARNING, "Mismatching LJIF tag\n");
1970 goto out;
1971 }
1972
1973 s->rgb = rgb;
1974 s->pegasus_rct = pegasus_rct;
1975
1976 goto out;
1977 }
1978 if (id == AV_RL32("colr") && len > 0) {
1979 s->colr = bytestream2_get_byteu(&s->gB);
1980 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1981 av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1982 len--;
1983 goto out;
1984 }
1985 if (id == AV_RL32("xfrm") && len > 0) {
1986 s->xfrm = bytestream2_get_byteu(&s->gB);
1987 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1988 av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1989 len--;
1990 goto out;
1991 }
1992
1993 /* JPS extension by VRex */
1994 if (start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1995 int flags, layout, type;
1996 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1997 av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
1998
1999 bytestream2_skipu(&s->gB, 4); len -= 4; /* JPS_ */
2000 bytestream2_skipu(&s->gB, 2); len -= 2; /* block length */
2001 bytestream2_skipu(&s->gB, 1); /* reserved */
2002 flags = bytestream2_get_byteu(&s->gB);
2003 layout = bytestream2_get_byteu(&s->gB);
2004 type = bytestream2_get_byteu(&s->gB);
2005 len -= 4;
2006
2007 av_freep(&s->stereo3d);
2008 s->stereo3d = av_stereo3d_alloc();
2009 if (!s->stereo3d) {
2010 goto out;
2011 }
2012 if (type == 0) {
2013 s->stereo3d->type = AV_STEREO3D_2D;
2014 } else if (type == 1) {
2015 switch (layout) {
2016 case 0x01:
2017 s->stereo3d->type = AV_STEREO3D_LINES;
2018 break;
2019 case 0x02:
2020 s->stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
2021 break;
2022 case 0x03:
2023 s->stereo3d->type = AV_STEREO3D_TOPBOTTOM;
2024 break;
2025 }
2026 if (!(flags & 0x04)) {
2027 s->stereo3d->flags = AV_STEREO3D_FLAG_INVERT;
2028 }
2029 }
2030 goto out;
2031 }
2032
2033 /* EXIF metadata */
2034 if (start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
2035 int ret;
2036
2037 bytestream2_skipu(&s->gB, 2); // skip padding
2038 len -= 2;
2039
2040 if (s->exif_metadata.entries) {
2041 av_log(s->avctx, AV_LOG_WARNING, "multiple EXIF\n");
2042 goto out;
2043 }
2044
2045 ret = av_exif_parse_buffer(s->avctx, s->gB.buffer, len, &s->exif_metadata, AV_EXIF_TIFF_HEADER);
2046 if (ret < 0) {
2047 av_log(s->avctx, AV_LOG_WARNING, "unable to parse EXIF buffer\n");
2048 goto out;
2049 }
2050
2051 bytestream2_skipu(&s->gB, ret);
2052 len -= ret;
2053
2054 goto out;
2055 }
2056
2057 /* Apple MJPEG-A */
2058 if ((start_code == APP1) && (len > (0x28 - 8))) {
2059 id = bytestream2_get_be32u(&s->gB);
2060 len -= 4;
2061 /* Apple MJPEG-A */
2062 if (id == AV_RB32("mjpg")) {
2063 /* structure:
2064 4bytes field size
2065 4bytes pad field size
2066 4bytes next off
2067 4bytes quant off
2068 4bytes huff off
2069 4bytes image off
2070 4bytes scan off
2071 4bytes data off
2072 */
2073 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2074 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
2075 }
2076 }
2077
2078 if (start_code == APP2 && id == AV_RB32("ICC_") && len >= 10) {
2079 int id2;
2080 unsigned seqno;
2081 unsigned nummarkers;
2082
2083 id = bytestream2_get_be32u(&s->gB);
2084 id2 = bytestream2_get_be24u(&s->gB);
2085 len -= 7;
2086 if (id != AV_RB32("PROF") || id2 != AV_RB24("ILE")) {
2087 av_log(s->avctx, AV_LOG_WARNING, "Invalid ICC_PROFILE header in APP2\n");
2088 goto out;
2089 }
2090
2091 bytestream2_skipu(&s->gB, 1);
2092 seqno = bytestream2_get_byteu(&s->gB);
2093 len -= 2;
2094 if (seqno == 0) {
2095 av_log(s->avctx, AV_LOG_WARNING, "Invalid sequence number in APP2\n");
2096 goto out;
2097 }
2098
2099 nummarkers = bytestream2_get_byteu(&s->gB);
2100 len -= 1;
2101 if (nummarkers == 0) {
2102 av_log(s->avctx, AV_LOG_WARNING, "Invalid number of markers coded in APP2\n");
2103 goto out;
2104 } else if (s->iccnum != 0 && nummarkers != s->iccnum) {
2105 av_log(s->avctx, AV_LOG_WARNING, "Mismatch in coded number of ICC markers between markers\n");
2106 goto out;
2107 } else if (seqno > nummarkers) {
2108 av_log(s->avctx, AV_LOG_WARNING, "Mismatching sequence number and coded number of ICC markers\n");
2109 goto out;
2110 }
2111
2112 /* Allocate if this is the first APP2 we've seen. */
2113 if (s->iccnum == 0) {
2114 if (!FF_ALLOCZ_TYPED_ARRAY(s->iccentries, nummarkers)) {
2115 av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data arrays\n");
2116 return AVERROR(ENOMEM);
2117 }
2118 s->iccnum = nummarkers;
2119 }
2120
2121 if (s->iccentries[seqno - 1].data) {
2122 av_log(s->avctx, AV_LOG_WARNING, "Duplicate ICC sequence number\n");
2123 goto out;
2124 }
2125
2126 s->iccentries[seqno - 1].length = len;
2127 s->iccentries[seqno - 1].data = av_malloc(len);
2128 if (!s->iccentries[seqno - 1].data) {
2129 av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data buffer\n");
2130 return AVERROR(ENOMEM);
2131 }
2132
2133 bytestream2_get_bufferu(&s->gB, s->iccentries[seqno - 1].data, len);
2134 len = 0;
2135 s->iccread++;
2136
2137 if (s->iccread > s->iccnum)
2138 av_log(s->avctx, AV_LOG_WARNING, "Read more ICC markers than are supposed to be coded\n");
2139 }
2140
2141 if (start_code == APP2 && id == AV_RB32("urn:")) {
2142 bytestream2_seek(&s->gB, -4, SEEK_CUR); /* include `urn:` in the string */
2143 len += 4;
2144
2145 if (len >= sizeof(AV_ISO21496_IDENTIFIER) &&
2146 !memcmp(s->gB.buffer, AV_ISO21496_IDENTIFIER, sizeof(AV_ISO21496_IDENTIFIER)))
2147 {
2149 len -= sizeof(AV_ISO21496_IDENTIFIER);
2150
2151 /* Discard gain map version stubs from base layer */
2152 if (len <= sizeof(uint16_t[2]))
2153 goto out;
2154 else if (s->gain_map) {
2155 av_log(s->avctx, AV_LOG_WARNING, "Multiple ISO 21496 gain map headers\n");
2156 goto out;
2157 }
2158
2159 s->gain_map = av_gain_map_params_alloc(&s->gain_map_size);
2160 if (!s->gain_map)
2161 return AVERROR(ENOMEM);
2162
2163 ret = av_gain_map_params_from_iso21496(s->gain_map, s->gB.buffer, len);
2164 if (ret < 0) {
2165 av_log(s->avctx, AV_LOG_WARNING, "Failed to parse ISO 21496 gain map header\n");
2166 av_freep(&s->gain_map);
2167 s->gain_map_size = 0;
2168 goto out;
2169 }
2170 }
2171 }
2172
2173out:
2174 /* slow but needed for extreme adobe jpegs */
2175 if (len < 0)
2176 av_log(s->avctx, AV_LOG_ERROR,
2177 "mjpeg: error, decode_app parser read over the end\n");
2178 if (len > 0)
2179 bytestream2_skipu(&s->gB, len);
2180
2181 return 0;
2182}
2183
2185{
2186 int len;
2187 int ret = mjpeg_parse_len(s, &len, "com");
2188 if (ret < 0)
2189 return ret;
2190 if (!len)
2191 return 0;
2192
2193 int i;
2194 char *cbuf = av_malloc(len + 1);
2195 if (!cbuf)
2196 return AVERROR(ENOMEM);
2197
2198 for (i = 0; i < len; i++)
2199 cbuf[i] = bytestream2_get_byteu(&s->gB);
2200 if (cbuf[i - 1] == '\n')
2201 cbuf[i - 1] = 0;
2202 else
2203 cbuf[i] = 0;
2204
2205 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2206 av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
2207
2208 /* buggy avid, it puts EOI only at every 10th frame */
2209 if (!strncmp(cbuf, "AVID", 4)) {
2210 parse_avid(s, cbuf, len);
2211 } else if (!strcmp(cbuf, "CS=ITU601"))
2212 s->cs_itu601 = 1;
2213 else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
2214 (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
2215 s->flipped = 1;
2216 else if (!strcmp(cbuf, "MULTISCOPE II")) {
2217 s->avctx->sample_aspect_ratio = (AVRational) { 1, 2 };
2218 s->multiscope = 2;
2219 }
2220
2221 av_free(cbuf);
2222
2223 return 0;
2224}
2225
2226/* return the 8 bit start code value and update the search
2227 state. Return -1 if no start code found */
2228int ff_mjpeg_find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
2229{
2230 const uint8_t *buf_ptr;
2231 int val;
2232
2233 buf_ptr = *pbuf_ptr;
2234 while ((buf_ptr = memchr(buf_ptr, 0xff, buf_end - buf_ptr))) {
2235 buf_ptr++;
2236 while (buf_ptr < buf_end) {
2237 val = *buf_ptr++;
2238 if (val != 0xff) {
2239 if ((val >= SOF0) && (val <= COM))
2240 goto found;
2241 break;
2242 }
2243 }
2244 }
2245 buf_ptr = buf_end;
2246 val = -1;
2247found:
2248 ff_dlog(NULL, "find_marker skipped %td bytes\n",
2249 (buf_ptr - *pbuf_ptr) - (val < 0 ? 0 : 2));
2250 *pbuf_ptr = buf_ptr;
2251 return val;
2252}
2253
2255 const uint8_t **pbuf_ptr, size_t *pbuf_size)
2256{
2257 const uint8_t *buf_ptr = s->gB.buffer;
2258 const uint8_t *buf_end = buf_ptr + bytestream2_get_bytes_left(&s->gB);
2259
2260 /* Find size of image data buffer (including restart markers).
2261 * No unescaping is performed. */
2262 const uint8_t *ptr = buf_ptr;
2263 while ((ptr = memchr(ptr, 0xff, buf_end - ptr))) {
2264 ptr++;
2265 if (ptr < buf_end) {
2266 uint8_t x = *ptr++;
2267 /* Discard multiple optional 0xFF fill bytes. */
2268 while (x == 0xff && ptr < buf_end)
2269 x = *ptr++;
2270 if (x && (x < RST0 || x > RST7)) {
2271 /* Non-restart marker */
2272 ptr -= 2;
2273 goto found_hw;
2274 }
2275 }
2276 }
2277 ptr = buf_end;
2278found_hw:
2279 *pbuf_ptr = buf_ptr;
2280 *pbuf_size = ptr - buf_ptr;
2281 bytestream2_skipu(&s->gB, *pbuf_size);
2282}
2283
2285{
2286 const uint8_t *buf_ptr = s->gB.buffer;
2287 const uint8_t *buf_end = buf_ptr + bytestream2_get_bytes_left(&s->gB);
2288 const uint8_t *unescaped_buf_ptr;
2289 size_t unescaped_buf_size;
2290
2291 if (s->avctx->codec_id == AV_CODEC_ID_MEDIA100 ||
2292 s->avctx->codec_id == AV_CODEC_ID_MJPEGB ||
2293 s->avctx->codec_id == AV_CODEC_ID_THP) {
2294 /* The image data buffer is already unescaped. The only way to
2295 * find the size of the buffer is by fully decoding it. */
2296 unescaped_buf_ptr = buf_ptr;
2297 unescaped_buf_size = buf_end - buf_ptr;
2298 goto the_end;
2299 }
2300
2301 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - buf_ptr);
2302 if (!s->buffer)
2303 return AVERROR(ENOMEM);
2304
2305 /* unescape buffer of SOS, use special treatment for JPEG-LS */
2306 if (!s->ls) {
2307 const uint8_t *src = buf_ptr;
2308 const uint8_t *ptr = src;
2309 uint8_t *dst = s->buffer;
2310 PutByteContext pb;
2311
2312 bytestream2_init_writer(&pb, dst, buf_end - src);
2313
2314 while ((ptr = memchr(ptr, 0xff, buf_end - ptr))) {
2315 ptr++;
2316 if (ptr < buf_end) {
2317 /* Copy verbatim data. */
2318 ptrdiff_t length = (ptr - 1) - src;
2319 if (length > 0)
2320 bytestream2_put_bufferu(&pb, src, length);
2321
2322 uint8_t x = *ptr++;
2323 /* Discard multiple optional 0xFF fill bytes. */
2324 while (x == 0xff && ptr < buf_end)
2325 x = *ptr++;
2326
2327 src = ptr;
2328 if (x == 0) {
2329 /* Stuffed zero byte */
2330 bytestream2_put_byteu(&pb, 0xff);
2331 } else if (x >= RST0 && x <= RST7) {
2332 /* Restart marker */
2333 goto found;
2334 } else {
2335 /* Non-restart marker */
2336 ptr -= 2;
2337 goto found;
2338 }
2339 }
2340 }
2341 /* Copy remaining verbatim data. */
2342 ptr = buf_end;
2343 ptrdiff_t length = ptr - src;
2344 if (length > 0)
2345 bytestream2_put_bufferu(&pb, src, length);
2346
2347found:
2348 unescaped_buf_ptr = s->buffer;
2349 unescaped_buf_size = bytestream2_tell_p(&pb);
2350 memset(s->buffer + unescaped_buf_size, 0,
2352
2353 bytestream2_skipu(&s->gB, ptr - buf_ptr);
2354
2355 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
2356 (buf_end - buf_ptr) - (unescaped_buf_size));
2357 } else {
2358 const uint8_t *src = buf_ptr;
2359 const uint8_t *ptr = src;
2360 uint8_t *dst = s->buffer;
2361 PutBitContext pb;
2362
2363 init_put_bits(&pb, dst, buf_end - src);
2364
2365 while ((ptr = memchr(ptr, 0xff, buf_end - ptr))) {
2366 ptr++;
2367 if (ptr < buf_end) {
2368 /* Copy verbatim data. */
2369 ptrdiff_t length = (ptr - 1) - src;
2370 if (length > 0)
2371 ff_copy_bits(&pb, src, length * 8);
2372
2373 uint8_t x = *ptr++;
2374 /* Discard multiple optional 0xFF fill bytes. */
2375 while (x == 0xff && ptr < buf_end)
2376 x = *ptr++;
2377
2378 src = ptr;
2379 if (!(x & 0x80)) {
2380 /* Stuffed zero bit */
2381 put_bits(&pb, 15, 0x7f80 | x);
2382 } else if (x >= RST0 && x <= RST7) {
2383 /* Restart marker */
2384 goto found_ls;
2385 } else {
2386 /* Non-restart marker */
2387 ptr -= 2;
2388 goto found_ls;
2389 }
2390 }
2391 }
2392 /* Copy remaining verbatim data. */
2393 ptr = buf_end;
2394 ptrdiff_t length = ptr - src;
2395 if (length > 0)
2396 ff_copy_bits(&pb, src, length * 8);
2397
2398found_ls:
2399 flush_put_bits(&pb);
2400
2401 unescaped_buf_ptr = dst;
2402 unescaped_buf_size = put_bytes_output(&pb);
2403 memset(s->buffer + unescaped_buf_size, 0,
2405
2406 bytestream2_skipu(&s->gB, ptr - buf_ptr);
2407 }
2408
2409the_end:
2410 return init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
2411}
2412
2414{
2415 int i;
2416
2417 if (s->iccentries) {
2418 for (i = 0; i < s->iccnum; i++)
2419 av_freep(&s->iccentries[i].data);
2420 av_freep(&s->iccentries);
2421 }
2422
2423 s->iccread = 0;
2424 s->iccnum = 0;
2425}
2426
2428 int *got_frame, const AVPacket *avpkt,
2429 const uint8_t *buf, const int buf_size)
2430{
2431 MJpegDecodeContext *s = avctx->priv_data;
2432 const uint8_t *buf_end, *buf_ptr;
2433 int hshift, vshift;
2434 int start_code;
2435 int index;
2436 int ret = 0;
2437 int is16bit;
2438
2439 s->force_pal8 = 0;
2440 s->total_ls_decoded_height = 0;
2441
2442 s->buf_size = buf_size;
2443 s->raw_image_buffer = NULL;
2444 s->raw_image_buffer_size = 0;
2445
2446 av_exif_free(&s->exif_metadata);
2447 av_freep(&s->stereo3d);
2448 av_freep(&s->gain_map);
2449 s->adobe_transform = -1;
2450
2451 if (s->iccnum != 0)
2453
2454redo_for_pal8:
2455 buf_ptr = buf;
2456 buf_end = buf + buf_size;
2457 while (buf_ptr < buf_end) {
2458 /* find start next marker */
2459 start_code = ff_mjpeg_find_marker(&buf_ptr, buf_end);
2460 /* EOF */
2461 if (start_code < 0)
2462 break;
2463
2464 ptrdiff_t bytes_left = buf_end - buf_ptr;
2465 if (bytes_left > INT_MAX / 8) {
2466 av_log(avctx, AV_LOG_ERROR,
2467 "MJPEG packet 0x%x too big (%td/%d), corrupt data?\n",
2468 start_code, bytes_left, buf_size);
2469 return AVERROR_INVALIDDATA;
2470 }
2471 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
2472 start_code, buf_end - buf_ptr);
2473
2474 bytestream2_init(&s->gB, buf_ptr, bytes_left);
2475
2476 if (avctx->debug & FF_DEBUG_STARTCODE)
2477 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
2478
2479 /* process markers */
2480 if (start_code >= RST0 && start_code <= RST7) {
2481 av_log(avctx, AV_LOG_DEBUG,
2482 "restart marker: %d\n", start_code & 0x0f);
2483 /* APP fields */
2484 } else if (start_code >= APP0 && start_code <= APP15) {
2485 if ((ret = mjpeg_decode_app(s, start_code)) < 0)
2486 av_log(avctx, AV_LOG_ERROR, "unable to decode APP fields: %s\n",
2487 av_err2str(ret));
2488 /* Comment */
2489 } else if (start_code == COM) {
2491 if (ret < 0)
2492 return ret;
2493 } else if (start_code == DQT) {
2495 if (ret < 0)
2496 return ret;
2497 }
2498
2499 ret = -1;
2500
2501 if (!CONFIG_JPEGLS_DECODER &&
2502 (start_code == SOF55 || start_code == LSE)) {
2503 av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
2504 return AVERROR(ENOSYS);
2505 }
2506
2507 if (avctx->skip_frame == AVDISCARD_ALL) {
2508 switch (start_code) {
2509 case SOF0:
2510 case SOF1:
2511 case SOF2:
2512 case SOF3:
2513 case SOF55:
2514 break;
2515 default:
2516 goto skip;
2517 }
2518 }
2519
2520 switch (start_code) {
2521 case SOI:
2522 s->restart_interval = 0;
2523 s->raw_image_buffer = buf_ptr;
2524 s->raw_image_buffer_size = buf_end - buf_ptr;
2525 /* nothing to do on SOI */
2526 break;
2527 case DHT:
2528 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
2529 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
2530 goto fail;
2531 }
2532 break;
2533 case SOF0:
2534 case SOF1:
2535 if (start_code == SOF0)
2537 else
2539 s->lossless = 0;
2540 s->ls = 0;
2541 s->progressive = 0;
2542 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2543 goto fail;
2544 break;
2545 case SOF2:
2547 s->lossless = 0;
2548 s->ls = 0;
2549 s->progressive = 1;
2550 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2551 goto fail;
2552 break;
2553 case SOF3:
2555 s->lossless = 1;
2556 s->ls = 0;
2557 s->progressive = 0;
2558 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2559 goto fail;
2560 break;
2561 case SOF55:
2563 s->lossless = 1;
2564 s->ls = 1;
2565 s->progressive = 0;
2566 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2567 goto fail;
2568 break;
2569 case LSE:
2570 if (!CONFIG_JPEGLS_DECODER ||
2571 (ret = ff_jpegls_decode_lse(s)) < 0)
2572 goto fail;
2573 if (ret == 1)
2574 goto redo_for_pal8;
2575 break;
2576 case EOI:
2577eoi_parser:
2578 if (!avctx->hwaccel &&
2579 s->progressive && s->cur_scan && s->got_picture)
2581 s->cur_scan = 0;
2582 if (!s->got_picture) {
2583 av_log(avctx, AV_LOG_WARNING,
2584 "Found EOI before any SOF, ignoring\n");
2585 break;
2586 }
2587 if (s->interlaced) {
2588 s->bottom_field ^= 1;
2589 /* if not bottom field, do not output image yet */
2590 if (s->bottom_field == !s->interlace_polarity)
2591 break;
2592 }
2593 if (avctx->hwaccel) {
2594 ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
2595 if (ret < 0)
2596 return ret;
2597
2598 av_refstruct_unref(&s->hwaccel_picture_private);
2599 }
2600 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2601 return ret;
2602 if (s->lossless)
2603 frame->flags |= AV_FRAME_FLAG_LOSSLESS;
2604 *got_frame = 1;
2605 s->got_picture = 0;
2606
2607 if (!s->lossless && avctx->debug & FF_DEBUG_QP) {
2608 int qp = FFMAX3(s->qscale[0],
2609 s->qscale[1],
2610 s->qscale[2]);
2611
2612 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2613 }
2614
2615 goto the_end;
2616 case SOS:
2617 s->cur_scan++;
2618
2619 if ((ret = ff_mjpeg_decode_sos(s)) < 0 &&
2620 (avctx->err_recognition & AV_EF_EXPLODE))
2621 goto fail;
2622 break;
2623 case DRI:
2624 if ((ret = mjpeg_decode_dri(s)) < 0)
2625 return ret;
2626 break;
2627 case SOF5:
2628 case SOF6:
2629 case SOF7:
2630 case SOF9:
2631 case SOF10:
2632 case SOF11:
2633 case SOF13:
2634 case SOF14:
2635 case SOF15:
2636 case JPG:
2637 av_log(avctx, AV_LOG_ERROR,
2638 "mjpeg: unsupported coding type (%x)\n", start_code);
2639 break;
2640 }
2641
2642 if (avctx->skip_frame == AVDISCARD_ALL) {
2643 switch (start_code) {
2644 case SOF0:
2645 case SOF1:
2646 case SOF2:
2647 case SOF3:
2648 case SOF55:
2649 s->got_picture = 0;
2650 goto the_end_no_picture;
2651 }
2652 }
2653
2654skip:
2655 /* eof process start code */
2656 buf_ptr += bytestream2_tell(&s->gB);
2657 av_log(avctx, AV_LOG_DEBUG,
2658 "marker parser used %d bytes\n",
2659 bytestream2_tell(&s->gB));
2660 }
2661 if (s->got_picture && s->cur_scan) {
2662 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2663 goto eoi_parser;
2664 }
2665 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2666 return AVERROR_INVALIDDATA;
2667fail:
2668 s->got_picture = 0;
2669 return ret;
2670the_end:
2671
2672 is16bit = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].step > 1;
2673
2674 if (AV_RB32(s->upscale_h)) {
2675 int p;
2677 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2678 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2679 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2680 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2681 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2682 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2683 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2684 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2685 avctx->pix_fmt == AV_PIX_FMT_YUV420P16||
2686 avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2687 avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2688 avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2689 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2690 );
2691 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &hshift, &vshift);
2692 if (ret)
2693 return ret;
2694
2695 av_assert0(s->nb_components == av_pix_fmt_count_planes(s->picture_ptr->format));
2696 for (p = 0; p < s->nb_components; p++) {
2697 uint8_t *line = s->picture_ptr->data[p];
2698 int w = s->width;
2699 int h = s->height;
2700 if (!s->upscale_h[p])
2701 continue;
2702 if (p == 1 || p == 2) {
2703 w = AV_CEIL_RSHIFT(w, hshift);
2704 h = AV_CEIL_RSHIFT(h, vshift);
2705 }
2706 if (s->upscale_v[p] == 1)
2707 h = (h + 1) >> 1;
2708 av_assert0(w > 0);
2709 for (int i = 0; i < h; i++) {
2710 if (s->upscale_h[p] == 1) {
2711 if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2712 else line[w - 1] = line[(w - 1) / 2];
2713 for (index = w - 2; index > 0; index--) {
2714 if (is16bit)
2715 ((uint16_t*)line)[index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2716 else
2717 line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2718 }
2719 } else if (s->upscale_h[p] == 2) {
2720 if (is16bit) {
2721 ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 3];
2722 if (w > 1)
2723 ((uint16_t*)line)[w - 2] = ((uint16_t*)line)[w - 1];
2724 } else {
2725 line[w - 1] = line[(w - 1) / 3];
2726 if (w > 1)
2727 line[w - 2] = line[w - 1];
2728 }
2729 for (index = w - 3; index > 0; index--) {
2730 line[index] = (line[index / 3] + line[(index + 1) / 3] + line[(index + 2) / 3] + 1) / 3;
2731 }
2732 } else if (s->upscale_h[p] == 4) {
2733 if (is16bit) {
2734 uint16_t *line16 = (uint16_t *) line;
2735 line16[w - 1] = line16[(w - 1) >> 2];
2736 if (w > 1)
2737 line16[w - 2] = (line16[(w - 1) >> 2] * 3 + line16[(w - 2) >> 2]) >> 2;
2738 if (w > 2)
2739 line16[w - 3] = (line16[(w - 1) >> 2] + line16[(w - 2) >> 2]) >> 1;
2740 } else {
2741 line[w - 1] = line[(w - 1) >> 2];
2742 if (w > 1)
2743 line[w - 2] = (line[(w - 1) >> 2] * 3 + line[(w - 2) >> 2]) >> 2;
2744 if (w > 2)
2745 line[w - 3] = (line[(w - 1) >> 2] + line[(w - 2) >> 2]) >> 1;
2746 }
2747 for (index = w - 4; index > 0; index--)
2748 line[index] = (line[(index + 3) >> 2] + line[(index + 2) >> 2]
2749 + line[(index + 1) >> 2] + line[index >> 2]) >> 2;
2750 }
2751 line += s->linesize[p];
2752 }
2753 }
2754 }
2755 if (AV_RB32(s->upscale_v)) {
2756 int p;
2758 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2759 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2760 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2761 avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2762 avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2763 avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2764 avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2765 avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2766 avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2767 avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2768 avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2769 avctx->pix_fmt == AV_PIX_FMT_GBRAP
2770 );
2771 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &hshift, &vshift);
2772 if (ret)
2773 return ret;
2774
2775 av_assert0(s->nb_components == av_pix_fmt_count_planes(s->picture_ptr->format));
2776 for (p = 0; p < s->nb_components; p++) {
2777 uint8_t *dst;
2778 int w = s->width;
2779 int h = s->height;
2780 if (!s->upscale_v[p])
2781 continue;
2782 if (p == 1 || p == 2) {
2783 w = AV_CEIL_RSHIFT(w, hshift);
2784 h = AV_CEIL_RSHIFT(h, vshift);
2785 }
2786 dst = &((uint8_t *)s->picture_ptr->data[p])[(h - 1) * s->linesize[p]];
2787 for (int i = h - 1; i; i--) {
2788 uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2789 uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2790 if (s->upscale_v[p] != 2 && (src1 == src2 || i == h - 1)) {
2791 memcpy(dst, src1, w);
2792 } else {
2793 for (index = 0; index < w; index++)
2794 dst[index] = (src1[index] + src2[index]) >> 1;
2795 }
2796 dst -= s->linesize[p];
2797 }
2798 }
2799 }
2800 if (avctx->codec_id != AV_CODEC_ID_SMVJPEG &&
2801 (avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
2802 avctx->codec_tag == MKTAG('A', 'V', 'D', 'J')) &&
2803 avctx->coded_height > s->orig_height) {
2804 frame->height = AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres);
2805 frame->crop_top = frame->height - avctx->height;
2806 }
2807
2808 if (s->flipped && !s->rgb && !s->bayer) {
2809 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &hshift, &vshift);
2810 if (ret)
2811 return ret;
2812
2813 av_assert0(s->nb_components == av_pix_fmt_count_planes(frame->format));
2814 for (index = 0; index < s->nb_components; index++) {
2815 int h = frame->height;
2816 if (index && index < 3)
2817 h = AV_CEIL_RSHIFT(h, vshift);
2818 if (frame->data[index]) {
2819 frame->data[index] += (h - 1) * frame->linesize[index];
2820 frame->linesize[index] *= -1;
2821 }
2822 }
2823 }
2824
2825 if (avctx->pix_fmt == AV_PIX_FMT_GBRP) {
2826 av_assert0(s->nb_components == 3);
2827 FFSWAP(uint8_t *, frame->data[0], frame->data[2]);
2828 FFSWAP(uint8_t *, frame->data[0], frame->data[1]);
2829 FFSWAP(int, frame->linesize[0], frame->linesize[2]);
2830 FFSWAP(int, frame->linesize[0], frame->linesize[1]);
2831 }
2832
2833 if (s->adobe_transform == 0 && avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2834 int w = s->picture_ptr->width;
2835 int h = s->picture_ptr->height;
2836 av_assert0(s->nb_components == 4);
2837 for (int i = 0; i < h; i++) {
2838 int j;
2839 uint8_t *dst[4];
2840 for (index = 0; index < 4; index++) {
2841 dst[index] = s->picture_ptr->data[index]
2842 + s->picture_ptr->linesize[index]*i;
2843 }
2844 for (j = 0; j < w; j++) {
2845 int k = dst[3][j];
2846 int r = dst[0][j] * k;
2847 int g = dst[1][j] * k;
2848 int b = dst[2][j] * k;
2849 dst[0][j] = g * 257 >> 16;
2850 dst[1][j] = b * 257 >> 16;
2851 dst[2][j] = r * 257 >> 16;
2852 }
2853 memset(dst[3], 255, w);
2854 }
2855 }
2856 if (s->adobe_transform == 2 && avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2857 int w = s->picture_ptr->width;
2858 int h = s->picture_ptr->height;
2859 av_assert0(s->nb_components == 4);
2860 for (int i = 0; i < h; i++) {
2861 int j;
2862 uint8_t *dst[4];
2863 for (index = 0; index < 4; index++) {
2864 dst[index] = s->picture_ptr->data[index]
2865 + s->picture_ptr->linesize[index]*i;
2866 }
2867 for (j = 0; j < w; j++) {
2868 int k = dst[3][j];
2869 int r = (255 - dst[0][j]) * k;
2870 int g = (128 - dst[1][j]) * k;
2871 int b = (128 - dst[2][j]) * k;
2872 dst[0][j] = r * 257 >> 16;
2873 dst[1][j] = (g * 257 >> 16) + 128;
2874 dst[2][j] = (b * 257 >> 16) + 128;
2875 }
2876 memset(dst[3], 255, w);
2877 }
2878 }
2879
2880 if (s->stereo3d) {
2882 if (stereo) {
2883 stereo->type = s->stereo3d->type;
2884 stereo->flags = s->stereo3d->flags;
2885 }
2886 av_freep(&s->stereo3d);
2887 }
2888
2889 if (s->gain_map) {
2890 AVFrameSideData *sd;
2891 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_GAIN_MAP_PARAMS, s->gain_map_size, &sd);
2892 if (ret < 0)
2893 return ret;
2894
2895 if (sd)
2896 memcpy(sd->data, s->gain_map, s->gain_map_size);
2897 av_freep(&s->gain_map);
2898 s->gain_map_size = 0;
2899 }
2900
2901 if (s->iccnum != 0 && s->iccnum == s->iccread) {
2902 AVFrameSideData *sd;
2903 size_t offset = 0;
2904 int total_size = 0;
2905
2906 /* Sum size of all parts. */
2907 for (int i = 0; i < s->iccnum; i++)
2908 total_size += s->iccentries[i].length;
2909
2910 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_ICC_PROFILE, total_size, &sd);
2911 if (ret < 0) {
2912 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame side data\n");
2913 return ret;
2914 }
2915
2916 if (sd) {
2917 /* Reassemble the parts, which are now in-order. */
2918 for (int i = 0; i < s->iccnum; i++) {
2919 memcpy(sd->data + offset, s->iccentries[i].data, s->iccentries[i].length);
2920 offset += s->iccentries[i].length;
2921 }
2922 }
2923 }
2924
2925 if (s->exif_metadata.entries) {
2926 ret = ff_decode_exif_attach_ifd(avctx, frame, &s->exif_metadata);
2927 av_exif_free(&s->exif_metadata);
2928 if (ret < 0)
2929 av_log(avctx, AV_LOG_WARNING, "couldn't attach EXIF metadata\n");
2930 }
2931
2932the_end_no_picture:
2933 av_log(avctx, AV_LOG_DEBUG, "decode frame unused %td bytes\n",
2934 buf_end - buf_ptr);
2935 return buf_ptr - buf;
2936}
2937
2939 AVPacket *avpkt)
2940{
2941 return ff_mjpeg_decode_frame_from_buf(avctx, frame, got_frame,
2942 avpkt, avpkt->data, avpkt->size);
2943}
2944
2945
2946/* mxpeg may call the following function (with a blank MJpegDecodeContext)
2947 * even without having called ff_mjpeg_decode_init(). */
2949{
2950 MJpegDecodeContext *s = avctx->priv_data;
2951 int i, j;
2952
2953 if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_num) {
2954 av_log(avctx, AV_LOG_INFO, "Single field\n");
2955 }
2956
2957 av_refstruct_unref(&s->hwaccel_picture_private);
2958 av_frame_free(&s->picture);
2959 s->picture_ptr = NULL;
2960
2961 av_frame_free(&s->smv_frame);
2962
2963 av_freep(&s->buffer);
2964 av_freep(&s->stereo3d);
2965 av_freep(&s->ljpeg_buffer);
2966 s->ljpeg_buffer_size = 0;
2967 av_freep(&s->gain_map);
2968 s->gain_map_size = 0;
2969
2970 for (i = 0; i < 3; i++) {
2971 for (j = 0; j < 4; j++)
2972 ff_vlc_free(&s->vlcs[i][j]);
2973 }
2974 for (i = 0; i < MAX_COMPONENTS; i++) {
2975 av_freep(&s->blocks[i]);
2976 av_freep(&s->last_nnz[i]);
2977 }
2978 av_exif_free(&s->exif_metadata);
2979
2981
2982 av_freep(&s->jls_state);
2983
2984 return 0;
2985}
2986
2988{
2989 MJpegDecodeContext *s = avctx->priv_data;
2990 s->got_picture = 0;
2991
2992 av_refstruct_unref(&s->hwaccel_picture_private);
2993
2994 s->smv_next_frame = 0;
2995 av_frame_unref(s->smv_frame);
2996}
2997
2998#if CONFIG_MJPEG_DECODER
2999#if FF_API_MJPEG_EXTERN_HUFF
3000#define OFFSET(x) offsetof(MJpegDecodeContext, x)
3001#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
3002static const AVOption options[] = {
3003 { "extern_huff", "Use external huffman table.",
3004 OFFSET(extern_huff), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD | AV_OPT_FLAG_DEPRECATED },
3005 { NULL },
3006};
3007#endif
3008
3009static const AVClass mjpegdec_class = {
3010 .class_name = "MJPEG decoder",
3011 .item_name = av_default_item_name,
3012#if FF_API_MJPEG_EXTERN_HUFF
3013 .option = options,
3014#endif
3015 .version = LIBAVUTIL_VERSION_INT,
3016};
3017
3018const FFCodec ff_mjpeg_decoder = {
3019 .p.name = "mjpeg",
3020 CODEC_LONG_NAME("MJPEG (Motion JPEG)"),
3021 .p.type = AVMEDIA_TYPE_VIDEO,
3022 .p.id = AV_CODEC_ID_MJPEG,
3023 .priv_data_size = sizeof(MJpegDecodeContext),
3027 .flush = decode_flush,
3028 .p.capabilities = AV_CODEC_CAP_DR1,
3029 .p.max_lowres = 3,
3030 .p.priv_class = &mjpegdec_class,
3032 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3035 .hw_configs = (const AVCodecHWConfigInternal *const []) {
3036#if CONFIG_MJPEG_NVDEC_HWACCEL
3037 HWACCEL_NVDEC(mjpeg),
3038#endif
3039#if CONFIG_MJPEG_VAAPI_HWACCEL
3040 HWACCEL_VAAPI(mjpeg),
3041#endif
3042 NULL
3043 },
3044};
3045#endif
3046#if CONFIG_THP_DECODER
3047const FFCodec ff_thp_decoder = {
3048 .p.name = "thp",
3049 CODEC_LONG_NAME("Nintendo Gamecube THP video"),
3050 .p.type = AVMEDIA_TYPE_VIDEO,
3051 .p.id = AV_CODEC_ID_THP,
3052 .priv_data_size = sizeof(MJpegDecodeContext),
3056 .flush = decode_flush,
3057 .p.capabilities = AV_CODEC_CAP_DR1,
3058 .p.max_lowres = 3,
3059 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
3060};
3061#endif
3062
3063#if CONFIG_SMVJPEG_DECODER
3064// SMV JPEG just stacks several output frames into one JPEG picture
3065// we handle that by setting up the cropping parameters appropriately
3066static void smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
3067{
3068 MJpegDecodeContext *s = avctx->priv_data;
3069
3070 av_assert0((s->smv_next_frame + 1) * avctx->height <= avctx->coded_height);
3071
3072 frame->width = avctx->coded_width;
3073 frame->height = avctx->coded_height;
3074 frame->crop_top = FFMIN(s->smv_next_frame * avctx->height, frame->height);
3075 frame->crop_bottom = frame->height - (s->smv_next_frame + 1) * avctx->height;
3076
3077 if (s->smv_frame->pts != AV_NOPTS_VALUE)
3078 s->smv_frame->pts += s->smv_frame->duration;
3079 s->smv_next_frame = (s->smv_next_frame + 1) % s->smv_frames_per_jpeg;
3080
3081 if (s->smv_next_frame == 0)
3082 av_frame_unref(s->smv_frame);
3083}
3084
3085static int smvjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
3086{
3087 MJpegDecodeContext *s = avctx->priv_data;
3088 AVPacket *const pkt = avctx->internal->in_pkt;
3089 int got_frame = 0;
3090 int ret;
3091
3092 if (s->smv_next_frame > 0)
3093 goto return_frame;
3094
3095 ret = ff_decode_get_packet(avctx, pkt);
3096 if (ret < 0)
3097 return ret;
3098
3099 av_frame_unref(s->smv_frame);
3100
3101 ret = ff_mjpeg_decode_frame(avctx, s->smv_frame, &got_frame, pkt);
3102 s->smv_frame->pkt_dts = pkt->dts;
3104 if (ret < 0)
3105 return ret;
3106
3107 if (!got_frame)
3108 return AVERROR(EAGAIN);
3109
3110 // packet duration covers all the frames in the packet
3111 s->smv_frame->duration /= s->smv_frames_per_jpeg;
3112
3114 av_assert0(s->smv_frame->buf[0]);
3115 ret = av_frame_ref(frame, s->smv_frame);
3116 if (ret < 0)
3117 return ret;
3118
3119 smv_process_frame(avctx, frame);
3120 return 0;
3121}
3122
3124 .p.name = "smvjpeg",
3125 CODEC_LONG_NAME("SMV JPEG"),
3126 .p.type = AVMEDIA_TYPE_VIDEO,
3127 .p.id = AV_CODEC_ID_SMVJPEG,
3128 .priv_data_size = sizeof(MJpegDecodeContext),
3131 FF_CODEC_RECEIVE_FRAME_CB(smvjpeg_receive_frame),
3132 .flush = decode_flush,
3133 .p.capabilities = AV_CODEC_CAP_DR1,
3134 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
3136};
3137#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:787
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:2539
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:901
void av_exif_free(AVExifMetadata *ifd)
Frees all resources associated with the given EXIF metadata struct.
Definition exif.c:678
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:499
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:281
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:763
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:97
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:2987
static void mjpeg_find_raw_scan_data(MJpegDecodeContext *s, const uint8_t **pbuf_ptr, size_t *pbuf_size)
Definition mjpegdec.c:2254
static int mjpeg_decode_app(MJpegDecodeContext *s, int start_code)
Definition mjpegdec.c:1847
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s)
Definition mjpegdec.c:1250
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s)
Definition mjpegdec.c:1097
static void reset_icc_profile(MJpegDecodeContext *s)
Definition mjpegdec.c:2413
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition mjpegdec.c:125
int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
Definition mjpegdec.c:1681
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:2184
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition mjpegdec.c:209
int ff_mjpeg_unescape_sos(MJpegDecodeContext *s)
Definition mjpegdec.c:2284
static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s, uint8_t *dst, const uint8_t *src, int linesize, int lowres)
Definition mjpegdec.c:1411
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition mjpegdec.c:2948
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:2427
#define ZERO_RUN
Definition mjpegdec.c:1013
static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
Definition mjpegdec.c:1427
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:2938
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:1591
#define REFINE_BIT(j)
Definition mjpegdec.c:1005
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition mjpegdec.c:1836
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:1442
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition mjpegdec.c:311
static void mjpeg_idct_scan_progressive_ac(MJpegDecodeContext *s)
Definition mjpegdec.c:1646
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:2228
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:149
uint8_t interlaced
Definition mxfenc.c:2337
#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)