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