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