2 * JPEG 2000 image decoder
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * JPEG 2000 image decoder
30 #include "libavutil/attributes.h"
31 #include "libavutil/common.h"
32 #include "libavutil/opt.h"
34 #include "bytestream.h"
38 #include "jpeg2000dsp.h"
40 #define JP2_SIG_TYPE 0x6A502020
41 #define JP2_SIG_VALUE 0x0D0A870A
42 #define JP2_CODESTREAM 0x6A703263
47 typedef struct Jpeg2000TilePart {
48 uint8_t tile_index; // Tile index who refers the tile-part
49 const uint8_t *tp_end;
50 GetByteContext tpg; // bit stream in tile-part
53 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
54 * one per component, so tile_part elements have a size of 3 */
55 typedef struct Jpeg2000Tile {
56 Jpeg2000Component *comp;
57 uint8_t properties[4];
58 Jpeg2000CodingStyle codsty[4];
59 Jpeg2000QuantStyle qntsty[4];
60 Jpeg2000TilePart tile_part[3];
61 uint16_t tp_idx; // Tile-part index
64 typedef struct Jpeg2000DecoderContext {
66 AVCodecContext *avctx;
70 int image_offset_x, image_offset_y;
71 int tile_offset_x, tile_offset_y;
72 uint8_t cbps[4]; // bits per sample in particular components
73 uint8_t sgnd[4]; // if a component is signed
74 uint8_t properties[4];
78 int tile_width, tile_height;
79 unsigned numXtiles, numYtiles;
82 Jpeg2000CodingStyle codsty[4];
83 Jpeg2000QuantStyle qntsty[4];
89 Jpeg2000DSPContext dsp;
91 /*options parameters*/
93 } Jpeg2000DecoderContext;
95 /* get_bits functions for JPEG2000 packet bitstream
96 * It is a get_bit function with a bit-stuffing routine. If the value of the
97 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
98 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
99 static int get_bits(Jpeg2000DecoderContext *s, int n)
104 if (s->bit_index == 0) {
105 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
108 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
113 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
115 if (bytestream2_get_byte(&s->g) == 0xff)
116 bytestream2_skip(&s->g, 1);
120 /* decode the value stored in node */
121 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
124 Jpeg2000TgtNode *stack[30];
125 int sp = -1, curval = 0;
128 return AVERROR_INVALIDDATA;
130 while (node && !node->vis) {
138 curval = stack[sp]->val;
140 while (curval < threshold && sp >= 0) {
141 if (curval < stack[sp]->val)
142 curval = stack[sp]->val;
143 while (curval < threshold) {
145 if ((ret = get_bits(s, 1)) > 0) {
153 stack[sp]->val = curval;
159 /* marker segments */
160 /* get sizes and offsets of image, tiles; number of components */
161 static int get_siz(Jpeg2000DecoderContext *s)
166 if (bytestream2_get_bytes_left(&s->g) < 36)
167 return AVERROR_INVALIDDATA;
169 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
170 s->width = bytestream2_get_be32u(&s->g); // Width
171 s->height = bytestream2_get_be32u(&s->g); // Height
172 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
173 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
174 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
175 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
176 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
177 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
178 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
180 if (ncomponents <= 0) {
181 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
183 return AVERROR_INVALIDDATA;
186 if (ncomponents > 4) {
187 avpriv_request_sample(s->avctx, "Support for %d components",
189 return AVERROR_PATCHWELCOME;
192 s->ncomponents = ncomponents;
194 if (s->tile_width <= 0 || s->tile_height <= 0 ||
195 s->tile_width > s->width || s->tile_height > s->height) {
196 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
197 s->tile_width, s->tile_height);
198 return AVERROR_INVALIDDATA;
201 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
202 return AVERROR_INVALIDDATA;
204 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
205 uint8_t x = bytestream2_get_byteu(&s->g);
206 s->cbps[i] = (x & 0x7f) + 1;
207 s->precision = FFMAX(s->cbps[i], s->precision);
208 s->sgnd[i] = !!(x & 0x80);
209 s->cdx[i] = bytestream2_get_byteu(&s->g);
210 s->cdy[i] = bytestream2_get_byteu(&s->g);
212 if (s->cdx[i] != 1 || s->cdy[i] != 1) {
213 avpriv_request_sample(s->avctx,
214 "CDxy values %d %d for component %d",
215 s->cdx[i], s->cdy[i], i);
216 if (!s->cdx[i] || !s->cdy[i])
217 return AVERROR_INVALIDDATA;
219 return AVERROR_PATCHWELCOME;
223 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
224 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
226 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
228 s->numXtiles = s->numYtiles = 0;
229 return AVERROR(ENOMEM);
232 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
233 Jpeg2000Tile *tile = s->tile + i;
235 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
237 return AVERROR(ENOMEM);
240 /* compute image size with reduction factor */
241 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
242 s->reduction_factor);
243 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
244 s->reduction_factor);
246 switch (s->ncomponents) {
248 if (s->precision > 8)
249 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
251 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
254 switch (s->avctx->profile) {
255 case FF_PROFILE_JPEG2000_DCINEMA_2K:
256 case FF_PROFILE_JPEG2000_DCINEMA_4K:
257 /* XYZ color-space for digital cinema profiles */
258 s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
261 if (s->precision > 8)
262 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
264 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
269 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
272 /* pixel format can not be identified */
273 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
279 /* get common part for COD and COC segments */
280 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
284 if (bytestream2_get_bytes_left(&s->g) < 5)
285 return AVERROR_INVALIDDATA;
287 /* nreslevels = number of resolution levels
288 = number of decomposition level +1 */
289 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
291 if (c->nreslevels > JPEG2000_MAX_RESLEVELS)
292 return AVERROR_INVALIDDATA;
294 /* compute number of resolution levels to decode */
295 if (c->nreslevels < s->reduction_factor)
296 c->nreslevels2decode = 1;
298 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
300 c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
301 c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
303 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
304 c->log2_cblk_width + c->log2_cblk_height > 12) {
305 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
306 return AVERROR_INVALIDDATA;
309 c->cblk_style = bytestream2_get_byteu(&s->g);
310 if (c->cblk_style != 0) { // cblk style
311 avpriv_request_sample(s->avctx, "Support for extra cblk styles");
312 return AVERROR_PATCHWELCOME;
314 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
315 /* set integer 9/7 DWT in case of BITEXACT flag */
316 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
317 c->transform = FF_DWT97_INT;
319 if (c->csty & JPEG2000_CSTY_PREC) {
321 for (i = 0; i < c->nreslevels; i++) {
322 byte = bytestream2_get_byte(&s->g);
323 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
324 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
327 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
328 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
333 /* get coding parameters for a particular tile or whole image*/
334 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
337 Jpeg2000CodingStyle tmp;
340 if (bytestream2_get_bytes_left(&s->g) < 5)
341 return AVERROR_INVALIDDATA;
343 tmp.csty = bytestream2_get_byteu(&s->g);
345 // get progression order
346 tmp.prog_order = bytestream2_get_byteu(&s->g);
348 tmp.nlayers = bytestream2_get_be16u(&s->g);
349 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
351 if (tmp.mct && s->ncomponents < 3) {
352 av_log(s->avctx, AV_LOG_ERROR,
353 "MCT %"PRIu8" with too few components (%d)\n",
354 tmp.mct, s->ncomponents);
355 return AVERROR_INVALIDDATA;
358 if ((ret = get_cox(s, &tmp)) < 0)
361 for (compno = 0; compno < s->ncomponents; compno++)
362 if (!(properties[compno] & HAD_COC))
363 memcpy(c + compno, &tmp, sizeof(tmp));
367 /* Get coding parameters for a component in the whole image or a
368 * particular tile. */
369 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
374 if (bytestream2_get_bytes_left(&s->g) < 2)
375 return AVERROR_INVALIDDATA;
377 compno = bytestream2_get_byteu(&s->g);
379 if (compno >= s->ncomponents) {
380 av_log(s->avctx, AV_LOG_ERROR,
381 "Invalid compno %d. There are %d components in the image.\n",
382 compno, s->ncomponents);
383 return AVERROR_INVALIDDATA;
387 c->csty = bytestream2_get_byteu(&s->g);
389 if ((ret = get_cox(s, c)) < 0)
392 properties[compno] |= HAD_COC;
396 /* Get common part for QCD and QCC segments. */
397 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
401 if (bytestream2_get_bytes_left(&s->g) < 1)
402 return AVERROR_INVALIDDATA;
404 x = bytestream2_get_byteu(&s->g); // Sqcd
406 q->nguardbits = x >> 5;
407 q->quantsty = x & 0x1f;
409 if (q->quantsty == JPEG2000_QSTY_NONE) {
411 if (bytestream2_get_bytes_left(&s->g) < n ||
412 n > JPEG2000_MAX_DECLEVELS)
413 return AVERROR_INVALIDDATA;
414 for (i = 0; i < n; i++)
415 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
416 } else if (q->quantsty == JPEG2000_QSTY_SI) {
417 if (bytestream2_get_bytes_left(&s->g) < 2)
418 return AVERROR_INVALIDDATA;
419 x = bytestream2_get_be16u(&s->g);
420 q->expn[0] = x >> 11;
421 q->mant[0] = x & 0x7ff;
422 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
423 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
424 q->expn[i] = curexpn;
425 q->mant[i] = q->mant[0];
429 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
430 n > JPEG2000_MAX_DECLEVELS)
431 return AVERROR_INVALIDDATA;
432 for (i = 0; i < n; i++) {
433 x = bytestream2_get_be16u(&s->g);
434 q->expn[i] = x >> 11;
435 q->mant[i] = x & 0x7ff;
441 /* Get quantization parameters for a particular tile or a whole image. */
442 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
445 Jpeg2000QuantStyle tmp;
448 if ((ret = get_qcx(s, n, &tmp)) < 0)
450 for (compno = 0; compno < s->ncomponents; compno++)
451 if (!(properties[compno] & HAD_QCC))
452 memcpy(q + compno, &tmp, sizeof(tmp));
456 /* Get quantization parameters for a component in the whole image
457 * on in a particular tile. */
458 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
463 if (bytestream2_get_bytes_left(&s->g) < 1)
464 return AVERROR_INVALIDDATA;
466 compno = bytestream2_get_byteu(&s->g);
468 if (compno >= s->ncomponents) {
469 av_log(s->avctx, AV_LOG_ERROR,
470 "Invalid compno %d. There are %d components in the image.\n",
471 compno, s->ncomponents);
472 return AVERROR_INVALIDDATA;
475 properties[compno] |= HAD_QCC;
476 return get_qcx(s, n - 1, q + compno);
479 /* Get start of tile segment. */
480 static int get_sot(Jpeg2000DecoderContext *s, int n)
482 Jpeg2000TilePart *tp;
487 if (bytestream2_get_bytes_left(&s->g) < 8)
488 return AVERROR_INVALIDDATA;
490 Isot = bytestream2_get_be16u(&s->g); // Isot
491 if (Isot >= s->numXtiles * s->numYtiles)
492 return AVERROR_INVALIDDATA;
495 avpriv_request_sample(s->avctx, "Support for more than one tile");
496 return AVERROR_PATCHWELCOME;
498 Psot = bytestream2_get_be32u(&s->g); // Psot
499 TPsot = bytestream2_get_byteu(&s->g); // TPsot
501 /* Read TNSot but not used */
502 bytestream2_get_byteu(&s->g); // TNsot
504 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
505 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
506 return AVERROR_INVALIDDATA;
509 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
510 avpriv_request_sample(s->avctx, "Support for %"PRIu8" components", TPsot);
511 return AVERROR_PATCHWELCOME;
514 s->tile[Isot].tp_idx = TPsot;
515 tp = s->tile[Isot].tile_part + TPsot;
516 tp->tile_index = Isot;
517 tp->tp_end = s->g.buffer + Psot - n - 2;
520 Jpeg2000Tile *tile = s->tile + s->curtileno;
523 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
524 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
530 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
531 * Used to know the number of tile parts and lengths.
532 * There may be multiple TLMs in the header.
533 * TODO: The function is not used for tile-parts management, nor anywhere else.
534 * It can be useful to allocate memory for tile parts, before managing the SOT
535 * markers. Parsing the TLM header is needed to increment the input header
537 * This marker is mandatory for DCI. */
538 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
540 uint8_t Stlm, ST, SP, tile_tlm, i;
541 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
542 Stlm = bytestream2_get_byte(&s->g);
544 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
545 ST = (Stlm >> 4) & 0x03;
546 // TODO: Manage case of ST = 0b11 --> raise error
547 SP = (Stlm >> 6) & 0x01;
548 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
549 for (i = 0; i < tile_tlm; i++) {
554 bytestream2_get_byte(&s->g);
557 bytestream2_get_be16(&s->g);
560 bytestream2_get_be32(&s->g);
564 bytestream2_get_be16(&s->g);
566 bytestream2_get_be32(&s->g);
572 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
575 int tilex = tileno % s->numXtiles;
576 int tiley = tileno / s->numXtiles;
577 Jpeg2000Tile *tile = s->tile + tileno;
580 return AVERROR(ENOMEM);
582 for (compno = 0; compno < s->ncomponents; compno++) {
583 Jpeg2000Component *comp = tile->comp + compno;
584 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
585 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
586 int ret; // global bandno
588 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
589 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
590 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
591 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
593 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
594 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
595 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
596 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
598 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
599 s->cbps[compno], s->cdx[compno],
600 s->cdy[compno], s->avctx))
606 /* Read the number of coding passes. */
607 static int getnpasses(Jpeg2000DecoderContext *s)
614 if ((num = get_bits(s, 2)) != 3)
615 return num < 0 ? num : 3 + num;
616 if ((num = get_bits(s, 5)) != 31)
617 return num < 0 ? num : 6 + num;
618 num = get_bits(s, 7);
619 return num < 0 ? num : 37 + num;
622 static int getlblockinc(Jpeg2000DecoderContext *s)
625 while (ret = get_bits(s, 1)) {
633 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
634 Jpeg2000CodingStyle *codsty,
635 Jpeg2000ResLevel *rlevel, int precno,
636 int layno, uint8_t *expn, int numgbits)
638 int bandno, cblkno, ret, nb_code_blocks;
640 if (!(ret = get_bits(s, 1))) {
646 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
647 Jpeg2000Band *band = rlevel->band + bandno;
648 Jpeg2000Prec *prec = band->prec + precno;
650 if (band->coord[0][0] == band->coord[0][1] ||
651 band->coord[1][0] == band->coord[1][1])
653 nb_code_blocks = prec->nb_codeblocks_height *
654 prec->nb_codeblocks_width;
655 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
656 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
657 int incl, newpasses, llen;
660 incl = get_bits(s, 1);
662 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
668 if (!cblk->npasses) {
669 int v = expn[bandno] + numgbits - 1 -
670 tag_tree_decode(s, prec->zerobits + cblkno, 100);
672 av_log(s->avctx, AV_LOG_ERROR,
673 "nonzerobits %d invalid\n", v);
674 return AVERROR_INVALIDDATA;
676 cblk->nonzerobits = v;
678 if ((newpasses = getnpasses(s)) < 0)
680 if ((llen = getlblockinc(s)) < 0)
682 cblk->lblock += llen;
683 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
685 if (ret > sizeof(cblk->data)) {
686 avpriv_request_sample(s->avctx,
687 "Block with lengthinc greater than %zu",
689 return AVERROR_PATCHWELCOME;
691 cblk->lengthinc = ret;
692 cblk->npasses += newpasses;
697 if (codsty->csty & JPEG2000_CSTY_EPH) {
698 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
699 bytestream2_skip(&s->g, 2);
701 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
704 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
705 Jpeg2000Band *band = rlevel->band + bandno;
706 Jpeg2000Prec *prec = band->prec + precno;
708 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
709 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
710 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
711 if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
712 return AVERROR_INVALIDDATA;
713 /* Code-block data can be empty. In that case initialize data
715 if (cblk->lengthinc > 0) {
716 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
718 cblk->data[0] = 0xFF;
719 cblk->data[1] = 0xFF;
721 cblk->length += cblk->lengthinc;
724 if (cblk->length > sizeof(cblk->data)) {
725 av_log(s->avctx, AV_LOG_ERROR,
726 "Block length %"PRIu16" > data size %zd\n",
727 cblk->length, sizeof(cblk->data));
728 return AVERROR_INVALIDDATA;
735 static int decode_pgod_lrcp(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
737 int layno, reslevelno, compno, precno, ok_reslevel;
740 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
742 for (reslevelno = 0; ok_reslevel; reslevelno++) {
744 for (compno = 0; compno < s->ncomponents; compno++) {
745 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
746 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
747 if (reslevelno < codsty->nreslevels) {
748 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
751 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
752 if ((ret = jpeg2000_decode_packet(s,
755 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
756 qntsty->nguardbits)) < 0)
766 static int decode_pgod_cprl(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
768 int layno, reslevelno, compno, precno;
771 for (compno = 0; compno < s->ncomponents; compno++) {
772 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
773 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
775 /* Set bit stream buffer address according to tile-part.
776 * For DCinema one tile-part per component, so can be
777 * indexed by component. */
778 s->g = tile->tile_part[compno].tpg;
780 /* Position loop (y axis)
781 * TODO: Automate computing of step 256.
782 * Fixed here, but to be computed before entering here. */
783 for (y = 0; y < s->height; y += 256) {
784 /* Position loop (y axis)
785 * TODO: automate computing of step 256.
786 * Fixed here, but to be computed before entering here. */
787 for (x = 0; x < s->width; x += 256) {
788 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
790 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
791 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
793 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
794 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
797 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
798 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
801 // check if a precinct exists
802 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
803 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
804 precno = prcx + rlevel->num_precincts_x * prcy;
805 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
806 if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
808 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
809 qntsty->nguardbits)) < 0)
820 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
825 switch (tile->codsty[0].prog_order) {
826 case JPEG2000_PGOD_LRCP:
827 ret = decode_pgod_lrcp(s, tile);
830 case JPEG2000_PGOD_CPRL:
831 ret = decode_pgod_cprl(s, tile);
834 case JPEG2000_PGOD_RLCP:
835 avpriv_request_sample(s->avctx, "Progression order RLCP");
836 ret = AVERROR_PATCHWELCOME;
839 case JPEG2000_PGOD_RPCL:
840 avpriv_request_sample(s->avctx, "Progression order RPCL");
841 ret = AVERROR_PATCHWELCOME;
844 case JPEG2000_PGOD_PCRL:
845 avpriv_request_sample(s->avctx, "Progression order PCRL");
846 ret = AVERROR_PATCHWELCOME;
853 /* EOC marker reached */
854 bytestream2_skip(&s->g, 2);
859 /* TIER-1 routines */
860 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
861 int bpno, int bandno, int bpass_csty_symbol,
862 int vert_causal_ctx_csty_symbol)
864 int mask = 3 << (bpno - 1), y0, x, y;
866 for (y0 = 0; y0 < height; y0 += 4)
867 for (x = 0; x < width; x++)
868 for (y = y0; y < height && y < y0 + 4; y++) {
869 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
870 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
872 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
873 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
874 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
875 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
876 if (bpass_csty_symbol)
877 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
879 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
882 ff_jpeg2000_set_significance(t1, x, y,
885 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
890 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
896 phalf = 1 << (bpno - 1);
899 for (y0 = 0; y0 < height; y0 += 4)
900 for (x = 0; x < width; x++)
901 for (y = y0; y < height && y < y0 + 4; y++)
902 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
903 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
904 int r = ff_mqc_decode(&t1->mqc,
905 t1->mqc.cx_states + ctxno)
907 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
908 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
912 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
913 int width, int height, int bpno, int bandno,
914 int seg_symbols, int vert_causal_ctx_csty_symbol)
916 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
918 for (y0 = 0; y0 < height; y0 += 4) {
919 for (x = 0; x < width; x++) {
920 if (y0 + 3 < height &&
921 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
922 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
923 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
924 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
925 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
927 runlen = ff_mqc_decode(&t1->mqc,
928 t1->mqc.cx_states + MQC_CX_UNI);
929 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
938 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
940 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
942 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
943 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
944 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
950 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
952 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
953 t1->mqc.cx_states + ctxno) ^
956 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
959 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
965 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
966 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
967 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
968 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
970 av_log(s->avctx, AV_LOG_ERROR,
971 "Segmentation symbol value incorrect\n");
975 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
976 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
977 int width, int height, int bandpos)
979 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
981 int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
982 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
984 for (y = 0; y < height; y++)
985 memset(t1->data[y], 0, width * sizeof(**t1->data));
987 /* If code-block contains no compressed data: nothing to do. */
990 for (y = 0; y < height + 2; y++)
991 memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
993 ff_mqc_initdec(&t1->mqc, cblk->data);
994 cblk->data[cblk->length] = 0xff;
995 cblk->data[cblk->length + 1] = 0xff;
1000 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1001 bpass_csty_symbol && (clnpass_cnt >= 4),
1002 vert_causal_ctx_csty_symbol);
1005 decode_refpass(t1, width, height, bpno + 1);
1006 if (bpass_csty_symbol && clnpass_cnt >= 4)
1007 ff_mqc_initdec(&t1->mqc, cblk->data);
1010 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1011 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1012 vert_causal_ctx_csty_symbol);
1013 clnpass_cnt = clnpass_cnt + 1;
1014 if (bpass_csty_symbol && clnpass_cnt >= 4)
1015 ff_mqc_initdec(&t1->mqc, cblk->data);
1028 /* TODO: Verify dequantization for lossless case
1029 * comp->data can be float or int
1030 * band->stepsize can be float or int
1031 * depending on the type of DWT transformation.
1032 * see ISO/IEC 15444-1:2002 A.6.1 */
1034 /* Float dequantization of a codeblock.*/
1035 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1036 Jpeg2000Component *comp,
1037 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1040 int w = cblk->coord[0][1] - cblk->coord[0][0];
1041 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1042 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1043 int *src = t1->data[j];
1044 for (i = 0; i < w; ++i)
1045 datap[i] = src[i] * band->f_stepsize;
1049 /* Integer dequantization of a codeblock.*/
1050 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1051 Jpeg2000Component *comp,
1052 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1055 int w = cblk->coord[0][1] - cblk->coord[0][0];
1056 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1057 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1058 int *src = t1->data[j];
1059 for (i = 0; i < w; ++i)
1060 datap[i] = (src[i] * band->i_stepsize + (1 << 15)) >> 16;
1064 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1069 for (i = 0; i < 3; i++)
1070 if (tile->codsty[0].transform == FF_DWT97)
1071 src[i] = tile->comp[i].f_data;
1073 src[i] = tile->comp[i].i_data;
1075 for (i = 0; i < 2; i++)
1076 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1078 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1081 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1083 Jpeg2000T1Context t1;
1085 int compno, reslevelno, bandno;
1087 /* Loop on tile components */
1089 for (compno = 0; compno < s->ncomponents; compno++) {
1090 Jpeg2000Component *comp = tile->comp + compno;
1091 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1092 /* Loop on resolution levels */
1093 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1094 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1096 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1097 uint16_t nb_precincts, precno;
1098 Jpeg2000Band *band = rlevel->band + bandno;
1099 int cblkno = 0, bandpos;
1100 bandpos = bandno + (reslevelno > 0);
1102 if (band->coord[0][0] == band->coord[0][1] ||
1103 band->coord[1][0] == band->coord[1][1])
1106 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1107 /* Loop on precincts */
1108 for (precno = 0; precno < nb_precincts; precno++) {
1109 Jpeg2000Prec *prec = band->prec + precno;
1111 /* Loop on codeblocks */
1113 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1116 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1117 decode_cblk(s, codsty, &t1, cblk,
1118 cblk->coord[0][1] - cblk->coord[0][0],
1119 cblk->coord[1][1] - cblk->coord[1][0],
1122 x = cblk->coord[0][0];
1123 y = cblk->coord[1][0];
1125 if (codsty->transform == FF_DWT97)
1126 dequantization_float(x, y, cblk, comp, &t1, band);
1128 dequantization_int(x, y, cblk, comp, &t1, band);
1132 } /* end reslevel */
1135 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1139 #define WRITE_FRAME(D, PIXEL) \
1140 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1141 AVFrame * picture) \
1143 int linesize = picture->linesize[0] / sizeof(PIXEL); \
1147 for (compno = 0; compno < s->ncomponents; compno++) { \
1148 Jpeg2000Component *comp = tile->comp + compno; \
1149 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1151 float *datap = comp->f_data; \
1152 int32_t *i_datap = comp->i_data; \
1153 int cbps = s->cbps[compno]; \
1154 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
1156 y = tile->comp[compno].coord[1][0] - s->image_offset_y; \
1157 line = (PIXEL *)picture->data[0] + y * linesize; \
1158 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) { \
1161 x = tile->comp[compno].coord[0][0] - s->image_offset_x; \
1162 dst = line + x * s->ncomponents + compno; \
1164 if (codsty->transform == FF_DWT97) { \
1165 for (; x < w; x += s->cdx[compno]) { \
1166 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1167 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1168 val = av_clip(val, 0, (1 << cbps) - 1); \
1169 *dst = val << (8 * sizeof(PIXEL) - cbps); \
1171 dst += s->ncomponents; \
1174 for (; x < w; x += s->cdx[compno]) { \
1175 int val = *i_datap + (1 << (cbps - 1)); \
1176 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1177 val = av_clip(val, 0, (1 << cbps) - 1); \
1178 *dst = val << (8 * sizeof(PIXEL) - cbps); \
1180 dst += s->ncomponents; \
1189 WRITE_FRAME(8, uint8_t)
1190 WRITE_FRAME(16, uint16_t)
1194 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1197 tile_codeblocks(s, tile);
1199 /* inverse MCT transformation */
1200 if (tile->codsty[0].mct)
1201 mct_decode(s, tile);
1203 if (s->precision <= 8) {
1204 write_frame_8(s, tile, picture);
1206 write_frame_16(s, tile, picture);
1212 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1215 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1216 for (compno = 0; compno < s->ncomponents; compno++) {
1217 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1218 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1220 ff_jpeg2000_cleanup(comp, codsty);
1222 av_freep(&s->tile[tileno].comp);
1225 s->numXtiles = s->numYtiles = 0;
1228 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1230 Jpeg2000CodingStyle *codsty = s->codsty;
1231 Jpeg2000QuantStyle *qntsty = s->qntsty;
1232 uint8_t *properties = s->properties;
1239 if (bytestream2_get_bytes_left(&s->g) < 2) {
1240 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1244 marker = bytestream2_get_be16u(&s->g);
1245 oldpos = bytestream2_tell(&s->g);
1247 if (marker == JPEG2000_SOD) {
1249 Jpeg2000TilePart *tp;
1251 if (s->curtileno < 0) {
1252 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1253 return AVERROR_INVALIDDATA;
1256 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1257 return AVERROR_INVALIDDATA;
1260 tile = s->tile + s->curtileno;
1261 tp = tile->tile_part + tile->tp_idx;
1262 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1263 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1267 if (marker == JPEG2000_EOC)
1270 len = bytestream2_get_be16u(&s->g);
1271 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1272 return AVERROR_INVALIDDATA;
1279 ret = get_coc(s, codsty, properties);
1282 ret = get_cod(s, codsty, properties);
1285 ret = get_qcc(s, len, qntsty, properties);
1288 ret = get_qcd(s, len, qntsty, properties);
1291 if (!(ret = get_sot(s, len))) {
1292 codsty = s->tile[s->curtileno].codsty;
1293 qntsty = s->tile[s->curtileno].qntsty;
1294 properties = s->tile[s->curtileno].properties;
1298 // the PLT marker is ignored
1300 // the PLM marker is ignored
1302 // the comment is ignored
1303 bytestream2_skip(&s->g, len - 2);
1306 // Tile-part lengths
1307 ret = get_tlm(s, len);
1310 av_log(s->avctx, AV_LOG_ERROR,
1311 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1312 marker, bytestream2_tell(&s->g) - 4);
1313 bytestream2_skip(&s->g, len - 2);
1316 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1317 av_log(s->avctx, AV_LOG_ERROR,
1318 "error during processing marker segment %.4"PRIx16"\n",
1320 return ret ? ret : -1;
1326 /* Read bit stream packets --> T2 operation. */
1327 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1332 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1333 Jpeg2000Tile *tile = s->tile + tileno;
1335 if (ret = init_tile(s, tileno))
1338 s->g = tile->tile_part[0].tpg;
1339 if (ret = jpeg2000_decode_packets(s, tile))
1346 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1348 uint32_t atom_size, atom;
1349 int found_codestream = 0, search_range = 10;
1351 while(!found_codestream && search_range
1353 bytestream2_get_bytes_left(&s->g) >= 8) {
1354 atom_size = bytestream2_get_be32u(&s->g);
1355 atom = bytestream2_get_be32u(&s->g);
1356 if (atom == JP2_CODESTREAM) {
1357 found_codestream = 1;
1359 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1361 bytestream2_skipu(&s->g, atom_size - 8);
1366 if (found_codestream)
1371 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
1373 Jpeg2000DecoderContext *s = avctx->priv_data;
1375 ff_jpeg2000dsp_init(&s->dsp);
1380 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1381 int *got_frame, AVPacket *avpkt)
1383 Jpeg2000DecoderContext *s = avctx->priv_data;
1384 ThreadFrame frame = { .f = data };
1385 AVFrame *picture = data;
1389 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1390 s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1392 if (bytestream2_get_bytes_left(&s->g) < 2) {
1393 ret = AVERROR_INVALIDDATA;
1397 // check if the image is in jp2 format
1398 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1399 (bytestream2_get_be32u(&s->g) == 12) &&
1400 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1401 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1402 if (!jp2_find_codestream(s)) {
1403 av_log(avctx, AV_LOG_ERROR,
1404 "Could not find Jpeg2000 codestream atom.\n");
1405 ret = AVERROR_INVALIDDATA;
1409 bytestream2_seek(&s->g, 0, SEEK_SET);
1412 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1413 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1414 ret = AVERROR_INVALIDDATA;
1417 if (ret = jpeg2000_read_main_headers(s))
1420 /* get picture buffer */
1421 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
1422 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
1425 picture->pict_type = AV_PICTURE_TYPE_I;
1426 picture->key_frame = 1;
1428 if (ret = jpeg2000_read_bitstream_packets(s))
1430 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1431 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1434 jpeg2000_dec_cleanup(s);
1438 return bytestream2_tell(&s->g);
1441 jpeg2000_dec_cleanup(s);
1445 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
1447 ff_jpeg2000_init_tier1_luts();
1448 ff_mqc_init_context_tables();
1451 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1452 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1454 static const AVOption options[] = {
1455 { "lowres", "Lower the decoding resolution by a power of two",
1456 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1460 static const AVProfile profiles[] = {
1461 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1462 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1463 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1464 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1465 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1466 { FF_PROFILE_UNKNOWN },
1469 static const AVClass class = {
1470 .class_name = "jpeg2000",
1471 .item_name = av_default_item_name,
1473 .version = LIBAVUTIL_VERSION_INT,
1476 AVCodec ff_jpeg2000_decoder = {
1478 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1479 .type = AVMEDIA_TYPE_VIDEO,
1480 .id = AV_CODEC_ID_JPEG2000,
1481 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
1482 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1483 .init_static_data = jpeg2000_init_static_data,
1484 .init = jpeg2000_decode_init,
1485 .decode = jpeg2000_decode_frame,
1486 .priv_class = &class,
1487 .profiles = NULL_IF_CONFIG_SMALL(profiles)