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