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