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