]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeg2000dec.c
libavcodec/decode: avoid UB when getting plane sizes
[ffmpeg] / libavcodec / jpeg2000dec.c
1 /*
2  * JPEG 2000 image decoder
3  * Copyright (c) 2007 Kamil Nowosad
4  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * JPEG 2000 image decoder
26  */
27
28 #include <inttypes.h>
29 #include <math.h>
30
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/common.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/thread.h"
38 #include "avcodec.h"
39 #include "bytestream.h"
40 #include "internal.h"
41 #include "thread.h"
42 #include "jpeg2000.h"
43 #include "jpeg2000dsp.h"
44 #include "profiles.h"
45
46 #define JP2_SIG_TYPE    0x6A502020
47 #define JP2_SIG_VALUE   0x0D0A870A
48 #define JP2_CODESTREAM  0x6A703263
49 #define JP2_HEADER      0x6A703268
50
51 #define HAD_COC 0x01
52 #define HAD_QCC 0x02
53
54 #define MAX_POCS 32
55
56 typedef struct Jpeg2000POCEntry {
57     uint16_t LYEpoc;
58     uint16_t CSpoc;
59     uint16_t CEpoc;
60     uint8_t RSpoc;
61     uint8_t REpoc;
62     uint8_t Ppoc;
63 } Jpeg2000POCEntry;
64
65 typedef struct Jpeg2000POC {
66     Jpeg2000POCEntry poc[MAX_POCS];
67     int nb_poc;
68     int is_default;
69 } Jpeg2000POC;
70
71 typedef struct Jpeg2000TilePart {
72     uint8_t tile_index;                 // Tile index who refers the tile-part
73     const uint8_t *tp_end;
74     GetByteContext tpg;                 // bit stream in tile-part
75 } Jpeg2000TilePart;
76
77 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
78  * one per component, so tile_part elements have a size of 3 */
79 typedef struct Jpeg2000Tile {
80     Jpeg2000Component   *comp;
81     uint8_t             properties[4];
82     Jpeg2000CodingStyle codsty[4];
83     Jpeg2000QuantStyle  qntsty[4];
84     Jpeg2000POC         poc;
85     Jpeg2000TilePart    tile_part[32];
86     uint8_t             has_ppt;                // whether this tile has a ppt marker
87     uint8_t             *packed_headers;        // contains packed headers. Used only along with PPT marker
88     int                 packed_headers_size;    // size in bytes of the packed headers
89     GetByteContext      packed_headers_stream;  // byte context corresponding to packed headers
90     uint16_t tp_idx;                    // Tile-part index
91     int coord[2][2];                    // border coordinates {{x0, x1}, {y0, y1}}
92 } Jpeg2000Tile;
93
94 typedef struct Jpeg2000DecoderContext {
95     AVClass         *class;
96     AVCodecContext  *avctx;
97     GetByteContext  g;
98
99     int             width, height;
100     int             image_offset_x, image_offset_y;
101     int             tile_offset_x, tile_offset_y;
102     uint8_t         cbps[4];    // bits per sample in particular components
103     uint8_t         sgnd[4];    // if a component is signed
104     uint8_t         properties[4];
105     int             cdx[4], cdy[4];
106     int             precision;
107     int             ncomponents;
108     int             colour_space;
109     uint32_t        palette[256];
110     int8_t          pal8;
111     int             cdef[4];
112     int             tile_width, tile_height;
113     unsigned        numXtiles, numYtiles;
114     int             maxtilelen;
115     AVRational      sar;
116
117     Jpeg2000CodingStyle codsty[4];
118     Jpeg2000QuantStyle  qntsty[4];
119     Jpeg2000POC         poc;
120     uint8_t             roi_shift[4];
121
122     int             bit_index;
123
124     int             curtileno;
125
126     Jpeg2000Tile    *tile;
127     Jpeg2000DSPContext dsp;
128
129     /*options parameters*/
130     int             reduction_factor;
131 } Jpeg2000DecoderContext;
132
133 /* get_bits functions for JPEG2000 packet bitstream
134  * It is a get_bit function with a bit-stuffing routine. If the value of the
135  * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
136  * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
137 static int get_bits(Jpeg2000DecoderContext *s, int n)
138 {
139     int res = 0;
140
141     while (--n >= 0) {
142         res <<= 1;
143         if (s->bit_index == 0) {
144             s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
145         }
146         s->bit_index--;
147         res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
148     }
149     return res;
150 }
151
152 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
153 {
154     if (bytestream2_get_byte(&s->g) == 0xff)
155         bytestream2_skip(&s->g, 1);
156     s->bit_index = 8;
157 }
158
159 /* decode the value stored in node */
160 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
161                            int threshold)
162 {
163     Jpeg2000TgtNode *stack[30];
164     int sp = -1, curval = 0;
165
166     if (!node) {
167         av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
168         return AVERROR_INVALIDDATA;
169     }
170
171     while (node && !node->vis) {
172         stack[++sp] = node;
173         node        = node->parent;
174     }
175
176     if (node)
177         curval = node->val;
178     else
179         curval = stack[sp]->val;
180
181     while (curval < threshold && sp >= 0) {
182         if (curval < stack[sp]->val)
183             curval = stack[sp]->val;
184         while (curval < threshold) {
185             int ret;
186             if ((ret = get_bits(s, 1)) > 0) {
187                 stack[sp]->vis++;
188                 break;
189             } else if (!ret)
190                 curval++;
191             else
192                 return ret;
193         }
194         stack[sp]->val = curval;
195         sp--;
196     }
197     return curval;
198 }
199
200 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
201                          int bpc, uint32_t log2_chroma_wh, int pal8)
202 {
203     int match = 1;
204     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
205
206     av_assert2(desc);
207
208     if (desc->nb_components != components) {
209         return 0;
210     }
211
212     switch (components) {
213     case 4:
214         match = match && desc->comp[3].depth >= bpc &&
215                          (log2_chroma_wh >> 14 & 3) == 0 &&
216                          (log2_chroma_wh >> 12 & 3) == 0;
217     case 3:
218         match = match && desc->comp[2].depth >= bpc &&
219                          (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
220                          (log2_chroma_wh >>  8 & 3) == desc->log2_chroma_h;
221     case 2:
222         match = match && desc->comp[1].depth >= bpc &&
223                          (log2_chroma_wh >>  6 & 3) == desc->log2_chroma_w &&
224                          (log2_chroma_wh >>  4 & 3) == desc->log2_chroma_h;
225
226     case 1:
227         match = match && desc->comp[0].depth >= bpc &&
228                          (log2_chroma_wh >>  2 & 3) == 0 &&
229                          (log2_chroma_wh       & 3) == 0 &&
230                          (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
231     }
232     return match;
233 }
234
235 // pix_fmts with lower bpp have to be listed before
236 // similar pix_fmts with higher bpp.
237 #define RGB_PIXEL_FORMATS   AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
238 #define GRAY_PIXEL_FORMATS  AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
239 #define YUV_PIXEL_FORMATS   AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
240                             AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
241                             AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
242                             AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
243                             AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
244                             AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
245                             AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
246                             AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
247                             AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
248                             AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
249                             AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
250 #define XYZ_PIXEL_FORMATS   AV_PIX_FMT_XYZ12
251
252 static const enum AVPixelFormat rgb_pix_fmts[]  = {RGB_PIXEL_FORMATS};
253 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
254 static const enum AVPixelFormat yuv_pix_fmts[]  = {YUV_PIXEL_FORMATS};
255 static const enum AVPixelFormat xyz_pix_fmts[]  = {XYZ_PIXEL_FORMATS,
256                                                    YUV_PIXEL_FORMATS};
257 static const enum AVPixelFormat all_pix_fmts[]  = {RGB_PIXEL_FORMATS,
258                                                    GRAY_PIXEL_FORMATS,
259                                                    YUV_PIXEL_FORMATS,
260                                                    XYZ_PIXEL_FORMATS};
261
262 /* marker segments */
263 /* get sizes and offsets of image, tiles; number of components */
264 static int get_siz(Jpeg2000DecoderContext *s)
265 {
266     int i;
267     int ncomponents;
268     uint32_t log2_chroma_wh = 0;
269     const enum AVPixelFormat *possible_fmts = NULL;
270     int possible_fmts_nb = 0;
271     int ret;
272     int o_dimx, o_dimy; //original image dimensions.
273     int dimx, dimy;
274
275     if (bytestream2_get_bytes_left(&s->g) < 36) {
276         av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
277         return AVERROR_INVALIDDATA;
278     }
279
280     s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
281     s->width          = bytestream2_get_be32u(&s->g); // Width
282     s->height         = bytestream2_get_be32u(&s->g); // Height
283     s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
284     s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
285     s->tile_width     = bytestream2_get_be32u(&s->g); // XTSiz
286     s->tile_height    = bytestream2_get_be32u(&s->g); // YTSiz
287     s->tile_offset_x  = bytestream2_get_be32u(&s->g); // XT0Siz
288     s->tile_offset_y  = bytestream2_get_be32u(&s->g); // YT0Siz
289     ncomponents       = bytestream2_get_be16u(&s->g); // CSiz
290
291     if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
292         avpriv_request_sample(s->avctx, "Large Dimensions");
293         return AVERROR_PATCHWELCOME;
294     }
295
296     if (ncomponents <= 0) {
297         av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
298                s->ncomponents);
299         return AVERROR_INVALIDDATA;
300     }
301
302     if (ncomponents > 4) {
303         avpriv_request_sample(s->avctx, "Support for %d components",
304                               ncomponents);
305         return AVERROR_PATCHWELCOME;
306     }
307
308     if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
309         s->image_offset_x < s->tile_offset_x ||
310         s->image_offset_y < s->tile_offset_y ||
311         s->tile_width  + (int64_t)s->tile_offset_x <= s->image_offset_x ||
312         s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
313     ) {
314         av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
315         return AVERROR_INVALIDDATA;
316     }
317
318     s->ncomponents = ncomponents;
319
320     if (s->tile_width <= 0 || s->tile_height <= 0) {
321         av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
322                s->tile_width, s->tile_height);
323         return AVERROR_INVALIDDATA;
324     }
325
326     if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
327         av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
328         return AVERROR_INVALIDDATA;
329     }
330
331     for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
332         uint8_t x    = bytestream2_get_byteu(&s->g);
333         s->cbps[i]   = (x & 0x7f) + 1;
334         s->precision = FFMAX(s->cbps[i], s->precision);
335         s->sgnd[i]   = !!(x & 0x80);
336         s->cdx[i]    = bytestream2_get_byteu(&s->g);
337         s->cdy[i]    = bytestream2_get_byteu(&s->g);
338         if (   !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
339             || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
340             av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
341             return AVERROR_INVALIDDATA;
342         }
343         log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
344     }
345
346     s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
347     s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
348
349     // There must be at least a SOT and SOD per tile, their minimum size is 14
350     if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
351         s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
352     ) {
353         s->numXtiles = s->numYtiles = 0;
354         return AVERROR(EINVAL);
355     }
356
357     s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
358     if (!s->tile) {
359         s->numXtiles = s->numYtiles = 0;
360         return AVERROR(ENOMEM);
361     }
362
363     for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
364         Jpeg2000Tile *tile = s->tile + i;
365
366         tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
367         if (!tile->comp)
368             return AVERROR(ENOMEM);
369     }
370
371     /* compute image size with reduction factor */
372     o_dimx = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
373                                                s->reduction_factor);
374     o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
375                                                s->reduction_factor);
376     dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
377     dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
378     for (i = 1; i < s->ncomponents; i++) {
379         dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
380         dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
381     }
382
383     ret = ff_set_dimensions(s->avctx, dimx, dimy);
384     if (ret < 0)
385         return ret;
386
387     if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
388         s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
389         possible_fmts = xyz_pix_fmts;
390         possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
391     } else {
392         switch (s->colour_space) {
393         case 16:
394             possible_fmts = rgb_pix_fmts;
395             possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
396             break;
397         case 17:
398             possible_fmts = gray_pix_fmts;
399             possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
400             break;
401         case 18:
402             possible_fmts = yuv_pix_fmts;
403             possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
404             break;
405         default:
406             possible_fmts = all_pix_fmts;
407             possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
408             break;
409         }
410     }
411     if (   s->avctx->pix_fmt != AV_PIX_FMT_NONE
412         && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
413             s->avctx->pix_fmt = AV_PIX_FMT_NONE;
414     if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
415         for (i = 0; i < possible_fmts_nb; ++i) {
416             if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
417                 s->avctx->pix_fmt = possible_fmts[i];
418                 break;
419             }
420         }
421
422     if (i == possible_fmts_nb) {
423         if (ncomponents == 4 &&
424             s->cdy[0] == 1 && s->cdx[0] == 1 &&
425             s->cdy[1] == 1 && s->cdx[1] == 1 &&
426             s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
427             if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
428                 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
429                 s->cdef[0] = 0;
430                 s->cdef[1] = 1;
431                 s->cdef[2] = 2;
432                 s->cdef[3] = 3;
433                 i = 0;
434             }
435         } else if (ncomponents == 3 && s->precision == 8 &&
436                    s->cdx[0] == s->cdx[1] && s->cdx[0] == s->cdx[2] &&
437                    s->cdy[0] == s->cdy[1] && s->cdy[0] == s->cdy[2]) {
438             s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
439             i = 0;
440         } else if (ncomponents == 2 && s->precision == 8 &&
441                    s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
442             s->avctx->pix_fmt = AV_PIX_FMT_YA8;
443             i = 0;
444         } else if (ncomponents == 1 && s->precision == 8) {
445             s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
446             i = 0;
447         }
448     }
449
450
451     if (i == possible_fmts_nb) {
452         av_log(s->avctx, AV_LOG_ERROR,
453                "Unknown pix_fmt, profile: %d, colour_space: %d, "
454                "components: %d, precision: %d\n"
455                "cdx[0]: %d, cdy[0]: %d\n"
456                "cdx[1]: %d, cdy[1]: %d\n"
457                "cdx[2]: %d, cdy[2]: %d\n"
458                "cdx[3]: %d, cdy[3]: %d\n",
459                s->avctx->profile, s->colour_space, ncomponents, s->precision,
460                s->cdx[0],
461                s->cdy[0],
462                ncomponents > 1 ? s->cdx[1] : 0,
463                ncomponents > 1 ? s->cdy[1] : 0,
464                ncomponents > 2 ? s->cdx[2] : 0,
465                ncomponents > 2 ? s->cdy[2] : 0,
466                ncomponents > 3 ? s->cdx[3] : 0,
467                ncomponents > 3 ? s->cdy[3] : 0);
468         return AVERROR_PATCHWELCOME;
469     }
470     s->avctx->bits_per_raw_sample = s->precision;
471     return 0;
472 }
473
474 /* get common part for COD and COC segments */
475 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
476 {
477     uint8_t byte;
478
479     if (bytestream2_get_bytes_left(&s->g) < 5) {
480         av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
481         return AVERROR_INVALIDDATA;
482     }
483
484     /*  nreslevels = number of resolution levels
485                    = number of decomposition level +1 */
486     c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
487     if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
488         av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
489         return AVERROR_INVALIDDATA;
490     }
491
492     if (c->nreslevels <= s->reduction_factor) {
493         /* we are forced to update reduction_factor as its requested value is
494            not compatible with this bitstream, and as we might have used it
495            already in setup earlier we have to fail this frame until
496            reinitialization is implemented */
497         av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
498         s->reduction_factor = c->nreslevels - 1;
499         return AVERROR(EINVAL);
500     }
501
502     /* compute number of resolution levels to decode */
503     c->nreslevels2decode = c->nreslevels - s->reduction_factor;
504
505     c->log2_cblk_width  = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
506     c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
507
508     if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
509         c->log2_cblk_width + c->log2_cblk_height > 12) {
510         av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
511         return AVERROR_INVALIDDATA;
512     }
513
514     c->cblk_style = bytestream2_get_byteu(&s->g);
515     if (c->cblk_style != 0) { // cblk style
516         av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
517         if (c->cblk_style & JPEG2000_CBLK_BYPASS)
518             av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
519     }
520     c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
521     /* set integer 9/7 DWT in case of BITEXACT flag */
522     if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
523         c->transform = FF_DWT97_INT;
524     else if (c->transform == FF_DWT53) {
525         s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
526     }
527
528     if (c->csty & JPEG2000_CSTY_PREC) {
529         int i;
530         for (i = 0; i < c->nreslevels; i++) {
531             byte = bytestream2_get_byte(&s->g);
532             c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
533             c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
534             if (i)
535                 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
536                     av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
537                            c->log2_prec_widths[i], c->log2_prec_heights[i]);
538                     c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
539                     return AVERROR_INVALIDDATA;
540                 }
541         }
542     } else {
543         memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
544         memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
545     }
546     return 0;
547 }
548
549 /* get coding parameters for a particular tile or whole image*/
550 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
551                    uint8_t *properties)
552 {
553     Jpeg2000CodingStyle tmp;
554     int compno, ret;
555
556     if (bytestream2_get_bytes_left(&s->g) < 5) {
557         av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
558         return AVERROR_INVALIDDATA;
559     }
560
561     tmp.csty = bytestream2_get_byteu(&s->g);
562
563     // get progression order
564     tmp.prog_order = bytestream2_get_byteu(&s->g);
565
566     tmp.nlayers    = bytestream2_get_be16u(&s->g);
567     tmp.mct        = bytestream2_get_byteu(&s->g); // multiple component transformation
568
569     if (tmp.mct && s->ncomponents < 3) {
570         av_log(s->avctx, AV_LOG_ERROR,
571                "MCT %"PRIu8" with too few components (%d)\n",
572                tmp.mct, s->ncomponents);
573         return AVERROR_INVALIDDATA;
574     }
575
576     if ((ret = get_cox(s, &tmp)) < 0)
577         return ret;
578     tmp.init = 1;
579     for (compno = 0; compno < s->ncomponents; compno++)
580         if (!(properties[compno] & HAD_COC))
581             memcpy(c + compno, &tmp, sizeof(tmp));
582     return 0;
583 }
584
585 /* Get coding parameters for a component in the whole image or a
586  * particular tile. */
587 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
588                    uint8_t *properties)
589 {
590     int compno, ret;
591     uint8_t has_eph, has_sop;
592
593     if (bytestream2_get_bytes_left(&s->g) < 2) {
594         av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
595         return AVERROR_INVALIDDATA;
596     }
597
598     compno = bytestream2_get_byteu(&s->g);
599
600     if (compno >= s->ncomponents) {
601         av_log(s->avctx, AV_LOG_ERROR,
602                "Invalid compno %d. There are %d components in the image.\n",
603                compno, s->ncomponents);
604         return AVERROR_INVALIDDATA;
605     }
606
607     c      += compno;
608     has_eph = c->csty & JPEG2000_CSTY_EPH;
609     has_sop = c->csty & JPEG2000_CSTY_SOP;
610     c->csty = bytestream2_get_byteu(&s->g);
611     c->csty |= has_eph; //do not override eph present bits from COD
612     c->csty |= has_sop; //do not override sop present bits from COD
613
614     if ((ret = get_cox(s, c)) < 0)
615         return ret;
616
617     properties[compno] |= HAD_COC;
618     c->init = 1;
619     return 0;
620 }
621
622 static int get_rgn(Jpeg2000DecoderContext *s, int n)
623 {
624     uint16_t compno;
625     compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
626                                      bytestream2_get_be16u(&s->g);
627     if (bytestream2_get_byte(&s->g)) {
628         av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
629         return AVERROR_INVALIDDATA; // SRgn field value is 0
630     }
631     // SPrgn field
632     // Currently compno cannot be greater than 4.
633     // However, future implementation should support compno up to 65536
634     if (compno < s->ncomponents) {
635         int v;
636         if (s->curtileno == -1) {
637             v =  bytestream2_get_byte(&s->g);
638             if (v > 30)
639                 return AVERROR_PATCHWELCOME;
640             s->roi_shift[compno] = v;
641         } else {
642             if (s->tile[s->curtileno].tp_idx != 0)
643                 return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
644             v = bytestream2_get_byte(&s->g);
645             if (v > 30)
646                 return AVERROR_PATCHWELCOME;
647             s->tile[s->curtileno].comp[compno].roi_shift = v;
648         }
649         return 0;
650     }
651     return AVERROR_INVALIDDATA;
652 }
653
654 /* Get common part for QCD and QCC segments. */
655 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
656 {
657     int i, x;
658
659     if (bytestream2_get_bytes_left(&s->g) < 1)
660         return AVERROR_INVALIDDATA;
661
662     x = bytestream2_get_byteu(&s->g); // Sqcd
663
664     q->nguardbits = x >> 5;
665     q->quantsty   = x & 0x1f;
666
667     if (q->quantsty == JPEG2000_QSTY_NONE) {
668         n -= 3;
669         if (bytestream2_get_bytes_left(&s->g) < n ||
670             n > JPEG2000_MAX_DECLEVELS*3)
671             return AVERROR_INVALIDDATA;
672         for (i = 0; i < n; i++)
673             q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
674     } else if (q->quantsty == JPEG2000_QSTY_SI) {
675         if (bytestream2_get_bytes_left(&s->g) < 2)
676             return AVERROR_INVALIDDATA;
677         x          = bytestream2_get_be16u(&s->g);
678         q->expn[0] = x >> 11;
679         q->mant[0] = x & 0x7ff;
680         for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
681             int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
682             q->expn[i] = curexpn;
683             q->mant[i] = q->mant[0];
684         }
685     } else {
686         n = (n - 3) >> 1;
687         if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
688             n > JPEG2000_MAX_DECLEVELS*3)
689             return AVERROR_INVALIDDATA;
690         for (i = 0; i < n; i++) {
691             x          = bytestream2_get_be16u(&s->g);
692             q->expn[i] = x >> 11;
693             q->mant[i] = x & 0x7ff;
694         }
695     }
696     return 0;
697 }
698
699 /* Get quantization parameters for a particular tile or a whole image. */
700 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
701                    uint8_t *properties)
702 {
703     Jpeg2000QuantStyle tmp;
704     int compno, ret;
705
706     memset(&tmp, 0, sizeof(tmp));
707
708     if ((ret = get_qcx(s, n, &tmp)) < 0)
709         return ret;
710     for (compno = 0; compno < s->ncomponents; compno++)
711         if (!(properties[compno] & HAD_QCC))
712             memcpy(q + compno, &tmp, sizeof(tmp));
713     return 0;
714 }
715
716 /* Get quantization parameters for a component in the whole image
717  * on in a particular tile. */
718 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
719                    uint8_t *properties)
720 {
721     int compno;
722
723     if (bytestream2_get_bytes_left(&s->g) < 1)
724         return AVERROR_INVALIDDATA;
725
726     compno = bytestream2_get_byteu(&s->g);
727
728     if (compno >= s->ncomponents) {
729         av_log(s->avctx, AV_LOG_ERROR,
730                "Invalid compno %d. There are %d components in the image.\n",
731                compno, s->ncomponents);
732         return AVERROR_INVALIDDATA;
733     }
734
735     properties[compno] |= HAD_QCC;
736     return get_qcx(s, n - 1, q + compno);
737 }
738
739 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
740 {
741     int i;
742     int elem_size = s->ncomponents <= 257 ? 7 : 9;
743     Jpeg2000POC tmp = {{{0}}};
744
745     if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
746         av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
747         return AVERROR_INVALIDDATA;
748     }
749
750     if (elem_size > 7) {
751         avpriv_request_sample(s->avctx, "Fat POC not supported");
752         return AVERROR_PATCHWELCOME;
753     }
754
755     tmp.nb_poc = (size - 2) / elem_size;
756     if (tmp.nb_poc > MAX_POCS) {
757         avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
758         return AVERROR_PATCHWELCOME;
759     }
760
761     for (i = 0; i<tmp.nb_poc; i++) {
762         Jpeg2000POCEntry *e = &tmp.poc[i];
763         e->RSpoc  = bytestream2_get_byteu(&s->g);
764         e->CSpoc  = bytestream2_get_byteu(&s->g);
765         e->LYEpoc = bytestream2_get_be16u(&s->g);
766         e->REpoc  = bytestream2_get_byteu(&s->g);
767         e->CEpoc  = bytestream2_get_byteu(&s->g);
768         e->Ppoc   = bytestream2_get_byteu(&s->g);
769         if (!e->CEpoc)
770             e->CEpoc = 256;
771         if (e->CEpoc > s->ncomponents)
772             e->CEpoc = s->ncomponents;
773         if (   e->RSpoc >= e->REpoc || e->REpoc > 33
774             || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
775             || !e->LYEpoc) {
776             av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
777                 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
778             );
779             return AVERROR_INVALIDDATA;
780         }
781     }
782
783     if (!p->nb_poc || p->is_default) {
784         *p = tmp;
785     } else {
786         if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
787             av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
788             return AVERROR_INVALIDDATA;
789         }
790         memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
791         p->nb_poc += tmp.nb_poc;
792     }
793
794     p->is_default = 0;
795
796     return 0;
797 }
798
799
800 /* Get start of tile segment. */
801 static int get_sot(Jpeg2000DecoderContext *s, int n)
802 {
803     Jpeg2000TilePart *tp;
804     uint16_t Isot;
805     uint32_t Psot;
806     unsigned TPsot;
807
808     if (bytestream2_get_bytes_left(&s->g) < 8)
809         return AVERROR_INVALIDDATA;
810
811     s->curtileno = 0;
812     Isot = bytestream2_get_be16u(&s->g);        // Isot
813     if (Isot >= s->numXtiles * s->numYtiles)
814         return AVERROR_INVALIDDATA;
815
816     s->curtileno = Isot;
817     Psot  = bytestream2_get_be32u(&s->g);       // Psot
818     TPsot = bytestream2_get_byteu(&s->g);       // TPsot
819
820     /* Read TNSot but not used */
821     bytestream2_get_byteu(&s->g);               // TNsot
822
823     if (!Psot)
824         Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
825
826     if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
827         av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
828         return AVERROR_INVALIDDATA;
829     }
830
831     if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
832         avpriv_request_sample(s->avctx, "Too many tile parts");
833         return AVERROR_PATCHWELCOME;
834     }
835
836     s->tile[Isot].tp_idx = TPsot;
837     tp             = s->tile[Isot].tile_part + TPsot;
838     tp->tile_index = Isot;
839     tp->tp_end     = s->g.buffer + Psot - n - 2;
840
841     if (!TPsot) {
842         Jpeg2000Tile *tile = s->tile + s->curtileno;
843
844         /* copy defaults */
845         memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
846         memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
847         memcpy(&tile->poc  , &s->poc  , sizeof(tile->poc));
848         tile->poc.is_default = 1;
849     }
850
851     return 0;
852 }
853
854 static int read_crg(Jpeg2000DecoderContext *s, int n)
855 {
856     if (s->ncomponents*4 != n - 2) {
857         av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
858         return AVERROR_INVALIDDATA;
859     }
860     bytestream2_skip(&s->g, n - 2);
861     return 0;
862 }
863 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
864  * Used to know the number of tile parts and lengths.
865  * There may be multiple TLMs in the header.
866  * TODO: The function is not used for tile-parts management, nor anywhere else.
867  * It can be useful to allocate memory for tile parts, before managing the SOT
868  * markers. Parsing the TLM header is needed to increment the input header
869  * buffer.
870  * This marker is mandatory for DCI. */
871 static int get_tlm(Jpeg2000DecoderContext *s, int n)
872 {
873     uint8_t Stlm, ST, SP, tile_tlm, i;
874     bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
875     Stlm = bytestream2_get_byte(&s->g);
876
877     // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
878     ST = (Stlm >> 4) & 0x03;
879     if (ST == 0x03) {
880         av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
881         return AVERROR_INVALIDDATA;
882     }
883
884     SP       = (Stlm >> 6) & 0x01;
885     tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
886     for (i = 0; i < tile_tlm; i++) {
887         switch (ST) {
888         case 0:
889             break;
890         case 1:
891             bytestream2_get_byte(&s->g);
892             break;
893         case 2:
894             bytestream2_get_be16(&s->g);
895             break;
896         case 3:
897             bytestream2_get_be32(&s->g);
898             break;
899         }
900         if (SP == 0) {
901             bytestream2_get_be16(&s->g);
902         } else {
903             bytestream2_get_be32(&s->g);
904         }
905     }
906     return 0;
907 }
908
909 static int get_plt(Jpeg2000DecoderContext *s, int n)
910 {
911     int i;
912     int v;
913
914     av_log(s->avctx, AV_LOG_DEBUG,
915             "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
916
917     if (n < 4)
918         return AVERROR_INVALIDDATA;
919
920     /*Zplt =*/ bytestream2_get_byte(&s->g);
921
922     for (i = 0; i < n - 3; i++) {
923         v = bytestream2_get_byte(&s->g);
924     }
925     if (v & 0x80)
926         return AVERROR_INVALIDDATA;
927
928     return 0;
929 }
930
931 static int get_ppt(Jpeg2000DecoderContext *s, int n)
932 {
933     Jpeg2000Tile *tile;
934     void *new;
935
936     if (n < 3) {
937         av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
938         return AVERROR_INVALIDDATA;
939     }
940     if (s->curtileno < 0)
941         return AVERROR_INVALIDDATA;
942
943     tile = &s->tile[s->curtileno];
944     if (tile->tp_idx != 0) {
945         av_log(s->avctx, AV_LOG_ERROR,
946                "PPT marker can occur only on first tile part of a tile.\n");
947         return AVERROR_INVALIDDATA;
948     }
949
950     tile->has_ppt = 1;  // this tile has a ppt marker
951     bytestream2_get_byte(&s->g); // Zppt is skipped and not used
952     new = av_realloc(tile->packed_headers,
953                      tile->packed_headers_size + n - 3);
954     if (new) {
955         tile->packed_headers = new;
956     } else
957         return AVERROR(ENOMEM);
958     memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
959     memcpy(tile->packed_headers + tile->packed_headers_size,
960            s->g.buffer, n - 3);
961     tile->packed_headers_size += n - 3;
962     bytestream2_skip(&s->g, n - 3);
963
964     return 0;
965 }
966
967 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
968 {
969     int compno;
970     int tilex = tileno % s->numXtiles;
971     int tiley = tileno / s->numXtiles;
972     Jpeg2000Tile *tile = s->tile + tileno;
973
974     if (!tile->comp)
975         return AVERROR(ENOMEM);
976
977     tile->coord[0][0] = av_clip(tilex       * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
978     tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
979     tile->coord[1][0] = av_clip(tiley       * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
980     tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
981
982     for (compno = 0; compno < s->ncomponents; compno++) {
983         Jpeg2000Component *comp = tile->comp + compno;
984         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
985         Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
986         int ret; // global bandno
987
988         comp->coord_o[0][0] = tile->coord[0][0];
989         comp->coord_o[0][1] = tile->coord[0][1];
990         comp->coord_o[1][0] = tile->coord[1][0];
991         comp->coord_o[1][1] = tile->coord[1][1];
992
993         comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
994         comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
995         comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
996         comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
997
998         comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
999         comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
1000         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
1001         comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
1002
1003         if (!comp->roi_shift)
1004             comp->roi_shift = s->roi_shift[compno];
1005         if (!codsty->init)
1006             return AVERROR_INVALIDDATA;
1007         if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
1008                                              s->cbps[compno], s->cdx[compno],
1009                                              s->cdy[compno], s->avctx))
1010             return ret;
1011     }
1012     return 0;
1013 }
1014
1015 /* Read the number of coding passes. */
1016 static int getnpasses(Jpeg2000DecoderContext *s)
1017 {
1018     int num;
1019     if (!get_bits(s, 1))
1020         return 1;
1021     if (!get_bits(s, 1))
1022         return 2;
1023     if ((num = get_bits(s, 2)) != 3)
1024         return num < 0 ? num : 3 + num;
1025     if ((num = get_bits(s, 5)) != 31)
1026         return num < 0 ? num : 6 + num;
1027     num = get_bits(s, 7);
1028     return num < 0 ? num : 37 + num;
1029 }
1030
1031 static int getlblockinc(Jpeg2000DecoderContext *s)
1032 {
1033     int res = 0, ret;
1034     while (ret = get_bits(s, 1)) {
1035         if (ret < 0)
1036             return ret;
1037         res++;
1038     }
1039     return res;
1040 }
1041
1042 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1043                                  int *tp_index)
1044 {
1045     s->g = tile->tile_part[*tp_index].tpg;
1046     if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1047         if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1048             s->g = tile->tile_part[++(*tp_index)].tpg;
1049         }
1050     }
1051     if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1052         bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1053 }
1054
1055 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1056                                   Jpeg2000CodingStyle *codsty,
1057                                   Jpeg2000ResLevel *rlevel, int precno,
1058                                   int layno, uint8_t *expn, int numgbits)
1059 {
1060     int bandno, cblkno, ret, nb_code_blocks;
1061     int cwsno;
1062
1063     if (layno < rlevel->band[0].prec[precno].decoded_layers)
1064         return 0;
1065     rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1066     // Select stream to read from
1067     if (tile->has_ppt)
1068         s->g = tile->packed_headers_stream;
1069     else
1070         select_stream(s, tile, tp_index);
1071
1072     if (!(ret = get_bits(s, 1))) {
1073         jpeg2000_flush(s);
1074         goto skip_data;
1075     } else if (ret < 0)
1076         return ret;
1077
1078     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1079         Jpeg2000Band *band = rlevel->band + bandno;
1080         Jpeg2000Prec *prec = band->prec + precno;
1081
1082         if (band->coord[0][0] == band->coord[0][1] ||
1083             band->coord[1][0] == band->coord[1][1])
1084             continue;
1085         nb_code_blocks =  prec->nb_codeblocks_height *
1086                           prec->nb_codeblocks_width;
1087         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1088             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1089             int incl, newpasses, llen;
1090             void *tmp;
1091
1092             if (cblk->npasses)
1093                 incl = get_bits(s, 1);
1094             else
1095                 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1096             if (!incl)
1097                 continue;
1098             else if (incl < 0)
1099                 return incl;
1100
1101             if (!cblk->npasses) {
1102                 int v = expn[bandno] + numgbits - 1 -
1103                         tag_tree_decode(s, prec->zerobits + cblkno, 100);
1104                 if (v < 0 || v > 30) {
1105                     av_log(s->avctx, AV_LOG_ERROR,
1106                            "nonzerobits %d invalid or unsupported\n", v);
1107                     return AVERROR_INVALIDDATA;
1108                 }
1109                 cblk->nonzerobits = v;
1110             }
1111             if ((newpasses = getnpasses(s)) < 0)
1112                 return newpasses;
1113             av_assert2(newpasses > 0);
1114             if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1115                 avpriv_request_sample(s->avctx, "Too many passes");
1116                 return AVERROR_PATCHWELCOME;
1117             }
1118             if ((llen = getlblockinc(s)) < 0)
1119                 return llen;
1120             if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1121                 avpriv_request_sample(s->avctx,
1122                                       "Block with length beyond 16 bits");
1123                 return AVERROR_PATCHWELCOME;
1124             }
1125
1126             cblk->lblock += llen;
1127
1128             cblk->nb_lengthinc = 0;
1129             cblk->nb_terminationsinc = 0;
1130             av_free(cblk->lengthinc);
1131             cblk->lengthinc  = av_mallocz_array(newpasses    , sizeof(*cblk->lengthinc));
1132             if (!cblk->lengthinc)
1133                 return AVERROR(ENOMEM);
1134             tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1135             if (!tmp)
1136                 return AVERROR(ENOMEM);
1137             cblk->data_start = tmp;
1138             do {
1139                 int newpasses1 = 0;
1140
1141                 while (newpasses1 < newpasses) {
1142                     newpasses1 ++;
1143                     if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1144                         cblk->nb_terminationsinc ++;
1145                         break;
1146                     }
1147                 }
1148
1149                 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1150                     return ret;
1151                 if (ret > cblk->data_allocated) {
1152                     size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1153                     void *new = av_realloc(cblk->data, new_size);
1154                     if (new) {
1155                         cblk->data = new;
1156                         cblk->data_allocated = new_size;
1157                     }
1158                 }
1159                 if (ret > cblk->data_allocated) {
1160                     avpriv_request_sample(s->avctx,
1161                                         "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1162                                         cblk->data_allocated);
1163                     return AVERROR_PATCHWELCOME;
1164                 }
1165                 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1166                 cblk->npasses  += newpasses1;
1167                 newpasses -= newpasses1;
1168             } while(newpasses);
1169         }
1170     }
1171     jpeg2000_flush(s);
1172
1173     if (codsty->csty & JPEG2000_CSTY_EPH) {
1174         if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1175             bytestream2_skip(&s->g, 2);
1176         else
1177             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1178     }
1179
1180     // Save state of stream
1181     if (tile->has_ppt) {
1182         tile->packed_headers_stream = s->g;
1183         select_stream(s, tile, tp_index);
1184     }
1185     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1186         Jpeg2000Band *band = rlevel->band + bandno;
1187         Jpeg2000Prec *prec = band->prec + precno;
1188
1189         nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1190         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1191             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1192             if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1193                 continue;
1194             for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1195                 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1196                     size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1197                     void *new = av_realloc(cblk->data, new_size);
1198                     if (new) {
1199                         cblk->data = new;
1200                         cblk->data_allocated = new_size;
1201                     }
1202                 }
1203                 if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1204                     || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1205                 ) {
1206                     av_log(s->avctx, AV_LOG_ERROR,
1207                         "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1208                         cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1209                     return AVERROR_INVALIDDATA;
1210                 }
1211
1212                 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1213                 cblk->length   += cblk->lengthinc[cwsno];
1214                 cblk->lengthinc[cwsno] = 0;
1215                 if (cblk->nb_terminationsinc) {
1216                     cblk->nb_terminationsinc--;
1217                     cblk->nb_terminations++;
1218                     cblk->data[cblk->length++] = 0xFF;
1219                     cblk->data[cblk->length++] = 0xFF;
1220                     cblk->data_start[cblk->nb_terminations] = cblk->length;
1221                 }
1222             }
1223             av_freep(&cblk->lengthinc);
1224         }
1225     }
1226     // Save state of stream
1227     tile->tile_part[*tp_index].tpg = s->g;
1228     return 0;
1229
1230 skip_data:
1231     if (tile->has_ppt)
1232         tile->packed_headers_stream = s->g;
1233     else
1234         tile->tile_part[*tp_index].tpg = s->g;
1235     return 0;
1236 }
1237
1238 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1239                                              int RSpoc, int CSpoc,
1240                                              int LYEpoc, int REpoc, int CEpoc,
1241                                              int Ppoc, int *tp_index)
1242 {
1243     int ret = 0;
1244     int layno, reslevelno, compno, precno, ok_reslevel;
1245     int x, y;
1246     int step_x, step_y;
1247
1248     switch (Ppoc) {
1249     case JPEG2000_PGOD_RLCP:
1250         av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1251         ok_reslevel = 1;
1252         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1253             ok_reslevel = 0;
1254             for (layno = 0; layno < LYEpoc; layno++) {
1255                 for (compno = CSpoc; compno < CEpoc; compno++) {
1256                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1257                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1258                     if (reslevelno < codsty->nreslevels) {
1259                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1260                                                 reslevelno;
1261                         ok_reslevel = 1;
1262                         for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1263                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1264                                                               codsty, rlevel,
1265                                                               precno, layno,
1266                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1267                                                               qntsty->nguardbits)) < 0)
1268                                 return ret;
1269                     }
1270                 }
1271             }
1272         }
1273         break;
1274
1275     case JPEG2000_PGOD_LRCP:
1276         av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1277         for (layno = 0; layno < LYEpoc; layno++) {
1278             ok_reslevel = 1;
1279             for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1280                 ok_reslevel = 0;
1281                 for (compno = CSpoc; compno < CEpoc; compno++) {
1282                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1283                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1284                     if (reslevelno < codsty->nreslevels) {
1285                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1286                                                 reslevelno;
1287                         ok_reslevel = 1;
1288                         for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1289                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1290                                                               codsty, rlevel,
1291                                                               precno, layno,
1292                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1293                                                               qntsty->nguardbits)) < 0)
1294                                 return ret;
1295                     }
1296                 }
1297             }
1298         }
1299         break;
1300
1301     case JPEG2000_PGOD_CPRL:
1302         av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1303         for (compno = CSpoc; compno < CEpoc; compno++) {
1304             Jpeg2000Component *comp     = tile->comp + compno;
1305             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1306             Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1307             step_x = 32;
1308             step_y = 32;
1309
1310             if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1311                 continue;
1312
1313             for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1314                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1315                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1316                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1317                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1318             }
1319             if (step_x >= 31 || step_y >= 31){
1320                 avpriv_request_sample(s->avctx, "CPRL with large step");
1321                 return AVERROR_PATCHWELCOME;
1322             }
1323             step_x = 1<<step_x;
1324             step_y = 1<<step_y;
1325
1326             for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1327                 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1328                     for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1329                         unsigned prcx, prcy;
1330                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1331                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1332                         int xc = x / s->cdx[compno];
1333                         int yc = y / s->cdy[compno];
1334
1335                         if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1336                             continue;
1337
1338                         if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1339                             continue;
1340
1341                         // check if a precinct exists
1342                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1343                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1344                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1345                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1346
1347                         precno = prcx + rlevel->num_precincts_x * prcy;
1348
1349                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1350                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1351                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1352                             continue;
1353                         }
1354
1355                         for (layno = 0; layno < LYEpoc; layno++) {
1356                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1357                                                               precno, layno,
1358                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1359                                                               qntsty->nguardbits)) < 0)
1360                                 return ret;
1361                         }
1362                     }
1363                 }
1364             }
1365         }
1366         break;
1367
1368     case JPEG2000_PGOD_RPCL:
1369         av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1370         ok_reslevel = 1;
1371         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1372             ok_reslevel = 0;
1373             step_x = 30;
1374             step_y = 30;
1375             for (compno = CSpoc; compno < CEpoc; compno++) {
1376                 Jpeg2000Component *comp     = tile->comp + compno;
1377                 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1378
1379                 if (reslevelno < codsty->nreslevels) {
1380                     uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1381                     Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1382                     step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1383                     step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1384                 }
1385             }
1386             step_x = 1<<step_x;
1387             step_y = 1<<step_y;
1388
1389             for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1390                 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1391                     for (compno = CSpoc; compno < CEpoc; compno++) {
1392                         Jpeg2000Component *comp     = tile->comp + compno;
1393                         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1394                         Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1395                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1396                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1397                         unsigned prcx, prcy;
1398                         int trx0, try0;
1399
1400                         if (!s->cdx[compno] || !s->cdy[compno])
1401                             return AVERROR_INVALIDDATA;
1402
1403                         trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], s->cdx[compno] << reducedresno);
1404                         try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], s->cdy[compno] << reducedresno);
1405
1406                         if (reslevelno >= codsty->nreslevels)
1407                             continue;
1408
1409                         if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1410                              (y == tile->coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + rlevel->log2_prec_height)))))
1411                             continue;
1412
1413                         if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1414                              (x == tile->coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + rlevel->log2_prec_width)))))
1415                             continue;
1416
1417                         // check if a precinct exists
1418                         prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1419                         prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1420                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1421                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1422
1423                         precno = prcx + rlevel->num_precincts_x * prcy;
1424
1425                         ok_reslevel = 1;
1426                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1427                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1428                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1429                             continue;
1430                         }
1431
1432                         for (layno = 0; layno < LYEpoc; layno++) {
1433                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1434                                                               codsty, rlevel,
1435                                                               precno, layno,
1436                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1437                                                               qntsty->nguardbits)) < 0)
1438                                 return ret;
1439                         }
1440                     }
1441                 }
1442             }
1443         }
1444         break;
1445
1446     case JPEG2000_PGOD_PCRL:
1447         av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1448         step_x = 32;
1449         step_y = 32;
1450         for (compno = CSpoc; compno < CEpoc; compno++) {
1451             Jpeg2000Component *comp     = tile->comp + compno;
1452             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1453
1454             for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1455                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1456                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1457                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1458                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1459             }
1460         }
1461         if (step_x >= 31 || step_y >= 31){
1462             avpriv_request_sample(s->avctx, "PCRL with large step");
1463             return AVERROR_PATCHWELCOME;
1464         }
1465         step_x = 1<<step_x;
1466         step_y = 1<<step_y;
1467
1468         for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1469             for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1470                 for (compno = CSpoc; compno < CEpoc; compno++) {
1471                     Jpeg2000Component *comp     = tile->comp + compno;
1472                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1473                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1474
1475                     if (!s->cdx[compno] || !s->cdy[compno])
1476                         return AVERROR_INVALIDDATA;
1477
1478                     for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1479                         unsigned prcx, prcy;
1480                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1481                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1482                         int trx0, try0;
1483
1484                         trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], s->cdx[compno] << reducedresno);
1485                         try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], s->cdy[compno] << reducedresno);
1486
1487                         if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1488                              (y == tile->coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + rlevel->log2_prec_height)))))
1489                              continue;
1490
1491                         if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1492                              (x == tile->coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + rlevel->log2_prec_width)))))
1493                              continue;
1494
1495                         // check if a precinct exists
1496                         prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1497                         prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1498                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1499                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1500
1501                         precno = prcx + rlevel->num_precincts_x * prcy;
1502
1503                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1504                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1505                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1506                             continue;
1507                         }
1508
1509                         for (layno = 0; layno < LYEpoc; layno++) {
1510                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1511                                                               precno, layno,
1512                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1513                                                               qntsty->nguardbits)) < 0)
1514                                 return ret;
1515                         }
1516                     }
1517                 }
1518             }
1519         }
1520         break;
1521
1522     default:
1523         break;
1524     }
1525
1526     return ret;
1527 }
1528
1529 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1530 {
1531     int ret = AVERROR_BUG;
1532     int i;
1533     int tp_index = 0;
1534
1535     s->bit_index = 8;
1536     if (tile->poc.nb_poc) {
1537         for (i=0; i<tile->poc.nb_poc; i++) {
1538             Jpeg2000POCEntry *e = &tile->poc.poc[i];
1539             ret = jpeg2000_decode_packets_po_iteration(s, tile,
1540                 e->RSpoc, e->CSpoc,
1541                 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1542                 e->REpoc,
1543                 FFMIN(e->CEpoc, s->ncomponents),
1544                 e->Ppoc, &tp_index
1545                 );
1546             if (ret < 0)
1547                 return ret;
1548         }
1549     } else {
1550         ret = jpeg2000_decode_packets_po_iteration(s, tile,
1551             0, 0,
1552             tile->codsty[0].nlayers,
1553             33,
1554             s->ncomponents,
1555             tile->codsty[0].prog_order,
1556             &tp_index
1557         );
1558     }
1559     /* EOC marker reached */
1560     bytestream2_skip(&s->g, 2);
1561
1562     return ret;
1563 }
1564
1565 /* TIER-1 routines */
1566 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1567                            int bpno, int bandno,
1568                            int vert_causal_ctx_csty_symbol)
1569 {
1570     int mask = 3 << (bpno - 1), y0, x, y;
1571
1572     for (y0 = 0; y0 < height; y0 += 4)
1573         for (x = 0; x < width; x++)
1574             for (y = y0; y < height && y < y0 + 4; y++) {
1575                 int flags_mask = -1;
1576                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1577                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1578                 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1579                 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1580                     if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1581                         int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1582                         if (t1->mqc.raw)
1583                              t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1584                         else
1585                              t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1586                                                -mask : mask;
1587
1588                         ff_jpeg2000_set_significance(t1, x, y,
1589                                                      t1->data[(y) * t1->stride + x] < 0);
1590                     }
1591                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1592                 }
1593             }
1594 }
1595
1596 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1597                            int bpno, int vert_causal_ctx_csty_symbol)
1598 {
1599     int phalf, nhalf;
1600     int y0, x, y;
1601
1602     phalf = 1 << (bpno - 1);
1603     nhalf = -phalf;
1604
1605     for (y0 = 0; y0 < height; y0 += 4)
1606         for (x = 0; x < width; x++)
1607             for (y = y0; y < height && y < y0 + 4; y++)
1608                 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1609                     int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1610                         ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1611                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1612                     int r     = ff_mqc_decode(&t1->mqc,
1613                                               t1->mqc.cx_states + ctxno)
1614                                 ? phalf : nhalf;
1615                     t1->data[(y) * t1->stride + x]          += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1616                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1617                 }
1618 }
1619
1620 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1621                            int width, int height, int bpno, int bandno,
1622                            int seg_symbols, int vert_causal_ctx_csty_symbol)
1623 {
1624     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1625
1626     for (y0 = 0; y0 < height; y0 += 4) {
1627         for (x = 0; x < width; x++) {
1628             int flags_mask = -1;
1629             if (vert_causal_ctx_csty_symbol)
1630                 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1631             if (y0 + 3 < height &&
1632                 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1633                   (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1634                   (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1635                   (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1636                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1637                     continue;
1638                 runlen = ff_mqc_decode(&t1->mqc,
1639                                        t1->mqc.cx_states + MQC_CX_UNI);
1640                 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1641                                                        t1->mqc.cx_states +
1642                                                        MQC_CX_UNI);
1643                 dec = 1;
1644             } else {
1645                 runlen = 0;
1646                 dec    = 0;
1647             }
1648
1649             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1650                 int flags_mask = -1;
1651                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1652                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1653                 if (!dec) {
1654                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1655                         dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1656                                                                                              bandno));
1657                     }
1658                 }
1659                 if (dec) {
1660                     int xorbit;
1661                     int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1662                                                         &xorbit);
1663                     t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1664                                                     t1->mqc.cx_states + ctxno) ^
1665                                       xorbit)
1666                                      ? -mask : mask;
1667                     ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1668                 }
1669                 dec = 0;
1670                 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1671             }
1672         }
1673     }
1674     if (seg_symbols) {
1675         int val;
1676         val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1677         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1678         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1679         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1680         if (val != 0xa)
1681             av_log(s->avctx, AV_LOG_ERROR,
1682                    "Segmentation symbol value incorrect\n");
1683     }
1684 }
1685
1686 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1687                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1688                        int width, int height, int bandpos, uint8_t roi_shift)
1689 {
1690     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1691     int pass_cnt = 0;
1692     int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1693     int term_cnt = 0;
1694     int coder_type;
1695
1696     av_assert0(width <= 1024U && height <= 1024U);
1697     av_assert0(width*height <= 4096);
1698
1699     memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1700
1701     /* If code-block contains no compressed data: nothing to do. */
1702     if (!cblk->length)
1703         return 0;
1704
1705     memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1706
1707     cblk->data[cblk->length] = 0xff;
1708     cblk->data[cblk->length+1] = 0xff;
1709     ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1710
1711     while (passno--) {
1712         if (bpno < 0 || bpno > 29) {
1713             av_log(s->avctx, AV_LOG_ERROR, "bpno became invalid\n");
1714             return AVERROR_INVALIDDATA;
1715         }
1716         switch(pass_t) {
1717         case 0:
1718             decode_sigpass(t1, width, height, bpno + 1, bandpos,
1719                            vert_causal_ctx_csty_symbol);
1720             break;
1721         case 1:
1722             decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1723             break;
1724         case 2:
1725             av_assert2(!t1->mqc.raw);
1726             decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1727                            codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1728                            vert_causal_ctx_csty_symbol);
1729             break;
1730         }
1731         if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1732             ff_mqc_init_contexts(&t1->mqc);
1733
1734         if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1735             if (term_cnt >= cblk->nb_terminations) {
1736                 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1737                 return AVERROR_INVALIDDATA;
1738             }
1739             if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1740                 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1741                     cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1742                     pass_cnt, cblk->npasses);
1743             }
1744
1745             ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1746         }
1747
1748         pass_t++;
1749         if (pass_t == 3) {
1750             bpno--;
1751             pass_t = 0;
1752         }
1753         pass_cnt ++;
1754     }
1755
1756     if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1757         av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1758                cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1759     }
1760
1761     return 1;
1762 }
1763
1764 static inline int roi_shift_param(Jpeg2000Component *comp,
1765                                    int quan_parameter)
1766 {
1767     uint8_t roi_shift;
1768     int val;
1769     roi_shift = comp->roi_shift;
1770     val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1771
1772     if (val > (1 << roi_shift))
1773         return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1774     return quan_parameter;
1775 }
1776
1777 /* TODO: Verify dequantization for lossless case
1778  * comp->data can be float or int
1779  * band->stepsize can be float or int
1780  * depending on the type of DWT transformation.
1781  * see ISO/IEC 15444-1:2002 A.6.1 */
1782
1783 /* Float dequantization of a codeblock.*/
1784 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1785                                  Jpeg2000Component *comp,
1786                                  Jpeg2000T1Context *t1, Jpeg2000Band *band)
1787 {
1788     int i, j;
1789     int w = cblk->coord[0][1] - cblk->coord[0][0];
1790     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1791         float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1792         int *src = t1->data + j*t1->stride;
1793         for (i = 0; i < w; ++i)
1794             datap[i] = src[i] * band->f_stepsize;
1795     }
1796 }
1797
1798 /* Integer dequantization of a codeblock.*/
1799 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1800                                Jpeg2000Component *comp,
1801                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1802 {
1803     int i, j;
1804     int w = cblk->coord[0][1] - cblk->coord[0][0];
1805     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1806         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1807         int *src = t1->data + j*t1->stride;
1808         if (band->i_stepsize == 32768) {
1809             for (i = 0; i < w; ++i)
1810                 datap[i] = src[i] / 2;
1811         } else {
1812             // This should be VERY uncommon
1813             for (i = 0; i < w; ++i)
1814                 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1815         }
1816     }
1817 }
1818
1819 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1820                                Jpeg2000Component *comp,
1821                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1822 {
1823     int i, j;
1824     int w = cblk->coord[0][1] - cblk->coord[0][0];
1825     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1826         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1827         int *src = t1->data + j*t1->stride;
1828         for (i = 0; i < w; ++i)
1829             datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1830     }
1831 }
1832
1833 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1834 {
1835     int i, csize = 1;
1836     void *src[3];
1837
1838     for (i = 1; i < 3; i++) {
1839         if (tile->codsty[0].transform != tile->codsty[i].transform) {
1840             av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1841             return;
1842         }
1843         if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1844             av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1845             return;
1846         }
1847     }
1848
1849     for (i = 0; i < 3; i++)
1850         if (tile->codsty[0].transform == FF_DWT97)
1851             src[i] = tile->comp[i].f_data;
1852         else
1853             src[i] = tile->comp[i].i_data;
1854
1855     for (i = 0; i < 2; i++)
1856         csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1857
1858     s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1859 }
1860
1861 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1862                                   Jpeg2000Component *comp,
1863                                   Jpeg2000T1Context *t1)
1864 {
1865     int i, j;
1866     int w = cblk->coord[0][1] - cblk->coord[0][0];
1867     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1868         int *src = t1->data + j*t1->stride;
1869         for (i = 0; i < w; ++i)
1870             src[i] = roi_shift_param(comp, src[i]);
1871     }
1872 }
1873
1874 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1875 {
1876     Jpeg2000T1Context t1;
1877
1878     int compno, reslevelno, bandno;
1879
1880     /* Loop on tile components */
1881     for (compno = 0; compno < s->ncomponents; compno++) {
1882         Jpeg2000Component *comp     = tile->comp + compno;
1883         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1884         int coded = 0;
1885
1886         t1.stride = (1<<codsty->log2_cblk_width) + 2;
1887
1888         /* Loop on resolution levels */
1889         for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1890             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1891             /* Loop on bands */
1892             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1893                 int nb_precincts, precno;
1894                 Jpeg2000Band *band = rlevel->band + bandno;
1895                 int cblkno = 0, bandpos;
1896
1897                 bandpos = bandno + (reslevelno > 0);
1898
1899                 if (band->coord[0][0] == band->coord[0][1] ||
1900                     band->coord[1][0] == band->coord[1][1])
1901                     continue;
1902
1903                 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1904                 /* Loop on precincts */
1905                 for (precno = 0; precno < nb_precincts; precno++) {
1906                     Jpeg2000Prec *prec = band->prec + precno;
1907
1908                     /* Loop on codeblocks */
1909                     for (cblkno = 0;
1910                          cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1911                          cblkno++) {
1912                         int x, y;
1913                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1914                         int ret = decode_cblk(s, codsty, &t1, cblk,
1915                                     cblk->coord[0][1] - cblk->coord[0][0],
1916                                     cblk->coord[1][1] - cblk->coord[1][0],
1917                                     bandpos, comp->roi_shift);
1918                         if (ret)
1919                             coded = 1;
1920                         else
1921                             continue;
1922                         x = cblk->coord[0][0] - band->coord[0][0];
1923                         y = cblk->coord[1][0] - band->coord[1][0];
1924
1925                         if (comp->roi_shift)
1926                             roi_scale_cblk(cblk, comp, &t1);
1927                         if (codsty->transform == FF_DWT97)
1928                             dequantization_float(x, y, cblk, comp, &t1, band);
1929                         else if (codsty->transform == FF_DWT97_INT)
1930                             dequantization_int_97(x, y, cblk, comp, &t1, band);
1931                         else
1932                             dequantization_int(x, y, cblk, comp, &t1, band);
1933                    } /* end cblk */
1934                 } /*end prec */
1935             } /* end band */
1936         } /* end reslevel */
1937
1938         /* inverse DWT */
1939         if (coded)
1940             ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1941
1942     } /*end comp */
1943 }
1944
1945 #define WRITE_FRAME(D, PIXEL)                                                                     \
1946     static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,         \
1947                                          AVFrame * picture, int precision)                        \
1948     {                                                                                             \
1949         const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);               \
1950         int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);                              \
1951         int pixelsize = planar ? 1 : pixdesc->nb_components;                                      \
1952                                                                                                   \
1953         int compno;                                                                               \
1954         int x, y;                                                                                 \
1955                                                                                                   \
1956         for (compno = 0; compno < s->ncomponents; compno++) {                                     \
1957             Jpeg2000Component *comp     = tile->comp + compno;                                    \
1958             Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \
1959             PIXEL *line;                                                                          \
1960             float *datap     = comp->f_data;                                                      \
1961             int32_t *i_datap = comp->i_data;                                                      \
1962             int cbps         = s->cbps[compno];                                                   \
1963             int w            = tile->comp[compno].coord[0][1] -                                   \
1964                                ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]);            \
1965             int h            = tile->comp[compno].coord[1][1] -                                   \
1966                                ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]);            \
1967             int plane        = 0;                                                                 \
1968                                                                                                   \
1969             if (planar)                                                                           \
1970                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);                 \
1971                                                                                                   \
1972             y    = tile->comp[compno].coord[1][0] -                                               \
1973                    ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]);                        \
1974             line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1975             for (; y < h; y++) {                                                                  \
1976                 PIXEL *dst;                                                                       \
1977                                                                                                   \
1978                 x   = tile->comp[compno].coord[0][0] -                                            \
1979                       ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]);                     \
1980                 dst = line + x * pixelsize + compno*!planar;                                      \
1981                                                                                                   \
1982                 if (codsty->transform == FF_DWT97) {                                              \
1983                     for (; x < w; x++) {                                                          \
1984                         int val = lrintf(*datap) + (1 << (cbps - 1));                             \
1985                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1986                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1987                         *dst = val << (precision - cbps);                                         \
1988                         datap++;                                                                  \
1989                         dst += pixelsize;                                                         \
1990                     }                                                                             \
1991                 } else {                                                                          \
1992                     for (; x < w; x++) {                                                          \
1993                         int val = *i_datap + (1 << (cbps - 1));                                   \
1994                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1995                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1996                         *dst = val << (precision - cbps);                                         \
1997                         i_datap++;                                                                \
1998                         dst += pixelsize;                                                         \
1999                     }                                                                             \
2000                 }                                                                                 \
2001                 line += picture->linesize[plane] / sizeof(PIXEL);                                 \
2002             }                                                                                     \
2003         }                                                                                         \
2004                                                                                                   \
2005     }
2006
2007 WRITE_FRAME(8, uint8_t)
2008 WRITE_FRAME(16, uint16_t)
2009
2010 #undef WRITE_FRAME
2011
2012 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2013                                 int jobnr, int threadnr)
2014 {
2015     Jpeg2000DecoderContext *s = avctx->priv_data;
2016     AVFrame *picture = td;
2017     Jpeg2000Tile *tile = s->tile + jobnr;
2018     int x;
2019
2020     tile_codeblocks(s, tile);
2021
2022     /* inverse MCT transformation */
2023     if (tile->codsty[0].mct)
2024         mct_decode(s, tile);
2025
2026     for (x = 0; x < s->ncomponents; x++) {
2027         if (s->cdef[x] < 0) {
2028             for (x = 0; x < s->ncomponents; x++) {
2029                 s->cdef[x] = x + 1;
2030             }
2031             if ((s->ncomponents & 1) == 0)
2032                 s->cdef[s->ncomponents-1] = 0;
2033             break;
2034         }
2035     }
2036
2037     if (s->precision <= 8) {
2038         write_frame_8(s, tile, picture, 8);
2039     } else {
2040         int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2041                         picture->format == AV_PIX_FMT_RGB48 ||
2042                         picture->format == AV_PIX_FMT_RGBA64 ||
2043                         picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2044
2045         write_frame_16(s, tile, picture, precision);
2046     }
2047
2048     return 0;
2049 }
2050
2051 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2052 {
2053     int tileno, compno;
2054     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2055         if (s->tile[tileno].comp) {
2056             for (compno = 0; compno < s->ncomponents; compno++) {
2057                 Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
2058                 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2059
2060                 ff_jpeg2000_cleanup(comp, codsty);
2061             }
2062             av_freep(&s->tile[tileno].comp);
2063             av_freep(&s->tile[tileno].packed_headers);
2064             s->tile[tileno].packed_headers_size = 0;
2065         }
2066     }
2067     av_freep(&s->tile);
2068     memset(s->codsty, 0, sizeof(s->codsty));
2069     memset(s->qntsty, 0, sizeof(s->qntsty));
2070     memset(s->properties, 0, sizeof(s->properties));
2071     memset(&s->poc  , 0, sizeof(s->poc));
2072     s->numXtiles = s->numYtiles = 0;
2073     s->ncomponents = 0;
2074 }
2075
2076 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2077 {
2078     Jpeg2000CodingStyle *codsty = s->codsty;
2079     Jpeg2000QuantStyle *qntsty  = s->qntsty;
2080     Jpeg2000POC         *poc    = &s->poc;
2081     uint8_t *properties         = s->properties;
2082
2083     for (;;) {
2084         int len, ret = 0;
2085         uint16_t marker;
2086         int oldpos;
2087
2088         if (bytestream2_get_bytes_left(&s->g) < 2) {
2089             av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2090             break;
2091         }
2092
2093         marker = bytestream2_get_be16u(&s->g);
2094         oldpos = bytestream2_tell(&s->g);
2095         if (marker >= 0xFF30 && marker <= 0xFF3F)
2096             continue;
2097         if (marker == JPEG2000_SOD) {
2098             Jpeg2000Tile *tile;
2099             Jpeg2000TilePart *tp;
2100
2101             if (!s->tile) {
2102                 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2103                 return AVERROR_INVALIDDATA;
2104             }
2105             if (s->curtileno < 0) {
2106                 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2107                 return AVERROR_INVALIDDATA;
2108             }
2109
2110             tile = s->tile + s->curtileno;
2111             tp = tile->tile_part + tile->tp_idx;
2112             if (tp->tp_end < s->g.buffer) {
2113                 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2114                 return AVERROR_INVALIDDATA;
2115             }
2116
2117             if (tile->has_ppt && tile->tp_idx == 0) {
2118                 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2119             }
2120
2121             bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2122             bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2123
2124             continue;
2125         }
2126         if (marker == JPEG2000_EOC)
2127             break;
2128
2129         len = bytestream2_get_be16(&s->g);
2130         if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2131             if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2132                 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2133                 return AVERROR_INVALIDDATA;
2134             }
2135             av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2136             break;
2137         }
2138
2139         switch (marker) {
2140         case JPEG2000_SIZ:
2141             if (s->ncomponents) {
2142                 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2143                 return AVERROR_INVALIDDATA;
2144             }
2145             ret = get_siz(s);
2146             if (!s->tile)
2147                 s->numXtiles = s->numYtiles = 0;
2148             break;
2149         case JPEG2000_COC:
2150             ret = get_coc(s, codsty, properties);
2151             break;
2152         case JPEG2000_COD:
2153             ret = get_cod(s, codsty, properties);
2154             break;
2155         case JPEG2000_RGN:
2156             ret = get_rgn(s, len);
2157             break;
2158         case JPEG2000_QCC:
2159             ret = get_qcc(s, len, qntsty, properties);
2160             break;
2161         case JPEG2000_QCD:
2162             ret = get_qcd(s, len, qntsty, properties);
2163             break;
2164         case JPEG2000_POC:
2165             ret = get_poc(s, len, poc);
2166             break;
2167         case JPEG2000_SOT:
2168             if (!(ret = get_sot(s, len))) {
2169                 av_assert1(s->curtileno >= 0);
2170                 codsty = s->tile[s->curtileno].codsty;
2171                 qntsty = s->tile[s->curtileno].qntsty;
2172                 poc    = &s->tile[s->curtileno].poc;
2173                 properties = s->tile[s->curtileno].properties;
2174             }
2175             break;
2176         case JPEG2000_PLM:
2177             // the PLM marker is ignored
2178         case JPEG2000_COM:
2179             // the comment is ignored
2180             bytestream2_skip(&s->g, len - 2);
2181             break;
2182         case JPEG2000_CRG:
2183             ret = read_crg(s, len);
2184             break;
2185         case JPEG2000_TLM:
2186             // Tile-part lengths
2187             ret = get_tlm(s, len);
2188             break;
2189         case JPEG2000_PLT:
2190             // Packet length, tile-part header
2191             ret = get_plt(s, len);
2192             break;
2193         case JPEG2000_PPT:
2194             // Packed headers, tile-part header
2195             ret = get_ppt(s, len);
2196             break;
2197         default:
2198             av_log(s->avctx, AV_LOG_ERROR,
2199                    "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2200                    marker, bytestream2_tell(&s->g) - 4);
2201             bytestream2_skip(&s->g, len - 2);
2202             break;
2203         }
2204         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2205             av_log(s->avctx, AV_LOG_ERROR,
2206                    "error during processing marker segment %.4"PRIx16"\n",
2207                    marker);
2208             return ret ? ret : -1;
2209         }
2210     }
2211     return 0;
2212 }
2213
2214 /* Read bit stream packets --> T2 operation. */
2215 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2216 {
2217     int ret = 0;
2218     int tileno;
2219
2220     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2221         Jpeg2000Tile *tile = s->tile + tileno;
2222
2223         if ((ret = init_tile(s, tileno)) < 0)
2224             return ret;
2225
2226         if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2227             return ret;
2228     }
2229
2230     return 0;
2231 }
2232
2233 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2234 {
2235     uint32_t atom_size, atom, atom_end;
2236     int search_range = 10;
2237
2238     while (search_range
2239            &&
2240            bytestream2_get_bytes_left(&s->g) >= 8) {
2241         atom_size = bytestream2_get_be32u(&s->g);
2242         atom      = bytestream2_get_be32u(&s->g);
2243         if (atom_size == 1) {
2244             if (bytestream2_get_be32u(&s->g)) {
2245                 avpriv_request_sample(s->avctx, "Huge atom");
2246                 return 0;
2247             }
2248             atom_size = bytestream2_get_be32u(&s->g);
2249             atom_end  = bytestream2_tell(&s->g) + atom_size - 16;
2250         } else {
2251             atom_end  = bytestream2_tell(&s->g) + atom_size -  8;
2252         }
2253
2254         if (atom == JP2_CODESTREAM)
2255             return 1;
2256
2257         if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2258             return 0;
2259
2260         if (atom == JP2_HEADER &&
2261                    atom_size >= 16) {
2262             uint32_t atom2_size, atom2, atom2_end;
2263             do {
2264                 atom2_size = bytestream2_get_be32u(&s->g);
2265                 atom2      = bytestream2_get_be32u(&s->g);
2266                 atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
2267                 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2268                     break;
2269                 atom2_size -= 8;
2270                 if (atom2 == JP2_CODESTREAM) {
2271                     return 1;
2272                 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2273                     int method = bytestream2_get_byteu(&s->g);
2274                     bytestream2_skipu(&s->g, 2);
2275                     if (method == 1) {
2276                         s->colour_space = bytestream2_get_be32u(&s->g);
2277                     }
2278                 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2279                     int i, size, colour_count, colour_channels, colour_depth[3];
2280                     colour_count = bytestream2_get_be16u(&s->g);
2281                     colour_channels = bytestream2_get_byteu(&s->g);
2282                     // FIXME: Do not ignore channel_sign
2283                     colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2284                     colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2285                     colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2286                     size = (colour_depth[0] + 7 >> 3) * colour_count +
2287                            (colour_depth[1] + 7 >> 3) * colour_count +
2288                            (colour_depth[2] + 7 >> 3) * colour_count;
2289                     if (colour_count > AVPALETTE_COUNT ||
2290                         colour_channels != 3 ||
2291                         colour_depth[0] > 16 ||
2292                         colour_depth[1] > 16 ||
2293                         colour_depth[2] > 16 ||
2294                         atom2_size < size) {
2295                         avpriv_request_sample(s->avctx, "Unknown palette");
2296                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2297                         continue;
2298                     }
2299                     s->pal8 = 1;
2300                     for (i = 0; i < colour_count; i++) {
2301                         uint32_t r, g, b;
2302                         if (colour_depth[0] <= 8) {
2303                             r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2304                             r |= r >> colour_depth[0];
2305                         } else {
2306                             r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2307                         }
2308                         if (colour_depth[1] <= 8) {
2309                             g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2310                             g |= g >> colour_depth[1];
2311                         } else {
2312                             g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2313                         }
2314                         if (colour_depth[2] <= 8) {
2315                             b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2316                             b |= b >> colour_depth[2];
2317                         } else {
2318                             b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2319                         }
2320                         s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2321                     }
2322                 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2323                     int n = bytestream2_get_be16u(&s->g);
2324                     for (; n>0; n--) {
2325                         int cn   = bytestream2_get_be16(&s->g);
2326                         int av_unused typ  = bytestream2_get_be16(&s->g);
2327                         int asoc = bytestream2_get_be16(&s->g);
2328                         if (cn < 4 && asoc < 4)
2329                             s->cdef[cn] = asoc;
2330                     }
2331                 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2332                     int64_t vnum, vden, hnum, hden, vexp, hexp;
2333                     uint32_t resx;
2334                     bytestream2_skip(&s->g, 4);
2335                     resx = bytestream2_get_be32u(&s->g);
2336                     if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2337                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2338                         continue;
2339                     }
2340                     vnum = bytestream2_get_be16u(&s->g);
2341                     vden = bytestream2_get_be16u(&s->g);
2342                     hnum = bytestream2_get_be16u(&s->g);
2343                     hden = bytestream2_get_be16u(&s->g);
2344                     vexp = bytestream2_get_byteu(&s->g);
2345                     hexp = bytestream2_get_byteu(&s->g);
2346                     if (!vnum || !vden || !hnum || !hden) {
2347                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2348                         av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2349                         continue;
2350                     }
2351                     if (vexp > hexp) {
2352                         vexp -= hexp;
2353                         hexp = 0;
2354                     } else {
2355                         hexp -= vexp;
2356                         vexp = 0;
2357                     }
2358                     if (   INT64_MAX / (hnum * vden) > pow(10, hexp)
2359                         && INT64_MAX / (vnum * hden) > pow(10, vexp))
2360                         av_reduce(&s->sar.den, &s->sar.num,
2361                                   hnum * vden * pow(10, hexp),
2362                                   vnum * hden * pow(10, vexp),
2363                                   INT32_MAX);
2364                 }
2365                 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2366             } while (atom_end - atom2_end >= 8);
2367         } else {
2368             search_range--;
2369         }
2370         bytestream2_seek(&s->g, atom_end, SEEK_SET);
2371     }
2372
2373     return 0;
2374 }
2375
2376 static av_cold void jpeg2000_init_static_data(void)
2377 {
2378     ff_jpeg2000_init_tier1_luts();
2379     ff_mqc_init_context_tables();
2380 }
2381
2382 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2383 {
2384     static AVOnce init_static_once = AV_ONCE_INIT;
2385     Jpeg2000DecoderContext *s = avctx->priv_data;
2386
2387     ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2388     ff_jpeg2000dsp_init(&s->dsp);
2389
2390     return 0;
2391 }
2392
2393 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2394                                  int *got_frame, AVPacket *avpkt)
2395 {
2396     Jpeg2000DecoderContext *s = avctx->priv_data;
2397     ThreadFrame frame = { .f = data };
2398     AVFrame *picture = data;
2399     int ret;
2400
2401     s->avctx     = avctx;
2402     bytestream2_init(&s->g, avpkt->data, avpkt->size);
2403     s->curtileno = -1;
2404     memset(s->cdef, -1, sizeof(s->cdef));
2405
2406     if (bytestream2_get_bytes_left(&s->g) < 2) {
2407         ret = AVERROR_INVALIDDATA;
2408         goto end;
2409     }
2410
2411     // check if the image is in jp2 format
2412     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2413        (bytestream2_get_be32u(&s->g) == 12) &&
2414        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2415        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2416         if (!jp2_find_codestream(s)) {
2417             av_log(avctx, AV_LOG_ERROR,
2418                    "Could not find Jpeg2000 codestream atom.\n");
2419             ret = AVERROR_INVALIDDATA;
2420             goto end;
2421         }
2422     } else {
2423         bytestream2_seek(&s->g, 0, SEEK_SET);
2424     }
2425
2426     while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2427         bytestream2_skip(&s->g, 1);
2428
2429     if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2430         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2431         ret = AVERROR_INVALIDDATA;
2432         goto end;
2433     }
2434     if (ret = jpeg2000_read_main_headers(s))
2435         goto end;
2436
2437     /* get picture buffer */
2438     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2439         goto end;
2440     picture->pict_type = AV_PICTURE_TYPE_I;
2441     picture->key_frame = 1;
2442
2443     if (ret = jpeg2000_read_bitstream_packets(s))
2444         goto end;
2445
2446     avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2447
2448     jpeg2000_dec_cleanup(s);
2449
2450     *got_frame = 1;
2451
2452     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2453         memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2454     if (s->sar.num && s->sar.den)
2455         avctx->sample_aspect_ratio = s->sar;
2456     s->sar.num = s->sar.den = 0;
2457
2458     return bytestream2_tell(&s->g);
2459
2460 end:
2461     jpeg2000_dec_cleanup(s);
2462     return ret;
2463 }
2464
2465 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2466 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2467
2468 static const AVOption options[] = {
2469     { "lowres",  "Lower the decoding resolution by a power of two",
2470         OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2471     { NULL },
2472 };
2473
2474 static const AVClass jpeg2000_class = {
2475     .class_name = "jpeg2000",
2476     .item_name  = av_default_item_name,
2477     .option     = options,
2478     .version    = LIBAVUTIL_VERSION_INT,
2479 };
2480
2481 AVCodec ff_jpeg2000_decoder = {
2482     .name             = "jpeg2000",
2483     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2484     .type             = AVMEDIA_TYPE_VIDEO,
2485     .id               = AV_CODEC_ID_JPEG2000,
2486     .capabilities     = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2487     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
2488     .init             = jpeg2000_decode_init,
2489     .decode           = jpeg2000_decode_frame,
2490     .priv_class       = &jpeg2000_class,
2491     .max_lowres       = 5,
2492     .profiles         = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)
2493 };