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