]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeg2000dec.c
avcodec/jpeg2000dec: Replace the step_x/y assert by a check in the CPRL case as with...
[ffmpeg] / libavcodec / jpeg2000dec.c
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 static int get_bits(Jpeg2000DecoderContext *s, int n)
133 {
134     int res = 0;
135
136     while (--n >= 0) {
137         res <<= 1;
138         if (s->bit_index == 0) {
139             s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
140         }
141         s->bit_index--;
142         res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
143     }
144     return res;
145 }
146
147 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
148 {
149     if (bytestream2_get_byte(&s->g) == 0xff)
150         bytestream2_skip(&s->g, 1);
151     s->bit_index = 8;
152 }
153
154 /* decode the value stored in node */
155 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
156                            int threshold)
157 {
158     Jpeg2000TgtNode *stack[30];
159     int sp = -1, curval = 0;
160
161     if (!node) {
162         av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
163         return AVERROR_INVALIDDATA;
164     }
165
166     while (node && !node->vis) {
167         stack[++sp] = node;
168         node        = node->parent;
169     }
170
171     if (node)
172         curval = node->val;
173     else
174         curval = stack[sp]->val;
175
176     while (curval < threshold && sp >= 0) {
177         if (curval < stack[sp]->val)
178             curval = stack[sp]->val;
179         while (curval < threshold) {
180             int ret;
181             if ((ret = get_bits(s, 1)) > 0) {
182                 stack[sp]->vis++;
183                 break;
184             } else if (!ret)
185                 curval++;
186             else
187                 return ret;
188         }
189         stack[sp]->val = curval;
190         sp--;
191     }
192     return curval;
193 }
194
195 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
196                          int bpc, uint32_t log2_chroma_wh, int pal8)
197 {
198     int match = 1;
199     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
200
201     av_assert2(desc);
202
203     if (desc->nb_components != components) {
204         return 0;
205     }
206
207     switch (components) {
208     case 4:
209         match = match && desc->comp[3].depth >= bpc &&
210                          (log2_chroma_wh >> 14 & 3) == 0 &&
211                          (log2_chroma_wh >> 12 & 3) == 0;
212     case 3:
213         match = match && desc->comp[2].depth >= bpc &&
214                          (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
215                          (log2_chroma_wh >>  8 & 3) == desc->log2_chroma_h;
216     case 2:
217         match = match && desc->comp[1].depth >= bpc &&
218                          (log2_chroma_wh >>  6 & 3) == desc->log2_chroma_w &&
219                          (log2_chroma_wh >>  4 & 3) == desc->log2_chroma_h;
220
221     case 1:
222         match = match && desc->comp[0].depth >= bpc &&
223                          (log2_chroma_wh >>  2 & 3) == 0 &&
224                          (log2_chroma_wh       & 3) == 0 &&
225                          (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
226     }
227     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 static int get_siz(Jpeg2000DecoderContext *s)
260 {
261     int i;
262     int ncomponents;
263     uint32_t log2_chroma_wh = 0;
264     const enum AVPixelFormat *possible_fmts = NULL;
265     int possible_fmts_nb = 0;
266     int ret;
267
268     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     s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
274     s->width          = bytestream2_get_be32u(&s->g); // Width
275     s->height         = bytestream2_get_be32u(&s->g); // Height
276     s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
277     s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
278     s->tile_width     = bytestream2_get_be32u(&s->g); // XTSiz
279     s->tile_height    = bytestream2_get_be32u(&s->g); // YTSiz
280     s->tile_offset_x  = bytestream2_get_be32u(&s->g); // XT0Siz
281     s->tile_offset_y  = bytestream2_get_be32u(&s->g); // YT0Siz
282     ncomponents       = bytestream2_get_be16u(&s->g); // CSiz
283
284     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     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     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     if (ncomponents > 4) {
300         avpriv_request_sample(s->avctx, "Support for %d components",
301                               ncomponents);
302         return AVERROR_PATCHWELCOME;
303     }
304
305     if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
306         s->image_offset_x < s->tile_offset_x ||
307         s->image_offset_y < s->tile_offset_y ||
308         s->tile_width  + (int64_t)s->tile_offset_x <= s->image_offset_x ||
309         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     s->ncomponents = ncomponents;
316
317     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     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     for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
329         uint8_t x    = bytestream2_get_byteu(&s->g);
330         s->cbps[i]   = (x & 0x7f) + 1;
331         s->precision = FFMAX(s->cbps[i], s->precision);
332         s->sgnd[i]   = !!(x & 0x80);
333         s->cdx[i]    = bytestream2_get_byteu(&s->g);
334         s->cdy[i]    = bytestream2_get_byteu(&s->g);
335         if (   !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
336             || !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         log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
341     }
342
343     s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
344     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     if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
348         s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
349     ) {
350         s->numXtiles = s->numYtiles = 0;
351         return AVERROR(EINVAL);
352     }
353
354     s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
355     if (!s->tile) {
356         s->numXtiles = s->numYtiles = 0;
357         return AVERROR(ENOMEM);
358     }
359
360     for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
361         Jpeg2000Tile *tile = s->tile + i;
362
363         tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
364         if (!tile->comp)
365             return AVERROR(ENOMEM);
366     }
367
368     /* compute image size with reduction factor */
369     ret = ff_set_dimensions(s->avctx,
370             ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
371                                                s->reduction_factor),
372             ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
373                                                s->reduction_factor));
374     if (ret < 0)
375         return ret;
376
377     if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
378         s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
379         possible_fmts = xyz_pix_fmts;
380         possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
381     } else {
382         switch (s->colour_space) {
383         case 16:
384             possible_fmts = rgb_pix_fmts;
385             possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
386             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     for (i = 0; i < possible_fmts_nb; ++i) {
402         if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
403             s->avctx->pix_fmt = possible_fmts[i];
404             break;
405         }
406     }
407
408     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     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     s->avctx->bits_per_raw_sample = s->precision;
445     return 0;
446 }
447
448 /* get common part for COD and COC segments */
449 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
450 {
451     uint8_t byte;
452
453     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     c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
461     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     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     c->nreslevels2decode = c->nreslevels - s->reduction_factor;
478
479     c->log2_cblk_width  = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
480     c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
481
482     if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
483         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     c->cblk_style = bytestream2_get_byteu(&s->g);
489     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     c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
495     /* set integer 9/7 DWT in case of BITEXACT flag */
496     if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
497         c->transform = FF_DWT97_INT;
498     else if (c->transform == FF_DWT53) {
499         s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
500     }
501
502     if (c->csty & JPEG2000_CSTY_PREC) {
503         int i;
504         for (i = 0; i < c->nreslevels; i++) {
505             byte = bytestream2_get_byte(&s->g);
506             c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
507             c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
508             if (i)
509                 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         memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
518         memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
519     }
520     return 0;
521 }
522
523 /* get coding parameters for a particular tile or whole image*/
524 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
525                    uint8_t *properties)
526 {
527     Jpeg2000CodingStyle tmp;
528     int compno, ret;
529
530     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     tmp.csty = bytestream2_get_byteu(&s->g);
536
537     // get progression order
538     tmp.prog_order = bytestream2_get_byteu(&s->g);
539
540     tmp.nlayers    = bytestream2_get_be16u(&s->g);
541     tmp.mct        = bytestream2_get_byteu(&s->g); // multiple component transformation
542
543     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     if ((ret = get_cox(s, &tmp)) < 0)
551         return ret;
552
553     for (compno = 0; compno < s->ncomponents; compno++)
554         if (!(properties[compno] & HAD_COC))
555             memcpy(c + compno, &tmp, sizeof(tmp));
556     return 0;
557 }
558
559 /* Get coding parameters for a component in the whole image or a
560  * particular tile. */
561 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
562                    uint8_t *properties)
563 {
564     int compno, ret;
565
566     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     compno = bytestream2_get_byteu(&s->g);
572
573     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     c      += compno;
581     c->csty = bytestream2_get_byteu(&s->g);
582
583     if ((ret = get_cox(s, c)) < 0)
584         return ret;
585
586     properties[compno] |= HAD_COC;
587     return 0;
588 }
589
590 /* Get common part for QCD and QCC segments. */
591 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
592 {
593     int i, x;
594
595     if (bytestream2_get_bytes_left(&s->g) < 1)
596         return AVERROR_INVALIDDATA;
597
598     x = bytestream2_get_byteu(&s->g); // Sqcd
599
600     q->nguardbits = x >> 5;
601     q->quantsty   = x & 0x1f;
602
603     if (q->quantsty == JPEG2000_QSTY_NONE) {
604         n -= 3;
605         if (bytestream2_get_bytes_left(&s->g) < n ||
606             n > JPEG2000_MAX_DECLEVELS*3)
607             return AVERROR_INVALIDDATA;
608         for (i = 0; i < n; i++)
609             q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
610     } 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         n = (n - 3) >> 1;
623         if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
624             n > JPEG2000_MAX_DECLEVELS*3)
625             return AVERROR_INVALIDDATA;
626         for (i = 0; i < n; i++) {
627             x          = bytestream2_get_be16u(&s->g);
628             q->expn[i] = x >> 11;
629             q->mant[i] = x & 0x7ff;
630         }
631     }
632     return 0;
633 }
634
635 /* Get quantization parameters for a particular tile or a whole image. */
636 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
637                    uint8_t *properties)
638 {
639     Jpeg2000QuantStyle tmp;
640     int compno, ret;
641
642     memset(&tmp, 0, sizeof(tmp));
643
644     if ((ret = get_qcx(s, n, &tmp)) < 0)
645         return ret;
646     for (compno = 0; compno < s->ncomponents; compno++)
647         if (!(properties[compno] & HAD_QCC))
648             memcpy(q + compno, &tmp, sizeof(tmp));
649     return 0;
650 }
651
652 /* Get quantization parameters for a component in the whole image
653  * on in a particular tile. */
654 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
655                    uint8_t *properties)
656 {
657     int compno;
658
659     if (bytestream2_get_bytes_left(&s->g) < 1)
660         return AVERROR_INVALIDDATA;
661
662     compno = bytestream2_get_byteu(&s->g);
663
664     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     properties[compno] |= HAD_QCC;
672     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 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     if (bytestream2_get_bytes_left(&s->g) < 8)
745         return AVERROR_INVALIDDATA;
746
747     s->curtileno = 0;
748     Isot = bytestream2_get_be16u(&s->g);        // Isot
749     if (Isot >= s->numXtiles * s->numYtiles)
750         return AVERROR_INVALIDDATA;
751
752     s->curtileno = Isot;
753     Psot  = bytestream2_get_be32u(&s->g);       // Psot
754     TPsot = bytestream2_get_byteu(&s->g);       // TPsot
755
756     /* Read TNSot but not used */
757     bytestream2_get_byteu(&s->g);               // TNsot
758
759     if (!Psot)
760         Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
761
762     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     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     s->tile[Isot].tp_idx = TPsot;
773     tp             = s->tile[Isot].tile_part + TPsot;
774     tp->tile_index = Isot;
775     tp->tp_end     = s->g.buffer + Psot - n - 2;
776
777     if (!TPsot) {
778         Jpeg2000Tile *tile = s->tile + s->curtileno;
779
780         /* copy defaults */
781         memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
782         memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
783         memcpy(&tile->poc  , &s->poc  , sizeof(tile->poc));
784         tile->poc.is_default = 1;
785     }
786
787     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 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
799 {
800     uint8_t Stlm, ST, SP, tile_tlm, i;
801     bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
802     Stlm = bytestream2_get_byte(&s->g);
803
804     // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
805     ST = (Stlm >> 4) & 0x03;
806     // TODO: Manage case of ST = 0b11 --> raise error
807     SP       = (Stlm >> 6) & 0x01;
808     tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
809     for (i = 0; i < tile_tlm; i++) {
810         switch (ST) {
811         case 0:
812             break;
813         case 1:
814             bytestream2_get_byte(&s->g);
815             break;
816         case 2:
817             bytestream2_get_be16(&s->g);
818             break;
819         case 3:
820             bytestream2_get_be32(&s->g);
821             break;
822         }
823         if (SP == 0) {
824             bytestream2_get_be16(&s->g);
825         } else {
826             bytestream2_get_be32(&s->g);
827         }
828     }
829     return 0;
830 }
831
832 static uint8_t get_plt(Jpeg2000DecoderContext *s, int n)
833 {
834     int i;
835
836     av_log(s->avctx, AV_LOG_DEBUG,
837             "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
838
839     /*Zplt =*/ bytestream2_get_byte(&s->g);
840
841     for (i = 0; i < n - 3; i++) {
842         bytestream2_get_byte(&s->g);
843     }
844
845     return 0;
846 }
847
848 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
849 {
850     int compno;
851     int tilex = tileno % s->numXtiles;
852     int tiley = tileno / s->numXtiles;
853     Jpeg2000Tile *tile = s->tile + tileno;
854
855     if (!tile->comp)
856         return AVERROR(ENOMEM);
857
858     tile->coord[0][0] = av_clip(tilex       * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
859     tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
860     tile->coord[1][0] = av_clip(tiley       * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
861     tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
862
863     for (compno = 0; compno < s->ncomponents; compno++) {
864         Jpeg2000Component *comp = tile->comp + compno;
865         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
866         Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
867         int ret; // global bandno
868
869         comp->coord_o[0][0] = tile->coord[0][0];
870         comp->coord_o[0][1] = tile->coord[0][1];
871         comp->coord_o[1][0] = tile->coord[1][0];
872         comp->coord_o[1][1] = tile->coord[1][1];
873         if (compno) {
874             comp->coord_o[0][0] /= s->cdx[compno];
875             comp->coord_o[0][1] /= s->cdx[compno];
876             comp->coord_o[1][0] /= s->cdy[compno];
877             comp->coord_o[1][1] /= s->cdy[compno];
878         }
879
880         comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
881         comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
882         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
883         comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
884
885         if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
886                                              s->cbps[compno], s->cdx[compno],
887                                              s->cdy[compno], s->avctx))
888             return ret;
889     }
890     return 0;
891 }
892
893 /* Read the number of coding passes. */
894 static int getnpasses(Jpeg2000DecoderContext *s)
895 {
896     int num;
897     if (!get_bits(s, 1))
898         return 1;
899     if (!get_bits(s, 1))
900         return 2;
901     if ((num = get_bits(s, 2)) != 3)
902         return num < 0 ? num : 3 + num;
903     if ((num = get_bits(s, 5)) != 31)
904         return num < 0 ? num : 6 + num;
905     num = get_bits(s, 7);
906     return num < 0 ? num : 37 + num;
907 }
908
909 static int getlblockinc(Jpeg2000DecoderContext *s)
910 {
911     int res = 0, ret;
912     while (ret = get_bits(s, 1)) {
913         if (ret < 0)
914             return ret;
915         res++;
916     }
917     return res;
918 }
919
920 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
921                                   Jpeg2000CodingStyle *codsty,
922                                   Jpeg2000ResLevel *rlevel, int precno,
923                                   int layno, uint8_t *expn, int numgbits)
924 {
925     int bandno, cblkno, ret, nb_code_blocks;
926     int cwsno;
927
928     if (layno < rlevel->band[0].prec[precno].decoded_layers)
929         return 0;
930     rlevel->band[0].prec[precno].decoded_layers = layno + 1;
931
932     if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
933         if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
934             s->g = tile->tile_part[++(*tp_index)].tpg;
935         }
936     }
937
938     if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
939         bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
940
941     if (!(ret = get_bits(s, 1))) {
942         jpeg2000_flush(s);
943         return 0;
944     } else if (ret < 0)
945         return ret;
946
947     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
948         Jpeg2000Band *band = rlevel->band + bandno;
949         Jpeg2000Prec *prec = band->prec + precno;
950
951         if (band->coord[0][0] == band->coord[0][1] ||
952             band->coord[1][0] == band->coord[1][1])
953             continue;
954         nb_code_blocks =  prec->nb_codeblocks_height *
955                           prec->nb_codeblocks_width;
956         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
957             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
958             int incl, newpasses, llen;
959             void *tmp;
960
961             if (cblk->npasses)
962                 incl = get_bits(s, 1);
963             else
964                 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
965             if (!incl)
966                 continue;
967             else if (incl < 0)
968                 return incl;
969
970             if (!cblk->npasses) {
971                 int v = expn[bandno] + numgbits - 1 -
972                         tag_tree_decode(s, prec->zerobits + cblkno, 100);
973                 if (v < 0 || v > 30) {
974                     av_log(s->avctx, AV_LOG_ERROR,
975                            "nonzerobits %d invalid or unsupported\n", v);
976                     return AVERROR_INVALIDDATA;
977                 }
978                 cblk->nonzerobits = v;
979             }
980             if ((newpasses = getnpasses(s)) < 0)
981                 return newpasses;
982             av_assert2(newpasses > 0);
983             if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
984                 avpriv_request_sample(s->avctx, "Too many passes");
985                 return AVERROR_PATCHWELCOME;
986             }
987             if ((llen = getlblockinc(s)) < 0)
988                 return llen;
989             if (cblk->lblock + llen + av_log2(newpasses) > 16) {
990                 avpriv_request_sample(s->avctx,
991                                       "Block with length beyond 16 bits");
992                 return AVERROR_PATCHWELCOME;
993             }
994
995             cblk->lblock += llen;
996
997             cblk->nb_lengthinc = 0;
998             cblk->nb_terminationsinc = 0;
999             av_free(cblk->lengthinc);
1000             cblk->lengthinc  = av_mallocz_array(newpasses    , sizeof(*cblk->lengthinc));
1001             if (!cblk->lengthinc)
1002                 return AVERROR(ENOMEM);
1003             tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1004             if (!tmp)
1005                 return AVERROR(ENOMEM);
1006             cblk->data_start = tmp;
1007             do {
1008                 int newpasses1 = 0;
1009
1010                 while (newpasses1 < newpasses) {
1011                     newpasses1 ++;
1012                     if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1013                         cblk->nb_terminationsinc ++;
1014                         break;
1015                     }
1016                 }
1017
1018                 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1019                     return ret;
1020                 if (ret > cblk->data_allocated) {
1021                     size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1022                     void *new = av_realloc(cblk->data, new_size);
1023                     if (new) {
1024                         cblk->data = new;
1025                         cblk->data_allocated = new_size;
1026                     }
1027                 }
1028                 if (ret > cblk->data_allocated) {
1029                     avpriv_request_sample(s->avctx,
1030                                         "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1031                                         cblk->data_allocated);
1032                     return AVERROR_PATCHWELCOME;
1033                 }
1034                 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1035                 cblk->npasses  += newpasses1;
1036                 newpasses -= newpasses1;
1037             } while(newpasses);
1038         }
1039     }
1040     jpeg2000_flush(s);
1041
1042     if (codsty->csty & JPEG2000_CSTY_EPH) {
1043         if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1044             bytestream2_skip(&s->g, 2);
1045         else
1046             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1047     }
1048
1049     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1050         Jpeg2000Band *band = rlevel->band + bandno;
1051         Jpeg2000Prec *prec = band->prec + precno;
1052
1053         nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1054         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1055             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1056             if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1057                 continue;
1058             for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1059                 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1060                     size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1061                     void *new = av_realloc(cblk->data, new_size);
1062                     if (new) {
1063                         cblk->data = new;
1064                         cblk->data_allocated = new_size;
1065                     }
1066                 }
1067                 if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1068                     || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1069                 ) {
1070                     av_log(s->avctx, AV_LOG_ERROR,
1071                         "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1072                         cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1073                     return AVERROR_INVALIDDATA;
1074                 }
1075
1076                 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1077                 cblk->length   += cblk->lengthinc[cwsno];
1078                 cblk->lengthinc[cwsno] = 0;
1079                 if (cblk->nb_terminationsinc) {
1080                     cblk->nb_terminationsinc--;
1081                     cblk->nb_terminations++;
1082                     cblk->data[cblk->length++] = 0xFF;
1083                     cblk->data[cblk->length++] = 0xFF;
1084                     cblk->data_start[cblk->nb_terminations] = cblk->length;
1085                 }
1086             }
1087             av_freep(&cblk->lengthinc);
1088         }
1089     }
1090     return 0;
1091 }
1092
1093 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1094                                              int RSpoc, int CSpoc,
1095                                              int LYEpoc, int REpoc, int CEpoc,
1096                                              int Ppoc, int *tp_index)
1097 {
1098     int ret = 0;
1099     int layno, reslevelno, compno, precno, ok_reslevel;
1100     int x, y;
1101     int step_x, step_y;
1102
1103     switch (Ppoc) {
1104     case JPEG2000_PGOD_RLCP:
1105         av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1106         ok_reslevel = 1;
1107         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1108             ok_reslevel = 0;
1109             for (layno = 0; layno < LYEpoc; layno++) {
1110                 for (compno = CSpoc; compno < CEpoc; compno++) {
1111                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1112                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1113                     if (reslevelno < codsty->nreslevels) {
1114                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1115                                                 reslevelno;
1116                         ok_reslevel = 1;
1117                         for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1118                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1119                                                               codsty, rlevel,
1120                                                               precno, layno,
1121                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1122                                                               qntsty->nguardbits)) < 0)
1123                                 return ret;
1124                     }
1125                 }
1126             }
1127         }
1128         break;
1129
1130     case JPEG2000_PGOD_LRCP:
1131         av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1132         for (layno = 0; layno < LYEpoc; layno++) {
1133             ok_reslevel = 1;
1134             for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1135                 ok_reslevel = 0;
1136                 for (compno = CSpoc; compno < CEpoc; compno++) {
1137                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1138                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1139                     if (reslevelno < codsty->nreslevels) {
1140                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1141                                                 reslevelno;
1142                         ok_reslevel = 1;
1143                         for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1144                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1145                                                               codsty, rlevel,
1146                                                               precno, layno,
1147                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1148                                                               qntsty->nguardbits)) < 0)
1149                                 return ret;
1150                     }
1151                 }
1152             }
1153         }
1154         break;
1155
1156     case JPEG2000_PGOD_CPRL:
1157         av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1158         for (compno = CSpoc; compno < CEpoc; compno++) {
1159             Jpeg2000Component *comp     = tile->comp + compno;
1160             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1161             Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1162             step_x = 32;
1163             step_y = 32;
1164
1165             if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1166                 continue;
1167
1168             for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1169                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1170                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1171                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1172                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1173             }
1174             if (step_x >= 31 || step_y >= 31){
1175                 avpriv_request_sample(s->avctx, "CPRL with large step");
1176                 return AVERROR_PATCHWELCOME;
1177             }
1178             step_x = 1<<step_x;
1179             step_y = 1<<step_y;
1180
1181             for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1182                 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1183                     for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1184                         unsigned prcx, prcy;
1185                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1186                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1187                         int xc = x / s->cdx[compno];
1188                         int yc = y / s->cdy[compno];
1189
1190                         if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1191                             continue;
1192
1193                         if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1194                             continue;
1195
1196                         // check if a precinct exists
1197                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1198                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1199                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1200                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1201
1202                         precno = prcx + rlevel->num_precincts_x * prcy;
1203
1204                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1205                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1206                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1207                             continue;
1208                         }
1209
1210                         for (layno = 0; layno < LYEpoc; layno++) {
1211                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1212                                                               precno, layno,
1213                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1214                                                               qntsty->nguardbits)) < 0)
1215                                 return ret;
1216                         }
1217                     }
1218                 }
1219             }
1220         }
1221         break;
1222
1223     case JPEG2000_PGOD_RPCL:
1224         av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1225         ok_reslevel = 1;
1226         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1227             ok_reslevel = 0;
1228             step_x = 30;
1229             step_y = 30;
1230             for (compno = CSpoc; compno < CEpoc; compno++) {
1231                 Jpeg2000Component *comp     = tile->comp + compno;
1232                 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1233
1234                 if (reslevelno < codsty->nreslevels) {
1235                     uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1236                     Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1237                     step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1238                     step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1239                 }
1240             }
1241             step_x = 1<<step_x;
1242             step_y = 1<<step_y;
1243
1244             for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1245                 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1246                     for (compno = CSpoc; compno < CEpoc; compno++) {
1247                         Jpeg2000Component *comp     = tile->comp + compno;
1248                         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1249                         Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1250                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1251                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1252                         unsigned prcx, prcy;
1253
1254                         int xc = x / s->cdx[compno];
1255                         int yc = y / s->cdy[compno];
1256
1257                         if (reslevelno >= codsty->nreslevels)
1258                             continue;
1259
1260                         if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1261                             continue;
1262
1263                         if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1264                             continue;
1265
1266                         // check if a precinct exists
1267                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1268                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1269                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1270                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1271
1272                         precno = prcx + rlevel->num_precincts_x * prcy;
1273
1274                         ok_reslevel = 1;
1275                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1276                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1277                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1278                             continue;
1279                         }
1280
1281                             for (layno = 0; layno < LYEpoc; layno++) {
1282                                 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1283                                                                 codsty, rlevel,
1284                                                                 precno, layno,
1285                                                                 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1286                                                                 qntsty->nguardbits)) < 0)
1287                                     return ret;
1288                             }
1289                     }
1290                 }
1291             }
1292         }
1293         break;
1294
1295     case JPEG2000_PGOD_PCRL:
1296         av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1297         step_x = 32;
1298         step_y = 32;
1299         for (compno = CSpoc; compno < CEpoc; compno++) {
1300             Jpeg2000Component *comp     = tile->comp + compno;
1301             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1302
1303             for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1304                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1305                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1306                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1307                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1308             }
1309         }
1310         if (step_x >= 31 || step_y >= 31){
1311             avpriv_request_sample(s->avctx, "PCRL with large step");
1312             return AVERROR_PATCHWELCOME;
1313         }
1314         step_x = 1<<step_x;
1315         step_y = 1<<step_y;
1316
1317         for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1318             for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1319                 for (compno = CSpoc; compno < CEpoc; compno++) {
1320                     Jpeg2000Component *comp     = tile->comp + compno;
1321                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1322                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1323                     int xc = x / s->cdx[compno];
1324                     int yc = y / s->cdy[compno];
1325
1326                     for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1327                         unsigned prcx, prcy;
1328                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1329                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1330
1331                         if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1332                             continue;
1333
1334                         if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1335                             continue;
1336
1337                         // check if a precinct exists
1338                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1339                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1340                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1341                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1342
1343                         precno = prcx + rlevel->num_precincts_x * prcy;
1344
1345                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1346                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1347                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1348                             continue;
1349                         }
1350
1351                         for (layno = 0; layno < LYEpoc; layno++) {
1352                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1353                                                               precno, layno,
1354                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1355                                                               qntsty->nguardbits)) < 0)
1356                                 return ret;
1357                         }
1358                     }
1359                 }
1360             }
1361         }
1362         break;
1363
1364     default:
1365         break;
1366     }
1367
1368     return ret;
1369 }
1370
1371 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1372 {
1373     int ret = AVERROR_BUG;
1374     int i;
1375     int tp_index = 0;
1376
1377     s->bit_index = 8;
1378     if (tile->poc.nb_poc) {
1379         for (i=0; i<tile->poc.nb_poc; i++) {
1380             Jpeg2000POCEntry *e = &tile->poc.poc[i];
1381             ret = jpeg2000_decode_packets_po_iteration(s, tile,
1382                 e->RSpoc, e->CSpoc,
1383                 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1384                 e->REpoc,
1385                 FFMIN(e->CEpoc, s->ncomponents),
1386                 e->Ppoc, &tp_index
1387                 );
1388             if (ret < 0)
1389                 return ret;
1390         }
1391     } else {
1392         ret = jpeg2000_decode_packets_po_iteration(s, tile,
1393             0, 0,
1394             tile->codsty[0].nlayers,
1395             33,
1396             s->ncomponents,
1397             tile->codsty[0].prog_order,
1398             &tp_index
1399         );
1400     }
1401     /* EOC marker reached */
1402     bytestream2_skip(&s->g, 2);
1403
1404     return ret;
1405 }
1406
1407 /* TIER-1 routines */
1408 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1409                            int bpno, int bandno,
1410                            int vert_causal_ctx_csty_symbol)
1411 {
1412     int mask = 3 << (bpno - 1), y0, x, y;
1413
1414     for (y0 = 0; y0 < height; y0 += 4)
1415         for (x = 0; x < width; x++)
1416             for (y = y0; y < height && y < y0 + 4; y++) {
1417                 int flags_mask = -1;
1418                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1419                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1420                 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1421                 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1422                     if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1423                         int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1424                         if (t1->mqc.raw)
1425                              t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1426                         else
1427                              t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1428                                                -mask : mask;
1429
1430                         ff_jpeg2000_set_significance(t1, x, y,
1431                                                      t1->data[(y) * t1->stride + x] < 0);
1432                     }
1433                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1434                 }
1435             }
1436 }
1437
1438 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1439                            int bpno, int vert_causal_ctx_csty_symbol)
1440 {
1441     int phalf, nhalf;
1442     int y0, x, y;
1443
1444     phalf = 1 << (bpno - 1);
1445     nhalf = -phalf;
1446
1447     for (y0 = 0; y0 < height; y0 += 4)
1448         for (x = 0; x < width; x++)
1449             for (y = y0; y < height && y < y0 + 4; y++)
1450                 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1451                     int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1452                         ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1453                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1454                     int r     = ff_mqc_decode(&t1->mqc,
1455                                               t1->mqc.cx_states + ctxno)
1456                                 ? phalf : nhalf;
1457                     t1->data[(y) * t1->stride + x]          += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1458                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1459                 }
1460 }
1461
1462 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1463                            int width, int height, int bpno, int bandno,
1464                            int seg_symbols, int vert_causal_ctx_csty_symbol)
1465 {
1466     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1467
1468     for (y0 = 0; y0 < height; y0 += 4) {
1469         for (x = 0; x < width; x++) {
1470             int flags_mask = -1;
1471             if (vert_causal_ctx_csty_symbol)
1472                 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1473             if (y0 + 3 < height &&
1474                 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1475                   (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1476                   (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1477                   (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1478                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1479                     continue;
1480                 runlen = ff_mqc_decode(&t1->mqc,
1481                                        t1->mqc.cx_states + MQC_CX_UNI);
1482                 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1483                                                        t1->mqc.cx_states +
1484                                                        MQC_CX_UNI);
1485                 dec = 1;
1486             } else {
1487                 runlen = 0;
1488                 dec    = 0;
1489             }
1490
1491             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1492                 int flags_mask = -1;
1493                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1494                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1495                 if (!dec) {
1496                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1497                         dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1498                                                                                              bandno));
1499                     }
1500                 }
1501                 if (dec) {
1502                     int xorbit;
1503                     int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1504                                                         &xorbit);
1505                     t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1506                                                     t1->mqc.cx_states + ctxno) ^
1507                                       xorbit)
1508                                      ? -mask : mask;
1509                     ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1510                 }
1511                 dec = 0;
1512                 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1513             }
1514         }
1515     }
1516     if (seg_symbols) {
1517         int val;
1518         val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1519         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1520         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1521         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1522         if (val != 0xa)
1523             av_log(s->avctx, AV_LOG_ERROR,
1524                    "Segmentation symbol value incorrect\n");
1525     }
1526 }
1527
1528 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1529                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1530                        int width, int height, int bandpos)
1531 {
1532     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
1533     int pass_cnt = 0;
1534     int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1535     int term_cnt = 0;
1536     int coder_type;
1537
1538     av_assert0(width <= 1024U && height <= 1024U);
1539     av_assert0(width*height <= 4096);
1540
1541     memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1542
1543     /* If code-block contains no compressed data: nothing to do. */
1544     if (!cblk->length)
1545         return 0;
1546
1547     memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1548
1549     cblk->data[cblk->length] = 0xff;
1550     cblk->data[cblk->length+1] = 0xff;
1551     ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1552
1553     while (passno--) {
1554         if (bpno < 0) {
1555             av_log(s->avctx, AV_LOG_ERROR, "bpno became negative\n");
1556             return AVERROR_INVALIDDATA;
1557         }
1558         switch(pass_t) {
1559         case 0:
1560             decode_sigpass(t1, width, height, bpno + 1, bandpos,
1561                            vert_causal_ctx_csty_symbol);
1562             break;
1563         case 1:
1564             decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1565             break;
1566         case 2:
1567             av_assert2(!t1->mqc.raw);
1568             decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1569                            codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1570                            vert_causal_ctx_csty_symbol);
1571             break;
1572         }
1573         if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1574             ff_mqc_init_contexts(&t1->mqc);
1575
1576         if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1577             if (term_cnt >= cblk->nb_terminations) {
1578                 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1579                 return AVERROR_INVALIDDATA;
1580             }
1581             if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1582                 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1583                     cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1584                     pass_cnt, cblk->npasses);
1585             }
1586
1587             ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1588         }
1589
1590         pass_t++;
1591         if (pass_t == 3) {
1592             bpno--;
1593             pass_t = 0;
1594         }
1595         pass_cnt ++;
1596     }
1597
1598     if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1599         av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1600                cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1601     }
1602
1603     return 1;
1604 }
1605
1606 /* TODO: Verify dequantization for lossless case
1607  * comp->data can be float or int
1608  * band->stepsize can be float or int
1609  * depending on the type of DWT transformation.
1610  * see ISO/IEC 15444-1:2002 A.6.1 */
1611
1612 /* Float dequantization of a codeblock.*/
1613 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1614                                  Jpeg2000Component *comp,
1615                                  Jpeg2000T1Context *t1, Jpeg2000Band *band)
1616 {
1617     int i, j;
1618     int w = cblk->coord[0][1] - cblk->coord[0][0];
1619     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1620         float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1621         int *src = t1->data + j*t1->stride;
1622         for (i = 0; i < w; ++i)
1623             datap[i] = src[i] * band->f_stepsize;
1624     }
1625 }
1626
1627 /* Integer dequantization of a codeblock.*/
1628 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1629                                Jpeg2000Component *comp,
1630                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1631 {
1632     int i, j;
1633     int w = cblk->coord[0][1] - cblk->coord[0][0];
1634     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1635         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1636         int *src = t1->data + j*t1->stride;
1637         if (band->i_stepsize == 32768) {
1638             for (i = 0; i < w; ++i)
1639                 datap[i] = src[i] / 2;
1640         } else {
1641             // This should be VERY uncommon
1642             for (i = 0; i < w; ++i)
1643                 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1644         }
1645     }
1646 }
1647
1648 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1649                                Jpeg2000Component *comp,
1650                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1651 {
1652     int i, j;
1653     int w = cblk->coord[0][1] - cblk->coord[0][0];
1654     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1655         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1656         int *src = t1->data + j*t1->stride;
1657         for (i = 0; i < w; ++i)
1658             datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1659     }
1660 }
1661
1662 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1663 {
1664     int i, csize = 1;
1665     void *src[3];
1666
1667     for (i = 1; i < 3; i++) {
1668         if (tile->codsty[0].transform != tile->codsty[i].transform) {
1669             av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1670             return;
1671         }
1672         if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1673             av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1674             return;
1675         }
1676     }
1677
1678     for (i = 0; i < 3; i++)
1679         if (tile->codsty[0].transform == FF_DWT97)
1680             src[i] = tile->comp[i].f_data;
1681         else
1682             src[i] = tile->comp[i].i_data;
1683
1684     for (i = 0; i < 2; i++)
1685         csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1686
1687     s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1688 }
1689
1690 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1691 {
1692     Jpeg2000T1Context t1;
1693
1694     int compno, reslevelno, bandno;
1695
1696     /* Loop on tile components */
1697     for (compno = 0; compno < s->ncomponents; compno++) {
1698         Jpeg2000Component *comp     = tile->comp + compno;
1699         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1700         int coded = 0;
1701
1702         t1.stride = (1<<codsty->log2_cblk_width) + 2;
1703
1704         /* Loop on resolution levels */
1705         for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1706             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1707             /* Loop on bands */
1708             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1709                 int nb_precincts, precno;
1710                 Jpeg2000Band *band = rlevel->band + bandno;
1711                 int cblkno = 0, bandpos;
1712
1713                 bandpos = bandno + (reslevelno > 0);
1714
1715                 if (band->coord[0][0] == band->coord[0][1] ||
1716                     band->coord[1][0] == band->coord[1][1])
1717                     continue;
1718
1719                 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1720                 /* Loop on precincts */
1721                 for (precno = 0; precno < nb_precincts; precno++) {
1722                     Jpeg2000Prec *prec = band->prec + precno;
1723
1724                     /* Loop on codeblocks */
1725                     for (cblkno = 0;
1726                          cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1727                          cblkno++) {
1728                         int x, y;
1729                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1730                         int ret = decode_cblk(s, codsty, &t1, cblk,
1731                                     cblk->coord[0][1] - cblk->coord[0][0],
1732                                     cblk->coord[1][1] - cblk->coord[1][0],
1733                                     bandpos);
1734                         if (ret)
1735                             coded = 1;
1736                         else
1737                             continue;
1738                         x = cblk->coord[0][0] - band->coord[0][0];
1739                         y = cblk->coord[1][0] - band->coord[1][0];
1740
1741                         if (codsty->transform == FF_DWT97)
1742                             dequantization_float(x, y, cblk, comp, &t1, band);
1743                         else if (codsty->transform == FF_DWT97_INT)
1744                             dequantization_int_97(x, y, cblk, comp, &t1, band);
1745                         else
1746                             dequantization_int(x, y, cblk, comp, &t1, band);
1747                    } /* end cblk */
1748                 } /*end prec */
1749             } /* end band */
1750         } /* end reslevel */
1751
1752         /* inverse DWT */
1753         if (coded)
1754             ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1755
1756     } /*end comp */
1757 }
1758
1759 #define WRITE_FRAME(D, PIXEL)                                                                     \
1760     static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,         \
1761                                          AVFrame * picture, int precision)                        \
1762     {                                                                                             \
1763         const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);               \
1764         int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);                              \
1765         int pixelsize = planar ? 1 : pixdesc->nb_components;                                      \
1766                                                                                                   \
1767         int compno;                                                                               \
1768         int x, y;                                                                                 \
1769                                                                                                   \
1770         for (compno = 0; compno < s->ncomponents; compno++) {                                     \
1771             Jpeg2000Component *comp     = tile->comp + compno;                                    \
1772             Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \
1773             PIXEL *line;                                                                          \
1774             float *datap     = comp->f_data;                                                      \
1775             int32_t *i_datap = comp->i_data;                                                      \
1776             int cbps         = s->cbps[compno];                                                   \
1777             int w            = tile->comp[compno].coord[0][1] - s->image_offset_x;                \
1778             int plane        = 0;                                                                 \
1779                                                                                                   \
1780             if (planar)                                                                           \
1781                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);                 \
1782                                                                                                   \
1783             y    = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];           \
1784             line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1785             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) {                 \
1786                 PIXEL *dst;                                                                       \
1787                                                                                                   \
1788                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];        \
1789                 dst = line + x * pixelsize + compno*!planar;                                      \
1790                                                                                                   \
1791                 if (codsty->transform == FF_DWT97) {                                              \
1792                     for (; x < w; x++) {                                                          \
1793                         int val = lrintf(*datap) + (1 << (cbps - 1));                             \
1794                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1795                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1796                         *dst = val << (precision - cbps);                                         \
1797                         datap++;                                                                  \
1798                         dst += pixelsize;                                                         \
1799                     }                                                                             \
1800                 } else {                                                                          \
1801                     for (; x < w; x++) {                                                          \
1802                         int val = *i_datap + (1 << (cbps - 1));                                   \
1803                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1804                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1805                         *dst = val << (precision - cbps);                                         \
1806                         i_datap++;                                                                \
1807                         dst += pixelsize;                                                         \
1808                     }                                                                             \
1809                 }                                                                                 \
1810                 line += picture->linesize[plane] / sizeof(PIXEL);                                 \
1811             }                                                                                     \
1812         }                                                                                         \
1813                                                                                                   \
1814     }
1815
1816 WRITE_FRAME(8, uint8_t)
1817 WRITE_FRAME(16, uint16_t)
1818
1819 #undef WRITE_FRAME
1820
1821 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1822                                 int jobnr, int threadnr)
1823 {
1824     Jpeg2000DecoderContext *s = avctx->priv_data;
1825     AVFrame *picture = td;
1826     Jpeg2000Tile *tile = s->tile + jobnr;
1827     int x;
1828
1829     tile_codeblocks(s, tile);
1830
1831     /* inverse MCT transformation */
1832     if (tile->codsty[0].mct)
1833         mct_decode(s, tile);
1834
1835     for (x = 0; x < s->ncomponents; x++) {
1836         if (s->cdef[x] < 0) {
1837             for (x = 0; x < s->ncomponents; x++) {
1838                 s->cdef[x] = x + 1;
1839             }
1840             if ((s->ncomponents & 1) == 0)
1841                 s->cdef[s->ncomponents-1] = 0;
1842             break;
1843         }
1844     }
1845
1846     if (s->precision <= 8) {
1847         write_frame_8(s, tile, picture, 8);
1848     } else {
1849         int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1850                         picture->format == AV_PIX_FMT_RGB48 ||
1851                         picture->format == AV_PIX_FMT_RGBA64 ||
1852                         picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1853
1854         write_frame_16(s, tile, picture, precision);
1855     }
1856
1857     return 0;
1858 }
1859
1860 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1861 {
1862     int tileno, compno;
1863     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1864         if (s->tile[tileno].comp) {
1865             for (compno = 0; compno < s->ncomponents; compno++) {
1866                 Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
1867                 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1868
1869                 ff_jpeg2000_cleanup(comp, codsty);
1870             }
1871             av_freep(&s->tile[tileno].comp);
1872         }
1873     }
1874     av_freep(&s->tile);
1875     memset(s->codsty, 0, sizeof(s->codsty));
1876     memset(s->qntsty, 0, sizeof(s->qntsty));
1877     memset(s->properties, 0, sizeof(s->properties));
1878     memset(&s->poc  , 0, sizeof(s->poc));
1879     s->numXtiles = s->numYtiles = 0;
1880     s->ncomponents = 0;
1881 }
1882
1883 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1884 {
1885     Jpeg2000CodingStyle *codsty = s->codsty;
1886     Jpeg2000QuantStyle *qntsty  = s->qntsty;
1887     Jpeg2000POC         *poc    = &s->poc;
1888     uint8_t *properties         = s->properties;
1889
1890     for (;;) {
1891         int len, ret = 0;
1892         uint16_t marker;
1893         int oldpos;
1894
1895         if (bytestream2_get_bytes_left(&s->g) < 2) {
1896             av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1897             break;
1898         }
1899
1900         marker = bytestream2_get_be16u(&s->g);
1901         oldpos = bytestream2_tell(&s->g);
1902
1903         if (marker == JPEG2000_SOD) {
1904             Jpeg2000Tile *tile;
1905             Jpeg2000TilePart *tp;
1906
1907             if (!s->tile) {
1908                 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1909                 return AVERROR_INVALIDDATA;
1910             }
1911             if (s->curtileno < 0) {
1912                 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1913                 return AVERROR_INVALIDDATA;
1914             }
1915
1916             tile = s->tile + s->curtileno;
1917             tp = tile->tile_part + tile->tp_idx;
1918             if (tp->tp_end < s->g.buffer) {
1919                 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1920                 return AVERROR_INVALIDDATA;
1921             }
1922             bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1923             bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1924
1925             continue;
1926         }
1927         if (marker == JPEG2000_EOC)
1928             break;
1929
1930         len = bytestream2_get_be16(&s->g);
1931         if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
1932             av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
1933             return AVERROR_INVALIDDATA;
1934         }
1935
1936         switch (marker) {
1937         case JPEG2000_SIZ:
1938             if (s->ncomponents) {
1939                 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
1940                 return AVERROR_INVALIDDATA;
1941             }
1942             ret = get_siz(s);
1943             if (!s->tile)
1944                 s->numXtiles = s->numYtiles = 0;
1945             break;
1946         case JPEG2000_COC:
1947             ret = get_coc(s, codsty, properties);
1948             break;
1949         case JPEG2000_COD:
1950             ret = get_cod(s, codsty, properties);
1951             break;
1952         case JPEG2000_QCC:
1953             ret = get_qcc(s, len, qntsty, properties);
1954             break;
1955         case JPEG2000_QCD:
1956             ret = get_qcd(s, len, qntsty, properties);
1957             break;
1958         case JPEG2000_POC:
1959             ret = get_poc(s, len, poc);
1960             break;
1961         case JPEG2000_SOT:
1962             if (!(ret = get_sot(s, len))) {
1963                 av_assert1(s->curtileno >= 0);
1964                 codsty = s->tile[s->curtileno].codsty;
1965                 qntsty = s->tile[s->curtileno].qntsty;
1966                 poc    = &s->tile[s->curtileno].poc;
1967                 properties = s->tile[s->curtileno].properties;
1968             }
1969             break;
1970         case JPEG2000_PLM:
1971             // the PLM marker is ignored
1972         case JPEG2000_COM:
1973             // the comment is ignored
1974             bytestream2_skip(&s->g, len - 2);
1975             break;
1976         case JPEG2000_TLM:
1977             // Tile-part lengths
1978             ret = get_tlm(s, len);
1979             break;
1980         case JPEG2000_PLT:
1981             // Packet length, tile-part header
1982             ret = get_plt(s, len);
1983             break;
1984         default:
1985             av_log(s->avctx, AV_LOG_ERROR,
1986                    "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1987                    marker, bytestream2_tell(&s->g) - 4);
1988             bytestream2_skip(&s->g, len - 2);
1989             break;
1990         }
1991         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1992             av_log(s->avctx, AV_LOG_ERROR,
1993                    "error during processing marker segment %.4"PRIx16"\n",
1994                    marker);
1995             return ret ? ret : -1;
1996         }
1997     }
1998     return 0;
1999 }
2000
2001 /* Read bit stream packets --> T2 operation. */
2002 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2003 {
2004     int ret = 0;
2005     int tileno;
2006
2007     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2008         Jpeg2000Tile *tile = s->tile + tileno;
2009
2010         if ((ret = init_tile(s, tileno)) < 0)
2011             return ret;
2012
2013         s->g = tile->tile_part[0].tpg;
2014         if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2015             return ret;
2016     }
2017
2018     return 0;
2019 }
2020
2021 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2022 {
2023     uint32_t atom_size, atom, atom_end;
2024     int search_range = 10;
2025
2026     while (search_range
2027            &&
2028            bytestream2_get_bytes_left(&s->g) >= 8) {
2029         atom_size = bytestream2_get_be32u(&s->g);
2030         atom      = bytestream2_get_be32u(&s->g);
2031         if (atom_size == 1) {
2032             if (bytestream2_get_be32u(&s->g)) {
2033                 avpriv_request_sample(s->avctx, "Huge atom");
2034                 return 0;
2035             }
2036             atom_size = bytestream2_get_be32u(&s->g);
2037             atom_end  = bytestream2_tell(&s->g) + atom_size - 16;
2038         } else {
2039             atom_end  = bytestream2_tell(&s->g) + atom_size -  8;
2040         }
2041
2042         if (atom == JP2_CODESTREAM)
2043             return 1;
2044
2045         if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2046             return 0;
2047
2048         if (atom == JP2_HEADER &&
2049                    atom_size >= 16) {
2050             uint32_t atom2_size, atom2, atom2_end;
2051             do {
2052                 atom2_size = bytestream2_get_be32u(&s->g);
2053                 atom2      = bytestream2_get_be32u(&s->g);
2054                 atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
2055                 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2056                     break;
2057                 atom2_size -= 8;
2058                 if (atom2 == JP2_CODESTREAM) {
2059                     return 1;
2060                 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2061                     int method = bytestream2_get_byteu(&s->g);
2062                     bytestream2_skipu(&s->g, 2);
2063                     if (method == 1) {
2064                         s->colour_space = bytestream2_get_be32u(&s->g);
2065                     }
2066                 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2067                     int i, size, colour_count, colour_channels, colour_depth[3];
2068                     colour_count = bytestream2_get_be16u(&s->g);
2069                     colour_channels = bytestream2_get_byteu(&s->g);
2070                     // FIXME: Do not ignore channel_sign
2071                     colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2072                     colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2073                     colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2074                     size = (colour_depth[0] + 7 >> 3) * colour_count +
2075                            (colour_depth[1] + 7 >> 3) * colour_count +
2076                            (colour_depth[2] + 7 >> 3) * colour_count;
2077                     if (colour_count > AVPALETTE_COUNT ||
2078                         colour_channels != 3 ||
2079                         colour_depth[0] > 16 ||
2080                         colour_depth[1] > 16 ||
2081                         colour_depth[2] > 16 ||
2082                         atom2_size < size) {
2083                         avpriv_request_sample(s->avctx, "Unknown palette");
2084                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2085                         continue;
2086                     }
2087                     s->pal8 = 1;
2088                     for (i = 0; i < colour_count; i++) {
2089                         uint32_t r, g, b;
2090                         if (colour_depth[0] <= 8) {
2091                             r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2092                             r |= r >> colour_depth[0];
2093                         } else {
2094                             r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2095                         }
2096                         if (colour_depth[1] <= 8) {
2097                             g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2098                             g |= g >> colour_depth[1];
2099                         } else {
2100                             g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2101                         }
2102                         if (colour_depth[2] <= 8) {
2103                             b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2104                             b |= b >> colour_depth[2];
2105                         } else {
2106                             b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2107                         }
2108                         s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2109                     }
2110                 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2111                     int n = bytestream2_get_be16u(&s->g);
2112                     for (; n>0; n--) {
2113                         int cn   = bytestream2_get_be16(&s->g);
2114                         int av_unused typ  = bytestream2_get_be16(&s->g);
2115                         int asoc = bytestream2_get_be16(&s->g);
2116                         if (cn < 4 && asoc < 4)
2117                             s->cdef[cn] = asoc;
2118                     }
2119                 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2120                     int64_t vnum, vden, hnum, hden, vexp, hexp;
2121                     uint32_t resx;
2122                     bytestream2_skip(&s->g, 4);
2123                     resx = bytestream2_get_be32u(&s->g);
2124                     if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2125                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2126                         continue;
2127                     }
2128                     vnum = bytestream2_get_be16u(&s->g);
2129                     vden = bytestream2_get_be16u(&s->g);
2130                     hnum = bytestream2_get_be16u(&s->g);
2131                     hden = bytestream2_get_be16u(&s->g);
2132                     vexp = bytestream2_get_byteu(&s->g);
2133                     hexp = bytestream2_get_byteu(&s->g);
2134                     if (!vnum || !vden || !hnum || !hden) {
2135                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2136                         av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2137                         continue;
2138                     }
2139                     if (vexp > hexp) {
2140                         vexp -= hexp;
2141                         hexp = 0;
2142                     } else {
2143                         hexp -= vexp;
2144                         vexp = 0;
2145                     }
2146                     if (   INT64_MAX / (hnum * vden) > pow(10, hexp)
2147                         && INT64_MAX / (vnum * hden) > pow(10, vexp))
2148                         av_reduce(&s->sar.den, &s->sar.num,
2149                                   hnum * vden * pow(10, hexp),
2150                                   vnum * hden * pow(10, vexp),
2151                                   INT32_MAX);
2152                 }
2153                 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2154             } while (atom_end - atom2_end >= 8);
2155         } else {
2156             search_range--;
2157         }
2158         bytestream2_seek(&s->g, atom_end, SEEK_SET);
2159     }
2160
2161     return 0;
2162 }
2163
2164 static av_cold void jpeg2000_init_static_data(void)
2165 {
2166     ff_jpeg2000_init_tier1_luts();
2167     ff_mqc_init_context_tables();
2168 }
2169
2170 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2171 {
2172     static AVOnce init_static_once = AV_ONCE_INIT;
2173     Jpeg2000DecoderContext *s = avctx->priv_data;
2174
2175     ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2176     ff_jpeg2000dsp_init(&s->dsp);
2177
2178     return 0;
2179 }
2180
2181 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2182                                  int *got_frame, AVPacket *avpkt)
2183 {
2184     Jpeg2000DecoderContext *s = avctx->priv_data;
2185     ThreadFrame frame = { .f = data };
2186     AVFrame *picture = data;
2187     int ret;
2188
2189     s->avctx     = avctx;
2190     bytestream2_init(&s->g, avpkt->data, avpkt->size);
2191     s->curtileno = -1;
2192     memset(s->cdef, -1, sizeof(s->cdef));
2193
2194     if (bytestream2_get_bytes_left(&s->g) < 2) {
2195         ret = AVERROR_INVALIDDATA;
2196         goto end;
2197     }
2198
2199     // check if the image is in jp2 format
2200     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2201        (bytestream2_get_be32u(&s->g) == 12) &&
2202        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2203        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2204         if (!jp2_find_codestream(s)) {
2205             av_log(avctx, AV_LOG_ERROR,
2206                    "Could not find Jpeg2000 codestream atom.\n");
2207             ret = AVERROR_INVALIDDATA;
2208             goto end;
2209         }
2210     } else {
2211         bytestream2_seek(&s->g, 0, SEEK_SET);
2212     }
2213
2214     while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2215         bytestream2_skip(&s->g, 1);
2216
2217     if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2218         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2219         ret = AVERROR_INVALIDDATA;
2220         goto end;
2221     }
2222     if (ret = jpeg2000_read_main_headers(s))
2223         goto end;
2224
2225     /* get picture buffer */
2226     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2227         goto end;
2228     picture->pict_type = AV_PICTURE_TYPE_I;
2229     picture->key_frame = 1;
2230
2231     if (ret = jpeg2000_read_bitstream_packets(s))
2232         goto end;
2233
2234     avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2235
2236     jpeg2000_dec_cleanup(s);
2237
2238     *got_frame = 1;
2239
2240     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2241         memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2242     if (s->sar.num && s->sar.den)
2243         avctx->sample_aspect_ratio = s->sar;
2244     s->sar.num = s->sar.den = 0;
2245
2246     return bytestream2_tell(&s->g);
2247
2248 end:
2249     jpeg2000_dec_cleanup(s);
2250     return ret;
2251 }
2252
2253 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2254 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2255
2256 static const AVOption options[] = {
2257     { "lowres",  "Lower the decoding resolution by a power of two",
2258         OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2259     { NULL },
2260 };
2261
2262 static const AVClass jpeg2000_class = {
2263     .class_name = "jpeg2000",
2264     .item_name  = av_default_item_name,
2265     .option     = options,
2266     .version    = LIBAVUTIL_VERSION_INT,
2267 };
2268
2269 AVCodec ff_jpeg2000_decoder = {
2270     .name             = "jpeg2000",
2271     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2272     .type             = AVMEDIA_TYPE_VIDEO,
2273     .id               = AV_CODEC_ID_JPEG2000,
2274     .capabilities     = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2275     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
2276     .init             = jpeg2000_decode_init,
2277     .decode           = jpeg2000_decode_frame,
2278     .priv_class       = &jpeg2000_class,
2279     .max_lowres       = 5,
2280     .profiles         = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)
2281 };