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