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 FFmpeg.
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.
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.
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
25 * JPEG 2000 image decoder
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
32 #include "bytestream.h"
37 #define JP2_SIG_TYPE 0x6A502020
38 #define JP2_SIG_VALUE 0x0D0A870A
39 #define JP2_CODESTREAM 0x6A703263
44 typedef struct Jpeg2000TilePart {
45 uint8_t tile_index; // Tile index who refers the tile-part
46 const uint8_t *tp_end;
47 GetByteContext tpg; // bit stream in tile-part
50 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
51 * one per component, so tile_part elements have a size of 3 */
52 typedef struct Jpeg2000Tile {
53 Jpeg2000Component *comp;
54 uint8_t properties[4];
55 Jpeg2000CodingStyle codsty[4];
56 Jpeg2000QuantStyle qntsty[4];
57 Jpeg2000TilePart tile_part[4];
58 uint16_t tp_idx; // Tile-part index
61 typedef struct Jpeg2000DecoderContext {
63 AVCodecContext *avctx;
67 int image_offset_x, image_offset_y;
68 int tile_offset_x, tile_offset_y;
69 uint8_t cbps[4]; // bits per sample in particular components
70 uint8_t sgnd[4]; // if a component is signed
71 uint8_t properties[4];
75 int tile_width, tile_height;
76 int numXtiles, numYtiles;
79 Jpeg2000CodingStyle codsty[4];
80 Jpeg2000QuantStyle qntsty[4];
88 /*options parameters*/
91 } Jpeg2000DecoderContext;
93 /* get_bits functions for JPEG2000 packet bitstream
94 * It is a get_bit function with a bit-stuffing routine. If the value of the
95 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
96 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
97 static int get_bits(Jpeg2000DecoderContext *s, int n)
103 if (s->bit_index == 0) {
104 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
107 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
112 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
114 if (bytestream2_get_byte(&s->g) == 0xff)
115 bytestream2_skip(&s->g, 1);
119 /* decode the value stored in node */
120 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
123 Jpeg2000TgtNode *stack[30];
124 int sp = -1, curval = 0;
127 return AVERROR(EINVAL);
129 while (node && !node->vis) {
137 curval = stack[sp]->val;
139 while (curval < threshold && sp >= 0) {
140 if (curval < stack[sp]->val)
141 curval = stack[sp]->val;
142 while (curval < threshold) {
144 if ((ret = get_bits(s, 1)) > 0) {
152 stack[sp]->val = curval;
158 /* marker segments */
159 /* get sizes and offsets of image, tiles; number of components */
160 static int get_siz(Jpeg2000DecoderContext *s)
165 if (bytestream2_get_bytes_left(&s->g) < 36)
166 return AVERROR(EINVAL);
168 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
169 s->width = bytestream2_get_be32u(&s->g); // Width
170 s->height = bytestream2_get_be32u(&s->g); // Height
171 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
172 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
173 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
174 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
175 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
176 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
177 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
179 if (ncomponents <= 0 || ncomponents > 4) {
180 av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", ncomponents);
181 return AVERROR(EINVAL);
183 s->ncomponents = ncomponents;
185 if (s->tile_width<=0 || s->tile_height<=0)
186 return AVERROR(EINVAL);
188 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
189 return AVERROR(EINVAL);
191 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
192 uint8_t x = bytestream2_get_byteu(&s->g);
193 s->cbps[i] = (x & 0x7f) + 1;
194 s->precision = FFMAX(s->cbps[i], s->precision);
195 s->sgnd[i] = !!(x & 0x80);
196 s->cdx[i] = bytestream2_get_byteu(&s->g);
197 s->cdy[i] = bytestream2_get_byteu(&s->g);
198 if (s->cdx[i] != 1 || s->cdy[i] != 1) {
199 av_log(s->avctx, AV_LOG_ERROR, "unsupported/ CDxy values %d %d for component %d\n", s->cdx[i], s->cdy[i], i);
200 if (!s->cdx[i] || !s->cdy[i])
201 return AVERROR_INVALIDDATA;
205 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
206 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
208 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
209 return AVERROR(EINVAL);
211 s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
213 return AVERROR(ENOMEM);
215 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
216 Jpeg2000Tile *tile = s->tile + i;
218 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
220 return AVERROR(ENOMEM);
223 /* compute image size with reduction factor */
224 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
225 s->reduction_factor);
226 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
227 s->reduction_factor);
229 switch(s->ncomponents) {
231 if (s->precision > 8)
232 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
234 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
237 switch (s->avctx->profile) {
238 case FF_PROFILE_JPEG2000_DCINEMA_2K:
239 case FF_PROFILE_JPEG2000_DCINEMA_4K:
240 /* XYZ color-space for digital cinema profiles */
241 s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
244 if (s->precision > 8)
245 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
247 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
252 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
255 /* pixel format can not be identified */
256 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
262 /* get common part for COD and COC segments */
263 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
267 if (bytestream2_get_bytes_left(&s->g) < 5)
268 return AVERROR(EINVAL);
269 /* nreslevels = number of resolution levels
270 = number of decomposition level +1 */
271 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
272 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
273 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
274 return AVERROR_INVALIDDATA;
277 /* compute number of resolution levels to decode */
278 if (c->nreslevels < s->reduction_factor)
279 c->nreslevels2decode = 1;
281 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
283 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
284 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
286 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
287 c->log2_cblk_width + c->log2_cblk_height > 14) {
288 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
289 return AVERROR_INVALIDDATA;
292 c->cblk_style = bytestream2_get_byteu(&s->g);
293 if (c->cblk_style != 0) { // cblk style
294 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
296 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
297 /* set integer 9/7 DWT in case of BITEXACT flag */
298 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
299 c->transform = FF_DWT97_INT;
301 if (c->csty & JPEG2000_CSTY_PREC) {
303 for (i = 0; i < c->nreslevels; i++) {
304 byte = bytestream2_get_byte(&s->g);
305 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
306 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
309 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
310 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
315 /* get coding parameters for a particular tile or whole image*/
316 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
319 Jpeg2000CodingStyle tmp;
322 if (bytestream2_get_bytes_left(&s->g) < 5)
323 return AVERROR(EINVAL);
325 tmp.csty = bytestream2_get_byteu(&s->g);
327 // get progression order
328 tmp.prog_order = bytestream2_get_byteu(&s->g);
330 tmp.nlayers = bytestream2_get_be16u(&s->g);
331 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
333 if (tmp.mct && s->ncomponents < 3) {
334 av_log(s->avctx, AV_LOG_ERROR, "MCT %d with too few components (%d)\n", tmp.mct, s->ncomponents);
335 return AVERROR_INVALIDDATA;
338 if ((ret = get_cox(s, &tmp)) < 0)
341 for (compno = 0; compno < s->ncomponents; compno++)
342 if (!(properties[compno] & HAD_COC))
343 memcpy(c + compno, &tmp, sizeof(tmp));
347 /* Get coding parameters for a component in the whole image or a
348 * particular tile. */
349 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
354 if (bytestream2_get_bytes_left(&s->g) < 2)
355 return AVERROR(EINVAL);
357 compno = bytestream2_get_byteu(&s->g);
359 if (compno >= s->ncomponents) {
360 av_log(s->avctx, AV_LOG_ERROR, "Invalid compno %d\n", compno);
361 return AVERROR_INVALIDDATA;
365 c->csty = bytestream2_get_byteu(&s->g);
367 if ((ret = get_cox(s, c)) < 0)
370 properties[compno] |= HAD_COC;
374 /* Get common part for QCD and QCC segments. */
375 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
379 if (bytestream2_get_bytes_left(&s->g) < 1)
380 return AVERROR(EINVAL);
382 x = bytestream2_get_byteu(&s->g); // Sqcd
384 q->nguardbits = x >> 5;
385 q->quantsty = x & 0x1f;
387 if (q->quantsty == JPEG2000_QSTY_NONE) {
389 if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
390 return AVERROR(EINVAL);
391 for (i = 0; i < n; i++)
392 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
393 } else if (q->quantsty == JPEG2000_QSTY_SI) {
394 if (bytestream2_get_bytes_left(&s->g) < 2)
395 return AVERROR(EINVAL);
396 x = bytestream2_get_be16u(&s->g);
397 q->expn[0] = x >> 11;
398 q->mant[0] = x & 0x7ff;
399 for (i = 1; i < 32 * 3; i++) {
400 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
401 q->expn[i] = curexpn;
402 q->mant[i] = q->mant[0];
406 if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
407 return AVERROR(EINVAL);
408 for (i = 0; i < n; i++) {
409 x = bytestream2_get_be16u(&s->g);
410 q->expn[i] = x >> 11;
411 q->mant[i] = x & 0x7ff;
417 /* Get quantization parameters for a particular tile or a whole image. */
418 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
421 Jpeg2000QuantStyle tmp;
424 if (get_qcx(s, n, &tmp))
426 for (compno = 0; compno < s->ncomponents; compno++)
427 if (!(properties[compno] & HAD_QCC))
428 memcpy(q + compno, &tmp, sizeof(tmp));
432 /* Get quantization parameters for a component in the whole image
433 * on in a particular tile. */
434 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
439 if (bytestream2_get_bytes_left(&s->g) < 1)
440 return AVERROR(EINVAL);
442 compno = bytestream2_get_byteu(&s->g);
443 if (compno >= s->ncomponents) {
444 av_log(s->avctx, AV_LOG_ERROR, "Invalid compno\n");
445 return AVERROR_INVALIDDATA;
448 properties[compno] |= HAD_QCC;
449 return get_qcx(s, n - 1, q + compno);
452 /* Get start of tile segment. */
453 static int get_sot(Jpeg2000DecoderContext *s, int n)
455 Jpeg2000TilePart *tp;
460 if (bytestream2_get_bytes_left(&s->g) < 8)
461 return AVERROR(EINVAL);
463 s->curtileno = Isot = bytestream2_get_be16u(&s->g); // Isot
464 if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
466 return AVERROR(EINVAL);
468 Psot = bytestream2_get_be32u(&s->g); // Psot
469 TPsot = bytestream2_get_byteu(&s->g); // TPsot
471 /* Read TNSot but not used */
472 bytestream2_get_byteu(&s->g); // TNsot
474 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
475 av_log(s->avctx, AV_LOG_ERROR, "Psot %d too big\n", Psot);
476 return AVERROR_INVALIDDATA;
478 if (TPsot >= FF_ARRAY_ELEMS(s->tile[s->curtileno].tile_part)) {
479 av_log(s->avctx, AV_LOG_ERROR, "TPsot %d too big\n", TPsot);
480 return AVERROR_PATCHWELCOME;
483 s->tile[s->curtileno].tp_idx = TPsot;
484 tp = s->tile[s->curtileno].tile_part + TPsot;
485 tp->tile_index = Isot;
486 tp->tp_end = s->g.buffer + Psot - n - 2;
489 Jpeg2000Tile *tile = s->tile + s->curtileno;
492 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
493 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
499 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
500 * Used to know the number of tile parts and lengths.
501 * There may be multiple TLMs in the header.
502 * TODO: The function is not used for tile-parts management, nor anywhere else.
503 * It can be useful to allocate memory for tile parts, before managing the SOT
504 * markers. Parsing the TLM header is needed to increment the input header
506 * This marker is mandatory for DCI. */
507 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
509 uint8_t Stlm, ST, SP, tile_tlm, i;
510 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
511 Stlm = bytestream2_get_byte(&s->g);
513 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
514 ST = (Stlm >> 4) & 0x03;
515 // TODO: Manage case of ST = 0b11 --> raise error
516 SP = (Stlm >> 6) & 0x01;
517 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
518 for (i = 0; i < tile_tlm; i++) {
523 bytestream2_get_byte(&s->g);
526 bytestream2_get_be16(&s->g);
529 bytestream2_get_be32(&s->g);
533 bytestream2_get_be16(&s->g);
535 bytestream2_get_be32(&s->g);
541 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
544 int tilex = tileno % s->numXtiles;
545 int tiley = tileno / s->numXtiles;
546 Jpeg2000Tile *tile = s->tile + tileno;
549 return AVERROR(ENOMEM);
551 for (compno = 0; compno < s->ncomponents; compno++) {
552 Jpeg2000Component *comp = tile->comp + compno;
553 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
554 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
555 int ret; // global bandno
557 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
558 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
559 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
560 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
562 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
563 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
564 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
565 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
567 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
568 s->cbps[compno], s->cdx[compno],
569 s->cdy[compno], s->avctx))
575 /* Read the number of coding passes. */
576 static int getnpasses(Jpeg2000DecoderContext *s)
583 if ((num = get_bits(s, 2)) != 3)
584 return num < 0 ? num : 3 + num;
585 if ((num = get_bits(s, 5)) != 31)
586 return num < 0 ? num : 6 + num;
587 num = get_bits(s, 7);
588 return num < 0 ? num : 37 + num;
591 static int getlblockinc(Jpeg2000DecoderContext *s)
594 while (ret = get_bits(s, 1)) {
602 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
603 Jpeg2000CodingStyle *codsty,
604 Jpeg2000ResLevel *rlevel, int precno,
605 int layno, uint8_t *expn, int numgbits)
607 int bandno, cblkno, ret, nb_code_blocks;
609 if (!(ret = get_bits(s, 1))) {
615 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
616 Jpeg2000Band *band = rlevel->band + bandno;
617 Jpeg2000Prec *prec = band->prec + precno;
619 if (band->coord[0][0] == band->coord[0][1] ||
620 band->coord[1][0] == band->coord[1][1])
623 nb_code_blocks = prec->nb_codeblocks_height *
624 prec->nb_codeblocks_width;
625 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
626 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
627 int incl, newpasses, llen;
630 incl = get_bits(s, 1);
632 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
638 if (!cblk->npasses) {
639 int v = expn[bandno] + numgbits - 1 -
640 tag_tree_decode(s, prec->zerobits + cblkno,
643 av_log(s->avctx, AV_LOG_ERROR, "nonzerobits %d invalid\n", v);
644 return AVERROR_INVALIDDATA;
646 cblk->nonzerobits = v;
648 if ((newpasses = getnpasses(s)) < 0)
650 if ((llen = getlblockinc(s)) < 0)
652 cblk->lblock += llen;
653 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
655 cblk->lengthinc = ret;
656 cblk->npasses += newpasses;
661 if (codsty->csty & JPEG2000_CSTY_EPH) {
662 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
663 bytestream2_skip(&s->g, 2);
665 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
668 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
669 Jpeg2000Band *band = rlevel->band + bandno;
670 Jpeg2000Prec *prec = band->prec + precno;
672 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
673 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
674 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
675 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
676 || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
678 return AVERROR(EINVAL);
680 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
681 cblk->length += cblk->lengthinc;
688 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
690 int layno, reslevelno, compno, precno, ok_reslevel;
694 switch (tile->codsty[0].prog_order) {
695 case JPEG2000_PGOD_LRCP:
696 case JPEG2000_PGOD_RLCP:
697 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
699 for (reslevelno = 0; ok_reslevel; reslevelno++) {
701 for (compno = 0; compno < s->ncomponents; compno++) {
702 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
703 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
704 if (reslevelno < codsty->nreslevels) {
705 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
708 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
709 if (jpeg2000_decode_packet(s,
712 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
721 case JPEG2000_PGOD_CPRL:
722 for (compno = 0; compno < s->ncomponents; compno++) {
723 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
724 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
726 /* Set bit stream buffer address according to tile-part.
727 * For DCinema one tile-part per component, so can be
728 * indexed by component. */
729 s->g = tile->tile_part[compno].tpg;
731 /* Position loop (y axis)
732 * TODO: Automate computing of step 256.
733 * Fixed here, but to be computed before entering here. */
734 for (y = 0; y < s->height; y += 256) {
735 /* Position loop (y axis)
736 * TODO: automate computing of step 256.
737 * Fixed here, but to be computed before entering here. */
738 for (x = 0; x < s->width; x += 256) {
739 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
741 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
742 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
744 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
745 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
748 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
749 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
752 // check if a precinct exists
753 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
754 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
755 precno = prcx + rlevel->num_precincts_x * prcy;
756 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
757 if (jpeg2000_decode_packet(s, codsty, rlevel,
759 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
773 /* EOC marker reached */
774 bytestream2_skip(&s->g, 2);
779 /* TIER-1 routines */
780 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
781 int bpno, int bandno, int bpass_csty_symbol,
782 int vert_causal_ctx_csty_symbol)
784 int mask = 3 << (bpno - 1), y0, x, y;
786 for (y0 = 0; y0 < height; y0 += 4)
787 for (x = 0; x < width; x++)
788 for (y = y0; y < height && y < y0 + 4; y++) {
789 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
790 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
792 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
793 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
794 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
795 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
796 if (bpass_csty_symbol)
797 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
799 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
802 ff_jpeg2000_set_significance(t1, x, y,
805 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
810 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
816 phalf = 1 << (bpno - 1);
819 for (y0 = 0; y0 < height; y0 += 4)
820 for (x = 0; x < width; x++)
821 for (y = y0; y < height && y < y0 + 4; y++)
822 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
823 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
824 int r = ff_mqc_decode(&t1->mqc,
825 t1->mqc.cx_states + ctxno)
827 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
828 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
832 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
833 int width, int height, int bpno, int bandno,
834 int seg_symbols, int vert_causal_ctx_csty_symbol)
836 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
838 for (y0 = 0; y0 < height; y0 += 4) {
839 for (x = 0; x < width; x++) {
840 if (y0 + 3 < height &&
841 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
842 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
843 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
844 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
845 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
847 runlen = ff_mqc_decode(&t1->mqc,
848 t1->mqc.cx_states + MQC_CX_UNI);
849 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
858 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
860 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
862 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
863 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
864 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
870 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
872 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
873 t1->mqc.cx_states + ctxno) ^
876 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
879 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
885 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
886 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
887 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
888 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
890 av_log(s->avctx, AV_LOG_ERROR,
891 "Segmentation symbol value incorrect\n");
895 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
896 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
897 int width, int height, int bandpos)
899 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
900 int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
901 int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
903 for (y = 0; y < height; y++)
904 memset(t1->data[y], 0, width * sizeof(**t1->data));
906 /* If code-block contains no compressed data: nothing to do. */
910 for (y = 0; y < height+2; y++)
911 memset(t1->flags[y], 0, (width + 2)*sizeof(**t1->flags));
913 cblk->data[cblk->length] = 0xff;
914 cblk->data[cblk->length+1] = 0xff;
915 ff_mqc_initdec(&t1->mqc, cblk->data);
920 decode_sigpass(t1, width, height, bpno + 1, bandpos,
921 bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
924 decode_refpass(t1, width, height, bpno + 1);
925 if (bpass_csty_symbol && clnpass_cnt >= 4)
926 ff_mqc_initdec(&t1->mqc, cblk->data);
929 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
930 codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
931 clnpass_cnt = clnpass_cnt + 1;
932 if (bpass_csty_symbol && clnpass_cnt >= 4)
933 ff_mqc_initdec(&t1->mqc, cblk->data);
946 /* TODO: Verify dequantization for lossless case
947 * comp->data can be float or int
948 * band->stepsize can be float or int
949 * depending on the type of DWT transformation.
950 * see ISO/IEC 15444-1:2002 A.6.1 */
952 /* Float dequantization of a codeblock.*/
953 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
954 Jpeg2000Component *comp,
955 Jpeg2000T1Context *t1, Jpeg2000Band *band)
958 int w = cblk->coord[0][1] - cblk->coord[0][0];
959 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
960 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
961 int *src = t1->data[j];
962 for (i = 0; i < w; ++i)
963 datap[i] = src[i] * band->f_stepsize;
967 /* Integer dequantization of a codeblock.*/
968 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
969 Jpeg2000Component *comp,
970 Jpeg2000T1Context *t1, Jpeg2000Band *band)
973 int w = cblk->coord[0][1] - cblk->coord[0][0];
974 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
975 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
976 int *src = t1->data[j];
977 for (i = 0; i < w; ++i)
978 datap[i] = (src[i] * band->i_stepsize + (1 << 15)) >> 16;
982 /* Inverse ICT parameters in float and integer.
983 * int value = (float value) * (1<<16) */
984 static const float f_ict_params[4] = {
990 static const int i_ict_params[4] = {
997 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1000 int32_t *src[3], i0, i1, i2;
1001 float *srcf[3], i0f, i1f, i2f;
1003 for (i = 0; i < 3; i++)
1004 if (tile->codsty[0].transform == FF_DWT97)
1005 srcf[i] = tile->comp[i].f_data;
1007 src [i] = tile->comp[i].i_data;
1009 for (i = 0; i < 2; i++)
1010 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1012 switch (tile->codsty[0].transform) {
1014 for (i = 0; i < csize; i++) {
1015 i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
1016 i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
1017 - (f_ict_params[2] * *srcf[2]);
1018 i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
1025 for (i = 0; i < csize; i++) {
1026 i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1027 i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1028 - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
1029 i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1036 for (i = 0; i < csize; i++) {
1037 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1048 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1051 int compno, reslevelno, bandno;
1055 Jpeg2000T1Context t1;
1057 /* Loop on tile components */
1058 for (compno = 0; compno < s->ncomponents; compno++) {
1059 Jpeg2000Component *comp = tile->comp + compno;
1060 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1062 /* Loop on resolution levels */
1063 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1064 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1066 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1067 int nb_precincts, precno;
1068 Jpeg2000Band *band = rlevel->band + bandno;
1069 int cblkno = 0, bandpos;
1071 bandpos = bandno + (reslevelno > 0);
1073 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1076 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1077 /* Loop on precincts */
1078 for (precno = 0; precno < nb_precincts; precno++) {
1079 Jpeg2000Prec *prec = band->prec + precno;
1081 /* Loop on codeblocks */
1082 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1084 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1085 decode_cblk(s, codsty, &t1, cblk,
1086 cblk->coord[0][1] - cblk->coord[0][0],
1087 cblk->coord[1][1] - cblk->coord[1][0],
1090 /* Manage band offsets */
1091 x = cblk->coord[0][0];
1092 y = cblk->coord[1][0];
1094 if (codsty->transform == FF_DWT97)
1095 dequantization_float(x, y, cblk, comp, &t1, band);
1097 dequantization_int(x, y, cblk, comp, &t1, band);
1101 } /* end reslevel */
1104 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1107 /* inverse MCT transformation */
1108 if (tile->codsty[0].mct)
1109 mct_decode(s, tile);
1111 if (s->precision <= 8) {
1112 for (compno = 0; compno < s->ncomponents; compno++) {
1113 Jpeg2000Component *comp = tile->comp + compno;
1114 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1115 float *datap = comp->f_data;
1116 int32_t *i_datap = comp->i_data;
1117 int cbps = s->cbps[compno];
1118 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1120 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1121 line = picture->data[0] + y * picture->linesize[0];
1122 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1125 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1126 dst = line + x * s->ncomponents + compno;
1128 if (codsty->transform == FF_DWT97) {
1129 for (; x < w; x += s->cdx[compno]) {
1130 int val = lrintf(*datap) + (1 << (cbps - 1));
1131 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1132 val = av_clip(val, 0, (1 << cbps) - 1);
1133 *dst = val << (8 - cbps);
1135 dst += s->ncomponents;
1138 for (; x < w; x += s->cdx[compno]) {
1139 int val = *i_datap + (1 << (cbps - 1));
1140 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1141 val = av_clip(val, 0, (1 << cbps) - 1);
1142 *dst = val << (8 - cbps);
1144 dst += s->ncomponents;
1147 line += picture->linesize[0];
1151 for (compno = 0; compno < s->ncomponents; compno++) {
1152 Jpeg2000Component *comp = tile->comp + compno;
1153 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1154 float *datap = comp->f_data;
1155 int32_t *i_datap = comp->i_data;
1157 int cbps = s->cbps[compno];
1158 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1160 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1161 linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
1162 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1165 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1166 dst = linel + (x * s->ncomponents + compno);
1167 if (codsty->transform == FF_DWT97) {
1168 for (; x < w; x += s-> cdx[compno]) {
1169 int val = lrintf(*datap) + (1 << (cbps - 1));
1170 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1171 val = av_clip(val, 0, (1 << cbps) - 1);
1172 /* align 12 bit values in little-endian mode */
1173 *dst = val << (16 - cbps);
1175 dst += s->ncomponents;
1178 for (; x < w; x += s-> cdx[compno]) {
1179 int val = *i_datap + (1 << (cbps - 1));
1180 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1181 val = av_clip(val, 0, (1 << cbps) - 1);
1182 /* align 12 bit values in little-endian mode */
1183 *dst = val << (16 - cbps);
1185 dst += s->ncomponents;
1188 linel += picture->linesize[0] >> 1;
1196 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1199 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1200 for (compno = 0; compno < s->ncomponents; compno++) {
1201 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1202 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1204 ff_jpeg2000_cleanup(comp, codsty);
1206 av_freep(&s->tile[tileno].comp);
1209 s->numXtiles = s->numYtiles = 0;
1212 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1214 Jpeg2000CodingStyle *codsty = s->codsty;
1215 Jpeg2000QuantStyle *qntsty = s->qntsty;
1216 uint8_t *properties = s->properties;
1223 if (bytestream2_get_bytes_left(&s->g) < 2) {
1224 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1228 marker = bytestream2_get_be16u(&s->g);
1229 oldpos = bytestream2_tell(&s->g);
1231 if (marker == JPEG2000_SOD) {
1233 Jpeg2000TilePart *tp;
1235 if (s->curtileno < 0) {
1236 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1237 return AVERROR_INVALIDDATA;
1240 tile = s->tile + s->curtileno;
1241 tp = tile->tile_part + tile->tp_idx;
1242 if (tp->tp_end < s->g.buffer) {
1243 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1244 return AVERROR_INVALIDDATA;
1246 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1247 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1251 if (marker == JPEG2000_EOC)
1254 len = bytestream2_get_be16(&s->g);
1255 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1256 return AVERROR(EINVAL);
1261 s->numXtiles = s->numYtiles = 0;
1264 ret = get_coc(s, codsty, properties);
1267 ret = get_cod(s, codsty, properties);
1270 ret = get_qcc(s, len, qntsty, properties);
1273 ret = get_qcd(s, len, qntsty, properties);
1276 if (!(ret = get_sot(s, len))) {
1277 av_assert1(s->curtileno >= 0);
1278 codsty = s->tile[s->curtileno].codsty;
1279 qntsty = s->tile[s->curtileno].qntsty;
1280 properties = s->tile[s->curtileno].properties;
1284 // the comment is ignored
1285 bytestream2_skip(&s->g, len - 2);
1288 // Tile-part lengths
1289 ret = get_tlm(s, len);
1292 av_log(s->avctx, AV_LOG_ERROR,
1293 "unsupported marker 0x%.4X at pos 0x%X\n",
1294 marker, bytestream2_tell(&s->g) - 4);
1295 bytestream2_skip(&s->g, len - 2);
1298 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1299 av_log(s->avctx, AV_LOG_ERROR,
1300 "error during processing marker segment %.4x\n", marker);
1301 return ret ? ret : -1;
1307 /* Read bit stream packets --> T2 operation. */
1308 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1313 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1314 Jpeg2000Tile *tile = s->tile + tileno;
1316 if (ret = init_tile(s, tileno))
1319 s->g = tile->tile_part[0].tpg;
1320 if (ret = jpeg2000_decode_packets(s, tile))
1327 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1329 uint32_t atom_size, atom;
1330 int found_codestream = 0, search_range = 10;
1332 while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1333 atom_size = bytestream2_get_be32u(&s->g);
1334 atom = bytestream2_get_be32u(&s->g);
1335 if (atom == JP2_CODESTREAM) {
1336 found_codestream = 1;
1338 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1340 bytestream2_skipu(&s->g, atom_size - 8);
1345 if (found_codestream)
1350 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1351 int *got_frame, AVPacket *avpkt)
1353 Jpeg2000DecoderContext *s = avctx->priv_data;
1354 ThreadFrame frame = { .f = data };
1355 AVFrame *picture = data;
1359 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1362 // reduction factor, i.e number of resolution levels to skip
1363 s->reduction_factor = s->lowres;
1365 if (bytestream2_get_bytes_left(&s->g) < 2) {
1366 ret = AVERROR(EINVAL);
1370 // check if the image is in jp2 format
1371 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1372 (bytestream2_get_be32u(&s->g) == 12) &&
1373 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1374 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1375 if (!jp2_find_codestream(s)) {
1376 av_log(avctx, AV_LOG_ERROR,
1377 "couldn't find jpeg2k codestream atom\n");
1382 bytestream2_seek(&s->g, 0, SEEK_SET);
1385 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1386 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1390 if (ret = jpeg2000_read_main_headers(s))
1393 /* get picture buffer */
1394 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1396 picture->pict_type = AV_PICTURE_TYPE_I;
1397 picture->key_frame = 1;
1399 if (ret = jpeg2000_read_bitstream_packets(s))
1402 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1403 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1406 jpeg2000_dec_cleanup(s);
1410 return bytestream2_tell(&s->g);
1412 jpeg2000_dec_cleanup(s);
1416 static void jpeg2000_init_static_data(AVCodec *codec)
1418 ff_jpeg2000_init_tier1_luts();
1421 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1422 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1424 static const AVOption options[] = {
1425 { "lowres", "Lower the decoding resolution by a power of two",
1426 OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1430 static const AVProfile profiles[] = {
1431 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1432 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1433 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1434 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1435 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1436 { FF_PROFILE_UNKNOWN },
1439 static const AVClass jpeg2000_class = {
1440 .class_name = "jpeg2000",
1441 .item_name = av_default_item_name,
1443 .version = LIBAVUTIL_VERSION_INT,
1446 AVCodec ff_jpeg2000_decoder = {
1448 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1449 .type = AVMEDIA_TYPE_VIDEO,
1450 .id = AV_CODEC_ID_JPEG2000,
1451 .capabilities = CODEC_CAP_FRAME_THREADS,
1452 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1453 .init_static_data = jpeg2000_init_static_data,
1454 .decode = jpeg2000_decode_frame,
1455 .priv_class = &jpeg2000_class,
1457 .profiles = NULL_IF_CONFIG_SMALL(profiles)