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