FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegdec.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 1226 1824 67.2%
Functions: 32 33 97.0%
Branches: 911 1683 54.1%

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