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