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