]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeg2000dec.c
avcodec/jpeg2000dec: iterate over positions with the special cases from jpeg2000
[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
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             int maxlogstep_x = 0;
1016             int maxlogstep_y = 0;
1017             int start_x, start_y;
1018             step_x = 32;
1019             step_y = 32;
1020
1021             for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1022                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1023                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1024                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1025                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1026                 maxlogstep_x = FFMAX(maxlogstep_x, rlevel->log2_prec_width  + reducedresno);
1027                 maxlogstep_y = FFMAX(maxlogstep_y, rlevel->log2_prec_height + reducedresno);
1028             }
1029             step_x = 1<<step_x;
1030             step_y = 1<<step_y;
1031
1032             start_y = comp->coord_o[1][0] >> maxlogstep_y << maxlogstep_y;
1033             start_x = comp->coord_o[0][0] >> maxlogstep_x << maxlogstep_x;
1034             for (y = start_y; y < comp->coord_o[1][1]; y += step_y) {
1035                 for (x = start_x; x < comp->coord_o[0][1]; x += step_x) {
1036                     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1037                         unsigned prcx, prcy;
1038                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1039                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1040
1041                         if (y % (1 << (rlevel->log2_prec_height + reducedresno)))
1042                             continue;
1043
1044                         if (x % (1 << (rlevel->log2_prec_width + reducedresno)))
1045                             continue;
1046
1047                         // check if a precinct exists
1048                         prcx   = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
1049                         prcy   = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
1050                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1051                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1052
1053                         precno = prcx + rlevel->num_precincts_x * prcy;
1054
1055                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1056                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1057                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1058                             continue;
1059                         }
1060
1061                         for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
1062                             if ((ret = jpeg2000_decode_packet(s, tile, &tp_index, codsty, rlevel,
1063                                                               precno, layno,
1064                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1065                                                               qntsty->nguardbits)) < 0)
1066                                 return ret;
1067                         }
1068                     }
1069                 }
1070             }
1071         }
1072         break;
1073
1074     case JPEG2000_PGOD_RPCL:
1075         av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1076         ok_reslevel = 1;
1077         for (reslevelno = 0; ok_reslevel; reslevelno++) {
1078             ok_reslevel = 0;
1079             step_x = 30;
1080             step_y = 30;
1081             for (compno = 0; compno < s->ncomponents; compno++) {
1082                 Jpeg2000Component *comp     = tile->comp + compno;
1083                 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1084
1085                 if (reslevelno < codsty->nreslevels) {
1086                     uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1087                     Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1088                     step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1089                     step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1090                 }
1091             }
1092             step_x = 1<<step_x;
1093             step_y = 1<<step_y;
1094
1095             //FIXME we could iterate over less than the whole image
1096             for (y = 0; y < s->height; y += step_y) {
1097                 for (x = 0; x < s->width; x += step_x) {
1098                     for (compno = 0; compno < s->ncomponents; compno++) {
1099                         Jpeg2000Component *comp     = tile->comp + compno;
1100                         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1101                         Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1102                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1103                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1104                         unsigned prcx, prcy;
1105
1106                         int xc = x / s->cdx[compno];
1107                         int yc = y / s->cdy[compno];
1108
1109                         if (reslevelno >= codsty->nreslevels)
1110                             continue;
1111
1112                         if (yc % (1 << (rlevel->log2_prec_height + reducedresno)))
1113                             continue;
1114
1115                         if (xc % (1 << (rlevel->log2_prec_width + reducedresno)))
1116                             continue;
1117
1118                         // check if a precinct exists
1119                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1120                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1121                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1122                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1123
1124                         precno = prcx + rlevel->num_precincts_x * prcy;
1125
1126                         ok_reslevel = 1;
1127                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1128                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1129                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1130                             continue;
1131                         }
1132
1133                             for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
1134                                 if ((ret = jpeg2000_decode_packet(s, tile, &tp_index,
1135                                                                 codsty, rlevel,
1136                                                                 precno, layno,
1137                                                                 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1138                                                                 qntsty->nguardbits)) < 0)
1139                                     return ret;
1140                             }
1141                     }
1142                 }
1143             }
1144         }
1145         break;
1146
1147     case JPEG2000_PGOD_PCRL:
1148         av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL");
1149         step_x = 32;
1150         step_y = 32;
1151         for (compno = 0; compno < s->ncomponents; compno++) {
1152             Jpeg2000Component *comp     = tile->comp + compno;
1153             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1154             Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1155
1156             for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1157                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1158                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1159                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1160                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1161             }
1162         }
1163         step_x = 1<<step_x;
1164         step_y = 1<<step_y;
1165
1166         for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1167             for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1168                 for (compno = 0; compno < s->ncomponents; compno++) {
1169                     Jpeg2000Component *comp     = tile->comp + compno;
1170                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1171                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1172                     int xc = x / s->cdx[compno];
1173                     int yc = y / s->cdy[compno];
1174
1175                     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1176                         unsigned prcx, prcy;
1177                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1178                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1179
1180                         if (yc % (1 << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1181                             continue;
1182
1183                         if (xc % (1 << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1184                             continue;
1185
1186                         // check if a precinct exists
1187                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1188                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1189                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1190                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1191
1192                         precno = prcx + rlevel->num_precincts_x * prcy;
1193
1194                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1195                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1196                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1197                             continue;
1198                         }
1199
1200                         for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
1201                             if ((ret = jpeg2000_decode_packet(s, tile, &tp_index, codsty, rlevel,
1202                                                               precno, layno,
1203                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1204                                                               qntsty->nguardbits)) < 0)
1205                                 return ret;
1206                         }
1207                     }
1208                 }
1209             }
1210         }
1211         break;
1212
1213     default:
1214         break;
1215     }
1216
1217     /* EOC marker reached */
1218     bytestream2_skip(&s->g, 2);
1219
1220     return ret;
1221 }
1222
1223 /* TIER-1 routines */
1224 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1225                            int bpno, int bandno,
1226                            int vert_causal_ctx_csty_symbol)
1227 {
1228     int mask = 3 << (bpno - 1), y0, x, y;
1229
1230     for (y0 = 0; y0 < height; y0 += 4)
1231         for (x = 0; x < width; x++)
1232             for (y = y0; y < height && y < y0 + 4; y++) {
1233                 int flags_mask = -1;
1234                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1235                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1236                 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1237                 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1238                     if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1239                         int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1240                         if (t1->mqc.raw)
1241                              t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1242                         else
1243                              t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1244                                                -mask : mask;
1245
1246                         ff_jpeg2000_set_significance(t1, x, y,
1247                                                      t1->data[(y) * t1->stride + x] < 0);
1248                     }
1249                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1250                 }
1251             }
1252 }
1253
1254 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1255                            int bpno, int vert_causal_ctx_csty_symbol)
1256 {
1257     int phalf, nhalf;
1258     int y0, x, y;
1259
1260     phalf = 1 << (bpno - 1);
1261     nhalf = -phalf;
1262
1263     for (y0 = 0; y0 < height; y0 += 4)
1264         for (x = 0; x < width; x++)
1265             for (y = y0; y < height && y < y0 + 4; y++)
1266                 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1267                     int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1268                         ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1269                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1270                     int r     = ff_mqc_decode(&t1->mqc,
1271                                               t1->mqc.cx_states + ctxno)
1272                                 ? phalf : nhalf;
1273                     t1->data[(y) * t1->stride + x]          += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1274                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1275                 }
1276 }
1277
1278 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1279                            int width, int height, int bpno, int bandno,
1280                            int seg_symbols, int vert_causal_ctx_csty_symbol)
1281 {
1282     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1283
1284     for (y0 = 0; y0 < height; y0 += 4) {
1285         for (x = 0; x < width; x++) {
1286             int flags_mask = -1;
1287             if (vert_causal_ctx_csty_symbol)
1288                 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1289             if (y0 + 3 < height &&
1290                 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1291                   (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1292                   (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1293                   (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1294                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1295                     continue;
1296                 runlen = ff_mqc_decode(&t1->mqc,
1297                                        t1->mqc.cx_states + MQC_CX_UNI);
1298                 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1299                                                        t1->mqc.cx_states +
1300                                                        MQC_CX_UNI);
1301                 dec = 1;
1302             } else {
1303                 runlen = 0;
1304                 dec    = 0;
1305             }
1306
1307             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1308                 int flags_mask = -1;
1309                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1310                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1311                 if (!dec) {
1312                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1313                         dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1314                                                                                              bandno));
1315                     }
1316                 }
1317                 if (dec) {
1318                     int xorbit;
1319                     int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1320                                                         &xorbit);
1321                     t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1322                                                     t1->mqc.cx_states + ctxno) ^
1323                                       xorbit)
1324                                      ? -mask : mask;
1325                     ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1326                 }
1327                 dec = 0;
1328                 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1329             }
1330         }
1331     }
1332     if (seg_symbols) {
1333         int val;
1334         val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1335         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1336         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1337         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1338         if (val != 0xa)
1339             av_log(s->avctx, AV_LOG_ERROR,
1340                    "Segmentation symbol value incorrect\n");
1341     }
1342 }
1343
1344 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1345                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1346                        int width, int height, int bandpos)
1347 {
1348     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
1349     int pass_cnt = 0;
1350     int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1351     int term_cnt = 0;
1352     int coder_type;
1353
1354     av_assert0(width <= 1024U && height <= 1024U);
1355     av_assert0(width*height <= 4096);
1356
1357     memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1358
1359     /* If code-block contains no compressed data: nothing to do. */
1360     if (!cblk->length)
1361         return 0;
1362
1363     memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1364
1365     cblk->data[cblk->length] = 0xff;
1366     cblk->data[cblk->length+1] = 0xff;
1367     ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1368
1369     while (passno--) {
1370         switch(pass_t) {
1371         case 0:
1372             decode_sigpass(t1, width, height, bpno + 1, bandpos,
1373                            vert_causal_ctx_csty_symbol);
1374             break;
1375         case 1:
1376             decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1377             break;
1378         case 2:
1379             av_assert2(!t1->mqc.raw);
1380             decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1381                            codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1382                            vert_causal_ctx_csty_symbol);
1383             break;
1384         }
1385         if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1386             ff_mqc_init_contexts(&t1->mqc);
1387
1388         if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1389             if (term_cnt >= cblk->nb_terminations) {
1390                 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1391                 return AVERROR_INVALIDDATA;
1392             }
1393             ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1394         }
1395
1396         pass_t++;
1397         if (pass_t == 3) {
1398             bpno--;
1399             pass_t = 0;
1400         }
1401         pass_cnt ++;
1402     }
1403
1404     if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1405         av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1406                cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1407     }
1408
1409     return 0;
1410 }
1411
1412 /* TODO: Verify dequantization for lossless case
1413  * comp->data can be float or int
1414  * band->stepsize can be float or int
1415  * depending on the type of DWT transformation.
1416  * see ISO/IEC 15444-1:2002 A.6.1 */
1417
1418 /* Float dequantization of a codeblock.*/
1419 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1420                                  Jpeg2000Component *comp,
1421                                  Jpeg2000T1Context *t1, Jpeg2000Band *band)
1422 {
1423     int i, j;
1424     int w = cblk->coord[0][1] - cblk->coord[0][0];
1425     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1426         float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1427         int *src = t1->data + j*t1->stride;
1428         for (i = 0; i < w; ++i)
1429             datap[i] = src[i] * band->f_stepsize;
1430     }
1431 }
1432
1433 /* Integer dequantization of a codeblock.*/
1434 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1435                                Jpeg2000Component *comp,
1436                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1437 {
1438     int i, j;
1439     int w = cblk->coord[0][1] - cblk->coord[0][0];
1440     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1441         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1442         int *src = t1->data + j*t1->stride;
1443         if (band->i_stepsize == 16384) {
1444             for (i = 0; i < w; ++i)
1445                 datap[i] = src[i] / 2;
1446         } else {
1447             // This should be VERY uncommon
1448             for (i = 0; i < w; ++i)
1449                 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 32768;
1450         }
1451     }
1452 }
1453
1454 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1455                                Jpeg2000Component *comp,
1456                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1457 {
1458     int i, j;
1459     int w = cblk->coord[0][1] - cblk->coord[0][0];
1460     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1461         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1462         int *src = t1->data + j*t1->stride;
1463         for (i = 0; i < w; ++i)
1464             datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<14)) >> 15;
1465     }
1466 }
1467
1468 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1469 {
1470     int i, csize = 1;
1471     void *src[3];
1472
1473     for (i = 1; i < 3; i++) {
1474         if (tile->codsty[0].transform != tile->codsty[i].transform) {
1475             av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1476             return;
1477         }
1478         if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1479             av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1480             return;
1481         }
1482     }
1483
1484     for (i = 0; i < 3; i++)
1485         if (tile->codsty[0].transform == FF_DWT97)
1486             src[i] = tile->comp[i].f_data;
1487         else
1488             src[i] = tile->comp[i].i_data;
1489
1490     for (i = 0; i < 2; i++)
1491         csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1492
1493     s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1494 }
1495
1496 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1497                                 AVFrame *picture)
1498 {
1499     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1500     int compno, reslevelno, bandno;
1501     int x, y;
1502     int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
1503     int pixelsize = planar ? 1 : pixdesc->nb_components;
1504
1505     uint8_t *line;
1506     Jpeg2000T1Context t1;
1507
1508     /* Loop on tile components */
1509     for (compno = 0; compno < s->ncomponents; compno++) {
1510         Jpeg2000Component *comp     = tile->comp + compno;
1511         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1512
1513         t1.stride = (1<<codsty->log2_cblk_width) + 2;
1514
1515         /* Loop on resolution levels */
1516         for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1517             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1518             /* Loop on bands */
1519             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1520                 int nb_precincts, precno;
1521                 Jpeg2000Band *band = rlevel->band + bandno;
1522                 int cblkno = 0, bandpos;
1523
1524                 bandpos = bandno + (reslevelno > 0);
1525
1526                 if (band->coord[0][0] == band->coord[0][1] ||
1527                     band->coord[1][0] == band->coord[1][1])
1528                     continue;
1529
1530                 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1531                 /* Loop on precincts */
1532                 for (precno = 0; precno < nb_precincts; precno++) {
1533                     Jpeg2000Prec *prec = band->prec + precno;
1534
1535                     /* Loop on codeblocks */
1536                     for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1537                         int x, y;
1538                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1539                         decode_cblk(s, codsty, &t1, cblk,
1540                                     cblk->coord[0][1] - cblk->coord[0][0],
1541                                     cblk->coord[1][1] - cblk->coord[1][0],
1542                                     bandpos);
1543
1544                         x = cblk->coord[0][0] - band->coord[0][0];
1545                         y = cblk->coord[1][0] - band->coord[1][0];
1546
1547                         if (codsty->transform == FF_DWT97)
1548                             dequantization_float(x, y, cblk, comp, &t1, band);
1549                         else if (codsty->transform == FF_DWT97_INT)
1550                             dequantization_int_97(x, y, cblk, comp, &t1, band);
1551                         else
1552                             dequantization_int(x, y, cblk, comp, &t1, band);
1553                    } /* end cblk */
1554                 } /*end prec */
1555             } /* end band */
1556         } /* end reslevel */
1557
1558         /* inverse DWT */
1559         ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1560     } /*end comp */
1561
1562     /* inverse MCT transformation */
1563     if (tile->codsty[0].mct)
1564         mct_decode(s, tile);
1565
1566     if (s->cdef[0] < 0) {
1567         for (x = 0; x < s->ncomponents; x++)
1568             s->cdef[x] = x + 1;
1569         if ((s->ncomponents & 1) == 0)
1570             s->cdef[s->ncomponents-1] = 0;
1571     }
1572
1573     if (s->precision <= 8) {
1574         for (compno = 0; compno < s->ncomponents; compno++) {
1575             Jpeg2000Component *comp = tile->comp + compno;
1576             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1577             float *datap = comp->f_data;
1578             int32_t *i_datap = comp->i_data;
1579             int cbps = s->cbps[compno];
1580             int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1581             int plane = 0;
1582
1583             if (planar)
1584                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1585
1586
1587             y    = tile->comp[compno].coord[1][0] - s->image_offset_y;
1588             line = picture->data[plane] + y / s->cdy[compno] * picture->linesize[plane];
1589             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
1590                 uint8_t *dst;
1591
1592                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
1593                 dst = line + x / s->cdx[compno] * pixelsize + compno*!planar;
1594
1595                 if (codsty->transform == FF_DWT97) {
1596                     for (; x < w; x ++) {
1597                         int val = lrintf(*datap) + (1 << (cbps - 1));
1598                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1599                         val = av_clip(val, 0, (1 << cbps) - 1);
1600                         *dst = val << (8 - cbps);
1601                         datap++;
1602                         dst += pixelsize;
1603                     }
1604                 } else {
1605                     for (; x < w; x ++) {
1606                         int val = *i_datap + (1 << (cbps - 1));
1607                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1608                         val = av_clip(val, 0, (1 << cbps) - 1);
1609                         *dst = val << (8 - cbps);
1610                         i_datap++;
1611                         dst += pixelsize;
1612                     }
1613                 }
1614                 line += picture->linesize[plane];
1615             }
1616         }
1617     } else {
1618         int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1619                         picture->format == AV_PIX_FMT_RGB48 ||
1620                         picture->format == AV_PIX_FMT_RGBA64 ||
1621                         picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1622
1623         for (compno = 0; compno < s->ncomponents; compno++) {
1624             Jpeg2000Component *comp = tile->comp + compno;
1625             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1626             float *datap = comp->f_data;
1627             int32_t *i_datap = comp->i_data;
1628             uint16_t *linel;
1629             int cbps = s->cbps[compno];
1630             int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1631             int plane = 0;
1632
1633             if (planar)
1634                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1635
1636             y     = tile->comp[compno].coord[1][0] - s->image_offset_y;
1637             linel = (uint16_t *)picture->data[plane] + y / s->cdy[compno] * (picture->linesize[plane] >> 1);
1638             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
1639                 uint16_t *dst;
1640
1641                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
1642                 dst = linel + (x / s->cdx[compno] * pixelsize + compno*!planar);
1643                 if (codsty->transform == FF_DWT97) {
1644                     for (; x < w; x ++) {
1645                         int  val = lrintf(*datap) + (1 << (cbps - 1));
1646                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1647                         val = av_clip(val, 0, (1 << cbps) - 1);
1648                         /* align 12 bit values in little-endian mode */
1649                         *dst = val << (precision - cbps);
1650                         datap++;
1651                         dst += pixelsize;
1652                     }
1653                 } else {
1654                     for (; x < w; x ++) {
1655                         int val = *i_datap + (1 << (cbps - 1));
1656                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1657                         val = av_clip(val, 0, (1 << cbps) - 1);
1658                         /* align 12 bit values in little-endian mode */
1659                         *dst = val << (precision - cbps);
1660                         i_datap++;
1661                         dst += pixelsize;
1662                     }
1663                 }
1664                 linel += picture->linesize[plane] >> 1;
1665             }
1666         }
1667     }
1668
1669     return 0;
1670 }
1671
1672 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1673 {
1674     int tileno, compno;
1675     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1676         if (s->tile[tileno].comp) {
1677             for (compno = 0; compno < s->ncomponents; compno++) {
1678                 Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
1679                 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1680
1681                 ff_jpeg2000_cleanup(comp, codsty);
1682             }
1683             av_freep(&s->tile[tileno].comp);
1684         }
1685     }
1686     av_freep(&s->tile);
1687     memset(s->codsty, 0, sizeof(s->codsty));
1688     memset(s->qntsty, 0, sizeof(s->qntsty));
1689     s->numXtiles = s->numYtiles = 0;
1690 }
1691
1692 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1693 {
1694     Jpeg2000CodingStyle *codsty = s->codsty;
1695     Jpeg2000QuantStyle *qntsty  = s->qntsty;
1696     uint8_t *properties         = s->properties;
1697
1698     for (;;) {
1699         int len, ret = 0;
1700         uint16_t marker;
1701         int oldpos;
1702
1703         if (bytestream2_get_bytes_left(&s->g) < 2) {
1704             av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1705             break;
1706         }
1707
1708         marker = bytestream2_get_be16u(&s->g);
1709         oldpos = bytestream2_tell(&s->g);
1710
1711         if (marker == JPEG2000_SOD) {
1712             Jpeg2000Tile *tile;
1713             Jpeg2000TilePart *tp;
1714
1715             if (!s->tile) {
1716                 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1717                 return AVERROR_INVALIDDATA;
1718             }
1719             if (s->curtileno < 0) {
1720                 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1721                 return AVERROR_INVALIDDATA;
1722             }
1723
1724             tile = s->tile + s->curtileno;
1725             tp = tile->tile_part + tile->tp_idx;
1726             if (tp->tp_end < s->g.buffer) {
1727                 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1728                 return AVERROR_INVALIDDATA;
1729             }
1730             bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1731             bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1732
1733             continue;
1734         }
1735         if (marker == JPEG2000_EOC)
1736             break;
1737
1738         len = bytestream2_get_be16(&s->g);
1739         if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
1740             av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
1741             return AVERROR_INVALIDDATA;
1742         }
1743
1744         switch (marker) {
1745         case JPEG2000_SIZ:
1746             ret = get_siz(s);
1747             if (!s->tile)
1748                 s->numXtiles = s->numYtiles = 0;
1749             break;
1750         case JPEG2000_COC:
1751             ret = get_coc(s, codsty, properties);
1752             break;
1753         case JPEG2000_COD:
1754             ret = get_cod(s, codsty, properties);
1755             break;
1756         case JPEG2000_QCC:
1757             ret = get_qcc(s, len, qntsty, properties);
1758             break;
1759         case JPEG2000_QCD:
1760             ret = get_qcd(s, len, qntsty, properties);
1761             break;
1762         case JPEG2000_SOT:
1763             if (!(ret = get_sot(s, len))) {
1764                 av_assert1(s->curtileno >= 0);
1765                 codsty = s->tile[s->curtileno].codsty;
1766                 qntsty = s->tile[s->curtileno].qntsty;
1767                 properties = s->tile[s->curtileno].properties;
1768             }
1769             break;
1770         case JPEG2000_COM:
1771             // the comment is ignored
1772             bytestream2_skip(&s->g, len - 2);
1773             break;
1774         case JPEG2000_TLM:
1775             // Tile-part lengths
1776             ret = get_tlm(s, len);
1777             break;
1778         case JPEG2000_PLT:
1779             // Packet length, tile-part header
1780             ret = get_plt(s, len);
1781             break;
1782         default:
1783             av_log(s->avctx, AV_LOG_ERROR,
1784                    "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1785                    marker, bytestream2_tell(&s->g) - 4);
1786             bytestream2_skip(&s->g, len - 2);
1787             break;
1788         }
1789         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1790             av_log(s->avctx, AV_LOG_ERROR,
1791                    "error during processing marker segment %.4"PRIx16"\n",
1792                    marker);
1793             return ret ? ret : -1;
1794         }
1795     }
1796     return 0;
1797 }
1798
1799 /* Read bit stream packets --> T2 operation. */
1800 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1801 {
1802     int ret = 0;
1803     int tileno;
1804
1805     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1806         Jpeg2000Tile *tile = s->tile + tileno;
1807
1808         if ((ret = init_tile(s, tileno)) < 0)
1809             return ret;
1810
1811         s->g = tile->tile_part[0].tpg;
1812         if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
1813             return ret;
1814     }
1815
1816     return 0;
1817 }
1818
1819 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1820 {
1821     uint32_t atom_size, atom, atom_end;
1822     int search_range = 10;
1823
1824     while (search_range
1825            &&
1826            bytestream2_get_bytes_left(&s->g) >= 8) {
1827         atom_size = bytestream2_get_be32u(&s->g);
1828         atom      = bytestream2_get_be32u(&s->g);
1829         atom_end  = bytestream2_tell(&s->g) + atom_size - 8;
1830
1831         if (atom == JP2_CODESTREAM)
1832             return 1;
1833
1834         if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
1835             return 0;
1836
1837         if (atom == JP2_HEADER &&
1838                    atom_size >= 16) {
1839             uint32_t atom2_size, atom2, atom2_end;
1840             do {
1841                 atom2_size = bytestream2_get_be32u(&s->g);
1842                 atom2      = bytestream2_get_be32u(&s->g);
1843                 atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
1844                 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1845                     break;
1846                 if (atom2 == JP2_CODESTREAM) {
1847                     return 1;
1848                 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
1849                     int method = bytestream2_get_byteu(&s->g);
1850                     bytestream2_skipu(&s->g, 2);
1851                     if (method == 1) {
1852                         s->colour_space = bytestream2_get_be32u(&s->g);
1853                     }
1854                 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
1855                     int i, size, colour_count, colour_channels, colour_depth[3];
1856                     uint32_t r, g, b;
1857                     colour_count = bytestream2_get_be16u(&s->g);
1858                     colour_channels = bytestream2_get_byteu(&s->g);
1859                     // FIXME: Do not ignore channel_sign
1860                     colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1861                     colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1862                     colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1863                     size = (colour_depth[0] + 7 >> 3) * colour_count +
1864                            (colour_depth[1] + 7 >> 3) * colour_count +
1865                            (colour_depth[2] + 7 >> 3) * colour_count;
1866                     if (colour_count > 256   ||
1867                         colour_channels != 3 ||
1868                         colour_depth[0] > 16 ||
1869                         colour_depth[1] > 16 ||
1870                         colour_depth[2] > 16 ||
1871                         atom2_size < size) {
1872                         avpriv_request_sample(s->avctx, "Unknown palette");
1873                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1874                         continue;
1875                     }
1876                     s->pal8 = 1;
1877                     for (i = 0; i < colour_count; i++) {
1878                         if (colour_depth[0] <= 8) {
1879                             r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
1880                             r |= r >> colour_depth[0];
1881                         } else {
1882                             r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
1883                         }
1884                         if (colour_depth[1] <= 8) {
1885                             g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
1886                             r |= r >> colour_depth[1];
1887                         } else {
1888                             g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
1889                         }
1890                         if (colour_depth[2] <= 8) {
1891                             b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
1892                             r |= r >> colour_depth[2];
1893                         } else {
1894                             b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
1895                         }
1896                         s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
1897                     }
1898                 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
1899                     int n = bytestream2_get_be16u(&s->g);
1900                     for (; n>0; n--) {
1901                         int cn   = bytestream2_get_be16(&s->g);
1902                         int av_unused typ  = bytestream2_get_be16(&s->g);
1903                         int asoc = bytestream2_get_be16(&s->g);
1904                         if (cn < 4 && asoc < 4)
1905                             s->cdef[cn] = asoc;
1906                     }
1907                 }
1908                 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1909             } while (atom_end - atom2_end >= 8);
1910         } else {
1911             search_range--;
1912         }
1913         bytestream2_seek(&s->g, atom_end, SEEK_SET);
1914     }
1915
1916     return 0;
1917 }
1918
1919 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
1920 {
1921     Jpeg2000DecoderContext *s = avctx->priv_data;
1922
1923     ff_jpeg2000dsp_init(&s->dsp);
1924
1925     return 0;
1926 }
1927
1928 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1929                                  int *got_frame, AVPacket *avpkt)
1930 {
1931     Jpeg2000DecoderContext *s = avctx->priv_data;
1932     ThreadFrame frame = { .f = data };
1933     AVFrame *picture = data;
1934     int tileno, ret;
1935
1936     s->avctx     = avctx;
1937     bytestream2_init(&s->g, avpkt->data, avpkt->size);
1938     s->curtileno = -1;
1939     memset(s->cdef, -1, sizeof(s->cdef));
1940
1941     if (bytestream2_get_bytes_left(&s->g) < 2) {
1942         ret = AVERROR_INVALIDDATA;
1943         goto end;
1944     }
1945
1946     // check if the image is in jp2 format
1947     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1948        (bytestream2_get_be32u(&s->g) == 12) &&
1949        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1950        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1951         if (!jp2_find_codestream(s)) {
1952             av_log(avctx, AV_LOG_ERROR,
1953                    "Could not find Jpeg2000 codestream atom.\n");
1954             ret = AVERROR_INVALIDDATA;
1955             goto end;
1956         }
1957     } else {
1958         bytestream2_seek(&s->g, 0, SEEK_SET);
1959     }
1960
1961     while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
1962         bytestream2_skip(&s->g, 1);
1963
1964     if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1965         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1966         ret = AVERROR_INVALIDDATA;
1967         goto end;
1968     }
1969     if (ret = jpeg2000_read_main_headers(s))
1970         goto end;
1971
1972     /* get picture buffer */
1973     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1974         goto end;
1975     picture->pict_type = AV_PICTURE_TYPE_I;
1976     picture->key_frame = 1;
1977
1978     if (ret = jpeg2000_read_bitstream_packets(s))
1979         goto end;
1980
1981     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1982         if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1983             goto end;
1984
1985     jpeg2000_dec_cleanup(s);
1986
1987     *got_frame = 1;
1988
1989     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
1990         memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
1991
1992     return bytestream2_tell(&s->g);
1993
1994 end:
1995     jpeg2000_dec_cleanup(s);
1996     return ret;
1997 }
1998
1999 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
2000 {
2001     ff_jpeg2000_init_tier1_luts();
2002     ff_mqc_init_context_tables();
2003 }
2004
2005 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2006 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2007
2008 static const AVOption options[] = {
2009     { "lowres",  "Lower the decoding resolution by a power of two",
2010         OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2011     { NULL },
2012 };
2013
2014 static const AVProfile profiles[] = {
2015     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0,  "JPEG 2000 codestream restriction 0"   },
2016     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1,  "JPEG 2000 codestream restriction 1"   },
2017     { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
2018     { FF_PROFILE_JPEG2000_DCINEMA_2K,             "JPEG 2000 digital cinema 2K"          },
2019     { FF_PROFILE_JPEG2000_DCINEMA_4K,             "JPEG 2000 digital cinema 4K"          },
2020     { FF_PROFILE_UNKNOWN },
2021 };
2022
2023 static const AVClass jpeg2000_class = {
2024     .class_name = "jpeg2000",
2025     .item_name  = av_default_item_name,
2026     .option     = options,
2027     .version    = LIBAVUTIL_VERSION_INT,
2028 };
2029
2030 AVCodec ff_jpeg2000_decoder = {
2031     .name             = "jpeg2000",
2032     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2033     .type             = AVMEDIA_TYPE_VIDEO,
2034     .id               = AV_CODEC_ID_JPEG2000,
2035     .capabilities     = CODEC_CAP_FRAME_THREADS,
2036     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
2037     .init_static_data = jpeg2000_init_static_data,
2038     .init             = jpeg2000_decode_init,
2039     .decode           = jpeg2000_decode_frame,
2040     .priv_class       = &jpeg2000_class,
2041     .max_lowres       = 5,
2042     .profiles         = NULL_IF_CONFIG_SMALL(profiles)
2043 };