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