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"
41 #define JP2_SIG_TYPE 0x6A502020
42 #define JP2_SIG_VALUE 0x0D0A870A
43 #define JP2_CODESTREAM 0x6A703263
48 typedef struct Jpeg2000TilePart {
49 uint8_t tile_index; // Tile index who refers the tile-part
50 const uint8_t *tp_end;
51 GetByteContext tpg; // bit stream in tile-part
54 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
55 * one per component, so tile_part elements have a size of 3 */
56 typedef struct Jpeg2000Tile {
57 Jpeg2000Component *comp;
58 uint8_t properties[4];
59 Jpeg2000CodingStyle codsty[4];
60 Jpeg2000QuantStyle qntsty[4];
61 Jpeg2000TilePart tile_part[3];
62 uint16_t tp_idx; // Tile-part index
65 typedef struct Jpeg2000DecoderContext {
67 AVCodecContext *avctx;
71 int image_offset_x, image_offset_y;
72 int tile_offset_x, tile_offset_y;
73 uint8_t cbps[4]; // bits per sample in particular components
74 uint8_t sgnd[4]; // if a component is signed
75 uint8_t properties[4];
79 int tile_width, tile_height;
80 unsigned numXtiles, numYtiles;
83 Jpeg2000CodingStyle codsty[4];
84 Jpeg2000QuantStyle qntsty[4];
90 Jpeg2000DSPContext dsp;
92 /*options parameters*/
94 } Jpeg2000DecoderContext;
96 /* get_bits functions for JPEG2000 packet bitstream
97 * It is a get_bit function with a bit-stuffing routine. If the value of the
98 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
99 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
100 static int get_bits(Jpeg2000DecoderContext *s, int n)
105 if (s->bit_index == 0) {
106 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
109 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
114 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
116 if (bytestream2_get_byte(&s->g) == 0xff)
117 bytestream2_skip(&s->g, 1);
121 /* decode the value stored in node */
122 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
125 Jpeg2000TgtNode *stack[30];
126 int sp = -1, curval = 0;
129 return AVERROR_INVALIDDATA;
131 while (node && !node->vis) {
139 curval = stack[sp]->val;
141 while (curval < threshold && sp >= 0) {
142 if (curval < stack[sp]->val)
143 curval = stack[sp]->val;
144 while (curval < threshold) {
146 if ((ret = get_bits(s, 1)) > 0) {
154 stack[sp]->val = curval;
160 /* marker segments */
161 /* get sizes and offsets of image, tiles; number of components */
162 static int get_siz(Jpeg2000DecoderContext *s)
167 if (bytestream2_get_bytes_left(&s->g) < 36)
168 return AVERROR_INVALIDDATA;
170 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
171 s->width = bytestream2_get_be32u(&s->g); // Width
172 s->height = bytestream2_get_be32u(&s->g); // Height
173 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
174 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
175 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
176 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
177 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
178 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
179 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
181 if (ncomponents <= 0) {
182 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
184 return AVERROR_INVALIDDATA;
187 if (ncomponents > 4) {
188 avpriv_request_sample(s->avctx, "Support for %d components",
190 return AVERROR_PATCHWELCOME;
193 s->ncomponents = ncomponents;
195 if (s->tile_width <= 0 || s->tile_height <= 0 ||
196 s->tile_width > s->width || s->tile_height > s->height) {
197 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
198 s->tile_width, s->tile_height);
199 return AVERROR_INVALIDDATA;
202 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
203 return AVERROR_INVALIDDATA;
205 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
206 uint8_t x = bytestream2_get_byteu(&s->g);
207 s->cbps[i] = (x & 0x7f) + 1;
208 s->precision = FFMAX(s->cbps[i], s->precision);
209 s->sgnd[i] = !!(x & 0x80);
210 s->cdx[i] = bytestream2_get_byteu(&s->g);
211 s->cdy[i] = bytestream2_get_byteu(&s->g);
213 if (s->cdx[i] != 1 || s->cdy[i] != 1) {
214 avpriv_request_sample(s->avctx,
215 "CDxy values %d %d for component %d",
216 s->cdx[i], s->cdy[i], i);
217 if (!s->cdx[i] || !s->cdy[i])
218 return AVERROR_INVALIDDATA;
220 return AVERROR_PATCHWELCOME;
224 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
225 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
227 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
229 s->numXtiles = s->numYtiles = 0;
230 return AVERROR(ENOMEM);
233 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
234 Jpeg2000Tile *tile = s->tile + i;
236 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
238 return AVERROR(ENOMEM);
241 /* compute image size with reduction factor */
242 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
243 s->reduction_factor);
244 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
245 s->reduction_factor);
247 switch (s->ncomponents) {
249 if (s->precision > 8)
250 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
252 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
255 switch (s->avctx->profile) {
256 case FF_PROFILE_JPEG2000_DCINEMA_2K:
257 case FF_PROFILE_JPEG2000_DCINEMA_4K:
258 /* XYZ color-space for digital cinema profiles */
259 s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
262 if (s->precision > 8)
263 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
265 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
270 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
273 /* pixel format can not be identified */
274 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
280 /* get common part for COD and COC segments */
281 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
285 if (bytestream2_get_bytes_left(&s->g) < 5)
286 return AVERROR_INVALIDDATA;
288 /* nreslevels = number of resolution levels
289 = number of decomposition level +1 */
290 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
292 if (c->nreslevels > JPEG2000_MAX_RESLEVELS)
293 return AVERROR_INVALIDDATA;
295 /* compute number of resolution levels to decode */
296 if (c->nreslevels < s->reduction_factor)
297 c->nreslevels2decode = 1;
299 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
301 c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
302 c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
304 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
305 c->log2_cblk_width + c->log2_cblk_height > 12) {
306 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
307 return AVERROR_INVALIDDATA;
310 c->cblk_style = bytestream2_get_byteu(&s->g);
311 if (c->cblk_style != 0) { // cblk style
312 avpriv_request_sample(s->avctx, "Support for extra cblk styles");
313 return AVERROR_PATCHWELCOME;
315 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
316 /* set integer 9/7 DWT in case of BITEXACT flag */
317 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
318 c->transform = FF_DWT97_INT;
320 if (c->csty & JPEG2000_CSTY_PREC) {
322 for (i = 0; i < c->nreslevels; i++) {
323 byte = bytestream2_get_byte(&s->g);
324 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
325 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
328 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
329 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
334 /* get coding parameters for a particular tile or whole image*/
335 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
338 Jpeg2000CodingStyle tmp;
341 if (bytestream2_get_bytes_left(&s->g) < 5)
342 return AVERROR_INVALIDDATA;
344 tmp.csty = bytestream2_get_byteu(&s->g);
346 // get progression order
347 tmp.prog_order = bytestream2_get_byteu(&s->g);
349 tmp.nlayers = bytestream2_get_be16u(&s->g);
350 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
352 if (tmp.mct && s->ncomponents < 3) {
353 av_log(s->avctx, AV_LOG_ERROR,
354 "MCT %"PRIu8" with too few components (%d)\n",
355 tmp.mct, s->ncomponents);
356 return AVERROR_INVALIDDATA;
359 if ((ret = get_cox(s, &tmp)) < 0)
362 for (compno = 0; compno < s->ncomponents; compno++)
363 if (!(properties[compno] & HAD_COC))
364 memcpy(c + compno, &tmp, sizeof(tmp));
368 /* Get coding parameters for a component in the whole image or a
369 * particular tile. */
370 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
375 if (bytestream2_get_bytes_left(&s->g) < 2)
376 return AVERROR_INVALIDDATA;
378 compno = bytestream2_get_byteu(&s->g);
380 if (compno >= s->ncomponents) {
381 av_log(s->avctx, AV_LOG_ERROR,
382 "Invalid compno %d. There are %d components in the image.\n",
383 compno, s->ncomponents);
384 return AVERROR_INVALIDDATA;
388 c->csty = bytestream2_get_byteu(&s->g);
390 if ((ret = get_cox(s, c)) < 0)
393 properties[compno] |= HAD_COC;
397 /* Get common part for QCD and QCC segments. */
398 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
402 if (bytestream2_get_bytes_left(&s->g) < 1)
403 return AVERROR_INVALIDDATA;
405 x = bytestream2_get_byteu(&s->g); // Sqcd
407 q->nguardbits = x >> 5;
408 q->quantsty = x & 0x1f;
410 if (q->quantsty == JPEG2000_QSTY_NONE) {
412 if (bytestream2_get_bytes_left(&s->g) < n ||
413 n > JPEG2000_MAX_DECLEVELS)
414 return AVERROR_INVALIDDATA;
415 for (i = 0; i < n; i++)
416 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
417 } else if (q->quantsty == JPEG2000_QSTY_SI) {
418 if (bytestream2_get_bytes_left(&s->g) < 2)
419 return AVERROR_INVALIDDATA;
420 x = bytestream2_get_be16u(&s->g);
421 q->expn[0] = x >> 11;
422 q->mant[0] = x & 0x7ff;
423 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
424 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
425 q->expn[i] = curexpn;
426 q->mant[i] = q->mant[0];
430 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
431 n > JPEG2000_MAX_DECLEVELS)
432 return AVERROR_INVALIDDATA;
433 for (i = 0; i < n; i++) {
434 x = bytestream2_get_be16u(&s->g);
435 q->expn[i] = x >> 11;
436 q->mant[i] = x & 0x7ff;
442 /* Get quantization parameters for a particular tile or a whole image. */
443 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
446 Jpeg2000QuantStyle tmp;
449 if ((ret = get_qcx(s, n, &tmp)) < 0)
451 for (compno = 0; compno < s->ncomponents; compno++)
452 if (!(properties[compno] & HAD_QCC))
453 memcpy(q + compno, &tmp, sizeof(tmp));
457 /* Get quantization parameters for a component in the whole image
458 * on in a particular tile. */
459 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
464 if (bytestream2_get_bytes_left(&s->g) < 1)
465 return AVERROR_INVALIDDATA;
467 compno = bytestream2_get_byteu(&s->g);
469 if (compno >= s->ncomponents) {
470 av_log(s->avctx, AV_LOG_ERROR,
471 "Invalid compno %d. There are %d components in the image.\n",
472 compno, s->ncomponents);
473 return AVERROR_INVALIDDATA;
476 properties[compno] |= HAD_QCC;
477 return get_qcx(s, n - 1, q + compno);
480 /* Get start of tile segment. */
481 static int get_sot(Jpeg2000DecoderContext *s, int n)
483 Jpeg2000TilePart *tp;
488 if (bytestream2_get_bytes_left(&s->g) < 8)
489 return AVERROR_INVALIDDATA;
491 Isot = bytestream2_get_be16u(&s->g); // Isot
492 if (Isot >= s->numXtiles * s->numYtiles)
493 return AVERROR_INVALIDDATA;
496 avpriv_request_sample(s->avctx, "Support for more than one tile");
497 return AVERROR_PATCHWELCOME;
499 Psot = bytestream2_get_be32u(&s->g); // Psot
500 TPsot = bytestream2_get_byteu(&s->g); // TPsot
502 /* Read TNSot but not used */
503 bytestream2_get_byteu(&s->g); // TNsot
505 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
506 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
507 return AVERROR_INVALIDDATA;
510 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
511 avpriv_request_sample(s->avctx, "Support for %"PRIu8" components", TPsot);
512 return AVERROR_PATCHWELCOME;
515 s->tile[Isot].tp_idx = TPsot;
516 tp = s->tile[Isot].tile_part + TPsot;
517 tp->tile_index = Isot;
518 tp->tp_end = s->g.buffer + Psot - n - 2;
521 Jpeg2000Tile *tile = s->tile + s->curtileno;
524 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
525 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
531 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
532 * Used to know the number of tile parts and lengths.
533 * There may be multiple TLMs in the header.
534 * TODO: The function is not used for tile-parts management, nor anywhere else.
535 * It can be useful to allocate memory for tile parts, before managing the SOT
536 * markers. Parsing the TLM header is needed to increment the input header
538 * This marker is mandatory for DCI. */
539 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
541 uint8_t Stlm, ST, SP, tile_tlm, i;
542 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
543 Stlm = bytestream2_get_byte(&s->g);
545 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
546 ST = (Stlm >> 4) & 0x03;
547 // TODO: Manage case of ST = 0b11 --> raise error
548 SP = (Stlm >> 6) & 0x01;
549 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
550 for (i = 0; i < tile_tlm; i++) {
555 bytestream2_get_byte(&s->g);
558 bytestream2_get_be16(&s->g);
561 bytestream2_get_be32(&s->g);
565 bytestream2_get_be16(&s->g);
567 bytestream2_get_be32(&s->g);
573 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
576 int tilex = tileno % s->numXtiles;
577 int tiley = tileno / s->numXtiles;
578 Jpeg2000Tile *tile = s->tile + tileno;
581 return AVERROR(ENOMEM);
583 for (compno = 0; compno < s->ncomponents; compno++) {
584 Jpeg2000Component *comp = tile->comp + compno;
585 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
586 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
587 int ret; // global bandno
589 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
590 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
591 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
592 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
594 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
595 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
596 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
597 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
599 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
600 s->cbps[compno], s->cdx[compno],
601 s->cdy[compno], s->avctx))
607 /* Read the number of coding passes. */
608 static int getnpasses(Jpeg2000DecoderContext *s)
615 if ((num = get_bits(s, 2)) != 3)
616 return num < 0 ? num : 3 + num;
617 if ((num = get_bits(s, 5)) != 31)
618 return num < 0 ? num : 6 + num;
619 num = get_bits(s, 7);
620 return num < 0 ? num : 37 + num;
623 static int getlblockinc(Jpeg2000DecoderContext *s)
626 while (ret = get_bits(s, 1)) {
634 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
635 Jpeg2000CodingStyle *codsty,
636 Jpeg2000ResLevel *rlevel, int precno,
637 int layno, uint8_t *expn, int numgbits)
639 int bandno, cblkno, ret, nb_code_blocks;
641 if (!(ret = get_bits(s, 1))) {
647 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
648 Jpeg2000Band *band = rlevel->band + bandno;
649 Jpeg2000Prec *prec = band->prec + precno;
651 if (band->coord[0][0] == band->coord[0][1] ||
652 band->coord[1][0] == band->coord[1][1])
654 nb_code_blocks = prec->nb_codeblocks_height *
655 prec->nb_codeblocks_width;
656 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
657 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
658 int incl, newpasses, llen;
661 incl = get_bits(s, 1);
663 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
669 if (!cblk->npasses) {
670 int v = expn[bandno] + numgbits - 1 -
671 tag_tree_decode(s, prec->zerobits + cblkno, 100);
673 av_log(s->avctx, AV_LOG_ERROR,
674 "nonzerobits %d invalid\n", v);
675 return AVERROR_INVALIDDATA;
677 cblk->nonzerobits = v;
679 if ((newpasses = getnpasses(s)) < 0)
681 if ((llen = getlblockinc(s)) < 0)
683 cblk->lblock += llen;
684 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
686 if (ret > sizeof(cblk->data)) {
687 avpriv_request_sample(s->avctx,
688 "Block with lengthinc greater than %zu",
690 return AVERROR_PATCHWELCOME;
692 cblk->lengthinc = ret;
693 cblk->npasses += newpasses;
698 if (codsty->csty & JPEG2000_CSTY_EPH) {
699 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
700 bytestream2_skip(&s->g, 2);
702 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
705 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
706 Jpeg2000Band *band = rlevel->band + bandno;
707 Jpeg2000Prec *prec = band->prec + precno;
709 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
710 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
711 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
712 if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
713 return AVERROR_INVALIDDATA;
714 /* Code-block data can be empty. In that case initialize data
716 if (cblk->lengthinc > 0) {
717 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
719 cblk->data[0] = 0xFF;
720 cblk->data[1] = 0xFF;
722 cblk->length += cblk->lengthinc;
725 if (cblk->length > sizeof(cblk->data)) {
726 av_log(s->avctx, AV_LOG_ERROR,
727 "Block length %"PRIu16" > data size %zd\n",
728 cblk->length, sizeof(cblk->data));
729 return AVERROR_INVALIDDATA;
736 static int decode_pgod_lrcp(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
738 int layno, reslevelno, compno, precno, ok_reslevel;
741 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
743 for (reslevelno = 0; ok_reslevel; reslevelno++) {
745 for (compno = 0; compno < s->ncomponents; compno++) {
746 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
747 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
748 if (reslevelno < codsty->nreslevels) {
749 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
752 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
753 if ((ret = jpeg2000_decode_packet(s,
756 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
757 qntsty->nguardbits)) < 0)
767 static int decode_pgod_cprl(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
769 int layno, reslevelno, compno, precno;
772 for (compno = 0; compno < s->ncomponents; compno++) {
773 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
774 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
776 /* Set bit stream buffer address according to tile-part.
777 * For DCinema one tile-part per component, so can be
778 * indexed by component. */
779 s->g = tile->tile_part[compno].tpg;
781 /* Position loop (y axis)
782 * TODO: Automate computing of step 256.
783 * Fixed here, but to be computed before entering here. */
784 for (y = 0; y < s->height; y += 256) {
785 /* Position loop (y axis)
786 * TODO: automate computing of step 256.
787 * Fixed here, but to be computed before entering here. */
788 for (x = 0; x < s->width; x += 256) {
789 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
791 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
792 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
794 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
795 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
798 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
799 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
802 // check if a precinct exists
803 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
804 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
805 precno = prcx + rlevel->num_precincts_x * prcy;
806 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
807 if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
809 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
810 qntsty->nguardbits)) < 0)
821 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
826 switch (tile->codsty[0].prog_order) {
827 case JPEG2000_PGOD_LRCP:
828 ret = decode_pgod_lrcp(s, tile);
831 case JPEG2000_PGOD_CPRL:
832 ret = decode_pgod_cprl(s, tile);
835 case JPEG2000_PGOD_RLCP:
836 avpriv_request_sample(s->avctx, "Progression order RLCP");
837 ret = AVERROR_PATCHWELCOME;
840 case JPEG2000_PGOD_RPCL:
841 avpriv_request_sample(s->avctx, "Progression order RPCL");
842 ret = AVERROR_PATCHWELCOME;
845 case JPEG2000_PGOD_PCRL:
846 avpriv_request_sample(s->avctx, "Progression order PCRL");
847 ret = AVERROR_PATCHWELCOME;
854 /* EOC marker reached */
855 bytestream2_skip(&s->g, 2);
860 /* TIER-1 routines */
861 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
862 int bpno, int bandno, int bpass_csty_symbol,
863 int vert_causal_ctx_csty_symbol)
865 int mask = 3 << (bpno - 1), y0, x, y;
867 for (y0 = 0; y0 < height; y0 += 4)
868 for (x = 0; x < width; x++)
869 for (y = y0; y < height && y < y0 + 4; y++) {
870 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
871 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
873 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
874 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
875 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
876 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
877 if (bpass_csty_symbol)
878 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
880 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
883 ff_jpeg2000_set_significance(t1, x, y,
886 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
891 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
897 phalf = 1 << (bpno - 1);
900 for (y0 = 0; y0 < height; y0 += 4)
901 for (x = 0; x < width; x++)
902 for (y = y0; y < height && y < y0 + 4; y++)
903 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
904 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
905 int r = ff_mqc_decode(&t1->mqc,
906 t1->mqc.cx_states + ctxno)
908 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
909 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
913 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
914 int width, int height, int bpno, int bandno,
915 int seg_symbols, int vert_causal_ctx_csty_symbol)
917 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
919 for (y0 = 0; y0 < height; y0 += 4) {
920 for (x = 0; x < width; x++) {
921 if (y0 + 3 < height &&
922 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
923 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
924 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
925 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
926 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
928 runlen = ff_mqc_decode(&t1->mqc,
929 t1->mqc.cx_states + MQC_CX_UNI);
930 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
939 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
941 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
943 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
944 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
945 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
951 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
953 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
954 t1->mqc.cx_states + ctxno) ^
957 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
960 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
966 val = 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);
969 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
971 av_log(s->avctx, AV_LOG_ERROR,
972 "Segmentation symbol value incorrect\n");
976 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
977 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
978 int width, int height, int bandpos)
980 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
982 int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
983 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
985 for (y = 0; y < height; y++)
986 memset(t1->data[y], 0, width * sizeof(**t1->data));
988 /* If code-block contains no compressed data: nothing to do. */
991 for (y = 0; y < height + 2; y++)
992 memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
994 ff_mqc_initdec(&t1->mqc, cblk->data);
995 cblk->data[cblk->length] = 0xff;
996 cblk->data[cblk->length + 1] = 0xff;
1001 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1002 bpass_csty_symbol && (clnpass_cnt >= 4),
1003 vert_causal_ctx_csty_symbol);
1006 decode_refpass(t1, width, height, bpno + 1);
1007 if (bpass_csty_symbol && clnpass_cnt >= 4)
1008 ff_mqc_initdec(&t1->mqc, cblk->data);
1011 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1012 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1013 vert_causal_ctx_csty_symbol);
1014 clnpass_cnt = clnpass_cnt + 1;
1015 if (bpass_csty_symbol && clnpass_cnt >= 4)
1016 ff_mqc_initdec(&t1->mqc, cblk->data);
1029 /* TODO: Verify dequantization for lossless case
1030 * comp->data can be float or int
1031 * band->stepsize can be float or int
1032 * depending on the type of DWT transformation.
1033 * see ISO/IEC 15444-1:2002 A.6.1 */
1035 /* Float dequantization of a codeblock.*/
1036 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1037 Jpeg2000Component *comp,
1038 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1041 int w = cblk->coord[0][1] - cblk->coord[0][0];
1042 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1043 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1044 int *src = t1->data[j];
1045 for (i = 0; i < w; ++i)
1046 datap[i] = src[i] * band->f_stepsize;
1050 /* Integer dequantization of a codeblock.*/
1051 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1052 Jpeg2000Component *comp,
1053 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1056 int w = cblk->coord[0][1] - cblk->coord[0][0];
1057 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1058 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1059 int *src = t1->data[j];
1060 for (i = 0; i < w; ++i)
1061 datap[i] = (src[i] * band->i_stepsize + (1 << 15)) >> 16;
1065 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1070 for (i = 0; i < 3; i++)
1071 if (tile->codsty[0].transform == FF_DWT97)
1072 src[i] = tile->comp[i].f_data;
1074 src[i] = tile->comp[i].i_data;
1076 for (i = 0; i < 2; i++)
1077 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1079 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1082 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1084 Jpeg2000T1Context t1;
1086 int compno, reslevelno, bandno;
1088 /* Loop on tile components */
1090 for (compno = 0; compno < s->ncomponents; compno++) {
1091 Jpeg2000Component *comp = tile->comp + compno;
1092 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1093 /* Loop on resolution levels */
1094 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1095 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1097 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1098 uint16_t nb_precincts, precno;
1099 Jpeg2000Band *band = rlevel->band + bandno;
1100 int cblkno = 0, bandpos;
1101 bandpos = bandno + (reslevelno > 0);
1103 if (band->coord[0][0] == band->coord[0][1] ||
1104 band->coord[1][0] == band->coord[1][1])
1107 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1108 /* Loop on precincts */
1109 for (precno = 0; precno < nb_precincts; precno++) {
1110 Jpeg2000Prec *prec = band->prec + precno;
1112 /* Loop on codeblocks */
1114 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1117 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1118 decode_cblk(s, codsty, &t1, cblk,
1119 cblk->coord[0][1] - cblk->coord[0][0],
1120 cblk->coord[1][1] - cblk->coord[1][0],
1123 x = cblk->coord[0][0];
1124 y = cblk->coord[1][0];
1126 if (codsty->transform == FF_DWT97)
1127 dequantization_float(x, y, cblk, comp, &t1, band);
1129 dequantization_int(x, y, cblk, comp, &t1, band);
1133 } /* end reslevel */
1136 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1140 #define WRITE_FRAME(D, PIXEL) \
1141 static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1142 AVFrame * picture) \
1144 int linesize = picture->linesize[0] / sizeof(PIXEL); \
1148 for (compno = 0; compno < s->ncomponents; compno++) { \
1149 Jpeg2000Component *comp = tile->comp + compno; \
1150 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1152 float *datap = comp->f_data; \
1153 int32_t *i_datap = comp->i_data; \
1154 int cbps = s->cbps[compno]; \
1155 int w = tile->comp[compno].coord[0][1] - s->image_offset_x; \
1157 y = tile->comp[compno].coord[1][0] - s->image_offset_y; \
1158 line = (PIXEL *)picture->data[0] + y * linesize; \
1159 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) { \
1162 x = tile->comp[compno].coord[0][0] - s->image_offset_x; \
1163 dst = line + x * s->ncomponents + compno; \
1165 if (codsty->transform == FF_DWT97) { \
1166 for (; x < w; x += s->cdx[compno]) { \
1167 int val = lrintf(*datap) + (1 << (cbps - 1)); \
1168 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1169 val = av_clip(val, 0, (1 << cbps) - 1); \
1170 *dst = val << (8 * sizeof(PIXEL) - cbps); \
1172 dst += s->ncomponents; \
1175 for (; x < w; x += s->cdx[compno]) { \
1176 int val = *i_datap + (1 << (cbps - 1)); \
1177 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
1178 val = av_clip(val, 0, (1 << cbps) - 1); \
1179 *dst = val << (8 * sizeof(PIXEL) - cbps); \
1181 dst += s->ncomponents; \
1190 WRITE_FRAME(8, uint8_t)
1191 WRITE_FRAME(16, uint16_t)
1195 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1198 tile_codeblocks(s, tile);
1200 /* inverse MCT transformation */
1201 if (tile->codsty[0].mct)
1202 mct_decode(s, tile);
1204 if (s->precision <= 8) {
1205 write_frame_8(s, tile, picture);
1207 write_frame_16(s, tile, picture);
1213 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1216 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1217 for (compno = 0; compno < s->ncomponents; compno++) {
1218 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1219 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1221 ff_jpeg2000_cleanup(comp, codsty);
1223 av_freep(&s->tile[tileno].comp);
1226 s->numXtiles = s->numYtiles = 0;
1229 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1231 Jpeg2000CodingStyle *codsty = s->codsty;
1232 Jpeg2000QuantStyle *qntsty = s->qntsty;
1233 uint8_t *properties = s->properties;
1240 if (bytestream2_get_bytes_left(&s->g) < 2) {
1241 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1245 marker = bytestream2_get_be16u(&s->g);
1246 oldpos = bytestream2_tell(&s->g);
1248 if (marker == JPEG2000_SOD) {
1250 Jpeg2000TilePart *tp;
1252 if (s->curtileno < 0) {
1253 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1254 return AVERROR_INVALIDDATA;
1257 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1258 return AVERROR_INVALIDDATA;
1261 tile = s->tile + s->curtileno;
1262 tp = tile->tile_part + tile->tp_idx;
1263 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1264 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1268 if (marker == JPEG2000_EOC)
1271 len = bytestream2_get_be16u(&s->g);
1272 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1273 return AVERROR_INVALIDDATA;
1280 ret = get_coc(s, codsty, properties);
1283 ret = get_cod(s, codsty, properties);
1286 ret = get_qcc(s, len, qntsty, properties);
1289 ret = get_qcd(s, len, qntsty, properties);
1292 if (!(ret = get_sot(s, len))) {
1293 codsty = s->tile[s->curtileno].codsty;
1294 qntsty = s->tile[s->curtileno].qntsty;
1295 properties = s->tile[s->curtileno].properties;
1299 // the PLT marker is ignored
1301 // the PLM marker is ignored
1303 // the comment is ignored
1304 bytestream2_skip(&s->g, len - 2);
1307 // Tile-part lengths
1308 ret = get_tlm(s, len);
1311 av_log(s->avctx, AV_LOG_ERROR,
1312 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1313 marker, bytestream2_tell(&s->g) - 4);
1314 bytestream2_skip(&s->g, len - 2);
1317 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1318 av_log(s->avctx, AV_LOG_ERROR,
1319 "error during processing marker segment %.4"PRIx16"\n",
1321 return ret ? ret : -1;
1327 /* Read bit stream packets --> T2 operation. */
1328 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1333 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1334 Jpeg2000Tile *tile = s->tile + tileno;
1336 if (ret = init_tile(s, tileno))
1339 s->g = tile->tile_part[0].tpg;
1340 if (ret = jpeg2000_decode_packets(s, tile))
1347 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1349 uint32_t atom_size, atom;
1350 int found_codestream = 0, search_range = 10;
1352 while(!found_codestream && search_range
1354 bytestream2_get_bytes_left(&s->g) >= 8) {
1355 atom_size = bytestream2_get_be32u(&s->g);
1356 atom = bytestream2_get_be32u(&s->g);
1357 if (atom == JP2_CODESTREAM) {
1358 found_codestream = 1;
1360 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1362 bytestream2_skipu(&s->g, atom_size - 8);
1367 if (found_codestream)
1372 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
1374 Jpeg2000DecoderContext *s = avctx->priv_data;
1376 ff_jpeg2000dsp_init(&s->dsp);
1381 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1382 int *got_frame, AVPacket *avpkt)
1384 Jpeg2000DecoderContext *s = avctx->priv_data;
1385 ThreadFrame frame = { .f = data };
1386 AVFrame *picture = data;
1390 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1391 s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1393 if (bytestream2_get_bytes_left(&s->g) < 2) {
1394 ret = AVERROR_INVALIDDATA;
1398 // check if the image is in jp2 format
1399 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1400 (bytestream2_get_be32u(&s->g) == 12) &&
1401 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1402 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1403 if (!jp2_find_codestream(s)) {
1404 av_log(avctx, AV_LOG_ERROR,
1405 "Could not find Jpeg2000 codestream atom.\n");
1406 ret = AVERROR_INVALIDDATA;
1410 bytestream2_seek(&s->g, 0, SEEK_SET);
1413 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1414 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1415 ret = AVERROR_INVALIDDATA;
1418 if (ret = jpeg2000_read_main_headers(s))
1421 /* get picture buffer */
1422 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
1423 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
1426 picture->pict_type = AV_PICTURE_TYPE_I;
1427 picture->key_frame = 1;
1429 if (ret = jpeg2000_read_bitstream_packets(s))
1431 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1432 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1435 jpeg2000_dec_cleanup(s);
1439 return bytestream2_tell(&s->g);
1442 jpeg2000_dec_cleanup(s);
1446 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
1448 ff_jpeg2000_init_tier1_luts();
1449 ff_mqc_init_context_tables();
1452 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1453 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1455 static const AVOption options[] = {
1456 { "lowres", "Lower the decoding resolution by a power of two",
1457 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1461 static const AVClass class = {
1462 .class_name = "jpeg2000",
1463 .item_name = av_default_item_name,
1465 .version = LIBAVUTIL_VERSION_INT,
1468 AVCodec ff_jpeg2000_decoder = {
1470 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1471 .type = AVMEDIA_TYPE_VIDEO,
1472 .id = AV_CODEC_ID_JPEG2000,
1473 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
1474 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1475 .init_static_data = jpeg2000_init_static_data,
1476 .init = jpeg2000_decode_init,
1477 .decode = jpeg2000_decode_frame,
1478 .priv_class = &class,
1479 .profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)