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 unsigned 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
)
159 if (bytestream2_get_bytes_left(&s
->g
) < 36)
160 return AVERROR_INVALIDDATA
;
162 s
->avctx
->profile
= bytestream2_get_be16u(&s
->g
); // Rsiz
163 s
->width
= bytestream2_get_be32u(&s
->g
); // Width
164 s
->height
= bytestream2_get_be32u(&s
->g
); // Height
165 s
->image_offset_x
= bytestream2_get_be32u(&s
->g
); // X0Siz
166 s
->image_offset_y
= bytestream2_get_be32u(&s
->g
); // Y0Siz
167 s
->tile_width
= bytestream2_get_be32u(&s
->g
); // XTSiz
168 s
->tile_height
= bytestream2_get_be32u(&s
->g
); // YTSiz
169 s
->tile_offset_x
= bytestream2_get_be32u(&s
->g
); // XT0Siz
170 s
->tile_offset_y
= bytestream2_get_be32u(&s
->g
); // YT0Siz
171 ncomponents
= bytestream2_get_be16u(&s
->g
); // CSiz
173 if (ncomponents
<= 0) {
174 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid number of components: %d\n",
176 return AVERROR_INVALIDDATA
;
179 if (ncomponents
> 3) {
180 avpriv_request_sample(s
->avctx
, "Support for %d components",
182 return AVERROR_PATCHWELCOME
;
185 s
->ncomponents
= ncomponents
;
187 if (s
->tile_width
<= 0 || s
->tile_height
<= 0 ||
188 s
->tile_width
> s
->width
|| s
->tile_height
> s
->height
) {
189 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid tile dimension %dx%d.\n",
190 s
->tile_width
, s
->tile_height
);
191 return AVERROR_INVALIDDATA
;
194 if (bytestream2_get_bytes_left(&s
->g
) < 3 * s
->ncomponents
)
195 return AVERROR_INVALIDDATA
;
197 for (i
= 0; i
< s
->ncomponents
; i
++) { // Ssiz_i XRsiz_i, YRsiz_i
198 uint8_t x
= bytestream2_get_byteu(&s
->g
);
199 s
->cbps
[i
] = (x
& 0x7f) + 1;
200 s
->precision
= FFMAX(s
->cbps
[i
], s
->precision
);
201 s
->sgnd
[i
] = (x
& 0x80) == 1;
202 s
->cdx
[i
] = bytestream2_get_byteu(&s
->g
);
203 s
->cdy
[i
] = bytestream2_get_byteu(&s
->g
);
205 if (s
->cdx
[i
] != 1 || s
->cdy
[i
] != 1) {
206 avpriv_request_sample(s
->avctx
,
207 "CDxy values %d %d for component %d",
208 s
->cdx
[i
], s
->cdy
[i
], i
);
209 if (!s
->cdx
[i
] || !s
->cdy
[i
])
210 return AVERROR_INVALIDDATA
;
212 return AVERROR_PATCHWELCOME
;
216 s
->numXtiles
= ff_jpeg2000_ceildiv(s
->width
- s
->tile_offset_x
, s
->tile_width
);
217 s
->numYtiles
= ff_jpeg2000_ceildiv(s
->height
- s
->tile_offset_y
, s
->tile_height
);
219 s
->tile
= av_mallocz_array(s
->numXtiles
* s
->numYtiles
, sizeof(*s
->tile
));
221 s
->numXtiles
= s
->numYtiles
= 0;
222 return AVERROR(ENOMEM
);
225 for (i
= 0; i
< s
->numXtiles
* s
->numYtiles
; i
++) {
226 Jpeg2000Tile
*tile
= s
->tile
+ i
;
228 tile
->comp
= av_mallocz(s
->ncomponents
* sizeof(*tile
->comp
));
230 return AVERROR(ENOMEM
);
233 /* compute image size with reduction factor */
234 s
->avctx
->width
= ff_jpeg2000_ceildivpow2(s
->width
- s
->image_offset_x
,
235 s
->reduction_factor
);
236 s
->avctx
->height
= ff_jpeg2000_ceildivpow2(s
->height
- s
->image_offset_y
,
237 s
->reduction_factor
);
239 switch (s
->avctx
->profile
) {
240 case FF_PROFILE_JPEG2000_DCINEMA_2K
:
241 case FF_PROFILE_JPEG2000_DCINEMA_4K
:
242 /* XYZ color-space for digital cinema profiles */
243 s
->avctx
->pix_fmt
= AV_PIX_FMT_XYZ12
;
246 /* For other profiles selects color-space according number of
247 * components and bit depth precision. */
248 switch (s
->ncomponents
) {
250 if (s
->precision
> 8)
251 s
->avctx
->pix_fmt
= AV_PIX_FMT_GRAY16
;
253 s
->avctx
->pix_fmt
= AV_PIX_FMT_GRAY8
;
256 if (s
->precision
> 8)
257 s
->avctx
->pix_fmt
= AV_PIX_FMT_RGB48
;
259 s
->avctx
->pix_fmt
= AV_PIX_FMT_RGB24
;
262 s
->avctx
->pix_fmt
= AV_PIX_FMT_BGRA
;
265 /* pixel format can not be identified */
266 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
325 /* get coding parameters for a particular tile or whole image*/
326 static int get_cod(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*c
,
329 Jpeg2000CodingStyle tmp
;
332 if (bytestream2_get_bytes_left(&s
->g
) < 5)
333 return AVERROR_INVALIDDATA
;
335 tmp
.log2_prec_width
=
336 tmp
.log2_prec_height
= 15;
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
347 for (compno
= 0; compno
< s
->ncomponents
; compno
++)
348 if (!(properties
[compno
] & HAD_COC
))
349 memcpy(c
+ compno
, &tmp
, sizeof(tmp
));
353 /* Get coding parameters for a component in the whole image or a
354 * particular tile. */
355 static int get_coc(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*c
,
360 if (bytestream2_get_bytes_left(&s
->g
) < 2)
361 return AVERROR_INVALIDDATA
;
363 compno
= bytestream2_get_byteu(&s
->g
);
366 c
->csty
= bytestream2_get_byteu(&s
->g
);
369 properties
[compno
] |= HAD_COC
;
373 /* Get common part for QCD and QCC segments. */
374 static int get_qcx(Jpeg2000DecoderContext
*s
, int n
, Jpeg2000QuantStyle
*q
)
378 if (bytestream2_get_bytes_left(&s
->g
) < 1)
379 return AVERROR_INVALIDDATA
;
381 x
= bytestream2_get_byteu(&s
->g
); // Sqcd
383 q
->nguardbits
= x
>> 5;
384 q
->quantsty
= x
& 0x1f;
386 if (q
->quantsty
== JPEG2000_QSTY_NONE
) {
388 if (bytestream2_get_bytes_left(&s
->g
) < n
||
389 n
> JPEG2000_MAX_DECLEVELS
)
390 return AVERROR_INVALIDDATA
;
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_INVALIDDATA
;
396 x
= bytestream2_get_be16u(&s
->g
);
397 q
->expn
[0] = x
>> 11;
398 q
->mant
[0] = x
& 0x7ff;
399 for (i
= 1; i
< JPEG2000_MAX_DECLEVELS
* 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
||
407 n
> JPEG2000_MAX_DECLEVELS
)
408 return AVERROR_INVALIDDATA
;
409 for (i
= 0; i
< n
; i
++) {
410 x
= bytestream2_get_be16u(&s
->g
);
411 q
->expn
[i
] = x
>> 11;
412 q
->mant
[i
] = x
& 0x7ff;
418 /* Get quantization parameters for a particular tile or a whole image. */
419 static int get_qcd(Jpeg2000DecoderContext
*s
, int n
, Jpeg2000QuantStyle
*q
,
422 Jpeg2000QuantStyle tmp
;
425 if ((ret
= get_qcx(s
, n
, &tmp
)) < 0)
427 for (compno
= 0; compno
< s
->ncomponents
; compno
++)
428 if (!(properties
[compno
] & HAD_QCC
))
429 memcpy(q
+ compno
, &tmp
, sizeof(tmp
));
433 /* Get quantization parameters for a component in the whole image
434 * on in a particular tile. */
435 static int get_qcc(Jpeg2000DecoderContext
*s
, int n
, Jpeg2000QuantStyle
*q
,
440 if (bytestream2_get_bytes_left(&s
->g
) < 1)
441 return AVERROR_INVALIDDATA
;
443 compno
= bytestream2_get_byteu(&s
->g
);
444 properties
[compno
] |= HAD_QCC
;
445 return get_qcx(s
, n
- 1, q
+ compno
);
448 /* Get start of tile segment. */
449 static int get_sot(Jpeg2000DecoderContext
*s
, int n
)
451 Jpeg2000TilePart
*tp
;
456 if (bytestream2_get_bytes_left(&s
->g
) < 8)
457 return AVERROR_INVALIDDATA
;
459 Isot
= bytestream2_get_be16u(&s
->g
); // Isot
460 if (Isot
>= s
->numXtiles
* s
->numYtiles
)
461 return AVERROR_INVALIDDATA
;
464 avpriv_request_sample(s
->avctx
, "Support for more than one tile");
465 return AVERROR_PATCHWELCOME
;
467 Psot
= bytestream2_get_be32u(&s
->g
); // Psot
468 TPsot
= bytestream2_get_byteu(&s
->g
); // TPsot
470 /* Read TNSot but not used */
471 bytestream2_get_byteu(&s
->g
); // TNsot
473 if (Psot
> bytestream2_get_bytes_left(&s
->g
) + n
+ 2) {
474 av_log(s
->avctx
, AV_LOG_ERROR
, "Psot %d too big\n", Psot
);
475 return AVERROR_INVALIDDATA
;
478 if (TPsot
>= FF_ARRAY_ELEMS(s
->tile
[Isot
].tile_part
)) {
479 avpriv_request_sample(s
->avctx
, "Support for %d components", TPsot
);
480 return AVERROR_PATCHWELCOME
;
483 tp
= s
->tile
[s
->curtileno
].tile_part
+ TPsot
;
484 tp
->tile_index
= Isot
;
488 /* Start of bit stream. Pointer to SOD marker
489 * Check SOD marker is present. */
490 if (JPEG2000_SOD
== bytestream2_get_be16(&s
->g
)) {
491 bytestream2_init(&tp
->tpg
, s
->g
.buffer
, tp
->tp_len
- n
- 4);
492 bytestream2_skip(&s
->g
, tp
->tp_len
- n
- 4);
494 av_log(s
->avctx
, AV_LOG_ERROR
, "SOD marker not found \n");
495 return AVERROR_INVALIDDATA
;
498 /* End address of bit stream =
499 * start address + (Psot - size of SOT HEADER(n)
500 * - size of SOT MARKER(2) - size of SOD marker(2) */
505 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
506 * Used to know the number of tile parts and lengths.
507 * There may be multiple TLMs in the header.
508 * TODO: The function is not used for tile-parts management, nor anywhere else.
509 * It can be useful to allocate memory for tile parts, before managing the SOT
510 * markers. Parsing the TLM header is needed to increment the input header
512 * This marker is mandatory for DCI. */
513 static uint8_t get_tlm(Jpeg2000DecoderContext
*s
, int n
)
515 uint8_t Stlm
, ST
, SP
, tile_tlm
, i
;
516 bytestream2_get_byte(&s
->g
); /* Ztlm: skipped */
517 Stlm
= bytestream2_get_byte(&s
->g
);
519 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
520 ST
= (Stlm
>> 4) & 0x03;
521 // TODO: Manage case of ST = 0b11 --> raise error
522 SP
= (Stlm
>> 6) & 0x01;
523 tile_tlm
= (n
- 4) / ((SP
+ 1) * 2 + ST
);
524 for (i
= 0; i
< tile_tlm
; i
++) {
529 bytestream2_get_byte(&s
->g
);
532 bytestream2_get_be16(&s
->g
);
535 bytestream2_get_be32(&s
->g
);
539 bytestream2_get_be16(&s
->g
);
541 bytestream2_get_be32(&s
->g
);
547 static int init_tile(Jpeg2000DecoderContext
*s
, int tileno
)
550 int tilex
= tileno
% s
->numXtiles
;
551 int tiley
= tileno
/ s
->numXtiles
;
552 Jpeg2000Tile
*tile
= s
->tile
+ tileno
;
553 Jpeg2000CodingStyle
*codsty
;
554 Jpeg2000QuantStyle
*qntsty
;
557 return AVERROR(ENOMEM
);
559 /* copy codsty, qnsty to tile. TODO: Is it the best way?
560 * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle
561 * and Jpeg2000QuantStyle */
562 memcpy(tile
->codsty
, s
->codsty
, s
->ncomponents
* sizeof(*codsty
));
563 memcpy(tile
->qntsty
, s
->qntsty
, s
->ncomponents
* sizeof(*qntsty
));
565 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
566 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
567 int ret
; // global bandno
568 codsty
= tile
->codsty
+ compno
;
569 qntsty
= tile
->qntsty
+ compno
;
571 comp
->coord_o
[0][0] = FFMAX(tilex
* s
->tile_width
+ s
->tile_offset_x
, s
->image_offset_x
);
572 comp
->coord_o
[0][1] = FFMIN((tilex
+ 1) * s
->tile_width
+ s
->tile_offset_x
, s
->width
);
573 comp
->coord_o
[1][0] = FFMAX(tiley
* s
->tile_height
+ s
->tile_offset_y
, s
->image_offset_y
);
574 comp
->coord_o
[1][1] = FFMIN((tiley
+ 1) * s
->tile_height
+ s
->tile_offset_y
, s
->height
);
576 // FIXME: add a dcinema profile check ?
577 // value is guaranteed by profile (orig=0, 1 tile)
578 comp
->coord
[0][0] = 0;
579 comp
->coord
[0][1] = s
->avctx
->width
;
580 comp
->coord
[1][0] = 0;
581 comp
->coord
[1][1] = s
->avctx
->height
;
583 if (ret
= ff_jpeg2000_init_component(comp
, codsty
, qntsty
,
584 s
->cbps
[compno
], s
->cdx
[compno
],
585 s
->cdy
[compno
], s
->avctx
))
591 /* Read the number of coding passes. */
592 static int getnpasses(Jpeg2000DecoderContext
*s
)
599 if ((num
= get_bits(s
, 2)) != 3)
600 return num
< 0 ? num
: 3 + num
;
601 if ((num
= get_bits(s
, 5)) != 31)
602 return num
< 0 ? num
: 6 + num
;
603 num
= get_bits(s
, 7);
604 return num
< 0 ? num
: 37 + num
;
607 static int getlblockinc(Jpeg2000DecoderContext
*s
)
610 while (ret
= get_bits(s
, 1)) {
618 static int jpeg2000_decode_packet(Jpeg2000DecoderContext
*s
,
619 Jpeg2000CodingStyle
*codsty
,
620 Jpeg2000ResLevel
*rlevel
, int precno
,
621 int layno
, uint8_t *expn
, int numgbits
)
623 int bandno
, cblkno
, ret
, nb_code_blocks
;
625 if (!(ret
= get_bits(s
, 1))) {
631 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
632 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
633 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
635 if (band
->coord
[0][0] == band
->coord
[0][1] ||
636 band
->coord
[1][0] == band
->coord
[1][1])
640 nb_code_blocks
= prec
->nb_codeblocks_height
*
641 prec
->nb_codeblocks_width
;
642 for (cblkno
= 0; cblkno
< nb_code_blocks
; cblkno
++) {
643 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
644 int incl
, newpasses
, llen
;
647 incl
= get_bits(s
, 1);
649 incl
= tag_tree_decode(s
, prec
->cblkincl
+ cblkno
, layno
+ 1) == layno
;
656 cblk
->nonzerobits
= expn
[bandno
] + numgbits
- 1 -
657 tag_tree_decode(s
, prec
->zerobits
+ cblkno
,
659 if ((newpasses
= getnpasses(s
)) < 0)
661 if ((llen
= getlblockinc(s
)) < 0)
663 cblk
->lblock
+= llen
;
664 if ((ret
= get_bits(s
, av_log2(newpasses
) + cblk
->lblock
)) < 0)
666 cblk
->lengthinc
= ret
;
667 cblk
->npasses
+= newpasses
;
672 if (codsty
->csty
& JPEG2000_CSTY_EPH
) {
673 if (bytestream2_peek_be16(&s
->g
) == JPEG2000_EPH
)
674 bytestream2_skip(&s
->g
, 2);
676 av_log(s
->avctx
, AV_LOG_ERROR
, "EPH marker not found.\n");
679 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
680 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
681 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
683 nb_code_blocks
= prec
->nb_codeblocks_height
* prec
->nb_codeblocks_width
;
684 for (cblkno
= 0; cblkno
< nb_code_blocks
; cblkno
++) {
685 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
686 if (bytestream2_get_bytes_left(&s
->g
) < cblk
->lengthinc
)
687 return AVERROR_INVALIDDATA
;
688 /* Code-block data can be empty. In that case initialize data
690 if (cblk
->lengthinc
> 0) {
691 bytestream2_get_bufferu(&s
->g
, cblk
->data
, cblk
->lengthinc
);
693 cblk
->data
[0] = 0xFF;
694 cblk
->data
[1] = 0xFF;
696 cblk
->length
+= cblk
->lengthinc
;
703 static int jpeg2000_decode_packets(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
)
705 int layno
, reslevelno
, compno
, precno
, ok_reslevel
, ret
;
706 uint8_t prog_order
= tile
->codsty
[0].prog_order
;
711 switch (prog_order
) {
712 case JPEG2000_PGOD_LRCP
:
713 for (layno
= 0; layno
< tile
->codsty
[0].nlayers
; layno
++) {
715 for (reslevelno
= 0; ok_reslevel
; reslevelno
++) {
717 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
718 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
719 Jpeg2000QuantStyle
*qntsty
= tile
->qntsty
+ compno
;
720 if (reslevelno
< codsty
->nreslevels
) {
721 Jpeg2000ResLevel
*rlevel
= tile
->comp
[compno
].reslevel
+
724 for (precno
= 0; precno
< rlevel
->num_precincts_x
* rlevel
->num_precincts_y
; precno
++)
725 if ((ret
= jpeg2000_decode_packet(s
,
728 qntsty
->expn
+ (reslevelno ?
3 * (reslevelno
- 1) + 1 : 0),
729 qntsty
->nguardbits
)) < 0)
737 case JPEG2000_PGOD_CPRL
:
738 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
739 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
740 Jpeg2000QuantStyle
*qntsty
= tile
->qntsty
+ compno
;
742 /* Set bit stream buffer address according to tile-part.
743 * For DCinema one tile-part per component, so can be
744 * indexed by component. */
745 s
->g
= tile
->tile_part
[compno
].tpg
;
747 /* Position loop (y axis)
748 * TODO: Automate computing of step 256.
749 * Fixed here, but to be computed before entering here. */
750 for (y
= 0; y
< s
->height
; y
+= 256) {
751 /* Position loop (y axis)
752 * TODO: automate computing of step 256.
753 * Fixed here, but to be computed before entering here. */
754 for (x
= 0; x
< s
->width
; x
+= 256) {
755 for (reslevelno
= 0; reslevelno
< codsty
->nreslevels
; reslevelno
++) {
757 uint8_t reducedresno
= codsty
->nreslevels
- 1 -reslevelno
; // ==> N_L - r
758 Jpeg2000ResLevel
*rlevel
= tile
->comp
[compno
].reslevel
+ reslevelno
;
760 if (!((y
% (1 << (rlevel
->log2_prec_height
+ reducedresno
)) == 0) ||
761 (y
== 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
764 if (!((x
% (1 << (rlevel
->log2_prec_width
+ reducedresno
)) == 0) ||
765 (x
== 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
768 // check if a precinct exists
769 prcx
= ff_jpeg2000_ceildivpow2(x
, reducedresno
) >> rlevel
->log2_prec_width
;
770 prcy
= ff_jpeg2000_ceildivpow2(y
, reducedresno
) >> rlevel
->log2_prec_height
;
771 precno
= prcx
+ rlevel
->num_precincts_x
* prcy
;
772 for (layno
= 0; layno
< tile
->codsty
[0].nlayers
; layno
++) {
773 if ((ret
= jpeg2000_decode_packet(s
, codsty
, rlevel
,
775 qntsty
->expn
+ (reslevelno ?
3 * (reslevelno
- 1) + 1 : 0),
776 qntsty
->nguardbits
)) < 0)
789 /* EOC marker reached */
790 bytestream2_skip(&s
->g
, 2);
795 /* TIER-1 routines */
796 static void decode_sigpass(Jpeg2000T1Context
*t1
, int width
, int height
,
797 int bpno
, int bandno
)
799 int mask
= 3 << (bpno
- 1), y0
, x
, y
;
801 for (y0
= 0; y0
< height
; y0
+= 4)
802 for (x
= 0; x
< width
; x
++)
803 for (y
= y0
; y
< height
&& y
< y0
+ 4; y
++)
804 if ((t1
->flags
[y
+ 1][x
+ 1] & JPEG2000_T1_SIG_NB
)
805 && !(t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
))) {
806 if (ff_mqc_decode(&t1
->mqc
,
808 ff_jpeg2000_getsigctxno(t1
->flags
[y
+ 1][x
+ 1],
810 int xorbit
, ctxno
= ff_jpeg2000_getsgnctxno(t1
->flags
[y
+ 1][x
+ 1],
814 (ff_mqc_decode(&t1
->mqc
,
815 t1
->mqc
.cx_states
+ ctxno
) ^ xorbit
)
818 ff_jpeg2000_set_significance(t1
, x
, y
,
821 t1
->flags
[y
+ 1][x
+ 1] |= JPEG2000_T1_VIS
;
825 static void decode_refpass(Jpeg2000T1Context
*t1
, int width
, int height
,
831 phalf
= 1 << (bpno
- 1);
834 for (y0
= 0; y0
< height
; y0
+= 4)
835 for (x
= 0; x
< width
; x
++)
836 for (y
= y0
; y
< height
&& y
< y0
+ 4; y
++)
837 if ((t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
)) == JPEG2000_T1_SIG
) {
838 int ctxno
= ff_jpeg2000_getrefctxno(t1
->flags
[y
+ 1][x
+ 1]);
839 int r
= ff_mqc_decode(&t1
->mqc
,
840 t1
->mqc
.cx_states
+ ctxno
)
842 t1
->data
[y
][x
] += t1
->data
[y
][x
] < 0 ?
-r
: r
;
843 t1
->flags
[y
+ 1][x
+ 1] |= JPEG2000_T1_REF
;
847 static void decode_clnpass(Jpeg2000DecoderContext
*s
, Jpeg2000T1Context
*t1
,
848 int width
, int height
, int bpno
, int bandno
,
851 int mask
= 3 << (bpno
- 1), y0
, x
, y
, runlen
, dec
;
853 for (y0
= 0; y0
< height
; y0
+= 4)
854 for (x
= 0; x
< width
; x
++) {
855 if (y0
+ 3 < height
&&
856 !((t1
->flags
[y0
+ 1][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
857 (t1
->flags
[y0
+ 2][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
858 (t1
->flags
[y0
+ 3][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
859 (t1
->flags
[y0
+ 4][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)))) {
860 if (!ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_RL
))
862 runlen
= ff_mqc_decode(&t1
->mqc
,
863 t1
->mqc
.cx_states
+ MQC_CX_UNI
);
864 runlen
= (runlen
<< 1) | ff_mqc_decode(&t1
->mqc
,
873 for (y
= y0
+ runlen
; y
< y0
+ 4 && y
< height
; y
++) {
875 if (!(t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
)))
876 dec
= ff_mqc_decode(&t1
->mqc
,
878 ff_jpeg2000_getsigctxno(t1
->flags
[y
+ 1][x
+ 1],
883 int ctxno
= ff_jpeg2000_getsgnctxno(t1
->flags
[y
+ 1][x
+ 1],
885 t1
->data
[y
][x
] = (ff_mqc_decode(&t1
->mqc
,
886 t1
->mqc
.cx_states
+ ctxno
) ^
889 ff_jpeg2000_set_significance(t1
, x
, y
, t1
->data
[y
][x
] < 0);
892 t1
->flags
[y
+ 1][x
+ 1] &= ~JPEG2000_T1_VIS
;
897 val
= ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
898 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
899 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
900 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
902 av_log(s
->avctx
, AV_LOG_ERROR
,
903 "Segmentation symbol value incorrect\n");
907 static int decode_cblk(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*codsty
,
908 Jpeg2000T1Context
*t1
, Jpeg2000Cblk
*cblk
,
909 int width
, int height
, int bandpos
)
911 int passno
= cblk
->npasses
, pass_t
= 2, bpno
= cblk
->nonzerobits
- 1, y
;
913 for (y
= 0; y
< height
; y
++)
914 memset(t1
->data
[y
], 0, width
* sizeof(width
));
915 /* If code-block contains no compressed data: nothing to do. */
918 for (y
= 0; y
< height
+ 2; y
++)
919 memset(t1
->flags
[y
], 0, (width
+ 2) * sizeof(width
));
921 ff_mqc_initdec(&t1
->mqc
, cblk
->data
);
922 cblk
->data
[cblk
->length
] = 0xff;
923 cblk
->data
[cblk
->length
+ 1] = 0xff;
928 decode_sigpass(t1
, width
, height
, bpno
+ 1, bandpos
);
931 decode_refpass(t1
, width
, height
, bpno
+ 1);
934 decode_clnpass(s
, t1
, width
, height
, bpno
+ 1, bandpos
,
935 codsty
->cblk_style
& JPEG2000_CBLK_SEGSYM
);
948 /* TODO: Verify dequantization for lossless case
949 * comp->data can be float or int
950 * band->stepsize can be float or int
951 * depending on the type of DWT transformation.
952 * see ISO/IEC 15444-1:2002 A.6.1 */
954 /* Float dequantization of a codeblock.*/
955 static void dequantization_float(int x
, int y
, Jpeg2000Cblk
*cblk
,
956 Jpeg2000Component
*comp
,
957 Jpeg2000T1Context
*t1
, Jpeg2000Band
*band
)
960 float *datap
= &comp
->data
[(comp
->coord
[0][1] - comp
->coord
[0][0]) * y
+ x
];
961 for (j
= 0; j
< (cblk
->coord
[1][1] - cblk
->coord
[1][0]); ++j
)
962 for (i
= 0; i
< (cblk
->coord
[0][1] - cblk
->coord
[0][0]); ++i
) {
963 idx
= (comp
->coord
[0][1] - comp
->coord
[0][0]) * j
+ i
;
964 datap
[idx
] = (float)(t1
->data
[j
][i
]) * ((float)band
->stepsize
);
969 /* Integer dequantization of a codeblock.*/
970 static void dequantization_int(int x
, int y
, Jpeg2000Cblk
*cblk
,
971 Jpeg2000Component
*comp
,
972 Jpeg2000T1Context
*t1
, Jpeg2000Band
*band
)
976 (int32_t *) &comp
->data
[(comp
->coord
[0][1] - comp
->coord
[0][0]) * y
+ x
];
977 for (j
= 0; j
< (cblk
->coord
[1][1] - cblk
->coord
[1][0]); ++j
)
978 for (i
= 0; i
< (cblk
->coord
[0][1] - cblk
->coord
[0][0]); ++i
) {
979 idx
= (comp
->coord
[0][1] - comp
->coord
[0][0]) * j
+ i
;
981 ((int32_t)(t1
->data
[j
][i
]) * ((int32_t)band
->stepsize
) + (1 << 15)) >> 16;
986 /* Inverse ICT parameters in float and integer.
987 * int value = (float value) * (1<<16) */
988 static const float f_ict_params
[4] = {
994 static const int i_ict_params
[4] = {
1001 static int mct_decode(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
)
1005 int32_t *src
[3], i0
, i1
, i2
;
1006 float *srcf
[3], i0f
, i1f
, i2f
;
1008 for (i
= 0; i
< 3; i
++)
1009 if (tile
->codsty
[0].transform
== FF_DWT97
)
1010 srcf
[i
] = tile
->comp
[i
].data
;
1012 src
[i
] = (int32_t *)tile
->comp
[i
].data
;
1014 for (i
= 0; i
< 2; i
++)
1015 csize
*= tile
->comp
[0].coord
[i
][1] - tile
->comp
[0].coord
[i
][0];
1016 switch (tile
->codsty
[0].transform
) {
1018 for (i
= 0; i
< csize
; i
++) {
1019 i0f
= *srcf
[0] + (f_ict_params
[0] * *srcf
[2]);
1020 i1f
= *srcf
[0] - (f_ict_params
[1] * *srcf
[1])
1021 - (f_ict_params
[2] * *srcf
[2]);
1022 i2f
= *srcf
[0] + (f_ict_params
[3] * *srcf
[1]);
1029 for (i
= 0; i
< csize
; i
++) {
1030 i0
= *src
[0] + (((i_ict_params
[0] * *src
[2]) + (1 << 15)) >> 16);
1031 i1
= *src
[0] - (((i_ict_params
[1] * *src
[1]) + (1 << 15)) >> 16)
1032 - (((i_ict_params
[2] * *src
[2]) + (1 << 15)) >> 16);
1033 i2
= *src
[0] + (((i_ict_params
[3] * *src
[1]) + (1 << 15)) >> 16);
1040 for (i
= 0; i
< csize
; i
++) {
1041 i1
= *src
[0] - (*src
[2] + *src
[1] >> 2);
1053 static int jpeg2000_decode_tile(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
,
1056 int compno
, reslevelno
, bandno
;
1060 Jpeg2000T1Context t1
;
1061 /* Loop on tile components */
1063 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1064 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1065 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
1066 /* Loop on resolution levels */
1067 for (reslevelno
= 0; reslevelno
< codsty
->nreslevels2decode
; reslevelno
++) {
1068 Jpeg2000ResLevel
*rlevel
= comp
->reslevel
+ reslevelno
;
1070 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
1071 uint16_t nb_precincts
, precno
;
1072 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
1073 int cblkno
= 0, bandpos
;
1074 bandpos
= bandno
+ (reslevelno
> 0);
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];
1093 if ((reslevelno
> 0) && ((bandno
+ 1) & 1)) {
1094 Jpeg2000ResLevel
*pres
= comp
->reslevel
+ (reslevelno
- 1);
1095 x
+= pres
->coord
[0][1] - pres
->coord
[0][0];
1097 if ((reslevelno
> 0) && ((bandno
+ 1) & 2)) {
1098 Jpeg2000ResLevel
*pres
= comp
->reslevel
+ (reslevelno
- 1);
1099 y
+= pres
->coord
[1][1] - pres
->coord
[1][0];
1102 if (s
->avctx
->flags
& CODEC_FLAG_BITEXACT
)
1103 dequantization_int(x
, y
, cblk
, comp
, &t1
, band
);
1105 dequantization_float(x
, y
, cblk
, comp
, &t1
, band
);
1109 } /* end reslevel */
1112 ff_dwt_decode(&comp
->dwt
, comp
->data
);
1115 /* inverse MCT transformation */
1116 if (tile
->codsty
[0].mct
)
1117 mct_decode(s
, tile
);
1119 if (s
->avctx
->pix_fmt
== AV_PIX_FMT_BGRA
) // RGBA -> BGRA
1120 FFSWAP(float *, tile
->comp
[0].data
, tile
->comp
[2].data
);
1122 if (s
->precision
<= 8) {
1123 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1124 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1125 int32_t *datap
= (int32_t *)comp
->data
;
1126 y
= tile
->comp
[compno
].coord
[1][0] - s
->image_offset_y
;
1127 line
= picture
->data
[0] + y
* picture
->linesize
[0];
1128 for (; y
< tile
->comp
[compno
].coord
[1][1] - s
->image_offset_y
; y
+= s
->cdy
[compno
]) {
1131 x
= tile
->comp
[compno
].coord
[0][0] - s
->image_offset_x
;
1132 dst
= line
+ x
* s
->ncomponents
+ compno
;
1134 for (; x
< tile
->comp
[compno
].coord
[0][1] - s
->image_offset_x
; x
+= s
->cdx
[compno
]) {
1135 *datap
+= 1 << (s
->cbps
[compno
] - 1);
1138 else if (*datap
>= (1 << s
->cbps
[compno
]))
1139 *datap
= (1 << s
->cbps
[compno
]) - 1;
1141 dst
+= s
->ncomponents
;
1143 line
+= picture
->linesize
[0];
1147 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1148 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1149 float *datap
= comp
->data
;
1150 int32_t *i_datap
= (int32_t *) comp
->data
;
1153 y
= tile
->comp
[compno
].coord
[1][0] - s
->image_offset_y
;
1154 linel
= (uint16_t *)picture
->data
[0] + y
* (picture
->linesize
[0] >> 1);
1155 for (; y
< tile
->comp
[compno
].coord
[1][1] - s
->image_offset_y
; y
+= s
->cdy
[compno
]) {
1157 x
= tile
->comp
[compno
].coord
[0][0] - s
->image_offset_x
;
1158 dst
= linel
+ (x
* s
->ncomponents
+ compno
);
1159 for (; x
< s
->avctx
->width
; x
+= s
->cdx
[compno
]) {
1161 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1162 if (s
->avctx
->flags
& CODEC_FLAG_BITEXACT
)
1163 val
= *i_datap
+ (1 << (s
->cbps
[compno
] - 1));
1165 val
= lrintf(*datap
) + (1 << (s
->cbps
[compno
] - 1));
1166 val
= av_clip(val
, 0, (1 << s
->cbps
[compno
]) - 1);
1167 /* align 12 bit values in little-endian mode */
1171 dst
+= s
->ncomponents
;
1173 linel
+= picture
->linesize
[0] >> 1;
1180 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext
*s
)
1183 for (tileno
= 0; tileno
< s
->numXtiles
* s
->numYtiles
; tileno
++) {
1184 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1185 Jpeg2000Component
*comp
= s
->tile
[tileno
].comp
+ compno
;
1186 Jpeg2000CodingStyle
*codsty
= s
->tile
[tileno
].codsty
+ compno
;
1188 ff_jpeg2000_cleanup(comp
, codsty
);
1190 av_freep(&s
->tile
[tileno
].comp
);
1195 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext
*s
)
1197 Jpeg2000CodingStyle
*codsty
= s
->codsty
;
1198 Jpeg2000QuantStyle
*qntsty
= s
->qntsty
;
1199 uint8_t *properties
= s
->properties
;
1206 if (bytestream2_get_bytes_left(&s
->g
) < 2) {
1207 av_log(s
->avctx
, AV_LOG_ERROR
, "Missing EOC\n");
1211 marker
= bytestream2_get_be16u(&s
->g
);
1212 oldpos
= bytestream2_tell(&s
->g
);
1214 if (marker
== JPEG2000_EOC
)
1217 if (bytestream2_get_bytes_left(&s
->g
) < 2)
1218 return AVERROR_INVALIDDATA
;
1219 len
= bytestream2_get_be16u(&s
->g
);
1225 ret
= get_coc(s
, codsty
, properties
);
1228 ret
= get_cod(s
, codsty
, properties
);
1231 ret
= get_qcc(s
, len
, qntsty
, properties
);
1234 ret
= get_qcd(s
, len
, qntsty
, properties
);
1237 ret
= get_sot(s
, len
);
1240 // the comment is ignored
1241 bytestream2_skip(&s
->g
, len
- 2);
1244 // Tile-part lengths
1245 ret
= get_tlm(s
, len
);
1248 av_log(s
->avctx
, AV_LOG_ERROR
,
1249 "unsupported marker 0x%.4X at pos 0x%X\n",
1250 marker
, bytestream2_tell(&s
->g
) - 4);
1251 bytestream2_skip(&s
->g
, len
- 2);
1254 if (((bytestream2_tell(&s
->g
) - oldpos
!= len
) && (marker
!= JPEG2000_SOT
)) || ret
) {
1255 av_log(s
->avctx
, AV_LOG_ERROR
,
1256 "error during processing marker segment %.4x\n", marker
);
1257 return ret ? ret
: -1;
1263 /* Read bit stream packets --> T2 operation. */
1264 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext
*s
)
1267 Jpeg2000Tile
*tile
= s
->tile
+ s
->curtileno
;
1269 if (ret
= init_tile(s
, s
->curtileno
))
1271 if (ret
= jpeg2000_decode_packets(s
, tile
))
1277 static int jp2_find_codestream(Jpeg2000DecoderContext
*s
)
1279 uint32_t atom_size
, atom
;
1280 int found_codestream
= 0, search_range
= 10;
1282 while(!found_codestream
&& search_range
1284 bytestream2_get_bytes_left(&s
->g
) >= 8) {
1285 atom_size
= bytestream2_get_be32u(&s
->g
);
1286 atom
= bytestream2_get_be32u(&s
->g
);
1287 if (atom
== JP2_CODESTREAM
) {
1288 found_codestream
= 1;
1290 if (bytestream2_get_bytes_left(&s
->g
) < atom_size
- 8)
1292 bytestream2_skipu(&s
->g
, atom_size
- 8);
1297 if (found_codestream
)
1302 static int jpeg2000_decode_frame(AVCodecContext
*avctx
, void *data
,
1303 int *got_frame
, AVPacket
*avpkt
)
1305 Jpeg2000DecoderContext
*s
= avctx
->priv_data
;
1306 ThreadFrame frame
= { .f
= data
};
1307 AVFrame
*picture
= data
;
1311 bytestream2_init(&s
->g
, avpkt
->data
, avpkt
->size
);
1312 s
->curtileno
= 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1314 // reduction factor, i.e number of resolution levels to skip
1315 s
->reduction_factor
= s
->lowres
;
1317 if (bytestream2_get_bytes_left(&s
->g
) < 2)
1318 return AVERROR_INVALIDDATA
;
1320 // check if the image is in jp2 format
1321 if (bytestream2_get_bytes_left(&s
->g
) >= 12 &&
1322 (bytestream2_get_be32u(&s
->g
) == 12) &&
1323 (bytestream2_get_be32u(&s
->g
) == JP2_SIG_TYPE
) &&
1324 (bytestream2_get_be32u(&s
->g
) == JP2_SIG_VALUE
)) {
1325 if (!jp2_find_codestream(s
)) {
1326 av_log(avctx
, AV_LOG_ERROR
,
1327 "Could not find Jpeg2000 codestream atom.\n");
1328 return AVERROR_INVALIDDATA
;
1331 bytestream2_seek(&s
->g
, 0, SEEK_SET
);
1332 if (bytestream2_peek_be16(&s
->g
) != JPEG2000_SOC
)
1333 bytestream2_skip(&s
->g
, 8);
1336 if (bytestream2_get_be16u(&s
->g
) != JPEG2000_SOC
) {
1337 av_log(avctx
, AV_LOG_ERROR
, "SOC marker not present\n");
1338 return AVERROR_INVALIDDATA
;
1340 if (ret
= jpeg2000_read_main_headers(s
))
1343 /* get picture buffer */
1344 if ((ret
= ff_thread_get_buffer(avctx
, &frame
, 0)) < 0) {
1345 av_log(avctx
, AV_LOG_ERROR
, "ff_thread_get_buffer() failed.\n");
1348 picture
->pict_type
= AV_PICTURE_TYPE_I
;
1349 picture
->key_frame
= 1;
1351 if (ret
= jpeg2000_read_bitstream_packets(s
))
1353 for (tileno
= 0; tileno
< s
->numXtiles
* s
->numYtiles
; tileno
++)
1354 if (ret
= jpeg2000_decode_tile(s
, s
->tile
+ tileno
, picture
))
1359 return bytestream2_tell(&s
->g
);
1362 jpeg2000_dec_cleanup(s
);
1366 static void jpeg2000_init_static_data(AVCodec
*codec
)
1368 ff_jpeg2000_init_tier1_luts();
1371 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1372 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1374 static const AVOption options
[] = {
1375 { "lowres", "Lower the decoding resolution by a power of two",
1376 OFFSET(lowres
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, JPEG2000_MAX_RESLEVELS
- 1, VD
},
1380 static const AVProfile profiles
[] = {
1381 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0
, "JPEG 2000 codestream restriction 0" },
1382 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1
, "JPEG 2000 codestream restriction 1" },
1383 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION
, "JPEG 2000 no codestream restrictions" },
1384 { FF_PROFILE_JPEG2000_DCINEMA_2K
, "JPEG 2000 digital cinema 2K" },
1385 { FF_PROFILE_JPEG2000_DCINEMA_4K
, "JPEG 2000 digital cinema 4K" },
1386 { FF_PROFILE_UNKNOWN
},
1389 static const AVClass
class = {
1390 .class_name
= "jpeg2000",
1391 .item_name
= av_default_item_name
,
1393 .version
= LIBAVUTIL_VERSION_INT
,
1396 AVCodec ff_jpeg2000_decoder
= {
1398 .long_name
= NULL_IF_CONFIG_SMALL("JPEG 2000"),
1399 .type
= AVMEDIA_TYPE_VIDEO
,
1400 .id
= AV_CODEC_ID_JPEG2000
,
1401 .capabilities
= CODEC_CAP_FRAME_THREADS
,
1402 .priv_data_size
= sizeof(Jpeg2000DecoderContext
),
1403 .init_static_data
= jpeg2000_init_static_data
,
1404 .decode
= jpeg2000_decode_frame
,
1405 .priv_class
= &class,
1406 .pix_fmts
= (enum AVPixelFormat
[]) { AV_PIX_FMT_XYZ12
,
1409 .profiles
= NULL_IF_CONFIG_SMALL(profiles
)