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
28 #include "libavutil/common.h"
29 #include "libavutil/opt.h"
31 #include "bytestream.h"
36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
38 #define JP2_CODESTREAM 0x6A703263
43 typedef struct Jpeg2000TilePart {
44 uint8_t tile_index; // Tile index who refers the tile-part
45 const uint8_t *tp_end;
46 GetByteContext tpg; // bit stream in tile-part
49 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
50 * one per component, so tile_part elements have a size of 3 */
51 typedef struct Jpeg2000Tile {
52 Jpeg2000Component *comp;
53 uint8_t properties[4];
54 Jpeg2000CodingStyle codsty[4];
55 Jpeg2000QuantStyle qntsty[4];
56 Jpeg2000TilePart tile_part[3];
57 uint16_t tp_idx; // Tile-part index
60 typedef struct Jpeg2000DecoderContext {
62 AVCodecContext *avctx;
66 int image_offset_x, image_offset_y;
67 int tile_offset_x, tile_offset_y;
68 uint8_t cbps[4]; // bits per sample in particular components
69 uint8_t sgnd[4]; // if a component is signed
70 uint8_t properties[4];
74 int tile_width, tile_height;
75 unsigned numXtiles, numYtiles;
78 Jpeg2000CodingStyle codsty[4];
79 Jpeg2000QuantStyle qntsty[4];
86 /*options parameters*/
88 } Jpeg2000DecoderContext;
90 /* get_bits functions for JPEG2000 packet bitstream
91 * It is a get_bit function with a bit-stuffing routine. If the value of the
92 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
93 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
94 static int get_bits(Jpeg2000DecoderContext *s, int n)
99 if (s->bit_index == 0) {
100 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
103 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
108 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
110 if (bytestream2_get_byte(&s->g) == 0xff)
111 bytestream2_skip(&s->g, 1);
115 /* decode the value stored in node */
116 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
119 Jpeg2000TgtNode *stack[30];
120 int sp = -1, curval = 0;
123 return AVERROR_INVALIDDATA;
125 while (node && !node->vis) {
133 curval = stack[sp]->val;
135 while (curval < threshold && sp >= 0) {
136 if (curval < stack[sp]->val)
137 curval = stack[sp]->val;
138 while (curval < threshold) {
140 if ((ret = get_bits(s, 1)) > 0) {
148 stack[sp]->val = curval;
154 /* marker segments */
155 /* get sizes and offsets of image, tiles; number of components */
156 static int get_siz(Jpeg2000DecoderContext *s)
161 if (bytestream2_get_bytes_left(&s->g) < 36)
162 return AVERROR_INVALIDDATA;
164 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
165 s->width = bytestream2_get_be32u(&s->g); // Width
166 s->height = bytestream2_get_be32u(&s->g); // Height
167 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
168 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
169 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
170 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
171 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
172 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
173 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
175 if (ncomponents <= 0) {
176 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
178 return AVERROR_INVALIDDATA;
181 if (ncomponents > 3) {
182 avpriv_request_sample(s->avctx, "Support for %d components",
184 return AVERROR_PATCHWELCOME;
187 s->ncomponents = ncomponents;
189 if (s->tile_width <= 0 || s->tile_height <= 0 ||
190 s->tile_width > s->width || s->tile_height > s->height) {
191 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
192 s->tile_width, s->tile_height);
193 return AVERROR_INVALIDDATA;
196 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
197 return AVERROR_INVALIDDATA;
199 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
200 uint8_t x = bytestream2_get_byteu(&s->g);
201 s->cbps[i] = (x & 0x7f) + 1;
202 s->precision = FFMAX(s->cbps[i], s->precision);
203 s->sgnd[i] = !!(x & 0x80);
204 s->cdx[i] = bytestream2_get_byteu(&s->g);
205 s->cdy[i] = bytestream2_get_byteu(&s->g);
207 if (s->cdx[i] != 1 || s->cdy[i] != 1) {
208 avpriv_request_sample(s->avctx,
209 "CDxy values %d %d for component %d",
210 s->cdx[i], s->cdy[i], i);
211 if (!s->cdx[i] || !s->cdy[i])
212 return AVERROR_INVALIDDATA;
214 return AVERROR_PATCHWELCOME;
218 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
219 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
221 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
223 s->numXtiles = s->numYtiles = 0;
224 return AVERROR(ENOMEM);
227 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
228 Jpeg2000Tile *tile = s->tile + i;
230 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
232 return AVERROR(ENOMEM);
235 /* compute image size with reduction factor */
236 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
237 s->reduction_factor);
238 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
239 s->reduction_factor);
241 switch (s->ncomponents) {
243 if (s->precision > 8)
244 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
246 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
249 switch (s->avctx->profile) {
250 case FF_PROFILE_JPEG2000_DCINEMA_2K:
251 case FF_PROFILE_JPEG2000_DCINEMA_4K:
252 /* XYZ color-space for digital cinema profiles */
253 s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
256 if (s->precision > 8)
257 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
259 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
264 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
267 /* pixel format can not be identified */
268 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
274 /* get common part for COD and COC segments */
275 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
279 if (bytestream2_get_bytes_left(&s->g) < 5)
280 return AVERROR_INVALIDDATA;
282 /* nreslevels = number of resolution levels
283 = number of decomposition level +1 */
284 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
286 if (c->nreslevels > JPEG2000_MAX_RESLEVELS)
287 return AVERROR_INVALIDDATA;
289 /* compute number of resolution levels to decode */
290 if (c->nreslevels < s->reduction_factor)
291 c->nreslevels2decode = 1;
293 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
295 c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
296 c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
298 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
299 c->log2_cblk_width + c->log2_cblk_height > 12) {
300 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
301 return AVERROR_INVALIDDATA;
304 c->cblk_style = bytestream2_get_byteu(&s->g);
305 if (c->cblk_style != 0) { // cblk style
306 avpriv_request_sample(s->avctx, "Support for extra cblk styles");
307 return AVERROR_PATCHWELCOME;
309 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
310 /* set integer 9/7 DWT in case of BITEXACT flag */
311 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
312 c->transform = FF_DWT97_INT;
314 if (c->csty & JPEG2000_CSTY_PREC) {
316 for (i = 0; i < c->nreslevels; i++) {
317 byte = bytestream2_get_byte(&s->g);
318 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
319 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
322 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
323 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
328 /* get coding parameters for a particular tile or whole image*/
329 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
332 Jpeg2000CodingStyle tmp;
335 if (bytestream2_get_bytes_left(&s->g) < 5)
336 return AVERROR_INVALIDDATA;
338 tmp.csty = bytestream2_get_byteu(&s->g);
340 // get progression order
341 tmp.prog_order = bytestream2_get_byteu(&s->g);
343 tmp.nlayers = bytestream2_get_be16u(&s->g);
344 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
346 if (tmp.mct && s->ncomponents < 3) {
347 av_log(s->avctx, AV_LOG_ERROR,
348 "MCT %d with too few components (%d)\n",
349 tmp.mct, s->ncomponents);
350 return AVERROR_INVALIDDATA;
353 if ((ret = get_cox(s, &tmp)) < 0)
356 for (compno = 0; compno < s->ncomponents; compno++)
357 if (!(properties[compno] & HAD_COC))
358 memcpy(c + compno, &tmp, sizeof(tmp));
362 /* Get coding parameters for a component in the whole image or a
363 * particular tile. */
364 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
369 if (bytestream2_get_bytes_left(&s->g) < 2)
370 return AVERROR_INVALIDDATA;
372 compno = bytestream2_get_byteu(&s->g);
374 if (compno >= s->ncomponents) {
375 av_log(s->avctx, AV_LOG_ERROR,
376 "Invalid compno %d. There are %d components in the image.\n",
377 compno, s->ncomponents);
378 return AVERROR_INVALIDDATA;
382 c->csty = bytestream2_get_byteu(&s->g);
384 if ((ret = get_cox(s, c)) < 0)
387 properties[compno] |= HAD_COC;
391 /* Get common part for QCD and QCC segments. */
392 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
396 if (bytestream2_get_bytes_left(&s->g) < 1)
397 return AVERROR_INVALIDDATA;
399 x = bytestream2_get_byteu(&s->g); // Sqcd
401 q->nguardbits = x >> 5;
402 q->quantsty = x & 0x1f;
404 if (q->quantsty == JPEG2000_QSTY_NONE) {
406 if (bytestream2_get_bytes_left(&s->g) < n ||
407 n > JPEG2000_MAX_DECLEVELS)
408 return AVERROR_INVALIDDATA;
409 for (i = 0; i < n; i++)
410 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
411 } else if (q->quantsty == JPEG2000_QSTY_SI) {
412 if (bytestream2_get_bytes_left(&s->g) < 2)
413 return AVERROR_INVALIDDATA;
414 x = bytestream2_get_be16u(&s->g);
415 q->expn[0] = x >> 11;
416 q->mant[0] = x & 0x7ff;
417 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
418 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
419 q->expn[i] = curexpn;
420 q->mant[i] = q->mant[0];
424 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
425 n > JPEG2000_MAX_DECLEVELS)
426 return AVERROR_INVALIDDATA;
427 for (i = 0; i < n; i++) {
428 x = bytestream2_get_be16u(&s->g);
429 q->expn[i] = x >> 11;
430 q->mant[i] = x & 0x7ff;
436 /* Get quantization parameters for a particular tile or a whole image. */
437 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
440 Jpeg2000QuantStyle tmp;
443 if ((ret = get_qcx(s, n, &tmp)) < 0)
445 for (compno = 0; compno < s->ncomponents; compno++)
446 if (!(properties[compno] & HAD_QCC))
447 memcpy(q + compno, &tmp, sizeof(tmp));
451 /* Get quantization parameters for a component in the whole image
452 * on in a particular tile. */
453 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
458 if (bytestream2_get_bytes_left(&s->g) < 1)
459 return AVERROR_INVALIDDATA;
461 compno = bytestream2_get_byteu(&s->g);
463 if (compno >= s->ncomponents) {
464 av_log(s->avctx, AV_LOG_ERROR,
465 "Invalid compno %d. There are %d components in the image.\n",
466 compno, s->ncomponents);
467 return AVERROR_INVALIDDATA;
470 properties[compno] |= HAD_QCC;
471 return get_qcx(s, n - 1, q + compno);
474 /* Get start of tile segment. */
475 static int get_sot(Jpeg2000DecoderContext *s, int n)
477 Jpeg2000TilePart *tp;
482 if (bytestream2_get_bytes_left(&s->g) < 8)
483 return AVERROR_INVALIDDATA;
485 Isot = bytestream2_get_be16u(&s->g); // Isot
486 if (Isot >= s->numXtiles * s->numYtiles)
487 return AVERROR_INVALIDDATA;
490 avpriv_request_sample(s->avctx, "Support for more than one tile");
491 return AVERROR_PATCHWELCOME;
493 Psot = bytestream2_get_be32u(&s->g); // Psot
494 TPsot = bytestream2_get_byteu(&s->g); // TPsot
496 /* Read TNSot but not used */
497 bytestream2_get_byteu(&s->g); // TNsot
499 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
500 av_log(s->avctx, AV_LOG_ERROR, "Psot %d too big\n", Psot);
501 return AVERROR_INVALIDDATA;
504 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
505 avpriv_request_sample(s->avctx, "Support for %d components", TPsot);
506 return AVERROR_PATCHWELCOME;
509 s->tile[Isot].tp_idx = TPsot;
510 tp = s->tile[Isot].tile_part + TPsot;
511 tp->tile_index = Isot;
512 tp->tp_end = s->g.buffer + Psot - n - 2;
515 Jpeg2000Tile *tile = s->tile + s->curtileno;
518 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
519 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
525 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
526 * Used to know the number of tile parts and lengths.
527 * There may be multiple TLMs in the header.
528 * TODO: The function is not used for tile-parts management, nor anywhere else.
529 * It can be useful to allocate memory for tile parts, before managing the SOT
530 * markers. Parsing the TLM header is needed to increment the input header
532 * This marker is mandatory for DCI. */
533 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
535 uint8_t Stlm, ST, SP, tile_tlm, i;
536 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
537 Stlm = bytestream2_get_byte(&s->g);
539 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
540 ST = (Stlm >> 4) & 0x03;
541 // TODO: Manage case of ST = 0b11 --> raise error
542 SP = (Stlm >> 6) & 0x01;
543 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
544 for (i = 0; i < tile_tlm; i++) {
549 bytestream2_get_byte(&s->g);
552 bytestream2_get_be16(&s->g);
555 bytestream2_get_be32(&s->g);
559 bytestream2_get_be16(&s->g);
561 bytestream2_get_be32(&s->g);
567 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
570 int tilex = tileno % s->numXtiles;
571 int tiley = tileno / s->numXtiles;
572 Jpeg2000Tile *tile = s->tile + tileno;
575 return AVERROR(ENOMEM);
577 for (compno = 0; compno < s->ncomponents; compno++) {
578 Jpeg2000Component *comp = tile->comp + compno;
579 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
580 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
581 int ret; // global bandno
583 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
584 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
585 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
586 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
588 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
589 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
590 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
591 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
593 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
594 s->cbps[compno], s->cdx[compno],
595 s->cdy[compno], s->avctx))
601 /* Read the number of coding passes. */
602 static int getnpasses(Jpeg2000DecoderContext *s)
609 if ((num = get_bits(s, 2)) != 3)
610 return num < 0 ? num : 3 + num;
611 if ((num = get_bits(s, 5)) != 31)
612 return num < 0 ? num : 6 + num;
613 num = get_bits(s, 7);
614 return num < 0 ? num : 37 + num;
617 static int getlblockinc(Jpeg2000DecoderContext *s)
620 while (ret = get_bits(s, 1)) {
628 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
629 Jpeg2000CodingStyle *codsty,
630 Jpeg2000ResLevel *rlevel, int precno,
631 int layno, uint8_t *expn, int numgbits)
633 int bandno, cblkno, ret, nb_code_blocks;
635 if (!(ret = get_bits(s, 1))) {
641 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
642 Jpeg2000Band *band = rlevel->band + bandno;
643 Jpeg2000Prec *prec = band->prec + precno;
645 if (band->coord[0][0] == band->coord[0][1] ||
646 band->coord[1][0] == band->coord[1][1])
648 nb_code_blocks = prec->nb_codeblocks_height *
649 prec->nb_codeblocks_width;
650 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
651 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
652 int incl, newpasses, llen;
655 incl = get_bits(s, 1);
657 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
663 if (!cblk->npasses) {
664 int v = expn[bandno] + numgbits - 1 -
665 tag_tree_decode(s, prec->zerobits + cblkno, 100);
667 av_log(s->avctx, AV_LOG_ERROR,
668 "nonzerobits %d invalid\n", v);
669 return AVERROR_INVALIDDATA;
671 cblk->nonzerobits = v;
673 if ((newpasses = getnpasses(s)) < 0)
675 if ((llen = getlblockinc(s)) < 0)
677 cblk->lblock += llen;
678 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
680 if (ret > sizeof(cblk->data)) {
681 avpriv_request_sample(s->avctx,
682 "Block with lengthinc greater than %zu",
684 return AVERROR_PATCHWELCOME;
686 cblk->lengthinc = ret;
687 cblk->npasses += newpasses;
692 if (codsty->csty & JPEG2000_CSTY_EPH) {
693 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
694 bytestream2_skip(&s->g, 2);
696 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
699 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
700 Jpeg2000Band *band = rlevel->band + bandno;
701 Jpeg2000Prec *prec = band->prec + precno;
703 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
704 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
705 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
706 if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
707 return AVERROR_INVALIDDATA;
708 /* Code-block data can be empty. In that case initialize data
710 if (cblk->lengthinc > 0) {
711 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
713 cblk->data[0] = 0xFF;
714 cblk->data[1] = 0xFF;
716 cblk->length += cblk->lengthinc;
719 if (cblk->length > sizeof(cblk->data)) {
720 av_log(s->avctx, AV_LOG_ERROR,
721 "Block length %d > data size %zd\n",
722 cblk->length, sizeof(cblk->data));
723 return AVERROR_INVALIDDATA;
730 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
733 int layno, reslevelno, compno, precno, ok_reslevel;
737 switch (tile->codsty[0].prog_order) {
738 case JPEG2000_PGOD_LRCP:
739 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
741 for (reslevelno = 0; ok_reslevel; reslevelno++) {
743 for (compno = 0; compno < s->ncomponents; compno++) {
744 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
745 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
746 if (reslevelno < codsty->nreslevels) {
747 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
750 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
751 if ((ret = jpeg2000_decode_packet(s,
754 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
755 qntsty->nguardbits)) < 0)
763 case JPEG2000_PGOD_CPRL:
764 for (compno = 0; compno < s->ncomponents; compno++) {
765 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
766 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
768 /* Set bit stream buffer address according to tile-part.
769 * For DCinema one tile-part per component, so can be
770 * indexed by component. */
771 s->g = tile->tile_part[compno].tpg;
773 /* Position loop (y axis)
774 * TODO: Automate computing of step 256.
775 * Fixed here, but to be computed before entering here. */
776 for (y = 0; y < s->height; y += 256) {
777 /* Position loop (y axis)
778 * TODO: automate computing of step 256.
779 * Fixed here, but to be computed before entering here. */
780 for (x = 0; x < s->width; x += 256) {
781 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
783 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
784 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
786 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
787 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
790 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
791 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
794 // check if a precinct exists
795 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
796 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
797 precno = prcx + rlevel->num_precincts_x * prcy;
798 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
799 if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
801 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
802 qntsty->nguardbits)) < 0)
811 case JPEG2000_PGOD_RLCP:
812 avpriv_request_sample(s->avctx, "Progression order RLCP");
813 ret = AVERROR_PATCHWELCOME;
816 case JPEG2000_PGOD_RPCL:
817 avpriv_request_sample(s->avctx, "Progression order RPCL");
818 ret = AVERROR_PATCHWELCOME;
821 case JPEG2000_PGOD_PCRL:
822 avpriv_request_sample(s->avctx, "Progression order PCRL");
823 ret = AVERROR_PATCHWELCOME;
830 /* EOC marker reached */
831 bytestream2_skip(&s->g, 2);
836 /* TIER-1 routines */
837 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
838 int bpno, int bandno, int bpass_csty_symbol,
839 int vert_causal_ctx_csty_symbol)
841 int mask = 3 << (bpno - 1), y0, x, y;
843 for (y0 = 0; y0 < height; y0 += 4)
844 for (x = 0; x < width; x++)
845 for (y = y0; y < height && y < y0 + 4; y++) {
846 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
847 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
849 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
850 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
851 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
852 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
853 if (bpass_csty_symbol)
854 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
856 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
859 ff_jpeg2000_set_significance(t1, x, y,
862 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
867 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
873 phalf = 1 << (bpno - 1);
876 for (y0 = 0; y0 < height; y0 += 4)
877 for (x = 0; x < width; x++)
878 for (y = y0; y < height && y < y0 + 4; y++)
879 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
880 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
881 int r = ff_mqc_decode(&t1->mqc,
882 t1->mqc.cx_states + ctxno)
884 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
885 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
889 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
890 int width, int height, int bpno, int bandno,
891 int seg_symbols, int vert_causal_ctx_csty_symbol)
893 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
895 for (y0 = 0; y0 < height; y0 += 4) {
896 for (x = 0; x < width; x++) {
897 if (y0 + 3 < height &&
898 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
899 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
900 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
901 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
902 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
904 runlen = ff_mqc_decode(&t1->mqc,
905 t1->mqc.cx_states + MQC_CX_UNI);
906 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
915 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
917 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
919 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
920 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
921 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
927 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
929 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
930 t1->mqc.cx_states + ctxno) ^
933 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
936 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
942 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
943 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
944 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
945 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
947 av_log(s->avctx, AV_LOG_ERROR,
948 "Segmentation symbol value incorrect\n");
952 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
953 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
954 int width, int height, int bandpos)
956 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
958 int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
959 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
961 for (y = 0; y < height; y++)
962 memset(t1->data[y], 0, width * sizeof(**t1->data));
964 /* If code-block contains no compressed data: nothing to do. */
967 for (y = 0; y < height + 2; y++)
968 memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
970 ff_mqc_initdec(&t1->mqc, cblk->data);
971 cblk->data[cblk->length] = 0xff;
972 cblk->data[cblk->length + 1] = 0xff;
977 decode_sigpass(t1, width, height, bpno + 1, bandpos,
978 bpass_csty_symbol && (clnpass_cnt >= 4),
979 vert_causal_ctx_csty_symbol);
982 decode_refpass(t1, width, height, bpno + 1);
983 if (bpass_csty_symbol && clnpass_cnt >= 4)
984 ff_mqc_initdec(&t1->mqc, cblk->data);
987 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
988 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
989 vert_causal_ctx_csty_symbol);
990 clnpass_cnt = clnpass_cnt + 1;
991 if (bpass_csty_symbol && clnpass_cnt >= 4)
992 ff_mqc_initdec(&t1->mqc, cblk->data);
1005 /* TODO: Verify dequantization for lossless case
1006 * comp->data can be float or int
1007 * band->stepsize can be float or int
1008 * depending on the type of DWT transformation.
1009 * see ISO/IEC 15444-1:2002 A.6.1 */
1011 /* Float dequantization of a codeblock.*/
1012 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1013 Jpeg2000Component *comp,
1014 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1017 int w = cblk->coord[0][1] - cblk->coord[0][0];
1018 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1019 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1020 int *src = t1->data[j];
1021 for (i = 0; i < w; ++i)
1022 datap[i] = src[i] * band->f_stepsize;
1026 /* Integer dequantization of a codeblock.*/
1027 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1028 Jpeg2000Component *comp,
1029 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1032 int w = cblk->coord[0][1] - cblk->coord[0][0];
1033 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1034 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1035 int *src = t1->data[j];
1036 for (i = 0; i < w; ++i)
1037 datap[i] = (src[i] * band->i_stepsize + (1 << 15)) >> 16;
1041 /* Inverse ICT parameters in float and integer.
1042 * int value = (float value) * (1<<16) */
1043 static const float f_ict_params[4] = {
1049 static const int i_ict_params[4] = {
1056 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1059 int32_t *src[3], i0, i1, i2;
1060 float *srcf[3], i0f, i1f, i2f;
1062 for (i = 0; i < 3; i++)
1063 if (tile->codsty[0].transform == FF_DWT97)
1064 srcf[i] = tile->comp[i].f_data;
1066 src [i] = tile->comp[i].i_data;
1068 for (i = 0; i < 2; i++)
1069 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1070 switch (tile->codsty[0].transform) {
1072 for (i = 0; i < csize; i++) {
1073 i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
1074 i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
1075 - (f_ict_params[2] * *srcf[2]);
1076 i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
1083 for (i = 0; i < csize; i++) {
1084 i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1085 i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1086 - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
1087 i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1094 for (i = 0; i < csize; i++) {
1095 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1106 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1109 int compno, reslevelno, bandno;
1113 Jpeg2000T1Context t1;
1114 /* Loop on tile components */
1116 for (compno = 0; compno < s->ncomponents; compno++) {
1117 Jpeg2000Component *comp = tile->comp + compno;
1118 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1119 /* Loop on resolution levels */
1120 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1121 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1123 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1124 uint16_t nb_precincts, precno;
1125 Jpeg2000Band *band = rlevel->band + bandno;
1126 int cblkno = 0, bandpos;
1127 bandpos = bandno + (reslevelno > 0);
1129 if (band->coord[0][0] == band->coord[0][1] ||
1130 band->coord[1][0] == band->coord[1][1])
1133 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1134 /* Loop on precincts */
1135 for (precno = 0; precno < nb_precincts; precno++) {
1136 Jpeg2000Prec *prec = band->prec + precno;
1138 /* Loop on codeblocks */
1139 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1141 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1142 decode_cblk(s, codsty, &t1, cblk,
1143 cblk->coord[0][1] - cblk->coord[0][0],
1144 cblk->coord[1][1] - cblk->coord[1][0],
1147 x = cblk->coord[0][0];
1148 y = cblk->coord[1][0];
1150 if (codsty->transform == FF_DWT97)
1151 dequantization_float(x, y, cblk, comp, &t1, band);
1153 dequantization_int(x, y, cblk, comp, &t1, band);
1157 } /* end reslevel */
1160 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1163 /* inverse MCT transformation */
1164 if (tile->codsty[0].mct)
1165 mct_decode(s, tile);
1167 if (s->precision <= 8) {
1168 for (compno = 0; compno < s->ncomponents; compno++) {
1169 Jpeg2000Component *comp = tile->comp + compno;
1170 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1171 float *datap = comp->f_data;
1172 int32_t *i_datap = comp->i_data;
1173 int cbps = s->cbps[compno];
1174 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1176 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1177 line = picture->data[0] + y * picture->linesize[0];
1178 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1181 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1182 dst = line + x * s->ncomponents + compno;
1184 if (codsty->transform == FF_DWT97) {
1185 for (; x < w; x += s->cdx[compno]) {
1186 int val = lrintf(*datap) + (1 << (cbps - 1));
1187 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1188 val = av_clip(val, 0, (1 << cbps) - 1);
1189 *dst = val << (8 - cbps);
1191 dst += s->ncomponents;
1194 for (; x < w; x += s->cdx[compno]) {
1195 int val = *i_datap + (1 << (cbps - 1));
1196 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1197 val = av_clip(val, 0, (1 << cbps) - 1);
1198 *dst = val << (8 - cbps);
1200 dst += s->ncomponents;
1203 line += picture->linesize[0];
1207 for (compno = 0; compno < s->ncomponents; compno++) {
1208 Jpeg2000Component *comp = tile->comp + compno;
1209 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1210 float *datap = comp->f_data;
1211 int32_t *i_datap = comp->i_data;
1213 int cbps = s->cbps[compno];
1214 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1216 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1217 linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
1218 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1220 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1221 dst = linel + (x * s->ncomponents + compno);
1222 if (codsty->transform == FF_DWT97) {
1223 for (; x < w; x += s-> cdx[compno]) {
1224 int val = lrintf(*datap) + (1 << (cbps - 1));
1225 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1226 val = av_clip(val, 0, (1 << cbps) - 1);
1227 /* align 12 bit values in little-endian mode */
1228 *dst = val << (16 - cbps);
1230 dst += s->ncomponents;
1233 for (; x < w; x += s-> cdx[compno]) {
1234 int val = *i_datap + (1 << (cbps - 1));
1235 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1236 val = av_clip(val, 0, (1 << cbps) - 1);
1237 /* align 12 bit values in little-endian mode */
1238 *dst = val << (16 - cbps);
1240 dst += s->ncomponents;
1243 linel += picture->linesize[0] >> 1;
1251 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1254 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1255 for (compno = 0; compno < s->ncomponents; compno++) {
1256 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1257 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1259 ff_jpeg2000_cleanup(comp, codsty);
1261 av_freep(&s->tile[tileno].comp);
1264 s->numXtiles = s->numYtiles = 0;
1267 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1269 Jpeg2000CodingStyle *codsty = s->codsty;
1270 Jpeg2000QuantStyle *qntsty = s->qntsty;
1271 uint8_t *properties = s->properties;
1278 if (bytestream2_get_bytes_left(&s->g) < 2) {
1279 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1283 marker = bytestream2_get_be16u(&s->g);
1284 oldpos = bytestream2_tell(&s->g);
1286 if (marker == JPEG2000_SOD) {
1288 Jpeg2000TilePart *tp;
1290 if (s->curtileno < 0) {
1291 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1292 return AVERROR_INVALIDDATA;
1295 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1296 return AVERROR_INVALIDDATA;
1299 tile = s->tile + s->curtileno;
1300 tp = tile->tile_part + tile->tp_idx;
1301 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1302 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1306 if (marker == JPEG2000_EOC)
1309 len = bytestream2_get_be16u(&s->g);
1310 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1311 return AVERROR_INVALIDDATA;
1318 ret = get_coc(s, codsty, properties);
1321 ret = get_cod(s, codsty, properties);
1324 ret = get_qcc(s, len, qntsty, properties);
1327 ret = get_qcd(s, len, qntsty, properties);
1330 if (!(ret = get_sot(s, len))) {
1331 codsty = s->tile[s->curtileno].codsty;
1332 qntsty = s->tile[s->curtileno].qntsty;
1333 properties = s->tile[s->curtileno].properties;
1337 // the comment is ignored
1338 bytestream2_skip(&s->g, len - 2);
1341 // Tile-part lengths
1342 ret = get_tlm(s, len);
1345 av_log(s->avctx, AV_LOG_ERROR,
1346 "unsupported marker 0x%.4X at pos 0x%X\n",
1347 marker, bytestream2_tell(&s->g) - 4);
1348 bytestream2_skip(&s->g, len - 2);
1351 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1352 av_log(s->avctx, AV_LOG_ERROR,
1353 "error during processing marker segment %.4x\n", marker);
1354 return ret ? ret : -1;
1360 /* Read bit stream packets --> T2 operation. */
1361 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1366 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1367 Jpeg2000Tile *tile = s->tile + tileno;
1369 if (ret = init_tile(s, tileno))
1372 s->g = tile->tile_part[0].tpg;
1373 if (ret = jpeg2000_decode_packets(s, tile))
1380 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1382 uint32_t atom_size, atom;
1383 int found_codestream = 0, search_range = 10;
1385 while(!found_codestream && search_range
1387 bytestream2_get_bytes_left(&s->g) >= 8) {
1388 atom_size = bytestream2_get_be32u(&s->g);
1389 atom = bytestream2_get_be32u(&s->g);
1390 if (atom == JP2_CODESTREAM) {
1391 found_codestream = 1;
1393 if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1395 bytestream2_skipu(&s->g, atom_size - 8);
1400 if (found_codestream)
1405 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1406 int *got_frame, AVPacket *avpkt)
1408 Jpeg2000DecoderContext *s = avctx->priv_data;
1409 ThreadFrame frame = { .f = data };
1410 AVFrame *picture = data;
1414 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1415 s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1417 if (bytestream2_get_bytes_left(&s->g) < 2) {
1418 ret = AVERROR_INVALIDDATA;
1422 // check if the image is in jp2 format
1423 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1424 (bytestream2_get_be32u(&s->g) == 12) &&
1425 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1426 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1427 if (!jp2_find_codestream(s)) {
1428 av_log(avctx, AV_LOG_ERROR,
1429 "Could not find Jpeg2000 codestream atom.\n");
1430 ret = AVERROR_INVALIDDATA;
1434 bytestream2_seek(&s->g, 0, SEEK_SET);
1437 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1438 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1439 ret = AVERROR_INVALIDDATA;
1442 if (ret = jpeg2000_read_main_headers(s))
1445 /* get picture buffer */
1446 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
1447 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
1450 picture->pict_type = AV_PICTURE_TYPE_I;
1451 picture->key_frame = 1;
1453 if (ret = jpeg2000_read_bitstream_packets(s))
1455 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1456 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1459 jpeg2000_dec_cleanup(s);
1463 return bytestream2_tell(&s->g);
1466 jpeg2000_dec_cleanup(s);
1470 static void jpeg2000_init_static_data(AVCodec *codec)
1472 ff_jpeg2000_init_tier1_luts();
1473 ff_mqc_init_context_tables();
1476 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1477 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1479 static const AVOption options[] = {
1480 { "lowres", "Lower the decoding resolution by a power of two",
1481 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1485 static const AVProfile profiles[] = {
1486 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1487 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1488 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1489 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1490 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1491 { FF_PROFILE_UNKNOWN },
1494 static const AVClass class = {
1495 .class_name = "jpeg2000",
1496 .item_name = av_default_item_name,
1498 .version = LIBAVUTIL_VERSION_INT,
1501 AVCodec ff_jpeg2000_decoder = {
1503 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1504 .type = AVMEDIA_TYPE_VIDEO,
1505 .id = AV_CODEC_ID_JPEG2000,
1506 .capabilities = CODEC_CAP_FRAME_THREADS,
1507 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1508 .init_static_data = jpeg2000_init_static_data,
1509 .decode = jpeg2000_decode_frame,
1510 .priv_class = &class,
1511 .profiles = NULL_IF_CONFIG_SMALL(profiles)