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