GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/jpeg2000dec.c Lines: 798 1276 62.5 %
Date: 2020-04-02 05:41:20 Branches: 506 967 52.3 %

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
    uint16_t tp_idx;                    // Tile-part index
87
    int coord[2][2];                    // border coordinates {{x0, x1}, {y0, y1}}
88
} Jpeg2000Tile;
89
90
typedef struct Jpeg2000DecoderContext {
91
    AVClass         *class;
92
    AVCodecContext  *avctx;
93
    GetByteContext  g;
94
95
    int             width, height;
96
    int             image_offset_x, image_offset_y;
97
    int             tile_offset_x, tile_offset_y;
98
    uint8_t         cbps[4];    // bits per sample in particular components
99
    uint8_t         sgnd[4];    // if a component is signed
100
    uint8_t         properties[4];
101
    int             cdx[4], cdy[4];
102
    int             precision;
103
    int             ncomponents;
104
    int             colour_space;
105
    uint32_t        palette[256];
106
    int8_t          pal8;
107
    int             cdef[4];
108
    int             tile_width, tile_height;
109
    unsigned        numXtiles, numYtiles;
110
    int             maxtilelen;
111
    AVRational      sar;
112
113
    Jpeg2000CodingStyle codsty[4];
114
    Jpeg2000QuantStyle  qntsty[4];
115
    Jpeg2000POC         poc;
116
117
    int             bit_index;
118
119
    int             curtileno;
120
121
    Jpeg2000Tile    *tile;
122
    Jpeg2000DSPContext dsp;
123
124
    /*options parameters*/
125
    int             reduction_factor;
126
} Jpeg2000DecoderContext;
127
128
/* get_bits functions for JPEG2000 packet bitstream
129
 * It is a get_bit function with a bit-stuffing routine. If the value of the
130
 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
131
 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
132
4305168
static int get_bits(Jpeg2000DecoderContext *s, int n)
133
{
134
4305168
    int res = 0;
135
136
11521813
    while (--n >= 0) {
137
7216645
        res <<= 1;
138
7216645
        if (s->bit_index == 0) {
139
885441
            s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
140
        }
141
7216645
        s->bit_index--;
142
7216645
        res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
143
    }
144
4305168
    return res;
145
}
146
147
28473
static void jpeg2000_flush(Jpeg2000DecoderContext *s)
148
{
149
28473
    if (bytestream2_get_byte(&s->g) == 0xff)
150
        bytestream2_skip(&s->g, 1);
151
28473
    s->bit_index = 8;
152
28473
}
153
154
/* decode the value stored in node */
155
806199
static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
156
                           int threshold)
157
{
158
    Jpeg2000TgtNode *stack[30];
159
806199
    int sp = -1, curval = 0;
160
161
806199
    if (!node) {
162
        av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
163
        return AVERROR_INVALIDDATA;
164
    }
165
166

1944689
    while (node && !node->vis) {
167
1138490
        stack[++sp] = node;
168
1138490
        node        = node->parent;
169
    }
170
171
806199
    if (node)
172
652216
        curval = node->val;
173
    else
174
153983
        curval = stack[sp]->val;
175
176

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

413
    switch (components) {
208
2
    case 4:
209
4
        match = match && desc->comp[3].depth >= bpc &&
210

5
                         (log2_chroma_wh >> 14 & 3) == 0 &&
211
1
                         (log2_chroma_wh >> 12 & 3) == 0;
212
413
    case 3:
213
825
        match = match && desc->comp[2].depth >= bpc &&
214

1237
                         (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
215
412
                         (log2_chroma_wh >>  8 & 3) == desc->log2_chroma_h;
216
413
    case 2:
217
825
        match = match && desc->comp[1].depth >= bpc &&
218

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

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

412
    if (s->image_offset_x || s->image_offset_y) {
285
        avpriv_request_sample(s->avctx, "Support for image offsets");
286
        return AVERROR_PATCHWELCOME;
287
    }
288
412
    if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
289
        avpriv_request_sample(s->avctx, "Large Dimensions");
290
        return AVERROR_PATCHWELCOME;
291
    }
292
293
412
    if (ncomponents <= 0) {
294
        av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
295
               s->ncomponents);
296
        return AVERROR_INVALIDDATA;
297
    }
298
299
412
    if (ncomponents > 4) {
300
        avpriv_request_sample(s->avctx, "Support for %d components",
301
                              ncomponents);
302
        return AVERROR_PATCHWELCOME;
303
    }
304
305

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

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

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

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

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

418
    if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
483
418
        c->log2_cblk_width + c->log2_cblk_height > 12) {
484
        av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
485
        return AVERROR_INVALIDDATA;
486
    }
487
488
418
    c->cblk_style = bytestream2_get_byteu(&s->g);
489
418
    if (c->cblk_style != 0) { // cblk style
490
        av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
491
        if (c->cblk_style & JPEG2000_CBLK_BYPASS)
492
            av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
493
    }
494
418
    c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
495
    /* set integer 9/7 DWT in case of BITEXACT flag */
496

418
    if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
497
213
        c->transform = FF_DWT97_INT;
498
205
    else if (c->transform == FF_DWT53) {
499
204
        s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
500
    }
501
502
418
    if (c->csty & JPEG2000_CSTY_PREC) {
503
        int i;
504
63
        for (i = 0; i < c->nreslevels; i++) {
505
54
            byte = bytestream2_get_byte(&s->g);
506
54
            c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
507
54
            c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
508
54
            if (i)
509

45
                if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
510
                    av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
511
                           c->log2_prec_widths[i], c->log2_prec_heights[i]);
512
                    c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
513
                    return AVERROR_INVALIDDATA;
514
                }
515
        }
516
    } else {
517
409
        memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
518
409
        memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
519
    }
520
418
    return 0;
521
}
522
523
/* get coding parameters for a particular tile or whole image*/
524
412
static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
525
                   uint8_t *properties)
526
{
527
    Jpeg2000CodingStyle tmp;
528
    int compno, ret;
529
530
412
    if (bytestream2_get_bytes_left(&s->g) < 5) {
531
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
532
        return AVERROR_INVALIDDATA;
533
    }
534
535
412
    tmp.csty = bytestream2_get_byteu(&s->g);
536
537
    // get progression order
538
412
    tmp.prog_order = bytestream2_get_byteu(&s->g);
539
540
412
    tmp.nlayers    = bytestream2_get_be16u(&s->g);
541
412
    tmp.mct        = bytestream2_get_byteu(&s->g); // multiple component transformation
542
543

412
    if (tmp.mct && s->ncomponents < 3) {
544
        av_log(s->avctx, AV_LOG_ERROR,
545
               "MCT %"PRIu8" with too few components (%d)\n",
546
               tmp.mct, s->ncomponents);
547
        return AVERROR_INVALIDDATA;
548
    }
549
550
412
    if ((ret = get_cox(s, &tmp)) < 0)
551
        return ret;
552
553
1649
    for (compno = 0; compno < s->ncomponents; compno++)
554
1237
        if (!(properties[compno] & HAD_COC))
555
1237
            memcpy(c + compno, &tmp, sizeof(tmp));
556
412
    return 0;
557
}
558
559
/* Get coding parameters for a component in the whole image or a
560
 * particular tile. */
561
6
static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
562
                   uint8_t *properties)
563
{
564
    int compno, ret;
565
566
6
    if (bytestream2_get_bytes_left(&s->g) < 2) {
567
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
568
        return AVERROR_INVALIDDATA;
569
    }
570
571
6
    compno = bytestream2_get_byteu(&s->g);
572
573
6
    if (compno >= s->ncomponents) {
574
        av_log(s->avctx, AV_LOG_ERROR,
575
               "Invalid compno %d. There are %d components in the image.\n",
576
               compno, s->ncomponents);
577
        return AVERROR_INVALIDDATA;
578
    }
579
580
6
    c      += compno;
581
6
    c->csty = bytestream2_get_byteu(&s->g);
582
583
6
    if ((ret = get_cox(s, c)) < 0)
584
        return ret;
585
586
6
    properties[compno] |= HAD_COC;
587
6
    return 0;
588
}
589
590
/* Get common part for QCD and QCC segments. */
591
421
static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
592
{
593
    int i, x;
594
595
421
    if (bytestream2_get_bytes_left(&s->g) < 1)
596
        return AVERROR_INVALIDDATA;
597
598
421
    x = bytestream2_get_byteu(&s->g); // Sqcd
599
600
421
    q->nguardbits = x >> 5;
601
421
    q->quantsty   = x & 0x1f;
602
603
421
    if (q->quantsty == JPEG2000_QSTY_NONE) {
604
204
        n -= 3;
605

204
        if (bytestream2_get_bytes_left(&s->g) < n ||
606
            n > JPEG2000_MAX_DECLEVELS*3)
607
            return AVERROR_INVALIDDATA;
608
4080
        for (i = 0; i < n; i++)
609
3876
            q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
610
217
    } else if (q->quantsty == JPEG2000_QSTY_SI) {
611
        if (bytestream2_get_bytes_left(&s->g) < 2)
612
            return AVERROR_INVALIDDATA;
613
        x          = bytestream2_get_be16u(&s->g);
614
        q->expn[0] = x >> 11;
615
        q->mant[0] = x & 0x7ff;
616
        for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
617
            int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
618
            q->expn[i] = curexpn;
619
            q->mant[i] = q->mant[0];
620
        }
621
    } else {
622
217
        n = (n - 3) >> 1;
623

217
        if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
624
            n > JPEG2000_MAX_DECLEVELS*3)
625
            return AVERROR_INVALIDDATA;
626
4301
        for (i = 0; i < n; i++) {
627
4084
            x          = bytestream2_get_be16u(&s->g);
628
4084
            q->expn[i] = x >> 11;
629
4084
            q->mant[i] = x & 0x7ff;
630
        }
631
    }
632
421
    return 0;
633
}
634
635
/* Get quantization parameters for a particular tile or a whole image. */
636
412
static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
637
                   uint8_t *properties)
638
{
639
    Jpeg2000QuantStyle tmp;
640
    int compno, ret;
641
642
412
    memset(&tmp, 0, sizeof(tmp));
643
644
412
    if ((ret = get_qcx(s, n, &tmp)) < 0)
645
        return ret;
646
1649
    for (compno = 0; compno < s->ncomponents; compno++)
647
1237
        if (!(properties[compno] & HAD_QCC))
648
1237
            memcpy(q + compno, &tmp, sizeof(tmp));
649
412
    return 0;
650
}
651
652
/* Get quantization parameters for a component in the whole image
653
 * on in a particular tile. */
654
9
static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
655
                   uint8_t *properties)
656
{
657
    int compno;
658
659
9
    if (bytestream2_get_bytes_left(&s->g) < 1)
660
        return AVERROR_INVALIDDATA;
661
662
9
    compno = bytestream2_get_byteu(&s->g);
663
664
9
    if (compno >= s->ncomponents) {
665
        av_log(s->avctx, AV_LOG_ERROR,
666
               "Invalid compno %d. There are %d components in the image.\n",
667
               compno, s->ncomponents);
668
        return AVERROR_INVALIDDATA;
669
    }
670
671
9
    properties[compno] |= HAD_QCC;
672
9
    return get_qcx(s, n - 1, q + compno);
673
}
674
675
static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
676
{
677
    int i;
678
    int elem_size = s->ncomponents <= 257 ? 7 : 9;
679
    Jpeg2000POC tmp = {{{0}}};
680
681
    if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
682
        av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
683
        return AVERROR_INVALIDDATA;
684
    }
685
686
    if (elem_size > 7) {
687
        avpriv_request_sample(s->avctx, "Fat POC not supported");
688
        return AVERROR_PATCHWELCOME;
689
    }
690
691
    tmp.nb_poc = (size - 2) / elem_size;
692
    if (tmp.nb_poc > MAX_POCS) {
693
        avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
694
        return AVERROR_PATCHWELCOME;
695
    }
696
697
    for (i = 0; i<tmp.nb_poc; i++) {
698
        Jpeg2000POCEntry *e = &tmp.poc[i];
699
        e->RSpoc  = bytestream2_get_byteu(&s->g);
700
        e->CSpoc  = bytestream2_get_byteu(&s->g);
701
        e->LYEpoc = bytestream2_get_be16u(&s->g);
702
        e->REpoc  = bytestream2_get_byteu(&s->g);
703
        e->CEpoc  = bytestream2_get_byteu(&s->g);
704
        e->Ppoc   = bytestream2_get_byteu(&s->g);
705
        if (!e->CEpoc)
706
            e->CEpoc = 256;
707
        if (e->CEpoc > s->ncomponents)
708
            e->CEpoc = s->ncomponents;
709
        if (   e->RSpoc >= e->REpoc || e->REpoc > 33
710
            || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
711
            || !e->LYEpoc) {
712
            av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
713
                e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
714
            );
715
            return AVERROR_INVALIDDATA;
716
        }
717
    }
718
719
    if (!p->nb_poc || p->is_default) {
720
        *p = tmp;
721
    } else {
722
        if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
723
            av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
724
            return AVERROR_INVALIDDATA;
725
        }
726
        memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
727
        p->nb_poc += tmp.nb_poc;
728
    }
729
730
    p->is_default = 0;
731
732
    return 0;
733
}
734
735
736
/* Get start of tile segment. */
737
1343
static int get_sot(Jpeg2000DecoderContext *s, int n)
738
{
739
    Jpeg2000TilePart *tp;
740
    uint16_t Isot;
741
    uint32_t Psot;
742
    unsigned TPsot;
743
744
1343
    if (bytestream2_get_bytes_left(&s->g) < 8)
745
        return AVERROR_INVALIDDATA;
746
747
1343
    s->curtileno = 0;
748
1343
    Isot = bytestream2_get_be16u(&s->g);        // Isot
749
1343
    if (Isot >= s->numXtiles * s->numYtiles)
750
        return AVERROR_INVALIDDATA;
751
752
1343
    s->curtileno = Isot;
753
1343
    Psot  = bytestream2_get_be32u(&s->g);       // Psot
754
1343
    TPsot = bytestream2_get_byteu(&s->g);       // TPsot
755
756
    /* Read TNSot but not used */
757
1343
    bytestream2_get_byteu(&s->g);               // TNsot
758
759
1343
    if (!Psot)
760
        Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
761
762
1343
    if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
763
        av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
764
        return AVERROR_INVALIDDATA;
765
    }
766
767
1343
    if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
768
        avpriv_request_sample(s->avctx, "Too many tile parts");
769
        return AVERROR_PATCHWELCOME;
770
    }
771
772
1343
    s->tile[Isot].tp_idx = TPsot;
773
1343
    tp             = s->tile[Isot].tile_part + TPsot;
774
1343
    tp->tile_index = Isot;
775
1343
    tp->tp_end     = s->g.buffer + Psot - n - 2;
776
777
1343
    if (!TPsot) {
778
1331
        Jpeg2000Tile *tile = s->tile + s->curtileno;
779
780
        /* copy defaults */
781
1331
        memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
782
1331
        memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
783
1331
        memcpy(&tile->poc  , &s->poc  , sizeof(tile->poc));
784
1331
        tile->poc.is_default = 1;
785
    }
786
787
1343
    return 0;
788
}
789
790
/* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
791
 * Used to know the number of tile parts and lengths.
792
 * There may be multiple TLMs in the header.
793
 * TODO: The function is not used for tile-parts management, nor anywhere else.
794
 * It can be useful to allocate memory for tile parts, before managing the SOT
795
 * markers. Parsing the TLM header is needed to increment the input header
796
 * buffer.
797
 * This marker is mandatory for DCI. */
798
4
static int get_tlm(Jpeg2000DecoderContext *s, int n)
799
{
800
    uint8_t Stlm, ST, SP, tile_tlm, i;
801
4
    bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
802
4
    Stlm = bytestream2_get_byte(&s->g);
803
804
    // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
805
4
    ST = (Stlm >> 4) & 0x03;
806
4
    if (ST == 0x03) {
807
        av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
808
        return AVERROR_INVALIDDATA;
809
    }
810
811
4
    SP       = (Stlm >> 6) & 0x01;
812
4
    tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
813
21
    for (i = 0; i < tile_tlm; i++) {
814

17
        switch (ST) {
815
        case 0:
816
            break;
817
17
        case 1:
818
17
            bytestream2_get_byte(&s->g);
819
17
            break;
820
        case 2:
821
            bytestream2_get_be16(&s->g);
822
            break;
823
        case 3:
824
            bytestream2_get_be32(&s->g);
825
            break;
826
        }
827
17
        if (SP == 0) {
828
            bytestream2_get_be16(&s->g);
829
        } else {
830
17
            bytestream2_get_be32(&s->g);
831
        }
832
    }
833
4
    return 0;
834
}
835
836
static int get_plt(Jpeg2000DecoderContext *s, int n)
837
{
838
    int i;
839
    int v;
840
841
    av_log(s->avctx, AV_LOG_DEBUG,
842
            "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
843
844
    if (n < 4)
845
        return AVERROR_INVALIDDATA;
846
847
    /*Zplt =*/ bytestream2_get_byte(&s->g);
848
849
    for (i = 0; i < n - 3; i++) {
850
        v = bytestream2_get_byte(&s->g);
851
    }
852
    if (v & 0x80)
853
        return AVERROR_INVALIDDATA;
854
855
    return 0;
856
}
857
858
1331
static int init_tile(Jpeg2000DecoderContext *s, int tileno)
859
{
860
    int compno;
861
1331
    int tilex = tileno % s->numXtiles;
862
1331
    int tiley = tileno / s->numXtiles;
863
1331
    Jpeg2000Tile *tile = s->tile + tileno;
864
865
1331
    if (!tile->comp)
866
        return AVERROR(ENOMEM);
867
868
1331
    tile->coord[0][0] = av_clip(tilex       * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
869
1331
    tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
870
1331
    tile->coord[1][0] = av_clip(tiley       * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
871
1331
    tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
872
873
5326
    for (compno = 0; compno < s->ncomponents; compno++) {
874
3995
        Jpeg2000Component *comp = tile->comp + compno;
875
3995
        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
876
3995
        Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
877
        int ret; // global bandno
878
879
3995
        comp->coord_o[0][0] = tile->coord[0][0];
880
3995
        comp->coord_o[0][1] = tile->coord[0][1];
881
3995
        comp->coord_o[1][0] = tile->coord[1][0];
882
3995
        comp->coord_o[1][1] = tile->coord[1][1];
883
3995
        if (compno) {
884
2664
            comp->coord_o[0][0] /= s->cdx[compno];
885
2664
            comp->coord_o[0][1] /= s->cdx[compno];
886
2664
            comp->coord_o[1][0] /= s->cdy[compno];
887
2664
            comp->coord_o[1][1] /= s->cdy[compno];
888
        }
889
890
3995
        comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
891
3995
        comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
892
3995
        comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
893
3995
        comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
894
895
3995
        if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
896
3995
                                             s->cbps[compno], s->cdx[compno],
897
                                             s->cdy[compno], s->avctx))
898
            return ret;
899
    }
900
1331
    return 0;
901
}
902
903
/* Read the number of coding passes. */
904
364564
static int getnpasses(Jpeg2000DecoderContext *s)
905
{
906
    int num;
907
364564
    if (!get_bits(s, 1))
908
21434
        return 1;
909
343130
    if (!get_bits(s, 1))
910
12976
        return 2;
911
330154
    if ((num = get_bits(s, 2)) != 3)
912
110502
        return num < 0 ? num : 3 + num;
913
219652
    if ((num = get_bits(s, 5)) != 31)
914
219649
        return num < 0 ? num : 6 + num;
915
3
    num = get_bits(s, 7);
916
3
    return num < 0 ? num : 37 + num;
917
}
918
919
364564
static int getlblockinc(Jpeg2000DecoderContext *s)
920
{
921
364564
    int res = 0, ret;
922
518597
    while (ret = get_bits(s, 1)) {
923
154033
        if (ret < 0)
924
            return ret;
925
154033
        res++;
926
    }
927
364564
    return res;
928
}
929
930
28473
static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
931
                                  Jpeg2000CodingStyle *codsty,
932
                                  Jpeg2000ResLevel *rlevel, int precno,
933
                                  int layno, uint8_t *expn, int numgbits)
934
{
935
    int bandno, cblkno, ret, nb_code_blocks;
936
    int cwsno;
937
938
28473
    if (layno < rlevel->band[0].prec[precno].decoded_layers)
939
        return 0;
940
28473
    rlevel->band[0].prec[precno].decoded_layers = layno + 1;
941
942

28473
    if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
943
12
        if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
944
12
            s->g = tile->tile_part[++(*tp_index)].tpg;
945
        }
946
    }
947
948
28473
    if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
949
        bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
950
951
28473
    if (!(ret = get_bits(s, 1))) {
952
        jpeg2000_flush(s);
953
        return 0;
954
28473
    } else if (ret < 0)
955
        return ret;
956
957
105886
    for (bandno = 0; bandno < rlevel->nbands; bandno++) {
958
77413
        Jpeg2000Band *band = rlevel->band + bandno;
959
77413
        Jpeg2000Prec *prec = band->prec + precno;
960
961
77413
        if (band->coord[0][0] == band->coord[0][1] ||
962
77413
            band->coord[1][0] == band->coord[1][1])
963
3672
            continue;
964
73741
        nb_code_blocks =  prec->nb_codeblocks_height *
965
73741
                          prec->nb_codeblocks_width;
966
519268
        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
967
445527
            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
968
            int incl, newpasses, llen;
969
            void *tmp;
970
971
445527
            if (cblk->npasses)
972
2056
                incl = get_bits(s, 1);
973
            else
974
443471
                incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
975
445527
            if (!incl)
976
80963
                continue;
977
364564
            else if (incl < 0)
978
                return incl;
979
980
364564
            if (!cblk->npasses) {
981
725456
                int v = expn[bandno] + numgbits - 1 -
982
362728
                        tag_tree_decode(s, prec->zerobits + cblkno, 100);
983

362728
                if (v < 0 || v > 30) {
984
                    av_log(s->avctx, AV_LOG_ERROR,
985
                           "nonzerobits %d invalid or unsupported\n", v);
986
                    return AVERROR_INVALIDDATA;
987
                }
988
362728
                cblk->nonzerobits = v;
989
            }
990
364564
            if ((newpasses = getnpasses(s)) < 0)
991
                return newpasses;
992
            av_assert2(newpasses > 0);
993
364564
            if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
994
                avpriv_request_sample(s->avctx, "Too many passes");
995
                return AVERROR_PATCHWELCOME;
996
            }
997
364564
            if ((llen = getlblockinc(s)) < 0)
998
                return llen;
999
364564
            if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1000
                avpriv_request_sample(s->avctx,
1001
                                      "Block with length beyond 16 bits");
1002
                return AVERROR_PATCHWELCOME;
1003
            }
1004
1005
364564
            cblk->lblock += llen;
1006
1007
364564
            cblk->nb_lengthinc = 0;
1008
364564
            cblk->nb_terminationsinc = 0;
1009
364564
            av_free(cblk->lengthinc);
1010
364564
            cblk->lengthinc  = av_mallocz_array(newpasses    , sizeof(*cblk->lengthinc));
1011
364564
            if (!cblk->lengthinc)
1012
                return AVERROR(ENOMEM);
1013
364564
            tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1014
364564
            if (!tmp)
1015
                return AVERROR(ENOMEM);
1016
364564
            cblk->data_start = tmp;
1017
            do {
1018
364564
                int newpasses1 = 0;
1019
1020
3089579
                while (newpasses1 < newpasses) {
1021
2725015
                    newpasses1 ++;
1022
2725015
                    if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1023
                        cblk->nb_terminationsinc ++;
1024
                        break;
1025
                    }
1026
                }
1027
1028
364564
                if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1029
                    return ret;
1030
364564
                if (ret > cblk->data_allocated) {
1031
363060
                    size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1032
363060
                    void *new = av_realloc(cblk->data, new_size);
1033
363060
                    if (new) {
1034
363060
                        cblk->data = new;
1035
363060
                        cblk->data_allocated = new_size;
1036
                    }
1037
                }
1038
364564
                if (ret > cblk->data_allocated) {
1039
                    avpriv_request_sample(s->avctx,
1040
                                        "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1041
                                        cblk->data_allocated);
1042
                    return AVERROR_PATCHWELCOME;
1043
                }
1044
364564
                cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1045
364564
                cblk->npasses  += newpasses1;
1046
364564
                newpasses -= newpasses1;
1047
364564
            } while(newpasses);
1048
        }
1049
    }
1050
28473
    jpeg2000_flush(s);
1051
1052
28473
    if (codsty->csty & JPEG2000_CSTY_EPH) {
1053
        if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1054
            bytestream2_skip(&s->g, 2);
1055
        else
1056
            av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1057
    }
1058
1059
105886
    for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1060
77413
        Jpeg2000Band *band = rlevel->band + bandno;
1061
77413
        Jpeg2000Prec *prec = band->prec + precno;
1062
1063
77413
        nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1064
526612
        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1065
449199
            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1066

449199
            if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1067
84635
                continue;
1068
729128
            for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1069
364564
                if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1070
364272
                    size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1071
364272
                    void *new = av_realloc(cblk->data, new_size);
1072
364272
                    if (new) {
1073
364272
                        cblk->data = new;
1074
364272
                        cblk->data_allocated = new_size;
1075
                    }
1076
                }
1077
364564
                if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1078
364564
                    || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1079
                ) {
1080
                    av_log(s->avctx, AV_LOG_ERROR,
1081
                        "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1082
                        cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1083
                    return AVERROR_INVALIDDATA;
1084
                }
1085
1086
364564
                bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1087
364564
                cblk->length   += cblk->lengthinc[cwsno];
1088
364564
                cblk->lengthinc[cwsno] = 0;
1089
364564
                if (cblk->nb_terminationsinc) {
1090
                    cblk->nb_terminationsinc--;
1091
                    cblk->nb_terminations++;
1092
                    cblk->data[cblk->length++] = 0xFF;
1093
                    cblk->data[cblk->length++] = 0xFF;
1094
                    cblk->data_start[cblk->nb_terminations] = cblk->length;
1095
                }
1096
            }
1097
364564
            av_freep(&cblk->lengthinc);
1098
        }
1099
    }
1100
28473
    return 0;
1101
}
1102
1103
1331
static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1104
                                             int RSpoc, int CSpoc,
1105
                                             int LYEpoc, int REpoc, int CEpoc,
1106
                                             int Ppoc, int *tp_index)
1107
{
1108
1331
    int ret = 0;
1109
    int layno, reslevelno, compno, precno, ok_reslevel;
1110
    int x, y;
1111
    int step_x, step_y;
1112
1113

1331
    switch (Ppoc) {
1114
    case JPEG2000_PGOD_RLCP:
1115
        av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1116
        ok_reslevel = 1;
1117
        for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1118
            ok_reslevel = 0;
1119
            for (layno = 0; layno < LYEpoc; layno++) {
1120
                for (compno = CSpoc; compno < CEpoc; compno++) {
1121
                    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1122
                    Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1123
                    if (reslevelno < codsty->nreslevels) {
1124
                        Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1125
                                                reslevelno;
1126
                        ok_reslevel = 1;
1127
                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1128
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1129
                                                              codsty, rlevel,
1130
                                                              precno, layno,
1131
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1132
                                                              qntsty->nguardbits)) < 0)
1133
                                return ret;
1134
                    }
1135
                }
1136
            }
1137
        }
1138
        break;
1139
1140
1326
    case JPEG2000_PGOD_LRCP:
1141
1326
        av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1142
2652
        for (layno = 0; layno < LYEpoc; layno++) {
1143
1326
            ok_reslevel = 1;
1144

11934
            for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1145
10608
                ok_reslevel = 0;
1146
42432
                for (compno = CSpoc; compno < CEpoc; compno++) {
1147
31824
                    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1148
31824
                    Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1149
31824
                    if (reslevelno < codsty->nreslevels) {
1150
27846
                        Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1151
                                                reslevelno;
1152
27846
                        ok_reslevel = 1;
1153
55692
                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1154
27846
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1155
                                                              codsty, rlevel,
1156
                                                              precno, layno,
1157
27846
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1158
27846
                                                              qntsty->nguardbits)) < 0)
1159
                                return ret;
1160
                    }
1161
                }
1162
            }
1163
        }
1164
1326
        break;
1165
1166
5
    case JPEG2000_PGOD_CPRL:
1167
5
        av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1168
22
        for (compno = CSpoc; compno < CEpoc; compno++) {
1169
17
            Jpeg2000Component *comp     = tile->comp + compno;
1170
17
            Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1171
17
            Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1172
17
            step_x = 32;
1173
17
            step_y = 32;
1174
1175
17
            if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1176
                continue;
1177
1178
119
            for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1179
102
                uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1180
102
                Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1181
102
                step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1182
102
                step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1183
            }
1184

17
            if (step_x >= 31 || step_y >= 31){
1185
                avpriv_request_sample(s->avctx, "CPRL with large step");
1186
                return AVERROR_PATCHWELCOME;
1187
            }
1188
17
            step_x = 1<<step_x;
1189
17
            step_y = 1<<step_y;
1190
1191
70
            for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1192
421
                for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1193
2576
                    for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1194
                        unsigned prcx, prcy;
1195
2208
                        uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1196
2208
                        Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1197
2208
                        int xc = x / s->cdx[compno];
1198
2208
                        int yc = y / s->cdy[compno];
1199
1200

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

984
                        if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1204
405
                            continue;
1205
1206
                        // check if a precinct exists
1207
579
                        prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1208
579
                        prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1209
579
                        prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1210
579
                        prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1211
1212
579
                        precno = prcx + rlevel->num_precincts_x * prcy;
1213
1214

579
                        if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1215
                            av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1216
                                   prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1217
                            continue;
1218
                        }
1219
1220
1206
                        for (layno = 0; layno < LYEpoc; layno++) {
1221
627
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1222
                                                              precno, layno,
1223
627
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1224
627
                                                              qntsty->nguardbits)) < 0)
1225
                                return ret;
1226
                        }
1227
                    }
1228
                }
1229
            }
1230
        }
1231
5
        break;
1232
1233
    case JPEG2000_PGOD_RPCL:
1234
        av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1235
        ok_reslevel = 1;
1236
        for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1237
            ok_reslevel = 0;
1238
            step_x = 30;
1239
            step_y = 30;
1240
            for (compno = CSpoc; compno < CEpoc; compno++) {
1241
                Jpeg2000Component *comp     = tile->comp + compno;
1242
                Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1243
1244
                if (reslevelno < codsty->nreslevels) {
1245
                    uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1246
                    Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1247
                    step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1248
                    step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1249
                }
1250
            }
1251
            step_x = 1<<step_x;
1252
            step_y = 1<<step_y;
1253
1254
            for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1255
                for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1256
                    for (compno = CSpoc; compno < CEpoc; compno++) {
1257
                        Jpeg2000Component *comp     = tile->comp + compno;
1258
                        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1259
                        Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1260
                        uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1261
                        Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1262
                        unsigned prcx, prcy;
1263
1264
                        int xc = x / s->cdx[compno];
1265
                        int yc = y / s->cdy[compno];
1266
1267
                        if (reslevelno >= codsty->nreslevels)
1268
                            continue;
1269
1270
                        if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1271
                            continue;
1272
1273
                        if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1274
                            continue;
1275
1276
                        // check if a precinct exists
1277
                        prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1278
                        prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1279
                        prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1280
                        prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1281
1282
                        precno = prcx + rlevel->num_precincts_x * prcy;
1283
1284
                        ok_reslevel = 1;
1285
                        if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1286
                            av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1287
                                   prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1288
                            continue;
1289
                        }
1290
1291
                            for (layno = 0; layno < LYEpoc; layno++) {
1292
                                if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1293
                                                                codsty, rlevel,
1294
                                                                precno, layno,
1295
                                                                qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1296
                                                                qntsty->nguardbits)) < 0)
1297
                                    return ret;
1298
                            }
1299
                    }
1300
                }
1301
            }
1302
        }
1303
        break;
1304
1305
    case JPEG2000_PGOD_PCRL:
1306
        av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1307
        step_x = 32;
1308
        step_y = 32;
1309
        for (compno = CSpoc; compno < CEpoc; compno++) {
1310
            Jpeg2000Component *comp     = tile->comp + compno;
1311
            Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1312
1313
            for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1314
                uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1315
                Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1316
                step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1317
                step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1318
            }
1319
        }
1320
        if (step_x >= 31 || step_y >= 31){
1321
            avpriv_request_sample(s->avctx, "PCRL with large step");
1322
            return AVERROR_PATCHWELCOME;
1323
        }
1324
        step_x = 1<<step_x;
1325
        step_y = 1<<step_y;
1326
1327
        for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1328
            for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1329
                for (compno = CSpoc; compno < CEpoc; compno++) {
1330
                    Jpeg2000Component *comp     = tile->comp + compno;
1331
                    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1332
                    Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1333
                    int xc = x / s->cdx[compno];
1334
                    int yc = y / s->cdy[compno];
1335
1336
                    for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1337
                        unsigned prcx, prcy;
1338
                        uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1339
                        Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1340
1341
                        if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1342
                            continue;
1343
1344
                        if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1345
                            continue;
1346
1347
                        // check if a precinct exists
1348
                        prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1349
                        prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1350
                        prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1351
                        prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1352
1353
                        precno = prcx + rlevel->num_precincts_x * prcy;
1354
1355
                        if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1356
                            av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1357
                                   prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1358
                            continue;
1359
                        }
1360
1361
                        for (layno = 0; layno < LYEpoc; layno++) {
1362
                            if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1363
                                                              precno, layno,
1364
                                                              qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1365
                                                              qntsty->nguardbits)) < 0)
1366
                                return ret;
1367
                        }
1368
                    }
1369
                }
1370
            }
1371
        }
1372
        break;
1373
1374
    default:
1375
        break;
1376
    }
1377
1378
1331
    return ret;
1379
}
1380
1381
1331
static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1382
{
1383
1331
    int ret = AVERROR_BUG;
1384
    int i;
1385
1331
    int tp_index = 0;
1386
1387
1331
    s->bit_index = 8;
1388
1331
    if (tile->poc.nb_poc) {
1389
        for (i=0; i<tile->poc.nb_poc; i++) {
1390
            Jpeg2000POCEntry *e = &tile->poc.poc[i];
1391
            ret = jpeg2000_decode_packets_po_iteration(s, tile,
1392
                e->RSpoc, e->CSpoc,
1393
                FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1394
                e->REpoc,
1395
                FFMIN(e->CEpoc, s->ncomponents),
1396
                e->Ppoc, &tp_index
1397
                );
1398
            if (ret < 0)
1399
                return ret;
1400
        }
1401
    } else {
1402
1331
        ret = jpeg2000_decode_packets_po_iteration(s, tile,
1403
            0, 0,
1404
1331
            tile->codsty[0].nlayers,
1405
            33,
1406
            s->ncomponents,
1407
1331
            tile->codsty[0].prog_order,
1408
            &tp_index
1409
        );
1410
    }
1411
    /* EOC marker reached */
1412
1331
    bytestream2_skip(&s->g, 2);
1413
1414
1331
    return ret;
1415
}
1416
1417
/* TIER-1 routines */
1418
877598
static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1419
                           int bpno, int bandno,
1420
                           int vert_causal_ctx_csty_symbol)
1421
{
1422
877598
    int mask = 3 << (bpno - 1), y0, x, y;
1423
1424
3778218
    for (y0 = 0; y0 < height; y0 += 4)
1425
50688616
        for (x = 0; x < width; x++)
1426

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

189519617
                if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1429
                    flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1430
189519617
                if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1431
73928626
                && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1432
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))) {
1433
14815869
                        int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1434
14815869
                        if (t1->mqc.raw)
1435
                             t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1436
                        else
1437
29631738
                             t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1438
14815869
                                               -mask : mask;
1439
1440
14815869
                        ff_jpeg2000_set_significance(t1, x, y,
1441
14815869
                                                     t1->data[(y) * t1->stride + x] < 0);
1442
                    }
1443
51305930
                    t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1444
                }
1445
            }
1446
877598
}
1447
1448
774996
static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1449
                           int bpno, int vert_causal_ctx_csty_symbol)
1450
{
1451
    int phalf, nhalf;
1452
    int y0, x, y;
1453
1454
774996
    phalf = 1 << (bpno - 1);
1455
774996
    nhalf = -phalf;
1456
1457
3332385
    for (y0 = 0; y0 < height; y0 += 4)
1458
44968233
        for (x = 0; x < width; x++)
1459

210634860
            for (y = y0; y < height && y < y0 + 4; y++)
1460
168224016
                if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1461
                    int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1462
17836470
                        ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1463
17836470
                    int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1464
35672940
                    int r     = ff_mqc_decode(&t1->mqc,
1465
17836470
                                              t1->mqc.cx_states + ctxno)
1466
17836470
                                ? phalf : nhalf;
1467
17836470
                    t1->data[(y) * t1->stride + x]          += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1468
17836470
                    t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1469
                }
1470
774996
}
1471
1472
1072421
static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1473
                           int width, int height, int bpno, int bandno,
1474
                           int seg_symbols, int vert_causal_ctx_csty_symbol)
1475
{
1476
1072421
    int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1477
1478
4706924
    for (y0 = 0; y0 < height; y0 += 4) {
1479
64726351
        for (x = 0; x < width; x++) {
1480
61091848
            int flags_mask = -1;
1481
61091848
            if (vert_causal_ctx_csty_symbol)
1482
                flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1483
61091848
            if (y0 + 3 < height &&
1484
60371743
                !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1485
40370473
                  (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1486
38034169
                  (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1487
35951268
                  (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1488
35159913
                if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1489
32594876
                    continue;
1490
2565037
                runlen = ff_mqc_decode(&t1->mqc,
1491
2565037
                                       t1->mqc.cx_states + MQC_CX_UNI);
1492
5130074
                runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1493
2565037
                                                       t1->mqc.cx_states +
1494
                                                       MQC_CX_UNI);
1495
2565037
                dec = 1;
1496
            } else {
1497
25931935
                runlen = 0;
1498
25931935
                dec    = 0;
1499
            }
1500
1501

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

108103052
                if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1504
                    flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1505
108103052
                if (!dec) {
1506
105538015
                    if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1507
49943741
                        dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1508
                                                                                             bandno));
1509
                    }
1510
                }
1511
108103052
                if (dec) {
1512
                    int xorbit;
1513
8653736
                    int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1514
                                                        &xorbit);
1515
17307472
                    t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1516
8653736
                                                    t1->mqc.cx_states + ctxno) ^
1517
                                      xorbit)
1518
8653736
                                     ? -mask : mask;
1519
8653736
                    ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1520
                }
1521
108103052
                dec = 0;
1522
108103052
                t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1523
            }
1524
        }
1525
    }
1526
1072421
    if (seg_symbols) {
1527
        int val;
1528
        val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1529
        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1530
        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1531
        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1532
        if (val != 0xa)
1533
            av_log(s->avctx, AV_LOG_ERROR,
1534
                   "Segmentation symbol value incorrect\n");
1535
    }
1536
1072421
}
1537
1538
443079
static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1539
                       Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1540
                       int width, int height, int bandpos)
1541
{
1542
443079
    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
1543
443079
    int pass_cnt = 0;
1544
443079
    int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1545
443079
    int term_cnt = 0;
1546
    int coder_type;
1547
1548

443079
    av_assert0(width <= 1024U && height <= 1024U);
1549
443079
    av_assert0(width*height <= 4096);
1550
1551
443079
    memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1552
1553
    /* If code-block contains no compressed data: nothing to do. */
1554
443079
    if (!cblk->length)
1555
80351
        return 0;
1556
1557
362728
    memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1558
1559
362728
    cblk->data[cblk->length] = 0xff;
1560
362728
    cblk->data[cblk->length+1] = 0xff;
1561
362728
    ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1562
1563
3087743
    while (passno--) {
1564
2725015
        if (bpno < 0) {
1565
            av_log(s->avctx, AV_LOG_ERROR, "bpno became negative\n");
1566
            return AVERROR_INVALIDDATA;
1567
        }
1568

2725015
        switch(pass_t) {
1569
877598
        case 0:
1570
877598
            decode_sigpass(t1, width, height, bpno + 1, bandpos,
1571
                           vert_causal_ctx_csty_symbol);
1572
877598
            break;
1573
774996
        case 1:
1574
774996
            decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1575
774996
            break;
1576
1072421
        case 2:
1577
            av_assert2(!t1->mqc.raw);
1578
1072421
            decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1579
1072421
                           codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1580
                           vert_causal_ctx_csty_symbol);
1581
1072421
            break;
1582
        }
1583
2725015
        if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1584
            ff_mqc_init_contexts(&t1->mqc);
1585
1586

2725015
        if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1587
            if (term_cnt >= cblk->nb_terminations) {
1588
                av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1589
                return AVERROR_INVALIDDATA;
1590
            }
1591
            if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1592
                av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1593
                    cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1594
                    pass_cnt, cblk->npasses);
1595
            }
1596
1597
            ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1598
        }
1599
1600
2725015
        pass_t++;
1601
2725015
        if (pass_t == 3) {
1602
1072421
            bpno--;
1603
1072421
            pass_t = 0;
1604
        }
1605
2725015
        pass_cnt ++;
1606
    }
1607
1608

362728
    if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1609
1003
        av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1610
1003
               cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1611
    }
1612
1613
362728
    return 1;
1614
}
1615
1616
/* TODO: Verify dequantization for lossless case
1617
 * comp->data can be float or int
1618
 * band->stepsize can be float or int
1619
 * depending on the type of DWT transformation.
1620
 * see ISO/IEC 15444-1:2002 A.6.1 */
1621
1622
/* Float dequantization of a codeblock.*/
1623
2415
static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1624
                                 Jpeg2000Component *comp,
1625
                                 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1626
{
1627
    int i, j;
1628
2415
    int w = cblk->coord[0][1] - cblk->coord[0][0];
1629
75375
    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1630
72960
        float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1631
72960
        int *src = t1->data + j*t1->stride;
1632
9374976
        for (i = 0; i < w; ++i)
1633
9302016
            datap[i] = src[i] * band->f_stepsize;
1634
    }
1635
2415
}
1636
1637
/* Integer dequantization of a codeblock.*/
1638
163728
static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1639
                               Jpeg2000Component *comp,
1640
                               Jpeg2000T1Context *t1, Jpeg2000Band *band)
1641
{
1642
    int i, j;
1643
163728
    int w = cblk->coord[0][1] - cblk->coord[0][0];
1644
2456529
    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1645
2292801
        int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1646
2292801
        int *src = t1->data + j*t1->stride;
1647
2292801
        if (band->i_stepsize == 32768) {
1648
37101173
            for (i = 0; i < w; ++i)
1649
34808372
                datap[i] = src[i] / 2;
1650
        } else {
1651
            // This should be VERY uncommon
1652
            for (i = 0; i < w; ++i)
1653
                datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1654
        }
1655
    }
1656
163728
}
1657
1658
196585
static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1659
                               Jpeg2000Component *comp,
1660
                               Jpeg2000T1Context *t1, Jpeg2000Band *band)
1661
{
1662
    int i, j;
1663
196585
    int w = cblk->coord[0][1] - cblk->coord[0][0];
1664
3127387
    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1665
2930802
        int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1666
2930802
        int *src = t1->data + j*t1->stride;
1667
51769172
        for (i = 0; i < w; ++i)
1668
48838370
            datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1669
    }
1670
196585
}
1671
1672
3
static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1673
{
1674
3
    int i, csize = 1;
1675
    void *src[3];
1676
1677
9
    for (i = 1; i < 3; i++) {
1678
6
        if (tile->codsty[0].transform != tile->codsty[i].transform) {
1679
            av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1680
            return;
1681
        }
1682
6
        if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1683
            av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1684
            return;
1685
        }
1686
    }
1687
1688
12
    for (i = 0; i < 3; i++)
1689
9
        if (tile->codsty[0].transform == FF_DWT97)
1690
            src[i] = tile->comp[i].f_data;
1691
        else
1692
9
            src[i] = tile->comp[i].i_data;
1693
1694
9
    for (i = 0; i < 2; i++)
1695
6
        csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1696
1697
3
    s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1698
}
1699
1700
1331
static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1701
{
1702
    Jpeg2000T1Context t1;
1703
1704
    int compno, reslevelno, bandno;
1705
1706
    /* Loop on tile components */
1707
5326
    for (compno = 0; compno < s->ncomponents; compno++) {
1708
3995
        Jpeg2000Component *comp     = tile->comp + compno;
1709
3995
        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1710
3995
        int coded = 0;
1711
1712
3995
        t1.stride = (1<<codsty->log2_cblk_width) + 2;
1713
1714
        /* Loop on resolution levels */
1715
31943
        for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1716
27948
            Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1717
            /* Loop on bands */
1718
103802
            for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1719
                int nb_precincts, precno;
1720
75854
                Jpeg2000Band *band = rlevel->band + bandno;
1721
75854
                int cblkno = 0, bandpos;
1722
1723
75854
                bandpos = bandno + (reslevelno > 0);
1724
1725
75854
                if (band->coord[0][0] == band->coord[0][1] ||
1726
75854
                    band->coord[1][0] == band->coord[1][1])
1727
3672
                    continue;
1728
1729
72182
                nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1730
                /* Loop on precincts */
1731
145795
                for (precno = 0; precno < nb_precincts; precno++) {
1732
73613
                    Jpeg2000Prec *prec = band->prec + precno;
1733
1734
                    /* Loop on codeblocks */
1735
73613
                    for (cblkno = 0;
1736
516692
                         cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1737
443079
                         cblkno++) {
1738
                        int x, y;
1739
443079
                        Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1740
443079
                        int ret = decode_cblk(s, codsty, &t1, cblk,
1741
443079
                                    cblk->coord[0][1] - cblk->coord[0][0],
1742
443079
                                    cblk->coord[1][1] - cblk->coord[1][0],
1743
                                    bandpos);
1744
443079
                        if (ret)
1745
362728
                            coded = 1;
1746
                        else
1747
80351
                            continue;
1748
362728
                        x = cblk->coord[0][0] - band->coord[0][0];
1749
362728
                        y = cblk->coord[1][0] - band->coord[1][0];
1750
1751
362728
                        if (codsty->transform == FF_DWT97)
1752
2415
                            dequantization_float(x, y, cblk, comp, &t1, band);
1753
360313
                        else if (codsty->transform == FF_DWT97_INT)
1754
196585
                            dequantization_int_97(x, y, cblk, comp, &t1, band);
1755
                        else
1756
163728
                            dequantization_int(x, y, cblk, comp, &t1, band);
1757
                   } /* end cblk */
1758
                } /*end prec */
1759
            } /* end band */
1760
        } /* end reslevel */
1761
1762
        /* inverse DWT */
1763
3995
        if (coded)
1764
3995
            ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1765
1766
    } /*end comp */
1767
1331
}
1768
1769
#define WRITE_FRAME(D, PIXEL)                                                                     \
1770
    static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,         \
1771
                                         AVFrame * picture, int precision)                        \
1772
    {                                                                                             \
1773
        const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);               \
1774
        int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);                              \
1775
        int pixelsize = planar ? 1 : pixdesc->nb_components;                                      \
1776
                                                                                                  \
1777
        int compno;                                                                               \
1778
        int x, y;                                                                                 \
1779
                                                                                                  \
1780
        for (compno = 0; compno < s->ncomponents; compno++) {                                     \
1781
            Jpeg2000Component *comp     = tile->comp + compno;                                    \
1782
            Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \
1783
            PIXEL *line;                                                                          \
1784
            float *datap     = comp->f_data;                                                      \
1785
            int32_t *i_datap = comp->i_data;                                                      \
1786
            int cbps         = s->cbps[compno];                                                   \
1787
            int w            = tile->comp[compno].coord[0][1] - s->image_offset_x;                \
1788
            int plane        = 0;                                                                 \
1789
                                                                                                  \
1790
            if (planar)                                                                           \
1791
                plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);                 \
1792
                                                                                                  \
1793
            y    = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];           \
1794
            line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1795
            for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) {                 \
1796
                PIXEL *dst;                                                                       \
1797
                                                                                                  \
1798
                x   = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];        \
1799
                dst = line + x * pixelsize + compno*!planar;                                      \
1800
                                                                                                  \
1801
                if (codsty->transform == FF_DWT97) {                                              \
1802
                    for (; x < w; x++) {                                                          \
1803
                        int val = lrintf(*datap) + (1 << (cbps - 1));                             \
1804
                        /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1805
                        val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1806
                        *dst = val << (precision - cbps);                                         \
1807
                        datap++;                                                                  \
1808
                        dst += pixelsize;                                                         \
1809
                    }                                                                             \
1810
                } else {                                                                          \
1811
                    for (; x < w; x++) {                                                          \
1812
                        int val = *i_datap + (1 << (cbps - 1));                                   \
1813
                        /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1814
                        val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1815
                        *dst = val << (precision - cbps);                                         \
1816
                        i_datap++;                                                                \
1817
                        dst += pixelsize;                                                         \
1818
                    }                                                                             \
1819
                }                                                                                 \
1820
                line += picture->linesize[plane] / sizeof(PIXEL);                                 \
1821
            }                                                                                     \
1822
        }                                                                                         \
1823
                                                                                                  \
1824
    }
1825
1826




93961380
WRITE_FRAME(8, uint8_t)
1827




28113934
WRITE_FRAME(16, uint16_t)
1828
1829
#undef WRITE_FRAME
1830
1831
1331
static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1832
                                int jobnr, int threadnr)
1833
{
1834
1331
    Jpeg2000DecoderContext *s = avctx->priv_data;
1835
1331
    AVFrame *picture = td;
1836
1331
    Jpeg2000Tile *tile = s->tile + jobnr;
1837
    int x;
1838
1839
1331
    tile_codeblocks(s, tile);
1840
1841
    /* inverse MCT transformation */
1842
1331
    if (tile->codsty[0].mct)
1843
3
        mct_decode(s, tile);
1844
1845
4089
    for (x = 0; x < s->ncomponents; x++) {
1846
3170
        if (s->cdef[x] < 0) {
1847
1649
            for (x = 0; x < s->ncomponents; x++) {
1848
1237
                s->cdef[x] = x + 1;
1849
            }
1850
412
            if ((s->ncomponents & 1) == 0)
1851
1
                s->cdef[s->ncomponents-1] = 0;
1852
412
            break;
1853
        }
1854
    }
1855
1856
1331
    if (s->precision <= 8) {
1857
1326
        write_frame_8(s, tile, picture, 8);
1858
    } else {
1859
12
        int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1860
2
                        picture->format == AV_PIX_FMT_RGB48 ||
1861
2
                        picture->format == AV_PIX_FMT_RGBA64 ||
1862

7
                        picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1863
1864
5
        write_frame_16(s, tile, picture, precision);
1865
    }
1866
1867
1331
    return 0;
1868
}
1869
1870
412
static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1871
{
1872
    int tileno, compno;
1873
1743
    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1874
1331
        if (s->tile[tileno].comp) {
1875
5326
            for (compno = 0; compno < s->ncomponents; compno++) {
1876
3995
                Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
1877
3995
                Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1878
1879
3995
                ff_jpeg2000_cleanup(comp, codsty);
1880
            }
1881
1331
            av_freep(&s->tile[tileno].comp);
1882
        }
1883
    }
1884
412
    av_freep(&s->tile);
1885
412
    memset(s->codsty, 0, sizeof(s->codsty));
1886
412
    memset(s->qntsty, 0, sizeof(s->qntsty));
1887
412
    memset(s->properties, 0, sizeof(s->properties));
1888
412
    memset(&s->poc  , 0, sizeof(s->poc));
1889
412
    s->numXtiles = s->numYtiles = 0;
1890
412
    s->ncomponents = 0;
1891
412
}
1892
1893
412
static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1894
{
1895
412
    Jpeg2000CodingStyle *codsty = s->codsty;
1896
412
    Jpeg2000QuantStyle *qntsty  = s->qntsty;
1897
412
    Jpeg2000POC         *poc    = &s->poc;
1898
412
    uint8_t *properties         = s->properties;
1899
1900
3951
    for (;;) {
1901
4363
        int len, ret = 0;
1902
        uint16_t marker;
1903
        int oldpos;
1904
1905
4363
        if (bytestream2_get_bytes_left(&s->g) < 2) {
1906
            av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1907
            break;
1908
        }
1909
1910
4363
        marker = bytestream2_get_be16u(&s->g);
1911
4363
        oldpos = bytestream2_tell(&s->g);
1912
1913
4363
        if (marker == JPEG2000_SOD) {
1914
            Jpeg2000Tile *tile;
1915
            Jpeg2000TilePart *tp;
1916
1917
1343
            if (!s->tile) {
1918
                av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1919
                return AVERROR_INVALIDDATA;
1920
            }
1921
1343
            if (s->curtileno < 0) {
1922
                av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1923
                return AVERROR_INVALIDDATA;
1924
            }
1925
1926
1343
            tile = s->tile + s->curtileno;
1927
1343
            tp = tile->tile_part + tile->tp_idx;
1928
1343
            if (tp->tp_end < s->g.buffer) {
1929
                av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1930
                return AVERROR_INVALIDDATA;
1931
            }
1932
1343
            bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1933
1343
            bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1934
1935
1343
            continue;
1936
        }
1937
3020
        if (marker == JPEG2000_EOC)
1938
412
            break;
1939
1940
2608
        len = bytestream2_get_be16(&s->g);
1941

2608
        if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
1942
            av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
1943
            return AVERROR_INVALIDDATA;
1944
        }
1945
1946


2608
        switch (marker) {
1947
412
        case JPEG2000_SIZ:
1948
412
            if (s->ncomponents) {
1949
                av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
1950
                return AVERROR_INVALIDDATA;
1951
            }
1952
412
            ret = get_siz(s);
1953
412
            if (!s->tile)
1954
                s->numXtiles = s->numYtiles = 0;
1955
412
            break;
1956
6
        case JPEG2000_COC:
1957
6
            ret = get_coc(s, codsty, properties);
1958
6
            break;
1959
412
        case JPEG2000_COD:
1960
412
            ret = get_cod(s, codsty, properties);
1961
412
            break;
1962
9
        case JPEG2000_QCC:
1963
9
            ret = get_qcc(s, len, qntsty, properties);
1964
9
            break;
1965
412
        case JPEG2000_QCD:
1966
412
            ret = get_qcd(s, len, qntsty, properties);
1967
412
            break;
1968
        case JPEG2000_POC:
1969
            ret = get_poc(s, len, poc);
1970
            break;
1971
1343
        case JPEG2000_SOT:
1972
1343
            if (!(ret = get_sot(s, len))) {
1973
                av_assert1(s->curtileno >= 0);
1974
1343
                codsty = s->tile[s->curtileno].codsty;
1975
1343
                qntsty = s->tile[s->curtileno].qntsty;
1976
1343
                poc    = &s->tile[s->curtileno].poc;
1977
1343
                properties = s->tile[s->curtileno].properties;
1978
            }
1979
1343
            break;
1980
10
        case JPEG2000_PLM:
1981
            // the PLM marker is ignored
1982
        case JPEG2000_COM:
1983
            // the comment is ignored
1984
10
            bytestream2_skip(&s->g, len - 2);
1985
10
            break;
1986
4
        case JPEG2000_TLM:
1987
            // Tile-part lengths
1988
4
            ret = get_tlm(s, len);
1989
4
            break;
1990
        case JPEG2000_PLT:
1991
            // Packet length, tile-part header
1992
            ret = get_plt(s, len);
1993
            break;
1994
        default:
1995
            av_log(s->avctx, AV_LOG_ERROR,
1996
                   "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1997
                   marker, bytestream2_tell(&s->g) - 4);
1998
            bytestream2_skip(&s->g, len - 2);
1999
            break;
2000
        }
2001

2608
        if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2002
            av_log(s->avctx, AV_LOG_ERROR,
2003
                   "error during processing marker segment %.4"PRIx16"\n",
2004
                   marker);
2005
            return ret ? ret : -1;
2006
        }
2007
    }
2008
412
    return 0;
2009
}
2010
2011
/* Read bit stream packets --> T2 operation. */
2012
412
static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2013
{
2014
412
    int ret = 0;
2015
    int tileno;
2016
2017
1743
    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2018
1331
        Jpeg2000Tile *tile = s->tile + tileno;
2019
2020
1331
        if ((ret = init_tile(s, tileno)) < 0)
2021
            return ret;
2022
2023
1331
        s->g = tile->tile_part[0].tpg;
2024
1331
        if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2025
            return ret;
2026
    }
2027
2028
412
    return 0;
2029
}
2030
2031
409
static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2032
{
2033
    uint32_t atom_size, atom, atom_end;
2034
409
    int search_range = 10;
2035
2036
1228
    while (search_range
2037
1228
           &&
2038
1228
           bytestream2_get_bytes_left(&s->g) >= 8) {
2039
1228
        atom_size = bytestream2_get_be32u(&s->g);
2040
1228
        atom      = bytestream2_get_be32u(&s->g);
2041
1228
        if (atom_size == 1) {
2042
            if (bytestream2_get_be32u(&s->g)) {
2043
                avpriv_request_sample(s->avctx, "Huge atom");
2044
                return 0;
2045
            }
2046
            atom_size = bytestream2_get_be32u(&s->g);
2047
            atom_end  = bytestream2_tell(&s->g) + atom_size - 16;
2048
        } else {
2049
1228
            atom_end  = bytestream2_tell(&s->g) + atom_size -  8;
2050
        }
2051
2052
1228
        if (atom == JP2_CODESTREAM)
2053
409
            return 1;
2054
2055

819
        if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2056
            return 0;
2057
2058

819
        if (atom == JP2_HEADER &&
2059
409
                   atom_size >= 16) {
2060
            uint32_t atom2_size, atom2, atom2_end;
2061
            do {
2062
818
                atom2_size = bytestream2_get_be32u(&s->g);
2063
818
                atom2      = bytestream2_get_be32u(&s->g);
2064
818
                atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
2065

818
                if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2066
                    break;
2067
818
                atom2_size -= 8;
2068
818
                if (atom2 == JP2_CODESTREAM) {
2069
                    return 1;
2070

1227
                } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2071
409
                    int method = bytestream2_get_byteu(&s->g);
2072
409
                    bytestream2_skipu(&s->g, 2);
2073
409
                    if (method == 1) {
2074
409
                        s->colour_space = bytestream2_get_be32u(&s->g);
2075
                    }
2076

409
                } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2077
                    int i, size, colour_count, colour_channels, colour_depth[3];
2078
                    colour_count = bytestream2_get_be16u(&s->g);
2079
                    colour_channels = bytestream2_get_byteu(&s->g);
2080
                    // FIXME: Do not ignore channel_sign
2081
                    colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2082
                    colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2083
                    colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2084
                    size = (colour_depth[0] + 7 >> 3) * colour_count +
2085
                           (colour_depth[1] + 7 >> 3) * colour_count +
2086
                           (colour_depth[2] + 7 >> 3) * colour_count;
2087
                    if (colour_count > AVPALETTE_COUNT ||
2088
                        colour_channels != 3 ||
2089
                        colour_depth[0] > 16 ||
2090
                        colour_depth[1] > 16 ||
2091
                        colour_depth[2] > 16 ||
2092
                        atom2_size < size) {
2093
                        avpriv_request_sample(s->avctx, "Unknown palette");
2094
                        bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2095
                        continue;
2096
                    }
2097
                    s->pal8 = 1;
2098
                    for (i = 0; i < colour_count; i++) {
2099
                        uint32_t r, g, b;
2100
                        if (colour_depth[0] <= 8) {
2101
                            r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2102
                            r |= r >> colour_depth[0];
2103
                        } else {
2104
                            r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2105
                        }
2106
                        if (colour_depth[1] <= 8) {
2107
                            g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2108
                            g |= g >> colour_depth[1];
2109
                        } else {
2110
                            g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2111
                        }
2112
                        if (colour_depth[2] <= 8) {
2113
                            b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2114
                            b |= b >> colour_depth[2];
2115
                        } else {
2116
                            b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2117
                        }
2118
                        s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2119
                    }
2120

409
                } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2121
                    int n = bytestream2_get_be16u(&s->g);
2122
                    for (; n>0; n--) {
2123
                        int cn   = bytestream2_get_be16(&s->g);
2124
                        int av_unused typ  = bytestream2_get_be16(&s->g);
2125
                        int asoc = bytestream2_get_be16(&s->g);
2126
                        if (cn < 4 && asoc < 4)
2127
                            s->cdef[cn] = asoc;
2128
                    }
2129

409
                } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2130
                    int64_t vnum, vden, hnum, hden, vexp, hexp;
2131
                    uint32_t resx;
2132
                    bytestream2_skip(&s->g, 4);
2133
                    resx = bytestream2_get_be32u(&s->g);
2134
                    if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2135
                        bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2136
                        continue;
2137
                    }
2138
                    vnum = bytestream2_get_be16u(&s->g);
2139
                    vden = bytestream2_get_be16u(&s->g);
2140
                    hnum = bytestream2_get_be16u(&s->g);
2141
                    hden = bytestream2_get_be16u(&s->g);
2142
                    vexp = bytestream2_get_byteu(&s->g);
2143
                    hexp = bytestream2_get_byteu(&s->g);
2144
                    if (!vnum || !vden || !hnum || !hden) {
2145
                        bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2146
                        av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2147
                        continue;
2148
                    }
2149
                    if (vexp > hexp) {
2150
                        vexp -= hexp;
2151
                        hexp = 0;
2152
                    } else {
2153
                        hexp -= vexp;
2154
                        vexp = 0;
2155
                    }
2156
                    if (   INT64_MAX / (hnum * vden) > pow(10, hexp)
2157
                        && INT64_MAX / (vnum * hden) > pow(10, vexp))
2158
                        av_reduce(&s->sar.den, &s->sar.num,
2159
                                  hnum * vden * pow(10, hexp),
2160
                                  vnum * hden * pow(10, vexp),
2161
                                  INT32_MAX);
2162
                }
2163
818
                bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2164
818
            } while (atom_end - atom2_end >= 8);
2165
        } else {
2166
410
            search_range--;
2167
        }
2168
819
        bytestream2_seek(&s->g, atom_end, SEEK_SET);
2169
    }
2170
2171
    return 0;
2172
}
2173
2174
10
static av_cold void jpeg2000_init_static_data(void)
2175
{
2176
10
    ff_jpeg2000_init_tier1_luts();
2177
10
    ff_mqc_init_context_tables();
2178
10
}
2179
2180
19
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2181
{
2182
    static AVOnce init_static_once = AV_ONCE_INIT;
2183
19
    Jpeg2000DecoderContext *s = avctx->priv_data;
2184
2185
19
    ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2186
19
    ff_jpeg2000dsp_init(&s->dsp);
2187
2188
19
    return 0;
2189
}
2190
2191
412
static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2192
                                 int *got_frame, AVPacket *avpkt)
2193
{
2194
412
    Jpeg2000DecoderContext *s = avctx->priv_data;
2195
412
    ThreadFrame frame = { .f = data };
2196
412
    AVFrame *picture = data;
2197
    int ret;
2198
2199
412
    s->avctx     = avctx;
2200
412
    bytestream2_init(&s->g, avpkt->data, avpkt->size);
2201
412
    s->curtileno = -1;
2202
412
    memset(s->cdef, -1, sizeof(s->cdef));
2203
2204
412
    if (bytestream2_get_bytes_left(&s->g) < 2) {
2205
        ret = AVERROR_INVALIDDATA;
2206
        goto end;
2207
    }
2208
2209
    // check if the image is in jp2 format
2210

824
    if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2211
821
       (bytestream2_get_be32u(&s->g) == 12) &&
2212
818
       (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2213
409
       (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2214
409
        if (!jp2_find_codestream(s)) {
2215
            av_log(avctx, AV_LOG_ERROR,
2216
                   "Could not find Jpeg2000 codestream atom.\n");
2217
            ret = AVERROR_INVALIDDATA;
2218
            goto end;
2219
        }
2220
    } else {
2221
3
        bytestream2_seek(&s->g, 0, SEEK_SET);
2222
    }
2223
2224

412
    while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2225
        bytestream2_skip(&s->g, 1);
2226
2227
412
    if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2228
        av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2229
        ret = AVERROR_INVALIDDATA;
2230
        goto end;
2231
    }
2232
412
    if (ret = jpeg2000_read_main_headers(s))
2233
        goto end;
2234
2235
    /* get picture buffer */
2236
412
    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2237
        goto end;
2238
412
    picture->pict_type = AV_PICTURE_TYPE_I;
2239
412
    picture->key_frame = 1;
2240
2241
412
    if (ret = jpeg2000_read_bitstream_packets(s))
2242
        goto end;
2243
2244
412
    avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2245
2246
412
    jpeg2000_dec_cleanup(s);
2247
2248
412
    *got_frame = 1;
2249
2250
412
    if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2251
        memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2252

412
    if (s->sar.num && s->sar.den)
2253
        avctx->sample_aspect_ratio = s->sar;
2254
412
    s->sar.num = s->sar.den = 0;
2255
2256
412
    return bytestream2_tell(&s->g);
2257
2258
end:
2259
    jpeg2000_dec_cleanup(s);
2260
    return ret;
2261
}
2262
2263
#define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2264
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2265
2266
static const AVOption options[] = {
2267
    { "lowres",  "Lower the decoding resolution by a power of two",
2268
        OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2269
    { NULL },
2270
};
2271
2272
static const AVClass jpeg2000_class = {
2273
    .class_name = "jpeg2000",
2274
    .item_name  = av_default_item_name,
2275
    .option     = options,
2276
    .version    = LIBAVUTIL_VERSION_INT,
2277
};
2278
2279
AVCodec ff_jpeg2000_decoder = {
2280
    .name             = "jpeg2000",
2281
    .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2282
    .type             = AVMEDIA_TYPE_VIDEO,
2283
    .id               = AV_CODEC_ID_JPEG2000,
2284
    .capabilities     = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2285
    .priv_data_size   = sizeof(Jpeg2000DecoderContext),
2286
    .init             = jpeg2000_decode_init,
2287
    .decode           = jpeg2000_decode_frame,
2288
    .priv_class       = &jpeg2000_class,
2289
    .max_lowres       = 5,
2290
    .profiles         = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)
2291
};