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