]> git.sesse.net Git - ffmpeg/blob - libavcodec/j2kdec.c
74095250f2d3ac9f0d165e4fd1cb48bdf3cff461
[ffmpeg] / libavcodec / j2kdec.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  * JPEG2000 image decoder
25  * @file
26  * @author Kamil Nowosad
27  */
28
29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32 #include "avcodec.h"
33 #include "bytestream.h"
34 #include "internal.h"
35 #include "thread.h"
36 #include "jpeg2000.h"
37
38 #define JP2_SIG_TYPE    0x6A502020
39 #define JP2_SIG_VALUE   0x0D0A870A
40 #define JP2_CODESTREAM  0x6A703263
41
42 #define HAD_COC 0x01
43 #define HAD_QCC 0x02
44
45 typedef struct Jpeg2000Tile {
46     Jpeg2000Component  *comp;
47     uint8_t             properties[4];
48     Jpeg2000CodingStyle codsty[4];
49     Jpeg2000QuantStyle  qntsty[4];
50 } Jpeg2000Tile;
51
52 typedef struct Jpeg2000DecoderContext {
53     AVClass         *class;
54     AVCodecContext  *avctx;
55     AVFrame         *picture;
56     GetByteContext  g;
57
58     int             width, height;
59     int             image_offset_x, image_offset_y;
60     int             tile_offset_x, tile_offset_y;
61     uint8_t         cbps[4];    // bits per sample in particular components
62     uint8_t         sgnd[4];    // if a component is signed
63     uint8_t         properties[4];
64     int             cdx[4], cdy[4];
65     int             precision;
66     int             ncomponents;
67     int             tile_width, tile_height;
68     int             numXtiles, numYtiles;
69     int             maxtilelen;
70
71     Jpeg2000CodingStyle codsty[4];
72     Jpeg2000QuantStyle  qntsty[4];
73
74     int             bit_index;
75
76     int             curtileno;
77
78     Jpeg2000Tile    *tile;
79
80     /*options parameters*/
81     int             lowres;
82     int             reduction_factor;
83 } Jpeg2000DecoderContext;
84
85 /* get_bits functions for JPEG2000 packet bitstream
86  * It is a get_bit function with a bit-stuffing routine. If the value of the
87  * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
88  * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
89 static int get_bits(Jpeg2000DecoderContext *s, int n)
90 {
91     int res = 0;
92
93     while (--n >= 0) {
94         res <<= 1;
95         if (s->bit_index == 0) {
96             s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
97         }
98         s->bit_index--;
99         res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
100     }
101     return res;
102 }
103
104 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
105 {
106     if (bytestream2_get_byte(&s->g) == 0xff)
107         bytestream2_skip(&s->g, 1);
108     s->bit_index = 8;
109 }
110
111 /* decode the value stored in node */
112 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
113                            int threshold)
114 {
115     Jpeg2000TgtNode *stack[30];
116     int sp = -1, curval = 0;
117
118     if (!node)
119         return AVERROR(EINVAL);
120
121     while (node && !node->vis) {
122         stack[++sp] = node;
123         node        = node->parent;
124     }
125
126     if (node)
127         curval = node->val;
128     else
129         curval = stack[sp]->val;
130
131     while (curval < threshold && sp >= 0) {
132         if (curval < stack[sp]->val)
133             curval = stack[sp]->val;
134         while (curval < threshold) {
135             int ret;
136             if ((ret = get_bits(s, 1)) > 0) {
137                 stack[sp]->vis++;
138                 break;
139             } else if (!ret)
140                 curval++;
141             else
142                 return ret;
143         }
144         stack[sp]->val = curval;
145         sp--;
146     }
147     return curval;
148 }
149
150 /* marker segments */
151 /* get sizes and offsets of image, tiles; number of components */
152 static int get_siz(Jpeg2000DecoderContext *s)
153 {
154     int i, ret;
155     ThreadFrame frame = { .f = s->picture };
156
157     if (bytestream2_get_bytes_left(&s->g) < 36)
158         return AVERROR(EINVAL);
159
160     s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
161     s->width          = bytestream2_get_be32u(&s->g); // Width
162     s->height         = bytestream2_get_be32u(&s->g); // Height
163     s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
164     s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
165     s->tile_width     = bytestream2_get_be32u(&s->g); // XTSiz
166     s->tile_height    = bytestream2_get_be32u(&s->g); // YTSiz
167     s->tile_offset_x  = bytestream2_get_be32u(&s->g); // XT0Siz
168     s->tile_offset_y  = bytestream2_get_be32u(&s->g); // YT0Siz
169     s->ncomponents    = bytestream2_get_be16u(&s->g); // CSiz
170
171     if (s->ncomponents <= 0 || s->ncomponents > 4) {
172         av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
173         return AVERROR(EINVAL);
174     }
175     if (s->tile_width<=0 || s->tile_height<=0)
176         return AVERROR(EINVAL);
177
178     if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
179         return AVERROR(EINVAL);
180
181     for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
182         uint8_t x = bytestream2_get_byteu(&s->g);
183         s->cbps[i]   = (x & 0x7f) + 1;
184         s->precision = FFMAX(s->cbps[i], s->precision);
185         s->sgnd[i] = !!(x & 0x80);
186         s->cdx[i] = bytestream2_get_byteu(&s->g);
187         s->cdy[i] = bytestream2_get_byteu(&s->g);
188         if (s->cdx[i] != 1 || s->cdy[i] != 1) {
189             av_log(s->avctx, AV_LOG_ERROR, "unsupported/ CDxy values\n");
190         }
191     }
192
193     s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
194     s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
195
196     if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
197         return AVERROR(EINVAL);
198
199     s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
200     if (!s->tile)
201         return AVERROR(ENOMEM);
202
203     for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
204         Jpeg2000Tile *tile = s->tile + i;
205
206         tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
207         if (!tile->comp)
208             return AVERROR(ENOMEM);
209     }
210
211     /* compute image size with reduction factor */
212     s->avctx->width  = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
213                                                s->reduction_factor);
214     s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
215                                                s->reduction_factor);
216
217     switch(s->ncomponents) {
218     case 1:
219         if (s->precision > 8)
220             s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
221         else
222             s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
223         break;
224     case 3:
225         switch (s->avctx->profile) {
226         case FF_PROFILE_JPEG2000_DCINEMA_2K:
227         case FF_PROFILE_JPEG2000_DCINEMA_4K:
228             /* XYZ color-space for digital cinema profiles */
229             s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
230             break;
231         default:
232             if (s->precision > 8)
233                 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
234             else
235                 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
236             break;
237         }
238         break;
239     case 4:
240         s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
241         break;
242     default:
243         /* pixel format can not be identified */
244         s->avctx->pix_fmt = AV_PIX_FMT_NONE;
245         break;
246     }
247
248
249     if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 0)
250         return ret;
251
252     s->picture->pict_type = AV_PICTURE_TYPE_I;
253     s->picture->key_frame = 1;
254
255     return 0;
256 }
257
258 /* get common part for COD and COC segments */
259 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
260 {
261     uint8_t byte;
262
263     if (bytestream2_get_bytes_left(&s->g) < 5)
264         return AVERROR(EINVAL);
265     c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
266     if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
267         av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
268         return AVERROR_INVALIDDATA;
269     }
270
271     /* compute number of resolution levels to decode */
272     if (c->nreslevels < s->reduction_factor)
273         c->nreslevels2decode = 1;
274     else
275         c->nreslevels2decode = c->nreslevels - s->reduction_factor;
276
277     c->log2_cblk_width  = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
278     c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
279
280     if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
281         c->log2_cblk_width + c->log2_cblk_height > 14) {
282         av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
283         return AVERROR_INVALIDDATA;
284     }
285
286     c->cblk_style = bytestream2_get_byteu(&s->g);
287     if (c->cblk_style != 0) { // cblk style
288         av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
289     }
290     c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
291     /* set integer 9/7 DWT in case of BITEXACT flag */
292     if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
293         c->transform = FF_DWT97_INT;
294
295     if (c->csty & JPEG2000_CSTY_PREC) {
296         int i;
297         for (i = 0; i < c->nreslevels; i++) {
298             byte = bytestream2_get_byte(&s->g);
299             c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
300             c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
301         }
302     } else {
303         memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
304         memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
305     }
306     return 0;
307 }
308
309 /* get coding parameters for a particular tile or whole image*/
310 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
311                    uint8_t *properties)
312 {
313     Jpeg2000CodingStyle tmp;
314     int compno;
315
316     if (bytestream2_get_bytes_left(&s->g) < 5)
317         return AVERROR(EINVAL);
318
319     tmp.csty = bytestream2_get_byteu(&s->g);
320
321     // get progression order
322     tmp.prog_order = bytestream2_get_byteu(&s->g);
323     if (tmp.prog_order) {
324         av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
325     }
326
327     tmp.nlayers = bytestream2_get_be16u(&s->g);
328     tmp.mct     = bytestream2_get_byteu(&s->g); // multiple component transformation
329
330     get_cox(s, &tmp);
331     for (compno = 0; compno < s->ncomponents; compno++)
332         if (!(properties[compno] & HAD_COC))
333             memcpy(c + compno, &tmp, sizeof(tmp));
334     return 0;
335 }
336
337 /* Get coding parameters for a component in the whole image or a
338  * particular tile. */
339 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
340                    uint8_t *properties)
341 {
342     int compno;
343
344     if (bytestream2_get_bytes_left(&s->g) < 2)
345         return AVERROR(EINVAL);
346
347     compno = bytestream2_get_byteu(&s->g);
348
349     c += compno;
350     c->csty = bytestream2_get_byteu(&s->g);
351     get_cox(s, c);
352
353     properties[compno] |= HAD_COC;
354     return 0;
355 }
356
357 /* Get common part for QCD and QCC segments. */
358 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
359 {
360     int i, x;
361
362     if (bytestream2_get_bytes_left(&s->g) < 1)
363         return AVERROR(EINVAL);
364
365     x = bytestream2_get_byteu(&s->g); // Sqcd
366
367     q->nguardbits = x >> 5;
368     q->quantsty   = x & 0x1f;
369
370     if (q->quantsty == JPEG2000_QSTY_NONE) {
371         n -= 3;
372         if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
373             return AVERROR(EINVAL);
374         for (i = 0; i < n; i++)
375             q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
376     } else if (q->quantsty == JPEG2000_QSTY_SI) {
377         if (bytestream2_get_bytes_left(&s->g) < 2)
378             return AVERROR(EINVAL);
379         x          = bytestream2_get_be16u(&s->g);
380         q->expn[0] = x >> 11;
381         q->mant[0] = x & 0x7ff;
382         for (i = 1; i < 32 * 3; i++) {
383             int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
384             q->expn[i] = curexpn;
385             q->mant[i] = q->mant[0];
386         }
387     } else {
388         n = (n - 3) >> 1;
389         if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
390             return AVERROR(EINVAL);
391         for (i = 0; i < n; i++) {
392             x          = bytestream2_get_be16u(&s->g);
393             q->expn[i] = x >> 11;
394             q->mant[i] = x & 0x7ff;
395         }
396     }
397     return 0;
398 }
399
400 /* Get quantization parameters for a particular tile or a whole image. */
401 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
402                    uint8_t *properties)
403 {
404     Jpeg2000QuantStyle tmp;
405     int compno;
406
407     if (get_qcx(s, n, &tmp))
408         return -1;
409     for (compno = 0; compno < s->ncomponents; compno++)
410         if (!(properties[compno] & HAD_QCC))
411             memcpy(q + compno, &tmp, sizeof(tmp));
412     return 0;
413 }
414
415 /* Get quantization parameters for a component in the whole image
416  * on in a particular tile. */
417 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
418                    uint8_t *properties)
419 {
420     int compno;
421
422     if (bytestream2_get_bytes_left(&s->g) < 1)
423         return AVERROR(EINVAL);
424
425     compno              = bytestream2_get_byteu(&s->g);
426     properties[compno] |= HAD_QCC;
427     return get_qcx(s, n - 1, q + compno);
428 }
429
430 /* get start of tile segment */
431 static int get_sot(Jpeg2000DecoderContext *s)
432 {
433     if (bytestream2_get_bytes_left(&s->g) < 8)
434         return AVERROR(EINVAL);
435
436     s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
437     if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
438         s->curtileno=0;
439         return AVERROR(EINVAL);
440     }
441
442     bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
443
444     if (!bytestream2_get_byteu(&s->g)) { ///< TPsot
445         Jpeg2000Tile *tile = s->tile + s->curtileno;
446
447         /* copy defaults */
448         memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
449         memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
450     }
451     bytestream2_get_byteu(&s->g); ///< TNsot
452
453     return 0;
454 }
455
456 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
457  * Used to know the number of tile parts and lengths.
458  * There may be multiple TLMs in the header.
459  * TODO: The function is not used for tile-parts management, nor anywhere else.
460  * It can be useful to allocate memory for tile parts, before managing the SOT
461  * markers. Parsing the TLM header is needed to increment the input header
462  * buffer.
463  * This marker is mandatory for DCI. */
464 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
465 {
466     uint8_t Stlm, ST, SP, tile_tlm, i;
467     bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
468     Stlm = bytestream2_get_byte(&s->g);
469
470     // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
471     ST = (Stlm >> 4) & 0x03;
472     // TODO: Manage case of ST = 0b11 --> raise error
473     SP       = (Stlm >> 6) & 0x01;
474     tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
475     for (i = 0; i < tile_tlm; i++) {
476         switch (ST) {
477         case 0:
478             break;
479         case 1:
480             bytestream2_get_byte(&s->g);
481             break;
482         case 2:
483             bytestream2_get_be16(&s->g);
484             break;
485         case 3:
486             bytestream2_get_be32(&s->g);
487             break;
488         }
489         if (SP == 0) {
490             bytestream2_get_be16(&s->g);
491         } else {
492             bytestream2_get_be32(&s->g);
493         }
494     }
495     return 0;
496 }
497
498 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
499 {
500     int compno;
501     int tilex = tileno % s->numXtiles;
502     int tiley = tileno / s->numXtiles;
503     Jpeg2000Tile *tile = s->tile + tileno;
504
505     if (!tile->comp)
506         return AVERROR(ENOMEM);
507     for (compno = 0; compno < s->ncomponents; compno++) {
508         Jpeg2000Component *comp = tile->comp + compno;
509         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
510         Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
511         int ret; // global bandno
512
513         comp->coord_o[0][0] = FFMAX(tilex       * s->tile_width  + s->tile_offset_x, s->image_offset_x);
514         comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width  + s->tile_offset_x, s->width);
515         comp->coord_o[1][0] = FFMAX(tiley       * s->tile_height + s->tile_offset_y, s->image_offset_y);
516         comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
517
518         comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
519         comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
520         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
521         comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
522
523         if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno], s->avctx))
524             return ret;
525     }
526     return 0;
527 }
528
529 /* read the number of coding passes */
530 static int getnpasses(Jpeg2000DecoderContext *s)
531 {
532     int num;
533     if (!get_bits(s, 1))
534         return 1;
535     if (!get_bits(s, 1))
536         return 2;
537     if ((num = get_bits(s, 2)) != 3)
538         return num < 0 ? num : 3 + num;
539     if ((num = get_bits(s, 5)) != 31)
540         return num < 0 ? num : 6 + num;
541     num = get_bits(s, 7);
542     return num < 0 ? num : 37 + num;
543 }
544
545 static int getlblockinc(Jpeg2000DecoderContext *s)
546 {
547     int res = 0, ret;
548     while (ret = get_bits(s, 1)) {
549         if (ret < 0)
550             return ret;
551         res++;
552     }
553     return res;
554 }
555
556 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
557                                   Jpeg2000CodingStyle *codsty,
558                                   Jpeg2000ResLevel *rlevel, int precno,
559                                   int layno, uint8_t *expn, int numgbits)
560 {
561     int bandno, cblkno, ret, nb_code_blocks;
562
563     if (!(ret = get_bits(s, 1))) {
564         jpeg2000_flush(s);
565         return 0;
566     } else if (ret < 0)
567         return ret;
568
569     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
570         Jpeg2000Band *band = rlevel->band + bandno;
571         Jpeg2000Prec *prec = band->prec + precno;
572
573         if (band->coord[0][0] == band->coord[0][1] ||
574             band->coord[1][0] == band->coord[1][1])
575             continue;
576
577         nb_code_blocks =  prec->nb_codeblocks_height *
578                           prec->nb_codeblocks_width;
579         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
580             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
581             int incl, newpasses, llen;
582
583             if (cblk->npasses)
584                 incl = get_bits(s, 1);
585             else
586                 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
587             if (!incl)
588                 continue;
589             else if (incl < 0)
590                 return incl;
591
592             if (!cblk->npasses)
593                 cblk->nonzerobits = expn[bandno] + numgbits - 1 -
594                                     tag_tree_decode(s, prec->zerobits + cblkno,
595                                                     100);
596             if ((newpasses = getnpasses(s)) < 0)
597                 return newpasses;
598             if ((llen = getlblockinc(s)) < 0)
599                 return llen;
600             cblk->lblock += llen;
601             if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
602                 return ret;
603             cblk->lengthinc = ret;
604             cblk->npasses  += newpasses;
605         }
606     }
607     jpeg2000_flush(s);
608
609     if (codsty->csty & JPEG2000_CSTY_EPH) {
610         if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
611             bytestream2_skip(&s->g, 2);
612         else
613             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
614     }
615
616     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
617         Jpeg2000Band *band = rlevel->band + bandno;
618         Jpeg2000Prec *prec = band->prec + precno;
619
620         nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
621         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
622             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
623             if (   bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
624                 || sizeof(cblk->data) < cblk->lengthinc
625             )
626                 return AVERROR(EINVAL);
627             /* Code-block data can be empty. In that case initialize data
628              * with 0xFFFF. */
629             if (cblk->lengthinc > 0) {
630                  bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
631             } else {
632                 cblk->data[0] = 0xFF;
633                 cblk->data[1] = 0xFF;
634             }
635             cblk->length   += cblk->lengthinc;
636             cblk->lengthinc = 0;
637         }
638     }
639     return 0;
640 }
641
642 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
643 {
644     int layno, reslevelno, compno, precno, ok_reslevel;
645     s->bit_index = 8;
646     for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
647         ok_reslevel = 1;
648         for (reslevelno = 0; ok_reslevel; reslevelno++) {
649             ok_reslevel = 0;
650             for (compno = 0; compno < s->ncomponents; compno++) {
651                 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
652                 Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
653                 if (reslevelno < codsty->nreslevels) {
654                     Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
655                                                reslevelno;
656                     ok_reslevel = 1;
657                     for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
658                         if (jpeg2000_decode_packet(s,
659                                                    codsty, rlevel,
660                                                    precno, layno,
661                                                    qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
662                                                    qntsty->nguardbits))
663                             return -1;
664                 }
665             }
666         }
667     }
668     return 0;
669 }
670
671 /* TIER-1 routines */
672 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
673                            int bpno, int bandno, int bpass_csty_symbol,
674                            int vert_causal_ctx_csty_symbol)
675 {
676     int mask = 3 << (bpno - 1), y0, x, y;
677
678     for (y0 = 0; y0 < height; y0 += 4)
679         for (x = 0; x < width; x++)
680             for (y = y0; y < height && y < y0 + 4; y++) {
681                 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
682                 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
683                     int flags_mask = -1;
684                     if (vert_causal_ctx_csty_symbol && y == y0 + 3)
685                         flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
686                     if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
687                         int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
688                         if (bpass_csty_symbol)
689                              t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
690                         else
691                              t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
692                                                -mask : mask;
693
694                         ff_jpeg2000_set_significance(t1, x, y,
695                                                      t1->data[y][x] < 0);
696                     }
697                     t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
698                 }
699             }
700 }
701
702 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
703                            int bpno)
704 {
705     int phalf, nhalf;
706     int y0, x, y;
707
708     phalf = 1 << (bpno - 1);
709     nhalf = -phalf;
710
711     for (y0 = 0; y0 < height; y0 += 4)
712         for (x = 0; x < width; x++)
713             for (y = y0; y < height && y < y0 + 4; y++)
714                 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
715                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
716                     int r     = ff_mqc_decode(&t1->mqc,
717                                               t1->mqc.cx_states + ctxno)
718                                 ? phalf : nhalf;
719                     t1->data[y][x]          += t1->data[y][x] < 0 ? -r : r;
720                     t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
721                 }
722 }
723
724 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
725                            int width, int height, int bpno, int bandno,
726                            int seg_symbols, int vert_causal_ctx_csty_symbol)
727 {
728     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
729
730     for (y0 = 0; y0 < height; y0 += 4) {
731         for (x = 0; x < width; x++) {
732             if (y0 + 3 < height &&
733                 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
734                   (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
735                   (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
736                   (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
737                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
738                     continue;
739                 runlen = ff_mqc_decode(&t1->mqc,
740                                        t1->mqc.cx_states + MQC_CX_UNI);
741                 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
742                                                        t1->mqc.cx_states +
743                                                        MQC_CX_UNI);
744                 dec = 1;
745             } else {
746                 runlen = 0;
747                 dec    = 0;
748             }
749
750             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
751                 if (!dec) {
752                     if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
753                         int flags_mask = -1;
754                         if (vert_causal_ctx_csty_symbol && y == y0 + 3)
755                             flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
756                         dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
757                                                                                              bandno));
758                     }
759                 }
760                 if (dec) {
761                     int xorbit;
762                     int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
763                                                         &xorbit);
764                     t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
765                                                     t1->mqc.cx_states + ctxno) ^
766                                       xorbit)
767                                      ? -mask : mask;
768                     ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
769                 }
770                 dec = 0;
771                 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
772             }
773         }
774     }
775     if (seg_symbols) {
776         int val;
777         val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
778         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
779         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
780         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
781         if (val != 0xa)
782             av_log(s->avctx, AV_LOG_ERROR,
783                    "Segmentation symbol value incorrect\n");
784     }
785 }
786
787 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
788                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
789                        int width, int height, int bandpos)
790 {
791     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
792     int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
793     int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
794
795     for (y = 0; y < height; y++)
796         memset(t1->data[y], 0, width * sizeof(**t1->data));
797
798     /* If code-block contains no compressed data: nothing to do. */
799     if (!cblk->length)
800         return 0;
801
802     for (y = 0; y < height+2; y++)
803         memset(t1->flags[y], 0, (width + 2)*sizeof(**t1->flags));
804
805     cblk->data[cblk->length] = 0xff;
806     cblk->data[cblk->length+1] = 0xff;
807     ff_mqc_initdec(&t1->mqc, cblk->data);
808
809     while (passno--) {
810         switch(pass_t) {
811         case 0:
812             decode_sigpass(t1, width, height, bpno + 1, bandpos,
813                            bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
814             break;
815         case 1:
816             decode_refpass(t1, width, height, bpno + 1);
817             if (bpass_csty_symbol && clnpass_cnt >= 4)
818                 ff_mqc_initdec(&t1->mqc, cblk->data);
819             break;
820         case 2:
821             decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
822                            codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
823             clnpass_cnt = clnpass_cnt + 1;
824             if (bpass_csty_symbol && clnpass_cnt >= 4)
825                 ff_mqc_initdec(&t1->mqc, cblk->data);
826             break;
827         }
828
829         pass_t++;
830         if (pass_t == 3) {
831             bpno--;
832             pass_t = 0;
833         }
834     }
835     return 0;
836 }
837
838 /* Float dequantization of a codeblock.*/
839 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
840                                  Jpeg2000Component *comp,
841                                  Jpeg2000T1Context *t1, Jpeg2000Band *band)
842 {
843     int i, j, idx;
844     float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
845     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
846         for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
847             idx        = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
848             datap[idx] = (float)(t1->data[j][i]) * band->f_stepsize;
849         }
850 }
851
852 /* Integer dequantization of a codeblock.*/
853 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
854                                Jpeg2000Component *comp,
855                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
856 {
857     int i, j, idx;
858     int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
859     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
860         for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
861             idx        = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
862             datap[idx] =
863                 ((int32_t)(t1->data[j][i]) * band->i_stepsize + (1 << 15)) >> 16;
864         }
865 }
866
867 /* Inverse ICT parameters in float and integer.
868  * int value = (float value) * (1<<16) */
869 static const float f_ict_params[4] = {
870     1.402f,
871     0.34413f,
872     0.71414f,
873     1.772f
874 };
875 static const int   i_ict_params[4] = {
876      91881,
877      22553,
878      46802,
879     116130
880 };
881
882 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
883 {
884     int i, csize = 1;
885     int32_t *src[3],  i0,  i1,  i2;
886     float   *srcf[3], i0f, i1f, i2f;
887
888     for (i = 0; i < 3; i++)
889         if (tile->codsty[0].transform == FF_DWT97)
890             srcf[i] = tile->comp[i].f_data;
891         else
892             src [i] = tile->comp[i].i_data;
893
894     for (i = 0; i < 2; i++)
895         csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
896
897     switch (tile->codsty[0].transform) {
898     case FF_DWT97:
899         for (i = 0; i < csize; i++) {
900             i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
901             i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
902                            - (f_ict_params[2] * *srcf[2]);
903             i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
904             *srcf[0]++ = i0f;
905             *srcf[1]++ = i1f;
906             *srcf[2]++ = i2f;
907         }
908         break;
909     case FF_DWT97_INT:
910         for (i = 0; i < csize; i++) {
911             i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
912             i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
913                          - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
914             i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
915             *src[0]++ = i0;
916             *src[1]++ = i1;
917             *src[2]++ = i2;
918         }
919         break;
920     case FF_DWT53:
921         for (i = 0; i < csize; i++) {
922             i1 = *src[0] - (*src[2] + *src[1] >> 2);
923             i0 = i1 + *src[2];
924             i2 = i1 + *src[1];
925             *src[0]++ = i0;
926             *src[1]++ = i1;
927             *src[2]++ = i2;
928         }
929         break;
930     }
931 }
932
933 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
934                                 AVFrame *picture)
935 {
936     int compno, reslevelno, bandno;
937     int x, y;
938
939     uint8_t *line;
940     Jpeg2000T1Context t1;
941
942     /* Loop on tile components */
943     for (compno = 0; compno < s->ncomponents; compno++) {
944         Jpeg2000Component *comp     = tile->comp + compno;
945         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
946
947         /* Loop on resolution levels */
948         for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
949             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
950             /* Loop on bands */
951             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
952                 int nb_precincts, precno;
953                 Jpeg2000Band *band = rlevel->band + bandno;
954                 int cblkno=0, bandpos;
955
956                 bandpos = bandno + (reslevelno > 0);
957
958                 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
959                     continue;
960
961                 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
962                 /* Loop on precincts */
963                 for (precno = 0; precno < nb_precincts; precno++) {
964                     Jpeg2000Prec *prec = band->prec + precno;
965
966                     /* Loop on codeblocks */
967                     for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
968                         int x, y;
969                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
970                         decode_cblk(s, codsty, &t1, cblk,
971                                     cblk->coord[0][1] - cblk->coord[0][0],
972                                     cblk->coord[1][1] - cblk->coord[1][0],
973                                     bandpos);
974
975                         /* Manage band offsets */
976                         x = cblk->coord[0][0];
977                         y = cblk->coord[1][0];
978
979                         if (codsty->transform == FF_DWT97)
980                             dequantization_float(x, y, cblk, comp, &t1, band);
981                         else
982                             dequantization_int(x, y, cblk, comp, &t1, band);
983                    } /* end cblk */
984                 } /*end prec */
985             } /* end band */
986         } /* end reslevel */
987
988         ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
989     } /*end comp */
990
991     /* inverse MCT transformation */
992     if (tile->codsty[0].mct)
993         mct_decode(s, tile);
994
995     if (s->precision <= 8) {
996         for (compno = 0; compno < s->ncomponents; compno++) {
997             Jpeg2000Component *comp = tile->comp + compno;
998             float *datap = comp->f_data;
999             int32_t *i_datap = comp->i_data;
1000
1001             y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1002             line = picture->data[0] + y * picture->linesize[0];
1003             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1004                 uint8_t *dst;
1005
1006                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
1007                 dst = line + x * s->ncomponents + compno;
1008
1009                 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
1010                      int val;
1011                     /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1012                     if (tile->codsty->transform == FF_DWT97)
1013                         val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
1014                     else
1015                         val = *i_datap + (1 << (s->cbps[compno] - 1));
1016                     val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
1017                     *dst = val << (8 - s->cbps[compno]);
1018                     datap++;
1019                     i_datap++;
1020                     dst += s->ncomponents;
1021                 }
1022                 line += picture->linesize[0];
1023             }
1024         }
1025     } else {
1026         for (compno = 0; compno < s->ncomponents; compno++) {
1027             Jpeg2000Component *comp = tile->comp + compno;
1028             float *datap = comp->f_data;
1029             int32_t *i_datap = comp->i_data;
1030             uint16_t *linel;
1031
1032             y     = tile->comp[compno].coord[1][0] - s->image_offset_y;
1033             linel = (uint16_t*)picture->data[0] + y * (picture->linesize[0] >> 1);
1034             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1035                 uint16_t *dst;
1036
1037                 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1038                 dst = linel + (x * s->ncomponents + compno);
1039                 for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
1040                      int val;
1041                     /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1042                     if (tile->codsty->transform == FF_DWT97)
1043                         val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
1044                     else
1045                         val = *i_datap + (1 << (s->cbps[compno] - 1));
1046                     val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
1047                     /* align 12 bit values in little-endian mode */
1048                     *dst = val << (16 - s->cbps[compno]);
1049                     datap++;
1050                     i_datap++;
1051                     dst += s->ncomponents;
1052                 }
1053                 linel += picture->linesize[0]>>1;
1054             }
1055         }
1056     }
1057     return 0;
1058 }
1059
1060 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1061 {
1062     int tileno, compno;
1063     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1064         for (compno = 0; compno < s->ncomponents; compno++) {
1065             Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
1066             Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1067
1068             ff_jpeg2000_cleanup(comp, codsty);
1069         }
1070         av_freep(&s->tile[tileno].comp);
1071     }
1072     av_freep(&s->tile);
1073 }
1074
1075 static int jpeg2000_decode_codestream(Jpeg2000DecoderContext *s)
1076 {
1077     Jpeg2000CodingStyle *codsty = s->codsty;
1078     Jpeg2000QuantStyle *qntsty  = s->qntsty;
1079     uint8_t *properties         = s->properties;
1080
1081     for (;;) {
1082         int len, ret = 0;
1083         int marker;
1084         int oldpos;
1085
1086         if (bytestream2_get_bytes_left(&s->g) < 2) {
1087             av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1088             break;
1089         }
1090
1091         marker = bytestream2_get_be16u(&s->g);
1092         oldpos = bytestream2_tell(&s->g);
1093
1094         if (marker == JPEG2000_SOD) {
1095             Jpeg2000Tile *tile = s->tile + s->curtileno;
1096             if (ret = init_tile(s, s->curtileno)) {
1097                 av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
1098                 return ret;
1099             }
1100             if (ret = jpeg2000_decode_packets(s, tile)) {
1101                 av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
1102                 return ret;
1103             }
1104             continue;
1105         }
1106         if (marker == JPEG2000_EOC)
1107             break;
1108
1109         if (bytestream2_get_bytes_left(&s->g) < 2)
1110             return AVERROR(EINVAL);
1111         len = bytestream2_get_be16u(&s->g);
1112         switch (marker) {
1113         case JPEG2000_SIZ:
1114             ret = get_siz(s);
1115             if (!s->tile)
1116                 s->numXtiles = s->numYtiles = 0;
1117             break;
1118         case JPEG2000_COC:
1119             ret = get_coc(s, codsty, properties);
1120             break;
1121         case JPEG2000_COD:
1122             ret = get_cod(s, codsty, properties);
1123             break;
1124         case JPEG2000_QCC:
1125             ret = get_qcc(s, len, qntsty, properties);
1126             break;
1127         case JPEG2000_QCD:
1128             ret = get_qcd(s, len, qntsty, properties);
1129             break;
1130         case JPEG2000_SOT:
1131             if (!(ret = get_sot(s))) {
1132                 codsty = s->tile[s->curtileno].codsty;
1133                 qntsty = s->tile[s->curtileno].qntsty;
1134                 properties = s->tile[s->curtileno].properties;
1135             }
1136             break;
1137         case JPEG2000_COM:
1138             // the comment is ignored
1139             bytestream2_skip(&s->g, len - 2);
1140             break;
1141         case JPEG2000_TLM:
1142             // Tile-part lengths
1143             ret = get_tlm(s, len);
1144             break;
1145         default:
1146             av_log(s->avctx, AV_LOG_ERROR,
1147                    "unsupported marker 0x%.4X at pos 0x%X\n",
1148                    marker, bytestream2_tell(&s->g) - 4);
1149             bytestream2_skip(&s->g, len - 2);
1150             break;
1151         }
1152         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1153             av_log(s->avctx, AV_LOG_ERROR,
1154                    "error during processing marker segment %.4x\n", marker);
1155             return ret ? ret : -1;
1156         }
1157     }
1158     return 0;
1159 }
1160
1161 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1162 {
1163     uint32_t atom_size, atom;
1164     int found_codestream = 0, search_range = 10;
1165
1166     while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1167         atom_size = bytestream2_get_be32u(&s->g);
1168         atom      = bytestream2_get_be32u(&s->g);
1169         if (atom == JP2_CODESTREAM) {
1170             found_codestream = 1;
1171         } else {
1172             if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1173                 return 0;
1174             bytestream2_skipu(&s->g, atom_size - 8);
1175             search_range--;
1176         }
1177     }
1178
1179     if (found_codestream)
1180         return 1;
1181     return 0;
1182 }
1183
1184 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1185                                  int *got_frame, AVPacket *avpkt)
1186 {
1187     Jpeg2000DecoderContext *s = avctx->priv_data;
1188     AVFrame *picture = data;
1189     int tileno, ret;
1190
1191     s->picture = picture;
1192
1193     s->avctx     = avctx;
1194     bytestream2_init(&s->g, avpkt->data, avpkt->size);
1195     s->curtileno = -1;
1196
1197     // reduction factor, i.e number of resolution levels to skip
1198     s->reduction_factor = avctx->lowres;
1199
1200     if (bytestream2_get_bytes_left(&s->g) < 2) {
1201         ret = AVERROR(EINVAL);
1202         goto err_out;
1203     }
1204
1205     // check if the image is in jp2 format
1206     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1207        (bytestream2_get_be32u(&s->g) == 12) &&
1208        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1209        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1210         if (!jp2_find_codestream(s)) {
1211             av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1212             ret = -1;
1213             goto err_out;
1214         }
1215     } else {
1216         bytestream2_seek(&s->g, 0, SEEK_SET);
1217     }
1218
1219     if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1220         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1221         ret = -1;
1222         goto err_out;
1223     }
1224     if (ret = jpeg2000_decode_codestream(s))
1225         goto err_out;
1226
1227     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1228         if (ret = jpeg2000_decode_tile(s, s->tile + tileno, s->picture))
1229             goto err_out;
1230
1231     jpeg2000_dec_cleanup(s);
1232
1233     *got_frame = 1;
1234
1235     return bytestream2_tell(&s->g);
1236
1237 err_out:
1238     jpeg2000_dec_cleanup(s);
1239     return ret;
1240 }
1241
1242 static void jpeg2000_init_static_data(AVCodec *codec)
1243 {
1244     ff_jpeg2000_init_tier1_luts();
1245 }
1246
1247 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1248 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1249
1250 static const AVOption options[] = {
1251     { "lowres",  "Lower the decoding resolution by a power of two",
1252         OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1253     { NULL },
1254 };
1255
1256 static const AVProfile profiles[] = {
1257     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0,  "JPEG 2000 codestream restriction 0"   },
1258     { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1,  "JPEG 2000 codestream restriction 1"   },
1259     { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1260     { FF_PROFILE_JPEG2000_DCINEMA_2K,             "JPEG 2000 digital cinema 2K"          },
1261     { FF_PROFILE_JPEG2000_DCINEMA_4K,             "JPEG 2000 digital cinema 4K"          },
1262     { FF_PROFILE_UNKNOWN },
1263 };
1264
1265 static const AVClass class = {
1266     .class_name = "j2k",
1267     .item_name  = av_default_item_name,
1268     .option     = options,
1269     .version    = LIBAVUTIL_VERSION_INT,
1270 };
1271
1272 AVCodec ff_j2k_decoder = {
1273     .name             = "j2k",
1274     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1275     .type             = AVMEDIA_TYPE_VIDEO,
1276     .id               = AV_CODEC_ID_JPEG2000,
1277     .capabilities     = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
1278     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
1279     .init_static_data = jpeg2000_init_static_data,
1280     .decode           = jpeg2000_decode_frame,
1281     .priv_class       = &class,
1282     .max_lowres       = 5,
1283     .profiles         = NULL_IF_CONFIG_SMALL(profiles)
1284 };