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