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
426 if (Isot
>= s
->numXtiles
* s
->numYtiles
)
427 return AVERROR_INVALIDDATA
;
430 avpriv_request_sample(s
->avctx
, "Support for more than one tile");
431 return AVERROR_PATCHWELCOME
;
433 Psot
= bytestream2_get_be32u(&s
->g
); // Psot
434 TPsot
= bytestream2_get_byteu(&s
->g
); // TPsot
436 /* Read TNSot but not used */
437 bytestream2_get_byteu(&s
->g
); // TNsot
439 if (Psot
> bytestream2_get_bytes_left(&s
->g
) + n
+ 2) {
440 av_log(s
->avctx
, AV_LOG_ERROR
, "Psot %d too big\n", Psot
);
441 return AVERROR_INVALIDDATA
;
444 if (TPsot
>= FF_ARRAY_ELEMS(s
->tile
[Isot
].tile_part
)) {
445 avpriv_request_sample(s
->avctx
, "Support for %d components", TPsot
);
446 return AVERROR_PATCHWELCOME
;
449 tp
= s
->tile
[s
->curtileno
].tile_part
+ TPsot
;
450 tp
->tile_index
= Isot
;
454 /* Start of bit stream. Pointer to SOD marker
455 * Check SOD marker is present. */
456 if (JPEG2000_SOD
== bytestream2_get_be16(&s
->g
)) {
457 bytestream2_init(&tp
->tpg
, s
->g
.buffer
, tp
->tp_len
- n
- 4);
458 bytestream2_skip(&s
->g
, tp
->tp_len
- n
- 4);
460 av_log(s
->avctx
, AV_LOG_ERROR
, "SOD marker not found \n");
461 return AVERROR_INVALIDDATA
;
464 /* End address of bit stream =
465 * start address + (Psot - size of SOT HEADER(n)
466 * - size of SOT MARKER(2) - size of SOD marker(2) */
471 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
472 * Used to know the number of tile parts and lengths.
473 * There may be multiple TLMs in the header.
474 * TODO: The function is not used for tile-parts management, nor anywhere else.
475 * It can be useful to allocate memory for tile parts, before managing the SOT
476 * markers. Parsing the TLM header is needed to increment the input header
478 * This marker is mandatory for DCI. */
479 static uint8_t get_tlm(Jpeg2000DecoderContext
*s
, int n
)
481 uint8_t Stlm
, ST
, SP
, tile_tlm
, i
;
482 bytestream2_get_byte(&s
->g
); /* Ztlm: skipped */
483 Stlm
= bytestream2_get_byte(&s
->g
);
485 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
486 ST
= (Stlm
>> 4) & 0x03;
487 // TODO: Manage case of ST = 0b11 --> raise error
488 SP
= (Stlm
>> 6) & 0x01;
489 tile_tlm
= (n
- 4) / ((SP
+ 1) * 2 + ST
);
490 for (i
= 0; i
< tile_tlm
; i
++) {
495 bytestream2_get_byte(&s
->g
);
498 bytestream2_get_be16(&s
->g
);
501 bytestream2_get_be32(&s
->g
);
505 bytestream2_get_be16(&s
->g
);
507 bytestream2_get_be32(&s
->g
);
513 static int init_tile(Jpeg2000DecoderContext
*s
, int tileno
)
516 int tilex
= tileno
% s
->numXtiles
;
517 int tiley
= tileno
/ s
->numXtiles
;
518 Jpeg2000Tile
*tile
= s
->tile
+ tileno
;
519 Jpeg2000CodingStyle
*codsty
;
520 Jpeg2000QuantStyle
*qntsty
;
523 return AVERROR(ENOMEM
);
525 /* copy codsty, qnsty to tile. TODO: Is it the best way?
526 * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle
527 * and Jpeg2000QuantStyle */
528 memcpy(tile
->codsty
, s
->codsty
, s
->ncomponents
* sizeof(*codsty
));
529 memcpy(tile
->qntsty
, s
->qntsty
, s
->ncomponents
* sizeof(*qntsty
));
531 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
532 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
533 int ret
; // global bandno
534 codsty
= tile
->codsty
+ compno
;
535 qntsty
= tile
->qntsty
+ compno
;
537 comp
->coord_o
[0][0] = FFMAX(tilex
* s
->tile_width
+ s
->tile_offset_x
, s
->image_offset_x
);
538 comp
->coord_o
[0][1] = FFMIN((tilex
+ 1) * s
->tile_width
+ s
->tile_offset_x
, s
->width
);
539 comp
->coord_o
[1][0] = FFMAX(tiley
* s
->tile_height
+ s
->tile_offset_y
, s
->image_offset_y
);
540 comp
->coord_o
[1][1] = FFMIN((tiley
+ 1) * s
->tile_height
+ s
->tile_offset_y
, s
->height
);
542 // FIXME: add a dcinema profile check ?
543 // value is guaranteed by profile (orig=0, 1 tile)
544 comp
->coord
[0][0] = 0;
545 comp
->coord
[0][1] = s
->avctx
->width
;
546 comp
->coord
[1][0] = 0;
547 comp
->coord
[1][1] = s
->avctx
->height
;
549 if (ret
= ff_jpeg2000_init_component(comp
, codsty
, qntsty
,
550 s
->cbps
[compno
], s
->cdx
[compno
],
551 s
->cdy
[compno
], s
->avctx
))
557 /* Read the number of coding passes. */
558 static int getnpasses(Jpeg2000DecoderContext
*s
)
565 if ((num
= get_bits(s
, 2)) != 3)
566 return num
< 0 ? num
: 3 + num
;
567 if ((num
= get_bits(s
, 5)) != 31)
568 return num
< 0 ? num
: 6 + num
;
569 num
= get_bits(s
, 7);
570 return num
< 0 ? num
: 37 + num
;
573 static int getlblockinc(Jpeg2000DecoderContext
*s
)
576 while (ret
= get_bits(s
, 1)) {
584 static int jpeg2000_decode_packet(Jpeg2000DecoderContext
*s
,
585 Jpeg2000CodingStyle
*codsty
,
586 Jpeg2000ResLevel
*rlevel
, int precno
,
587 int layno
, uint8_t *expn
, int numgbits
)
589 int bandno
, cblkno
, ret
, nb_code_blocks
;
591 if (!(ret
= get_bits(s
, 1))) {
597 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
598 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
599 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
601 if (band
->coord
[0][0] == band
->coord
[0][1] ||
602 band
->coord
[1][0] == band
->coord
[1][1])
606 nb_code_blocks
= prec
->nb_codeblocks_height
*
607 prec
->nb_codeblocks_width
;
608 for (cblkno
= 0; cblkno
< nb_code_blocks
; cblkno
++) {
609 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
610 int incl
, newpasses
, llen
;
613 incl
= get_bits(s
, 1);
615 incl
= tag_tree_decode(s
, prec
->cblkincl
+ cblkno
, layno
+ 1) == layno
;
622 cblk
->nonzerobits
= expn
[bandno
] + numgbits
- 1 -
623 tag_tree_decode(s
, prec
->zerobits
+ cblkno
,
625 if ((newpasses
= getnpasses(s
)) < 0)
627 if ((llen
= getlblockinc(s
)) < 0)
629 cblk
->lblock
+= llen
;
630 if ((ret
= get_bits(s
, av_log2(newpasses
) + cblk
->lblock
)) < 0)
632 cblk
->lengthinc
= ret
;
633 cblk
->npasses
+= newpasses
;
638 if (codsty
->csty
& JPEG2000_CSTY_EPH
) {
639 if (bytestream2_peek_be16(&s
->g
) == JPEG2000_EPH
)
640 bytestream2_skip(&s
->g
, 2);
642 av_log(s
->avctx
, AV_LOG_ERROR
, "EPH marker not found.\n");
645 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
646 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
647 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
649 nb_code_blocks
= prec
->nb_codeblocks_height
* prec
->nb_codeblocks_width
;
650 for (cblkno
= 0; cblkno
< nb_code_blocks
; cblkno
++) {
651 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
652 if (bytestream2_get_bytes_left(&s
->g
) < cblk
->lengthinc
)
653 return AVERROR_INVALIDDATA
;
654 /* Code-block data can be empty. In that case initialize data
656 if (cblk
->lengthinc
> 0) {
657 bytestream2_get_bufferu(&s
->g
, cblk
->data
, cblk
->lengthinc
);
659 cblk
->data
[0] = 0xFF;
660 cblk
->data
[1] = 0xFF;
662 cblk
->length
+= cblk
->lengthinc
;
669 static int jpeg2000_decode_packets(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
)
671 int layno
, reslevelno
, compno
, precno
, ok_reslevel
, ret
;
672 uint8_t prog_order
= tile
->codsty
[0].prog_order
;
677 switch (prog_order
) {
678 case JPEG2000_PGOD_LRCP
:
679 for (layno
= 0; layno
< tile
->codsty
[0].nlayers
; layno
++) {
681 for (reslevelno
= 0; ok_reslevel
; reslevelno
++) {
683 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
684 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
685 Jpeg2000QuantStyle
*qntsty
= tile
->qntsty
+ compno
;
686 if (reslevelno
< codsty
->nreslevels
) {
687 Jpeg2000ResLevel
*rlevel
= tile
->comp
[compno
].reslevel
+
690 for (precno
= 0; precno
< rlevel
->num_precincts_x
* rlevel
->num_precincts_y
; precno
++)
691 if ((ret
= jpeg2000_decode_packet(s
,
694 qntsty
->expn
+ (reslevelno ?
3 * (reslevelno
- 1) + 1 : 0),
695 qntsty
->nguardbits
)) < 0)
703 case JPEG2000_PGOD_CPRL
:
704 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
705 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
706 Jpeg2000QuantStyle
*qntsty
= tile
->qntsty
+ compno
;
708 /* Set bit stream buffer address according to tile-part.
709 * For DCinema one tile-part per component, so can be
710 * indexed by component. */
711 s
->g
= tile
->tile_part
[compno
].tpg
;
713 /* Position loop (y axis)
714 * TODO: Automate computing of step 256.
715 * Fixed here, but to be computed before entering here. */
716 for (y
= 0; y
< s
->height
; y
+= 256) {
717 /* Position loop (y axis)
718 * TODO: automate computing of step 256.
719 * Fixed here, but to be computed before entering here. */
720 for (x
= 0; x
< s
->width
; x
+= 256) {
721 for (reslevelno
= 0; reslevelno
< codsty
->nreslevels
; reslevelno
++) {
723 uint8_t reducedresno
= codsty
->nreslevels
- 1 -reslevelno
; // ==> N_L - r
724 Jpeg2000ResLevel
*rlevel
= tile
->comp
[compno
].reslevel
+ reslevelno
;
726 if (!((y
% (1 << (rlevel
->log2_prec_height
+ reducedresno
)) == 0) ||
727 (y
== 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
730 if (!((x
% (1 << (rlevel
->log2_prec_width
+ reducedresno
)) == 0) ||
731 (x
== 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
734 // check if a precinct exists
735 prcx
= ff_jpeg2000_ceildivpow2(x
, reducedresno
) >> rlevel
->log2_prec_width
;
736 prcy
= ff_jpeg2000_ceildivpow2(y
, reducedresno
) >> rlevel
->log2_prec_height
;
737 precno
= prcx
+ rlevel
->num_precincts_x
* prcy
;
738 for (layno
= 0; layno
< tile
->codsty
[0].nlayers
; layno
++) {
739 if ((ret
= jpeg2000_decode_packet(s
, codsty
, rlevel
,
741 qntsty
->expn
+ (reslevelno ?
3 * (reslevelno
- 1) + 1 : 0),
742 qntsty
->nguardbits
)) < 0)
755 /* EOC marker reached */
756 bytestream2_skip(&s
->g
, 2);
761 /* TIER-1 routines */
762 static void decode_sigpass(Jpeg2000T1Context
*t1
, int width
, int height
,
763 int bpno
, int bandno
)
765 int mask
= 3 << (bpno
- 1), y0
, x
, y
;
767 for (y0
= 0; y0
< height
; y0
+= 4)
768 for (x
= 0; x
< width
; x
++)
769 for (y
= y0
; y
< height
&& y
< y0
+ 4; y
++)
770 if ((t1
->flags
[y
+ 1][x
+ 1] & JPEG2000_T1_SIG_NB
)
771 && !(t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
))) {
772 if (ff_mqc_decode(&t1
->mqc
,
774 ff_jpeg2000_getsigctxno(t1
->flags
[y
+ 1][x
+ 1],
776 int xorbit
, ctxno
= ff_jpeg2000_getsgnctxno(t1
->flags
[y
+ 1][x
+ 1],
780 (ff_mqc_decode(&t1
->mqc
,
781 t1
->mqc
.cx_states
+ ctxno
) ^ xorbit
)
784 ff_jpeg2000_set_significance(t1
, x
, y
,
787 t1
->flags
[y
+ 1][x
+ 1] |= JPEG2000_T1_VIS
;
791 static void decode_refpass(Jpeg2000T1Context
*t1
, int width
, int height
,
797 phalf
= 1 << (bpno
- 1);
800 for (y0
= 0; y0
< height
; y0
+= 4)
801 for (x
= 0; x
< width
; x
++)
802 for (y
= y0
; y
< height
&& y
< y0
+ 4; y
++)
803 if ((t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
)) == JPEG2000_T1_SIG
) {
804 int ctxno
= ff_jpeg2000_getrefctxno(t1
->flags
[y
+ 1][x
+ 1]);
805 int r
= ff_mqc_decode(&t1
->mqc
,
806 t1
->mqc
.cx_states
+ ctxno
)
808 t1
->data
[y
][x
] += t1
->data
[y
][x
] < 0 ?
-r
: r
;
809 t1
->flags
[y
+ 1][x
+ 1] |= JPEG2000_T1_REF
;
813 static void decode_clnpass(Jpeg2000DecoderContext
*s
, Jpeg2000T1Context
*t1
,
814 int width
, int height
, int bpno
, int bandno
,
817 int mask
= 3 << (bpno
- 1), y0
, x
, y
, runlen
, dec
;
819 for (y0
= 0; y0
< height
; y0
+= 4)
820 for (x
= 0; x
< width
; x
++) {
821 if (y0
+ 3 < height
&&
822 !((t1
->flags
[y0
+ 1][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
823 (t1
->flags
[y0
+ 2][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
824 (t1
->flags
[y0
+ 3][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)) ||
825 (t1
->flags
[y0
+ 4][x
+ 1] & (JPEG2000_T1_SIG_NB
| JPEG2000_T1_VIS
| JPEG2000_T1_SIG
)))) {
826 if (!ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_RL
))
828 runlen
= ff_mqc_decode(&t1
->mqc
,
829 t1
->mqc
.cx_states
+ MQC_CX_UNI
);
830 runlen
= (runlen
<< 1) | ff_mqc_decode(&t1
->mqc
,
839 for (y
= y0
+ runlen
; y
< y0
+ 4 && y
< height
; y
++) {
841 if (!(t1
->flags
[y
+ 1][x
+ 1] & (JPEG2000_T1_SIG
| JPEG2000_T1_VIS
)))
842 dec
= ff_mqc_decode(&t1
->mqc
,
844 ff_jpeg2000_getsigctxno(t1
->flags
[y
+ 1][x
+ 1],
849 int ctxno
= ff_jpeg2000_getsgnctxno(t1
->flags
[y
+ 1][x
+ 1],
851 t1
->data
[y
][x
] = (ff_mqc_decode(&t1
->mqc
,
852 t1
->mqc
.cx_states
+ ctxno
) ^
855 ff_jpeg2000_set_significance(t1
, x
, y
, t1
->data
[y
][x
] < 0);
858 t1
->flags
[y
+ 1][x
+ 1] &= ~JPEG2000_T1_VIS
;
863 val
= ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
864 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
865 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
866 val
= (val
<< 1) + ff_mqc_decode(&t1
->mqc
, t1
->mqc
.cx_states
+ MQC_CX_UNI
);
868 av_log(s
->avctx
, AV_LOG_ERROR
,
869 "Segmentation symbol value incorrect\n");
873 static int decode_cblk(Jpeg2000DecoderContext
*s
, Jpeg2000CodingStyle
*codsty
,
874 Jpeg2000T1Context
*t1
, Jpeg2000Cblk
*cblk
,
875 int width
, int height
, int bandpos
)
877 int passno
= cblk
->npasses
, pass_t
= 2, bpno
= cblk
->nonzerobits
- 1, y
;
879 for (y
= 0; y
< height
; y
++)
880 memset(t1
->data
[y
], 0, width
* sizeof(width
));
881 /* If code-block contains no compressed data: nothing to do. */
884 for (y
= 0; y
< height
+ 2; y
++)
885 memset(t1
->flags
[y
], 0, (width
+ 2) * sizeof(width
));
887 ff_mqc_initdec(&t1
->mqc
, cblk
->data
);
888 cblk
->data
[cblk
->length
] = 0xff;
889 cblk
->data
[cblk
->length
+ 1] = 0xff;
894 decode_sigpass(t1
, width
, height
, bpno
+ 1, bandpos
);
897 decode_refpass(t1
, width
, height
, bpno
+ 1);
900 decode_clnpass(s
, t1
, width
, height
, bpno
+ 1, bandpos
,
901 codsty
->cblk_style
& JPEG2000_CBLK_SEGSYM
);
914 /* TODO: Verify dequantization for lossless case
915 * comp->data can be float or int
916 * band->stepsize can be float or int
917 * depending on the type of DWT transformation.
918 * see ISO/IEC 15444-1:2002 A.6.1 */
920 /* Float dequantization of a codeblock.*/
921 static void dequantization_float(int x
, int y
, Jpeg2000Cblk
*cblk
,
922 Jpeg2000Component
*comp
,
923 Jpeg2000T1Context
*t1
, Jpeg2000Band
*band
)
926 float *datap
= &comp
->data
[(comp
->coord
[0][1] - comp
->coord
[0][0]) * y
+ x
];
927 for (j
= 0; j
< (cblk
->coord
[1][1] - cblk
->coord
[1][0]); ++j
)
928 for (i
= 0; i
< (cblk
->coord
[0][1] - cblk
->coord
[0][0]); ++i
) {
929 idx
= (comp
->coord
[0][1] - comp
->coord
[0][0]) * j
+ i
;
930 datap
[idx
] = (float)(t1
->data
[j
][i
]) * ((float)band
->stepsize
);
935 /* Integer dequantization of a codeblock.*/
936 static void dequantization_int(int x
, int y
, Jpeg2000Cblk
*cblk
,
937 Jpeg2000Component
*comp
,
938 Jpeg2000T1Context
*t1
, Jpeg2000Band
*band
)
942 (int32_t *) &comp
->data
[(comp
->coord
[0][1] - comp
->coord
[0][0]) * y
+ x
];
943 for (j
= 0; j
< (cblk
->coord
[1][1] - cblk
->coord
[1][0]); ++j
)
944 for (i
= 0; i
< (cblk
->coord
[0][1] - cblk
->coord
[0][0]); ++i
) {
945 idx
= (comp
->coord
[0][1] - comp
->coord
[0][0]) * j
+ i
;
947 ((int32_t)(t1
->data
[j
][i
]) * ((int32_t)band
->stepsize
) + (1 << 15)) >> 16;
952 /* Inverse ICT parameters in float and integer.
953 * int value = (float value) * (1<<16) */
954 static const float f_ict_params
[4] = {
960 static const int i_ict_params
[4] = {
967 static int mct_decode(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
)
971 int32_t *src
[3], i0
, i1
, i2
;
972 float *srcf
[3], i0f
, i1f
, i2f
;
974 for (i
= 0; i
< 3; i
++)
975 if (tile
->codsty
[0].transform
== FF_DWT97
)
976 srcf
[i
] = tile
->comp
[i
].data
;
978 src
[i
] = (int32_t *)tile
->comp
[i
].data
;
980 for (i
= 0; i
< 2; i
++)
981 csize
*= tile
->comp
[0].coord
[i
][1] - tile
->comp
[0].coord
[i
][0];
982 switch (tile
->codsty
[0].transform
) {
984 for (i
= 0; i
< csize
; i
++) {
985 i0f
= *srcf
[0] + (f_ict_params
[0] * *srcf
[2]);
986 i1f
= *srcf
[0] - (f_ict_params
[1] * *srcf
[1])
987 - (f_ict_params
[2] * *srcf
[2]);
988 i2f
= *srcf
[0] + (f_ict_params
[3] * *srcf
[1]);
995 for (i
= 0; i
< csize
; i
++) {
996 i0
= *src
[0] + (((i_ict_params
[0] * *src
[2]) + (1 << 15)) >> 16);
997 i1
= *src
[0] - (((i_ict_params
[1] * *src
[1]) + (1 << 15)) >> 16)
998 - (((i_ict_params
[2] * *src
[2]) + (1 << 15)) >> 16);
999 i2
= *src
[0] + (((i_ict_params
[3] * *src
[1]) + (1 << 15)) >> 16);
1006 for (i
= 0; i
< csize
; i
++) {
1007 i1
= *src
[0] - (*src
[2] + *src
[1] >> 2);
1019 static int jpeg2000_decode_tile(Jpeg2000DecoderContext
*s
, Jpeg2000Tile
*tile
,
1022 int compno
, reslevelno
, bandno
;
1026 Jpeg2000T1Context t1
;
1027 /* Loop on tile components */
1029 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1030 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1031 Jpeg2000CodingStyle
*codsty
= tile
->codsty
+ compno
;
1032 /* Loop on resolution levels */
1033 for (reslevelno
= 0; reslevelno
< codsty
->nreslevels2decode
; reslevelno
++) {
1034 Jpeg2000ResLevel
*rlevel
= comp
->reslevel
+ reslevelno
;
1036 for (bandno
= 0; bandno
< rlevel
->nbands
; bandno
++) {
1037 uint16_t nb_precincts
, precno
;
1038 Jpeg2000Band
*band
= rlevel
->band
+ bandno
;
1039 int cblkno
= 0, bandpos
;
1040 bandpos
= bandno
+ (reslevelno
> 0);
1042 nb_precincts
= rlevel
->num_precincts_x
* rlevel
->num_precincts_y
;
1043 /* Loop on precincts */
1044 for (precno
= 0; precno
< nb_precincts
; precno
++) {
1045 Jpeg2000Prec
*prec
= band
->prec
+ precno
;
1047 /* Loop on codeblocks */
1048 for (cblkno
= 0; cblkno
< prec
->nb_codeblocks_width
* prec
->nb_codeblocks_height
; cblkno
++) {
1050 Jpeg2000Cblk
*cblk
= prec
->cblk
+ cblkno
;
1051 decode_cblk(s
, codsty
, &t1
, cblk
,
1052 cblk
->coord
[0][1] - cblk
->coord
[0][0],
1053 cblk
->coord
[1][1] - cblk
->coord
[1][0],
1056 /* Manage band offsets */
1057 x
= cblk
->coord
[0][0];
1058 y
= cblk
->coord
[1][0];
1059 if ((reslevelno
> 0) && ((bandno
+ 1) & 1)) {
1060 Jpeg2000ResLevel
*pres
= comp
->reslevel
+ (reslevelno
- 1);
1061 x
+= pres
->coord
[0][1] - pres
->coord
[0][0];
1063 if ((reslevelno
> 0) && ((bandno
+ 1) & 2)) {
1064 Jpeg2000ResLevel
*pres
= comp
->reslevel
+ (reslevelno
- 1);
1065 y
+= pres
->coord
[1][1] - pres
->coord
[1][0];
1068 if (s
->avctx
->flags
& CODEC_FLAG_BITEXACT
)
1069 dequantization_int(x
, y
, cblk
, comp
, &t1
, band
);
1071 dequantization_float(x
, y
, cblk
, comp
, &t1
, band
);
1075 } /* end reslevel */
1078 ff_dwt_decode(&comp
->dwt
, comp
->data
);
1081 /* inverse MCT transformation */
1082 if (tile
->codsty
[0].mct
)
1083 mct_decode(s
, tile
);
1085 if (s
->avctx
->pix_fmt
== AV_PIX_FMT_BGRA
) // RGBA -> BGRA
1086 FFSWAP(float *, tile
->comp
[0].data
, tile
->comp
[2].data
);
1088 if (s
->precision
<= 8) {
1089 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1090 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1091 int32_t *datap
= (int32_t *)comp
->data
;
1092 y
= tile
->comp
[compno
].coord
[1][0] - s
->image_offset_y
;
1093 line
= picture
->data
[0] + y
* picture
->linesize
[0];
1094 for (; y
< tile
->comp
[compno
].coord
[1][1] - s
->image_offset_y
; y
+= s
->cdy
[compno
]) {
1097 x
= tile
->comp
[compno
].coord
[0][0] - s
->image_offset_x
;
1098 dst
= line
+ x
* s
->ncomponents
+ compno
;
1100 for (; x
< tile
->comp
[compno
].coord
[0][1] - s
->image_offset_x
; x
+= s
->cdx
[compno
]) {
1101 *datap
+= 1 << (s
->cbps
[compno
] - 1);
1104 else if (*datap
>= (1 << s
->cbps
[compno
]))
1105 *datap
= (1 << s
->cbps
[compno
]) - 1;
1107 dst
+= s
->ncomponents
;
1109 line
+= picture
->linesize
[0];
1113 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1114 Jpeg2000Component
*comp
= tile
->comp
+ compno
;
1115 float *datap
= comp
->data
;
1116 int32_t *i_datap
= (int32_t *) comp
->data
;
1119 y
= tile
->comp
[compno
].coord
[1][0] - s
->image_offset_y
;
1120 linel
= (uint16_t *)picture
->data
[0] + y
* (picture
->linesize
[0] >> 1);
1121 for (; y
< tile
->comp
[compno
].coord
[1][1] - s
->image_offset_y
; y
+= s
->cdy
[compno
]) {
1123 x
= tile
->comp
[compno
].coord
[0][0] - s
->image_offset_x
;
1124 dst
= linel
+ (x
* s
->ncomponents
+ compno
);
1125 for (; x
< s
->avctx
->width
; x
+= s
->cdx
[compno
]) {
1127 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1128 if (s
->avctx
->flags
& CODEC_FLAG_BITEXACT
)
1129 val
= *i_datap
+ (1 << (s
->cbps
[compno
] - 1));
1131 val
= lrintf(*datap
) + (1 << (s
->cbps
[compno
] - 1));
1132 val
= av_clip(val
, 0, (1 << s
->cbps
[compno
]) - 1);
1133 /* align 12 bit values in little-endian mode */
1137 dst
+= s
->ncomponents
;
1139 linel
+= picture
->linesize
[0] >> 1;
1146 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext
*s
)
1149 for (tileno
= 0; tileno
< s
->numXtiles
* s
->numYtiles
; tileno
++) {
1150 for (compno
= 0; compno
< s
->ncomponents
; compno
++) {
1151 Jpeg2000Component
*comp
= s
->tile
[tileno
].comp
+ compno
;
1152 Jpeg2000CodingStyle
*codsty
= s
->tile
[tileno
].codsty
+ compno
;
1154 ff_jpeg2000_cleanup(comp
, codsty
);
1156 av_freep(&s
->tile
[tileno
].comp
);
1161 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext
*s
)
1163 Jpeg2000CodingStyle
*codsty
= s
->codsty
;
1164 Jpeg2000QuantStyle
*qntsty
= s
->qntsty
;
1165 uint8_t *properties
= s
->properties
;
1172 if (bytestream2_get_bytes_left(&s
->g
) < 2) {
1173 av_log(s
->avctx
, AV_LOG_ERROR
, "Missing EOC\n");
1177 marker
= bytestream2_get_be16u(&s
->g
);
1178 oldpos
= bytestream2_tell(&s
->g
);
1180 if (marker
== JPEG2000_EOC
)
1183 if (bytestream2_get_bytes_left(&s
->g
) < 2)
1184 return AVERROR_INVALIDDATA
;
1185 len
= bytestream2_get_be16u(&s
->g
);
1191 ret
= get_coc(s
, codsty
, properties
);
1194 ret
= get_cod(s
, codsty
, properties
);
1197 ret
= get_qcc(s
, len
, qntsty
, properties
);
1200 ret
= get_qcd(s
, len
, qntsty
, properties
);
1203 ret
= get_sot(s
, len
);
1206 // the comment is ignored
1207 bytestream2_skip(&s
->g
, len
- 2);
1210 // Tile-part lengths
1211 ret
= get_tlm(s
, len
);
1214 av_log(s
->avctx
, AV_LOG_ERROR
,
1215 "unsupported marker 0x%.4X at pos 0x%X\n",
1216 marker
, bytestream2_tell(&s
->g
) - 4);
1217 bytestream2_skip(&s
->g
, len
- 2);
1220 if (((bytestream2_tell(&s
->g
) - oldpos
!= len
) && (marker
!= JPEG2000_SOT
)) || ret
) {
1221 av_log(s
->avctx
, AV_LOG_ERROR
,
1222 "error during processing marker segment %.4x\n", marker
);
1223 return ret ? ret
: -1;
1229 /* Read bit stream packets --> T2 operation. */
1230 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext
*s
)
1233 Jpeg2000Tile
*tile
= s
->tile
+ s
->curtileno
;
1235 if (ret
= init_tile(s
, s
->curtileno
))
1237 if (ret
= jpeg2000_decode_packets(s
, tile
))
1243 static int jp2_find_codestream(Jpeg2000DecoderContext
*s
)
1245 uint32_t atom_size
, atom
;
1246 int found_codestream
= 0, search_range
= 10;
1248 while(!found_codestream
&& search_range
1250 bytestream2_get_bytes_left(&s
->g
) >= 8) {
1251 atom_size
= bytestream2_get_be32u(&s
->g
);
1252 atom
= bytestream2_get_be32u(&s
->g
);
1253 if (atom
== JP2_CODESTREAM
) {
1254 found_codestream
= 1;
1256 if (bytestream2_get_bytes_left(&s
->g
) < atom_size
- 8)
1258 bytestream2_skipu(&s
->g
, atom_size
- 8);
1263 if (found_codestream
)
1268 static int jpeg2000_decode_frame(AVCodecContext
*avctx
, void *data
,
1269 int *got_frame
, AVPacket
*avpkt
)
1271 Jpeg2000DecoderContext
*s
= avctx
->priv_data
;
1272 ThreadFrame frame
= { .f
= data
};
1273 AVFrame
*picture
= data
;
1277 bytestream2_init(&s
->g
, avpkt
->data
, avpkt
->size
);
1278 s
->curtileno
= 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1280 // reduction factor, i.e number of resolution levels to skip
1281 s
->reduction_factor
= s
->lowres
;
1283 if (bytestream2_get_bytes_left(&s
->g
) < 2)
1284 return AVERROR_INVALIDDATA
;
1286 // check if the image is in jp2 format
1287 if (bytestream2_get_bytes_left(&s
->g
) >= 12 &&
1288 (bytestream2_get_be32u(&s
->g
) == 12) &&
1289 (bytestream2_get_be32u(&s
->g
) == JP2_SIG_TYPE
) &&
1290 (bytestream2_get_be32u(&s
->g
) == JP2_SIG_VALUE
)) {
1291 if (!jp2_find_codestream(s
)) {
1292 av_log(avctx
, AV_LOG_ERROR
,
1293 "Could not find Jpeg2000 codestream atom.\n");
1294 return AVERROR_INVALIDDATA
;
1297 bytestream2_seek(&s
->g
, 0, SEEK_SET
);
1298 if (bytestream2_peek_be16(&s
->g
) != JPEG2000_SOC
)
1299 bytestream2_skip(&s
->g
, 8);
1302 if (bytestream2_get_be16u(&s
->g
) != JPEG2000_SOC
) {
1303 av_log(avctx
, AV_LOG_ERROR
, "SOC marker not present\n");
1304 return AVERROR_INVALIDDATA
;
1306 if (ret
= jpeg2000_read_main_headers(s
))
1309 /* get picture buffer */
1310 if ((ret
= ff_thread_get_buffer(avctx
, &frame
, 0)) < 0) {
1311 av_log(avctx
, AV_LOG_ERROR
, "ff_thread_get_buffer() failed.\n");
1314 picture
->pict_type
= AV_PICTURE_TYPE_I
;
1315 picture
->key_frame
= 1;
1317 if (ret
= jpeg2000_read_bitstream_packets(s
))
1319 for (tileno
= 0; tileno
< s
->numXtiles
* s
->numYtiles
; tileno
++)
1320 if (ret
= jpeg2000_decode_tile(s
, s
->tile
+ tileno
, picture
))
1325 return bytestream2_tell(&s
->g
);
1328 jpeg2000_dec_cleanup(s
);
1332 static void jpeg2000_init_static_data(AVCodec
*codec
)
1334 ff_jpeg2000_init_tier1_luts();
1337 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1338 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1340 static const AVOption options
[] = {
1341 { "lowres", "Lower the decoding resolution by a power of two",
1342 OFFSET(lowres
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, JPEG2000_MAX_RESLEVELS
- 1, VD
},
1346 static const AVProfile profiles
[] = {
1347 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0
, "JPEG 2000 codestream restriction 0" },
1348 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1
, "JPEG 2000 codestream restriction 1" },
1349 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION
, "JPEG 2000 no codestream restrictions" },
1350 { FF_PROFILE_JPEG2000_DCINEMA_2K
, "JPEG 2000 digital cinema 2K" },
1351 { FF_PROFILE_JPEG2000_DCINEMA_4K
, "JPEG 2000 digital cinema 4K" },
1352 { FF_PROFILE_UNKNOWN
},
1355 static const AVClass
class = {
1356 .class_name
= "jpeg2000",
1357 .item_name
= av_default_item_name
,
1359 .version
= LIBAVUTIL_VERSION_INT
,
1362 AVCodec ff_jpeg2000_decoder
= {
1364 .long_name
= NULL_IF_CONFIG_SMALL("JPEG 2000"),
1365 .type
= AVMEDIA_TYPE_VIDEO
,
1366 .id
= AV_CODEC_ID_JPEG2000
,
1367 .capabilities
= CODEC_CAP_FRAME_THREADS
,
1368 .priv_data_size
= sizeof(Jpeg2000DecoderContext
),
1369 .init_static_data
= jpeg2000_init_static_data
,
1370 .decode
= jpeg2000_decode_frame
,
1371 .priv_class
= &class,
1372 .pix_fmts
= (enum AVPixelFormat
[]) { AV_PIX_FMT_XYZ12
,
1375 .profiles
= NULL_IF_CONFIG_SMALL(profiles
)