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