OSDN Git Service

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