GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mjpegdec.c Lines: 914 1794 50.9 %
Date: 2021-01-21 13:05:02 Branches: 646 1635 39.5 %

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 "libavutil/imgutils.h"
34
#include "libavutil/avassert.h"
35
#include "libavutil/opt.h"
36
#include "avcodec.h"
37
#include "blockdsp.h"
38
#include "copy_block.h"
39
#include "decode.h"
40
#include "hwconfig.h"
41
#include "idctdsp.h"
42
#include "internal.h"
43
#include "jpegtables.h"
44
#include "mjpeg.h"
45
#include "mjpegdec.h"
46
#include "jpeglsdec.h"
47
#include "profiles.h"
48
#include "put_bits.h"
49
#include "tiff.h"
50
#include "exif.h"
51
#include "bytestream.h"
52
53
54
14541
static void build_huffman_codes(uint8_t *huff_size, const uint8_t *bits_table)
55
{
56
247197
    for (int i = 1, k = 0; i <= 16; i++) {
57
232656
        int nb = bits_table[i];
58
1181703
        for (int j = 0; j < nb;j++) {
59
949047
            huff_size[k] = i;
60
949047
            k++;
61
        }
62
    }
63
14541
}
64
65
14541
static int build_vlc(VLC *vlc, const uint8_t *bits_table,
66
                     const uint8_t *val_table, int nb_codes,
67
                     int is_ac, void *logctx)
68
{
69
    uint8_t huff_size[256];
70
    uint16_t huff_sym[256];
71
    int i;
72
73
14541
    av_assert0(nb_codes <= 256);
74
75
14541
    build_huffman_codes(huff_size, bits_table);
76
77
963588
    for (i = 0; i < nb_codes; i++) {
78
949047
        huff_sym[i] = val_table[i] + 16 * is_ac;
79
80

949047
        if (is_ac && !val_table[i])
81
4847
            huff_sym[i] = 16 * 256;
82
    }
83
84
14541
    return ff_init_vlc_from_lengths(vlc, 9, nb_codes, huff_size, 1,
85
                                    huff_sym, 2, 2, 0, 0, logctx);
86
}
87
88
140
static int init_default_huffman_tables(MJpegDecodeContext *s)
89
{
90
    static const struct {
91
        int class;
92
        int index;
93
        const uint8_t *bits;
94
        const uint8_t *values;
95
        int length;
96
    } ht[] = {
97
        { 0, 0, avpriv_mjpeg_bits_dc_luminance,
98
                avpriv_mjpeg_val_dc, 12 },
99
        { 0, 1, avpriv_mjpeg_bits_dc_chrominance,
100
                avpriv_mjpeg_val_dc, 12 },
101
        { 1, 0, avpriv_mjpeg_bits_ac_luminance,
102
                avpriv_mjpeg_val_ac_luminance,   162 },
103
        { 1, 1, avpriv_mjpeg_bits_ac_chrominance,
104
                avpriv_mjpeg_val_ac_chrominance, 162 },
105
        { 2, 0, avpriv_mjpeg_bits_ac_luminance,
106
                avpriv_mjpeg_val_ac_luminance,   162 },
107
        { 2, 1, avpriv_mjpeg_bits_ac_chrominance,
108
                avpriv_mjpeg_val_ac_chrominance, 162 },
109
    };
110
    int i, ret;
111
112
980
    for (i = 0; i < FF_ARRAY_ELEMS(ht); i++) {
113
840
        ret = build_vlc(&s->vlcs[ht[i].class][ht[i].index],
114
                        ht[i].bits, ht[i].values, ht[i].length,
115
840
                        ht[i].class == 1, s->avctx);
116
840
        if (ret < 0)
117
            return ret;
118
119
840
        if (ht[i].class < 2) {
120
560
            memcpy(s->raw_huffman_lengths[ht[i].class][ht[i].index],
121
560
                   ht[i].bits + 1, 16);
122
560
            memcpy(s->raw_huffman_values[ht[i].class][ht[i].index],
123
560
                   ht[i].values, ht[i].length);
124
        }
125
    }
126
127
140
    return 0;
128
}
129
130
11
static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
131
{
132
11
    s->buggy_avid = 1;
133

11
    if (len > 14 && buf[12] == 1) /* 1 - NTSC */
134
11
        s->interlace_polarity = 1;
135

11
    if (len > 14 && buf[12] == 2) /* 2 - PAL */
136
        s->interlace_polarity = 0;
137
11
    if (s->avctx->debug & FF_DEBUG_PICT_INFO)
138
        av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 14 ? buf[12] : -1);
139
11
}
140
141
215
static void init_idct(AVCodecContext *avctx)
142
{
143
215
    MJpegDecodeContext *s = avctx->priv_data;
144
145
215
    ff_idctdsp_init(&s->idsp, avctx);
146
215
    ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
147
                      ff_zigzag_direct);
148
215
}
149
150
140
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
151
{
152
140
    MJpegDecodeContext *s = avctx->priv_data;
153
    int ret;
154
155
140
    if (!s->picture_ptr) {
156
138
        s->picture = av_frame_alloc();
157
138
        if (!s->picture)
158
            return AVERROR(ENOMEM);
159
138
        s->picture_ptr = s->picture;
160
    }
161
162
140
    s->pkt = av_packet_alloc();
163
140
    if (!s->pkt)
164
        return AVERROR(ENOMEM);
165
166
140
    s->avctx = avctx;
167
140
    ff_blockdsp_init(&s->bdsp, avctx);
168
140
    ff_hpeldsp_init(&s->hdsp, avctx->flags);
169
140
    init_idct(avctx);
170
140
    s->buffer_size   = 0;
171
140
    s->buffer        = NULL;
172
140
    s->start_code    = -1;
173
140
    s->first_picture = 1;
174
140
    s->got_picture   = 0;
175
140
    s->orig_height    = avctx->coded_height;
176
140
    avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
177
140
    avctx->colorspace = AVCOL_SPC_BT470BG;
178
140
    s->hwaccel_pix_fmt = s->hwaccel_sw_pix_fmt = AV_PIX_FMT_NONE;
179
180
140
    if ((ret = init_default_huffman_tables(s)) < 0)
181
        return ret;
182
183
140
    if (s->extern_huff) {
184
        av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
185
        if ((ret = init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8)) < 0)
186
            return ret;
187
        if (ff_mjpeg_decode_dht(s)) {
188
            av_log(avctx, AV_LOG_ERROR,
189
                   "error using external huffman table, switching back to internal\n");
190
            init_default_huffman_tables(s);
191
        }
192
    }
193
140
    if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
194
        s->interlace_polarity = 1;           /* bottom field first */
195
        av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
196
140
    } else if (avctx->field_order == AV_FIELD_UNKNOWN) {
197
138
        if (avctx->codec_tag == AV_RL32("MJPG"))
198
51
            s->interlace_polarity = 1;
199
    }
200
201
140
    if (avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
202
2
        if (avctx->extradata_size >= 4)
203
2
            s->smv_frames_per_jpeg = AV_RL32(avctx->extradata);
204
205
2
        if (s->smv_frames_per_jpeg <= 0) {
206
            av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
207
            return AVERROR_INVALIDDATA;
208
        }
209
210
2
        s->smv_frame = av_frame_alloc();
211
2
        if (!s->smv_frame)
212
            return AVERROR(ENOMEM);
213
138
    } else if (avctx->extradata_size > 8
214
        && AV_RL32(avctx->extradata) == 0x2C
215
        && AV_RL32(avctx->extradata+4) == 0x18) {
216
        parse_avid(s, avctx->extradata, avctx->extradata_size);
217
    }
218
219
140
    if (avctx->codec->id == AV_CODEC_ID_AMV)
220
11
        s->flipped = 1;
221
222
140
    return 0;
223
}
224
225
226
/* quantize tables */
227
2519
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
228
{
229
    int len, index, i;
230
231
2519
    len = get_bits(&s->gb, 16) - 2;
232
233
2519
    if (8*len > get_bits_left(&s->gb)) {
234
        av_log(s->avctx, AV_LOG_ERROR, "dqt: len %d is too large\n", len);
235
        return AVERROR_INVALIDDATA;
236
    }
237
238
5533
    while (len >= 65) {
239
3014
        int pr = get_bits(&s->gb, 4);
240
3014
        if (pr > 1) {
241
            av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
242
            return AVERROR_INVALIDDATA;
243
        }
244
3014
        index = get_bits(&s->gb, 4);
245
3014
        if (index >= 4)
246
            return -1;
247
3014
        av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
248
        /* read quant table */
249
195910
        for (i = 0; i < 64; i++) {
250
192896
            s->quant_matrixes[index][i] = get_bits(&s->gb, pr ? 16 : 8);
251
192896
            if (s->quant_matrixes[index][i] == 0) {
252
                av_log(s->avctx, AV_LOG_ERROR, "dqt: 0 quant value\n");
253
                return AVERROR_INVALIDDATA;
254
            }
255
        }
256
257
        // XXX FIXME fine-tune, and perhaps add dc too
258
3014
        s->qscale[index] = FFMAX(s->quant_matrixes[index][1],
259
3014
                                 s->quant_matrixes[index][8]) >> 1;
260
3014
        av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
261
               index, s->qscale[index]);
262
3014
        len -= 1 + 64 * (1+pr);
263
    }
264
2519
    return 0;
265
}
266
267
/* decode huffman tables and build VLC decoders */
268
2545
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
269
{
270
    int len, index, i, class, n, v;
271
    uint8_t bits_table[17];
272
    uint8_t val_table[256];
273
2545
    int ret = 0;
274
275
2545
    len = get_bits(&s->gb, 16) - 2;
276
277
2545
    if (8*len > get_bits_left(&s->gb)) {
278
        av_log(s->avctx, AV_LOG_ERROR, "dht: len %d is too large\n", len);
279
        return AVERROR_INVALIDDATA;
280
    }
281
282
11679
    while (len > 0) {
283
9134
        if (len < 17)
284
            return AVERROR_INVALIDDATA;
285
9134
        class = get_bits(&s->gb, 4);
286
9134
        if (class >= 2)
287
            return AVERROR_INVALIDDATA;
288
9134
        index = get_bits(&s->gb, 4);
289
9134
        if (index >= 4)
290
            return AVERROR_INVALIDDATA;
291
9134
        n = 0;
292
155278
        for (i = 1; i <= 16; i++) {
293
146144
            bits_table[i] = get_bits(&s->gb, 8);
294
146144
            n += bits_table[i];
295
        }
296
9134
        len -= 17;
297

9134
        if (len < n || n > 256)
298
            return AVERROR_INVALIDDATA;
299
300
458539
        for (i = 0; i < n; i++) {
301
449405
            v = get_bits(&s->gb, 8);
302
449405
            val_table[i] = v;
303
        }
304
9134
        len -= n;
305
306
        /* build VLC and flush previous vlc if present */
307
9134
        ff_free_vlc(&s->vlcs[class][index]);
308
9134
        av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
309
               class, index, n);
310
9134
        if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
311
9134
                             n, class > 0, s->avctx)) < 0)
312
            return ret;
313
314
9134
        if (class > 0) {
315
4567
            ff_free_vlc(&s->vlcs[2][index]);
316
4567
            if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
317
4567
                                 n, 0, s->avctx)) < 0)
318
                return ret;
319
        }
320
321
155278
        for (i = 0; i < 16; i++)
322
146144
            s->raw_huffman_lengths[class][index][i] = bits_table[i + 1];
323
2347438
        for (i = 0; i < 256; i++)
324
2338304
            s->raw_huffman_values[class][index][i] = val_table[i];
325
    }
326
2545
    return 0;
327
}
328
329
2551
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
330
{
331
    int len, nb_components, i, width, height, bits, ret, size_change;
332
    unsigned pix_fmt_id;
333
2551
    int h_count[MAX_COMPONENTS] = { 0 };
334
2551
    int v_count[MAX_COMPONENTS] = { 0 };
335
336
2551
    s->cur_scan = 0;
337
2551
    memset(s->upscale_h, 0, sizeof(s->upscale_h));
338
2551
    memset(s->upscale_v, 0, sizeof(s->upscale_v));
339
340
2551
    len     = get_bits(&s->gb, 16);
341
2551
    bits    = get_bits(&s->gb, 8);
342
343

2551
    if (bits > 16 || bits < 1) {
344
        av_log(s->avctx, AV_LOG_ERROR, "bits %d is invalid\n", bits);
345
        return AVERROR_INVALIDDATA;
346
    }
347
348
2551
    if (s->avctx->bits_per_raw_sample != bits) {
349
75
        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);
350
75
        s->avctx->bits_per_raw_sample = bits;
351
75
        init_idct(s->avctx);
352
    }
353
2551
    if (s->pegasus_rct)
354
        bits = 9;
355

2551
    if (bits == 9 && !s->pegasus_rct)
356
        s->rct  = 1;    // FIXME ugly
357
358

2551
    if(s->lossless && s->avctx->lowres){
359
        av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
360
        return -1;
361
    }
362
363
2551
    height = get_bits(&s->gb, 16);
364
2551
    width  = get_bits(&s->gb, 16);
365
366
    // HACK for odd_height.mov
367

2551
    if (s->interlaced && s->width == width && s->height == height + 1)
368
        height= s->height;
369
370
2551
    av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
371
2551
    if (av_image_check_size(width, height, 0, s->avctx) < 0)
372
        return AVERROR_INVALIDDATA;
373

2551
    if (s->buf_size && (width + 7) / 8 * ((height + 7) / 8) > s->buf_size * 4LL)
374
        return AVERROR_INVALIDDATA;
375
376
2551
    nb_components = get_bits(&s->gb, 8);
377

2551
    if (nb_components <= 0 ||
378
        nb_components > MAX_COMPONENTS)
379
        return -1;
380

2551
    if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
381
15
        if (nb_components != s->nb_components) {
382
            av_log(s->avctx, AV_LOG_ERROR,
383
                   "nb_components changing in interlaced picture\n");
384
            return AVERROR_INVALIDDATA;
385
        }
386
    }
387

2551
    if (s->ls && !(bits <= 8 || nb_components == 1)) {
388
        avpriv_report_missing_feature(s->avctx,
389
                                      "JPEG-LS that is not <= 8 "
390
                                      "bits/component or 16-bit gray");
391
        return AVERROR_PATCHWELCOME;
392
    }
393
2551
    if (len != 8 + 3 * nb_components) {
394
        av_log(s->avctx, AV_LOG_ERROR, "decode_sof0: error, len(%d) mismatch %d components\n", len, nb_components);
395
        return AVERROR_INVALIDDATA;
396
    }
397
398
2551
    s->nb_components = nb_components;
399
2551
    s->h_max         = 1;
400
2551
    s->v_max         = 1;
401
10200
    for (i = 0; i < nb_components; i++) {
402
        /* component id */
403
7649
        s->component_id[i] = get_bits(&s->gb, 8) - 1;
404
7649
        h_count[i]         = get_bits(&s->gb, 4);
405
7649
        v_count[i]         = get_bits(&s->gb, 4);
406
        /* compute hmax and vmax (only used in interleaved case) */
407
7649
        if (h_count[i] > s->h_max)
408
2137
            s->h_max = h_count[i];
409
7649
        if (v_count[i] > s->v_max)
410
2298
            s->v_max = v_count[i];
411
7649
        s->quant_index[i] = get_bits(&s->gb, 8);
412
7649
        if (s->quant_index[i] >= 4) {
413
            av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
414
            return AVERROR_INVALIDDATA;
415
        }
416

7649
        if (!h_count[i] || !v_count[i]) {
417
            av_log(s->avctx, AV_LOG_ERROR,
418
                   "Invalid sampling factor in component %d %d:%d\n",
419
                   i, h_count[i], v_count[i]);
420
            return AVERROR_INVALIDDATA;
421
        }
422
423
7649
        av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
424
               i, h_count[i], v_count[i],
425
               s->component_id[i], s->quant_index[i]);
426
    }
427
2551
    if (   nb_components == 4
428
        && s->component_id[0] == 'C' - 1
429
        && s->component_id[1] == 'M' - 1
430
        && s->component_id[2] == 'Y' - 1
431
        && s->component_id[3] == 'K' - 1)
432
        s->adobe_transform = 0;
433
434

2551
    if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
435
        avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
436
        return AVERROR_PATCHWELCOME;
437
    }
438
439
2551
    if (s->bayer) {
440
        if (nb_components == 2) {
441
            /* Bayer images embedded in DNGs can contain 2 interleaved components and the
442
               width stored in their SOF3 markers is the width of each one.  We only output
443
               a single component, therefore we need to adjust the output image width.  We
444
               handle the deinterleaving (but not the debayering) in this file. */
445
            width *= 2;
446
        }
447
        /* They can also contain 1 component, which is double the width and half the height
448
            of the final image (rows are interleaved).  We don't handle the decoding in this
449
            file, but leave that to the TIFF/DNG decoder. */
450
    }
451
452
    /* if different size, realloc/alloc picture */
453

2551
    if (width != s->width || height != s->height || bits != s->bits ||
454
2222
        memcmp(s->h_count, h_count, sizeof(h_count))                ||
455
2222
        memcmp(s->v_count, v_count, sizeof(v_count))) {
456
329
        size_change = 1;
457
458
329
        s->width      = width;
459
329
        s->height     = height;
460
329
        s->bits       = bits;
461
329
        memcpy(s->h_count, h_count, sizeof(h_count));
462
329
        memcpy(s->v_count, v_count, sizeof(v_count));
463
329
        s->interlaced = 0;
464
329
        s->got_picture = 0;
465
466
        /* test interlaced mode */
467
329
        if (s->first_picture   &&
468

126
            (s->multiscope != 2 || s->avctx->time_base.den >= 25 * s->avctx->time_base.num) &&
469
126
            s->orig_height != 0 &&
470
107
            s->height < ((s->orig_height * 3) / 4)) {
471
4
            s->interlaced                    = 1;
472
4
            s->bottom_field                  = s->interlace_polarity;
473
4
            s->picture_ptr->interlaced_frame = 1;
474
4
            s->picture_ptr->top_field_first  = !s->interlace_polarity;
475
4
            height *= 2;
476
        }
477
478
329
        ret = ff_set_dimensions(s->avctx, width, height);
479
329
        if (ret < 0)
480
            return ret;
481
482
329
        s->first_picture = 0;
483
    } else {
484
2222
        size_change = 0;
485
    }
486
487
2551
    if (s->avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
488
3
        s->avctx->height = s->avctx->coded_height / s->smv_frames_per_jpeg;
489
3
        if (s->avctx->height <= 0)
490
            return AVERROR_INVALIDDATA;
491
    }
492
493

2551
    if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
494
15
        if (s->progressive) {
495
            avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
496
            return AVERROR_INVALIDDATA;
497
        }
498
    } else {
499


2536
        if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4))
500
205
            s->rgb = 1;
501
2331
        else if (!s->lossless)
502
2126
            s->rgb = 0;
503
        /* XXX: not complete test ! */
504
2536
        pix_fmt_id = ((unsigned)s->h_count[0] << 28) | (s->v_count[0] << 24) |
505
2536
                     (s->h_count[1] << 20) | (s->v_count[1] << 16) |
506
2536
                     (s->h_count[2] << 12) | (s->v_count[2] <<  8) |
507
2536
                     (s->h_count[3] <<  4) |  s->v_count[3];
508
2536
        av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
509
        /* NOTE we do not allocate pictures large enough for the possible
510
         * padding of h/v_count being 4 */
511
2536
        if (!(pix_fmt_id & 0xD0D0D0D0))
512
            pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
513
2536
        if (!(pix_fmt_id & 0x0D0D0D0D))
514
408
            pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
515
516
22824
        for (i = 0; i < 8; i++) {
517
20288
            int j = 6 + (i&1) - (i&6);
518
20288
            int is = (pix_fmt_id >> (4*i)) & 0xF;
519
20288
            int js = (pix_fmt_id >> (4*j)) & 0xF;
520
521


20288
            if (is == 1 && js != 2 && (i < 2 || i > 5))
522
1060
                js = (pix_fmt_id >> ( 8 + 4*(i&1))) & 0xF;
523


20288
            if (is == 1 && js != 2 && (i < 2 || i > 5))
524
1060
                js = (pix_fmt_id >> (16 + 4*(i&1))) & 0xF;
525
526

20288
            if (is == 1 && js == 2) {
527
                if (i & 1) s->upscale_h[j/2] = 1;
528
                else       s->upscale_v[j/2] = 1;
529
            }
530
        }
531
532
2536
        if (s->bayer) {
533
            if (pix_fmt_id != 0x11110000 && pix_fmt_id != 0x11000000)
534
                goto unk_pixfmt;
535
        }
536
537



2536
        switch (pix_fmt_id) {
538
        case 0x11110000: /* for bayer-encoded huffman lossless JPEGs embedded in DNGs */
539
            if (!s->bayer)
540
                goto unk_pixfmt;
541
            s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
542
            break;
543
412
        case 0x11111100:
544
412
            if (s->rgb)
545
205
                s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_BGR48;
546
            else {
547
207
                if (   s->adobe_transform == 0
548

207
                    || s->component_id[0] == 'R' - 1 && s->component_id[1] == 'G' - 1 && s->component_id[2] == 'B' - 1) {
549
                    s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_GBRP : AV_PIX_FMT_GBRP16;
550
                } else {
551

207
                    if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
552
                    else              s->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
553
207
                s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
554
                }
555
            }
556
412
            av_assert0(s->nb_components == 3);
557
412
            break;
558
        case 0x11111111:
559
            if (s->rgb)
560
                s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA64;
561
            else {
562
                if (s->adobe_transform == 0 && s->bits <= 8) {
563
                    s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
564
                } else {
565
                    s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_YUVA444P : AV_PIX_FMT_YUVA444P16;
566
                    s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
567
                }
568
            }
569
            av_assert0(s->nb_components == 4);
570
            break;
571
        case 0x22111122:
572
        case 0x22111111:
573
            if (s->adobe_transform == 0 && s->bits <= 8) {
574
                s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
575
                s->upscale_v[1] = s->upscale_v[2] = 1;
576
                s->upscale_h[1] = s->upscale_h[2] = 1;
577
            } else if (s->adobe_transform == 2 && s->bits <= 8) {
578
                s->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
579
                s->upscale_v[1] = s->upscale_v[2] = 1;
580
                s->upscale_h[1] = s->upscale_h[2] = 1;
581
                s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
582
            } else {
583
                if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
584
                else              s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16;
585
                s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
586
            }
587
            av_assert0(s->nb_components == 4);
588
            break;
589
        case 0x12121100:
590
        case 0x22122100:
591
        case 0x21211100:
592
        case 0x22211200:
593
        case 0x22221100:
594
        case 0x22112200:
595
        case 0x11222200:
596
            if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
597
            else
598
                goto unk_pixfmt;
599
            s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
600
            break;
601
2
        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
            if(s->bits <= 8)
611
                s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
612
            else
613
2
                s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
614
2
            break;
615
        case 0x12111100:
616
        case 0x14121200:
617
        case 0x14111100:
618
        case 0x22211100:
619
        case 0x22112100:
620
            if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
621
                if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
622
                else
623
                    goto unk_pixfmt;
624
                s->upscale_v[0] = s->upscale_v[1] = 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
232
        case 0x21111100:
635

232
            if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
636
                if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
637
                else
638
                    goto unk_pixfmt;
639
                s->upscale_h[0] = s->upscale_h[1] = 1;
640
            } else {
641

232
                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
232
                s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
644
            }
645
232
            break;
646
        case 0x31111100:
647
            if (s->bits > 8)
648
                goto unk_pixfmt;
649
            s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
650
            s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
651
            s->upscale_h[1] = s->upscale_h[2] = 2;
652
            break;
653
        case 0x22121100:
654
        case 0x22111200:
655
            if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
656
            else
657
                goto unk_pixfmt;
658
            s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
659
            break;
660
1890
        case 0x22111100:
661
        case 0x23111100:
662
        case 0x42111100:
663
        case 0x24111100:
664

1890
            if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
665
            else              s->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
666
1890
            s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
667
1890
            if (pix_fmt_id == 0x42111100) {
668
                if (s->bits > 8)
669
                    goto unk_pixfmt;
670
                s->upscale_h[1] = s->upscale_h[2] = 1;
671
1890
            } else if (pix_fmt_id == 0x24111100) {
672
                if (s->bits > 8)
673
                    goto unk_pixfmt;
674
                s->upscale_v[1] = s->upscale_v[2] = 1;
675
1890
            } else if (pix_fmt_id == 0x23111100) {
676
                if (s->bits > 8)
677
                    goto unk_pixfmt;
678
                s->upscale_v[1] = s->upscale_v[2] = 2;
679
            }
680
1890
            break;
681
        case 0x41111100:
682
            if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV411P : AV_PIX_FMT_YUVJ411P;
683
            else
684
                goto unk_pixfmt;
685
            s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
686
            break;
687
        default:
688
    unk_pixfmt:
689
            avpriv_report_missing_feature(s->avctx, "Pixel format 0x%x bits:%d", pix_fmt_id, s->bits);
690
            memset(s->upscale_h, 0, sizeof(s->upscale_h));
691
            memset(s->upscale_v, 0, sizeof(s->upscale_v));
692
            return AVERROR_PATCHWELCOME;
693
        }
694

2536
        if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->avctx->lowres) {
695
            avpriv_report_missing_feature(s->avctx, "Lowres for weird subsampling");
696
            return AVERROR_PATCHWELCOME;
697
        }
698


2536
        if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->progressive && s->avctx->pix_fmt == AV_PIX_FMT_GBRP) {
699
            avpriv_report_missing_feature(s->avctx, "progressive for weird subsampling");
700
            return AVERROR_PATCHWELCOME;
701
        }
702
2536
        if (s->ls) {
703
205
            memset(s->upscale_h, 0, sizeof(s->upscale_h));
704
205
            memset(s->upscale_v, 0, sizeof(s->upscale_v));
705
205
            if (s->nb_components == 3) {
706
205
                s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
707
            } else if (s->nb_components != 1) {
708
                av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of components %d\n", s->nb_components);
709
                return AVERROR_PATCHWELCOME;
710
            } else if (s->palette_index && s->bits <= 8)
711
                s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
712
            else if (s->bits <= 8)
713
                s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
714
            else
715
                s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
716
        }
717
718
2536
        s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
719
2536
        if (!s->pix_desc) {
720
            av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
721
            return AVERROR_BUG;
722
        }
723
724

2536
        if (s->avctx->pix_fmt == s->hwaccel_sw_pix_fmt && !size_change) {
725
2207
            s->avctx->pix_fmt = s->hwaccel_pix_fmt;
726
        } else {
727
329
            enum AVPixelFormat pix_fmts[] = {
728
#if CONFIG_MJPEG_NVDEC_HWACCEL
729
                AV_PIX_FMT_CUDA,
730
#endif
731
#if CONFIG_MJPEG_VAAPI_HWACCEL
732
                AV_PIX_FMT_VAAPI,
733
#endif
734
329
                s->avctx->pix_fmt,
735
                AV_PIX_FMT_NONE,
736
            };
737
329
            s->hwaccel_pix_fmt = ff_get_format(s->avctx, pix_fmts);
738
329
            if (s->hwaccel_pix_fmt < 0)
739
                return AVERROR(EINVAL);
740
741
329
            s->hwaccel_sw_pix_fmt = s->avctx->pix_fmt;
742
329
            s->avctx->pix_fmt     = s->hwaccel_pix_fmt;
743
        }
744
745
2536
        if (s->avctx->skip_frame == AVDISCARD_ALL) {
746
55
            s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
747
55
            s->picture_ptr->key_frame = 1;
748
55
            s->got_picture            = 1;
749
55
            return 0;
750
        }
751
752
2481
        av_frame_unref(s->picture_ptr);
753
2481
        if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0)
754
            return -1;
755
2481
        s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
756
2481
        s->picture_ptr->key_frame = 1;
757
2481
        s->got_picture            = 1;
758
759
12405
        for (i = 0; i < 4; i++)
760
9924
            s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
761
762
        ff_dlog(s->avctx, "%d %d %d %d %d %d\n",
763
                s->width, s->height, s->linesize[0], s->linesize[1],
764
                s->interlaced, s->avctx->height);
765
766
    }
767
768

2496
    if ((s->rgb && !s->lossless && !s->ls) ||
769

2496
        (!s->rgb && s->ls && s->nb_components > 1) ||
770

2496
        (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 && !s->ls)
771
    ) {
772
        av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
773
        return AVERROR_PATCHWELCOME;
774
    }
775
776
    /* totally blank picture as progressive JPEG will only add details to it */
777
2496
    if (s->progressive) {
778
        int bw = (width  + s->h_max * 8 - 1) / (s->h_max * 8);
779
        int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
780
        for (i = 0; i < s->nb_components; i++) {
781
            int size = bw * bh * s->h_count[i] * s->v_count[i];
782
            av_freep(&s->blocks[i]);
783
            av_freep(&s->last_nnz[i]);
784
            s->blocks[i]       = av_mallocz_array(size, sizeof(**s->blocks));
785
            s->last_nnz[i]     = av_mallocz_array(size, sizeof(**s->last_nnz));
786
            if (!s->blocks[i] || !s->last_nnz[i])
787
                return AVERROR(ENOMEM);
788
            s->block_stride[i] = bw * s->h_count[i];
789
        }
790
        memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
791
    }
792
793
2496
    if (s->avctx->hwaccel) {
794
        s->hwaccel_picture_private =
795
            av_mallocz(s->avctx->hwaccel->frame_priv_data_size);
796
        if (!s->hwaccel_picture_private)
797
            return AVERROR(ENOMEM);
798
799
        ret = s->avctx->hwaccel->start_frame(s->avctx, s->raw_image_buffer,
800
                                             s->raw_image_buffer_size);
801
        if (ret < 0)
802
            return ret;
803
    }
804
805
2496
    return 0;
806
}
807
808
26782336
static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
809
{
810
    int code;
811
26782336
    code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
812

26782336
    if (code < 0 || code > 16) {
813
        av_log(s->avctx, AV_LOG_WARNING,
814
               "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
815
               0, dc_index, &s->vlcs[0][dc_index]);
816
        return 0xfffff;
817
    }
818
819
26782336
    if (code)
820
23751174
        return get_xbits(&s->gb, code);
821
    else
822
3031162
        return 0;
823
}
824
825
/* decode block and dequantize */
826
3886036
static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
827
                        int dc_index, int ac_index, uint16_t *quant_matrix)
828
{
829
    int code, i, j, level, val;
830
831
    /* DC coef */
832
3886036
    val = mjpeg_decode_dc(s, dc_index);
833
3886036
    if (val == 0xfffff) {
834
        av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
835
        return AVERROR_INVALIDDATA;
836
    }
837
3886036
    val = val * (unsigned)quant_matrix[0] + s->last_dc[component];
838
3886036
    val = av_clip_int16(val);
839
3886036
    s->last_dc[component] = val;
840
3886036
    block[0] = val;
841
    /* AC coefs */
842
3886036
    i = 0;
843
3886036
    {OPEN_READER(re, &s->gb);
844
    do {
845
35997167
        UPDATE_CACHE(re, &s->gb);
846
35997167
        GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
847
848
35997167
        i += ((unsigned)code) >> 4;
849
35997167
            code &= 0xf;
850
35997167
        if (code) {
851
32161844
            if (code > MIN_CACHE_BITS - 16)
852
291
                UPDATE_CACHE(re, &s->gb);
853
854
            {
855
32161844
                int cache = GET_CACHE(re, &s->gb);
856
32161844
                int sign  = (~cache) >> 31;
857
32161844
                level     = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
858
            }
859
860
32161844
            LAST_SKIP_BITS(re, &s->gb, code);
861
862
32161844
            if (i > 63) {
863
                av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
864
                return AVERROR_INVALIDDATA;
865
            }
866
32161844
            j        = s->scantable.permutated[i];
867
32161844
            block[j] = level * quant_matrix[i];
868
        }
869
35997167
    } while (i < 63);
870
3886036
    CLOSE_READER(re, &s->gb);}
871
872
3886036
    return 0;
873
}
874
875
static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
876
                                 int component, int dc_index,
877
                                 uint16_t *quant_matrix, int Al)
878
{
879
    unsigned val;
880
    s->bdsp.clear_block(block);
881
    val = mjpeg_decode_dc(s, dc_index);
882
    if (val == 0xfffff) {
883
        av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
884
        return AVERROR_INVALIDDATA;
885
    }
886
    val = (val * (quant_matrix[0] << Al)) + s->last_dc[component];
887
    s->last_dc[component] = val;
888
    block[0] = val;
889
    return 0;
890
}
891
892
/* decode block and dequantize - progressive JPEG version */
893
static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
894
                                    uint8_t *last_nnz, int ac_index,
895
                                    uint16_t *quant_matrix,
896
                                    int ss, int se, int Al, int *EOBRUN)
897
{
898
    int code, i, j, val, run;
899
    unsigned level;
900
901
    if (*EOBRUN) {
902
        (*EOBRUN)--;
903
        return 0;
904
    }
905
906
    {
907
        OPEN_READER(re, &s->gb);
908
        for (i = ss; ; i++) {
909
            UPDATE_CACHE(re, &s->gb);
910
            GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
911
912
            run = ((unsigned) code) >> 4;
913
            code &= 0xF;
914
            if (code) {
915
                i += run;
916
                if (code > MIN_CACHE_BITS - 16)
917
                    UPDATE_CACHE(re, &s->gb);
918
919
                {
920
                    int cache = GET_CACHE(re, &s->gb);
921
                    int sign  = (~cache) >> 31;
922
                    level     = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
923
                }
924
925
                LAST_SKIP_BITS(re, &s->gb, code);
926
927
                if (i >= se) {
928
                    if (i == se) {
929
                        j = s->scantable.permutated[se];
930
                        block[j] = level * (quant_matrix[se] << Al);
931
                        break;
932
                    }
933
                    av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
934
                    return AVERROR_INVALIDDATA;
935
                }
936
                j = s->scantable.permutated[i];
937
                block[j] = level * (quant_matrix[i] << Al);
938
            } else {
939
                if (run == 0xF) {// ZRL - skip 15 coefficients
940
                    i += 15;
941
                    if (i >= se) {
942
                        av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
943
                        return AVERROR_INVALIDDATA;
944
                    }
945
                } else {
946
                    val = (1 << run);
947
                    if (run) {
948
                        UPDATE_CACHE(re, &s->gb);
949
                        val += NEG_USR32(GET_CACHE(re, &s->gb), run);
950
                        LAST_SKIP_BITS(re, &s->gb, run);
951
                    }
952
                    *EOBRUN = val - 1;
953
                    break;
954
                }
955
            }
956
        }
957
        CLOSE_READER(re, &s->gb);
958
    }
959
960
    if (i > *last_nnz)
961
        *last_nnz = i;
962
963
    return 0;
964
}
965
966
#define REFINE_BIT(j) {                                             \
967
    UPDATE_CACHE(re, &s->gb);                                       \
968
    sign = block[j] >> 15;                                          \
969
    block[j] += SHOW_UBITS(re, &s->gb, 1) *                         \
970
                ((quant_matrix[i] ^ sign) - sign) << Al;            \
971
    LAST_SKIP_BITS(re, &s->gb, 1);                                  \
972
}
973
974
#define ZERO_RUN                                                    \
975
for (; ; i++) {                                                     \
976
    if (i > last) {                                                 \
977
        i += run;                                                   \
978
        if (i > se) {                                               \
979
            av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
980
            return -1;                                              \
981
        }                                                           \
982
        break;                                                      \
983
    }                                                               \
984
    j = s->scantable.permutated[i];                                 \
985
    if (block[j])                                                   \
986
        REFINE_BIT(j)                                               \
987
    else if (run-- == 0)                                            \
988
        break;                                                      \
989
}
990
991
/* decode block and dequantize - progressive JPEG refinement pass */
992
static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
993
                                   uint8_t *last_nnz,
994
                                   int ac_index, uint16_t *quant_matrix,
995
                                   int ss, int se, int Al, int *EOBRUN)
996
{
997
    int code, i = ss, j, sign, val, run;
998
    int last    = FFMIN(se, *last_nnz);
999
1000
    OPEN_READER(re, &s->gb);
1001
    if (*EOBRUN) {
1002
        (*EOBRUN)--;
1003
    } else {
1004
        for (; ; i++) {
1005
            UPDATE_CACHE(re, &s->gb);
1006
            GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
1007
1008
            if (code & 0xF) {
1009
                run = ((unsigned) code) >> 4;
1010
                UPDATE_CACHE(re, &s->gb);
1011
                val = SHOW_UBITS(re, &s->gb, 1);
1012
                LAST_SKIP_BITS(re, &s->gb, 1);
1013
                ZERO_RUN;
1014
                j = s->scantable.permutated[i];
1015
                val--;
1016
                block[j] = ((quant_matrix[i] << Al) ^ val) - val;
1017
                if (i == se) {
1018
                    if (i > *last_nnz)
1019
                        *last_nnz = i;
1020
                    CLOSE_READER(re, &s->gb);
1021
                    return 0;
1022
                }
1023
            } else {
1024
                run = ((unsigned) code) >> 4;
1025
                if (run == 0xF) {
1026
                    ZERO_RUN;
1027
                } else {
1028
                    val = run;
1029
                    run = (1 << run);
1030
                    if (val) {
1031
                        UPDATE_CACHE(re, &s->gb);
1032
                        run += SHOW_UBITS(re, &s->gb, val);
1033
                        LAST_SKIP_BITS(re, &s->gb, val);
1034
                    }
1035
                    *EOBRUN = run - 1;
1036
                    break;
1037
                }
1038
            }
1039
        }
1040
1041
        if (i > *last_nnz)
1042
            *last_nnz = i;
1043
    }
1044
1045
    for (; i <= last; i++) {
1046
        j = s->scantable.permutated[i];
1047
        if (block[j])
1048
            REFINE_BIT(j)
1049
    }
1050
    CLOSE_READER(re, &s->gb);
1051
1052
    return 0;
1053
}
1054
#undef REFINE_BIT
1055
#undef ZERO_RUN
1056
1057
772930
static int handle_rstn(MJpegDecodeContext *s, int nb_components)
1058
{
1059
    int i;
1060
772930
    int reset = 0;
1061
1062
772930
    if (s->restart_interval) {
1063
        s->restart_count--;
1064
        if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
1065
            align_get_bits(&s->gb);
1066
            for (i = 0; i < nb_components; i++) /* reset dc */
1067
                s->last_dc[i] = (4 << s->bits);
1068
        }
1069
1070
        i = 8 + ((-get_bits_count(&s->gb)) & 7);
1071
        /* skip RSTn */
1072
        if (s->restart_count == 0) {
1073
            if(   show_bits(&s->gb, i) == (1 << i) - 1
1074
               || show_bits(&s->gb, i) == 0xFF) {
1075
                int pos = get_bits_count(&s->gb);
1076
                align_get_bits(&s->gb);
1077
                while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
1078
                    skip_bits(&s->gb, 8);
1079
                if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
1080
                    for (i = 0; i < nb_components; i++) /* reset dc */
1081
                        s->last_dc[i] = (4 << s->bits);
1082
                    reset = 1;
1083
                } else
1084
                    skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
1085
            }
1086
        }
1087
    }
1088
772930
    return reset;
1089
}
1090
1091
/* Handles 1 to 4 components */
1092
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
1093
{
1094
    int i, mb_x, mb_y;
1095
    unsigned width;
1096
    uint16_t (*buffer)[4];
1097
    int left[4], top[4], topleft[4];
1098
    const int linesize = s->linesize[0];
1099
    const int mask     = ((1 << s->bits) - 1) << point_transform;
1100
    int resync_mb_y = 0;
1101
    int resync_mb_x = 0;
1102
    int vpred[6];
1103
1104
    if (!s->bayer && s->nb_components < 3)
1105
        return AVERROR_INVALIDDATA;
1106
    if (s->bayer && s->nb_components > 2)
1107
        return AVERROR_INVALIDDATA;
1108
    if (s->nb_components <= 0 || s->nb_components > 4)
1109
        return AVERROR_INVALIDDATA;
1110
    if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
1111
        return AVERROR_INVALIDDATA;
1112
1113
1114
    s->restart_count = s->restart_interval;
1115
1116
    if (s->restart_interval == 0)
1117
        s->restart_interval = INT_MAX;
1118
1119
    if (s->bayer)
1120
        width = s->mb_width / nb_components; /* Interleaved, width stored is the total so need to divide */
1121
    else
1122
        width = s->mb_width;
1123
1124
    av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * sizeof(s->ljpeg_buffer[0][0]));
1125
    if (!s->ljpeg_buffer)
1126
        return AVERROR(ENOMEM);
1127
1128
    buffer = s->ljpeg_buffer;
1129
1130
    for (i = 0; i < 4; i++)
1131
        buffer[0][i] = 1 << (s->bits - 1);
1132
1133
    for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1134
        uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
1135
1136
        if (s->interlaced && s->bottom_field)
1137
            ptr += linesize >> 1;
1138
1139
        for (i = 0; i < 4; i++)
1140
            top[i] = left[i] = topleft[i] = buffer[0][i];
1141
1142
        if ((mb_y * s->width) % s->restart_interval == 0) {
1143
            for (i = 0; i < 6; i++)
1144
                vpred[i] = 1 << (s->bits-1);
1145
        }
1146
1147
        for (mb_x = 0; mb_x < width; mb_x++) {
1148
            int modified_predictor = predictor;
1149
1150
            if (get_bits_left(&s->gb) < 1) {
1151
                av_log(s->avctx, AV_LOG_ERROR, "bitstream end in rgb_scan\n");
1152
                return AVERROR_INVALIDDATA;
1153
            }
1154
1155
            if (s->restart_interval && !s->restart_count){
1156
                s->restart_count = s->restart_interval;
1157
                resync_mb_x = mb_x;
1158
                resync_mb_y = mb_y;
1159
                for(i=0; i<4; i++)
1160
                    top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
1161
            }
1162
            if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
1163
                modified_predictor = 1;
1164
1165
            for (i=0;i<nb_components;i++) {
1166
                int pred, dc;
1167
1168
                topleft[i] = top[i];
1169
                top[i]     = buffer[mb_x][i];
1170
1171
                dc = mjpeg_decode_dc(s, s->dc_index[i]);
1172
                if(dc == 0xFFFFF)
1173
                    return -1;
1174
1175
                if (!s->bayer || mb_x) {
1176
                    pred = left[i];
1177
                } else { /* This path runs only for the first line in bayer images */
1178
                    vpred[i] += dc;
1179
                    pred = vpred[i] - dc;
1180
                }
1181
1182
                PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
1183
1184
                left[i] = buffer[mb_x][i] =
1185
                    mask & (pred + (unsigned)(dc * (1 << point_transform)));
1186
            }
1187
1188
            if (s->restart_interval && !--s->restart_count) {
1189
                align_get_bits(&s->gb);
1190
                skip_bits(&s->gb, 16); /* skip RSTn */
1191
            }
1192
        }
1193
        if (s->rct && s->nb_components == 4) {
1194
            for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1195
                ptr[4*mb_x + 2] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1196
                ptr[4*mb_x + 1] = buffer[mb_x][1] + ptr[4*mb_x + 2];
1197
                ptr[4*mb_x + 3] = buffer[mb_x][2] + ptr[4*mb_x + 2];
1198
                ptr[4*mb_x + 0] = buffer[mb_x][3];
1199
            }
1200
        } else if (s->nb_components == 4) {
1201
            for(i=0; i<nb_components; i++) {
1202
                int c= s->comp_index[i];
1203
                if (s->bits <= 8) {
1204
                    for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1205
                        ptr[4*mb_x+3-c] = buffer[mb_x][i];
1206
                    }
1207
                } else if(s->bits == 9) {
1208
                    return AVERROR_PATCHWELCOME;
1209
                } else {
1210
                    for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1211
                        ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i];
1212
                    }
1213
                }
1214
            }
1215
        } else if (s->rct) {
1216
            for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1217
                ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1218
                ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1219
                ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1220
            }
1221
        } else if (s->pegasus_rct) {
1222
            for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1223
                ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
1224
                ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1225
                ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1226
            }
1227
        } else if (s->bayer) {
1228
            if (nb_components == 1) {
1229
                /* Leave decoding to the TIFF/DNG decoder (see comment in ff_mjpeg_decode_sof) */
1230
                for (mb_x = 0; mb_x < width; mb_x++)
1231
                    ((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
1232
            } else if (nb_components == 2) {
1233
                for (mb_x = 0; mb_x < width; mb_x++) {
1234
                    ((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
1235
                    ((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
1236
                }
1237
            }
1238
        } else {
1239
            for(i=0; i<nb_components; i++) {
1240
                int c= s->comp_index[i];
1241
                if (s->bits <= 8) {
1242
                    for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1243
                        ptr[3*mb_x+2-c] = buffer[mb_x][i];
1244
                    }
1245
                } else if(s->bits == 9) {
1246
                    return AVERROR_PATCHWELCOME;
1247
                } else {
1248
                    for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1249
                        ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i];
1250
                    }
1251
                }
1252
            }
1253
        }
1254
    }
1255
    return 0;
1256
}
1257
1258
200
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
1259
                                 int point_transform, int nb_components)
1260
{
1261
    int i, mb_x, mb_y, mask;
1262
200
    int bits= (s->bits+7)&~7;
1263
200
    int resync_mb_y = 0;
1264
200
    int resync_mb_x = 0;
1265
1266
200
    point_transform += bits - s->bits;
1267
200
    mask = ((1 << s->bits) - 1) << point_transform;
1268
1269

200
    av_assert0(nb_components>=1 && nb_components<=4);
1270
1271
22650
    for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1272
3838500
        for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1273
3816050
            if (get_bits_left(&s->gb) < 1) {
1274
                av_log(s->avctx, AV_LOG_ERROR, "bitstream end in yuv_scan\n");
1275
                return AVERROR_INVALIDDATA;
1276
            }
1277

3816050
            if (s->restart_interval && !s->restart_count){
1278
                s->restart_count = s->restart_interval;
1279
                resync_mb_x = mb_x;
1280
                resync_mb_y = mb_y;
1281
            }
1282
1283


3865550
            if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
1284

49500
                int toprow  = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1285

49500
                int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1286
198000
                for (i = 0; i < nb_components; i++) {
1287
                    uint8_t *ptr;
1288
                    uint16_t *ptr16;
1289
                    int n, h, v, x, y, c, j, linesize;
1290
148500
                    n = s->nb_blocks[i];
1291
148500
                    c = s->comp_index[i];
1292
148500
                    h = s->h_scount[i];
1293
148500
                    v = s->v_scount[i];
1294
148500
                    x = 0;
1295
148500
                    y = 0;
1296
148500
                    linesize= s->linesize[c];
1297
1298
148500
                    if(bits>8) linesize /= 2;
1299
1300
445500
                    for(j=0; j<n; j++) {
1301
                        int pred, dc;
1302
1303
297000
                        dc = mjpeg_decode_dc(s, s->dc_index[i]);
1304
297000
                        if(dc == 0xFFFFF)
1305
                            return -1;
1306
297000
                        if (   h * mb_x + x >= s->width
1307
297000
                            || v * mb_y + y >= s->height) {
1308
                            // Nothing to do
1309
297000
                        } else if (bits<=8) {
1310
297000
                            ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
1311

297000
                            if(y==0 && toprow){
1312

109000
                                if(x==0 && leftcol){
1313
600
                                    pred= 1 << (bits - 1);
1314
                                }else{
1315
108400
                                    pred= ptr[-1];
1316
                                }
1317
                            }else{
1318

188000
                                if(x==0 && leftcol){
1319
89200
                                    pred= ptr[-linesize];
1320
                                }else{
1321


98800
                                    PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1322
                                }
1323
                            }
1324
1325

297000
                            if (s->interlaced && s->bottom_field)
1326
                                ptr += linesize >> 1;
1327
297000
                            pred &= mask;
1328
297000
                            *ptr= pred + ((unsigned)dc << point_transform);
1329
                        }else{
1330
                            ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1331
                            if(y==0 && toprow){
1332
                                if(x==0 && leftcol){
1333
                                    pred= 1 << (bits - 1);
1334
                                }else{
1335
                                    pred= ptr16[-1];
1336
                                }
1337
                            }else{
1338
                                if(x==0 && leftcol){
1339
                                    pred= ptr16[-linesize];
1340
                                }else{
1341
                                    PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1342
                                }
1343
                            }
1344
1345
                            if (s->interlaced && s->bottom_field)
1346
                                ptr16 += linesize >> 1;
1347
                            pred &= mask;
1348
                            *ptr16= pred + ((unsigned)dc << point_transform);
1349
                        }
1350
297000
                        if (++x == h) {
1351
198000
                            x = 0;
1352
198000
                            y++;
1353
                        }
1354
                    }
1355
                }
1356
            } else {
1357
15066200
                for (i = 0; i < nb_components; i++) {
1358
                    uint8_t *ptr;
1359
                    uint16_t *ptr16;
1360
                    int n, h, v, x, y, c, j, linesize, dc;
1361
11299650
                    n        = s->nb_blocks[i];
1362
11299650
                    c        = s->comp_index[i];
1363
11299650
                    h        = s->h_scount[i];
1364
11299650
                    v        = s->v_scount[i];
1365
11299650
                    x        = 0;
1366
11299650
                    y        = 0;
1367
11299650
                    linesize = s->linesize[c];
1368
1369
11299650
                    if(bits>8) linesize /= 2;
1370
1371
33898950
                    for (j = 0; j < n; j++) {
1372
                        int pred;
1373
1374
22599300
                        dc = mjpeg_decode_dc(s, s->dc_index[i]);
1375
22599300
                        if(dc == 0xFFFFF)
1376
                            return -1;
1377
22599300
                        if (   h * mb_x + x >= s->width
1378
22599300
                            || v * mb_y + y >= s->height) {
1379
                            // Nothing to do
1380
22599300
                        } else if (bits<=8) {
1381
22599300
                            ptr = s->picture_ptr->data[c] +
1382
22599300
                              (linesize * (v * mb_y + y)) +
1383
22599300
                              (h * mb_x + x); //FIXME optimize this crap
1384


22599300
                            PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1385
1386
22599300
                            pred &= mask;
1387
22599300
                            *ptr = pred + ((unsigned)dc << point_transform);
1388
                        }else{
1389
                            ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1390
                            PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1391
1392
                            pred &= mask;
1393
                            *ptr16= pred + ((unsigned)dc << point_transform);
1394
                        }
1395
1396
22599300
                        if (++x == h) {
1397
15066200
                            x = 0;
1398
15066200
                            y++;
1399
                        }
1400
                    }
1401
                }
1402
            }
1403

3816050
            if (s->restart_interval && !--s->restart_count) {
1404
                align_get_bits(&s->gb);
1405
                skip_bits(&s->gb, 16); /* skip RSTn */
1406
            }
1407
        }
1408
    }
1409
200
    return 0;
1410
}
1411
1412
786684
static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s,
1413
                                              uint8_t *dst, const uint8_t *src,
1414
                                              int linesize, int lowres)
1415
{
1416

786684
    switch (lowres) {
1417
786684
    case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
1418
786684
        break;
1419
    case 1: copy_block4(dst, src, linesize, linesize, 4);
1420
        break;
1421
    case 2: copy_block2(dst, src, linesize, linesize, 2);
1422
        break;
1423
    case 3: *dst = *src;
1424
        break;
1425
    }
1426
786684
}
1427
1428
11750
static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1429
{
1430
    int block_x, block_y;
1431
11750
    int size = 8 >> s->avctx->lowres;
1432
11750
    if (s->bits > 8) {
1433
105750
        for (block_y=0; block_y<size; block_y++)
1434
846000
            for (block_x=0; block_x<size; block_x++)
1435
752000
                *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits;
1436
    } else {
1437
        for (block_y=0; block_y<size; block_y++)
1438
            for (block_x=0; block_x<size; block_x++)
1439
                *(ptr + block_x + block_y*linesize) <<= 8 - s->bits;
1440
    }
1441
11750
}
1442
1443
2119
static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1444
                             int Al, const uint8_t *mb_bitmask,
1445
                             int mb_bitmask_size,
1446
                             const AVFrame *reference)
1447
{
1448
    int i, mb_x, mb_y, chroma_h_shift, chroma_v_shift, chroma_width, chroma_height;
1449
    uint8_t *data[MAX_COMPONENTS];
1450
    const uint8_t *reference_data[MAX_COMPONENTS];
1451
    int linesize[MAX_COMPONENTS];
1452
2119
    GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1453
2119
    int bytes_per_pixel = 1 + (s->bits > 8);
1454
1455
2119
    if (mb_bitmask) {
1456
31
        if (mb_bitmask_size != (s->mb_width * s->mb_height + 7)>>3) {
1457
            av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1458
            return AVERROR_INVALIDDATA;
1459
        }
1460
31
        init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1461
    }
1462
1463
2119
    s->restart_count = 0;
1464
1465
2119
    av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &chroma_h_shift,
1466
                                     &chroma_v_shift);
1467
2119
    chroma_width  = AV_CEIL_RSHIFT(s->width,  chroma_h_shift);
1468
2119
    chroma_height = AV_CEIL_RSHIFT(s->height, chroma_v_shift);
1469
1470
8474
    for (i = 0; i < nb_components; i++) {
1471
6355
        int c   = s->comp_index[i];
1472
6355
        data[c] = s->picture_ptr->data[c];
1473
6355
        reference_data[c] = reference ? reference->data[c] : NULL;
1474
6355
        linesize[c] = s->linesize[c];
1475
6355
        s->coefs_finished[c] |= 1;
1476
    }
1477
1478
30300
    for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1479
801111
        for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1480

772930
            const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1481
1482

772930
            if (s->restart_interval && !s->restart_count)
1483
                s->restart_count = s->restart_interval;
1484
1485
772930
            if (get_bits_left(&s->gb) < 0) {
1486
                av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1487
                       -get_bits_left(&s->gb));
1488
                return AVERROR_INVALIDDATA;
1489
            }
1490
3068220
            for (i = 0; i < nb_components; i++) {
1491
                uint8_t *ptr;
1492
                int n, h, v, x, y, c, j;
1493
                int block_offset;
1494
2295290
                n = s->nb_blocks[i];
1495
2295290
                c = s->comp_index[i];
1496
2295290
                h = s->h_scount[i];
1497
2295290
                v = s->v_scount[i];
1498
2295290
                x = 0;
1499
2295290
                y = 0;
1500
6968010
                for (j = 0; j < n; j++) {
1501
4672720
                    block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1502
4672720
                                     (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1503
1504

4672720
                    if (s->interlaced && s->bottom_field)
1505
10720
                        block_offset += linesize[c] >> 1;
1506

4672720
                    if (   8*(h * mb_x + x) < ((c == 1) || (c == 2) ? chroma_width  : s->width)
1507

4670692
                        && 8*(v * mb_y + y) < ((c == 1) || (c == 2) ? chroma_height : s->height)) {
1508
4664772
                        ptr = data[c] + block_offset;
1509
                    } else
1510
7948
                        ptr = NULL;
1511
4672720
                    if (!s->progressive) {
1512
4672720
                        if (copy_mb) {
1513
786684
                            if (ptr)
1514
786684
                                mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1515
786684
                                                linesize[c], s->avctx->lowres);
1516
1517
                        } else {
1518
3886036
                            s->bdsp.clear_block(s->block);
1519
3886036
                            if (decode_block(s, s->block, i,
1520
                                             s->dc_index[i], s->ac_index[i],
1521
3886036
                                             s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1522
                                av_log(s->avctx, AV_LOG_ERROR,
1523
                                       "error y=%d x=%d\n", mb_y, mb_x);
1524
                                return AVERROR_INVALIDDATA;
1525
                            }
1526
3886036
                            if (ptr) {
1527
3878088
                                s->idsp.idct_put(ptr, linesize[c], s->block);
1528
3878088
                                if (s->bits & 7)
1529
11750
                                    shift_output(s, ptr, linesize[c]);
1530
                            }
1531
                        }
1532
                    } else {
1533
                        int block_idx  = s->block_stride[c] * (v * mb_y + y) +
1534
                                         (h * mb_x + x);
1535
                        int16_t *block = s->blocks[c][block_idx];
1536
                        if (Ah)
1537
                            block[0] += get_bits1(&s->gb) *
1538
                                        s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1539
                        else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1540
                                                       s->quant_matrixes[s->quant_sindex[i]],
1541
                                                       Al) < 0) {
1542
                            av_log(s->avctx, AV_LOG_ERROR,
1543
                                   "error y=%d x=%d\n", mb_y, mb_x);
1544
                            return AVERROR_INVALIDDATA;
1545
                        }
1546
                    }
1547
                    ff_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1548
                    ff_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1549
                            mb_x, mb_y, x, y, c, s->bottom_field,
1550
                            (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1551
4672720
                    if (++x == h) {
1552
3402365
                        x = 0;
1553
3402365
                        y++;
1554
                    }
1555
                }
1556
            }
1557
1558
772930
            handle_rstn(s, nb_components);
1559
        }
1560
    }
1561
2119
    return 0;
1562
}
1563
1564
static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
1565
                                            int se, int Ah, int Al)
1566
{
1567
    int mb_x, mb_y;
1568
    int EOBRUN = 0;
1569
    int c = s->comp_index[0];
1570
    uint16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1571
1572
    av_assert0(ss>=0 && Ah>=0 && Al>=0);
1573
    if (se < ss || se > 63) {
1574
        av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se);
1575
        return AVERROR_INVALIDDATA;
1576
    }
1577
1578
    // s->coefs_finished is a bitmask for coefficients coded
1579
    // ss and se are parameters telling start and end coefficients
1580
    s->coefs_finished[c] |= (2ULL << se) - (1ULL << ss);
1581
1582
    s->restart_count = 0;
1583
1584
    for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1585
        int block_idx    = mb_y * s->block_stride[c];
1586
        int16_t (*block)[64] = &s->blocks[c][block_idx];
1587
        uint8_t *last_nnz    = &s->last_nnz[c][block_idx];
1588
        if (get_bits_left(&s->gb) <= 0) {
1589
            av_log(s->avctx, AV_LOG_ERROR, "bitstream truncated in mjpeg_decode_scan_progressive_ac\n");
1590
            return AVERROR_INVALIDDATA;
1591
        }
1592
        for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1593
                int ret;
1594
                if (s->restart_interval && !s->restart_count)
1595
                    s->restart_count = s->restart_interval;
1596
1597
                if (Ah)
1598
                    ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1599
                                                  quant_matrix, ss, se, Al, &EOBRUN);
1600
                else
1601
                    ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1602
                                                   quant_matrix, ss, se, Al, &EOBRUN);
1603
                if (ret < 0) {
1604
                    av_log(s->avctx, AV_LOG_ERROR,
1605
                           "error y=%d x=%d\n", mb_y, mb_x);
1606
                    return AVERROR_INVALIDDATA;
1607
                }
1608
1609
            if (handle_rstn(s, 0))
1610
                EOBRUN = 0;
1611
        }
1612
    }
1613
    return 0;
1614
}
1615
1616
static void mjpeg_idct_scan_progressive_ac(MJpegDecodeContext *s)
1617
{
1618
    int mb_x, mb_y;
1619
    int c;
1620
    const int bytes_per_pixel = 1 + (s->bits > 8);
1621
    const int block_size = s->lossless ? 1 : 8;
1622
1623
    for (c = 0; c < s->nb_components; c++) {
1624
        uint8_t *data = s->picture_ptr->data[c];
1625
        int linesize  = s->linesize[c];
1626
        int h = s->h_max / s->h_count[c];
1627
        int v = s->v_max / s->v_count[c];
1628
        int mb_width     = (s->width  + h * block_size - 1) / (h * block_size);
1629
        int mb_height    = (s->height + v * block_size - 1) / (v * block_size);
1630
1631
        if (~s->coefs_finished[c])
1632
            av_log(s->avctx, AV_LOG_WARNING, "component %d is incomplete\n", c);
1633
1634
        if (s->interlaced && s->bottom_field)
1635
            data += linesize >> 1;
1636
1637
        for (mb_y = 0; mb_y < mb_height; mb_y++) {
1638
            uint8_t *ptr     = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1639
            int block_idx    = mb_y * s->block_stride[c];
1640
            int16_t (*block)[64] = &s->blocks[c][block_idx];
1641
            for (mb_x = 0; mb_x < mb_width; mb_x++, block++) {
1642
                s->idsp.idct_put(ptr, linesize, *block);
1643
                if (s->bits & 7)
1644
                    shift_output(s, ptr, linesize);
1645
                ptr += bytes_per_pixel*8 >> s->avctx->lowres;
1646
            }
1647
        }
1648
    }
1649
}
1650
1651
2524
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
1652
                        int mb_bitmask_size, const AVFrame *reference)
1653
{
1654
    int len, nb_components, i, h, v, predictor, point_transform;
1655
    int index, id, ret;
1656
2524
    const int block_size = s->lossless ? 1 : 8;
1657
    int ilv, prev_shift;
1658
1659
2524
    if (!s->got_picture) {
1660
        av_log(s->avctx, AV_LOG_WARNING,
1661
                "Can not process SOS before SOF, skipping\n");
1662
        return -1;
1663
    }
1664
1665
2524
    if (reference) {
1666
31
        if (reference->width  != s->picture_ptr->width  ||
1667
31
            reference->height != s->picture_ptr->height ||
1668
31
            reference->format != s->picture_ptr->format) {
1669
            av_log(s->avctx, AV_LOG_ERROR, "Reference mismatching\n");
1670
            return AVERROR_INVALIDDATA;
1671
        }
1672
    }
1673
1674
    /* XXX: verify len field validity */
1675
2524
    len = get_bits(&s->gb, 16);
1676
2524
    nb_components = get_bits(&s->gb, 8);
1677

2524
    if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1678
        avpriv_report_missing_feature(s->avctx,
1679
                                      "decode_sos: nb_components (%d)",
1680
                                      nb_components);
1681
        return AVERROR_PATCHWELCOME;
1682
    }
1683
2524
    if (len != 6 + 2 * nb_components) {
1684
        av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1685
        return AVERROR_INVALIDDATA;
1686
    }
1687
10094
    for (i = 0; i < nb_components; i++) {
1688
7570
        id = get_bits(&s->gb, 8) - 1;
1689
7570
        av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1690
        /* find component index */
1691
15139
        for (index = 0; index < s->nb_components; index++)
1692
15139
            if (id == s->component_id[index])
1693
7570
                break;
1694
7570
        if (index == s->nb_components) {
1695
            av_log(s->avctx, AV_LOG_ERROR,
1696
                   "decode_sos: index(%d) out of components\n", index);
1697
            return AVERROR_INVALIDDATA;
1698
        }
1699
        /* Metasoft MJPEG codec has Cb and Cr swapped */
1700
7570
        if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1701
            && nb_components == 3 && s->nb_components == 3 && i)
1702
            index = 3 - i;
1703
1704
7570
        s->quant_sindex[i] = s->quant_index[index];
1705
7570
        s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1706
7570
        s->h_scount[i]  = s->h_count[index];
1707
7570
        s->v_scount[i]  = s->v_count[index];
1708
1709


7570
        if((nb_components == 1 || nb_components == 3) && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1710
            index = (index+2)%3;
1711
1712
7570
        s->comp_index[i] = index;
1713
1714
7570
        s->dc_index[i] = get_bits(&s->gb, 4);
1715
7570
        s->ac_index[i] = get_bits(&s->gb, 4);
1716
1717

7570
        if (s->dc_index[i] <  0 || s->ac_index[i] < 0 ||
1718

7570
            s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1719
            goto out_of_range;
1720


7570
        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))
1721
            goto out_of_range;
1722
    }
1723
1724
2524
    predictor = get_bits(&s->gb, 8);       /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1725
2524
    ilv = get_bits(&s->gb, 8);             /* JPEG Se / JPEG-LS ILV */
1726
2524
    if(s->avctx->codec_tag != AV_RL32("CJPG")){
1727
2524
        prev_shift      = get_bits(&s->gb, 4); /* Ah */
1728
2524
        point_transform = get_bits(&s->gb, 4); /* Al */
1729
    }else
1730
        prev_shift = point_transform = 0;
1731
1732
2524
    if (nb_components > 1) {
1733
        /* interleaved stream */
1734
2523
        s->mb_width  = (s->width  + s->h_max * block_size - 1) / (s->h_max * block_size);
1735
2523
        s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1736
1
    } else if (!s->ls) { /* skip this for JPEG-LS */
1737
1
        h = s->h_max / s->h_scount[0];
1738
1
        v = s->v_max / s->v_scount[0];
1739
1
        s->mb_width     = (s->width  + h * block_size - 1) / (h * block_size);
1740
1
        s->mb_height    = (s->height + v * block_size - 1) / (v * block_size);
1741
1
        s->nb_blocks[0] = 1;
1742
1
        s->h_scount[0]  = 1;
1743
1
        s->v_scount[0]  = 1;
1744
    }
1745
1746
2524
    if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1747
        av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1748
               s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1749
               predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1750
               s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1751
1752
1753
    /* mjpeg-b can have padding bytes between sos and image data, skip them */
1754
2546
    for (i = s->mjpb_skiptosod; i > 0; i--)
1755
22
        skip_bits(&s->gb, 8);
1756
1757
2524
next_field:
1758
10094
    for (i = 0; i < nb_components; i++)
1759
7570
        s->last_dc[i] = (4 << s->bits);
1760
1761
2524
    if (s->avctx->hwaccel) {
1762
        int bytes_to_start = get_bits_count(&s->gb) / 8;
1763
        av_assert0(bytes_to_start >= 0 &&
1764
                   s->raw_scan_buffer_size >= bytes_to_start);
1765
1766
        ret = s->avctx->hwaccel->decode_slice(s->avctx,
1767
                                              s->raw_scan_buffer      + bytes_to_start,
1768
                                              s->raw_scan_buffer_size - bytes_to_start);
1769
        if (ret < 0)
1770
            return ret;
1771
1772
2524
    } else if (s->lossless) {
1773
405
        av_assert0(s->picture_ptr == s->picture);
1774
405
        if (CONFIG_JPEGLS_DECODER && s->ls) {
1775
//            for () {
1776
//            reset_ls_coding_parameters(s, 0);
1777
1778
205
            if ((ret = ff_jpegls_decode_picture(s, predictor,
1779
                                                point_transform, ilv)) < 0)
1780
                return ret;
1781
        } else {
1782

200
            if (s->rgb || s->bayer) {
1783
                if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1784
                    return ret;
1785
            } else {
1786
200
                if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1787
                                                 point_transform,
1788
                                                 nb_components)) < 0)
1789
                    return ret;
1790
            }
1791
        }
1792
    } else {
1793

2119
        if (s->progressive && predictor) {
1794
            av_assert0(s->picture_ptr == s->picture);
1795
            if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1796
                                                        ilv, prev_shift,
1797
                                                        point_transform)) < 0)
1798
                return ret;
1799
        } else {
1800
2119
            if ((ret = mjpeg_decode_scan(s, nb_components,
1801
                                         prev_shift, point_transform,
1802
                                         mb_bitmask, mb_bitmask_size, reference)) < 0)
1803
                return ret;
1804
        }
1805
    }
1806
1807

2552
    if (s->interlaced &&
1808
43
        get_bits_left(&s->gb) > 32 &&
1809
15
        show_bits(&s->gb, 8) == 0xFF) {
1810
        GetBitContext bak = s->gb;
1811
        align_get_bits(&bak);
1812
        if (show_bits(&bak, 16) == 0xFFD1) {
1813
            av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1814
            s->gb = bak;
1815
            skip_bits(&s->gb, 16);
1816
            s->bottom_field ^= 1;
1817
1818
            goto next_field;
1819
        }
1820
    }
1821
1822
2524
    emms_c();
1823
2524
    return 0;
1824
 out_of_range:
1825
    av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1826
    return AVERROR_INVALIDDATA;
1827
}
1828
1829
17
static int mjpeg_decode_dri(MJpegDecodeContext *s)
1830
{
1831
17
    if (get_bits(&s->gb, 16) != 4)
1832
        return AVERROR_INVALIDDATA;
1833
17
    s->restart_interval = get_bits(&s->gb, 16);
1834
17
    s->restart_count    = 0;
1835
17
    av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1836
           s->restart_interval);
1837
1838
17
    return 0;
1839
}
1840
1841
147
static int mjpeg_decode_app(MJpegDecodeContext *s)
1842
{
1843
    int len, id, i;
1844
1845
147
    len = get_bits(&s->gb, 16);
1846
147
    if (len < 6) {
1847
        if (s->bayer) {
1848
            // Pentax K-1 (digital camera) JPEG images embedded in DNG images contain unknown APP0 markers
1849
            av_log(s->avctx, AV_LOG_WARNING, "skipping APPx (len=%"PRId32") for bayer-encoded image\n", len);
1850
            skip_bits(&s->gb, len);
1851
            return 0;
1852
        } else
1853
            return AVERROR_INVALIDDATA;
1854
    }
1855
147
    if (8 * len > get_bits_left(&s->gb))
1856
        return AVERROR_INVALIDDATA;
1857
1858
147
    id   = get_bits_long(&s->gb, 32);
1859
147
    len -= 6;
1860
1861
147
    if (s->avctx->debug & FF_DEBUG_STARTCODE)
1862
        av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n",
1863
               av_fourcc2str(av_bswap32(id)), id, len);
1864
1865
    /* Buggy AVID, it puts EOI only at every 10th frame. */
1866
    /* Also, this fourcc is used by non-avid files too, it holds some
1867
       information, but it's always present in AVID-created files. */
1868
147
    if (id == AV_RB32("AVI1")) {
1869
        /* structure:
1870
            4bytes      AVI1
1871
            1bytes      polarity
1872
            1bytes      always zero
1873
            4bytes      field_size
1874
            4bytes      field_size_less_padding
1875
        */
1876
19
            s->buggy_avid = 1;
1877
19
        i = get_bits(&s->gb, 8); len--;
1878
19
        av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1879
19
        goto out;
1880
    }
1881
1882
128
    if (id == AV_RB32("JFIF")) {
1883
        int t_w, t_h, v1, v2;
1884
107
        if (len < 8)
1885
            goto out;
1886
107
        skip_bits(&s->gb, 8); /* the trailing zero-byte */
1887
107
        v1 = get_bits(&s->gb, 8);
1888
107
        v2 = get_bits(&s->gb, 8);
1889
107
        skip_bits(&s->gb, 8);
1890
1891
107
        s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1892
107
        s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1893
107
        if (   s->avctx->sample_aspect_ratio.num <= 0
1894
107
            || s->avctx->sample_aspect_ratio.den <= 0) {
1895
            s->avctx->sample_aspect_ratio.num = 0;
1896
            s->avctx->sample_aspect_ratio.den = 1;
1897
        }
1898
1899
107
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1900
            av_log(s->avctx, AV_LOG_INFO,
1901
                   "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1902
                   v1, v2,
1903
                   s->avctx->sample_aspect_ratio.num,
1904
                   s->avctx->sample_aspect_ratio.den);
1905
1906
107
        len -= 8;
1907
107
        if (len >= 2) {
1908
105
            t_w = get_bits(&s->gb, 8);
1909
105
            t_h = get_bits(&s->gb, 8);
1910

105
            if (t_w && t_h) {
1911
                /* skip thumbnail */
1912
                if (len -10 - (t_w * t_h * 3) > 0)
1913
                    len -= t_w * t_h * 3;
1914
            }
1915
105
            len -= 2;
1916
        }
1917
107
        goto out;
1918
    }
1919
1920
21
    if (   id == AV_RB32("Adob")
1921
5
        && len >= 7
1922
5
        && show_bits(&s->gb, 8) == 'e'
1923
5
        && show_bits_long(&s->gb, 32) != AV_RB32("e_CM")) {
1924
5
        skip_bits(&s->gb,  8); /* 'e' */
1925
5
        skip_bits(&s->gb, 16); /* version */
1926
5
        skip_bits(&s->gb, 16); /* flags0 */
1927
5
        skip_bits(&s->gb, 16); /* flags1 */
1928
5
        s->adobe_transform = get_bits(&s->gb,  8);
1929
5
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1930
            av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1931
5
        len -= 7;
1932
5
        goto out;
1933
    }
1934
1935
16
    if (id == AV_RB32("LJIF")) {
1936
        int rgb = s->rgb;
1937
        int pegasus_rct = s->pegasus_rct;
1938
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1939
            av_log(s->avctx, AV_LOG_INFO,
1940
                   "Pegasus lossless jpeg header found\n");
1941
        skip_bits(&s->gb, 16); /* version ? */
1942
        skip_bits(&s->gb, 16); /* unknown always 0? */
1943
        skip_bits(&s->gb, 16); /* unknown always 0? */
1944
        skip_bits(&s->gb, 16); /* unknown always 0? */
1945
        switch (i=get_bits(&s->gb, 8)) {
1946
        case 1:
1947
            rgb         = 1;
1948
            pegasus_rct = 0;
1949
            break;
1950
        case 2:
1951
            rgb         = 1;
1952
            pegasus_rct = 1;
1953
            break;
1954
        default:
1955
            av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1956
        }
1957
1958
        len -= 9;
1959
        if (s->got_picture)
1960
            if (rgb != s->rgb || pegasus_rct != s->pegasus_rct) {
1961
                av_log(s->avctx, AV_LOG_WARNING, "Mismatching LJIF tag\n");
1962
                goto out;
1963
            }
1964
1965
        s->rgb = rgb;
1966
        s->pegasus_rct = pegasus_rct;
1967
1968
        goto out;
1969
    }
1970

16
    if (id == AV_RL32("colr") && len > 0) {
1971
        s->colr = get_bits(&s->gb, 8);
1972
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1973
            av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1974
        len --;
1975
        goto out;
1976
    }
1977

16
    if (id == AV_RL32("xfrm") && len > 0) {
1978
        s->xfrm = get_bits(&s->gb, 8);
1979
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1980
            av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1981
        len --;
1982
        goto out;
1983
    }
1984
1985
    /* JPS extension by VRex */
1986

16
    if (s->start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1987
        int flags, layout, type;
1988
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1989
            av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
1990
1991
        skip_bits(&s->gb, 32); len -= 4;  /* JPS_ */
1992
        skip_bits(&s->gb, 16); len -= 2;  /* block length */
1993
        skip_bits(&s->gb, 8);             /* reserved */
1994
        flags  = get_bits(&s->gb, 8);
1995
        layout = get_bits(&s->gb, 8);
1996
        type   = get_bits(&s->gb, 8);
1997
        len -= 4;
1998
1999
        av_freep(&s->stereo3d);
2000
        s->stereo3d = av_stereo3d_alloc();
2001
        if (!s->stereo3d) {
2002
            goto out;
2003
        }
2004
        if (type == 0) {
2005
            s->stereo3d->type = AV_STEREO3D_2D;
2006
        } else if (type == 1) {
2007
            switch (layout) {
2008
            case 0x01:
2009
                s->stereo3d->type = AV_STEREO3D_LINES;
2010
                break;
2011
            case 0x02:
2012
                s->stereo3d->type = AV_STEREO3D_SIDEBYSIDE;
2013
                break;
2014
            case 0x03:
2015
                s->stereo3d->type = AV_STEREO3D_TOPBOTTOM;
2016
                break;
2017
            }
2018
            if (!(flags & 0x04)) {
2019
                s->stereo3d->flags = AV_STEREO3D_FLAG_INVERT;
2020
            }
2021
        }
2022
        goto out;
2023
    }
2024
2025
    /* EXIF metadata */
2026

16
    if (s->start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
2027
        GetByteContext gbytes;
2028
        int ret, le, ifd_offset, bytes_read;
2029
        const uint8_t *aligned;
2030
2031
10
        skip_bits(&s->gb, 16); // skip padding
2032
10
        len -= 2;
2033
2034
        // init byte wise reading
2035
10
        aligned = align_get_bits(&s->gb);
2036
10
        bytestream2_init(&gbytes, aligned, len);
2037
2038
        // read TIFF header
2039
10
        ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
2040
10
        if (ret) {
2041
            av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
2042
        } else {
2043
10
            bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
2044
2045
            // read 0th IFD and store the metadata
2046
            // (return values > 0 indicate the presence of subimage metadata)
2047
10
            ret = ff_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata);
2048
10
            if (ret < 0) {
2049
                av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n");
2050
            }
2051
        }
2052
2053
10
        bytes_read = bytestream2_tell(&gbytes);
2054
10
        skip_bits(&s->gb, bytes_read << 3);
2055
10
        len -= bytes_read;
2056
2057
10
        goto out;
2058
    }
2059
2060
    /* Apple MJPEG-A */
2061

6
    if ((s->start_code == APP1) && (len > (0x28 - 8))) {
2062
2
        id   = get_bits_long(&s->gb, 32);
2063
2
        len -= 4;
2064
        /* Apple MJPEG-A */
2065
2
        if (id == AV_RB32("mjpg")) {
2066
            /* structure:
2067
                4bytes      field size
2068
                4bytes      pad field size
2069
                4bytes      next off
2070
                4bytes      quant off
2071
                4bytes      huff off
2072
                4bytes      image off
2073
                4bytes      scan off
2074
                4bytes      data off
2075
            */
2076
            if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2077
                av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
2078
        }
2079
    }
2080
2081

6
    if (s->start_code == APP2 && id == AV_RB32("ICC_") && len >= 10) {
2082
        int id2;
2083
        unsigned seqno;
2084
        unsigned nummarkers;
2085
2086
        id   = get_bits_long(&s->gb, 32);
2087
        id2  = get_bits(&s->gb, 24);
2088
        len -= 7;
2089
        if (id != AV_RB32("PROF") || id2 != AV_RB24("ILE")) {
2090
            av_log(s->avctx, AV_LOG_WARNING, "Invalid ICC_PROFILE header in APP2\n");
2091
            goto out;
2092
        }
2093
2094
        skip_bits(&s->gb, 8);
2095
        seqno  = get_bits(&s->gb, 8);
2096
        len   -= 2;
2097
        if (seqno == 0) {
2098
            av_log(s->avctx, AV_LOG_WARNING, "Invalid sequence number in APP2\n");
2099
            goto out;
2100
        }
2101
2102
        nummarkers  = get_bits(&s->gb, 8);
2103
        len        -= 1;
2104
        if (nummarkers == 0) {
2105
            av_log(s->avctx, AV_LOG_WARNING, "Invalid number of markers coded in APP2\n");
2106
            goto out;
2107
        } else if (s->iccnum != 0 && nummarkers != s->iccnum) {
2108
            av_log(s->avctx, AV_LOG_WARNING, "Mistmatch in coded number of ICC markers between markers\n");
2109
            goto out;
2110
        } else if (seqno > nummarkers) {
2111
            av_log(s->avctx, AV_LOG_WARNING, "Mismatching sequence number and coded number of ICC markers\n");
2112
            goto out;
2113
        }
2114
2115
        /* Allocate if this is the first APP2 we've seen. */
2116
        if (s->iccnum == 0) {
2117
            s->iccdata     = av_mallocz(nummarkers * sizeof(*(s->iccdata)));
2118
            s->iccdatalens = av_mallocz(nummarkers * sizeof(*(s->iccdatalens)));
2119
            if (!s->iccdata || !s->iccdatalens) {
2120
                av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data arrays\n");
2121
                return AVERROR(ENOMEM);
2122
            }
2123
            s->iccnum = nummarkers;
2124
        }
2125
2126
        if (s->iccdata[seqno - 1]) {
2127
            av_log(s->avctx, AV_LOG_WARNING, "Duplicate ICC sequence number\n");
2128
            goto out;
2129
        }
2130
2131
        s->iccdatalens[seqno - 1]  = len;
2132
        s->iccdata[seqno - 1]      = av_malloc(len);
2133
        if (!s->iccdata[seqno - 1]) {
2134
            av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data buffer\n");
2135
            return AVERROR(ENOMEM);
2136
        }
2137
2138
        memcpy(s->iccdata[seqno - 1], align_get_bits(&s->gb), len);
2139
        skip_bits(&s->gb, len << 3);
2140
        len = 0;
2141
        s->iccread++;
2142
2143
        if (s->iccread > s->iccnum)
2144
            av_log(s->avctx, AV_LOG_WARNING, "Read more ICC markers than are supposed to be coded\n");
2145
    }
2146
2147
6
out:
2148
    /* slow but needed for extreme adobe jpegs */
2149
147
    if (len < 0)
2150
        av_log(s->avctx, AV_LOG_ERROR,
2151
               "mjpeg: error, decode_app parser read over the end\n");
2152
107405
    while (--len > 0)
2153
107258
        skip_bits(&s->gb, 8);
2154
2155
147
    return 0;
2156
}
2157
2158
220
static int mjpeg_decode_com(MJpegDecodeContext *s)
2159
{
2160
220
    int len = get_bits(&s->gb, 16);
2161

220
    if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
2162
        int i;
2163
220
        char *cbuf = av_malloc(len - 1);
2164
220
        if (!cbuf)
2165
            return AVERROR(ENOMEM);
2166
2167
3053
        for (i = 0; i < len - 2; i++)
2168
2833
            cbuf[i] = get_bits(&s->gb, 8);
2169

220
        if (i > 0 && cbuf[i - 1] == '\n')
2170
1
            cbuf[i - 1] = 0;
2171
        else
2172
219
            cbuf[i] = 0;
2173
2174
220
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2175
            av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
2176
2177
        /* buggy avid, it puts EOI only at every 10th frame */
2178
220
        if (!strncmp(cbuf, "AVID", 4)) {
2179
11
            parse_avid(s, cbuf, len);
2180
209
        } else if (!strcmp(cbuf, "CS=ITU601"))
2181
205
            s->cs_itu601 = 1;
2182

4
        else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
2183
4
                 (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
2184
            s->flipped = 1;
2185
4
        else if (!strcmp(cbuf, "MULTISCOPE II")) {
2186
            s->avctx->sample_aspect_ratio = (AVRational) { 1, 2 };
2187
            s->multiscope = 2;
2188
        }
2189
2190
220
        av_free(cbuf);
2191
    }
2192
2193
220
    return 0;
2194
}
2195
2196
/* return the 8 bit start code value and update the search
2197
   state. Return -1 if no start code found */
2198
15817
static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
2199
{
2200
    const uint8_t *buf_ptr;
2201
    unsigned int v, v2;
2202
    int val;
2203
15817
    int skipped = 0;
2204
2205
15817
    buf_ptr = *pbuf_ptr;
2206
269326
    while (buf_end - buf_ptr > 1) {
2207
269321
        v  = *buf_ptr++;
2208
269321
        v2 = *buf_ptr;
2209


269321
        if ((v == 0xff) && (v2 >= SOF0) && (v2 <= COM) && buf_ptr < buf_end) {
2210
15812
            val = *buf_ptr++;
2211
15812
            goto found;
2212
        }
2213
253509
        skipped++;
2214
    }
2215
5
    buf_ptr = buf_end;
2216
5
    val = -1;
2217
15817
found:
2218
    ff_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
2219
15817
    *pbuf_ptr = buf_ptr;
2220
15817
    return val;
2221
}
2222
2223
15817
int ff_mjpeg_find_marker(MJpegDecodeContext *s,
2224
                         const uint8_t **buf_ptr, const uint8_t *buf_end,
2225
                         const uint8_t **unescaped_buf_ptr,
2226
                         int *unescaped_buf_size)
2227
{
2228
    int start_code;
2229
15817
    start_code = find_marker(buf_ptr, buf_end);
2230
2231
15817
    av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
2232
15817
    if (!s->buffer)
2233
        return AVERROR(ENOMEM);
2234
2235
    /* unescape buffer of SOS, use special treatment for JPEG-LS */
2236

18170
    if (start_code == SOS && !s->ls) {
2237
2353
        const uint8_t *src = *buf_ptr;
2238
2353
        const uint8_t *ptr = src;
2239
2353
        uint8_t *dst = s->buffer;
2240
2241
        #define copy_data_segment(skip) do {       \
2242
            ptrdiff_t length = (ptr - src) - (skip);  \
2243
            if (length > 0) {                         \
2244
                memcpy(dst, src, length);             \
2245
                dst += length;                        \
2246
                src = ptr;                            \
2247
            }                                         \
2248
        } while (0)
2249
2250
2353
        if (s->avctx->codec_id == AV_CODEC_ID_THP) {
2251
74
            ptr = buf_end;
2252
74
            copy_data_segment(0);
2253
        } else {
2254
42639241
            while (ptr < buf_end) {
2255
42639241
                uint8_t x = *(ptr++);
2256
2257
42639241
                if (x == 0xff) {
2258
182577
                    ptrdiff_t skip = 0;
2259

365168
                    while (ptr < buf_end && x == 0xff) {
2260
182591
                        x = *(ptr++);
2261
182591
                        skip++;
2262
                    }
2263
2264
                    /* 0xFF, 0xFF, ... */
2265
182577
                    if (skip > 1) {
2266
7
                        copy_data_segment(skip);
2267
2268
                        /* decrement src as it is equal to ptr after the
2269
                         * copy_data_segment macro and we might want to
2270
                         * copy the current value of x later on */
2271
7
                        src--;
2272
                    }
2273
2274

182577
                    if (x < RST0 || x > RST7) {
2275
182501
                        copy_data_segment(1);
2276
182501
                        if (x)
2277
2279
                            break;
2278
                    }
2279
                }
2280
            }
2281
2279
            if (src < ptr)
2282
7
                copy_data_segment(0);
2283
        }
2284
        #undef copy_data_segment
2285
2286
2353
        *unescaped_buf_ptr  = s->buffer;
2287
2353
        *unescaped_buf_size = dst - s->buffer;
2288
2353
        memset(s->buffer + *unescaped_buf_size, 0,
2289
               AV_INPUT_BUFFER_PADDING_SIZE);
2290
2291
2353
        av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %"PTRDIFF_SPECIFIER" bytes\n",
2292
2353
               (buf_end - *buf_ptr) - (dst - s->buffer));
2293

13464
    } else if (start_code == SOS && s->ls) {
2294
205
        const uint8_t *src = *buf_ptr;
2295
205
        uint8_t *dst  = s->buffer;
2296
205
        int bit_count = 0;
2297
205
        int t = 0, b = 0;
2298
        PutBitContext pb;
2299
2300
        /* find marker */
2301
26357335
        while (src + t < buf_end) {
2302
26357335
            uint8_t x = src[t++];
2303
26357335
            if (x == 0xff) {
2304

295862
                while ((src + t < buf_end) && x == 0xff)
2305
147931
                    x = src[t++];
2306
147931
                if (x & 0x80) {
2307
205
                    t -= FFMIN(2, t);
2308
205
                    break;
2309
                }
2310
            }
2311
        }
2312
205
        bit_count = t * 8;
2313
205
        init_put_bits(&pb, dst, t);
2314
2315
        /* unescape bitstream */
2316
26357335
        while (b < t) {
2317
26357130
            uint8_t x = src[b++];
2318
26357130
            put_bits(&pb, 8, x);
2319

26357130
            if (x == 0xFF && b < t) {
2320
147726
                x = src[b++];
2321
147726
                if (x & 0x80) {
2322
                    av_log(s->avctx, AV_LOG_WARNING, "Invalid escape sequence\n");
2323
                    x &= 0x7f;
2324
                }
2325
147726
                put_bits(&pb, 7, x);
2326
147726
                bit_count--;
2327
            }
2328
        }
2329
205
        flush_put_bits(&pb);
2330
2331
205
        *unescaped_buf_ptr  = dst;
2332
205
        *unescaped_buf_size = (bit_count + 7) >> 3;
2333
205
        memset(s->buffer + *unescaped_buf_size, 0,
2334
               AV_INPUT_BUFFER_PADDING_SIZE);
2335
    } else {
2336
13259
        *unescaped_buf_ptr  = *buf_ptr;
2337
13259
        *unescaped_buf_size = buf_end - *buf_ptr;
2338
    }
2339
2340
15817
    return start_code;
2341
}
2342
2343
140
static void reset_icc_profile(MJpegDecodeContext *s)
2344
{
2345
    int i;
2346
2347
140
    if (s->iccdata)
2348
        for (i = 0; i < s->iccnum; i++)
2349
            av_freep(&s->iccdata[i]);
2350
140
    av_freep(&s->iccdata);
2351
140
    av_freep(&s->iccdatalens);
2352
2353
140
    s->iccread = 0;
2354
140
    s->iccnum  = 0;
2355
140
}
2356
2357
// SMV JPEG just stacks several output frames into one JPEG picture
2358
// we handle that by setting up the cropping parameters appropriately
2359
13
static int smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
2360
{
2361
13
    MJpegDecodeContext *s = avctx->priv_data;
2362
    int ret;
2363
2364
13
    if (s->smv_next_frame > 0) {
2365
10
        av_assert0(s->smv_frame->buf[0]);
2366
10
        av_frame_unref(frame);
2367
10
        ret = av_frame_ref(frame, s->smv_frame);
2368
10
        if (ret < 0)
2369
            return ret;
2370
    } else {
2371
3
        av_assert0(frame->buf[0]);
2372
3
        av_frame_unref(s->smv_frame);
2373
3
        ret = av_frame_ref(s->smv_frame, frame);
2374
3
        if (ret < 0)
2375
            return ret;
2376
    }
2377
2378
13
    av_assert0((s->smv_next_frame + 1) * avctx->height <= avctx->coded_height);
2379
2380
13
    frame->width       = avctx->coded_width;
2381
13
    frame->height      = avctx->coded_height;
2382
13
    frame->crop_top    = FFMIN(s->smv_next_frame * avctx->height, frame->height);
2383
13
    frame->crop_bottom = frame->height - (s->smv_next_frame + 1) * avctx->height;
2384
2385
13
    s->smv_next_frame = (s->smv_next_frame + 1) % s->smv_frames_per_jpeg;
2386
2387
13
    if (s->smv_next_frame == 0)
2388
2
        av_frame_unref(s->smv_frame);
2389
2390
13
    return 0;
2391
}
2392
2393
4861
static int mjpeg_get_packet(AVCodecContext *avctx)
2394
{
2395
4861
    MJpegDecodeContext *s = avctx->priv_data;
2396
    int ret;
2397
2398
4861
    av_packet_unref(s->pkt);
2399
4861
    ret = ff_decode_get_packet(avctx, s->pkt);
2400
4861
    if (ret < 0)
2401
2337
        return ret;
2402
2403
#if CONFIG_SP5X_DECODER || CONFIG_AMV_DECODER
2404
2524
    if (avctx->codec_id == AV_CODEC_ID_SP5X ||
2405
2513
        avctx->codec_id == AV_CODEC_ID_AMV) {
2406
378
        ret = ff_sp5x_process_packet(avctx, s->pkt);
2407
378
        if (ret < 0)
2408
            return ret;
2409
    }
2410
#endif
2411
2412
2524
    s->buf_size = s->pkt->size;
2413
2414
2524
    return 0;
2415
}
2416
2417
4871
int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
2418
{
2419
4871
    MJpegDecodeContext *s = avctx->priv_data;
2420
    const uint8_t *buf_end, *buf_ptr;
2421
    const uint8_t *unescaped_buf_ptr;
2422
    int hshift, vshift;
2423
    int unescaped_buf_size;
2424
    int start_code;
2425
    int i, index;
2426
4871
    int ret = 0;
2427
    int is16bit;
2428
2429

4871
    if (avctx->codec_id == AV_CODEC_ID_SMVJPEG && s->smv_next_frame > 0)
2430
10
        return smv_process_frame(avctx, frame);
2431
2432
4861
    av_dict_free(&s->exif_metadata);
2433
4861
    av_freep(&s->stereo3d);
2434
4861
    s->adobe_transform = -1;
2435
2436
4861
    if (s->iccnum != 0)
2437
        reset_icc_profile(s);
2438
2439
4861
    ret = mjpeg_get_packet(avctx);
2440
4861
    if (ret < 0)
2441
2337
        return ret;
2442
2443
2524
    buf_ptr = s->pkt->data;
2444
2524
    buf_end = s->pkt->data + s->pkt->size;
2445
15615
    while (buf_ptr < buf_end) {
2446
        /* find start next marker */
2447
15615
        start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
2448
                                          &unescaped_buf_ptr,
2449
                                          &unescaped_buf_size);
2450
        /* EOF */
2451
15615
        if (start_code < 0) {
2452
5
            break;
2453
15610
        } else if (unescaped_buf_size > INT_MAX / 8) {
2454
            av_log(avctx, AV_LOG_ERROR,
2455
                   "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
2456
                   start_code, unescaped_buf_size, s->pkt->size);
2457
            return AVERROR_INVALIDDATA;
2458
        }
2459
15610
        av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
2460
               start_code, buf_end - buf_ptr);
2461
2462
15610
        ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
2463
2464
15610
        if (ret < 0) {
2465
            av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
2466
            goto fail;
2467
        }
2468
2469
15610
        s->start_code = start_code;
2470
15610
        if (s->avctx->debug & FF_DEBUG_STARTCODE)
2471
            av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
2472
2473
        /* process markers */
2474

15610
        if (start_code >= RST0 && start_code <= RST7) {
2475
2
            av_log(avctx, AV_LOG_DEBUG,
2476
                   "restart marker: %d\n", start_code & 0x0f);
2477
            /* APP fields */
2478

15608
        } else if (start_code >= APP0 && start_code <= APP15) {
2479
147
            if ((ret = mjpeg_decode_app(s)) < 0)
2480
                av_log(avctx, AV_LOG_ERROR, "unable to decode APP fields: %s\n",
2481
                       av_err2str(ret));
2482
            /* Comment */
2483
15461
        } else if (start_code == COM) {
2484
220
            ret = mjpeg_decode_com(s);
2485
220
            if (ret < 0)
2486
                return ret;
2487
15241
        } else if (start_code == DQT) {
2488
2493
            ret = ff_mjpeg_decode_dqt(s);
2489
2493
            if (ret < 0)
2490
                return ret;
2491
        }
2492
2493
15610
        ret = -1;
2494
2495
        if (!CONFIG_JPEGLS_DECODER &&
2496
            (start_code == SOF48 || start_code == LSE)) {
2497
            av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
2498
            return AVERROR(ENOSYS);
2499
        }
2500
2501
15610
        if (avctx->skip_frame == AVDISCARD_ALL) {
2502
449
            switch(start_code) {
2503
220
            case SOF0:
2504
            case SOF1:
2505
            case SOF2:
2506
            case SOF3:
2507
            case SOF48:
2508
            case SOI:
2509
            case SOS:
2510
            case EOI:
2511
220
                break;
2512
229
            default:
2513
229
                goto skip;
2514
            }
2515
15161
        }
2516
2517



15381
        switch (start_code) {
2518
2527
        case SOI:
2519
2527
            s->restart_interval = 0;
2520
2527
            s->restart_count    = 0;
2521
2527
            s->raw_image_buffer      = buf_ptr;
2522
2527
            s->raw_image_buffer_size = buf_end - buf_ptr;
2523
            /* nothing to do on SOI */
2524
2527
            break;
2525
2515
        case DHT:
2526
2515
            if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
2527
                av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
2528
                goto fail;
2529
            }
2530
2515
            break;
2531
2117
        case SOF0:
2532
        case SOF1:
2533
2117
            if (start_code == SOF0)
2534
2115
                s->avctx->profile = FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT;
2535
            else
2536
2
                s->avctx->profile = FF_PROFILE_MJPEG_HUFFMAN_EXTENDED_SEQUENTIAL_DCT;
2537
2117
            s->lossless    = 0;
2538
2117
            s->ls          = 0;
2539
2117
            s->progressive = 0;
2540
2117
            if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2541
                goto fail;
2542
2117
            break;
2543
        case SOF2:
2544
            s->avctx->profile = FF_PROFILE_MJPEG_HUFFMAN_PROGRESSIVE_DCT;
2545
            s->lossless    = 0;
2546
            s->ls          = 0;
2547
            s->progressive = 1;
2548
            if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2549
                goto fail;
2550
            break;
2551
205
        case SOF3:
2552
205
            s->avctx->profile     = FF_PROFILE_MJPEG_HUFFMAN_LOSSLESS;
2553
205
            s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
2554
205
            s->lossless    = 1;
2555
205
            s->ls          = 0;
2556
205
            s->progressive = 0;
2557
205
            if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2558
                goto fail;
2559
205
            break;
2560
205
        case SOF48:
2561
205
            s->avctx->profile     = FF_PROFILE_MJPEG_JPEG_LS;
2562
205
            s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
2563
205
            s->lossless    = 1;
2564
205
            s->ls          = 1;
2565
205
            s->progressive = 0;
2566
205
            if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2567
                goto fail;
2568
205
            break;
2569
        case LSE:
2570
            if (!CONFIG_JPEGLS_DECODER ||
2571
                (ret = ff_jpegls_decode_lse(s)) < 0)
2572
                goto fail;
2573
            break;
2574
        case EOI:
2575
2527
eoi_parser:
2576

2527
            if (!avctx->hwaccel && avctx->skip_frame != AVDISCARD_ALL &&
2577

2471
                s->progressive && s->cur_scan && s->got_picture)
2578
                mjpeg_idct_scan_progressive_ac(s);
2579
2527
            s->cur_scan = 0;
2580
2527
            if (!s->got_picture) {
2581
                av_log(avctx, AV_LOG_WARNING,
2582
                       "Found EOI before any SOF, ignoring\n");
2583
                break;
2584
            }
2585
2527
            if (s->interlaced) {
2586
8
                s->bottom_field ^= 1;
2587
                /* if not bottom field, do not output image yet */
2588
8
                if (s->bottom_field == !s->interlace_polarity)
2589
4
                    break;
2590
            }
2591
2523
            if (avctx->skip_frame == AVDISCARD_ALL) {
2592
55
                s->got_picture = 0;
2593
55
                ret = AVERROR(EAGAIN);
2594
55
                goto the_end_no_picture;
2595
            }
2596
2468
            if (s->avctx->hwaccel) {
2597
                ret = s->avctx->hwaccel->end_frame(s->avctx);
2598
                if (ret < 0)
2599
                    return ret;
2600
2601
                av_freep(&s->hwaccel_picture_private);
2602
            }
2603
2468
            if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2604
                return ret;
2605
2468
            s->got_picture = 0;
2606
2607
2468
            frame->pkt_dts = s->pkt->dts;
2608
2609
2468
            if (!s->lossless) {
2610
2063
                int qp = FFMAX3(s->qscale[0],
2611
                                s->qscale[1],
2612
                                s->qscale[2]);
2613
2063
                int qpw = (s->width + 15) / 16;
2614
2063
                AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
2615
2063
                if (qp_table_buf) {
2616
2063
                    memset(qp_table_buf->data, qp, qpw);
2617
2063
                    av_frame_set_qp_table(frame, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
2618
                }
2619
2620
2063
                if(avctx->debug & FF_DEBUG_QP)
2621
                    av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2622
            }
2623
2624
2468
            goto the_end;
2625
2527
        case SOS:
2626
2527
            s->raw_scan_buffer      = buf_ptr;
2627
2527
            s->raw_scan_buffer_size = buf_end - buf_ptr;
2628
2629
2527
            s->cur_scan++;
2630
2527
            if (avctx->skip_frame == AVDISCARD_ALL) {
2631
56
                skip_bits(&s->gb, get_bits_left(&s->gb));
2632
56
                break;
2633
            }
2634
2635
2471
            if ((ret = ff_mjpeg_decode_sos(s, NULL, 0, NULL)) < 0 &&
2636
                (avctx->err_recognition & AV_EF_EXPLODE))
2637
                goto fail;
2638
2471
            break;
2639
17
        case DRI:
2640
17
            if ((ret = mjpeg_decode_dri(s)) < 0)
2641
                return ret;
2642
17
            break;
2643
        case SOF5:
2644
        case SOF6:
2645
        case SOF7:
2646
        case SOF9:
2647
        case SOF10:
2648
        case SOF11:
2649
        case SOF13:
2650
        case SOF14:
2651
        case SOF15:
2652
        case JPG:
2653
            av_log(avctx, AV_LOG_ERROR,
2654
                   "mjpeg: unsupported coding type (%x)\n", start_code);
2655
            break;
2656
        }
2657
2658
13091
skip:
2659
        /* eof process start code */
2660
13091
        buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
2661
26182
        av_log(avctx, AV_LOG_DEBUG,
2662
               "marker parser used %d bytes (%d bits)\n",
2663
13091
               (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
2664
    }
2665

5
    if (s->got_picture && s->cur_scan) {
2666
4
        av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2667
4
        goto eoi_parser;
2668
    }
2669
1
    av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2670
1
    return AVERROR_INVALIDDATA;
2671
fail:
2672
    s->got_picture = 0;
2673
    return ret;
2674
2468
the_end:
2675
2676
2468
    is16bit = av_pix_fmt_desc_get(s->avctx->pix_fmt)->comp[0].step > 1;
2677
2678
2468
    if (AV_RB32(s->upscale_h)) {
2679
        int p;
2680
        av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2681
                   avctx->pix_fmt == AV_PIX_FMT_YUV444P  ||
2682
                   avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2683
                   avctx->pix_fmt == AV_PIX_FMT_YUV440P  ||
2684
                   avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2685
                   avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2686
                   avctx->pix_fmt == AV_PIX_FMT_YUV420P  ||
2687
                   avctx->pix_fmt == AV_PIX_FMT_YUV420P16||
2688
                   avctx->pix_fmt == AV_PIX_FMT_YUVA420P  ||
2689
                   avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2690
                   avctx->pix_fmt == AV_PIX_FMT_GBRP     ||
2691
                   avctx->pix_fmt == AV_PIX_FMT_GBRAP
2692
                  );
2693
        ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2694
        if (ret)
2695
            return ret;
2696
2697
        av_assert0(s->nb_components == av_pix_fmt_count_planes(s->picture_ptr->format));
2698
        for (p = 0; p<s->nb_components; p++) {
2699
            uint8_t *line = s->picture_ptr->data[p];
2700
            int w = s->width;
2701
            int h = s->height;
2702
            if (!s->upscale_h[p])
2703
                continue;
2704
            if (p==1 || p==2) {
2705
                w = AV_CEIL_RSHIFT(w, hshift);
2706
                h = AV_CEIL_RSHIFT(h, vshift);
2707
            }
2708
            if (s->upscale_v[p] == 1)
2709
                h = (h+1)>>1;
2710
            av_assert0(w > 0);
2711
            for (i = 0; i < h; i++) {
2712
                if (s->upscale_h[p] == 1) {
2713
                    if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2714
                    else                      line[w - 1] = line[(w - 1) / 2];
2715
                    for (index = w - 2; index > 0; index--) {
2716
                        if (is16bit)
2717
                            ((uint16_t*)line)[index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2718
                        else
2719
                            line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2720
                    }
2721
                } else if (s->upscale_h[p] == 2) {
2722
                    if (is16bit) {
2723
                        ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 3];
2724
                        if (w > 1)
2725
                            ((uint16_t*)line)[w - 2] = ((uint16_t*)line)[w - 1];
2726
                    } else {
2727
                        line[w - 1] = line[(w - 1) / 3];
2728
                        if (w > 1)
2729
                            line[w - 2] = line[w - 1];
2730
                    }
2731
                    for (index = w - 3; index > 0; index--) {
2732
                        line[index] = (line[index / 3] + line[(index + 1) / 3] + line[(index + 2) / 3] + 1) / 3;
2733
                    }
2734
                }
2735
                line += s->linesize[p];
2736
            }
2737
        }
2738
    }
2739
2468
    if (AV_RB32(s->upscale_v)) {
2740
        int p;
2741
        av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
2742
                   avctx->pix_fmt == AV_PIX_FMT_YUV444P  ||
2743
                   avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2744
                   avctx->pix_fmt == AV_PIX_FMT_YUV422P  ||
2745
                   avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2746
                   avctx->pix_fmt == AV_PIX_FMT_YUV420P  ||
2747
                   avctx->pix_fmt == AV_PIX_FMT_YUV440P  ||
2748
                   avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2749
                   avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2750
                   avctx->pix_fmt == AV_PIX_FMT_YUVA420P  ||
2751
                   avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2752
                   avctx->pix_fmt == AV_PIX_FMT_GBRP     ||
2753
                   avctx->pix_fmt == AV_PIX_FMT_GBRAP
2754
                   );
2755
        ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2756
        if (ret)
2757
            return ret;
2758
2759
        av_assert0(s->nb_components == av_pix_fmt_count_planes(s->picture_ptr->format));
2760
        for (p = 0; p < s->nb_components; p++) {
2761
            uint8_t *dst;
2762
            int w = s->width;
2763
            int h = s->height;
2764
            if (!s->upscale_v[p])
2765
                continue;
2766
            if (p==1 || p==2) {
2767
                w = AV_CEIL_RSHIFT(w, hshift);
2768
                h = AV_CEIL_RSHIFT(h, vshift);
2769
            }
2770
            dst = &((uint8_t *)s->picture_ptr->data[p])[(h - 1) * s->linesize[p]];
2771
            for (i = h - 1; i; i--) {
2772
                uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2773
                uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2774
                if (s->upscale_v[p] != 2 && (src1 == src2 || i == h - 1)) {
2775
                    memcpy(dst, src1, w);
2776
                } else {
2777
                    for (index = 0; index < w; index++)
2778
                        dst[index] = (src1[index] + src2[index]) >> 1;
2779
                }
2780
                dst -= s->linesize[p];
2781
            }
2782
        }
2783
    }
2784

2468
    if (s->flipped && !s->rgb) {
2785
        int j;
2786
367
        ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2787
367
        if (ret)
2788
            return ret;
2789
2790
367
        av_assert0(s->nb_components == av_pix_fmt_count_planes(s->picture_ptr->format));
2791
1468
        for (index=0; index<s->nb_components; index++) {
2792
1101
            uint8_t *dst = s->picture_ptr->data[index];
2793
1101
            int w = s->picture_ptr->width;
2794
1101
            int h = s->picture_ptr->height;
2795

1101
            if(index && index<3){
2796
734
                w = AV_CEIL_RSHIFT(w, hshift);
2797
734
                h = AV_CEIL_RSHIFT(h, vshift);
2798
            }
2799
1101
            if(dst){
2800
1101
                uint8_t *dst2 = dst + s->picture_ptr->linesize[index]*(h-1);
2801
66408
                for (i=0; i<h/2; i++) {
2802
14088753
                    for (j=0; j<w; j++)
2803
14023446
                        FFSWAP(int, dst[j], dst2[j]);
2804
65307
                    dst  += s->picture_ptr->linesize[index];
2805
65307
                    dst2 -= s->picture_ptr->linesize[index];
2806
                }
2807
            }
2808
        }
2809
    }
2810

2468
    if (s->adobe_transform == 0 && s->avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2811
        int w = s->picture_ptr->width;
2812
        int h = s->picture_ptr->height;
2813
        av_assert0(s->nb_components == 4);
2814
        for (i=0; i<h; i++) {
2815
            int j;
2816
            uint8_t *dst[4];
2817
            for (index=0; index<4; index++) {
2818
                dst[index] =   s->picture_ptr->data[index]
2819
                             + s->picture_ptr->linesize[index]*i;
2820
            }
2821
            for (j=0; j<w; j++) {
2822
                int k = dst[3][j];
2823
                int r = dst[0][j] * k;
2824
                int g = dst[1][j] * k;
2825
                int b = dst[2][j] * k;
2826
                dst[0][j] = g*257 >> 16;
2827
                dst[1][j] = b*257 >> 16;
2828
                dst[2][j] = r*257 >> 16;
2829
                dst[3][j] = 255;
2830
            }
2831
        }
2832
    }
2833

2468
    if (s->adobe_transform == 2 && s->avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2834
        int w = s->picture_ptr->width;
2835
        int h = s->picture_ptr->height;
2836
        av_assert0(s->nb_components == 4);
2837
        for (i=0; i<h; i++) {
2838
            int j;
2839
            uint8_t *dst[4];
2840
            for (index=0; index<4; index++) {
2841
                dst[index] =   s->picture_ptr->data[index]
2842
                             + s->picture_ptr->linesize[index]*i;
2843
            }
2844
            for (j=0; j<w; j++) {
2845
                int k = dst[3][j];
2846
                int r = (255 - dst[0][j]) * k;
2847
                int g = (128 - dst[1][j]) * k;
2848
                int b = (128 - dst[2][j]) * k;
2849
                dst[0][j] = r*257 >> 16;
2850
                dst[1][j] = (g*257 >> 16) + 128;
2851
                dst[2][j] = (b*257 >> 16) + 128;
2852
                dst[3][j] = 255;
2853
            }
2854
        }
2855
    }
2856
2857
2468
    if (s->stereo3d) {
2858
        AVStereo3D *stereo = av_stereo3d_create_side_data(frame);
2859
        if (stereo) {
2860
            stereo->type  = s->stereo3d->type;
2861
            stereo->flags = s->stereo3d->flags;
2862
        }
2863
        av_freep(&s->stereo3d);
2864
    }
2865
2866

2468
    if (s->iccnum != 0 && s->iccnum == s->iccread) {
2867
        AVFrameSideData *sd;
2868
        size_t offset = 0;
2869
        int total_size = 0;
2870
        int i;
2871
2872
        /* Sum size of all parts. */
2873
        for (i = 0; i < s->iccnum; i++)
2874
            total_size += s->iccdatalens[i];
2875
2876
        sd = av_frame_new_side_data(frame, AV_FRAME_DATA_ICC_PROFILE, total_size);
2877
        if (!sd) {
2878
            av_log(s->avctx, AV_LOG_ERROR, "Could not allocate frame side data\n");
2879
            return AVERROR(ENOMEM);
2880
        }
2881
2882
        /* Reassemble the parts, which are now in-order. */
2883
        for (i = 0; i < s->iccnum; i++) {
2884
            memcpy(sd->data + offset, s->iccdata[i], s->iccdatalens[i]);
2885
            offset += s->iccdatalens[i];
2886
        }
2887
    }
2888
2889
2468
    av_dict_copy(&frame->metadata, s->exif_metadata, 0);
2890
2468
    av_dict_free(&s->exif_metadata);
2891
2892
2468
    if (avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
2893
3
        ret = smv_process_frame(avctx, frame);
2894
3
        if (ret < 0) {
2895
            av_frame_unref(frame);
2896
            return ret;
2897
        }
2898
    }
2899
2900
2468
    ret = 0;
2901
2902
2523
the_end_no_picture:
2903
2523
    av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n",
2904
           buf_end - buf_ptr);
2905
2906
2523
    return ret;
2907
}
2908
2909
/* mxpeg may call the following function (with a blank MJpegDecodeContext)
2910
 * even without having called ff_mjpeg_decode_init(). */
2911
140
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
2912
{
2913
140
    MJpegDecodeContext *s = avctx->priv_data;
2914
    int i, j;
2915
2916


140
    if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) {
2917
        av_log(avctx, AV_LOG_INFO, "Single field\n");
2918
    }
2919
2920
140
    if (s->picture) {
2921
138
        av_frame_free(&s->picture);
2922
138
        s->picture_ptr = NULL;
2923
2
    } else if (s->picture_ptr)
2924
        av_frame_unref(s->picture_ptr);
2925
2926
140
    av_packet_free(&s->pkt);
2927
2928
140
    av_frame_free(&s->smv_frame);
2929
2930
140
    av_freep(&s->buffer);
2931
140
    av_freep(&s->stereo3d);
2932
140
    av_freep(&s->ljpeg_buffer);
2933
140
    s->ljpeg_buffer_size = 0;
2934
2935
560
    for (i = 0; i < 3; i++) {
2936
2100
        for (j = 0; j < 4; j++)
2937
1680
            ff_free_vlc(&s->vlcs[i][j]);
2938
    }
2939
700
    for (i = 0; i < MAX_COMPONENTS; i++) {
2940
560
        av_freep(&s->blocks[i]);
2941
560
        av_freep(&s->last_nnz[i]);
2942
    }
2943
140
    av_dict_free(&s->exif_metadata);
2944
2945
140
    reset_icc_profile(s);
2946
2947
140
    av_freep(&s->hwaccel_picture_private);
2948
2949
140
    return 0;
2950
}
2951
2952
static void decode_flush(AVCodecContext *avctx)
2953
{
2954
    MJpegDecodeContext *s = avctx->priv_data;
2955
    s->got_picture = 0;
2956
2957
    s->smv_next_frame = 0;
2958
    av_frame_unref(s->smv_frame);
2959
}
2960
2961
#if CONFIG_MJPEG_DECODER
2962
#define OFFSET(x) offsetof(MJpegDecodeContext, x)
2963
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2964
static const AVOption options[] = {
2965
    { "extern_huff", "Use external huffman table.",
2966
      OFFSET(extern_huff), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
2967
    { NULL },
2968
};
2969
2970
static const AVClass mjpegdec_class = {
2971
    .class_name = "MJPEG decoder",
2972
    .item_name  = av_default_item_name,
2973
    .option     = options,
2974
    .version    = LIBAVUTIL_VERSION_INT,
2975
};
2976
2977
AVCodec ff_mjpeg_decoder = {
2978
    .name           = "mjpeg",
2979
    .long_name      = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
2980
    .type           = AVMEDIA_TYPE_VIDEO,
2981
    .id             = AV_CODEC_ID_MJPEG,
2982
    .priv_data_size = sizeof(MJpegDecodeContext),
2983
    .init           = ff_mjpeg_decode_init,
2984
    .close          = ff_mjpeg_decode_end,
2985
    .receive_frame  = ff_mjpeg_receive_frame,
2986
    .flush          = decode_flush,
2987
    .capabilities   = AV_CODEC_CAP_DR1,
2988
    .max_lowres     = 3,
2989
    .priv_class     = &mjpegdec_class,
2990
    .profiles       = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
2991
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
2992
                      FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_SETS_PKT_DTS,
2993
    .hw_configs     = (const AVCodecHWConfigInternal *const []) {
2994
#if CONFIG_MJPEG_NVDEC_HWACCEL
2995
                        HWACCEL_NVDEC(mjpeg),
2996
#endif
2997
#if CONFIG_MJPEG_VAAPI_HWACCEL
2998
                        HWACCEL_VAAPI(mjpeg),
2999
#endif
3000
                        NULL
3001
                    },
3002
};
3003
#endif
3004
#if CONFIG_THP_DECODER
3005
AVCodec ff_thp_decoder = {
3006
    .name           = "thp",
3007
    .long_name      = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
3008
    .type           = AVMEDIA_TYPE_VIDEO,
3009
    .id             = AV_CODEC_ID_THP,
3010
    .priv_data_size = sizeof(MJpegDecodeContext),
3011
    .init           = ff_mjpeg_decode_init,
3012
    .close          = ff_mjpeg_decode_end,
3013
    .receive_frame  = ff_mjpeg_receive_frame,
3014
    .flush          = decode_flush,
3015
    .capabilities   = AV_CODEC_CAP_DR1,
3016
    .max_lowres     = 3,
3017
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
3018
                      FF_CODEC_CAP_SETS_PKT_DTS,
3019
};
3020
#endif
3021
3022
#if CONFIG_SMVJPEG_DECODER
3023
AVCodec ff_smvjpeg_decoder = {
3024
    .name           = "smvjpeg",
3025
    .long_name      = NULL_IF_CONFIG_SMALL("SMV JPEG"),
3026
    .type           = AVMEDIA_TYPE_VIDEO,
3027
    .id             = AV_CODEC_ID_SMVJPEG,
3028
    .priv_data_size = sizeof(MJpegDecodeContext),
3029
    .init           = ff_mjpeg_decode_init,
3030
    .close          = ff_mjpeg_decode_end,
3031
    .receive_frame  = ff_mjpeg_receive_frame,
3032
    .flush          = decode_flush,
3033
    .capabilities   = AV_CODEC_CAP_DR1,
3034
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING |
3035
                      FF_CODEC_CAP_SETS_PKT_DTS,
3036
};
3037
#endif