GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/jpeg2000dec.c Lines: 822 1395 58.9 %
Date: 2020-07-11 02:49:52 Branches: 518 1052 49.2 %

Line Branch Exec Source
1
/*
2
 * JPEG 2000 image decoder
3
 * Copyright (c) 2007 Kamil Nowosad
4
 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * JPEG 2000 image decoder
26
 */
27
28
#include <inttypes.h>
29
#include <math.h>
30
31
#include "libavutil/attributes.h"
32
#include "libavutil/avassert.h"
33
#include "libavutil/common.h"
34
#include "libavutil/imgutils.h"
35
#include "libavutil/opt.h"
36
#include "libavutil/pixdesc.h"
37
#include "libavutil/thread.h"
38
#include "avcodec.h"
39
#include "bytestream.h"
40
#include "internal.h"
41
#include "thread.h"
42
#include "jpeg2000.h"
43
#include "jpeg2000dsp.h"
44
#include "profiles.h"
45
46
#define JP2_SIG_TYPE    0x6A502020
47
#define JP2_SIG_VALUE   0x0D0A870A
48
#define JP2_CODESTREAM  0x6A703263
49
#define JP2_HEADER      0x6A703268
50
51
#define HAD_COC 0x01
52
#define HAD_QCC 0x02
53
54
#define MAX_POCS 32
55
56
typedef struct Jpeg2000POCEntry {
57
    uint16_t LYEpoc;
58
    uint16_t CSpoc;
59
    uint16_t CEpoc;
60
    uint8_t RSpoc;
61
    uint8_t REpoc;
62
    uint8_t Ppoc;
63
} Jpeg2000POCEntry;
64
65
typedef struct Jpeg2000POC {
66
    Jpeg2000POCEntry poc[MAX_POCS];
67
    int nb_poc;
68
    int is_default;
69
} Jpeg2000POC;
70
71
typedef struct Jpeg2000TilePart {
72
    uint8_t tile_index;                 // Tile index who refers the tile-part
73
    const uint8_t *tp_end;
74
    GetByteContext tpg;                 // bit stream in tile-part
75
} Jpeg2000TilePart;
76
77
/* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
78
 * one per component, so tile_part elements have a size of 3 */
79
typedef struct Jpeg2000Tile {
80
    Jpeg2000Component   *comp;
81
    uint8_t             properties[4];
82
    Jpeg2000CodingStyle codsty[4];
83
    Jpeg2000QuantStyle  qntsty[4];
84
    Jpeg2000POC         poc;
85
    Jpeg2000TilePart    tile_part[32];
86
    uint8_t             has_ppt;                // whether this tile has a ppt marker
87
    uint8_t             *packed_headers;        // contains packed headers. Used only along with PPT marker
88
    int                 packed_headers_size;    // size in bytes of the packed headers
89
    GetByteContext      packed_headers_stream;  // byte context corresponding to packed headers
90
    uint16_t tp_idx;                    // Tile-part index
91
    int coord[2][2];                    // border coordinates {{x0, x1}, {y0, y1}}
92
} Jpeg2000Tile;
93
94
typedef struct Jpeg2000DecoderContext {
95
    AVClass         *class;
96
    AVCodecContext  *avctx;
97
    GetByteContext  g;
98
99
    int             width, height;
100
    int             image_offset_x, image_offset_y;
101
    int             tile_offset_x, tile_offset_y;
102
    uint8_t         cbps[4];    // bits per sample in particular components
103
    uint8_t         sgnd[4];    // if a component is signed
104
    uint8_t         properties[4];
105
    int             cdx[4], cdy[4];
106
    int             precision;
107
    int             ncomponents;
108
    int             colour_space;
109
    uint32_t        palette[256];
110
    int8_t          pal8;
111
    int             cdef[4];
112
    int             tile_width, tile_height;
113
    unsigned        numXtiles, numYtiles;
114
    int             maxtilelen;
115
    AVRational      sar;
116
117
    Jpeg2000CodingStyle codsty[4];
118
    Jpeg2000QuantStyle  qntsty[4];
119
    Jpeg2000POC         poc;
120
    uint8_t             roi_shift[4];
121
122
    int             bit_index;
123
124
    int             curtileno;
125
126
    Jpeg2000Tile    *tile;
127
    Jpeg2000DSPContext dsp;
128
129
    /*options parameters*/
130
    int             reduction_factor;
131
} Jpeg2000DecoderContext;
132
133
/* get_bits functions for JPEG2000 packet bitstream
134
 * It is a get_bit function with a bit-stuffing routine. If the value of the
135
 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
136
 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
137
4305168
static int get_bits(Jpeg2000DecoderContext *s, int n)
138
{
139
4305168
    int res = 0;
140
141
11521813
    while (--n >= 0) {
142
7216645
        res <<= 1;
143
7216645
        if (s->bit_index == 0) {
144
885441
            s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
145
        }
146
7216645
        s->bit_index--;
147
7216645
        res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
148
    }
149
4305168
    return res;
150
}
151
152
28473
static void jpeg2000_flush(Jpeg2000DecoderContext *s)
153
{
154
28473
    if (bytestream2_get_byte(&s->g) == 0xff)
155
        bytestream2_skip(&s->g, 1);
156
28473
    s->bit_index = 8;
157
28473
}
158
159
/* decode the value stored in node */
160
806199
static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
161
                           int threshold)
162
{
163
    Jpeg2000TgtNode *stack[30];
164
806199
    int sp = -1, curval = 0;
165
166
806199
    if (!node) {
167
        av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
168
        return AVERROR_INVALIDDATA;
169
    }
170
171

1944689
    while (node && !node->vis) {
172
1138490
        stack[++sp] = node;
173
1138490
        node        = node->parent;
174
    }
175
176
806199
    if (node)
177
652216
        curval = node->val;
178
    else
179
153983
        curval = stack[sp]->val;
180
181

1917636
    while (curval < threshold && sp >= 0) {
182
1111437
        if (curval < stack[sp]->val)
183
1231
            curval = stack[sp]->val;
184
2206627
        while (curval < threshold) {
185
            int ret;
186
2133975
            if ((ret = get_bits(s, 1)) > 0) {
187
1038785
                stack[sp]->vis++;
188
1038785
                break;
189
1095190
            } else if (!ret)
190
1095190
                curval++;
191
            else
192
                return ret;
193
        }
194
1111437
        stack[sp]->val = curval;
195
1111437
        sp--;
196
    }
197
806199
    return curval;
198
}
199
200
424
static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
201
                         int bpc, uint32_t log2_chroma_wh, int pal8)
202
{
203
424
    int match = 1;
204
424
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
205
206
    av_assert2(desc);
207
208
424
    if (desc->nb_components != components) {
209
11
        return 0;
210
    }
211
212

413
    switch (components) {
213
2
    case 4:
214
4
        match = match && desc->comp[3].depth >= bpc &&
215

5
                         (log2_chroma_wh >> 14 & 3) == 0 &&
216
1
                         (log2_chroma_wh >> 12 & 3) == 0;
217
413
    case 3:
218
825
        match = match && desc->comp[2].depth >= bpc &&
219

1237
                         (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
220
412
                         (log2_chroma_wh >>  8 & 3) == desc->log2_chroma_h;
221
413
    case 2:
222
825
        match = match && desc->comp[1].depth >= bpc &&
223

1237
                         (log2_chroma_wh >>  6 & 3) == desc->log2_chroma_w &&
224
412
                         (log2_chroma_wh >>  4 & 3) == desc->log2_chroma_h;
225
226
413
    case 1:
227
825
        match = match && desc->comp[0].depth >= bpc &&
228
412
                         (log2_chroma_wh >>  2 & 3) == 0 &&
229

1237
                         (log2_chroma_wh       & 3) == 0 &&
230
412
                         (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
231
    }
232
413
    return match;
233
}
234
235
// pix_fmts with lower bpp have to be listed before
236
// similar pix_fmts with higher bpp.
237
#define RGB_PIXEL_FORMATS   AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
238
#define GRAY_PIXEL_FORMATS  AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
239
#define YUV_PIXEL_FORMATS   AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
240
                            AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
241
                            AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
242
                            AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
243
                            AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
244
                            AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
245
                            AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
246
                            AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
247
                            AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
248
                            AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
249
                            AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
250
#define XYZ_PIXEL_FORMATS   AV_PIX_FMT_XYZ12
251
252
static const enum AVPixelFormat rgb_pix_fmts[]  = {RGB_PIXEL_FORMATS};
253
static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
254
static const enum AVPixelFormat yuv_pix_fmts[]  = {YUV_PIXEL_FORMATS};
255
static const enum AVPixelFormat xyz_pix_fmts[]  = {XYZ_PIXEL_FORMATS,
256
                                                   YUV_PIXEL_FORMATS};
257
static const enum AVPixelFormat all_pix_fmts[]  = {RGB_PIXEL_FORMATS,
258
                                                   GRAY_PIXEL_FORMATS,
259
                                                   YUV_PIXEL_FORMATS,
260
                                                   XYZ_PIXEL_FORMATS};
261
262
/* marker segments */
263
/* get sizes and offsets of image, tiles; number of components */
264
412
static int get_siz(Jpeg2000DecoderContext *s)
265
{
266
    int i;
267
    int ncomponents;
268
412
    uint32_t log2_chroma_wh = 0;
269
412
    const enum AVPixelFormat *possible_fmts = NULL;
270
412
    int possible_fmts_nb = 0;
271
    int ret;
272
    int o_dimx, o_dimy; //original image dimensions.
273
    int dimx, dimy;
274
275
412
    if (bytestream2_get_bytes_left(&s->g) < 36) {
276
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
277
        return AVERROR_INVALIDDATA;
278
    }
279
280
412
    s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
281
412
    s->width          = bytestream2_get_be32u(&s->g); // Width
282
412
    s->height         = bytestream2_get_be32u(&s->g); // Height
283
412
    s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
284
412
    s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
285
412
    s->tile_width     = bytestream2_get_be32u(&s->g); // XTSiz
286
412
    s->tile_height    = bytestream2_get_be32u(&s->g); // YTSiz
287
412
    s->tile_offset_x  = bytestream2_get_be32u(&s->g); // XT0Siz
288
412
    s->tile_offset_y  = bytestream2_get_be32u(&s->g); // YT0Siz
289
412
    ncomponents       = bytestream2_get_be16u(&s->g); // CSiz
290
291
412
    if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
292
        avpriv_request_sample(s->avctx, "Large Dimensions");
293
        return AVERROR_PATCHWELCOME;
294
    }
295
296
412
    if (ncomponents <= 0) {
297
        av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
298
               s->ncomponents);
299
        return AVERROR_INVALIDDATA;
300
    }
301
302
412
    if (ncomponents > 4) {
303
        avpriv_request_sample(s->avctx, "Support for %d components",
304
                              ncomponents);
305
        return AVERROR_PATCHWELCOME;
306
    }
307
308

412
    if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
309
412
        s->image_offset_x < s->tile_offset_x ||
310
412
        s->image_offset_y < s->tile_offset_y ||
311
412
        s->tile_width  + (int64_t)s->tile_offset_x <= s->image_offset_x ||
312
412
        s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
313
    ) {
314
        av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
315
        return AVERROR_INVALIDDATA;
316
    }
317
318
412
    s->ncomponents = ncomponents;
319
320

412
    if (s->tile_width <= 0 || s->tile_height <= 0) {
321
        av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
322
               s->tile_width, s->tile_height);
323
        return AVERROR_INVALIDDATA;
324
    }
325
326
412
    if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
327
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
328
        return AVERROR_INVALIDDATA;
329
    }
330
331
1649
    for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
332
1237
        uint8_t x    = bytestream2_get_byteu(&s->g);
333
1237
        s->cbps[i]   = (x & 0x7f) + 1;
334
1237
        s->precision = FFMAX(s->cbps[i], s->precision);
335
1237
        s->sgnd[i]   = !!(x & 0x80);
336
1237
        s->cdx[i]    = bytestream2_get_byteu(&s->g);
337
1237
        s->cdy[i]    = bytestream2_get_byteu(&s->g);
338

1237
        if (   !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
339

1237
            || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
340
            av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
341
            return AVERROR_INVALIDDATA;
342
        }
343
1237
        log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
344
    }
345
346
412
    s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
347
412
    s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
348
349
    // There must be at least a SOT and SOD per tile, their minimum size is 14
350
412
    if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
351
412
        s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
352
    ) {
353
        s->numXtiles = s->numYtiles = 0;
354
        return AVERROR(EINVAL);
355
    }
356
357
412
    s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
358
412
    if (!s->tile) {
359
        s->numXtiles = s->numYtiles = 0;
360
        return AVERROR(ENOMEM);
361
    }
362
363
1743
    for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
364
1331
        Jpeg2000Tile *tile = s->tile + i;
365
366
1331
        tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
367
1331
        if (!tile->comp)
368
            return AVERROR(ENOMEM);
369
    }
370
371
    /* compute image size with reduction factor */
372
412
    o_dimx = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
373
                                               s->reduction_factor);
374
412
    o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
375
                                               s->reduction_factor);
376
412
    dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
377
412
    dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
378
1237
    for (i = 1; i < s->ncomponents; i++) {
379
825
        dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
380
825
        dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
381
    }
382
383
412
    ret = ff_set_dimensions(s->avctx, dimx, dimy);
384
412
    if (ret < 0)
385
        return ret;
386
387
412
    if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
388
409
        s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
389
3
        possible_fmts = xyz_pix_fmts;
390
3
        possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
391
    } else {
392

409
        switch (s->colour_space) {
393
409
        case 16:
394
409
            possible_fmts = rgb_pix_fmts;
395
409
            possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
396
409
            break;
397
        case 17:
398
            possible_fmts = gray_pix_fmts;
399
            possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
400
            break;
401
        case 18:
402
            possible_fmts = yuv_pix_fmts;
403
            possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
404
            break;
405
        default:
406
            possible_fmts = all_pix_fmts;
407
            possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
408
            break;
409
        }
410
    }
411
412
    if (   s->avctx->pix_fmt != AV_PIX_FMT_NONE
412
402
        && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
413
            s->avctx->pix_fmt = AV_PIX_FMT_NONE;
414
412
    if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
415
22
        for (i = 0; i < possible_fmts_nb; ++i) {
416
22
            if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
417
10
                s->avctx->pix_fmt = possible_fmts[i];
418
10
                break;
419
            }
420
        }
421
422
412
    if (i == possible_fmts_nb) {
423
        if (ncomponents == 4 &&
424
            s->cdy[0] == 1 && s->cdx[0] == 1 &&
425
            s->cdy[1] == 1 && s->cdx[1] == 1 &&
426
            s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
427
            if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
428
                s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
429
                s->cdef[0] = 0;
430
                s->cdef[1] = 1;
431
                s->cdef[2] = 2;
432
                s->cdef[3] = 3;
433
                i = 0;
434
            }
435
        } else if (ncomponents == 3 && s->precision == 8 &&
436
                   s->cdx[0] == s->cdx[1] && s->cdx[0] == s->cdx[2] &&
437
                   s->cdy[0] == s->cdy[1] && s->cdy[0] == s->cdy[2]) {
438
            s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
439
            i = 0;
440
        } else if (ncomponents == 2 && s->precision == 8 &&
441
                   s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
442
            s->avctx->pix_fmt = AV_PIX_FMT_YA8;
443
            i = 0;
444
        } else if (ncomponents == 1 && s->precision == 8) {
445
            s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
446
            i = 0;
447
        }
448
    }
449
450
451
412
    if (i == possible_fmts_nb) {
452
        av_log(s->avctx, AV_LOG_ERROR,
453
               "Unknown pix_fmt, profile: %d, colour_space: %d, "
454
               "components: %d, precision: %d\n"
455
               "cdx[0]: %d, cdy[0]: %d\n"
456
               "cdx[1]: %d, cdy[1]: %d\n"
457
               "cdx[2]: %d, cdy[2]: %d\n"
458
               "cdx[3]: %d, cdy[3]: %d\n",
459
               s->avctx->profile, s->colour_space, ncomponents, s->precision,
460
               s->cdx[0],
461
               s->cdy[0],
462
               ncomponents > 1 ? s->cdx[1] : 0,
463
               ncomponents > 1 ? s->cdy[1] : 0,
464
               ncomponents > 2 ? s->cdx[2] : 0,
465
               ncomponents > 2 ? s->cdy[2] : 0,
466
               ncomponents > 3 ? s->cdx[3] : 0,
467
               ncomponents > 3 ? s->cdy[3] : 0);
468
        return AVERROR_PATCHWELCOME;
469
    }
470
412
    s->avctx->bits_per_raw_sample = s->precision;
471
412
    return 0;
472
}
473
474
/* get common part for COD and COC segments */
475
418
static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
476
{
477
    uint8_t byte;
478
479
418
    if (bytestream2_get_bytes_left(&s->g) < 5) {
480
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
481
        return AVERROR_INVALIDDATA;
482
    }
483
484
    /*  nreslevels = number of resolution levels
485
                   = number of decomposition level +1 */
486
418
    c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
487
418
    if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
488
        av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
489
        return AVERROR_INVALIDDATA;
490
    }
491
492
418
    if (c->nreslevels <= s->reduction_factor) {
493
        /* we are forced to update reduction_factor as its requested value is
494
           not compatible with this bitstream, and as we might have used it
495
           already in setup earlier we have to fail this frame until
496
           reinitialization is implemented */
497
        av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
498
        s->reduction_factor = c->nreslevels - 1;
499
        return AVERROR(EINVAL);
500
    }
501
502
    /* compute number of resolution levels to decode */
503
418
    c->nreslevels2decode = c->nreslevels - s->reduction_factor;
504
505
418
    c->log2_cblk_width  = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
506
418
    c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
507
508

418
    if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
509
418
        c->log2_cblk_width + c->log2_cblk_height > 12) {
510
        av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
511
        return AVERROR_INVALIDDATA;
512
    }
513
514
418
    c->cblk_style = bytestream2_get_byteu(&s->g);
515
418
    if (c->cblk_style != 0) { // cblk style
516
        av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
517
        if (c->cblk_style & JPEG2000_CBLK_BYPASS)
518
            av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
519
    }
520
418
    c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
521
    /* set integer 9/7 DWT in case of BITEXACT flag */
522

418
    if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
523
213
        c->transform = FF_DWT97_INT;
524
205
    else if (c->transform == FF_DWT53) {
525
204
        s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
526
    }
527
528
418
    if (c->csty & JPEG2000_CSTY_PREC) {
529
        int i;
530
63
        for (i = 0; i < c->nreslevels; i++) {
531
54
            byte = bytestream2_get_byte(&s->g);
532
54
            c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
533
54
            c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
534
54
            if (i)
535

45
                if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
536
                    av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
537
                           c->log2_prec_widths[i], c->log2_prec_heights[i]);
538
                    c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
539
                    return AVERROR_INVALIDDATA;
540
                }
541
        }
542
    } else {
543
409
        memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
544
409
        memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
545
    }
546
418
    return 0;
547
}
548
549
/* get coding parameters for a particular tile or whole image*/
550
412
static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
551
                   uint8_t *properties)
552
{
553
    Jpeg2000CodingStyle tmp;
554
    int compno, ret;
555
556
412
    if (bytestream2_get_bytes_left(&s->g) < 5) {
557
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
558
        return AVERROR_INVALIDDATA;
559
    }
560
561
412
    tmp.csty = bytestream2_get_byteu(&s->g);
562
563
    // get progression order
564
412
    tmp.prog_order = bytestream2_get_byteu(&s->g);
565
566
412
    tmp.nlayers    = bytestream2_get_be16u(&s->g);
567
412
    tmp.mct        = bytestream2_get_byteu(&s->g); // multiple component transformation
568
569

412
    if (tmp.mct && s->ncomponents < 3) {
570
        av_log(s->avctx, AV_LOG_ERROR,
571
               "MCT %"PRIu8" with too few components (%d)\n",
572
               tmp.mct, s->ncomponents);
573
        return AVERROR_INVALIDDATA;
574
    }
575
576
412
    if ((ret = get_cox(s, &tmp)) < 0)
577
        return ret;
578
412
    tmp.init = 1;
579
1649
    for (compno = 0; compno < s->ncomponents; compno++)
580
1237
        if (!(properties[compno] & HAD_COC))
581
1237
            memcpy(c + compno, &tmp, sizeof(tmp));
582
412
    return 0;
583
}
584
585
/* Get coding parameters for a component in the whole image or a
586
 * particular tile. */
587
6
static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
588
                   uint8_t *properties)
589
{
590
    int compno, ret;
591
    uint8_t has_eph;
592
593
6
    if (bytestream2_get_bytes_left(&s->g) < 2) {
594
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
595
        return AVERROR_INVALIDDATA;
596
    }
597
598
6
    compno = bytestream2_get_byteu(&s->g);
599
600
6
    if (compno >= s->ncomponents) {
601
        av_log(s->avctx, AV_LOG_ERROR,
602
               "Invalid compno %d. There are %d components in the image.\n",
603
               compno, s->ncomponents);
604
        return AVERROR_INVALIDDATA;
605
    }
606
607
6
    c      += compno;
608
6
    has_eph = c->csty & JPEG2000_CSTY_EPH;
609
6
    c->csty = bytestream2_get_byteu(&s->g);
610
6
    c->csty |= has_eph; //do not override eph present bits from COD
611
612
6
    if ((ret = get_cox(s, c)) < 0)
613
        return ret;
614
615
6
    properties[compno] |= HAD_COC;
616
6
    c->init = 1;
617
6
    return 0;
618
}
619
620
static int get_rgn(Jpeg2000DecoderContext *s, int n)
621
{
622
    uint16_t compno;
623
    compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
624
                                     bytestream2_get_be16u(&s->g);
625
    if (bytestream2_get_byte(&s->g)) {
626
        av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
627
        return AVERROR_INVALIDDATA; // SRgn field value is 0
628
    }
629
    // SPrgn field
630
    // Currently compno cannot be greater than 4.
631
    // However, future implementation should support compno up to 65536
632
    if (compno < s->ncomponents) {
633
        int v;
634
        if (s->curtileno == -1) {
635
            v =  bytestream2_get_byte(&s->g);
636
            if (v > 30)
637
                return AVERROR_PATCHWELCOME;
638
            s->roi_shift[compno] = v;
639
        } else {
640
            if (s->tile[s->curtileno].tp_idx != 0)
641
                return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
642
            v = bytestream2_get_byte(&s->g);
643
            if (v > 30)
644
                return AVERROR_PATCHWELCOME;
645
            s->tile[s->curtileno].comp[compno].roi_shift = v;
646
        }
647
        return 0;
648
    }
649
    return AVERROR_INVALIDDATA;
650
}
651
652
/* Get common part for QCD and QCC segments. */
653
421
static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
654
{
655
    int i, x;
656
657
421
    if (bytestream2_get_bytes_left(&s->g) < 1)
658
        return AVERROR_INVALIDDATA;
659
660
421
    x = bytestream2_get_byteu(&s->g); // Sqcd
661
662
421
    q->nguardbits = x >> 5;
663
421
    q->quantsty   = x & 0x1f;
664
665
421
    if (q->quantsty == JPEG2000_QSTY_NONE) {
666
204
        n -= 3;
667

204
        if (bytestream2_get_bytes_left(&s->g) < n ||
668
            n > JPEG2000_MAX_DECLEVELS*3)
669
            return AVERROR_INVALIDDATA;
670
4080
        for (i = 0; i < n; i++)
671
3876
            q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
672
217
    } else if (q->quantsty == JPEG2000_QSTY_SI) {
673
        if (bytestream2_get_bytes_left(&s->g) < 2)
674
            return AVERROR_INVALIDDATA;
675
        x          = bytestream2_get_be16u(&s->g);
676
        q->expn[0] = x >> 11;
677
        q->mant[0] = x & 0x7ff;
678
        for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
679
            int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
680
            q->expn[i] = curexpn;
681
            q->mant[i] = q->mant[0];
682
        }
683
    } else {
684
217
        n = (n - 3) >> 1;
685

217
        if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
686
            n > JPEG2000_MAX_DECLEVELS*3)
687
            return AVERROR_INVALIDDATA;
688
4301
        for (i = 0; i < n; i++) {
689
4084
            x          = bytestream2_get_be16u(&s->g);
690
4084
            q->expn[i] = x >> 11;
691
4084
            q->mant[i] = x & 0x7ff;
692
        }
693
    }
694
421
    return 0;
695
}
696
697
/* Get quantization parameters for a particular tile or a whole image. */
698
412
static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
699
                   uint8_t *properties)
700
{
701
    Jpeg2000QuantStyle tmp;
702
    int compno, ret;
703
704
412
    memset(&tmp, 0, sizeof(tmp));
705
706
412
    if ((ret = get_qcx(s, n, &tmp)) < 0)
707
        return ret;
708
1649
    for (compno = 0; compno < s->ncomponents; compno++)
709
1237
        if (!(properties[compno] & HAD_QCC))
710
1237
            memcpy(q + compno, &tmp, sizeof(tmp));
711
412
    return 0;
712
}
713
714
/* Get quantization parameters for a component in the whole image
715
 * on in a particular tile. */
716
9
static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
717
                   uint8_t *properties)
718
{
719
    int compno;
720
721
9
    if (bytestream2_get_bytes_left(&s->g) < 1)
722
        return AVERROR_INVALIDDATA;
723
724
9
    compno = bytestream2_get_byteu(&s->g);
725
726
9
    if (compno >= s->ncomponents) {
727
        av_log(s->avctx, AV_LOG_ERROR,
728
               "Invalid compno %d. There are %d components in the image.\n",
729
               compno, s->ncomponents);
730
        return AVERROR_INVALIDDATA;
731
    }
732
733
9
    properties[compno] |= HAD_QCC;
734
9
    return get_qcx(s, n - 1, q + compno);
735
}
736
737
static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
738
{
739
    int i;
740
    int elem_size = s->ncomponents <= 257 ? 7 : 9;
741
    Jpeg2000POC tmp = {{{0}}};
742
743
    if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
744
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
745
        return AVERROR_INVALIDDATA;
746
    }
747
748
    if (elem_size > 7) {
749
        avpriv_request_sample(s->avctx, "Fat POC not supported");
750
        return AVERROR_PATCHWELCOME;
751
    }
752
753
    tmp.nb_poc = (size - 2) / elem_size;
754
    if (tmp.nb_poc > MAX_POCS) {
755
        avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
756
        return AVERROR_PATCHWELCOME;
757
    }
758
759
    for (i = 0; i<tmp.nb_poc; i++) {
760
        Jpeg2000POCEntry *e = &tmp.poc[i];
761
        e->RSpoc  = bytestream2_get_byteu(&s->g);
762
        e->CSpoc  = bytestream2_get_byteu(&s->g);
763
        e->LYEpoc = bytestream2_get_be16u(&s->g);
764
        e->REpoc  = bytestream2_get_byteu(&s->g);
765
        e->CEpoc  = bytestream2_get_byteu(&s->g);
766
        e->Ppoc   = bytestream2_get_byteu(&s->g);
767
        if (!e->CEpoc)
768
            e->CEpoc = 256;
769
        if (e->CEpoc > s->ncomponents)
770
            e->CEpoc = s->ncomponents;
771
        if (   e->RSpoc >= e->REpoc || e->REpoc > 33
772
            || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
773
            || !e->LYEpoc) {
774
            av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
775
                e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
776
            );
777
            return AVERROR_INVALIDDATA;
778
        }
779
    }
780
781
    if (!p->nb_poc || p->is_default) {
782
        *p = tmp;
783
    } else {
784
        if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
785
            av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
786
            return AVERROR_INVALIDDATA;
787
        }
788
        memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
789
        p->nb_poc += tmp.nb_poc;
790
    }
791
792
    p->is_default = 0;
793
794
    return 0;
795
}
796
797
798
/* Get start of tile segment. */
799
1343
static int get_sot(Jpeg2000DecoderContext *s, int n)
800
{
801
    Jpeg2000TilePart *tp;
802
    uint16_t Isot;
803
    uint32_t Psot;
804
    unsigned TPsot;
805
806
1343
    if (bytestream2_get_bytes_left(&s->g) < 8)
807
        return AVERROR_INVALIDDATA;
808
809
1343
    s->curtileno = 0;
810
1343
    Isot = bytestream2_get_be16u(&s->g);        // Isot
811
1343
    if (Isot >= s->numXtiles * s->numYtiles)
812
        return AVERROR_INVALIDDATA;
813
814
1343
    s->curtileno = Isot;
815
1343
    Psot  = bytestream2_get_be32u(&s->g);       // Psot
816
1343
    TPsot = bytestream2_get_byteu(&s->g);       // TPsot
817
818
    /* Read TNSot but not used */
819
1343
    bytestream2_get_byteu(&s->g);               // TNsot
820
821
1343
    if (!Psot)
822
        Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
823
824
1343
    if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
825
        av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
826
        return AVERROR_INVALIDDATA;
827
    }
828
829
1343
    if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
830
        avpriv_request_sample(s->avctx, "Too many tile parts");
831
        return AVERROR_PATCHWELCOME;
832
    }
833
834
1343
    s->tile[Isot].tp_idx = TPsot;
835
1343
    tp             = s->tile[Isot].tile_part + TPsot;
836
1343
    tp->tile_index = Isot;
837
1343
    tp->tp_end     = s->g.buffer + Psot - n - 2;
838
839
1343
    if (!TPsot) {
840
1331
        Jpeg2000Tile *tile = s->tile + s->curtileno;
841
842
        /* copy defaults */
843
1331
        memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
844
1331
        memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
845
1331
        memcpy(&tile->poc  , &s->poc  , sizeof(tile->poc));
846
1331
        tile->poc.is_default = 1;
847
    }
848
849
1343
    return 0;
850
}
851
852
static int read_crg(Jpeg2000DecoderContext *s, int n)
853
{
854
    if (s->ncomponents*4 != n - 2) {
855
        av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
856
        return AVERROR_INVALIDDATA;
857
    }
858
    bytestream2_skip(&s->g, n - 2);
859
    return 0;
860
}
861
/* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
862
 * Used to know the number of tile parts and lengths.
863
 * There may be multiple TLMs in the header.
864
 * TODO: The function is not used for tile-parts management, nor anywhere else.
865
 * It can be useful to allocate memory for tile parts, before managing the SOT
866
 * markers. Parsing the TLM header is needed to increment the input header
867
 * buffer.
868
 * This marker is mandatory for DCI. */
869
4
static int get_tlm(Jpeg2000DecoderContext *s, int n)
870
{
871
    uint8_t Stlm, ST, SP, tile_tlm, i;
872
4
    bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
873
4
    Stlm = bytestream2_get_byte(&s->g);
874
875
    // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
876
4
    ST = (Stlm >> 4) & 0x03;
877
4
    if (ST == 0x03) {
878
        av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
879
        return AVERROR_INVALIDDATA;
880
    }
881
882
4
    SP       = (Stlm >> 6) & 0x01;
883
4
    tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
884
21
    for (i = 0; i < tile_tlm; i++) {
885

17
        switch (ST) {
886
        case 0:
887
            break;
888
17
        case 1:
889
17
            bytestream2_get_byte(&s->g);
890
17
            break;
891
        case 2:
892
            bytestream2_get_be16(&s->g);
893
            break;
894
        case 3:
895
            bytestream2_get_be32(&s->g);
896
            break;
897
        }
898
17
        if (SP == 0) {
899
            bytestream2_get_be16(&s->g);
900
        } else {
901
17
            bytestream2_get_be32(&s->g);
902
        }
903
    }
904
4
    return 0;
905
}
906
907
static int get_plt(Jpeg2000DecoderContext *s, int n)
908
{
909
    int i;
910
    int v;
911
912
    av_log(s->avctx, AV_LOG_DEBUG,
913
            "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
914
915
    if (n < 4)
916
        return AVERROR_INVALIDDATA;
917
918
    /*Zplt =*/ bytestream2_get_byte(&s->g);
919
920
    for (i = 0; i < n - 3; i++) {
921
        v = bytestream2_get_byte(&s->g);
922
    }
923
    if (v & 0x80)
924
        return AVERROR_INVALIDDATA;
925
926
    return 0;
927
}
928
929
static int get_ppt(Jpeg2000DecoderContext *s, int n)
930
{
931
    Jpeg2000Tile *tile;
932
    void *new;
933
934
    if (n < 3) {
935
        av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
936
        return AVERROR_INVALIDDATA;
937
    }
938
    if (s->curtileno < 0)
939
        return AVERROR_INVALIDDATA;
940
941
    tile = &s->tile[s->curtileno];
942
    if (tile->tp_idx != 0) {
943
        av_log(s->avctx, AV_LOG_ERROR,
944
               "PPT marker can occur only on first tile part of a tile.\n");
945
        return AVERROR_INVALIDDATA;
946
    }
947
948
    tile->has_ppt = 1;  // this tile has a ppt marker
949
    bytestream2_get_byte(&s->g); // Zppt is skipped and not used
950
    new = av_realloc(tile->packed_headers,
951
                     tile->packed_headers_size + n - 3);
952
    if (new) {
953
        tile->packed_headers = new;
954
    } else
955
        return AVERROR(ENOMEM);
956
    memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
957
    memcpy(tile->packed_headers + tile->packed_headers_size,
958
           s->g.buffer, n - 3);
959
    tile->packed_headers_size += n - 3;
960
    bytestream2_skip(&s->g, n - 3);
961
962
    return 0;
963
}
964
965
1331
static int init_tile(Jpeg2000DecoderContext *s, int tileno)
966
{
967
    int compno;
968
1331
    int tilex = tileno % s->numXtiles;
969
1331
    int tiley = tileno / s->numXtiles;
970
1331
    Jpeg2000Tile *tile = s->tile + tileno;
971
972
1331
    if (!tile->comp)
973
        return AVERROR(ENOMEM);
974
975
1331
    tile->coord[0][0] = av_clip(tilex       * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
976
1331
    tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
977
1331
    tile->coord[1][0] = av_clip(tiley       * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
978
1331
    tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
979
980
5326
    for (compno = 0; compno < s->ncomponents; compno++) {
981
3995
        Jpeg2000Component *comp = tile->comp + compno;
982
3995
        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
983
3995
        Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
984
        int ret; // global bandno
985
986
3995
        comp->coord_o[0][0] = tile->coord[0][0];
987
3995
        comp->coord_o[0][1] = tile->coord[0][1];
988
3995
        comp->coord_o[1][0] = tile->coord[1][0];
989
3995
        comp->coord_o[1][1] = tile->coord[1][1];
990
991
3995
        comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
992
3995
        comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
993
3995
        comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
994
3995
        comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
995
996
3995
        comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
997
3995
        comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
998
3995
        comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
999
3995
        comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
1000
1001
3995
        if (!comp->roi_shift)
1002
3995
            comp->roi_shift = s->roi_shift[compno];
1003
3995
        if (!codsty->init)
1004
            return AVERROR_INVALIDDATA;
1005
3995
        if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
1006
3995
                                             s->cbps[compno], s->cdx[compno],
1007
                                             s->cdy[compno], s->avctx))
1008
            return ret;
1009
    }
1010
1331
    return 0;
1011
}
1012
1013
/* Read the number of coding passes. */
1014
364564
static int getnpasses(Jpeg2000DecoderContext *s)
1015
{
1016
    int num;
1017
364564
    if (!get_bits(s, 1))
1018
21434
        return 1;
1019
343130
    if (!get_bits(s, 1))
1020
12976
        return 2;
1021
330154
    if ((num = get_bits(s, 2)) != 3)
1022
110502
        return num < 0 ? num : 3 + num;
1023
219652
    if ((num = get_bits(s, 5)) != 31)
1024
219649
        return num < 0 ? num : 6 + num;
1025
3
    num = get_bits(s, 7);
1026
3
    return num < 0 ? num : 37 + num;
1027
}
1028
1029
364564
static int getlblockinc(Jpeg2000DecoderContext *s)
1030
{
1031
364564
    int res = 0, ret;
1032
518597
    while (ret = get_bits(s, 1)) {
1033
154033
        if (ret < 0)
1034
            return ret;
1035
154033
        res++;
1036
    }
1037
364564
    return res;
1038
}
1039
1040
28473
static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1041
                                 int *tp_index)
1042
{
1043
28473
    s->g = tile->tile_part[*tp_index].tpg;
1044

28473
    if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1045
12
        if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1046
12
            s->g = tile->tile_part[++(*tp_index)].tpg;
1047
        }
1048
    }
1049
28473
    if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1050
        bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1051
28473
}
1052
1053
28473
static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1054
                                  Jpeg2000CodingStyle *codsty,
1055
                                  Jpeg2000ResLevel *rlevel, int precno,
1056
                                  int layno, uint8_t *expn, int numgbits)
1057
{
1058
    int bandno, cblkno, ret, nb_code_blocks;
1059
    int cwsno;
1060
1061
28473
    if (layno < rlevel->band[0].prec[precno].decoded_layers)
1062
        return 0;
1063
28473
    rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1064
    // Select stream to read from
1065
28473
    if (tile->has_ppt)
1066
        s->g = tile->packed_headers_stream;
1067
    else
1068
28473
        select_stream(s, tile, tp_index);
1069
1070
28473
    if (!(ret = get_bits(s, 1))) {
1071
        jpeg2000_flush(s);
1072
        goto skip_data;
1073
28473
    } else if (ret < 0)
1074
        return ret;
1075
1076
105886
    for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1077
77413
        Jpeg2000Band *band = rlevel->band + bandno;
1078
77413
        Jpeg2000Prec *prec = band->prec + precno;
1079
1080
77413
        if (band->coord[0][0] == band->coord[0][1] ||
1081
77413
            band->coord[1][0] == band->coord[1][1])
1082
3672
            continue;
1083
73741
        nb_code_blocks =  prec->nb_codeblocks_height *
1084
73741
                          prec->nb_codeblocks_width;
1085
519268
        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1086
445527
            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1087
            int incl, newpasses, llen;
1088
            void *tmp;
1089
1090
445527
            if (cblk->npasses)
1091
2056
                incl = get_bits(s, 1);
1092
            else
1093
443471
                incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1094
445527
            if (!incl)
1095
80963
                continue;
1096
364564
            else if (incl < 0)
1097
                return incl;
1098
1099
364564
            if (!cblk->npasses) {
1100
725456
                int v = expn[bandno] + numgbits - 1 -
1101
362728
                        tag_tree_decode(s, prec->zerobits + cblkno, 100);
1102

362728
                if (v < 0 || v > 30) {
1103
                    av_log(s->avctx, AV_LOG_ERROR,
1104
                           "nonzerobits %d invalid or unsupported\n", v);
1105
                    return AVERROR_INVALIDDATA;
1106
                }
1107
362728
                cblk->nonzerobits = v;
1108
            }
1109
364564
            if ((newpasses = getnpasses(s)) < 0)
1110
                return newpasses;
1111
            av_assert2(newpasses > 0);
1112
364564
            if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1113
                avpriv_request_sample(s->avctx, "Too many passes");
1114
                return AVERROR_PATCHWELCOME;
1115
            }
1116
364564
            if ((llen = getlblockinc(s)) < 0)
1117
                return llen;
1118
364564
            if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1119
                avpriv_request_sample(s->avctx,
1120
                                      "Block with length beyond 16 bits");
1121
                return AVERROR_PATCHWELCOME;
1122
            }
1123
1124
364564
            cblk->lblock += llen;
1125
1126
364564
            cblk->nb_lengthinc = 0;
1127
364564
            cblk->nb_terminationsinc = 0;
1128
364564
            av_free(cblk->lengthinc);
1129
364564
            cblk->lengthinc  = av_mallocz_array(newpasses    , sizeof(*cblk->lengthinc));
1130
364564
            if (!cblk->lengthinc)
1131
                return AVERROR(ENOMEM);
1132
364564
            tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1133
364564
            if (!tmp)
1134
                return AVERROR(ENOMEM);
1135
364564
            cblk->data_start = tmp;
1136
            do {
1137
364564
                int newpasses1 = 0;
1138
1139
3089579
                while (newpasses1 < newpasses) {
1140
2725015
                    newpasses1 ++;
1141
2725015
                    if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1142
                        cblk->nb_terminationsinc ++;
1143
                        break;
1144
                    }
1145
                }
1146
1147
364564
                if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1148
                    return ret;
1149
364564
                if (ret > cblk->data_allocated) {
1150
363060
                    size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1151
363060
                    void *new = av_realloc(cblk->data, new_size);
1152
363060
                    if (new) {
1153
363060
                        cblk->data = new;
1154
363060
                        cblk->data_allocated = new_size;
1155
                    }
1156
                }
1157
364564
                if (ret > cblk->data_allocated) {
1158
                    avpriv_request_sample(s->avctx,
1159
                                        "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1160
                                        cblk->data_allocated);
1161
                    return AVERROR_PATCHWELCOME;
1162
                }
1163
364564
                cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1164
364564
                cblk->npasses  += newpasses1;
1165
364564
                newpasses -= newpasses1;
1166
364564
            } while(newpasses);
1167
        }
1168
    }
1169
28473
    jpeg2000_flush(s);
1170
1171
28473
    if (codsty->csty & JPEG2000_CSTY_EPH) {
1172
        if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1173
            bytestream2_skip(&s->g, 2);
1174
        else
1175
            av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1176
    }
1177
1178
    // Save state of stream
1179
28473
    if (tile->has_ppt) {
1180
        tile->packed_headers_stream = s->g;
1181
        select_stream(s, tile, tp_index);
1182
    }
1183
105886
    for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1184
77413
        Jpeg2000Band *band = rlevel->band + bandno;
1185
77413
        Jpeg2000Prec *prec = band->prec + precno;
1186
1187
77413
        nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1188
526612
        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1189
449199
            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1190

449199
            if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1191
84635
                continue;
1192
729128
            for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1193
364564
                if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1194
364272
                    size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1195
364272
                    void *new = av_realloc(cblk->data, new_size);
1196
364272
                    if (new) {
1197
364272
                        cblk->data = new;
1198
364272
                        cblk->data_allocated = new_size;
1199
                    }
1200
                }
1201
364564
                if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1202
364564
                    || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1203
                ) {
1204
                    av_log(s->avctx, AV_LOG_ERROR,
1205
                        "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1206
                        cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1207
                    return AVERROR_INVALIDDATA;
1208
                }
1209
1210
364564
                bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1211
364564
                cblk->length   += cblk->lengthinc[cwsno];
1212
364564
                cblk->lengthinc[cwsno] = 0;
1213
364564
                if (cblk->nb_terminationsinc) {
1214
                    cblk->nb_terminationsinc--;
1215
                    cblk->nb_terminations++;
1216
                    cblk->data[cblk->length++] = 0xFF;
1217
                    cblk->data[cblk->length++] = 0xFF;
1218
                    cblk->data_start[cblk->nb_terminations] = cblk->length;
1219
                }
1220
            }
1221
364564
            av_freep(&cblk->lengthinc);
1222
        }
1223
    }
1224
    // Save state of stream
1225
28473
    tile->tile_part[*tp_index].tpg = s->g;
1226
28473
    return 0;
1227
1228
skip_data:
1229
    if (tile->has_ppt)
1230
        tile->packed_headers_stream = s->g;
1231
    else
1232
        tile->tile_part[*tp_index].tpg = s->g;
1233
    return 0;
1234
}
1235
1236
1331
static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1237
                                             int RSpoc, int CSpoc,
1238
                                             int LYEpoc, int REpoc, int CEpoc,
1239
                                             int Ppoc, int *tp_index)
1240
{
1241
1331
    int ret = 0;
1242
    int layno, reslevelno, compno, precno, ok_reslevel;
1243
    int x, y;
1244
    int step_x, step_y;
1245
1246

1331
    switch (Ppoc) {
1247
    case JPEG2000_PGOD_RLCP:
1248
        av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1249
        ok_reslevel = 1;
1250
        for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1251
            ok_reslevel = 0;
1252
            for (layno = 0; layno < LYEpoc; layno++) {
1253
                for (compno = CSpoc; compno < CEpoc; compno++) {
1254
                    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1255
                    Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1256
                    if (reslevelno < codsty->nreslevels) {
1257
                        Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1258
                                                reslevelno;
1259
                        ok_reslevel = 1;
1260
                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1261
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1262
                                                              codsty, rlevel,
1263
                                                              precno, layno,
1264
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1265
                                                              qntsty->nguardbits)) < 0)
1266
                                return ret;
1267
                    }
1268
                }
1269
            }
1270
        }
1271
        break;
1272
1273
1326
    case JPEG2000_PGOD_LRCP:
1274
1326
        av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1275
2652
        for (layno = 0; layno < LYEpoc; layno++) {
1276
1326
            ok_reslevel = 1;
1277

11934
            for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1278
10608
                ok_reslevel = 0;
1279
42432
                for (compno = CSpoc; compno < CEpoc; compno++) {
1280
31824
                    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1281
31824
                    Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1282
31824
                    if (reslevelno < codsty->nreslevels) {
1283
27846
                        Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1284
                                                reslevelno;
1285
27846
                        ok_reslevel = 1;
1286
55692
                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1287
27846
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1288
                                                              codsty, rlevel,
1289
                                                              precno, layno,
1290
27846
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1291
27846
                                                              qntsty->nguardbits)) < 0)
1292
                                return ret;
1293
                    }
1294
                }
1295
            }
1296
        }
1297
1326
        break;
1298
1299
5
    case JPEG2000_PGOD_CPRL:
1300
5
        av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1301
22
        for (compno = CSpoc; compno < CEpoc; compno++) {
1302
17
            Jpeg2000Component *comp     = tile->comp + compno;
1303
17
            Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1304
17
            Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1305
17
            step_x = 32;
1306
17
            step_y = 32;
1307
1308
17
            if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1309
                continue;
1310
1311
119
            for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1312
102
                uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1313
102
                Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1314
102
                step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1315
102
                step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1316
            }
1317

17
            if (step_x >= 31 || step_y >= 31){
1318
                avpriv_request_sample(s->avctx, "CPRL with large step");
1319
                return AVERROR_PATCHWELCOME;
1320
            }
1321
17
            step_x = 1<<step_x;
1322
17
            step_y = 1<<step_y;
1323
1324
70
            for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1325
421
                for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1326
2576
                    for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1327
                        unsigned prcx, prcy;
1328
2208
                        uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1329
2208
                        Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1330
2208
                        int xc = x / s->cdx[compno];
1331
2208
                        int yc = y / s->cdy[compno];
1332
1333

2208
                        if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1334
1224
                            continue;
1335
1336

984
                        if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1337
405
                            continue;
1338
1339
                        // check if a precinct exists
1340
579
                        prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1341
579
                        prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1342
579
                        prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1343
579
                        prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1344
1345
579
                        precno = prcx + rlevel->num_precincts_x * prcy;
1346
1347

579
                        if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1348
                            av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1349
                                   prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1350
                            continue;
1351
                        }
1352
1353
1206
                        for (layno = 0; layno < LYEpoc; layno++) {
1354
627
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1355
                                                              precno, layno,
1356
627
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1357
627
                                                              qntsty->nguardbits)) < 0)
1358
                                return ret;
1359
                        }
1360
                    }
1361
                }
1362
            }
1363
        }
1364
5
        break;
1365
1366
    case JPEG2000_PGOD_RPCL:
1367
        av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1368
        ok_reslevel = 1;
1369
        for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1370
            ok_reslevel = 0;
1371
            step_x = 30;
1372
            step_y = 30;
1373
            for (compno = CSpoc; compno < CEpoc; compno++) {
1374
                Jpeg2000Component *comp     = tile->comp + compno;
1375
                Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1376
1377
                if (reslevelno < codsty->nreslevels) {
1378
                    uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1379
                    Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1380
                    step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1381
                    step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1382
                }
1383
            }
1384
            step_x = 1<<step_x;
1385
            step_y = 1<<step_y;
1386
1387
            for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1388
                for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1389
                    for (compno = CSpoc; compno < CEpoc; compno++) {
1390
                        Jpeg2000Component *comp     = tile->comp + compno;
1391
                        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1392
                        Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1393
                        uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1394
                        Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1395
                        unsigned prcx, prcy;
1396
1397
                        int xc = x / s->cdx[compno];
1398
                        int yc = y / s->cdy[compno];
1399
1400
                        if (reslevelno >= codsty->nreslevels)
1401
                            continue;
1402
1403
                        if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1404
                            continue;
1405
1406
                        if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1407
                            continue;
1408
1409
                        // check if a precinct exists
1410
                        prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1411
                        prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1412
                        prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1413
                        prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1414
1415
                        precno = prcx + rlevel->num_precincts_x * prcy;
1416
1417
                        ok_reslevel = 1;
1418
                        if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1419
                            av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1420
                                   prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1421
                            continue;
1422
                        }
1423
1424
                        for (layno = 0; layno < LYEpoc; layno++) {
1425
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1426
                                                              codsty, rlevel,
1427
                                                              precno, layno,
1428
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1429
                                                              qntsty->nguardbits)) < 0)
1430
                                return ret;
1431
                        }
1432
                    }
1433
                }
1434
            }
1435
        }
1436
        break;
1437
1438
    case JPEG2000_PGOD_PCRL:
1439
        av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1440
        step_x = 32;
1441
        step_y = 32;
1442
        for (compno = CSpoc; compno < CEpoc; compno++) {
1443
            Jpeg2000Component *comp     = tile->comp + compno;
1444
            Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1445
1446
            for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1447
                uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1448
                Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1449
                step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1450
                step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1451
            }
1452
        }
1453
        if (step_x >= 31 || step_y >= 31){
1454
            avpriv_request_sample(s->avctx, "PCRL with large step");
1455
            return AVERROR_PATCHWELCOME;
1456
        }
1457
        step_x = 1<<step_x;
1458
        step_y = 1<<step_y;
1459
1460
        for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1461
            for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1462
                for (compno = CSpoc; compno < CEpoc; compno++) {
1463
                    Jpeg2000Component *comp     = tile->comp + compno;
1464
                    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1465
                    Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1466
                    int xc = x / s->cdx[compno];
1467
                    int yc = y / s->cdy[compno];
1468
1469
                    for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1470
                        unsigned prcx, prcy;
1471
                        uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1472
                        Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1473
1474
                        if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1475
                            continue;
1476
1477
                        if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1478
                            continue;
1479
1480
                        // check if a precinct exists
1481
                        prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1482
                        prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1483
                        prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1484
                        prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1485
1486
                        precno = prcx + rlevel->num_precincts_x * prcy;
1487
1488
                        if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1489
                            av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1490
                                   prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1491
                            continue;
1492
                        }
1493
1494
                        for (layno = 0; layno < LYEpoc; layno++) {
1495
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1496
                                                              precno, layno,
1497
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1498
                                                              qntsty->nguardbits)) < 0)
1499
                                return ret;
1500
                        }
1501
                    }
1502
                }
1503
            }
1504
        }
1505
        break;
1506
1507
    default:
1508
        break;
1509
    }
1510
1511
1331
    return ret;
1512
}
1513
1514
1331
static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1515
{
1516
1331
    int ret = AVERROR_BUG;
1517
    int i;
1518
1331
    int tp_index = 0;
1519
1520
1331
    s->bit_index = 8;
1521
1331
    if (tile->poc.nb_poc) {
1522
        for (i=0; i<tile->poc.nb_poc; i++) {
1523
            Jpeg2000POCEntry *e = &tile->poc.poc[i];
1524
            ret = jpeg2000_decode_packets_po_iteration(s, tile,
1525
                e->RSpoc, e->CSpoc,
1526
                FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1527
                e->REpoc,
1528
                FFMIN(e->CEpoc, s->ncomponents),
1529
                e->Ppoc, &tp_index
1530
                );
1531
            if (ret < 0)
1532
                return ret;
1533
        }
1534
    } else {
1535
1331
        ret = jpeg2000_decode_packets_po_iteration(s, tile,
1536
            0, 0,
1537
1331
            tile->codsty[0].nlayers,
1538
            33,
1539
            s->ncomponents,
1540
1331
            tile->codsty[0].prog_order,
1541
            &tp_index
1542
        );
1543
    }
1544
    /* EOC marker reached */
1545
1331
    bytestream2_skip(&s->g, 2);
1546
1547
1331
    return ret;
1548
}
1549
1550
/* TIER-1 routines */
1551
877598
static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1552
                           int bpno, int bandno,
1553
                           int vert_causal_ctx_csty_symbol)
1554
{
1555
877598
    int mask = 3 << (bpno - 1), y0, x, y;
1556
1557
3778218
    for (y0 = 0; y0 < height; y0 += 4)
1558
50688616
        for (x = 0; x < width; x++)
1559

237307613
            for (y = y0; y < height && y < y0 + 4; y++) {
1560
189519617
                int flags_mask = -1;
1561

189519617
                if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1562
                    flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1563
189519617
                if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1564
73928626
                && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1565
51305930
                    if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1566
14815869
                        int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1567
14815869
                        if (t1->mqc.raw)
1568
                             t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1569
                        else
1570
29631738
                             t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1571
14815869
                                               -mask : mask;
1572
1573
14815869
                        ff_jpeg2000_set_significance(t1, x, y,
1574
14815869
                                                     t1->data[(y) * t1->stride + x] < 0);
1575
                    }
1576
51305930
                    t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1577
                }
1578
            }
1579
877598
}
1580
1581
774996
static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1582
                           int bpno, int vert_causal_ctx_csty_symbol)
1583
{
1584
    int phalf, nhalf;
1585
    int y0, x, y;
1586
1587
774996
    phalf = 1 << (bpno - 1);
1588
774996
    nhalf = -phalf;
1589
1590
3332385
    for (y0 = 0; y0 < height; y0 += 4)
1591
44968233
        for (x = 0; x < width; x++)
1592

210634860
            for (y = y0; y < height && y < y0 + 4; y++)
1593
168224016
                if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1594
                    int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1595
17836470
                        ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1596
17836470
                    int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1597
35672940
                    int r     = ff_mqc_decode(&t1->mqc,
1598
17836470
                                              t1->mqc.cx_states + ctxno)
1599
17836470
                                ? phalf : nhalf;
1600
17836470
                    t1->data[(y) * t1->stride + x]          += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1601
17836470
                    t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1602
                }
1603
774996
}
1604
1605
1072421
static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1606
                           int width, int height, int bpno, int bandno,
1607
                           int seg_symbols, int vert_causal_ctx_csty_symbol)
1608
{
1609
1072421
    int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1610
1611
4706924
    for (y0 = 0; y0 < height; y0 += 4) {
1612
64726351
        for (x = 0; x < width; x++) {
1613
61091848
            int flags_mask = -1;
1614
61091848
            if (vert_causal_ctx_csty_symbol)
1615
                flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1616
61091848
            if (y0 + 3 < height &&
1617
60371743
                !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1618
40370473
                  (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1619
38034169
                  (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1620
35951268
                  (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1621
35159913
                if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1622
32594876
                    continue;
1623
2565037
                runlen = ff_mqc_decode(&t1->mqc,
1624
2565037
                                       t1->mqc.cx_states + MQC_CX_UNI);
1625
5130074
                runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1626
2565037
                                                       t1->mqc.cx_states +
1627
                                                       MQC_CX_UNI);
1628
2565037
                dec = 1;
1629
            } else {
1630
25931935
                runlen = 0;
1631
25931935
                dec    = 0;
1632
            }
1633
1634

136600024
            for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1635
108103052
                int flags_mask = -1;
1636

108103052
                if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1637
                    flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1638
108103052
                if (!dec) {
1639
105538015
                    if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1640
49943741
                        dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1641
                                                                                             bandno));
1642
                    }
1643
                }
1644
108103052
                if (dec) {
1645
                    int xorbit;
1646
8653736
                    int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1647
                                                        &xorbit);
1648
17307472
                    t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1649
8653736
                                                    t1->mqc.cx_states + ctxno) ^
1650
                                      xorbit)
1651
8653736
                                     ? -mask : mask;
1652
8653736
                    ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1653
                }
1654
108103052
                dec = 0;
1655
108103052
                t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1656
            }
1657
        }
1658
    }
1659
1072421
    if (seg_symbols) {
1660
        int val;
1661
        val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1662
        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1663
        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1664
        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1665
        if (val != 0xa)
1666
            av_log(s->avctx, AV_LOG_ERROR,
1667
                   "Segmentation symbol value incorrect\n");
1668
    }
1669
1072421
}
1670
1671
443079
static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1672
                       Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1673
                       int width, int height, int bandpos, uint8_t roi_shift)
1674
{
1675
443079
    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1676
443079
    int pass_cnt = 0;
1677
443079
    int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1678
443079
    int term_cnt = 0;
1679
    int coder_type;
1680
1681

443079
    av_assert0(width <= 1024U && height <= 1024U);
1682
443079
    av_assert0(width*height <= 4096);
1683
1684
443079
    memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1685
1686
    /* If code-block contains no compressed data: nothing to do. */
1687
443079
    if (!cblk->length)
1688
80351
        return 0;
1689
1690
362728
    memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1691
1692
362728
    cblk->data[cblk->length] = 0xff;
1693
362728
    cblk->data[cblk->length+1] = 0xff;
1694
362728
    ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1695
1696
3087743
    while (passno--) {
1697

2725015
        if (bpno < 0 || bpno > 29) {
1698
            av_log(s->avctx, AV_LOG_ERROR, "bpno became invalid\n");
1699
            return AVERROR_INVALIDDATA;
1700
        }
1701

2725015
        switch(pass_t) {
1702
877598
        case 0:
1703
877598
            decode_sigpass(t1, width, height, bpno + 1, bandpos,
1704
                           vert_causal_ctx_csty_symbol);
1705
877598
            break;
1706
774996
        case 1:
1707
774996
            decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1708
774996
            break;
1709
1072421
        case 2:
1710
            av_assert2(!t1->mqc.raw);
1711
1072421
            decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1712
1072421
                           codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1713
                           vert_causal_ctx_csty_symbol);
1714
1072421
            break;
1715
        }
1716
2725015
        if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1717
            ff_mqc_init_contexts(&t1->mqc);
1718
1719

2725015
        if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1720
            if (term_cnt >= cblk->nb_terminations) {
1721
                av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1722
                return AVERROR_INVALIDDATA;
1723
            }
1724
            if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1725
                av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1726
                    cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1727
                    pass_cnt, cblk->npasses);
1728
            }
1729
1730
            ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1731
        }
1732
1733
2725015
        pass_t++;
1734
2725015
        if (pass_t == 3) {
1735
1072421
            bpno--;
1736
1072421
            pass_t = 0;
1737
        }
1738
2725015
        pass_cnt ++;
1739
    }
1740
1741

362728
    if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1742
1003
        av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1743
1003
               cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1744
    }
1745
1746
362728
    return 1;
1747
}
1748
1749
static inline int roi_shift_param(Jpeg2000Component *comp,
1750
                                   int quan_parameter)
1751
{
1752
    uint8_t roi_shift;
1753
    int val;
1754
    roi_shift = comp->roi_shift;
1755
    val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1756
1757
    if (val > (1 << roi_shift))
1758
        return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1759
    return quan_parameter;
1760
}
1761
1762
/* TODO: Verify dequantization for lossless case
1763
 * comp->data can be float or int
1764
 * band->stepsize can be float or int
1765
 * depending on the type of DWT transformation.
1766
 * see ISO/IEC 15444-1:2002 A.6.1 */
1767
1768
/* Float dequantization of a codeblock.*/
1769
2415
static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1770
                                 Jpeg2000Component *comp,
1771
                                 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1772
{
1773
    int i, j;
1774
2415
    int w = cblk->coord[0][1] - cblk->coord[0][0];
1775
75375
    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1776
72960
        float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1777
72960
        int *src = t1->data + j*t1->stride;
1778
9374976
        for (i = 0; i < w; ++i)
1779
9302016
            datap[i] = src[i] * band->f_stepsize;
1780
    }
1781
2415
}
1782
1783
/* Integer dequantization of a codeblock.*/
1784
163728
static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1785
                               Jpeg2000Component *comp,
1786
                               Jpeg2000T1Context *t1, Jpeg2000Band *band)
1787
{
1788
    int i, j;
1789
163728
    int w = cblk->coord[0][1] - cblk->coord[0][0];
1790
2456529
    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1791
2292801
        int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1792
2292801
        int *src = t1->data + j*t1->stride;
1793
2292801
        if (band->i_stepsize == 32768) {
1794
37101173
            for (i = 0; i < w; ++i)
1795
34808372
                datap[i] = src[i] / 2;
1796
        } else {
1797
            // This should be VERY uncommon
1798
            for (i = 0; i < w; ++i)
1799
                datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1800
        }
1801
    }
1802
163728
}
1803
1804
196585
static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1805
                               Jpeg2000Component *comp,
1806
                               Jpeg2000T1Context *t1, Jpeg2000Band *band)
1807
{
1808
    int i, j;
1809
196585
    int w = cblk->coord[0][1] - cblk->coord[0][0];
1810
3127387
    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1811
2930802
        int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1812
2930802
        int *src = t1->data + j*t1->stride;
1813
51769172
        for (i = 0; i < w; ++i)
1814
48838370
            datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1815
    }
1816
196585
}
1817
1818
3
static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1819
{
1820
3
    int i, csize = 1;
1821
    void *src[3];
1822
1823
9
    for (i = 1; i < 3; i++) {
1824
6
        if (tile->codsty[0].transform != tile->codsty[i].transform) {
1825
            av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1826
            return;
1827
        }
1828
6
        if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1829
            av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1830
            return;
1831
        }
1832
    }
1833
1834
12
    for (i = 0; i < 3; i++)
1835
9
        if (tile->codsty[0].transform == FF_DWT97)
1836
            src[i] = tile->comp[i].f_data;
1837
        else
1838
9
            src[i] = tile->comp[i].i_data;
1839
1840
9
    for (i = 0; i < 2; i++)
1841
6
        csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1842
1843
3
    s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1844
}
1845
1846
static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1847
                                  Jpeg2000Component *comp,
1848
                                  Jpeg2000T1Context *t1)
1849
{
1850
    int i, j;
1851
    int w = cblk->coord[0][1] - cblk->coord[0][0];
1852
    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1853
        int *src = t1->data + j*t1->stride;
1854
        for (i = 0; i < w; ++i)
1855
            src[i] = roi_shift_param(comp, src[i]);
1856
    }
1857
}
1858
1859
1331
static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1860
{
1861
    Jpeg2000T1Context t1;
1862
1863
    int compno, reslevelno, bandno;
1864
1865
    /* Loop on tile components */
1866
5326
    for (compno = 0; compno < s->ncomponents; compno++) {
1867
3995
        Jpeg2000Component *comp     = tile->comp + compno;
1868
3995
        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1869
3995
        int coded = 0;
1870
1871
3995
        t1.stride = (1<<codsty->log2_cblk_width) + 2;
1872
1873
        /* Loop on resolution levels */
1874
31943
        for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1875
27948
            Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1876
            /* Loop on bands */
1877
103802
            for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1878
                int nb_precincts, precno;
1879
75854
                Jpeg2000Band *band = rlevel->band + bandno;
1880
75854
                int cblkno = 0, bandpos;
1881
1882
75854
                bandpos = bandno + (reslevelno > 0);
1883
1884
75854
                if (band->coord[0][0] == band->coord[0][1] ||
1885
75854
                    band->coord[1][0] == band->coord[1][1])
1886
3672
                    continue;
1887
1888
72182
                nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1889
                /* Loop on precincts */
1890
145795
                for (precno = 0; precno < nb_precincts; precno++) {
1891
73613
                    Jpeg2000Prec *prec = band->prec + precno;
1892
1893
                    /* Loop on codeblocks */
1894
73613
                    for (cblkno = 0;
1895
516692
                         cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1896
443079
                         cblkno++) {
1897
                        int x, y;
1898
443079
                        Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1899
443079
                        int ret = decode_cblk(s, codsty, &t1, cblk,
1900
443079
                                    cblk->coord[0][1] - cblk->coord[0][0],
1901
443079
                                    cblk->coord[1][1] - cblk->coord[1][0],
1902
443079
                                    bandpos, comp->roi_shift);
1903
443079
                        if (ret)
1904
362728
                            coded = 1;
1905
                        else
1906
80351
                            continue;
1907
362728
                        x = cblk->coord[0][0] - band->coord[0][0];
1908
362728
                        y = cblk->coord[1][0] - band->coord[1][0];
1909
1910
362728
                        if (comp->roi_shift)
1911
                            roi_scale_cblk(cblk, comp, &t1);
1912
362728
                        if (codsty->transform == FF_DWT97)
1913
2415
                            dequantization_float(x, y, cblk, comp, &t1, band);
1914
360313
                        else if (codsty->transform == FF_DWT97_INT)
1915
196585
                            dequantization_int_97(x, y, cblk, comp, &t1, band);
1916
                        else
1917
163728
                            dequantization_int(x, y, cblk, comp, &t1, band);
1918
                   } /* end cblk */
1919
                } /*end prec */
1920
            } /* end band */
1921
        } /* end reslevel */
1922
1923
        /* inverse DWT */
1924
3995
        if (coded)
1925
3995
            ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1926
1927
    } /*end comp */
1928
1331
}
1929
1930
#define WRITE_FRAME(D, PIXEL)                                                                     \
1931
    static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,         \
1932
                                         AVFrame * picture, int precision)                        \
1933
    {                                                                                             \
1934
        const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);               \
1935
        int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);                              \
1936
        int pixelsize = planar ? 1 : pixdesc->nb_components;                                      \
1937
                                                                                                  \
1938
        int compno;                                                                               \
1939
        int x, y;                                                                                 \
1940
                                                                                                  \
1941
        for (compno = 0; compno < s->ncomponents; compno++) {                                     \
1942
            Jpeg2000Component *comp     = tile->comp + compno;                                    \
1943
            Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \
1944
            PIXEL *line;                                                                          \
1945
            float *datap     = comp->f_data;                                                      \
1946
            int32_t *i_datap = comp->i_data;                                                      \
1947
            int cbps         = s->cbps[compno];                                                   \
1948
            int w            = tile->comp[compno].coord[0][1] -                                   \
1949
                               ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]);            \
1950
            int h            = tile->comp[compno].coord[1][1] -                                   \
1951
                               ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]);            \
1952
            int plane        = 0;                                                                 \
1953
                                                                                                  \
1954
            if (planar)                                                                           \
1955
                plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);                 \
1956
                                                                                                  \
1957
            y    = tile->comp[compno].coord[1][0] -                                               \
1958
                   ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]);                        \
1959
            line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1960
            for (; y < h; y++) {                                                                  \
1961
                PIXEL *dst;                                                                       \
1962
                                                                                                  \
1963
                x   = tile->comp[compno].coord[0][0] -                                            \
1964
                      ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]);                     \
1965
                dst = line + x * pixelsize + compno*!planar;                                      \
1966
                                                                                                  \
1967
                if (codsty->transform == FF_DWT97) {                                              \
1968
                    for (; x < w; x++) {                                                          \
1969
                        int val = lrintf(*datap) + (1 << (cbps - 1));                             \
1970
                        /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1971
                        val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1972
                        *dst = val << (precision - cbps);                                         \
1973
                        datap++;                                                                  \
1974
                        dst += pixelsize;                                                         \
1975
                    }                                                                             \
1976
                } else {                                                                          \
1977
                    for (; x < w; x++) {                                                          \
1978
                        int val = *i_datap + (1 << (cbps - 1));                                   \
1979
                        /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1980
                        val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1981
                        *dst = val << (precision - cbps);                                         \
1982
                        i_datap++;                                                                \
1983
                        dst += pixelsize;                                                         \
1984
                    }                                                                             \
1985
                }                                                                                 \
1986
                line += picture->linesize[plane] / sizeof(PIXEL);                                 \
1987
            }                                                                                     \
1988
        }                                                                                         \
1989
                                                                                                  \
1990
    }
1991
1992




93961380
WRITE_FRAME(8, uint8_t)
1993




28113934
WRITE_FRAME(16, uint16_t)
1994
1995
#undef WRITE_FRAME
1996
1997
1331
static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1998
                                int jobnr, int threadnr)
1999
{
2000
1331
    Jpeg2000DecoderContext *s = avctx->priv_data;
2001
1331
    AVFrame *picture = td;
2002
1331
    Jpeg2000Tile *tile = s->tile + jobnr;
2003
    int x;
2004
2005
1331
    tile_codeblocks(s, tile);
2006
2007
    /* inverse MCT transformation */
2008
1331
    if (tile->codsty[0].mct)
2009
3
        mct_decode(s, tile);
2010
2011
4089
    for (x = 0; x < s->ncomponents; x++) {
2012
3170
        if (s->cdef[x] < 0) {
2013
1649
            for (x = 0; x < s->ncomponents; x++) {
2014
1237
                s->cdef[x] = x + 1;
2015
            }
2016
412
            if ((s->ncomponents & 1) == 0)
2017
1
                s->cdef[s->ncomponents-1] = 0;
2018
412
            break;
2019
        }
2020
    }
2021
2022
1331
    if (s->precision <= 8) {
2023
1326
        write_frame_8(s, tile, picture, 8);
2024
    } else {
2025
12
        int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2026
2
                        picture->format == AV_PIX_FMT_RGB48 ||
2027
2
                        picture->format == AV_PIX_FMT_RGBA64 ||
2028

7
                        picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2029
2030
5
        write_frame_16(s, tile, picture, precision);
2031
    }
2032
2033
1331
    return 0;
2034
}
2035
2036
412
static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2037
{
2038
    int tileno, compno;
2039
1743
    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2040
1331
        if (s->tile[tileno].comp) {
2041
5326
            for (compno = 0; compno < s->ncomponents; compno++) {
2042
3995
                Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
2043
3995
                Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2044
2045
3995
                ff_jpeg2000_cleanup(comp, codsty);
2046
            }
2047
1331
            av_freep(&s->tile[tileno].comp);
2048
1331
            av_freep(&s->tile[tileno].packed_headers);
2049
1331
            s->tile[tileno].packed_headers_size = 0;
2050
        }
2051
    }
2052
412
    av_freep(&s->tile);
2053
412
    memset(s->codsty, 0, sizeof(s->codsty));
2054
412
    memset(s->qntsty, 0, sizeof(s->qntsty));
2055
412
    memset(s->properties, 0, sizeof(s->properties));
2056
412
    memset(&s->poc  , 0, sizeof(s->poc));
2057
412
    s->numXtiles = s->numYtiles = 0;
2058
412
    s->ncomponents = 0;
2059
412
}
2060
2061
412
static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2062
{
2063
412
    Jpeg2000CodingStyle *codsty = s->codsty;
2064
412
    Jpeg2000QuantStyle *qntsty  = s->qntsty;
2065
412
    Jpeg2000POC         *poc    = &s->poc;
2066
412
    uint8_t *properties         = s->properties;
2067
2068
3951
    for (;;) {
2069
4363
        int len, ret = 0;
2070
        uint16_t marker;
2071
        int oldpos;
2072
2073
4363
        if (bytestream2_get_bytes_left(&s->g) < 2) {
2074
            av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2075
            break;
2076
        }
2077
2078
4363
        marker = bytestream2_get_be16u(&s->g);
2079
4363
        oldpos = bytestream2_tell(&s->g);
2080
2081
4363
        if (marker == JPEG2000_SOD) {
2082
            Jpeg2000Tile *tile;
2083
            Jpeg2000TilePart *tp;
2084
2085
1343
            if (!s->tile) {
2086
                av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2087
                return AVERROR_INVALIDDATA;
2088
            }
2089
1343
            if (s->curtileno < 0) {
2090
                av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2091
                return AVERROR_INVALIDDATA;
2092
            }
2093
2094
1343
            tile = s->tile + s->curtileno;
2095
1343
            tp = tile->tile_part + tile->tp_idx;
2096
1343
            if (tp->tp_end < s->g.buffer) {
2097
                av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2098
                return AVERROR_INVALIDDATA;
2099
            }
2100
2101

1343
            if (tile->has_ppt && tile->tp_idx == 0) {
2102
                bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2103
            }
2104
2105
1343
            bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2106
1343
            bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2107
2108
1343
            continue;
2109
        }
2110
3020
        if (marker == JPEG2000_EOC)
2111
412
            break;
2112
2113
2608
        len = bytestream2_get_be16(&s->g);
2114

2608
        if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2115
            if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2116
                av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2117
                return AVERROR_INVALIDDATA;
2118
            }
2119
            av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2120
            break;
2121
        }
2122
2123



2608
        switch (marker) {
2124
412
        case JPEG2000_SIZ:
2125
412
            if (s->ncomponents) {
2126
                av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2127
                return AVERROR_INVALIDDATA;
2128
            }
2129
412
            ret = get_siz(s);
2130
412
            if (!s->tile)
2131
                s->numXtiles = s->numYtiles = 0;
2132
412
            break;
2133
6
        case JPEG2000_COC:
2134
6
            ret = get_coc(s, codsty, properties);
2135
6
            break;
2136
412
        case JPEG2000_COD:
2137
412
            ret = get_cod(s, codsty, properties);
2138
412
            break;
2139
        case JPEG2000_RGN:
2140
            ret = get_rgn(s, len);
2141
            break;
2142
9
        case JPEG2000_QCC:
2143
9
            ret = get_qcc(s, len, qntsty, properties);
2144
9
            break;
2145
412
        case JPEG2000_QCD:
2146
412
            ret = get_qcd(s, len, qntsty, properties);
2147
412
            break;
2148
        case JPEG2000_POC:
2149
            ret = get_poc(s, len, poc);
2150
            break;
2151
1343
        case JPEG2000_SOT:
2152
1343
            if (!(ret = get_sot(s, len))) {
2153
                av_assert1(s->curtileno >= 0);
2154
1343
                codsty = s->tile[s->curtileno].codsty;
2155
1343
                qntsty = s->tile[s->curtileno].qntsty;
2156
1343
                poc    = &s->tile[s->curtileno].poc;
2157
1343
                properties = s->tile[s->curtileno].properties;
2158
            }
2159
1343
            break;
2160
10
        case JPEG2000_PLM:
2161
            // the PLM marker is ignored
2162
        case JPEG2000_COM:
2163
            // the comment is ignored
2164
10
            bytestream2_skip(&s->g, len - 2);
2165
10
            break;
2166
        case JPEG2000_CRG:
2167
            ret = read_crg(s, len);
2168
            break;
2169
4
        case JPEG2000_TLM:
2170
            // Tile-part lengths
2171
4
            ret = get_tlm(s, len);
2172
4
            break;
2173
        case JPEG2000_PLT:
2174
            // Packet length, tile-part header
2175
            ret = get_plt(s, len);
2176
            break;
2177
        case JPEG2000_PPT:
2178
            // Packed headers, tile-part header
2179
            ret = get_ppt(s, len);
2180
            break;
2181
        default:
2182
            av_log(s->avctx, AV_LOG_ERROR,
2183
                   "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2184
                   marker, bytestream2_tell(&s->g) - 4);
2185
            bytestream2_skip(&s->g, len - 2);
2186
            break;
2187
        }
2188

2608
        if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2189
            av_log(s->avctx, AV_LOG_ERROR,
2190
                   "error during processing marker segment %.4"PRIx16"\n",
2191
                   marker);
2192
            return ret ? ret : -1;
2193
        }
2194
    }
2195
412
    return 0;
2196
}
2197
2198
/* Read bit stream packets --> T2 operation. */
2199
412
static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2200
{
2201
412
    int ret = 0;
2202
    int tileno;
2203
2204
1743
    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2205
1331
        Jpeg2000Tile *tile = s->tile + tileno;
2206
2207
1331
        if ((ret = init_tile(s, tileno)) < 0)
2208
            return ret;
2209
2210
1331
        if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2211
            return ret;
2212
    }
2213
2214
412
    return 0;
2215
}
2216
2217
409
static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2218
{
2219
    uint32_t atom_size, atom, atom_end;
2220
409
    int search_range = 10;
2221
2222
1228
    while (search_range
2223
1228
           &&
2224
1228
           bytestream2_get_bytes_left(&s->g) >= 8) {
2225
1228
        atom_size = bytestream2_get_be32u(&s->g);
2226
1228
        atom      = bytestream2_get_be32u(&s->g);
2227
1228
        if (atom_size == 1) {
2228
            if (bytestream2_get_be32u(&s->g)) {
2229
                avpriv_request_sample(s->avctx, "Huge atom");
2230
                return 0;
2231
            }
2232
            atom_size = bytestream2_get_be32u(&s->g);
2233
            atom_end  = bytestream2_tell(&s->g) + atom_size - 16;
2234
        } else {
2235
1228
            atom_end  = bytestream2_tell(&s->g) + atom_size -  8;
2236
        }
2237
2238
1228
        if (atom == JP2_CODESTREAM)
2239
409
            return 1;
2240
2241

819
        if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2242
            return 0;
2243
2244

819
        if (atom == JP2_HEADER &&
2245
409
                   atom_size >= 16) {
2246
            uint32_t atom2_size, atom2, atom2_end;
2247
            do {
2248
818
                atom2_size = bytestream2_get_be32u(&s->g);
2249
818
                atom2      = bytestream2_get_be32u(&s->g);
2250
818
                atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
2251

818
                if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2252
                    break;
2253
818
                atom2_size -= 8;
2254
818
                if (atom2 == JP2_CODESTREAM) {
2255
                    return 1;
2256

1227
                } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2257
409
                    int method = bytestream2_get_byteu(&s->g);
2258
409
                    bytestream2_skipu(&s->g, 2);
2259
409
                    if (method == 1) {
2260
409
                        s->colour_space = bytestream2_get_be32u(&s->g);
2261
                    }
2262

409
                } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2263
                    int i, size, colour_count, colour_channels, colour_depth[3];
2264
                    colour_count = bytestream2_get_be16u(&s->g);
2265
                    colour_channels = bytestream2_get_byteu(&s->g);
2266
                    // FIXME: Do not ignore channel_sign
2267
                    colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2268
                    colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2269
                    colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2270
                    size = (colour_depth[0] + 7 >> 3) * colour_count +
2271
                           (colour_depth[1] + 7 >> 3) * colour_count +
2272
                           (colour_depth[2] + 7 >> 3) * colour_count;
2273
                    if (colour_count > AVPALETTE_COUNT ||
2274
                        colour_channels != 3 ||
2275
                        colour_depth[0] > 16 ||
2276
                        colour_depth[1] > 16 ||
2277
                        colour_depth[2] > 16 ||
2278
                        atom2_size < size) {
2279
                        avpriv_request_sample(s->avctx, "Unknown palette");
2280
                        bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2281
                        continue;
2282
                    }
2283
                    s->pal8 = 1;
2284
                    for (i = 0; i < colour_count; i++) {
2285
                        uint32_t r, g, b;
2286
                        if (colour_depth[0] <= 8) {
2287
                            r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2288
                            r |= r >> colour_depth[0];
2289
                        } else {
2290
                            r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2291
                        }
2292
                        if (colour_depth[1] <= 8) {
2293
                            g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2294
                            g |= g >> colour_depth[1];
2295
                        } else {
2296
                            g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2297
                        }
2298
                        if (colour_depth[2] <= 8) {
2299
                            b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2300
                            b |= b >> colour_depth[2];
2301
                        } else {
2302
                            b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2303
                        }
2304
                        s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2305
                    }
2306

409
                } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2307
                    int n = bytestream2_get_be16u(&s->g);
2308
                    for (; n>0; n--) {
2309
                        int cn   = bytestream2_get_be16(&s->g);
2310
                        int av_unused typ  = bytestream2_get_be16(&s->g);
2311
                        int asoc = bytestream2_get_be16(&s->g);
2312
                        if (cn < 4 && asoc < 4)
2313
                            s->cdef[cn] = asoc;
2314
                    }
2315

409
                } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2316
                    int64_t vnum, vden, hnum, hden, vexp, hexp;
2317
                    uint32_t resx;
2318
                    bytestream2_skip(&s->g, 4);
2319
                    resx = bytestream2_get_be32u(&s->g);
2320
                    if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2321
                        bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2322
                        continue;
2323
                    }
2324
                    vnum = bytestream2_get_be16u(&s->g);
2325
                    vden = bytestream2_get_be16u(&s->g);
2326
                    hnum = bytestream2_get_be16u(&s->g);
2327
                    hden = bytestream2_get_be16u(&s->g);
2328
                    vexp = bytestream2_get_byteu(&s->g);
2329
                    hexp = bytestream2_get_byteu(&s->g);
2330
                    if (!vnum || !vden || !hnum || !hden) {
2331
                        bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2332
                        av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2333
                        continue;
2334
                    }
2335
                    if (vexp > hexp) {
2336
                        vexp -= hexp;
2337
                        hexp = 0;
2338
                    } else {
2339
                        hexp -= vexp;
2340
                        vexp = 0;
2341
                    }
2342
                    if (   INT64_MAX / (hnum * vden) > pow(10, hexp)
2343
                        && INT64_MAX / (vnum * hden) > pow(10, vexp))
2344
                        av_reduce(&s->sar.den, &s->sar.num,
2345
                                  hnum * vden * pow(10, hexp),
2346
                                  vnum * hden * pow(10, vexp),
2347
                                  INT32_MAX);
2348
                }
2349
818
                bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2350
818
            } while (atom_end - atom2_end >= 8);
2351
        } else {
2352
410
            search_range--;
2353
        }
2354
819
        bytestream2_seek(&s->g, atom_end, SEEK_SET);
2355
    }
2356
2357
    return 0;
2358
}
2359
2360
10
static av_cold void jpeg2000_init_static_data(void)
2361
{
2362
10
    ff_jpeg2000_init_tier1_luts();
2363
10
    ff_mqc_init_context_tables();
2364
10
}
2365
2366
19
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2367
{
2368
    static AVOnce init_static_once = AV_ONCE_INIT;
2369
19
    Jpeg2000DecoderContext *s = avctx->priv_data;
2370
2371
19
    ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2372
19
    ff_jpeg2000dsp_init(&s->dsp);
2373
2374
19
    return 0;
2375
}
2376
2377
412
static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2378
                                 int *got_frame, AVPacket *avpkt)
2379
{
2380
412
    Jpeg2000DecoderContext *s = avctx->priv_data;
2381
412
    ThreadFrame frame = { .f = data };
2382
412
    AVFrame *picture = data;
2383
    int ret;
2384
2385
412
    s->avctx     = avctx;
2386
412
    bytestream2_init(&s->g, avpkt->data, avpkt->size);
2387
412
    s->curtileno = -1;
2388
412
    memset(s->cdef, -1, sizeof(s->cdef));
2389
2390
412
    if (bytestream2_get_bytes_left(&s->g) < 2) {
2391
        ret = AVERROR_INVALIDDATA;
2392
        goto end;
2393
    }
2394
2395
    // check if the image is in jp2 format
2396

824
    if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2397
821
       (bytestream2_get_be32u(&s->g) == 12) &&
2398
818
       (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2399
409
       (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2400
409
        if (!jp2_find_codestream(s)) {
2401
            av_log(avctx, AV_LOG_ERROR,
2402
                   "Could not find Jpeg2000 codestream atom.\n");
2403
            ret = AVERROR_INVALIDDATA;
2404
            goto end;
2405
        }
2406
    } else {
2407
3
        bytestream2_seek(&s->g, 0, SEEK_SET);
2408
    }
2409
2410

412
    while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2411
        bytestream2_skip(&s->g, 1);
2412
2413
412
    if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2414
        av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2415
        ret = AVERROR_INVALIDDATA;
2416
        goto end;
2417
    }
2418
412
    if (ret = jpeg2000_read_main_headers(s))
2419
        goto end;
2420
2421
    /* get picture buffer */
2422
412
    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2423
        goto end;
2424
412
    picture->pict_type = AV_PICTURE_TYPE_I;
2425
412
    picture->key_frame = 1;
2426
2427
412
    if (ret = jpeg2000_read_bitstream_packets(s))
2428
        goto end;
2429
2430
412
    avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2431
2432
412
    jpeg2000_dec_cleanup(s);
2433
2434
412
    *got_frame = 1;
2435
2436
412
    if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2437
        memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2438

412
    if (s->sar.num && s->sar.den)
2439
        avctx->sample_aspect_ratio = s->sar;
2440
412
    s->sar.num = s->sar.den = 0;
2441
2442
412
    return bytestream2_tell(&s->g);
2443
2444
end:
2445
    jpeg2000_dec_cleanup(s);
2446
    return ret;
2447
}
2448
2449
#define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2450
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2451
2452
static const AVOption options[] = {
2453
    { "lowres",  "Lower the decoding resolution by a power of two",
2454
        OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2455
    { NULL },
2456
};
2457
2458
static const AVClass jpeg2000_class = {
2459
    .class_name = "jpeg2000",
2460
    .item_name  = av_default_item_name,
2461
    .option     = options,
2462
    .version    = LIBAVUTIL_VERSION_INT,
2463
};
2464
2465
AVCodec ff_jpeg2000_decoder = {
2466
    .name             = "jpeg2000",
2467
    .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2468
    .type             = AVMEDIA_TYPE_VIDEO,
2469
    .id               = AV_CODEC_ID_JPEG2000,
2470
    .capabilities     = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2471
    .priv_data_size   = sizeof(Jpeg2000DecoderContext),
2472
    .init             = jpeg2000_decode_init,
2473
    .decode           = jpeg2000_decode_frame,
2474
    .priv_class       = &jpeg2000_class,
2475
    .max_lowres       = 5,
2476
    .profiles         = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)
2477
};