]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeg2000dec.c
avcodec/jpeg2000dec: clear pointer which become stale in get_ppt()
[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     memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
932     memcpy(tile->packed_headers + tile->packed_headers_size,
933            s->g.buffer, n - 3);
934     tile->packed_headers_size += n - 3;
935     bytestream2_skip(&s->g, n - 3);
936
937     return 0;
938 }
939
940 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
941 {
942     int compno;
943     int tilex = tileno % s->numXtiles;
944     int tiley = tileno / s->numXtiles;
945     Jpeg2000Tile *tile = s->tile + tileno;
946
947     if (!tile->comp)
948         return AVERROR(ENOMEM);
949
950     tile->coord[0][0] = av_clip(tilex       * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
951     tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width  + s->tile_offset_x, s->image_offset_x, s->width);
952     tile->coord[1][0] = av_clip(tiley       * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
953     tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
954
955     for (compno = 0; compno < s->ncomponents; compno++) {
956         Jpeg2000Component *comp = tile->comp + compno;
957         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
958         Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
959         int ret; // global bandno
960
961         comp->coord_o[0][0] = tile->coord[0][0];
962         comp->coord_o[0][1] = tile->coord[0][1];
963         comp->coord_o[1][0] = tile->coord[1][0];
964         comp->coord_o[1][1] = tile->coord[1][1];
965         if (compno) {
966             comp->coord_o[0][0] /= s->cdx[compno];
967             comp->coord_o[0][1] /= s->cdx[compno];
968             comp->coord_o[1][0] /= s->cdy[compno];
969             comp->coord_o[1][1] /= s->cdy[compno];
970         }
971
972         comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
973         comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
974         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
975         comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
976
977         if (!comp->roi_shift)
978             comp->roi_shift = s->roi_shift[compno];
979
980         if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
981                                              s->cbps[compno], s->cdx[compno],
982                                              s->cdy[compno], s->avctx))
983             return ret;
984     }
985     return 0;
986 }
987
988 /* Read the number of coding passes. */
989 static int getnpasses(Jpeg2000DecoderContext *s)
990 {
991     int num;
992     if (!get_bits(s, 1))
993         return 1;
994     if (!get_bits(s, 1))
995         return 2;
996     if ((num = get_bits(s, 2)) != 3)
997         return num < 0 ? num : 3 + num;
998     if ((num = get_bits(s, 5)) != 31)
999         return num < 0 ? num : 6 + num;
1000     num = get_bits(s, 7);
1001     return num < 0 ? num : 37 + num;
1002 }
1003
1004 static int getlblockinc(Jpeg2000DecoderContext *s)
1005 {
1006     int res = 0, ret;
1007     while (ret = get_bits(s, 1)) {
1008         if (ret < 0)
1009             return ret;
1010         res++;
1011     }
1012     return res;
1013 }
1014
1015 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1016                                  int *tp_index)
1017 {
1018     s->g = tile->tile_part[*tp_index].tpg;
1019     if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1020         if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1021             s->g = tile->tile_part[++(*tp_index)].tpg;
1022         }
1023     }
1024     if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1025         bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1026 }
1027
1028 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1029                                   Jpeg2000CodingStyle *codsty,
1030                                   Jpeg2000ResLevel *rlevel, int precno,
1031                                   int layno, uint8_t *expn, int numgbits)
1032 {
1033     int bandno, cblkno, ret, nb_code_blocks;
1034     int cwsno;
1035
1036     if (layno < rlevel->band[0].prec[precno].decoded_layers)
1037         return 0;
1038     rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1039     // Select stream to read from
1040     if (tile->has_ppt)
1041         s->g = tile->packed_headers_stream;
1042     else
1043         select_stream(s, tile, tp_index);
1044
1045     if (!(ret = get_bits(s, 1))) {
1046         jpeg2000_flush(s);
1047         goto skip_data;
1048     } else if (ret < 0)
1049         return ret;
1050
1051     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1052         Jpeg2000Band *band = rlevel->band + bandno;
1053         Jpeg2000Prec *prec = band->prec + precno;
1054
1055         if (band->coord[0][0] == band->coord[0][1] ||
1056             band->coord[1][0] == band->coord[1][1])
1057             continue;
1058         nb_code_blocks =  prec->nb_codeblocks_height *
1059                           prec->nb_codeblocks_width;
1060         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1061             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1062             int incl, newpasses, llen;
1063             void *tmp;
1064
1065             if (cblk->npasses)
1066                 incl = get_bits(s, 1);
1067             else
1068                 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1069             if (!incl)
1070                 continue;
1071             else if (incl < 0)
1072                 return incl;
1073
1074             if (!cblk->npasses) {
1075                 int v = expn[bandno] + numgbits - 1 -
1076                         tag_tree_decode(s, prec->zerobits + cblkno, 100);
1077                 if (v < 0 || v > 30) {
1078                     av_log(s->avctx, AV_LOG_ERROR,
1079                            "nonzerobits %d invalid or unsupported\n", v);
1080                     return AVERROR_INVALIDDATA;
1081                 }
1082                 cblk->nonzerobits = v;
1083             }
1084             if ((newpasses = getnpasses(s)) < 0)
1085                 return newpasses;
1086             av_assert2(newpasses > 0);
1087             if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1088                 avpriv_request_sample(s->avctx, "Too many passes");
1089                 return AVERROR_PATCHWELCOME;
1090             }
1091             if ((llen = getlblockinc(s)) < 0)
1092                 return llen;
1093             if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1094                 avpriv_request_sample(s->avctx,
1095                                       "Block with length beyond 16 bits");
1096                 return AVERROR_PATCHWELCOME;
1097             }
1098
1099             cblk->lblock += llen;
1100
1101             cblk->nb_lengthinc = 0;
1102             cblk->nb_terminationsinc = 0;
1103             av_free(cblk->lengthinc);
1104             cblk->lengthinc  = av_mallocz_array(newpasses    , sizeof(*cblk->lengthinc));
1105             if (!cblk->lengthinc)
1106                 return AVERROR(ENOMEM);
1107             tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1108             if (!tmp)
1109                 return AVERROR(ENOMEM);
1110             cblk->data_start = tmp;
1111             do {
1112                 int newpasses1 = 0;
1113
1114                 while (newpasses1 < newpasses) {
1115                     newpasses1 ++;
1116                     if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1117                         cblk->nb_terminationsinc ++;
1118                         break;
1119                     }
1120                 }
1121
1122                 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1123                     return ret;
1124                 if (ret > cblk->data_allocated) {
1125                     size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1126                     void *new = av_realloc(cblk->data, new_size);
1127                     if (new) {
1128                         cblk->data = new;
1129                         cblk->data_allocated = new_size;
1130                     }
1131                 }
1132                 if (ret > cblk->data_allocated) {
1133                     avpriv_request_sample(s->avctx,
1134                                         "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1135                                         cblk->data_allocated);
1136                     return AVERROR_PATCHWELCOME;
1137                 }
1138                 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1139                 cblk->npasses  += newpasses1;
1140                 newpasses -= newpasses1;
1141             } while(newpasses);
1142         }
1143     }
1144     jpeg2000_flush(s);
1145
1146     if (codsty->csty & JPEG2000_CSTY_EPH) {
1147         if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1148             bytestream2_skip(&s->g, 2);
1149         else
1150             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1151     }
1152
1153     // Save state of stream
1154     if (tile->has_ppt) {
1155         tile->packed_headers_stream = s->g;
1156         select_stream(s, tile, tp_index);
1157     }
1158     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1159         Jpeg2000Band *band = rlevel->band + bandno;
1160         Jpeg2000Prec *prec = band->prec + precno;
1161
1162         nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1163         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1164             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1165             if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1166                 continue;
1167             for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1168                 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1169                     size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1170                     void *new = av_realloc(cblk->data, new_size);
1171                     if (new) {
1172                         cblk->data = new;
1173                         cblk->data_allocated = new_size;
1174                     }
1175                 }
1176                 if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1177                     || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1178                 ) {
1179                     av_log(s->avctx, AV_LOG_ERROR,
1180                         "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1181                         cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1182                     return AVERROR_INVALIDDATA;
1183                 }
1184
1185                 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1186                 cblk->length   += cblk->lengthinc[cwsno];
1187                 cblk->lengthinc[cwsno] = 0;
1188                 if (cblk->nb_terminationsinc) {
1189                     cblk->nb_terminationsinc--;
1190                     cblk->nb_terminations++;
1191                     cblk->data[cblk->length++] = 0xFF;
1192                     cblk->data[cblk->length++] = 0xFF;
1193                     cblk->data_start[cblk->nb_terminations] = cblk->length;
1194                 }
1195             }
1196             av_freep(&cblk->lengthinc);
1197         }
1198     }
1199     // Save state of stream
1200     tile->tile_part[*tp_index].tpg = s->g;
1201     return 0;
1202
1203 skip_data:
1204     if (tile->has_ppt)
1205         tile->packed_headers_stream = s->g;
1206     else
1207         tile->tile_part[*tp_index].tpg = s->g;
1208     return 0;
1209 }
1210
1211 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1212                                              int RSpoc, int CSpoc,
1213                                              int LYEpoc, int REpoc, int CEpoc,
1214                                              int Ppoc, int *tp_index)
1215 {
1216     int ret = 0;
1217     int layno, reslevelno, compno, precno, ok_reslevel;
1218     int x, y;
1219     int step_x, step_y;
1220
1221     switch (Ppoc) {
1222     case JPEG2000_PGOD_RLCP:
1223         av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1224         ok_reslevel = 1;
1225         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1226             ok_reslevel = 0;
1227             for (layno = 0; layno < LYEpoc; layno++) {
1228                 for (compno = CSpoc; compno < CEpoc; compno++) {
1229                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1230                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1231                     if (reslevelno < codsty->nreslevels) {
1232                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1233                                                 reslevelno;
1234                         ok_reslevel = 1;
1235                         for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1236                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1237                                                               codsty, rlevel,
1238                                                               precno, layno,
1239                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1240                                                               qntsty->nguardbits)) < 0)
1241                                 return ret;
1242                     }
1243                 }
1244             }
1245         }
1246         break;
1247
1248     case JPEG2000_PGOD_LRCP:
1249         av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1250         for (layno = 0; layno < LYEpoc; layno++) {
1251             ok_reslevel = 1;
1252             for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1253                 ok_reslevel = 0;
1254                 for (compno = CSpoc; compno < CEpoc; compno++) {
1255                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1256                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1257                     if (reslevelno < codsty->nreslevels) {
1258                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1259                                                 reslevelno;
1260                         ok_reslevel = 1;
1261                         for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1262                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1263                                                               codsty, rlevel,
1264                                                               precno, layno,
1265                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1266                                                               qntsty->nguardbits)) < 0)
1267                                 return ret;
1268                     }
1269                 }
1270             }
1271         }
1272         break;
1273
1274     case JPEG2000_PGOD_CPRL:
1275         av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1276         for (compno = CSpoc; compno < CEpoc; compno++) {
1277             Jpeg2000Component *comp     = tile->comp + compno;
1278             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1279             Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1280             step_x = 32;
1281             step_y = 32;
1282
1283             if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1284                 continue;
1285
1286             for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1287                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1288                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1289                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1290                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1291             }
1292             if (step_x >= 31 || step_y >= 31){
1293                 avpriv_request_sample(s->avctx, "CPRL with large step");
1294                 return AVERROR_PATCHWELCOME;
1295             }
1296             step_x = 1<<step_x;
1297             step_y = 1<<step_y;
1298
1299             for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1300                 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1301                     for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1302                         unsigned prcx, prcy;
1303                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1304                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1305                         int xc = x / s->cdx[compno];
1306                         int yc = y / s->cdy[compno];
1307
1308                         if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1309                             continue;
1310
1311                         if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1312                             continue;
1313
1314                         // check if a precinct exists
1315                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1316                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1317                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1318                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1319
1320                         precno = prcx + rlevel->num_precincts_x * prcy;
1321
1322                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1323                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1324                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1325                             continue;
1326                         }
1327
1328                         for (layno = 0; layno < LYEpoc; layno++) {
1329                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1330                                                               precno, layno,
1331                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1332                                                               qntsty->nguardbits)) < 0)
1333                                 return ret;
1334                         }
1335                     }
1336                 }
1337             }
1338         }
1339         break;
1340
1341     case JPEG2000_PGOD_RPCL:
1342         av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1343         ok_reslevel = 1;
1344         for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1345             ok_reslevel = 0;
1346             step_x = 30;
1347             step_y = 30;
1348             for (compno = CSpoc; compno < CEpoc; compno++) {
1349                 Jpeg2000Component *comp     = tile->comp + compno;
1350                 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1351
1352                 if (reslevelno < codsty->nreslevels) {
1353                     uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1354                     Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1355                     step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1356                     step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1357                 }
1358             }
1359             step_x = 1<<step_x;
1360             step_y = 1<<step_y;
1361
1362             for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1363                 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1364                     for (compno = CSpoc; compno < CEpoc; compno++) {
1365                         Jpeg2000Component *comp     = tile->comp + compno;
1366                         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1367                         Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1368                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1369                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1370                         unsigned prcx, prcy;
1371
1372                         int xc = x / s->cdx[compno];
1373                         int yc = y / s->cdy[compno];
1374
1375                         if (reslevelno >= codsty->nreslevels)
1376                             continue;
1377
1378                         if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1379                             continue;
1380
1381                         if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1382                             continue;
1383
1384                         // check if a precinct exists
1385                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1386                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1387                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1388                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1389
1390                         precno = prcx + rlevel->num_precincts_x * prcy;
1391
1392                         ok_reslevel = 1;
1393                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1394                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1395                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1396                             continue;
1397                         }
1398
1399                         for (layno = 0; layno < LYEpoc; layno++) {
1400                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1401                                                               codsty, rlevel,
1402                                                               precno, layno,
1403                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1404                                                               qntsty->nguardbits)) < 0)
1405                                 return ret;
1406                         }
1407                     }
1408                 }
1409             }
1410         }
1411         break;
1412
1413     case JPEG2000_PGOD_PCRL:
1414         av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1415         step_x = 32;
1416         step_y = 32;
1417         for (compno = CSpoc; compno < CEpoc; compno++) {
1418             Jpeg2000Component *comp     = tile->comp + compno;
1419             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1420
1421             for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1422                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1423                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1424                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1425                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1426             }
1427         }
1428         if (step_x >= 31 || step_y >= 31){
1429             avpriv_request_sample(s->avctx, "PCRL with large step");
1430             return AVERROR_PATCHWELCOME;
1431         }
1432         step_x = 1<<step_x;
1433         step_y = 1<<step_y;
1434
1435         for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1436             for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1437                 for (compno = CSpoc; compno < CEpoc; compno++) {
1438                     Jpeg2000Component *comp     = tile->comp + compno;
1439                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1440                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
1441                     int xc = x / s->cdx[compno];
1442                     int yc = y / s->cdy[compno];
1443
1444                     for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1445                         unsigned prcx, prcy;
1446                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1447                         Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1448
1449                         if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1450                             continue;
1451
1452                         if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1453                             continue;
1454
1455                         // check if a precinct exists
1456                         prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1457                         prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1458                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1459                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1460
1461                         precno = prcx + rlevel->num_precincts_x * prcy;
1462
1463                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1464                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1465                                    prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1466                             continue;
1467                         }
1468
1469                         for (layno = 0; layno < LYEpoc; layno++) {
1470                             if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1471                                                               precno, layno,
1472                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1473                                                               qntsty->nguardbits)) < 0)
1474                                 return ret;
1475                         }
1476                     }
1477                 }
1478             }
1479         }
1480         break;
1481
1482     default:
1483         break;
1484     }
1485
1486     return ret;
1487 }
1488
1489 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1490 {
1491     int ret = AVERROR_BUG;
1492     int i;
1493     int tp_index = 0;
1494
1495     s->bit_index = 8;
1496     if (tile->poc.nb_poc) {
1497         for (i=0; i<tile->poc.nb_poc; i++) {
1498             Jpeg2000POCEntry *e = &tile->poc.poc[i];
1499             ret = jpeg2000_decode_packets_po_iteration(s, tile,
1500                 e->RSpoc, e->CSpoc,
1501                 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1502                 e->REpoc,
1503                 FFMIN(e->CEpoc, s->ncomponents),
1504                 e->Ppoc, &tp_index
1505                 );
1506             if (ret < 0)
1507                 return ret;
1508         }
1509     } else {
1510         ret = jpeg2000_decode_packets_po_iteration(s, tile,
1511             0, 0,
1512             tile->codsty[0].nlayers,
1513             33,
1514             s->ncomponents,
1515             tile->codsty[0].prog_order,
1516             &tp_index
1517         );
1518     }
1519     /* EOC marker reached */
1520     bytestream2_skip(&s->g, 2);
1521
1522     return ret;
1523 }
1524
1525 /* TIER-1 routines */
1526 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1527                            int bpno, int bandno,
1528                            int vert_causal_ctx_csty_symbol)
1529 {
1530     int mask = 3 << (bpno - 1), y0, x, y;
1531
1532     for (y0 = 0; y0 < height; y0 += 4)
1533         for (x = 0; x < width; x++)
1534             for (y = y0; y < height && y < y0 + 4; y++) {
1535                 int flags_mask = -1;
1536                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1537                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1538                 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1539                 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1540                     if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1541                         int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1542                         if (t1->mqc.raw)
1543                              t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1544                         else
1545                              t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1546                                                -mask : mask;
1547
1548                         ff_jpeg2000_set_significance(t1, x, y,
1549                                                      t1->data[(y) * t1->stride + x] < 0);
1550                     }
1551                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1552                 }
1553             }
1554 }
1555
1556 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1557                            int bpno, int vert_causal_ctx_csty_symbol)
1558 {
1559     int phalf, nhalf;
1560     int y0, x, y;
1561
1562     phalf = 1 << (bpno - 1);
1563     nhalf = -phalf;
1564
1565     for (y0 = 0; y0 < height; y0 += 4)
1566         for (x = 0; x < width; x++)
1567             for (y = y0; y < height && y < y0 + 4; y++)
1568                 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1569                     int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1570                         ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1571                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1572                     int r     = ff_mqc_decode(&t1->mqc,
1573                                               t1->mqc.cx_states + ctxno)
1574                                 ? phalf : nhalf;
1575                     t1->data[(y) * t1->stride + x]          += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1576                     t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1577                 }
1578 }
1579
1580 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1581                            int width, int height, int bpno, int bandno,
1582                            int seg_symbols, int vert_causal_ctx_csty_symbol)
1583 {
1584     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1585
1586     for (y0 = 0; y0 < height; y0 += 4) {
1587         for (x = 0; x < width; x++) {
1588             int flags_mask = -1;
1589             if (vert_causal_ctx_csty_symbol)
1590                 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1591             if (y0 + 3 < height &&
1592                 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1593                   (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1594                   (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1595                   (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1596                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1597                     continue;
1598                 runlen = ff_mqc_decode(&t1->mqc,
1599                                        t1->mqc.cx_states + MQC_CX_UNI);
1600                 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1601                                                        t1->mqc.cx_states +
1602                                                        MQC_CX_UNI);
1603                 dec = 1;
1604             } else {
1605                 runlen = 0;
1606                 dec    = 0;
1607             }
1608
1609             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1610                 int flags_mask = -1;
1611                 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1612                     flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1613                 if (!dec) {
1614                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1615                         dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1616                                                                                              bandno));
1617                     }
1618                 }
1619                 if (dec) {
1620                     int xorbit;
1621                     int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1622                                                         &xorbit);
1623                     t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1624                                                     t1->mqc.cx_states + ctxno) ^
1625                                       xorbit)
1626                                      ? -mask : mask;
1627                     ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1628                 }
1629                 dec = 0;
1630                 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1631             }
1632         }
1633     }
1634     if (seg_symbols) {
1635         int val;
1636         val = 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         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1640         if (val != 0xa)
1641             av_log(s->avctx, AV_LOG_ERROR,
1642                    "Segmentation symbol value incorrect\n");
1643     }
1644 }
1645
1646 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1647                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1648                        int width, int height, int bandpos, uint8_t roi_shift)
1649 {
1650     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1651     int pass_cnt = 0;
1652     int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1653     int term_cnt = 0;
1654     int coder_type;
1655
1656     av_assert0(width <= 1024U && height <= 1024U);
1657     av_assert0(width*height <= 4096);
1658
1659     memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1660
1661     /* If code-block contains no compressed data: nothing to do. */
1662     if (!cblk->length)
1663         return 0;
1664
1665     memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1666
1667     cblk->data[cblk->length] = 0xff;
1668     cblk->data[cblk->length+1] = 0xff;
1669     ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1670
1671     while (passno--) {
1672         if (bpno < 0) {
1673             av_log(s->avctx, AV_LOG_ERROR, "bpno became negative\n");
1674             return AVERROR_INVALIDDATA;
1675         }
1676         switch(pass_t) {
1677         case 0:
1678             decode_sigpass(t1, width, height, bpno + 1, bandpos,
1679                            vert_causal_ctx_csty_symbol);
1680             break;
1681         case 1:
1682             decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1683             break;
1684         case 2:
1685             av_assert2(!t1->mqc.raw);
1686             decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1687                            codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1688                            vert_causal_ctx_csty_symbol);
1689             break;
1690         }
1691         if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1692             ff_mqc_init_contexts(&t1->mqc);
1693
1694         if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1695             if (term_cnt >= cblk->nb_terminations) {
1696                 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1697                 return AVERROR_INVALIDDATA;
1698             }
1699             if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
1700                 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1701                     cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1702                     pass_cnt, cblk->npasses);
1703             }
1704
1705             ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1706         }
1707
1708         pass_t++;
1709         if (pass_t == 3) {
1710             bpno--;
1711             pass_t = 0;
1712         }
1713         pass_cnt ++;
1714     }
1715
1716     if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != t1->mqc.bp) {
1717         av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1718                cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) - t1->mqc.bp);
1719     }
1720
1721     return 1;
1722 }
1723
1724 static inline int roi_shift_param(Jpeg2000Component *comp,
1725                                    int quan_parameter)
1726 {
1727     uint8_t roi_shift;
1728     int val;
1729     roi_shift = comp->roi_shift;
1730     val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1731
1732     if (val > (1 << roi_shift))
1733         return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1734     return quan_parameter;
1735 }
1736
1737 /* TODO: Verify dequantization for lossless case
1738  * comp->data can be float or int
1739  * band->stepsize can be float or int
1740  * depending on the type of DWT transformation.
1741  * see ISO/IEC 15444-1:2002 A.6.1 */
1742
1743 /* Float dequantization of a codeblock.*/
1744 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1745                                  Jpeg2000Component *comp,
1746                                  Jpeg2000T1Context *t1, Jpeg2000Band *band)
1747 {
1748     int i, j;
1749     int w = cblk->coord[0][1] - cblk->coord[0][0];
1750     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1751         float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1752         int *src = t1->data + j*t1->stride;
1753         for (i = 0; i < w; ++i)
1754             datap[i] = src[i] * band->f_stepsize;
1755     }
1756 }
1757
1758 /* Integer dequantization of a codeblock.*/
1759 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1760                                Jpeg2000Component *comp,
1761                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1762 {
1763     int i, j;
1764     int w = cblk->coord[0][1] - cblk->coord[0][0];
1765     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1766         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1767         int *src = t1->data + j*t1->stride;
1768         if (band->i_stepsize == 32768) {
1769             for (i = 0; i < w; ++i)
1770                 datap[i] = src[i] / 2;
1771         } else {
1772             // This should be VERY uncommon
1773             for (i = 0; i < w; ++i)
1774                 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1775         }
1776     }
1777 }
1778
1779 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1780                                Jpeg2000Component *comp,
1781                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1782 {
1783     int i, j;
1784     int w = cblk->coord[0][1] - cblk->coord[0][0];
1785     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1786         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1787         int *src = t1->data + j*t1->stride;
1788         for (i = 0; i < w; ++i)
1789             datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1790     }
1791 }
1792
1793 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1794 {
1795     int i, csize = 1;
1796     void *src[3];
1797
1798     for (i = 1; i < 3; i++) {
1799         if (tile->codsty[0].transform != tile->codsty[i].transform) {
1800             av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1801             return;
1802         }
1803         if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1804             av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1805             return;
1806         }
1807     }
1808
1809     for (i = 0; i < 3; i++)
1810         if (tile->codsty[0].transform == FF_DWT97)
1811             src[i] = tile->comp[i].f_data;
1812         else
1813             src[i] = tile->comp[i].i_data;
1814
1815     for (i = 0; i < 2; i++)
1816         csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1817
1818     s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1819 }
1820
1821 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1822                                   Jpeg2000Component *comp,
1823                                   Jpeg2000T1Context *t1)
1824 {
1825     int i, j;
1826     int w = cblk->coord[0][1] - cblk->coord[0][0];
1827     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1828         int *src = t1->data + j*t1->stride;
1829         for (i = 0; i < w; ++i)
1830             src[i] = roi_shift_param(comp, src[i]);
1831     }
1832 }
1833
1834 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1835 {
1836     Jpeg2000T1Context t1;
1837
1838     int compno, reslevelno, bandno;
1839
1840     /* Loop on tile components */
1841     for (compno = 0; compno < s->ncomponents; compno++) {
1842         Jpeg2000Component *comp     = tile->comp + compno;
1843         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1844         int coded = 0;
1845
1846         t1.stride = (1<<codsty->log2_cblk_width) + 2;
1847
1848         /* Loop on resolution levels */
1849         for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1850             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1851             /* Loop on bands */
1852             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1853                 int nb_precincts, precno;
1854                 Jpeg2000Band *band = rlevel->band + bandno;
1855                 int cblkno = 0, bandpos;
1856
1857                 bandpos = bandno + (reslevelno > 0);
1858
1859                 if (band->coord[0][0] == band->coord[0][1] ||
1860                     band->coord[1][0] == band->coord[1][1])
1861                     continue;
1862
1863                 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1864                 /* Loop on precincts */
1865                 for (precno = 0; precno < nb_precincts; precno++) {
1866                     Jpeg2000Prec *prec = band->prec + precno;
1867
1868                     /* Loop on codeblocks */
1869                     for (cblkno = 0;
1870                          cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1871                          cblkno++) {
1872                         int x, y;
1873                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1874                         int ret = decode_cblk(s, codsty, &t1, cblk,
1875                                     cblk->coord[0][1] - cblk->coord[0][0],
1876                                     cblk->coord[1][1] - cblk->coord[1][0],
1877                                     bandpos, comp->roi_shift);
1878                         if (ret)
1879                             coded = 1;
1880                         else
1881                             continue;
1882                         x = cblk->coord[0][0] - band->coord[0][0];
1883                         y = cblk->coord[1][0] - band->coord[1][0];
1884
1885                         if (comp->roi_shift)
1886                             roi_scale_cblk(cblk, comp, &t1);
1887                         if (codsty->transform == FF_DWT97)
1888                             dequantization_float(x, y, cblk, comp, &t1, band);
1889                         else if (codsty->transform == FF_DWT97_INT)
1890                             dequantization_int_97(x, y, cblk, comp, &t1, band);
1891                         else
1892                             dequantization_int(x, y, cblk, comp, &t1, band);
1893                    } /* end cblk */
1894                 } /*end prec */
1895             } /* end band */
1896         } /* end reslevel */
1897
1898         /* inverse DWT */
1899         if (coded)
1900             ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1901
1902     } /*end comp */
1903 }
1904
1905 #define WRITE_FRAME(D, PIXEL)                                                                     \
1906     static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,         \
1907                                          AVFrame * picture, int precision)                        \
1908     {                                                                                             \
1909         const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);               \
1910         int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);                              \
1911         int pixelsize = planar ? 1 : pixdesc->nb_components;                                      \
1912                                                                                                   \
1913         int compno;                                                                               \
1914         int x, y;                                                                                 \
1915                                                                                                   \
1916         for (compno = 0; compno < s->ncomponents; compno++) {                                     \
1917             Jpeg2000Component *comp     = tile->comp + compno;                                    \
1918             Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \
1919             PIXEL *line;                                                                          \
1920             float *datap     = comp->f_data;                                                      \
1921             int32_t *i_datap = comp->i_data;                                                      \
1922             int cbps         = s->cbps[compno];                                                   \
1923             int w            = tile->comp[compno].coord[0][1] - s->image_offset_x;                \
1924             int plane        = 0;                                                                 \
1925                                                                                                   \
1926             if (planar)                                                                           \
1927                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);                 \
1928                                                                                                   \
1929             y    = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];           \
1930             line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
1931             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) {                 \
1932                 PIXEL *dst;                                                                       \
1933                                                                                                   \
1934                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];        \
1935                 dst = line + x * pixelsize + compno*!planar;                                      \
1936                                                                                                   \
1937                 if (codsty->transform == FF_DWT97) {                                              \
1938                     for (; x < w; x++) {                                                          \
1939                         int val = lrintf(*datap) + (1 << (cbps - 1));                             \
1940                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1941                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1942                         *dst = val << (precision - cbps);                                         \
1943                         datap++;                                                                  \
1944                         dst += pixelsize;                                                         \
1945                     }                                                                             \
1946                 } else {                                                                          \
1947                     for (; x < w; x++) {                                                          \
1948                         int val = *i_datap + (1 << (cbps - 1));                                   \
1949                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1950                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1951                         *dst = val << (precision - cbps);                                         \
1952                         i_datap++;                                                                \
1953                         dst += pixelsize;                                                         \
1954                     }                                                                             \
1955                 }                                                                                 \
1956                 line += picture->linesize[plane] / sizeof(PIXEL);                                 \
1957             }                                                                                     \
1958         }                                                                                         \
1959                                                                                                   \
1960     }
1961
1962 WRITE_FRAME(8, uint8_t)
1963 WRITE_FRAME(16, uint16_t)
1964
1965 #undef WRITE_FRAME
1966
1967 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
1968                                 int jobnr, int threadnr)
1969 {
1970     Jpeg2000DecoderContext *s = avctx->priv_data;
1971     AVFrame *picture = td;
1972     Jpeg2000Tile *tile = s->tile + jobnr;
1973     int x;
1974
1975     tile_codeblocks(s, tile);
1976
1977     /* inverse MCT transformation */
1978     if (tile->codsty[0].mct)
1979         mct_decode(s, tile);
1980
1981     for (x = 0; x < s->ncomponents; x++) {
1982         if (s->cdef[x] < 0) {
1983             for (x = 0; x < s->ncomponents; x++) {
1984                 s->cdef[x] = x + 1;
1985             }
1986             if ((s->ncomponents & 1) == 0)
1987                 s->cdef[s->ncomponents-1] = 0;
1988             break;
1989         }
1990     }
1991
1992     if (s->precision <= 8) {
1993         write_frame_8(s, tile, picture, 8);
1994     } else {
1995         int precision = picture->format == AV_PIX_FMT_XYZ12 ||
1996                         picture->format == AV_PIX_FMT_RGB48 ||
1997                         picture->format == AV_PIX_FMT_RGBA64 ||
1998                         picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
1999
2000         write_frame_16(s, tile, picture, precision);
2001     }
2002
2003     return 0;
2004 }
2005
2006 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2007 {
2008     int tileno, compno;
2009     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2010         if (s->tile[tileno].comp) {
2011             for (compno = 0; compno < s->ncomponents; compno++) {
2012                 Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
2013                 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2014
2015                 ff_jpeg2000_cleanup(comp, codsty);
2016             }
2017             av_freep(&s->tile[tileno].comp);
2018             av_freep(&s->tile[tileno].packed_headers);
2019             s->tile[tileno].packed_headers_size = 0;
2020         }
2021     }
2022     av_freep(&s->tile);
2023     memset(s->codsty, 0, sizeof(s->codsty));
2024     memset(s->qntsty, 0, sizeof(s->qntsty));
2025     memset(s->properties, 0, sizeof(s->properties));
2026     memset(&s->poc  , 0, sizeof(s->poc));
2027     s->numXtiles = s->numYtiles = 0;
2028     s->ncomponents = 0;
2029 }
2030
2031 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2032 {
2033     Jpeg2000CodingStyle *codsty = s->codsty;
2034     Jpeg2000QuantStyle *qntsty  = s->qntsty;
2035     Jpeg2000POC         *poc    = &s->poc;
2036     uint8_t *properties         = s->properties;
2037
2038     for (;;) {
2039         int len, ret = 0;
2040         uint16_t marker;
2041         int oldpos;
2042
2043         if (bytestream2_get_bytes_left(&s->g) < 2) {
2044             av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2045             break;
2046         }
2047
2048         marker = bytestream2_get_be16u(&s->g);
2049         oldpos = bytestream2_tell(&s->g);
2050
2051         if (marker == JPEG2000_SOD) {
2052             Jpeg2000Tile *tile;
2053             Jpeg2000TilePart *tp;
2054
2055             if (!s->tile) {
2056                 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2057                 return AVERROR_INVALIDDATA;
2058             }
2059             if (s->curtileno < 0) {
2060                 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2061                 return AVERROR_INVALIDDATA;
2062             }
2063
2064             tile = s->tile + s->curtileno;
2065             tp = tile->tile_part + tile->tp_idx;
2066             if (tp->tp_end < s->g.buffer) {
2067                 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2068                 return AVERROR_INVALIDDATA;
2069             }
2070
2071             if (tile->has_ppt && tile->tp_idx == 0) {
2072                 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2073             }
2074
2075             bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2076             bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2077
2078             continue;
2079         }
2080         if (marker == JPEG2000_EOC)
2081             break;
2082
2083         len = bytestream2_get_be16(&s->g);
2084         if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2085             if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2086                 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2087                 return AVERROR_INVALIDDATA;
2088             }
2089             av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2090             break;
2091         }
2092
2093         switch (marker) {
2094         case JPEG2000_SIZ:
2095             if (s->ncomponents) {
2096                 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2097                 return AVERROR_INVALIDDATA;
2098             }
2099             ret = get_siz(s);
2100             if (!s->tile)
2101                 s->numXtiles = s->numYtiles = 0;
2102             break;
2103         case JPEG2000_COC:
2104             ret = get_coc(s, codsty, properties);
2105             break;
2106         case JPEG2000_COD:
2107             ret = get_cod(s, codsty, properties);
2108             break;
2109         case JPEG2000_RGN:
2110             ret = get_rgn(s, len);
2111             break;
2112         case JPEG2000_QCC:
2113             ret = get_qcc(s, len, qntsty, properties);
2114             break;
2115         case JPEG2000_QCD:
2116             ret = get_qcd(s, len, qntsty, properties);
2117             break;
2118         case JPEG2000_POC:
2119             ret = get_poc(s, len, poc);
2120             break;
2121         case JPEG2000_SOT:
2122             if (!(ret = get_sot(s, len))) {
2123                 av_assert1(s->curtileno >= 0);
2124                 codsty = s->tile[s->curtileno].codsty;
2125                 qntsty = s->tile[s->curtileno].qntsty;
2126                 poc    = &s->tile[s->curtileno].poc;
2127                 properties = s->tile[s->curtileno].properties;
2128             }
2129             break;
2130         case JPEG2000_PLM:
2131             // the PLM marker is ignored
2132         case JPEG2000_COM:
2133             // the comment is ignored
2134             bytestream2_skip(&s->g, len - 2);
2135             break;
2136         case JPEG2000_CRG:
2137             ret = read_crg(s, len);
2138             break;
2139         case JPEG2000_TLM:
2140             // Tile-part lengths
2141             ret = get_tlm(s, len);
2142             break;
2143         case JPEG2000_PLT:
2144             // Packet length, tile-part header
2145             ret = get_plt(s, len);
2146             break;
2147         case JPEG2000_PPT:
2148             // Packed headers, tile-part header
2149             ret = get_ppt(s, len);
2150             break;
2151         default:
2152             av_log(s->avctx, AV_LOG_ERROR,
2153                    "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2154                    marker, bytestream2_tell(&s->g) - 4);
2155             bytestream2_skip(&s->g, len - 2);
2156             break;
2157         }
2158         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2159             av_log(s->avctx, AV_LOG_ERROR,
2160                    "error during processing marker segment %.4"PRIx16"\n",
2161                    marker);
2162             return ret ? ret : -1;
2163         }
2164     }
2165     return 0;
2166 }
2167
2168 /* Read bit stream packets --> T2 operation. */
2169 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2170 {
2171     int ret = 0;
2172     int tileno;
2173
2174     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2175         Jpeg2000Tile *tile = s->tile + tileno;
2176
2177         if ((ret = init_tile(s, tileno)) < 0)
2178             return ret;
2179
2180         if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2181             return ret;
2182     }
2183
2184     return 0;
2185 }
2186
2187 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2188 {
2189     uint32_t atom_size, atom, atom_end;
2190     int search_range = 10;
2191
2192     while (search_range
2193            &&
2194            bytestream2_get_bytes_left(&s->g) >= 8) {
2195         atom_size = bytestream2_get_be32u(&s->g);
2196         atom      = bytestream2_get_be32u(&s->g);
2197         if (atom_size == 1) {
2198             if (bytestream2_get_be32u(&s->g)) {
2199                 avpriv_request_sample(s->avctx, "Huge atom");
2200                 return 0;
2201             }
2202             atom_size = bytestream2_get_be32u(&s->g);
2203             atom_end  = bytestream2_tell(&s->g) + atom_size - 16;
2204         } else {
2205             atom_end  = bytestream2_tell(&s->g) + atom_size -  8;
2206         }
2207
2208         if (atom == JP2_CODESTREAM)
2209             return 1;
2210
2211         if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2212             return 0;
2213
2214         if (atom == JP2_HEADER &&
2215                    atom_size >= 16) {
2216             uint32_t atom2_size, atom2, atom2_end;
2217             do {
2218                 atom2_size = bytestream2_get_be32u(&s->g);
2219                 atom2      = bytestream2_get_be32u(&s->g);
2220                 atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
2221                 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2222                     break;
2223                 atom2_size -= 8;
2224                 if (atom2 == JP2_CODESTREAM) {
2225                     return 1;
2226                 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2227                     int method = bytestream2_get_byteu(&s->g);
2228                     bytestream2_skipu(&s->g, 2);
2229                     if (method == 1) {
2230                         s->colour_space = bytestream2_get_be32u(&s->g);
2231                     }
2232                 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2233                     int i, size, colour_count, colour_channels, colour_depth[3];
2234                     colour_count = bytestream2_get_be16u(&s->g);
2235                     colour_channels = bytestream2_get_byteu(&s->g);
2236                     // FIXME: Do not ignore channel_sign
2237                     colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2238                     colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2239                     colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2240                     size = (colour_depth[0] + 7 >> 3) * colour_count +
2241                            (colour_depth[1] + 7 >> 3) * colour_count +
2242                            (colour_depth[2] + 7 >> 3) * colour_count;
2243                     if (colour_count > AVPALETTE_COUNT ||
2244                         colour_channels != 3 ||
2245                         colour_depth[0] > 16 ||
2246                         colour_depth[1] > 16 ||
2247                         colour_depth[2] > 16 ||
2248                         atom2_size < size) {
2249                         avpriv_request_sample(s->avctx, "Unknown palette");
2250                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2251                         continue;
2252                     }
2253                     s->pal8 = 1;
2254                     for (i = 0; i < colour_count; i++) {
2255                         uint32_t r, g, b;
2256                         if (colour_depth[0] <= 8) {
2257                             r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2258                             r |= r >> colour_depth[0];
2259                         } else {
2260                             r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2261                         }
2262                         if (colour_depth[1] <= 8) {
2263                             g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2264                             g |= g >> colour_depth[1];
2265                         } else {
2266                             g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2267                         }
2268                         if (colour_depth[2] <= 8) {
2269                             b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2270                             b |= b >> colour_depth[2];
2271                         } else {
2272                             b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2273                         }
2274                         s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2275                     }
2276                 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2277                     int n = bytestream2_get_be16u(&s->g);
2278                     for (; n>0; n--) {
2279                         int cn   = bytestream2_get_be16(&s->g);
2280                         int av_unused typ  = bytestream2_get_be16(&s->g);
2281                         int asoc = bytestream2_get_be16(&s->g);
2282                         if (cn < 4 && asoc < 4)
2283                             s->cdef[cn] = asoc;
2284                     }
2285                 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2286                     int64_t vnum, vden, hnum, hden, vexp, hexp;
2287                     uint32_t resx;
2288                     bytestream2_skip(&s->g, 4);
2289                     resx = bytestream2_get_be32u(&s->g);
2290                     if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2291                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2292                         continue;
2293                     }
2294                     vnum = bytestream2_get_be16u(&s->g);
2295                     vden = bytestream2_get_be16u(&s->g);
2296                     hnum = bytestream2_get_be16u(&s->g);
2297                     hden = bytestream2_get_be16u(&s->g);
2298                     vexp = bytestream2_get_byteu(&s->g);
2299                     hexp = bytestream2_get_byteu(&s->g);
2300                     if (!vnum || !vden || !hnum || !hden) {
2301                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2302                         av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2303                         continue;
2304                     }
2305                     if (vexp > hexp) {
2306                         vexp -= hexp;
2307                         hexp = 0;
2308                     } else {
2309                         hexp -= vexp;
2310                         vexp = 0;
2311                     }
2312                     if (   INT64_MAX / (hnum * vden) > pow(10, hexp)
2313                         && INT64_MAX / (vnum * hden) > pow(10, vexp))
2314                         av_reduce(&s->sar.den, &s->sar.num,
2315                                   hnum * vden * pow(10, hexp),
2316                                   vnum * hden * pow(10, vexp),
2317                                   INT32_MAX);
2318                 }
2319                 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2320             } while (atom_end - atom2_end >= 8);
2321         } else {
2322             search_range--;
2323         }
2324         bytestream2_seek(&s->g, atom_end, SEEK_SET);
2325     }
2326
2327     return 0;
2328 }
2329
2330 static av_cold void jpeg2000_init_static_data(void)
2331 {
2332     ff_jpeg2000_init_tier1_luts();
2333     ff_mqc_init_context_tables();
2334 }
2335
2336 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2337 {
2338     static AVOnce init_static_once = AV_ONCE_INIT;
2339     Jpeg2000DecoderContext *s = avctx->priv_data;
2340
2341     ff_thread_once(&init_static_once, jpeg2000_init_static_data);
2342     ff_jpeg2000dsp_init(&s->dsp);
2343
2344     return 0;
2345 }
2346
2347 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
2348                                  int *got_frame, AVPacket *avpkt)
2349 {
2350     Jpeg2000DecoderContext *s = avctx->priv_data;
2351     ThreadFrame frame = { .f = data };
2352     AVFrame *picture = data;
2353     int ret;
2354
2355     s->avctx     = avctx;
2356     bytestream2_init(&s->g, avpkt->data, avpkt->size);
2357     s->curtileno = -1;
2358     memset(s->cdef, -1, sizeof(s->cdef));
2359
2360     if (bytestream2_get_bytes_left(&s->g) < 2) {
2361         ret = AVERROR_INVALIDDATA;
2362         goto end;
2363     }
2364
2365     // check if the image is in jp2 format
2366     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2367        (bytestream2_get_be32u(&s->g) == 12) &&
2368        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2369        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2370         if (!jp2_find_codestream(s)) {
2371             av_log(avctx, AV_LOG_ERROR,
2372                    "Could not find Jpeg2000 codestream atom.\n");
2373             ret = AVERROR_INVALIDDATA;
2374             goto end;
2375         }
2376     } else {
2377         bytestream2_seek(&s->g, 0, SEEK_SET);
2378     }
2379
2380     while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2381         bytestream2_skip(&s->g, 1);
2382
2383     if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2384         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2385         ret = AVERROR_INVALIDDATA;
2386         goto end;
2387     }
2388     if (ret = jpeg2000_read_main_headers(s))
2389         goto end;
2390
2391     /* get picture buffer */
2392     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2393         goto end;
2394     picture->pict_type = AV_PICTURE_TYPE_I;
2395     picture->key_frame = 1;
2396
2397     if (ret = jpeg2000_read_bitstream_packets(s))
2398         goto end;
2399
2400     avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2401
2402     jpeg2000_dec_cleanup(s);
2403
2404     *got_frame = 1;
2405
2406     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2407         memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2408     if (s->sar.num && s->sar.den)
2409         avctx->sample_aspect_ratio = s->sar;
2410     s->sar.num = s->sar.den = 0;
2411
2412     return bytestream2_tell(&s->g);
2413
2414 end:
2415     jpeg2000_dec_cleanup(s);
2416     return ret;
2417 }
2418
2419 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2420 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2421
2422 static const AVOption options[] = {
2423     { "lowres",  "Lower the decoding resolution by a power of two",
2424         OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2425     { NULL },
2426 };
2427
2428 static const AVClass jpeg2000_class = {
2429     .class_name = "jpeg2000",
2430     .item_name  = av_default_item_name,
2431     .option     = options,
2432     .version    = LIBAVUTIL_VERSION_INT,
2433 };
2434
2435 AVCodec ff_jpeg2000_decoder = {
2436     .name             = "jpeg2000",
2437     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
2438     .type             = AVMEDIA_TYPE_VIDEO,
2439     .id               = AV_CODEC_ID_JPEG2000,
2440     .capabilities     = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2441     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
2442     .init             = jpeg2000_decode_init,
2443     .decode           = jpeg2000_decode_frame,
2444     .priv_class       = &jpeg2000_class,
2445     .max_lowres       = 5,
2446     .profiles         = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)
2447 };