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