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