]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeg2000dec.c
Merge commit '18f4fa251b0eb36392839f5bf6180f280dc04d8d'
[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
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/common.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "avcodec.h"
36 #include "bytestream.h"
37 #include "internal.h"
38 #include "thread.h"
39 #include "jpeg2000.h"
40 #include "jpeg2000dsp.h"
41
42 #define JP2_SIG_TYPE    0x6A502020
43 #define JP2_SIG_VALUE   0x0D0A870A
44 #define JP2_CODESTREAM  0x6A703263
45 #define JP2_HEADER      0x6A703268
46
47 #define HAD_COC 0x01
48 #define HAD_QCC 0x02
49
50 typedef struct Jpeg2000TilePart {
51     uint8_t tile_index;                 // Tile index who refers the tile-part
52     const uint8_t *tp_end;
53     GetByteContext tpg;                 // bit stream in tile-part
54 } Jpeg2000TilePart;
55
56 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
57  * one per component, so tile_part elements have a size of 3 */
58 typedef struct Jpeg2000Tile {
59     Jpeg2000Component   *comp;
60     uint8_t             properties[4];
61     Jpeg2000CodingStyle codsty[4];
62     Jpeg2000QuantStyle  qntsty[4];
63     Jpeg2000TilePart    tile_part[4];
64     uint16_t tp_idx;                    // Tile-part index
65 } Jpeg2000Tile;
66
67 typedef struct Jpeg2000DecoderContext {
68     AVClass         *class;
69     AVCodecContext  *avctx;
70     GetByteContext  g;
71
72     int             width, height;
73     int             image_offset_x, image_offset_y;
74     int             tile_offset_x, tile_offset_y;
75     uint8_t         cbps[4];    // bits per sample in particular components
76     uint8_t         sgnd[4];    // if a component is signed
77     uint8_t         properties[4];
78     int             cdx[4], cdy[4];
79     int             precision;
80     int             ncomponents;
81     int             colour_space;
82     uint32_t        palette[256];
83     int8_t          pal8;
84     int             cdef[4];
85     int             tile_width, tile_height;
86     unsigned        numXtiles, numYtiles;
87     int             maxtilelen;
88
89     Jpeg2000CodingStyle codsty[4];
90     Jpeg2000QuantStyle  qntsty[4];
91
92     int             bit_index;
93
94     int             curtileno;
95
96     Jpeg2000Tile    *tile;
97     Jpeg2000DSPContext dsp;
98
99     /*options parameters*/
100     int             reduction_factor;
101 } Jpeg2000DecoderContext;
102
103 /* get_bits functions for JPEG2000 packet bitstream
104  * It is a get_bit function with a bit-stuffing routine. If the value of the
105  * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
106  * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
107 static int get_bits(Jpeg2000DecoderContext *s, int n)
108 {
109     int res = 0;
110
111     while (--n >= 0) {
112         res <<= 1;
113         if (s->bit_index == 0) {
114             s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
115         }
116         s->bit_index--;
117         res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
118     }
119     return res;
120 }
121
122 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
123 {
124     if (bytestream2_get_byte(&s->g) == 0xff)
125         bytestream2_skip(&s->g, 1);
126     s->bit_index = 8;
127 }
128
129 /* decode the value stored in node */
130 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
131                            int threshold)
132 {
133     Jpeg2000TgtNode *stack[30];
134     int sp = -1, curval = 0;
135
136     if (!node)
137         return AVERROR_INVALIDDATA;
138
139     while (node && !node->vis) {
140         stack[++sp] = node;
141         node        = node->parent;
142     }
143
144     if (node)
145         curval = node->val;
146     else
147         curval = stack[sp]->val;
148
149     while (curval < threshold && sp >= 0) {
150         if (curval < stack[sp]->val)
151             curval = stack[sp]->val;
152         while (curval < threshold) {
153             int ret;
154             if ((ret = get_bits(s, 1)) > 0) {
155                 stack[sp]->vis++;
156                 break;
157             } else if (!ret)
158                 curval++;
159             else
160                 return ret;
161         }
162         stack[sp]->val = curval;
163         sp--;
164     }
165     return curval;
166 }
167
168 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
169                          int bpc, uint32_t log2_chroma_wh, int pal8)
170 {
171     int match = 1;
172     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
173
174     if (desc->nb_components != components) {
175         return 0;
176     }
177
178     switch (components) {
179     case 4:
180         match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
181                          (log2_chroma_wh >> 14 & 3) == 0 &&
182                          (log2_chroma_wh >> 12 & 3) == 0;
183     case 3:
184         match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
185                          (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
186                          (log2_chroma_wh >>  8 & 3) == desc->log2_chroma_h;
187     case 2:
188         match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
189                          (log2_chroma_wh >>  6 & 3) == desc->log2_chroma_w &&
190                          (log2_chroma_wh >>  4 & 3) == desc->log2_chroma_h;
191
192     case 1:
193         match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
194                          (log2_chroma_wh >>  2 & 3) == 0 &&
195                          (log2_chroma_wh       & 3) == 0 &&
196                          (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
197     }
198     return match;
199 }
200
201 // pix_fmts with lower bpp have to be listed before
202 // similar pix_fmts with higher bpp.
203 #define RGB_PIXEL_FORMATS   AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
204 #define GRAY_PIXEL_FORMATS  AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
205 #define YUV_PIXEL_FORMATS   AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
206                             AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
207                             AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
208                             AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
209                             AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
210                             AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
211                             AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
212                             AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
213                             AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
214                             AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
215                             AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
216 #define XYZ_PIXEL_FORMATS   AV_PIX_FMT_XYZ12
217
218 static const enum AVPixelFormat rgb_pix_fmts[]  = {RGB_PIXEL_FORMATS};
219 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
220 static const enum AVPixelFormat yuv_pix_fmts[]  = {YUV_PIXEL_FORMATS};
221 static const enum AVPixelFormat xyz_pix_fmts[]  = {XYZ_PIXEL_FORMATS};
222 static const enum AVPixelFormat all_pix_fmts[]  = {RGB_PIXEL_FORMATS,
223                                                    GRAY_PIXEL_FORMATS,
224                                                    YUV_PIXEL_FORMATS,
225                                                    XYZ_PIXEL_FORMATS};
226
227 /* marker segments */
228 /* get sizes and offsets of image, tiles; number of components */
229 static int get_siz(Jpeg2000DecoderContext *s)
230 {
231     int i;
232     int ncomponents;
233     uint32_t log2_chroma_wh = 0;
234     const enum AVPixelFormat *possible_fmts = NULL;
235     int possible_fmts_nb = 0;
236
237     if (bytestream2_get_bytes_left(&s->g) < 36)
238         return AVERROR_INVALIDDATA;
239
240     s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
241     s->width          = bytestream2_get_be32u(&s->g); // Width
242     s->height         = bytestream2_get_be32u(&s->g); // Height
243     s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
244     s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
245     s->tile_width     = bytestream2_get_be32u(&s->g); // XTSiz
246     s->tile_height    = bytestream2_get_be32u(&s->g); // YTSiz
247     s->tile_offset_x  = bytestream2_get_be32u(&s->g); // XT0Siz
248     s->tile_offset_y  = bytestream2_get_be32u(&s->g); // YT0Siz
249     ncomponents       = bytestream2_get_be16u(&s->g); // CSiz
250
251     if (s->image_offset_x || s->image_offset_y) {
252         avpriv_request_sample(s->avctx, "Support for image offsets");
253         return AVERROR_PATCHWELCOME;
254     }
255
256     if (ncomponents <= 0) {
257         av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
258                s->ncomponents);
259         return AVERROR_INVALIDDATA;
260     }
261
262     if (ncomponents > 4) {
263         avpriv_request_sample(s->avctx, "Support for %d components",
264                               s->ncomponents);
265         return AVERROR_PATCHWELCOME;
266     }
267
268     s->ncomponents = ncomponents;
269
270     if (s->tile_width <= 0 || s->tile_height <= 0) {
271         av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
272                s->tile_width, s->tile_height);
273         return AVERROR_INVALIDDATA;
274     }
275
276     if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
277         return AVERROR_INVALIDDATA;
278
279     for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
280         uint8_t x    = bytestream2_get_byteu(&s->g);
281         s->cbps[i]   = (x & 0x7f) + 1;
282         s->precision = FFMAX(s->cbps[i], s->precision);
283         s->sgnd[i]   = !!(x & 0x80);
284         s->cdx[i]    = bytestream2_get_byteu(&s->g);
285         s->cdy[i]    = bytestream2_get_byteu(&s->g);
286         if (   !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
287             || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
288             av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
289             return AVERROR_INVALIDDATA;
290         }
291         log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
292     }
293
294     s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
295     s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
296
297     if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
298         s->numXtiles = s->numYtiles = 0;
299         return AVERROR(EINVAL);
300     }
301
302     s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
303     if (!s->tile) {
304         s->numXtiles = s->numYtiles = 0;
305         return AVERROR(ENOMEM);
306     }
307
308     for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
309         Jpeg2000Tile *tile = s->tile + i;
310
311         tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
312         if (!tile->comp)
313             return AVERROR(ENOMEM);
314     }
315
316     /* compute image size with reduction factor */
317     s->avctx->width  = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
318                                                s->reduction_factor);
319     s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
320                                                s->reduction_factor);
321
322     if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
323         s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
324         possible_fmts = xyz_pix_fmts;
325         possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
326     } else {
327         switch (s->colour_space) {
328         case 16:
329             possible_fmts = rgb_pix_fmts;
330             possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
331             break;
332         case 17:
333             possible_fmts = gray_pix_fmts;
334             possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
335             break;
336         case 18:
337             possible_fmts = yuv_pix_fmts;
338             possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
339             break;
340         default:
341             possible_fmts = all_pix_fmts;
342             possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
343             break;
344         }
345     }
346     for (i = 0; i < possible_fmts_nb; ++i) {
347         if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
348             s->avctx->pix_fmt = possible_fmts[i];
349             break;
350         }
351     }
352     if (i == possible_fmts_nb) {
353         av_log(s->avctx, AV_LOG_ERROR,
354                "Unknown pix_fmt, profile: %d, colour_space: %d, "
355                "components: %d, precision: %d, "
356                "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
357                s->avctx->profile, s->colour_space, ncomponents, s->precision,
358                ncomponents > 2 ? s->cdx[1] : 0,
359                ncomponents > 2 ? s->cdy[1] : 0,
360                ncomponents > 2 ? s->cdx[2] : 0,
361                ncomponents > 2 ? s->cdy[2] : 0);
362         return AVERROR_PATCHWELCOME;
363     }
364     s->avctx->bits_per_raw_sample = s->precision;
365     return 0;
366 }
367
368 /* get common part for COD and COC segments */
369 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
370 {
371     uint8_t byte;
372
373     if (bytestream2_get_bytes_left(&s->g) < 5)
374         return AVERROR_INVALIDDATA;
375
376     /*  nreslevels = number of resolution levels
377                    = number of decomposition level +1 */
378     c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
379     if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
380         av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
381         return AVERROR_INVALIDDATA;
382     }
383
384     if (c->nreslevels <= s->reduction_factor) {
385         /* we are forced to update reduction_factor as its requested value is
386            not compatible with this bitstream, and as we might have used it
387            already in setup earlier we have to fail this frame until
388            reinitialization is implemented */
389         av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
390         s->reduction_factor = c->nreslevels - 1;
391         return AVERROR(EINVAL);
392     }
393
394     /* compute number of resolution levels to decode */
395     c->nreslevels2decode = c->nreslevels - s->reduction_factor;
396
397     c->log2_cblk_width  = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
398     c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
399
400     if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
401         c->log2_cblk_width + c->log2_cblk_height > 12) {
402         av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
403         return AVERROR_INVALIDDATA;
404     }
405
406     if (c->log2_cblk_width > 6 || c->log2_cblk_height > 6) {
407         avpriv_request_sample(s->avctx, "cblk size > 64");
408         return AVERROR_PATCHWELCOME;
409     }
410
411     c->cblk_style = bytestream2_get_byteu(&s->g);
412     if (c->cblk_style != 0) { // cblk style
413         av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
414     }
415     c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
416     /* set integer 9/7 DWT in case of BITEXACT flag */
417     if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
418         c->transform = FF_DWT97_INT;
419
420     if (c->csty & JPEG2000_CSTY_PREC) {
421         int i;
422         for (i = 0; i < c->nreslevels; i++) {
423             byte = bytestream2_get_byte(&s->g);
424             c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
425             c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
426         }
427     } else {
428         memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
429         memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
430     }
431     return 0;
432 }
433
434 /* get coding parameters for a particular tile or whole image*/
435 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
436                    uint8_t *properties)
437 {
438     Jpeg2000CodingStyle tmp;
439     int compno, ret;
440
441     if (bytestream2_get_bytes_left(&s->g) < 5)
442         return AVERROR_INVALIDDATA;
443
444     tmp.csty = bytestream2_get_byteu(&s->g);
445
446     // get progression order
447     tmp.prog_order = bytestream2_get_byteu(&s->g);
448
449     tmp.nlayers    = bytestream2_get_be16u(&s->g);
450     tmp.mct        = bytestream2_get_byteu(&s->g); // multiple component transformation
451
452     if (tmp.mct && s->ncomponents < 3) {
453         av_log(s->avctx, AV_LOG_ERROR,
454                "MCT %"PRIu8" with too few components (%d)\n",
455                tmp.mct, s->ncomponents);
456         return AVERROR_INVALIDDATA;
457     }
458
459     if ((ret = get_cox(s, &tmp)) < 0)
460         return ret;
461
462     for (compno = 0; compno < s->ncomponents; compno++)
463         if (!(properties[compno] & HAD_COC))
464             memcpy(c + compno, &tmp, sizeof(tmp));
465     return 0;
466 }
467
468 /* Get coding parameters for a component in the whole image or a
469  * particular tile. */
470 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
471                    uint8_t *properties)
472 {
473     int compno, ret;
474
475     if (bytestream2_get_bytes_left(&s->g) < 2)
476         return AVERROR_INVALIDDATA;
477
478     compno = bytestream2_get_byteu(&s->g);
479
480     if (compno >= s->ncomponents) {
481         av_log(s->avctx, AV_LOG_ERROR,
482                "Invalid compno %d. There are %d components in the image.\n",
483                compno, s->ncomponents);
484         return AVERROR_INVALIDDATA;
485     }
486
487     c      += compno;
488     c->csty = bytestream2_get_byteu(&s->g);
489
490     if ((ret = get_cox(s, c)) < 0)
491         return ret;
492
493     properties[compno] |= HAD_COC;
494     return 0;
495 }
496
497 /* Get common part for QCD and QCC segments. */
498 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
499 {
500     int i, x;
501
502     if (bytestream2_get_bytes_left(&s->g) < 1)
503         return AVERROR_INVALIDDATA;
504
505     x = bytestream2_get_byteu(&s->g); // Sqcd
506
507     q->nguardbits = x >> 5;
508     q->quantsty   = x & 0x1f;
509
510     if (q->quantsty == JPEG2000_QSTY_NONE) {
511         n -= 3;
512         if (bytestream2_get_bytes_left(&s->g) < n ||
513             n > JPEG2000_MAX_DECLEVELS*3)
514             return AVERROR_INVALIDDATA;
515         for (i = 0; i < n; i++)
516             q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
517     } else if (q->quantsty == JPEG2000_QSTY_SI) {
518         if (bytestream2_get_bytes_left(&s->g) < 2)
519             return AVERROR_INVALIDDATA;
520         x          = bytestream2_get_be16u(&s->g);
521         q->expn[0] = x >> 11;
522         q->mant[0] = x & 0x7ff;
523         for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
524             int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
525             q->expn[i] = curexpn;
526             q->mant[i] = q->mant[0];
527         }
528     } else {
529         n = (n - 3) >> 1;
530         if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
531             n > JPEG2000_MAX_DECLEVELS*3)
532             return AVERROR_INVALIDDATA;
533         for (i = 0; i < n; i++) {
534             x          = bytestream2_get_be16u(&s->g);
535             q->expn[i] = x >> 11;
536             q->mant[i] = x & 0x7ff;
537         }
538     }
539     return 0;
540 }
541
542 /* Get quantization parameters for a particular tile or a whole image. */
543 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
544                    uint8_t *properties)
545 {
546     Jpeg2000QuantStyle tmp;
547     int compno, ret;
548
549     memset(&tmp, 0, sizeof(tmp));
550
551     if ((ret = get_qcx(s, n, &tmp)) < 0)
552         return ret;
553     for (compno = 0; compno < s->ncomponents; compno++)
554         if (!(properties[compno] & HAD_QCC))
555             memcpy(q + compno, &tmp, sizeof(tmp));
556     return 0;
557 }
558
559 /* Get quantization parameters for a component in the whole image
560  * on in a particular tile. */
561 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
562                    uint8_t *properties)
563 {
564     int compno;
565
566     if (bytestream2_get_bytes_left(&s->g) < 1)
567         return AVERROR_INVALIDDATA;
568
569     compno = bytestream2_get_byteu(&s->g);
570
571     if (compno >= s->ncomponents) {
572         av_log(s->avctx, AV_LOG_ERROR,
573                "Invalid compno %d. There are %d components in the image.\n",
574                compno, s->ncomponents);
575         return AVERROR_INVALIDDATA;
576     }
577
578     properties[compno] |= HAD_QCC;
579     return get_qcx(s, n - 1, q + compno);
580 }
581
582 /* Get start of tile segment. */
583 static int get_sot(Jpeg2000DecoderContext *s, int n)
584 {
585     Jpeg2000TilePart *tp;
586     uint16_t Isot;
587     uint32_t Psot;
588     uint8_t TPsot;
589
590     if (bytestream2_get_bytes_left(&s->g) < 8)
591         return AVERROR_INVALIDDATA;
592
593     s->curtileno = 0;
594     Isot = bytestream2_get_be16u(&s->g);        // Isot
595     if (Isot >= s->numXtiles * s->numYtiles)
596         return AVERROR_INVALIDDATA;
597
598     s->curtileno = Isot;
599     Psot  = bytestream2_get_be32u(&s->g);       // Psot
600     TPsot = bytestream2_get_byteu(&s->g);       // TPsot
601
602     /* Read TNSot but not used */
603     bytestream2_get_byteu(&s->g);               // TNsot
604
605     if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
606         av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
607         return AVERROR_INVALIDDATA;
608     }
609
610     if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
611         avpriv_request_sample(s->avctx, "Support for %"PRIu8" components", TPsot);
612         return AVERROR_PATCHWELCOME;
613     }
614
615     s->tile[Isot].tp_idx = TPsot;
616     tp             = s->tile[Isot].tile_part + TPsot;
617     tp->tile_index = Isot;
618     tp->tp_end     = s->g.buffer + Psot - n - 2;
619
620     if (!TPsot) {
621         Jpeg2000Tile *tile = s->tile + s->curtileno;
622
623         /* copy defaults */
624         memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
625         memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
626     }
627
628     return 0;
629 }
630
631 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
632  * Used to know the number of tile parts and lengths.
633  * There may be multiple TLMs in the header.
634  * TODO: The function is not used for tile-parts management, nor anywhere else.
635  * It can be useful to allocate memory for tile parts, before managing the SOT
636  * markers. Parsing the TLM header is needed to increment the input header
637  * buffer.
638  * This marker is mandatory for DCI. */
639 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
640 {
641     uint8_t Stlm, ST, SP, tile_tlm, i;
642     bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
643     Stlm = bytestream2_get_byte(&s->g);
644
645     // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
646     ST = (Stlm >> 4) & 0x03;
647     // TODO: Manage case of ST = 0b11 --> raise error
648     SP       = (Stlm >> 6) & 0x01;
649     tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
650     for (i = 0; i < tile_tlm; i++) {
651         switch (ST) {
652         case 0:
653             break;
654         case 1:
655             bytestream2_get_byte(&s->g);
656             break;
657         case 2:
658             bytestream2_get_be16(&s->g);
659             break;
660         case 3:
661             bytestream2_get_be32(&s->g);
662             break;
663         }
664         if (SP == 0) {
665             bytestream2_get_be16(&s->g);
666         } else {
667             bytestream2_get_be32(&s->g);
668         }
669     }
670     return 0;
671 }
672
673 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
674 {
675     int compno;
676     int tilex = tileno % s->numXtiles;
677     int tiley = tileno / s->numXtiles;
678     Jpeg2000Tile *tile = s->tile + tileno;
679
680     if (!tile->comp)
681         return AVERROR(ENOMEM);
682
683     for (compno = 0; compno < s->ncomponents; compno++) {
684         Jpeg2000Component *comp = tile->comp + compno;
685         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
686         Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
687         int ret; // global bandno
688
689         comp->coord_o[0][0] = FFMAX(tilex       * s->tile_width  + s->tile_offset_x, s->image_offset_x);
690         comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width  + s->tile_offset_x, s->width);
691         comp->coord_o[1][0] = FFMAX(tiley       * s->tile_height + s->tile_offset_y, s->image_offset_y);
692         comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
693
694         comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
695         comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
696         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
697         comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
698
699         if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
700                                              s->cbps[compno], s->cdx[compno],
701                                              s->cdy[compno], s->avctx))
702             return ret;
703     }
704     return 0;
705 }
706
707 /* Read the number of coding passes. */
708 static int getnpasses(Jpeg2000DecoderContext *s)
709 {
710     int num;
711     if (!get_bits(s, 1))
712         return 1;
713     if (!get_bits(s, 1))
714         return 2;
715     if ((num = get_bits(s, 2)) != 3)
716         return num < 0 ? num : 3 + num;
717     if ((num = get_bits(s, 5)) != 31)
718         return num < 0 ? num : 6 + num;
719     num = get_bits(s, 7);
720     return num < 0 ? num : 37 + num;
721 }
722
723 static int getlblockinc(Jpeg2000DecoderContext *s)
724 {
725     int res = 0, ret;
726     while (ret = get_bits(s, 1)) {
727         if (ret < 0)
728             return ret;
729         res++;
730     }
731     return res;
732 }
733
734 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
735                                   Jpeg2000CodingStyle *codsty,
736                                   Jpeg2000ResLevel *rlevel, int precno,
737                                   int layno, uint8_t *expn, int numgbits)
738 {
739     int bandno, cblkno, ret, nb_code_blocks;
740
741     if (!(ret = get_bits(s, 1))) {
742         jpeg2000_flush(s);
743         return 0;
744     } else if (ret < 0)
745         return ret;
746
747     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
748         Jpeg2000Band *band = rlevel->band + bandno;
749         Jpeg2000Prec *prec = band->prec + precno;
750
751         if (band->coord[0][0] == band->coord[0][1] ||
752             band->coord[1][0] == band->coord[1][1])
753             continue;
754         nb_code_blocks =  prec->nb_codeblocks_height *
755                           prec->nb_codeblocks_width;
756         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
757             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
758             int incl, newpasses, llen;
759
760             if (cblk->npasses)
761                 incl = get_bits(s, 1);
762             else
763                 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
764             if (!incl)
765                 continue;
766             else if (incl < 0)
767                 return incl;
768
769             if (!cblk->npasses) {
770                 int v = expn[bandno] + numgbits - 1 -
771                         tag_tree_decode(s, prec->zerobits + cblkno, 100);
772                 if (v < 0) {
773                     av_log(s->avctx, AV_LOG_ERROR,
774                            "nonzerobits %d invalid\n", v);
775                     return AVERROR_INVALIDDATA;
776                 }
777                 cblk->nonzerobits = v;
778             }
779             if ((newpasses = getnpasses(s)) < 0)
780                 return newpasses;
781             if ((llen = getlblockinc(s)) < 0)
782                 return llen;
783             cblk->lblock += llen;
784             if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
785                 return ret;
786             if (ret > sizeof(cblk->data)) {
787                 avpriv_request_sample(s->avctx,
788                                       "Block with lengthinc greater than %"SIZE_SPECIFIER"",
789                                       sizeof(cblk->data));
790                 return AVERROR_PATCHWELCOME;
791             }
792             cblk->lengthinc = ret;
793             cblk->npasses  += newpasses;
794         }
795     }
796     jpeg2000_flush(s);
797
798     if (codsty->csty & JPEG2000_CSTY_EPH) {
799         if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
800             bytestream2_skip(&s->g, 2);
801         else
802             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
803     }
804
805     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
806         Jpeg2000Band *band = rlevel->band + bandno;
807         Jpeg2000Prec *prec = band->prec + precno;
808
809         nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
810         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
811             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
812             if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
813                 || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
814             ) {
815                 av_log(s->avctx, AV_LOG_ERROR,
816                        "Block length %"PRIu16" or lengthinc %d is too large\n",
817                        cblk->length, cblk->lengthinc);
818                 return AVERROR_INVALIDDATA;
819             }
820
821             bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
822             cblk->length   += cblk->lengthinc;
823             cblk->lengthinc = 0;
824         }
825     }
826     return 0;
827 }
828
829 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
830 {
831     int ret = 0;
832     int layno, reslevelno, compno, precno, ok_reslevel;
833     int x, y;
834
835     s->bit_index = 8;
836     switch (tile->codsty[0].prog_order) {
837     case JPEG2000_PGOD_RLCP:
838         avpriv_request_sample(s->avctx, "Progression order RLCP");
839
840     case JPEG2000_PGOD_LRCP:
841         for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
842             ok_reslevel = 1;
843             for (reslevelno = 0; ok_reslevel; reslevelno++) {
844                 ok_reslevel = 0;
845                 for (compno = 0; compno < s->ncomponents; compno++) {
846                     Jpeg2000CodingStyle *codsty = tile->codsty + compno;
847                     Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
848                     if (reslevelno < codsty->nreslevels) {
849                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
850                                                 reslevelno;
851                         ok_reslevel = 1;
852                         for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
853                             if ((ret = jpeg2000_decode_packet(s,
854                                                               codsty, rlevel,
855                                                               precno, layno,
856                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
857                                                               qntsty->nguardbits)) < 0)
858                                 return ret;
859                     }
860                 }
861             }
862         }
863         break;
864
865     case JPEG2000_PGOD_CPRL:
866         for (compno = 0; compno < s->ncomponents; compno++) {
867             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
868             Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
869
870             /* Set bit stream buffer address according to tile-part.
871              * For DCinema one tile-part per component, so can be
872              * indexed by component. */
873             s->g = tile->tile_part[compno].tpg;
874
875             /* Position loop (y axis)
876              * TODO: Automate computing of step 256.
877              * Fixed here, but to be computed before entering here. */
878             for (y = 0; y < s->height; y += 256) {
879                 /* Position loop (y axis)
880                  * TODO: automate computing of step 256.
881                  * Fixed here, but to be computed before entering here. */
882                 for (x = 0; x < s->width; x += 256) {
883                     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
884                         uint16_t prcx, prcy;
885                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
886                         Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
887
888                         if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
889                               (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
890                             continue;
891
892                         if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
893                               (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
894                             continue;
895
896                         // check if a precinct exists
897                         prcx   = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
898                         prcy   = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
899                         precno = prcx + rlevel->num_precincts_x * prcy;
900
901                         if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y)
902                             return AVERROR_PATCHWELCOME;
903
904                         for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
905                             if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
906                                                               precno, layno,
907                                                               qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
908                                                               qntsty->nguardbits)) < 0)
909                                 return ret;
910                         }
911                     }
912                 }
913             }
914         }
915         break;
916
917     case JPEG2000_PGOD_RPCL:
918         avpriv_request_sample(s->avctx, "Progression order RPCL");
919         ret = AVERROR_PATCHWELCOME;
920         break;
921
922     case JPEG2000_PGOD_PCRL:
923         avpriv_request_sample(s->avctx, "Progression order PCRL");
924         ret = AVERROR_PATCHWELCOME;
925         break;
926
927     default:
928         break;
929     }
930
931     /* EOC marker reached */
932     bytestream2_skip(&s->g, 2);
933
934     return ret;
935 }
936
937 /* TIER-1 routines */
938 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
939                            int bpno, int bandno, int bpass_csty_symbol,
940                            int vert_causal_ctx_csty_symbol)
941 {
942     int mask = 3 << (bpno - 1), y0, x, y;
943
944     for (y0 = 0; y0 < height; y0 += 4)
945         for (x = 0; x < width; x++)
946             for (y = y0; y < height && y < y0 + 4; y++) {
947                 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
948                 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
949                     int flags_mask = -1;
950                     if (vert_causal_ctx_csty_symbol && y == y0 + 3)
951                         flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
952                     if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
953                         int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
954                         if (bpass_csty_symbol)
955                              t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
956                         else
957                              t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
958                                                -mask : mask;
959
960                         ff_jpeg2000_set_significance(t1, x, y,
961                                                      t1->data[y][x] < 0);
962                     }
963                     t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
964                 }
965             }
966 }
967
968 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
969                            int bpno)
970 {
971     int phalf, nhalf;
972     int y0, x, y;
973
974     phalf = 1 << (bpno - 1);
975     nhalf = -phalf;
976
977     for (y0 = 0; y0 < height; y0 += 4)
978         for (x = 0; x < width; x++)
979             for (y = y0; y < height && y < y0 + 4; y++)
980                 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
981                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
982                     int r     = ff_mqc_decode(&t1->mqc,
983                                               t1->mqc.cx_states + ctxno)
984                                 ? phalf : nhalf;
985                     t1->data[y][x]          += t1->data[y][x] < 0 ? -r : r;
986                     t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
987                 }
988 }
989
990 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
991                            int width, int height, int bpno, int bandno,
992                            int seg_symbols, int vert_causal_ctx_csty_symbol)
993 {
994     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
995
996     for (y0 = 0; y0 < height; y0 += 4) {
997         for (x = 0; x < width; x++) {
998             if (y0 + 3 < height &&
999                 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1000                   (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1001                   (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1002                   (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
1003                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1004                     continue;
1005                 runlen = ff_mqc_decode(&t1->mqc,
1006                                        t1->mqc.cx_states + MQC_CX_UNI);
1007                 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1008                                                        t1->mqc.cx_states +
1009                                                        MQC_CX_UNI);
1010                 dec = 1;
1011             } else {
1012                 runlen = 0;
1013                 dec    = 0;
1014             }
1015
1016             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1017                 if (!dec) {
1018                     if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1019                         int flags_mask = -1;
1020                         if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1021                             flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
1022                         dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
1023                                                                                              bandno));
1024                     }
1025                 }
1026                 if (dec) {
1027                     int xorbit;
1028                     int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
1029                                                         &xorbit);
1030                     t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
1031                                                     t1->mqc.cx_states + ctxno) ^
1032                                       xorbit)
1033                                      ? -mask : mask;
1034                     ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
1035                 }
1036                 dec = 0;
1037                 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
1038             }
1039         }
1040     }
1041     if (seg_symbols) {
1042         int val;
1043         val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1044         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1045         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1046         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1047         if (val != 0xa)
1048             av_log(s->avctx, AV_LOG_ERROR,
1049                    "Segmentation symbol value incorrect\n");
1050     }
1051 }
1052
1053 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1054                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1055                        int width, int height, int bandpos)
1056 {
1057     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
1058     int clnpass_cnt = 0;
1059     int bpass_csty_symbol           = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
1060     int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1061
1062     av_assert0(width  <= JPEG2000_MAX_CBLKW);
1063     av_assert0(height <= JPEG2000_MAX_CBLKH);
1064
1065     for (y = 0; y < height; y++)
1066         memset(t1->data[y], 0, width * sizeof(**t1->data));
1067
1068     /* If code-block contains no compressed data: nothing to do. */
1069     if (!cblk->length)
1070         return 0;
1071
1072     for (y = 0; y < height + 2; y++)
1073         memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
1074
1075     cblk->data[cblk->length] = 0xff;
1076     cblk->data[cblk->length+1] = 0xff;
1077     ff_mqc_initdec(&t1->mqc, cblk->data);
1078
1079     while (passno--) {
1080         switch(pass_t) {
1081         case 0:
1082             decode_sigpass(t1, width, height, bpno + 1, bandpos,
1083                            bpass_csty_symbol && (clnpass_cnt >= 4),
1084                            vert_causal_ctx_csty_symbol);
1085             break;
1086         case 1:
1087             decode_refpass(t1, width, height, bpno + 1);
1088             if (bpass_csty_symbol && clnpass_cnt >= 4)
1089                 ff_mqc_initdec(&t1->mqc, cblk->data);
1090             break;
1091         case 2:
1092             decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1093                            codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1094                            vert_causal_ctx_csty_symbol);
1095             clnpass_cnt = clnpass_cnt + 1;
1096             if (bpass_csty_symbol && clnpass_cnt >= 4)
1097                 ff_mqc_initdec(&t1->mqc, cblk->data);
1098             break;
1099         }
1100
1101         pass_t++;
1102         if (pass_t == 3) {
1103             bpno--;
1104             pass_t = 0;
1105         }
1106     }
1107     return 0;
1108 }
1109
1110 /* TODO: Verify dequantization for lossless case
1111  * comp->data can be float or int
1112  * band->stepsize can be float or int
1113  * depending on the type of DWT transformation.
1114  * see ISO/IEC 15444-1:2002 A.6.1 */
1115
1116 /* Float dequantization of a codeblock.*/
1117 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1118                                  Jpeg2000Component *comp,
1119                                  Jpeg2000T1Context *t1, Jpeg2000Band *band)
1120 {
1121     int i, j;
1122     int w = cblk->coord[0][1] - cblk->coord[0][0];
1123     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1124         float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1125         int *src = t1->data[j];
1126         for (i = 0; i < w; ++i)
1127             datap[i] = src[i] * band->f_stepsize;
1128     }
1129 }
1130
1131 /* Integer dequantization of a codeblock.*/
1132 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1133                                Jpeg2000Component *comp,
1134                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1135 {
1136     int i, j;
1137     int w = cblk->coord[0][1] - cblk->coord[0][0];
1138     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1139         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1140         int *src = t1->data[j];
1141         for (i = 0; i < w; ++i)
1142             datap[i] = (src[i] * band->i_stepsize + (1 << 14)) >> 15;
1143     }
1144 }
1145
1146 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1147 {
1148     int i, csize = 1;
1149     void *src[3];
1150
1151     for (i = 1; i < 3; i++)
1152         if (tile->codsty[0].transform != tile->codsty[i].transform) {
1153             av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1154             return;
1155         }
1156
1157     for (i = 0; i < 3; i++)
1158         if (tile->codsty[0].transform == FF_DWT97)
1159             src[i] = tile->comp[i].f_data;
1160         else
1161             src[i] = tile->comp[i].i_data;
1162
1163     for (i = 0; i < 2; i++)
1164         csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1165
1166     s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1167 }
1168
1169 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1170                                 AVFrame *picture)
1171 {
1172     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1173     int compno, reslevelno, bandno;
1174     int x, y;
1175     int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
1176     int pixelsize = planar ? 1 : pixdesc->nb_components;
1177
1178     uint8_t *line;
1179     Jpeg2000T1Context t1;
1180
1181     /* Loop on tile components */
1182     for (compno = 0; compno < s->ncomponents; compno++) {
1183         Jpeg2000Component *comp     = tile->comp + compno;
1184         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1185
1186         /* Loop on resolution levels */
1187         for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1188             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1189             /* Loop on bands */
1190             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1191                 int nb_precincts, precno;
1192                 Jpeg2000Band *band = rlevel->band + bandno;
1193                 int cblkno = 0, bandpos;
1194
1195                 bandpos = bandno + (reslevelno > 0);
1196
1197                 if (band->coord[0][0] == band->coord[0][1] ||
1198                     band->coord[1][0] == band->coord[1][1])
1199                     continue;
1200
1201                 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1202                 /* Loop on precincts */
1203                 for (precno = 0; precno < nb_precincts; precno++) {
1204                     Jpeg2000Prec *prec = band->prec + precno;
1205
1206                     /* Loop on codeblocks */
1207                     for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1208                         int x, y;
1209                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1210                         decode_cblk(s, codsty, &t1, cblk,
1211                                     cblk->coord[0][1] - cblk->coord[0][0],
1212                                     cblk->coord[1][1] - cblk->coord[1][0],
1213                                     bandpos);
1214
1215                         x = cblk->coord[0][0];
1216                         y = cblk->coord[1][0];
1217
1218                         if (codsty->transform == FF_DWT97)
1219                             dequantization_float(x, y, cblk, comp, &t1, band);
1220                         else
1221                             dequantization_int(x, y, cblk, comp, &t1, band);
1222                    } /* end cblk */
1223                 } /*end prec */
1224             } /* end band */
1225         } /* end reslevel */
1226
1227         /* inverse DWT */
1228         ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1229     } /*end comp */
1230
1231     /* inverse MCT transformation */
1232     if (tile->codsty[0].mct)
1233         mct_decode(s, tile);
1234
1235     if (s->cdef[0] < 0) {
1236         for (x = 0; x < s->ncomponents; x++)
1237             s->cdef[x] = x + 1;
1238         if ((s->ncomponents & 1) == 0)
1239             s->cdef[s->ncomponents-1] = 0;
1240     }
1241
1242     if (s->precision <= 8) {
1243         for (compno = 0; compno < s->ncomponents; compno++) {
1244             Jpeg2000Component *comp = tile->comp + compno;
1245             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1246             float *datap = comp->f_data;
1247             int32_t *i_datap = comp->i_data;
1248             int cbps = s->cbps[compno];
1249             int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1250             int plane = 0;
1251
1252             if (planar)
1253                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1254
1255
1256             y    = tile->comp[compno].coord[1][0] - s->image_offset_y;
1257             line = picture->data[plane] + y / s->cdy[compno] * picture->linesize[plane];
1258             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1259                 uint8_t *dst;
1260
1261                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
1262                 dst = line + x / s->cdx[compno] * pixelsize + compno*!planar;
1263
1264                 if (codsty->transform == FF_DWT97) {
1265                     for (; x < w; x += s->cdx[compno]) {
1266                         int val = lrintf(*datap) + (1 << (cbps - 1));
1267                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1268                         val = av_clip(val, 0, (1 << cbps) - 1);
1269                         *dst = val << (8 - cbps);
1270                         datap++;
1271                         dst += pixelsize;
1272                     }
1273                 } else {
1274                     for (; x < w; x += s->cdx[compno]) {
1275                         int val = *i_datap + (1 << (cbps - 1));
1276                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1277                         val = av_clip(val, 0, (1 << cbps) - 1);
1278                         *dst = val << (8 - cbps);
1279                         i_datap++;
1280                         dst += pixelsize;
1281                     }
1282                 }
1283                 line += picture->linesize[plane];
1284             }
1285         }
1286     } else {
1287         for (compno = 0; compno < s->ncomponents; compno++) {
1288             Jpeg2000Component *comp = tile->comp + compno;
1289             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1290             float *datap = comp->f_data;
1291             int32_t *i_datap = comp->i_data;
1292             uint16_t *linel;
1293             int cbps = s->cbps[compno];
1294             int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1295             int plane = 0;
1296
1297             if (planar)
1298                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1299
1300             y     = tile->comp[compno].coord[1][0] - s->image_offset_y;
1301             linel = (uint16_t *)picture->data[plane] + y / s->cdy[compno] * (picture->linesize[plane] >> 1);
1302             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1303                 uint16_t *dst;
1304
1305                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
1306                 dst = linel + (x / s->cdx[compno] * pixelsize + compno*!planar);
1307                 if (codsty->transform == FF_DWT97) {
1308                     for (; x < w; x += s-> cdx[compno]) {
1309                         int  val = lrintf(*datap) + (1 << (cbps - 1));
1310                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1311                         val = av_clip(val, 0, (1 << cbps) - 1);
1312                         /* align 12 bit values in little-endian mode */
1313                         *dst = val << (16 - cbps);
1314                         datap++;
1315                         dst += pixelsize;
1316                     }
1317                 } else {
1318                     for (; x < w; x += s-> cdx[compno]) {
1319                         int val = *i_datap + (1 << (cbps - 1));
1320                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1321                         val = av_clip(val, 0, (1 << cbps) - 1);
1322                         /* align 12 bit values in little-endian mode */
1323                         *dst = val << (16 - cbps);
1324                         i_datap++;
1325                         dst += pixelsize;
1326                     }
1327                 }
1328                 linel += picture->linesize[plane] >> 1;
1329             }
1330         }
1331     }
1332
1333     return 0;
1334 }
1335
1336 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1337 {
1338     int tileno, compno;
1339     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1340         if (s->tile[tileno].comp) {
1341             for (compno = 0; compno < s->ncomponents; compno++) {
1342                 Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
1343                 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1344
1345                 ff_jpeg2000_cleanup(comp, codsty);
1346             }
1347             av_freep(&s->tile[tileno].comp);
1348         }
1349     }
1350     av_freep(&s->tile);
1351     memset(s->codsty, 0, sizeof(s->codsty));
1352     memset(s->qntsty, 0, sizeof(s->qntsty));
1353     s->numXtiles = s->numYtiles = 0;
1354 }
1355
1356 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1357 {
1358     Jpeg2000CodingStyle *codsty = s->codsty;
1359     Jpeg2000QuantStyle *qntsty  = s->qntsty;
1360     uint8_t *properties         = s->properties;
1361
1362     for (;;) {
1363         int len, ret = 0;
1364         uint16_t marker;
1365         int oldpos;
1366
1367         if (bytestream2_get_bytes_left(&s->g) < 2) {
1368             av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1369             break;
1370         }
1371
1372         marker = bytestream2_get_be16u(&s->g);
1373         oldpos = bytestream2_tell(&s->g);
1374
1375         if (marker == JPEG2000_SOD) {
1376             Jpeg2000Tile *tile;
1377             Jpeg2000TilePart *tp;
1378
1379             if (!s->tile) {
1380                 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1381                 return AVERROR_INVALIDDATA;
1382             }
1383             if (s->curtileno < 0) {
1384                 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1385                 return AVERROR_INVALIDDATA;
1386             }
1387
1388             tile = s->tile + s->curtileno;
1389             tp = tile->tile_part + tile->tp_idx;
1390             if (tp->tp_end < s->g.buffer) {
1391                 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1392                 return AVERROR_INVALIDDATA;
1393             }
1394             bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1395             bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1396
1397             continue;
1398         }
1399         if (marker == JPEG2000_EOC)
1400             break;
1401
1402         len = bytestream2_get_be16(&s->g);
1403         if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1404             return AVERROR_INVALIDDATA;
1405
1406         switch (marker) {
1407         case JPEG2000_SIZ:
1408             ret = get_siz(s);
1409             if (!s->tile)
1410                 s->numXtiles = s->numYtiles = 0;
1411             break;
1412         case JPEG2000_COC:
1413             ret = get_coc(s, codsty, properties);
1414             break;
1415         case JPEG2000_COD:
1416             ret = get_cod(s, codsty, properties);
1417             break;
1418         case JPEG2000_QCC:
1419             ret = get_qcc(s, len, qntsty, properties);
1420             break;
1421         case JPEG2000_QCD:
1422             ret = get_qcd(s, len, qntsty, properties);
1423             break;
1424         case JPEG2000_SOT:
1425             if (!(ret = get_sot(s, len))) {
1426                 av_assert1(s->curtileno >= 0);
1427                 codsty = s->tile[s->curtileno].codsty;
1428                 qntsty = s->tile[s->curtileno].qntsty;
1429                 properties = s->tile[s->curtileno].properties;
1430             }
1431             break;
1432         case JPEG2000_COM:
1433             // the comment is ignored
1434             bytestream2_skip(&s->g, len - 2);
1435             break;
1436         case JPEG2000_TLM:
1437             // Tile-part lengths
1438             ret = get_tlm(s, len);
1439             break;
1440         default:
1441             av_log(s->avctx, AV_LOG_ERROR,
1442                    "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1443                    marker, bytestream2_tell(&s->g) - 4);
1444             bytestream2_skip(&s->g, len - 2);
1445             break;
1446         }
1447         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1448             av_log(s->avctx, AV_LOG_ERROR,
1449                    "error during processing marker segment %.4"PRIx16"\n",
1450                    marker);
1451             return ret ? ret : -1;
1452         }
1453     }
1454     return 0;
1455 }
1456
1457 /* Read bit stream packets --> T2 operation. */
1458 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1459 {
1460     int ret = 0;
1461     int tileno;
1462
1463     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1464         Jpeg2000Tile *tile = s->tile + tileno;
1465
1466         if (ret = init_tile(s, tileno))
1467             return ret;
1468
1469         s->g = tile->tile_part[0].tpg;
1470         if (ret = jpeg2000_decode_packets(s, tile))
1471             return ret;
1472     }
1473
1474     return 0;
1475 }
1476
1477 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1478 {
1479     uint32_t atom_size, atom, atom_end;
1480     int search_range = 10;
1481
1482     while (search_range
1483            &&
1484            bytestream2_get_bytes_left(&s->g) >= 8) {
1485         atom_size = bytestream2_get_be32u(&s->g);
1486         atom      = bytestream2_get_be32u(&s->g);
1487         atom_end  = bytestream2_tell(&s->g) + atom_size - 8;
1488
1489         if (atom == JP2_CODESTREAM)
1490             return 1;
1491
1492         if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
1493             return 0;
1494
1495         if (atom == JP2_HEADER &&
1496                    atom_size >= 16) {
1497             uint32_t atom2_size, atom2, atom2_end;
1498             do {
1499                 atom2_size = bytestream2_get_be32u(&s->g);
1500                 atom2      = bytestream2_get_be32u(&s->g);
1501                 atom2_end  = bytestream2_tell(&s->g) + atom2_size - 8;
1502                 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1503                     break;
1504                 if (atom2 == JP2_CODESTREAM) {
1505                     return 1;
1506                 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
1507                     int method = bytestream2_get_byteu(&s->g);
1508                     bytestream2_skipu(&s->g, 2);
1509                     if (method == 1) {
1510                         s->colour_space = bytestream2_get_be32u(&s->g);
1511                     }
1512                 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
1513                     int i, size, colour_count, colour_channels, colour_depth[3];
1514                     uint32_t r, g, b;
1515                     colour_count = bytestream2_get_be16u(&s->g);
1516                     colour_channels = bytestream2_get_byteu(&s->g);
1517                     // FIXME: Do not ignore channel_sign
1518                     colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1519                     colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1520                     colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1521                     size = (colour_depth[0] + 7 >> 3) * colour_count +
1522                            (colour_depth[1] + 7 >> 3) * colour_count +
1523                            (colour_depth[2] + 7 >> 3) * colour_count;
1524                     if (colour_count > 256   ||
1525                         colour_channels != 3 ||
1526                         colour_depth[0] > 16 ||
1527                         colour_depth[1] > 16 ||
1528                         colour_depth[2] > 16 ||
1529                         atom2_size < size) {
1530                         avpriv_request_sample(s->avctx, "Unknown palette");
1531                         bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1532                         continue;
1533                     }
1534                     s->pal8 = 1;
1535                     for (i = 0; i < colour_count; i++) {
1536                         if (colour_depth[0] <= 8) {
1537                             r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
1538                             r |= r >> colour_depth[0];
1539                         } else {
1540                             r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
1541                         }
1542                         if (colour_depth[1] <= 8) {
1543                             g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
1544                             r |= r >> colour_depth[1];
1545                         } else {
1546                             g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
1547                         }
1548                         if (colour_depth[2] <= 8) {
1549                             b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
1550                             r |= r >> colour_depth[2];
1551                         } else {
1552                             b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
1553                         }
1554                         s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
1555                     }
1556                 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
1557                     int n = bytestream2_get_be16u(&s->g);
1558                     for (; n>0; n--) {
1559                         int cn   = bytestream2_get_be16(&s->g);
1560                         int av_unused typ  = bytestream2_get_be16(&s->g);
1561                         int asoc = bytestream2_get_be16(&s->g);
1562                         if (cn < 4 || asoc < 4)
1563                             s->cdef[cn] = asoc;
1564                     }
1565                 }
1566                 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1567             } while (atom_end - atom2_end >= 8);
1568         } else {
1569             search_range--;
1570         }
1571         bytestream2_seek(&s->g, atom_end, SEEK_SET);
1572     }
1573
1574     return 0;
1575 }
1576
1577 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
1578 {
1579     Jpeg2000DecoderContext *s = avctx->priv_data;
1580
1581     ff_jpeg2000dsp_init(&s->dsp);
1582
1583     return 0;
1584 }
1585
1586 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1587                                  int *got_frame, AVPacket *avpkt)
1588 {
1589     Jpeg2000DecoderContext *s = avctx->priv_data;
1590     ThreadFrame frame = { .f = data };
1591     AVFrame *picture = data;
1592     int tileno, ret;
1593
1594     s->avctx     = avctx;
1595     bytestream2_init(&s->g, avpkt->data, avpkt->size);
1596     s->curtileno = -1;
1597     memset(s->cdef, -1, sizeof(s->cdef));
1598
1599     if (bytestream2_get_bytes_left(&s->g) < 2) {
1600         ret = AVERROR_INVALIDDATA;
1601         goto end;
1602     }
1603
1604     // check if the image is in jp2 format
1605     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1606        (bytestream2_get_be32u(&s->g) == 12) &&
1607        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1608        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1609         if (!jp2_find_codestream(s)) {
1610             av_log(avctx, AV_LOG_ERROR,
1611                    "Could not find Jpeg2000 codestream atom.\n");
1612             ret = AVERROR_INVALIDDATA;
1613             goto end;
1614         }
1615     } else {
1616         bytestream2_seek(&s->g, 0, SEEK_SET);
1617     }
1618
1619     while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
1620         bytestream2_skip(&s->g, 1);
1621
1622     if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1623         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1624         ret = AVERROR_INVALIDDATA;
1625         goto end;
1626     }
1627     if (ret = jpeg2000_read_main_headers(s))
1628         goto end;
1629
1630     /* get picture buffer */
1631     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1632         goto end;
1633     picture->pict_type = AV_PICTURE_TYPE_I;
1634     picture->key_frame = 1;
1635
1636     if (ret = jpeg2000_read_bitstream_packets(s))
1637         goto end;
1638
1639     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1640         if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1641             goto end;
1642
1643     jpeg2000_dec_cleanup(s);
1644
1645     *got_frame = 1;
1646
1647     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
1648         memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
1649
1650     return bytestream2_tell(&s->g);
1651
1652 end:
1653     jpeg2000_dec_cleanup(s);
1654     return ret;
1655 }
1656
1657 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
1658 {
1659     ff_jpeg2000_init_tier1_luts();
1660     ff_mqc_init_context_tables();
1661 }
1662
1663 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1664 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1665
1666 static const AVOption options[] = {
1667     { "lowres",  "Lower the decoding resolution by a power of two",
1668         OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1669     { NULL },
1670 };
1671
1672 static const AVProfile profiles[] = {
1673     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0,  "JPEG 2000 codestream restriction 0"   },
1674     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1,  "JPEG 2000 codestream restriction 1"   },
1675     { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1676     { FF_PROFILE_JPEG2000_DCINEMA_2K,             "JPEG 2000 digital cinema 2K"          },
1677     { FF_PROFILE_JPEG2000_DCINEMA_4K,             "JPEG 2000 digital cinema 4K"          },
1678     { FF_PROFILE_UNKNOWN },
1679 };
1680
1681 static const AVClass jpeg2000_class = {
1682     .class_name = "jpeg2000",
1683     .item_name  = av_default_item_name,
1684     .option     = options,
1685     .version    = LIBAVUTIL_VERSION_INT,
1686 };
1687
1688 AVCodec ff_jpeg2000_decoder = {
1689     .name             = "jpeg2000",
1690     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1691     .type             = AVMEDIA_TYPE_VIDEO,
1692     .id               = AV_CODEC_ID_JPEG2000,
1693     .capabilities     = CODEC_CAP_FRAME_THREADS,
1694     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
1695     .init_static_data = jpeg2000_init_static_data,
1696     .init             = jpeg2000_decode_init,
1697     .decode           = jpeg2000_decode_frame,
1698     .priv_class       = &jpeg2000_class,
1699     .max_lowres       = 5,
1700     .profiles         = NULL_IF_CONFIG_SMALL(profiles)
1701 };