FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegdec.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 1240 1867 66.4%
Functions: 32 33 97.0%
Branches: 919 1701 54.0%

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