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