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 uint16_t tp_idx
; // Tile-part index
45 uint8_t tile_index
; // Tile index who refers the tile-part
46 uint32_t tp_len
; // Length of tile-part
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
[3];
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 int numXtiles
, numYtiles
;
78 Jpeg2000CodingStyle codsty
[4];
79 Jpeg2000QuantStyle qntsty
[4];
86 /*options parameters*/
88 int16_t reduction_factor
;
89 } Jpeg2000DecoderContext
;
91 /* get_bits functions for JPEG2000 packet bitstream
92 * It is a get_bit function with a bit-stuffing routine. If the value of the
93 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
94 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
95 static int get_bits(Jpeg2000DecoderContext
*s
, int n
)
100 if (s
->bit_index
== 0) {
101 s
->bit_index
= 7 + (bytestream2_get_byte(&s
->g
) != 0xFFu
);
104 res
|= (bytestream2_peek_byte(&s
->g
) >> s
->bit_index
) & 1;
109 static void jpeg2000_flush(Jpeg2000DecoderContext
*s
)
111 if (bytestream2_get_byte(&s
->g
) == 0xff)
112 bytestream2_skip(&s
->g
, 1);
116 /* decode the value stored in node */
117 static int tag_tree_decode(Jpeg2000DecoderContext
*s
, Jpeg2000TgtNode
*node
,
120 Jpeg2000TgtNode
*stack
[30];
121 int sp
= -1, curval
= 0;
123 while (node
&& !node
->vis
) {
131 curval
= stack
[sp
]->val
;
133 while (curval
< threshold
&& sp
>= 0) {
134 if (curval
< stack
[sp
]->val
)
135 curval
= stack
[sp
]->val
;
136 while (curval
< threshold
) {
138 if ((ret
= get_bits(s
, 1)) > 0) {
146 stack
[sp
]->val
= curval
;
152 /* marker segments */
153 /* get sizes and offsets of image, tiles; number of components */
154 static int get_siz(Jpeg2000DecoderContext
*s
)
158 if (bytestream2_get_bytes_left(&s
->g
) < 36)
159 return AVERROR_INVALIDDATA
;
161 s
->avctx
->profile
= bytestream2_get_be16u(&s
->g
); // Rsiz
162 s
->width
= bytestream2_get_be32u(&s
->g
); // Width
163 s
->height
= bytestream2_get_be32u(&s
->g
); // Height
164 s
->image_offset_x
= bytestream2_get_be32u(&s
->g
); // X0Siz
165 s
->image_offset_y
= bytestream2_get_be32u(&s
->g
); // Y0Siz
166 s
->tile_width
= bytestream2_get_be32u(&s
->g
); // XTSiz
167 s
->tile_height
= bytestream2_get_be32u(&s
->g
); // YTSiz
168 s
->tile_offset_x
= bytestream2_get_be32u(&s
->g
); // XT0Siz
169 s
->tile_offset_y
= bytestream2_get_be32u(&s
->g
); // YT0Siz
170 s
->ncomponents
= bytestream2_get_be16u(&s
->g
); // CSiz
172 if (bytestream2_get_bytes_left(&s
->g
) < 3 * s
->ncomponents
)
173 return AVERROR_INVALIDDATA
;
175 for (i
= 0; i
< s
->ncomponents
; i
++) { // Ssiz_i XRsiz_i, YRsiz_i
176 uint8_t x
= bytestream2_get_byteu(&s
->g
);
177 s
->cbps
[i
] = (x
& 0x7f) + 1;
178 s
->precision
= FFMAX(s
->cbps
[i
], s
->precision
);
179 s
->sgnd
[i
] = (x
& 0x80) == 1;
180 s
->cdx
[i
] = bytestream2_get_byteu(&s
->g
);
181 s
->cdy
[i
] = bytestream2_get_byteu(&s
->g
);
184 s
->numXtiles
= ff_jpeg2000_ceildiv(s
->width
- s
->tile_offset_x
, s
->tile_width
);
185 s
->numYtiles
= ff_jpeg2000_ceildiv(s
->height
- s
->tile_offset_y
, s
->tile_height
);
187 s
->tile
= av_mallocz(s
->numXtiles
* s
->numYtiles
* sizeof(*s
->tile
));
189 return AVERROR(ENOMEM
);
191 for (i
= 0; i
< s
->numXtiles
* s
->numYtiles
; i
++) {
192 Jpeg2000Tile
*tile
= s
->tile
+ i
;
194 tile
->comp
= av_mallocz(s
->ncomponents
* sizeof(*tile
->comp
));
196 return AVERROR(ENOMEM
);
199 /* compute image size with reduction factor */
200 s
->avctx
->width
= ff_jpeg2000_ceildivpow2(s
->width
- s
->image_offset_x
,
201 s
->reduction_factor
);
202 s
->avctx
->height
= ff_jpeg2000_ceildivpow2(s
->height
- s
->image_offset_y
,
203 s
->reduction_factor
);
205 switch (s
->avctx
->profile
) {
206 case FF_PROFILE_JPEG2000_DCINEMA_2K
:
207 case FF_PROFILE_JPEG2000_DCINEMA_4K
:
208 /* XYZ color-space for digital cinema profiles */
209 s
->avctx
->pix_fmt
= AV_PIX_FMT_XYZ12
;
212 /* For other profiles selects color-space according number of
213 * components and bit depth precision. */
214 switch (s
->ncomponents
) {
216 if (s
->precision
> 8)
217 s
->avctx
->pix_fmt
= AV_PIX_FMT_GRAY16
;
219 s
->avctx
->pix_fmt
= AV_PIX_FMT_GRAY8
;
222 if (s
->precision
> 8)
223 s
->avctx
->pix_fmt
= AV_PIX_FMT_RGB48
;
225 s
->avctx
->pix_fmt
= AV_PIX_FMT_RGB24
;
228 s
->avctx
->pix_fmt
= AV_PIX_FMT_BGRA
;
231 /* pixel format can not be identified */
232 s
->avctx
->pix_fmt
= AV_PIX_FMT_NONE
;
240 /* get common part for COD and COC segments */
241 static int get_cox(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*c
)
245 if (bytestream2_get_bytes_left(&s
->g
) < 5)
246 return AVERROR_INVALIDDATA
;
248 /* nreslevels = number of resolution levels
249 = number of decomposition level +1 */
250 c
->nreslevels
= bytestream2_get_byteu(&s
->g
) + 1;
252 if (c
->nreslevels
> JPEG2000_MAX_RESLEVELS
)
253 return AVERROR_INVALIDDATA
;
255 /* compute number of resolution levels to decode */
256 if (c
->nreslevels
< s
->reduction_factor
)
257 c
->nreslevels2decode
= 1;
259 c
->nreslevels2decode
= c
->nreslevels
- s
->reduction_factor
;
261 c
->log2_cblk_width
= bytestream2_get_byteu(&s
->g
) + 2; // cblk width
262 c
->log2_cblk_height
= bytestream2_get_byteu(&s
->g
) + 2; // cblk height
264 if (c
->log2_cblk_width
> 10 || c
->log2_cblk_height
> 10 ||
265 c
->log2_cblk_width
+ c
->log2_cblk_height
> 12) {
266 av_log(s
->avctx
, AV_LOG_ERROR
, "cblk size invalid\n");
267 return AVERROR_INVALIDDATA
;
270 c
->cblk_style
= bytestream2_get_byteu(&s
->g
);
271 if (c
->cblk_style
!= 0) { // cblk style
272 avpriv_request_sample(s
->avctx
, "Support for extra cblk styles");
273 return AVERROR_PATCHWELCOME
;
275 c
->transform
= bytestream2_get_byteu(&s
->g
); // DWT transformation type
276 /* set integer 9/7 DWT in case of BITEXACT flag */
277 if ((s
->avctx
->flags
& CODEC_FLAG_BITEXACT
) && (c
->transform
== FF_DWT97
))
278 c
->transform
= FF_DWT97_INT
;
280 if (c
->csty
& JPEG2000_CSTY_PREC
) {
282 for (i
= 0; i
< c
->nreslevels
; i
++) {
283 byte
= bytestream2_get_byte(&s
->g
);
284 c
->log2_prec_widths
[i
] = byte
& 0x0F; // precinct PPx
285 c
->log2_prec_heights
[i
] = (byte
>> 4) & 0x0F; // precinct PPy
291 /* get coding parameters for a particular tile or whole image*/
292 static int get_cod(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*c
,
295 Jpeg2000CodingStyle tmp
;
298 if (bytestream2_get_bytes_left(&s
->g
) < 5)
299 return AVERROR_INVALIDDATA
;
301 tmp
.log2_prec_width
=
302 tmp
.log2_prec_height
= 15;
304 tmp
.csty
= bytestream2_get_byteu(&s
->g
);
306 // get progression order
307 tmp
.prog_order
= bytestream2_get_byteu(&s
->g
);
309 tmp
.nlayers
= bytestream2_get_be16u(&s
->g
);
310 tmp
.mct
= bytestream2_get_byteu(&s
->g
); // multiple component transformation
313 for (compno
= 0; compno
< s
->ncomponents
; compno
++)
314 if (!(properties
[compno
] & HAD_COC
))
315 memcpy(c
+ compno
, &tmp
, sizeof(tmp
));
319 /* Get coding parameters for a component in the whole image or a
320 * particular tile. */
321 static int get_coc(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*c
,
326 if (bytestream2_get_bytes_left(&s
->g
) < 2)
327 return AVERROR_INVALIDDATA
;
329 compno
= bytestream2_get_byteu(&s
->g
);
332 c
->csty
= bytestream2_get_byteu(&s
->g
);
335 properties
[compno
] |= HAD_COC
;
339 /* Get common part for QCD and QCC segments. */
340 static int get_qcx(Jpeg2000DecoderContext
*s
, int n
, Jpeg2000QuantStyle
*q
)
344 if (bytestream2_get_bytes_left(&s
->g
) < 1)
345 return AVERROR_INVALIDDATA
;
347 x
= bytestream2_get_byteu(&s
->g
); // Sqcd
349 q
->nguardbits
= x
>> 5;
350 q
->quantsty
= x
& 0x1f;
352 if (q
->quantsty
== JPEG2000_QSTY_NONE
) {
354 if (bytestream2_get_bytes_left(&s
->g
) < n
||
355 n
> JPEG2000_MAX_DECLEVELS
)
356 return AVERROR_INVALIDDATA
;
357 for (i
= 0; i
< n
; i
++)
358 q
->expn
[i
] = bytestream2_get_byteu(&s
->g
) >> 3;
359 } else if (q
->quantsty
== JPEG2000_QSTY_SI
) {
360 if (bytestream2_get_bytes_left(&s
->g
) < 2)
361 return AVERROR_INVALIDDATA
;
362 x
= bytestream2_get_be16u(&s
->g
);
363 q
->expn
[0] = x
>> 11;
364 q
->mant
[0] = x
& 0x7ff;
365 for (i
= 1; i
< JPEG2000_MAX_DECLEVELS
* 3; i
++) {
366 int curexpn
= FFMAX(0, q
->expn
[0] - (i
- 1) / 3);
367 q
->expn
[i
] = curexpn
;
368 q
->mant
[i
] = q
->mant
[0];
372 if (bytestream2_get_bytes_left(&s
->g
) < 2 * n
||
373 n
> JPEG2000_MAX_DECLEVELS
)
374 return AVERROR_INVALIDDATA
;
375 for (i
= 0; i
< n
; i
++) {
376 x
= bytestream2_get_be16u(&s
->g
);
377 q
->expn
[i
] = x
>> 11;
378 q
->mant
[i
] = x
& 0x7ff;
384 /* Get quantization parameters for a particular tile or a whole image. */
385 static int get_qcd(Jpeg2000DecoderContext
*s
, int n
, Jpeg2000QuantStyle
*q
,
388 Jpeg2000QuantStyle tmp
;
391 if ((ret
= get_qcx(s
, n
, &tmp
)) < 0)
393 for (compno
= 0; compno
< s
->ncomponents
; compno
++)
394 if (!(properties
[compno
] & HAD_QCC
))
395 memcpy(q
+ compno
, &tmp
, sizeof(tmp
));
399 /* Get quantization parameters for a component in the whole image
400 * on in a particular tile. */
401 static int get_qcc(Jpeg2000DecoderContext
*s
, int n
, Jpeg2000QuantStyle
*q
,
406 if (bytestream2_get_bytes_left(&s
->g
) < 1)
407 return AVERROR_INVALIDDATA
;
409 compno
= bytestream2_get_byteu(&s
->g
);
410 properties
[compno
] |= HAD_QCC
;
411 return get_qcx(s
, n
- 1, q
+ compno
);
414 /* Get start of tile segment. */
415 static int get_sot(Jpeg2000DecoderContext
*s
, int n
)
417 Jpeg2000TilePart
*tp
;
422 if (bytestream2_get_bytes_left(&s
->g
) < 8)
423 return AVERROR_INVALIDDATA
;
425 Isot
= bytestream2_get_be16u(&s
->g
); // Isot
427 avpriv_request_sample(s
->avctx
, "Support for more than one tile");
428 return AVERROR_PATCHWELCOME
;
430 Psot
= bytestream2_get_be32u(&s
->g
); // Psot
431 TPsot
= bytestream2_get_byteu(&s
->g
); // TPsot
433 /* Read TNSot but not used */
434 bytestream2_get_byteu(&s
->g
); // TNsot
436 tp
= s
->tile
[s
->curtileno
].tile_part
+ TPsot
;
437 tp
->tile_index
= Isot
;
441 /* Start of bit stream. Pointer to SOD marker
442 * Check SOD marker is present. */
443 if (JPEG2000_SOD
== bytestream2_get_be16(&s
->g
)) {
444 bytestream2_init(&tp
->tpg
, s
->g
.buffer
, tp
->tp_len
- n
- 4);
445 bytestream2_skip(&s
->g
, tp
->tp_len
- n
- 4);
447 av_log(s
->avctx
, AV_LOG_ERROR
, "SOD marker not found \n");
448 return AVERROR_INVALIDDATA
;
451 /* End address of bit stream =
452 * start address + (Psot - size of SOT HEADER(n)
453 * - size of SOT MARKER(2) - size of SOD marker(2) */
458 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
459 * Used to know the number of tile parts and lengths.
460 * There may be multiple TLMs in the header.
461 * TODO: The function is not used for tile-parts management, nor anywhere else.
462 * It can be useful to allocate memory for tile parts, before managing the SOT
463 * markers. Parsing the TLM header is needed to increment the input header
465 * This marker is mandatory for DCI. */
466 static uint8_t get_tlm(Jpeg2000DecoderContext
*s
, int n
)
468 uint8_t Stlm
, ST
, SP
, tile_tlm
, i
;
469 bytestream2_get_byte(&s
->g
); /* Ztlm: skipped */
470 Stlm
= bytestream2_get_byte(&s
->g
);
472 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
473 ST
= (Stlm
>> 4) & 0x03;
474 // TODO: Manage case of ST = 0b11 --> raise error
475 SP
= (Stlm
>> 6) & 0x01;
476 tile_tlm
= (n
- 4) / ((SP
+ 1) * 2 + ST
);
477 for (i
= 0; i
< tile_tlm
; i
++) {
482 bytestream2_get_byte(&s
->g
);
485 bytestream2_get_be16(&s
->g
);
488 bytestream2_get_be32(&s
->g
);
492 bytestream2_get_be16(&s
->g
);
494 bytestream2_get_be32(&s
->g
);
500 static int init_tile(Jpeg2000DecoderContext
*s
, int tileno
)
503 int tilex
= tileno
% s
->numXtiles
;
504 int tiley
= tileno
/ s
->numXtiles
;
505 Jpeg2000Tile
*tile
= s
->tile
+ tileno
;
506 Jpeg2000CodingStyle
*codsty
;
507 Jpeg2000QuantStyle
*qntsty
;
510 return AVERROR(ENOMEM
);
512 /* copy codsty, qnsty to tile. TODO: Is it the best way?
513 * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle
514 * and Jpeg2000QuantStyle */
515 memcpy(tile
->codsty
, s
->codsty
, s
->ncomponents
* sizeof(*codsty
));
516 memcpy(tile
->qntsty
, s
->qntsty
, s
->ncomponents
* sizeof(*qntsty
));
518 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
519 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
520 int ret
; // global bandno
521 codsty
= tile
->codsty
+ compno
;
522 qntsty
= tile
->qntsty
+ compno
;
524 comp
->coord_o
[0][0] = FFMAX(tilex
* s
->tile_width
+ s
->tile_offset_x
, s
->image_offset_x
);
525 comp
->coord_o
[0][1] = FFMIN((tilex
+ 1) * s
->tile_width
+ s
->tile_offset_x
, s
->width
);
526 comp
->coord_o
[1][0] = FFMAX(tiley
* s
->tile_height
+ s
->tile_offset_y
, s
->image_offset_y
);
527 comp
->coord_o
[1][1] = FFMIN((tiley
+ 1) * s
->tile_height
+ s
->tile_offset_y
, s
->height
);
529 // FIXME: add a dcinema profile check ?
530 // value is guaranteed by profile (orig=0, 1 tile)
531 comp
->coord
[0][0] = 0;
532 comp
->coord
[0][1] = s
->avctx
->width
;
533 comp
->coord
[1][0] = 0;
534 comp
->coord
[1][1] = s
->avctx
->height
;
536 if (ret
= ff_jpeg2000_init_component(comp
, codsty
, qntsty
,
537 s
->cbps
[compno
], s
->cdx
[compno
],
538 s
->cdy
[compno
], s
->avctx
))
544 /* Read the number of coding passes. */
545 static int getnpasses(Jpeg2000DecoderContext
*s
)
552 if ((num
= get_bits(s
, 2)) != 3)
553 return num
< 0 ? num
: 3 + num
;
554 if ((num
= get_bits(s
, 5)) != 31)
555 return num
< 0 ? num
: 6 + num
;
556 num
= get_bits(s
, 7);
557 return num
< 0 ? num
: 37 + num
;
560 static int getlblockinc(Jpeg2000DecoderContext
*s
)
563 while (ret
= get_bits(s
, 1)) {
571 static int jpeg2000_decode_packet(Jpeg2000DecoderContext
*s
,
572 Jpeg2000CodingStyle
*codsty
,
573 Jpeg2000ResLevel
*rlevel
, int precno
,
574 int layno
, uint8_t *expn
, int numgbits
)
576 int bandno
, cblkno
, ret
, nb_code_blocks
;
578 if (!(ret
= get_bits(s
, 1))) {
584 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
585 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
586 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
588 if (band
->coord
[0][0] == band
->coord
[0][1] ||
589 band
->coord
[1][0] == band
->coord
[1][1])
593 nb_code_blocks
= prec
->nb_codeblocks_height
*
594 prec
->nb_codeblocks_width
;
595 for (cblkno
= 0; cblkno
< nb_code_blocks
; cblkno
++) {
596 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
597 int incl
, newpasses
, llen
;
600 incl
= get_bits(s
, 1);
602 incl
= tag_tree_decode(s
, prec
->cblkincl
+ cblkno
, layno
+ 1) == layno
;
609 cblk
->nonzerobits
= expn
[bandno
] + numgbits
- 1 -
610 tag_tree_decode(s
, prec
->zerobits
+ cblkno
,
612 if ((newpasses
= getnpasses(s
)) < 0)
614 if ((llen
= getlblockinc(s
)) < 0)
616 cblk
->lblock
+= llen
;
617 if ((ret
= get_bits(s
, av_log2(newpasses
) + cblk
->lblock
)) < 0)
619 cblk
->lengthinc
= ret
;
620 cblk
->npasses
+= newpasses
;
625 if (codsty
->csty
& JPEG2000_CSTY_EPH
) {
626 if (bytestream2_peek_be16(&s
->g
) == JPEG2000_EPH
)
627 bytestream2_skip(&s
->g
, 2);
629 av_log(s
->avctx
, AV_LOG_ERROR
, "EPH marker not found.\n");
632 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
633 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
634 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
636 nb_code_blocks
= prec
->nb_codeblocks_height
* prec
->nb_codeblocks_width
;
637 for (cblkno
= 0; cblkno
< nb_code_blocks
; cblkno
++) {
638 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
639 if (bytestream2_get_bytes_left(&s
->g
) < cblk
->lengthinc
)
640 return AVERROR_INVALIDDATA
;
641 /* Code-block data can be empty. In that case initialize data
643 if (cblk
->lengthinc
> 0) {
644 bytestream2_get_bufferu(&s
->g
, cblk
->data
, cblk
->lengthinc
);
646 cblk
->data
[0] = 0xFF;
647 cblk
->data
[1] = 0xFF;
649 cblk
->length
+= cblk
->lengthinc
;
656 static int jpeg2000_decode_packets(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
)
658 int layno
, reslevelno
, compno
, precno
, ok_reslevel
, ret
;
659 uint8_t prog_order
= tile
->codsty
[0].prog_order
;
664 switch (prog_order
) {
665 case JPEG2000_PGOD_LRCP
:
666 for (layno
= 0; layno
< tile
->codsty
[0].nlayers
; layno
++) {
668 for (reslevelno
= 0; ok_reslevel
; reslevelno
++) {
670 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
671 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
672 Jpeg2000QuantStyle
*qntsty
= tile
->qntsty
+ compno
;
673 if (reslevelno
< codsty
->nreslevels
) {
674 Jpeg2000ResLevel
*rlevel
= tile
->comp
[compno
].reslevel
+
677 for (precno
= 0; precno
< rlevel
->num_precincts_x
* rlevel
->num_precincts_y
; precno
++)
678 if ((ret
= jpeg2000_decode_packet(s
,
681 qntsty
->expn
+ (reslevelno ?
3 * (reslevelno
- 1) + 1 : 0),
682 qntsty
->nguardbits
)) < 0)
690 case JPEG2000_PGOD_CPRL
:
691 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
692 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
693 Jpeg2000QuantStyle
*qntsty
= tile
->qntsty
+ compno
;
695 /* Set bit stream buffer address according to tile-part.
696 * For DCinema one tile-part per component, so can be
697 * indexed by component. */
698 s
->g
= tile
->tile_part
[compno
].tpg
;
700 /* Position loop (y axis)
701 * TODO: Automate computing of step 256.
702 * Fixed here, but to be computed before entering here. */
703 for (y
= 0; y
< s
->height
; y
+= 256) {
704 /* Position loop (y axis)
705 * TODO: automate computing of step 256.
706 * Fixed here, but to be computed before entering here. */
707 for (x
= 0; x
< s
->width
; x
+= 256) {
708 for (reslevelno
= 0; reslevelno
< codsty
->nreslevels
; reslevelno
++) {
710 uint8_t reducedresno
= codsty
->nreslevels
- 1 -reslevelno
; // ==> N_L - r
711 Jpeg2000ResLevel
*rlevel
= tile
->comp
[compno
].reslevel
+ reslevelno
;
713 if (!((y
% (1 << (rlevel
->log2_prec_height
+ reducedresno
)) == 0) ||
714 (y
== 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
717 if (!((x
% (1 << (rlevel
->log2_prec_width
+ reducedresno
)) == 0) ||
718 (x
== 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
721 // check if a precinct exists
722 prcx
= ff_jpeg2000_ceildivpow2(x
, reducedresno
) >> rlevel
->log2_prec_width
;
723 prcy
= ff_jpeg2000_ceildivpow2(y
, reducedresno
) >> rlevel
->log2_prec_height
;
724 precno
= prcx
+ rlevel
->num_precincts_x
* prcy
;
725 for (layno
= 0; layno
< tile
->codsty
[0].nlayers
; layno
++) {
726 if ((ret
= jpeg2000_decode_packet(s
, codsty
, rlevel
,
728 qntsty
->expn
+ (reslevelno ?
3 * (reslevelno
- 1) + 1 : 0),
729 qntsty
->nguardbits
)) < 0)
742 /* EOC marker reached */
743 bytestream2_skip(&s
->g
, 2);
748 /* TIER-1 routines */
749 static void decode_sigpass(Jpeg2000T1Context
*t1
, int width
, int height
,
750 int bpno
, int bandno
)
752 int mask
= 3 << (bpno
- 1), y0
, x
, y
;
754 for (y0
= 0; y0
< height
; y0
+= 4)
755 for (x
= 0; x
< width
; x
++)
756 for (y
= y0
; y
< height
&& y
< y0
+ 4; y
++)
757 if ((t1
->flags
[y
+ 1][x
+ 1] & JPEG2000_T1_SIG_NB
)
758 && !(t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
))) {
759 if (ff_mqc_decode(&t1
->mqc
,
761 ff_jpeg2000_getsigctxno(t1
->flags
[y
+ 1][x
+ 1],
763 int xorbit
, ctxno
= ff_jpeg2000_getsgnctxno(t1
->flags
[y
+ 1][x
+ 1],
767 (ff_mqc_decode(&t1
->mqc
,
768 t1
->mqc
.cx_states
+ ctxno
) ^ xorbit
)
771 ff_jpeg2000_set_significance(t1
, x
, y
,
774 t1
->flags
[y
+ 1][x
+ 1] |= JPEG2000_T1_VIS
;
778 static void decode_refpass(Jpeg2000T1Context
*t1
, int width
, int height
,
784 phalf
= 1 << (bpno
- 1);
787 for (y0
= 0; y0
< height
; y0
+= 4)
788 for (x
= 0; x
< width
; x
++)
789 for (y
= y0
; y
< height
&& y
< y0
+ 4; y
++)
790 if ((t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
)) == JPEG2000_T1_SIG
) {
791 int ctxno
= ff_jpeg2000_getrefctxno(t1
->flags
[y
+ 1][x
+ 1]);
792 int r
= ff_mqc_decode(&t1
->mqc
,
793 t1
->mqc
.cx_states
+ ctxno
)
795 t1
->data
[y
][x
] += t1
->data
[y
][x
] < 0 ?
-r
: r
;
796 t1
->flags
[y
+ 1][x
+ 1] |= JPEG2000_T1_REF
;
800 static void decode_clnpass(Jpeg2000DecoderContext
*s
, Jpeg2000T1Context
*t1
,
801 int width
, int height
, int bpno
, int bandno
,
804 int mask
= 3 << (bpno
- 1), y0
, x
, y
, runlen
, dec
;
806 for (y0
= 0; y0
< height
; y0
+= 4)
807 for (x
= 0; x
< width
; x
++) {
808 if (y0
+ 3 < height
&&
809 !((t1
->flags
[y0
+ 1][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
810 (t1
->flags
[y0
+ 2][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
811 (t1
->flags
[y0
+ 3][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
812 (t1
->flags
[y0
+ 4][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)))) {
813 if (!ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_RL
))
815 runlen
= ff_mqc_decode(&t1
->mqc
,
816 t1
->mqc
.cx_states
+ MQC_CX_UNI
);
817 runlen
= (runlen
<< 1) | ff_mqc_decode(&t1
->mqc
,
826 for (y
= y0
+ runlen
; y
< y0
+ 4 && y
< height
; y
++) {
828 if (!(t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
)))
829 dec
= ff_mqc_decode(&t1
->mqc
,
831 ff_jpeg2000_getsigctxno(t1
->flags
[y
+ 1][x
+ 1],
836 int ctxno
= ff_jpeg2000_getsgnctxno(t1
->flags
[y
+ 1][x
+ 1],
838 t1
->data
[y
][x
] = (ff_mqc_decode(&t1
->mqc
,
839 t1
->mqc
.cx_states
+ ctxno
) ^
842 ff_jpeg2000_set_significance(t1
, x
, y
, t1
->data
[y
][x
] < 0);
845 t1
->flags
[y
+ 1][x
+ 1] &= ~JPEG2000_T1_VIS
;
850 val
= ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
851 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
852 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
853 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
855 av_log(s
->avctx
, AV_LOG_ERROR
,
856 "Segmentation symbol value incorrect\n");
860 static int decode_cblk(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*codsty
,
861 Jpeg2000T1Context
*t1
, Jpeg2000Cblk
*cblk
,
862 int width
, int height
, int bandpos
)
864 int passno
= cblk
->npasses
, pass_t
= 2, bpno
= cblk
->nonzerobits
- 1, y
;
866 for (y
= 0; y
< height
; y
++)
867 memset(t1
->data
[y
], 0, width
* sizeof(width
));
868 /* If code-block contains no compressed data: nothing to do. */
871 for (y
= 0; y
< height
+ 2; y
++)
872 memset(t1
->flags
[y
], 0, (width
+ 2) * sizeof(width
));
874 ff_mqc_initdec(&t1
->mqc
, cblk
->data
);
875 cblk
->data
[cblk
->length
] = 0xff;
876 cblk
->data
[cblk
->length
+ 1] = 0xff;
881 decode_sigpass(t1
, width
, height
, bpno
+ 1, bandpos
);
884 decode_refpass(t1
, width
, height
, bpno
+ 1);
887 decode_clnpass(s
, t1
, width
, height
, bpno
+ 1, bandpos
,
888 codsty
->cblk_style
& JPEG2000_CBLK_SEGSYM
);
901 /* TODO: Verify dequantization for lossless case
902 * comp->data can be float or int
903 * band->stepsize can be float or int
904 * depending on the type of DWT transformation.
905 * see ISO/IEC 15444-1:2002 A.6.1 */
907 /* Float dequantization of a codeblock.*/
908 static void dequantization_float(int x
, int y
, Jpeg2000Cblk
*cblk
,
909 Jpeg2000Component
*comp
,
910 Jpeg2000T1Context
*t1
, Jpeg2000Band
*band
)
913 float *datap
= &comp
->data
[(comp
->coord
[0][1] - comp
->coord
[0][0]) * y
+ x
];
914 for (j
= 0; j
< (cblk
->coord
[1][1] - cblk
->coord
[1][0]); ++j
)
915 for (i
= 0; i
< (cblk
->coord
[0][1] - cblk
->coord
[0][0]); ++i
) {
916 idx
= (comp
->coord
[0][1] - comp
->coord
[0][0]) * j
+ i
;
917 datap
[idx
] = (float)(t1
->data
[j
][i
]) * ((float)band
->stepsize
);
922 /* Integer dequantization of a codeblock.*/
923 static void dequantization_int(int x
, int y
, Jpeg2000Cblk
*cblk
,
924 Jpeg2000Component
*comp
,
925 Jpeg2000T1Context
*t1
, Jpeg2000Band
*band
)
929 (int32_t *) &comp
->data
[(comp
->coord
[0][1] - comp
->coord
[0][0]) * y
+ x
];
930 for (j
= 0; j
< (cblk
->coord
[1][1] - cblk
->coord
[1][0]); ++j
)
931 for (i
= 0; i
< (cblk
->coord
[0][1] - cblk
->coord
[0][0]); ++i
) {
932 idx
= (comp
->coord
[0][1] - comp
->coord
[0][0]) * j
+ i
;
934 ((int32_t)(t1
->data
[j
][i
]) * ((int32_t)band
->stepsize
) + (1 << 15)) >> 16;
939 /* Inverse ICT parameters in float and integer.
940 * int value = (float value) * (1<<16) */
941 static const float f_ict_params
[4] = {
947 static const int i_ict_params
[4] = {
954 static int mct_decode(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
)
958 int32_t *src
[3], i0
, i1
, i2
;
959 float *srcf
[3], i0f
, i1f
, i2f
;
961 for (i
= 0; i
< 3; i
++)
962 if (tile
->codsty
[0].transform
== FF_DWT97
)
963 srcf
[i
] = tile
->comp
[i
].data
;
965 src
[i
] = (int32_t *)tile
->comp
[i
].data
;
967 for (i
= 0; i
< 2; i
++)
968 csize
*= tile
->comp
[0].coord
[i
][1] - tile
->comp
[0].coord
[i
][0];
969 switch (tile
->codsty
[0].transform
) {
971 for (i
= 0; i
< csize
; i
++) {
972 i0f
= *srcf
[0] + (f_ict_params
[0] * *srcf
[2]);
973 i1f
= *srcf
[0] - (f_ict_params
[1] * *srcf
[1])
974 - (f_ict_params
[2] * *srcf
[2]);
975 i2f
= *srcf
[0] + (f_ict_params
[3] * *srcf
[1]);
982 for (i
= 0; i
< csize
; i
++) {
983 i0
= *src
[0] + (((i_ict_params
[0] * *src
[2]) + (1 << 15)) >> 16);
984 i1
= *src
[0] - (((i_ict_params
[1] * *src
[1]) + (1 << 15)) >> 16)
985 - (((i_ict_params
[2] * *src
[2]) + (1 << 15)) >> 16);
986 i2
= *src
[0] + (((i_ict_params
[3] * *src
[1]) + (1 << 15)) >> 16);
993 for (i
= 0; i
< csize
; i
++) {
994 i1
= *src
[0] - (*src
[2] + *src
[1] >> 2);
1006 static int jpeg2000_decode_tile(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
,
1009 int compno
, reslevelno
, bandno
;
1013 Jpeg2000T1Context t1
;
1014 /* Loop on tile components */
1016 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1017 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1018 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
1019 /* Loop on resolution levels */
1020 for (reslevelno
= 0; reslevelno
< codsty
->nreslevels2decode
; reslevelno
++) {
1021 Jpeg2000ResLevel
*rlevel
= comp
->reslevel
+ reslevelno
;
1023 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
1024 uint16_t nb_precincts
, precno
;
1025 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
1026 int cblkno
= 0, bandpos
;
1027 bandpos
= bandno
+ (reslevelno
> 0);
1029 nb_precincts
= rlevel
->num_precincts_x
* rlevel
->num_precincts_y
;
1030 /* Loop on precincts */
1031 for (precno
= 0; precno
< nb_precincts
; precno
++) {
1032 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
1034 /* Loop on codeblocks */
1035 for (cblkno
= 0; cblkno
< prec
->nb_codeblocks_width
* prec
->nb_codeblocks_height
; cblkno
++) {
1037 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
1038 decode_cblk(s
, codsty
, &t1
, cblk
,
1039 cblk
->coord
[0][1] - cblk
->coord
[0][0],
1040 cblk
->coord
[1][1] - cblk
->coord
[1][0],
1043 /* Manage band offsets */
1044 x
= cblk
->coord
[0][0];
1045 y
= cblk
->coord
[1][0];
1046 if ((reslevelno
> 0) && ((bandno
+ 1) & 1)) {
1047 Jpeg2000ResLevel
*pres
= comp
->reslevel
+ (reslevelno
- 1);
1048 x
+= pres
->coord
[0][1] - pres
->coord
[0][0];
1050 if ((reslevelno
> 0) && ((bandno
+ 1) & 2)) {
1051 Jpeg2000ResLevel
*pres
= comp
->reslevel
+ (reslevelno
- 1);
1052 y
+= pres
->coord
[1][1] - pres
->coord
[1][0];
1055 if (s
->avctx
->flags
& CODEC_FLAG_BITEXACT
)
1056 dequantization_int(x
, y
, cblk
, comp
, &t1
, band
);
1058 dequantization_float(x
, y
, cblk
, comp
, &t1
, band
);
1062 } /* end reslevel */
1065 ff_dwt_decode(&comp
->dwt
, comp
->data
);
1068 /* inverse MCT transformation */
1069 if (tile
->codsty
[0].mct
)
1070 mct_decode(s
, tile
);
1072 if (s
->avctx
->pix_fmt
== AV_PIX_FMT_BGRA
) // RGBA -> BGRA
1073 FFSWAP(float *, tile
->comp
[0].data
, tile
->comp
[2].data
);
1075 if (s
->precision
<= 8) {
1076 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1077 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1078 int32_t *datap
= (int32_t *)comp
->data
;
1079 y
= tile
->comp
[compno
].coord
[1][0] - s
->image_offset_y
;
1080 line
= picture
->data
[0] + y
* picture
->linesize
[0];
1081 for (; y
< tile
->comp
[compno
].coord
[1][1] - s
->image_offset_y
; y
+= s
->cdy
[compno
]) {
1084 x
= tile
->comp
[compno
].coord
[0][0] - s
->image_offset_x
;
1085 dst
= line
+ x
* s
->ncomponents
+ compno
;
1087 for (; x
< tile
->comp
[compno
].coord
[0][1] - s
->image_offset_x
; x
+= s
->cdx
[compno
]) {
1088 *datap
+= 1 << (s
->cbps
[compno
] - 1);
1091 else if (*datap
>= (1 << s
->cbps
[compno
]))
1092 *datap
= (1 << s
->cbps
[compno
]) - 1;
1094 dst
+= s
->ncomponents
;
1096 line
+= picture
->linesize
[0];
1100 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1101 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1102 float *datap
= comp
->data
;
1103 int32_t *i_datap
= (int32_t *) comp
->data
;
1106 y
= tile
->comp
[compno
].coord
[1][0] - s
->image_offset_y
;
1107 linel
= (uint16_t *)picture
->data
[0] + y
* (picture
->linesize
[0] >> 1);
1108 for (; y
< tile
->comp
[compno
].coord
[1][1] - s
->image_offset_y
; y
+= s
->cdy
[compno
]) {
1110 x
= tile
->comp
[compno
].coord
[0][0] - s
->image_offset_x
;
1111 dst
= linel
+ (x
* s
->ncomponents
+ compno
);
1112 for (; x
< s
->avctx
->width
; x
+= s
->cdx
[compno
]) {
1114 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1115 if (s
->avctx
->flags
& CODEC_FLAG_BITEXACT
)
1116 val
= *i_datap
+ (1 << (s
->cbps
[compno
] - 1));
1118 val
= lrintf(*datap
) + (1 << (s
->cbps
[compno
] - 1));
1119 val
= av_clip(val
, 0, (1 << s
->cbps
[compno
]) - 1);
1120 /* align 12 bit values in little-endian mode */
1124 dst
+= s
->ncomponents
;
1126 linel
+= picture
->linesize
[0] >> 1;
1133 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext
*s
)
1136 for (tileno
= 0; tileno
< s
->numXtiles
* s
->numYtiles
; tileno
++) {
1137 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1138 Jpeg2000Component
*comp
= s
->tile
[tileno
].comp
+ compno
;
1139 Jpeg2000CodingStyle
*codsty
= s
->tile
[tileno
].codsty
+ compno
;
1141 ff_jpeg2000_cleanup(comp
, codsty
);
1143 av_freep(&s
->tile
[tileno
].comp
);
1148 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext
*s
)
1150 Jpeg2000CodingStyle
*codsty
= s
->codsty
;
1151 Jpeg2000QuantStyle
*qntsty
= s
->qntsty
;
1152 uint8_t *properties
= s
->properties
;
1159 if (bytestream2_get_bytes_left(&s
->g
) < 2) {
1160 av_log(s
->avctx
, AV_LOG_ERROR
, "Missing EOC\n");
1164 marker
= bytestream2_get_be16u(&s
->g
);
1165 oldpos
= bytestream2_tell(&s
->g
);
1167 if (marker
== JPEG2000_EOC
)
1170 if (bytestream2_get_bytes_left(&s
->g
) < 2)
1171 return AVERROR_INVALIDDATA
;
1172 len
= bytestream2_get_be16u(&s
->g
);
1178 ret
= get_coc(s
, codsty
, properties
);
1181 ret
= get_cod(s
, codsty
, properties
);
1184 ret
= get_qcc(s
, len
, qntsty
, properties
);
1187 ret
= get_qcd(s
, len
, qntsty
, properties
);
1190 ret
= get_sot(s
, len
);
1193 // the comment is ignored
1194 bytestream2_skip(&s
->g
, len
- 2);
1197 // Tile-part lengths
1198 ret
= get_tlm(s
, len
);
1201 av_log(s
->avctx
, AV_LOG_ERROR
,
1202 "unsupported marker 0x%.4X at pos 0x%X\n",
1203 marker
, bytestream2_tell(&s
->g
) - 4);
1204 bytestream2_skip(&s
->g
, len
- 2);
1207 if (((bytestream2_tell(&s
->g
) - oldpos
!= len
) && (marker
!= JPEG2000_SOT
)) || ret
) {
1208 av_log(s
->avctx
, AV_LOG_ERROR
,
1209 "error during processing marker segment %.4x\n", marker
);
1210 return ret ? ret
: -1;
1216 /* Read bit stream packets --> T2 operation. */
1217 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext
*s
)
1220 Jpeg2000Tile
*tile
= s
->tile
+ s
->curtileno
;
1222 if (ret
= init_tile(s
, s
->curtileno
))
1224 if (ret
= jpeg2000_decode_packets(s
, tile
))
1230 static int jp2_find_codestream(Jpeg2000DecoderContext
*s
)
1232 uint32_t atom_size
, atom
;
1233 int found_codestream
= 0, search_range
= 10;
1235 while(!found_codestream
&& search_range
1237 bytestream2_get_bytes_left(&s
->g
) >= 8) {
1238 atom_size
= bytestream2_get_be32u(&s
->g
);
1239 atom
= bytestream2_get_be32u(&s
->g
);
1240 if (atom
== JP2_CODESTREAM
) {
1241 found_codestream
= 1;
1243 if (bytestream2_get_bytes_left(&s
->g
) < atom_size
- 8)
1245 bytestream2_skipu(&s
->g
, atom_size
- 8);
1250 if (found_codestream
)
1255 static int jpeg2000_decode_frame(AVCodecContext
*avctx
, void *data
,
1256 int *got_frame
, AVPacket
*avpkt
)
1258 Jpeg2000DecoderContext
*s
= avctx
->priv_data
;
1259 ThreadFrame frame
= { .f
= data
};
1260 AVFrame
*picture
= data
;
1264 bytestream2_init(&s
->g
, avpkt
->data
, avpkt
->size
);
1265 s
->curtileno
= 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1267 // reduction factor, i.e number of resolution levels to skip
1268 s
->reduction_factor
= s
->lowres
;
1270 if (bytestream2_get_bytes_left(&s
->g
) < 2)
1271 return AVERROR_INVALIDDATA
;
1273 // check if the image is in jp2 format
1274 if (bytestream2_get_bytes_left(&s
->g
) >= 12 &&
1275 (bytestream2_get_be32u(&s
->g
) == 12) &&
1276 (bytestream2_get_be32u(&s
->g
) == JP2_SIG_TYPE
) &&
1277 (bytestream2_get_be32u(&s
->g
) == JP2_SIG_VALUE
)) {
1278 if (!jp2_find_codestream(s
)) {
1279 av_log(avctx
, AV_LOG_ERROR
,
1280 "Could not find Jpeg2000 codestream atom.\n");
1281 return AVERROR_INVALIDDATA
;
1284 bytestream2_seek(&s
->g
, 0, SEEK_SET
);
1285 if (bytestream2_peek_be16(&s
->g
) != JPEG2000_SOC
)
1286 bytestream2_skip(&s
->g
, 8);
1289 if (bytestream2_get_be16u(&s
->g
) != JPEG2000_SOC
) {
1290 av_log(avctx
, AV_LOG_ERROR
, "SOC marker not present\n");
1291 return AVERROR_INVALIDDATA
;
1293 if (ret
= jpeg2000_read_main_headers(s
))
1296 /* get picture buffer */
1297 if ((ret
= ff_thread_get_buffer(avctx
, &frame
, 0)) < 0) {
1298 av_log(avctx
, AV_LOG_ERROR
, "ff_thread_get_buffer() failed.\n");
1301 picture
->pict_type
= AV_PICTURE_TYPE_I
;
1302 picture
->key_frame
= 1;
1304 if (ret
= jpeg2000_read_bitstream_packets(s
))
1306 for (tileno
= 0; tileno
< s
->numXtiles
* s
->numYtiles
; tileno
++)
1307 if (ret
= jpeg2000_decode_tile(s
, s
->tile
+ tileno
, picture
))
1312 return bytestream2_tell(&s
->g
);
1315 jpeg2000_dec_cleanup(s
);
1319 static void jpeg2000_init_static_data(AVCodec
*codec
)
1321 ff_jpeg2000_init_tier1_luts();
1324 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1325 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1327 static const AVOption options
[] = {
1328 { "lowres", "Lower the decoding resolution by a power of two",
1329 OFFSET(lowres
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, JPEG2000_MAX_RESLEVELS
- 1, VD
},
1333 static const AVProfile profiles
[] = {
1334 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0
, "JPEG 2000 codestream restriction 0" },
1335 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1
, "JPEG 2000 codestream restriction 1" },
1336 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION
, "JPEG 2000 no codestream restrictions" },
1337 { FF_PROFILE_JPEG2000_DCINEMA_2K
, "JPEG 2000 digital cinema 2K" },
1338 { FF_PROFILE_JPEG2000_DCINEMA_4K
, "JPEG 2000 digital cinema 4K" },
1339 { FF_PROFILE_UNKNOWN
},
1342 static const AVClass
class = {
1343 .class_name
= "jpeg2000",
1344 .item_name
= av_default_item_name
,
1346 .version
= LIBAVUTIL_VERSION_INT
,
1349 AVCodec ff_jpeg2000_decoder
= {
1351 .long_name
= NULL_IF_CONFIG_SMALL("JPEG 2000"),
1352 .type
= AVMEDIA_TYPE_VIDEO
,
1353 .id
= AV_CODEC_ID_JPEG2000
,
1354 .capabilities
= CODEC_CAP_FRAME_THREADS
,
1355 .priv_data_size
= sizeof(Jpeg2000DecoderContext
),
1356 .init_static_data
= jpeg2000_init_static_data
,
1357 .decode
= jpeg2000_decode_frame
,
1358 .priv_class
= &class,
1359 .pix_fmts
= (enum AVPixelFormat
[]) { AV_PIX_FMT_XYZ12
,
1362 .profiles
= NULL_IF_CONFIG_SMALL(profiles
)