2 * JPEG 2000 image decoder
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * JPEG 2000 image decoder
30 #include "libavutil/avassert.h"
31 #include "libavutil/common.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
35 #include "bytestream.h"
40 #define JP2_SIG_TYPE 0x6A502020
41 #define JP2_SIG_VALUE 0x0D0A870A
42 #define JP2_CODESTREAM 0x6A703263
43 #define JP2_HEADER 0x6A703268
48 typedef struct Jpeg2000TilePart {
49 uint8_t tile_index; // Tile index who refers the tile-part
50 const uint8_t *tp_end;
51 GetByteContext tpg; // bit stream in tile-part
54 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
55 * one per component, so tile_part elements have a size of 3 */
56 typedef struct Jpeg2000Tile {
57 Jpeg2000Component *comp;
58 uint8_t properties[4];
59 Jpeg2000CodingStyle codsty[4];
60 Jpeg2000QuantStyle qntsty[4];
61 Jpeg2000TilePart tile_part[4];
62 uint16_t tp_idx; // Tile-part index
65 typedef struct Jpeg2000DecoderContext {
67 AVCodecContext *avctx;
71 int image_offset_x, image_offset_y;
72 int tile_offset_x, tile_offset_y;
73 uint8_t cbps[4]; // bits per sample in particular components
74 uint8_t sgnd[4]; // if a component is signed
75 uint8_t properties[4];
80 uint32_t palette[256];
83 int tile_width, tile_height;
84 unsigned numXtiles, numYtiles;
87 Jpeg2000CodingStyle codsty[4];
88 Jpeg2000QuantStyle qntsty[4];
96 /*options parameters*/
98 } Jpeg2000DecoderContext;
100 /* get_bits functions for JPEG2000 packet bitstream
101 * It is a get_bit function with a bit-stuffing routine. If the value of the
102 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
103 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
104 static int get_bits(Jpeg2000DecoderContext *s, int n)
110 if (s->bit_index == 0) {
111 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
114 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
119 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
121 if (bytestream2_get_byte(&s->g) == 0xff)
122 bytestream2_skip(&s->g, 1);
126 /* decode the value stored in node */
127 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
130 Jpeg2000TgtNode *stack[30];
131 int sp = -1, curval = 0;
134 return AVERROR_INVALIDDATA;
136 while (node && !node->vis) {
144 curval = stack[sp]->val;
146 while (curval < threshold && sp >= 0) {
147 if (curval < stack[sp]->val)
148 curval = stack[sp]->val;
149 while (curval < threshold) {
151 if ((ret = get_bits(s, 1)) > 0) {
159 stack[sp]->val = curval;
165 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
166 int bpc, uint32_t log2_chroma_wh, int pal8)
169 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
171 if (desc->nb_components != components) {
175 switch (components) {
177 match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
178 (log2_chroma_wh >> 14 & 3) == 0 &&
179 (log2_chroma_wh >> 12 & 3) == 0;
181 match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
182 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
183 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
185 match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
186 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
187 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
190 match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
191 (log2_chroma_wh >> 2 & 3) == 0 &&
192 (log2_chroma_wh & 3) == 0 &&
193 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
198 // pix_fmts with lower bpp have to be listed before
199 // similar pix_fmts with higher bpp.
200 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
201 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
202 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
203 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
204 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
205 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
206 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
207 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
208 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
209 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
210 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
211 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
212 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
213 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
215 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
216 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
217 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
218 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS};
219 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
224 /* marker segments */
225 /* get sizes and offsets of image, tiles; number of components */
226 static int get_siz(Jpeg2000DecoderContext *s)
230 uint32_t log2_chroma_wh = 0;
231 const enum AVPixelFormat *possible_fmts = NULL;
232 int possible_fmts_nb = 0;
234 if (bytestream2_get_bytes_left(&s->g) < 36)
235 return AVERROR_INVALIDDATA;
237 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
238 s->width = bytestream2_get_be32u(&s->g); // Width
239 s->height = bytestream2_get_be32u(&s->g); // Height
240 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
241 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
242 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
243 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
244 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
245 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
246 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
248 if (s->image_offset_x || s->image_offset_y) {
249 avpriv_request_sample(s->avctx, "Support for image offsets");
250 return AVERROR_PATCHWELCOME;
253 if (ncomponents <= 0) {
254 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
256 return AVERROR_INVALIDDATA;
259 if (ncomponents > 4) {
260 avpriv_request_sample(s->avctx, "Support for %d components",
262 return AVERROR_PATCHWELCOME;
265 s->ncomponents = ncomponents;
267 if (s->tile_width <= 0 || s->tile_height <= 0) {
268 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
269 s->tile_width, s->tile_height);
270 return AVERROR_INVALIDDATA;
273 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
274 return AVERROR_INVALIDDATA;
276 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
277 uint8_t x = bytestream2_get_byteu(&s->g);
278 s->cbps[i] = (x & 0x7f) + 1;
279 s->precision = FFMAX(s->cbps[i], s->precision);
280 s->sgnd[i] = !!(x & 0x80);
281 s->cdx[i] = bytestream2_get_byteu(&s->g);
282 s->cdy[i] = bytestream2_get_byteu(&s->g);
283 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
284 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
285 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
286 return AVERROR_INVALIDDATA;
288 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
291 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
292 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
294 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
295 s->numXtiles = s->numYtiles = 0;
296 return AVERROR(EINVAL);
299 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
301 s->numXtiles = s->numYtiles = 0;
302 return AVERROR(ENOMEM);
305 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
306 Jpeg2000Tile *tile = s->tile + i;
308 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
310 return AVERROR(ENOMEM);
313 /* compute image size with reduction factor */
314 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
315 s->reduction_factor);
316 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
317 s->reduction_factor);
319 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
320 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
321 possible_fmts = xyz_pix_fmts;
322 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
324 switch (s->colour_space) {
326 possible_fmts = rgb_pix_fmts;
327 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
330 possible_fmts = gray_pix_fmts;
331 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
334 possible_fmts = yuv_pix_fmts;
335 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
338 possible_fmts = all_pix_fmts;
339 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
343 for (i = 0; i < possible_fmts_nb; ++i) {
344 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
345 s->avctx->pix_fmt = possible_fmts[i];
349 if (i == possible_fmts_nb) {
350 av_log(s->avctx, AV_LOG_ERROR,
351 "Unknown pix_fmt, profile: %d, colour_space: %d, "
352 "components: %d, precision: %d, "
353 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
354 s->avctx->profile, s->colour_space, ncomponents, s->precision,
355 ncomponents > 2 ? s->cdx[1] : 0,
356 ncomponents > 2 ? s->cdy[1] : 0,
357 ncomponents > 2 ? s->cdx[2] : 0,
358 ncomponents > 2 ? s->cdy[2] : 0);
359 return AVERROR_PATCHWELCOME;
361 s->avctx->bits_per_raw_sample = s->precision;
365 /* get common part for COD and COC segments */
366 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
370 if (bytestream2_get_bytes_left(&s->g) < 5)
371 return AVERROR_INVALIDDATA;
373 /* nreslevels = number of resolution levels
374 = number of decomposition level +1 */
375 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
376 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
377 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
378 return AVERROR_INVALIDDATA;
381 if (c->nreslevels <= s->reduction_factor) {
382 /* we are forced to update reduction_factor as its requested value is
383 not compatible with this bitstream, and as we might have used it
384 already in setup earlier we have to fail this frame until
385 reinitialization is implemented */
386 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
387 s->reduction_factor = c->nreslevels - 1;
388 return AVERROR(EINVAL);
391 /* compute number of resolution levels to decode */
392 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
394 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
395 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
397 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
398 c->log2_cblk_width + c->log2_cblk_height > 12) {
399 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
400 return AVERROR_INVALIDDATA;
403 if (c->log2_cblk_width > 6 || c->log2_cblk_height > 6) {
404 avpriv_request_sample(s->avctx, "cblk size > 64");
405 return AVERROR_PATCHWELCOME;
408 c->cblk_style = bytestream2_get_byteu(&s->g);
409 if (c->cblk_style != 0) { // cblk style
410 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
412 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
413 /* set integer 9/7 DWT in case of BITEXACT flag */
414 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
415 c->transform = FF_DWT97_INT;
417 if (c->csty & JPEG2000_CSTY_PREC) {
419 for (i = 0; i < c->nreslevels; i++) {
420 byte = bytestream2_get_byte(&s->g);
421 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
422 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
425 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
426 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
431 /* get coding parameters for a particular tile or whole image*/
432 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
435 Jpeg2000CodingStyle tmp;
438 if (bytestream2_get_bytes_left(&s->g) < 5)
439 return AVERROR_INVALIDDATA;
441 tmp.csty = bytestream2_get_byteu(&s->g);
443 // get progression order
444 tmp.prog_order = bytestream2_get_byteu(&s->g);
446 tmp.nlayers = bytestream2_get_be16u(&s->g);
447 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
449 if (tmp.mct && s->ncomponents < 3) {
450 av_log(s->avctx, AV_LOG_ERROR,
451 "MCT %"PRIu8" with too few components (%d)\n",
452 tmp.mct, s->ncomponents);
453 return AVERROR_INVALIDDATA;
456 if ((ret = get_cox(s, &tmp)) < 0)
459 for (compno = 0; compno < s->ncomponents; compno++)
460 if (!(properties[compno] & HAD_COC))
461 memcpy(c + compno, &tmp, sizeof(tmp));
465 /* Get coding parameters for a component in the whole image or a
466 * particular tile. */
467 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
472 if (bytestream2_get_bytes_left(&s->g) < 2)
473 return AVERROR_INVALIDDATA;
475 compno = bytestream2_get_byteu(&s->g);
477 if (compno >= s->ncomponents) {
478 av_log(s->avctx, AV_LOG_ERROR,
479 "Invalid compno %d. There are %d components in the image.\n",
480 compno, s->ncomponents);
481 return AVERROR_INVALIDDATA;
485 c->csty = bytestream2_get_byteu(&s->g);
487 if ((ret = get_cox(s, c)) < 0)
490 properties[compno] |= HAD_COC;
494 /* Get common part for QCD and QCC segments. */
495 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
499 if (bytestream2_get_bytes_left(&s->g) < 1)
500 return AVERROR_INVALIDDATA;
502 x = bytestream2_get_byteu(&s->g); // Sqcd
504 q->nguardbits = x >> 5;
505 q->quantsty = x & 0x1f;
507 if (q->quantsty == JPEG2000_QSTY_NONE) {
509 if (bytestream2_get_bytes_left(&s->g) < n ||
510 n > JPEG2000_MAX_DECLEVELS*3)
511 return AVERROR_INVALIDDATA;
512 for (i = 0; i < n; i++)
513 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
514 } else if (q->quantsty == JPEG2000_QSTY_SI) {
515 if (bytestream2_get_bytes_left(&s->g) < 2)
516 return AVERROR_INVALIDDATA;
517 x = bytestream2_get_be16u(&s->g);
518 q->expn[0] = x >> 11;
519 q->mant[0] = x & 0x7ff;
520 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
521 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
522 q->expn[i] = curexpn;
523 q->mant[i] = q->mant[0];
527 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
528 n > JPEG2000_MAX_DECLEVELS*3)
529 return AVERROR_INVALIDDATA;
530 for (i = 0; i < n; i++) {
531 x = bytestream2_get_be16u(&s->g);
532 q->expn[i] = x >> 11;
533 q->mant[i] = x & 0x7ff;
539 /* Get quantization parameters for a particular tile or a whole image. */
540 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
543 Jpeg2000QuantStyle tmp;
546 memset(&tmp, 0, sizeof(tmp));
548 if ((ret = get_qcx(s, n, &tmp)) < 0)
550 for (compno = 0; compno < s->ncomponents; compno++)
551 if (!(properties[compno] & HAD_QCC))
552 memcpy(q + compno, &tmp, sizeof(tmp));
556 /* Get quantization parameters for a component in the whole image
557 * on in a particular tile. */
558 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
563 if (bytestream2_get_bytes_left(&s->g) < 1)
564 return AVERROR_INVALIDDATA;
566 compno = bytestream2_get_byteu(&s->g);
568 if (compno >= s->ncomponents) {
569 av_log(s->avctx, AV_LOG_ERROR,
570 "Invalid compno %d. There are %d components in the image.\n",
571 compno, s->ncomponents);
572 return AVERROR_INVALIDDATA;
575 properties[compno] |= HAD_QCC;
576 return get_qcx(s, n - 1, q + compno);
579 /* Get start of tile segment. */
580 static int get_sot(Jpeg2000DecoderContext *s, int n)
582 Jpeg2000TilePart *tp;
587 if (bytestream2_get_bytes_left(&s->g) < 8)
588 return AVERROR_INVALIDDATA;
591 Isot = bytestream2_get_be16u(&s->g); // Isot
592 if (Isot >= s->numXtiles * s->numYtiles)
593 return AVERROR_INVALIDDATA;
596 Psot = bytestream2_get_be32u(&s->g); // Psot
597 TPsot = bytestream2_get_byteu(&s->g); // TPsot
599 /* Read TNSot but not used */
600 bytestream2_get_byteu(&s->g); // TNsot
602 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
603 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
604 return AVERROR_INVALIDDATA;
607 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
608 avpriv_request_sample(s->avctx, "Support for %"PRIu8" components", TPsot);
609 return AVERROR_PATCHWELCOME;
612 s->tile[Isot].tp_idx = TPsot;
613 tp = s->tile[Isot].tile_part + TPsot;
614 tp->tile_index = Isot;
615 tp->tp_end = s->g.buffer + Psot - n - 2;
618 Jpeg2000Tile *tile = s->tile + s->curtileno;
621 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
622 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
628 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
629 * Used to know the number of tile parts and lengths.
630 * There may be multiple TLMs in the header.
631 * TODO: The function is not used for tile-parts management, nor anywhere else.
632 * It can be useful to allocate memory for tile parts, before managing the SOT
633 * markers. Parsing the TLM header is needed to increment the input header
635 * This marker is mandatory for DCI. */
636 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
638 uint8_t Stlm, ST, SP, tile_tlm, i;
639 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
640 Stlm = bytestream2_get_byte(&s->g);
642 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
643 ST = (Stlm >> 4) & 0x03;
644 // TODO: Manage case of ST = 0b11 --> raise error
645 SP = (Stlm >> 6) & 0x01;
646 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
647 for (i = 0; i < tile_tlm; i++) {
652 bytestream2_get_byte(&s->g);
655 bytestream2_get_be16(&s->g);
658 bytestream2_get_be32(&s->g);
662 bytestream2_get_be16(&s->g);
664 bytestream2_get_be32(&s->g);
670 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
673 int tilex = tileno % s->numXtiles;
674 int tiley = tileno / s->numXtiles;
675 Jpeg2000Tile *tile = s->tile + tileno;
678 return AVERROR(ENOMEM);
680 for (compno = 0; compno < s->ncomponents; compno++) {
681 Jpeg2000Component *comp = tile->comp + compno;
682 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
683 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
684 int ret; // global bandno
686 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
687 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
688 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
689 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
691 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
692 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
693 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
694 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
696 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
697 s->cbps[compno], s->cdx[compno],
698 s->cdy[compno], s->avctx))
704 /* Read the number of coding passes. */
705 static int getnpasses(Jpeg2000DecoderContext *s)
712 if ((num = get_bits(s, 2)) != 3)
713 return num < 0 ? num : 3 + num;
714 if ((num = get_bits(s, 5)) != 31)
715 return num < 0 ? num : 6 + num;
716 num = get_bits(s, 7);
717 return num < 0 ? num : 37 + num;
720 static int getlblockinc(Jpeg2000DecoderContext *s)
723 while (ret = get_bits(s, 1)) {
731 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
732 Jpeg2000CodingStyle *codsty,
733 Jpeg2000ResLevel *rlevel, int precno,
734 int layno, uint8_t *expn, int numgbits)
736 int bandno, cblkno, ret, nb_code_blocks;
738 if (!(ret = get_bits(s, 1))) {
744 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
745 Jpeg2000Band *band = rlevel->band + bandno;
746 Jpeg2000Prec *prec = band->prec + precno;
748 if (band->coord[0][0] == band->coord[0][1] ||
749 band->coord[1][0] == band->coord[1][1])
751 nb_code_blocks = prec->nb_codeblocks_height *
752 prec->nb_codeblocks_width;
753 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
754 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
755 int incl, newpasses, llen;
758 incl = get_bits(s, 1);
760 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
766 if (!cblk->npasses) {
767 int v = expn[bandno] + numgbits - 1 -
768 tag_tree_decode(s, prec->zerobits + cblkno, 100);
770 av_log(s->avctx, AV_LOG_ERROR,
771 "nonzerobits %d invalid\n", v);
772 return AVERROR_INVALIDDATA;
774 cblk->nonzerobits = v;
776 if ((newpasses = getnpasses(s)) < 0)
778 if ((llen = getlblockinc(s)) < 0)
780 cblk->lblock += llen;
781 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
783 if (ret > sizeof(cblk->data)) {
784 avpriv_request_sample(s->avctx,
785 "Block with lengthinc greater than %zu",
787 return AVERROR_PATCHWELCOME;
789 cblk->lengthinc = ret;
790 cblk->npasses += newpasses;
795 if (codsty->csty & JPEG2000_CSTY_EPH) {
796 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
797 bytestream2_skip(&s->g, 2);
799 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
802 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
803 Jpeg2000Band *band = rlevel->band + bandno;
804 Jpeg2000Prec *prec = band->prec + precno;
806 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
807 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
808 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
809 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
810 || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
812 av_log(s->avctx, AV_LOG_ERROR,
813 "Block length %"PRIu16" or lengthinc %d is too large\n",
814 cblk->length, cblk->lengthinc);
815 return AVERROR_INVALIDDATA;
818 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
819 cblk->length += cblk->lengthinc;
826 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
829 int layno, reslevelno, compno, precno, ok_reslevel;
833 switch (tile->codsty[0].prog_order) {
834 case JPEG2000_PGOD_RLCP:
835 avpriv_request_sample(s->avctx, "Progression order RLCP");
837 case JPEG2000_PGOD_LRCP:
838 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
840 for (reslevelno = 0; ok_reslevel; reslevelno++) {
842 for (compno = 0; compno < s->ncomponents; compno++) {
843 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
844 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
845 if (reslevelno < codsty->nreslevels) {
846 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
849 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
850 if ((ret = jpeg2000_decode_packet(s,
853 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
854 qntsty->nguardbits)) < 0)
862 case JPEG2000_PGOD_CPRL:
863 for (compno = 0; compno < s->ncomponents; compno++) {
864 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
865 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
867 /* Set bit stream buffer address according to tile-part.
868 * For DCinema one tile-part per component, so can be
869 * indexed by component. */
870 s->g = tile->tile_part[compno].tpg;
872 /* Position loop (y axis)
873 * TODO: Automate computing of step 256.
874 * Fixed here, but to be computed before entering here. */
875 for (y = 0; y < s->height; y += 256) {
876 /* Position loop (y axis)
877 * TODO: automate computing of step 256.
878 * Fixed here, but to be computed before entering here. */
879 for (x = 0; x < s->width; x += 256) {
880 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
882 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
883 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
885 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
886 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
889 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
890 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
893 // check if a precinct exists
894 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
895 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
896 precno = prcx + rlevel->num_precincts_x * prcy;
898 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y)
899 return AVERROR_PATCHWELCOME;
901 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
902 if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
904 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
905 qntsty->nguardbits)) < 0)
914 case JPEG2000_PGOD_RPCL:
915 avpriv_request_sample(s->avctx, "Progression order RPCL");
916 ret = AVERROR_PATCHWELCOME;
919 case JPEG2000_PGOD_PCRL:
920 avpriv_request_sample(s->avctx, "Progression order PCRL");
921 ret = AVERROR_PATCHWELCOME;
928 /* EOC marker reached */
929 bytestream2_skip(&s->g, 2);
934 /* TIER-1 routines */
935 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
936 int bpno, int bandno, int bpass_csty_symbol,
937 int vert_causal_ctx_csty_symbol)
939 int mask = 3 << (bpno - 1), y0, x, y;
941 for (y0 = 0; y0 < height; y0 += 4)
942 for (x = 0; x < width; x++)
943 for (y = y0; y < height && y < y0 + 4; y++) {
944 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
945 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
947 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
948 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
949 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
950 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
951 if (bpass_csty_symbol)
952 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
954 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
957 ff_jpeg2000_set_significance(t1, x, y,
960 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
965 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
971 phalf = 1 << (bpno - 1);
974 for (y0 = 0; y0 < height; y0 += 4)
975 for (x = 0; x < width; x++)
976 for (y = y0; y < height && y < y0 + 4; y++)
977 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
978 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
979 int r = ff_mqc_decode(&t1->mqc,
980 t1->mqc.cx_states + ctxno)
982 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
983 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
987 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
988 int width, int height, int bpno, int bandno,
989 int seg_symbols, int vert_causal_ctx_csty_symbol)
991 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
993 for (y0 = 0; y0 < height; y0 += 4) {
994 for (x = 0; x < width; x++) {
995 if (y0 + 3 < height &&
996 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
997 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
998 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
999 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
1000 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1002 runlen = ff_mqc_decode(&t1->mqc,
1003 t1->mqc.cx_states + MQC_CX_UNI);
1004 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1013 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1015 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1016 int flags_mask = -1;
1017 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1018 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
1019 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
1025 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
1027 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
1028 t1->mqc.cx_states + ctxno) ^
1031 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
1034 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
1040 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1041 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1042 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1043 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1045 av_log(s->avctx, AV_LOG_ERROR,
1046 "Segmentation symbol value incorrect\n");
1050 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1051 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1052 int width, int height, int bandpos)
1054 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
1055 int clnpass_cnt = 0;
1056 int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
1057 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1059 av_assert0(width <= JPEG2000_MAX_CBLKW);
1060 av_assert0(height <= JPEG2000_MAX_CBLKH);
1062 for (y = 0; y < height; y++)
1063 memset(t1->data[y], 0, width * sizeof(**t1->data));
1065 /* If code-block contains no compressed data: nothing to do. */
1069 for (y = 0; y < height + 2; y++)
1070 memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
1072 cblk->data[cblk->length] = 0xff;
1073 cblk->data[cblk->length+1] = 0xff;
1074 ff_mqc_initdec(&t1->mqc, cblk->data);
1079 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1080 bpass_csty_symbol && (clnpass_cnt >= 4),
1081 vert_causal_ctx_csty_symbol);
1084 decode_refpass(t1, width, height, bpno + 1);
1085 if (bpass_csty_symbol && clnpass_cnt >= 4)
1086 ff_mqc_initdec(&t1->mqc, cblk->data);
1089 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1090 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1091 vert_causal_ctx_csty_symbol);
1092 clnpass_cnt = clnpass_cnt + 1;
1093 if (bpass_csty_symbol && clnpass_cnt >= 4)
1094 ff_mqc_initdec(&t1->mqc, cblk->data);
1107 /* TODO: Verify dequantization for lossless case
1108 * comp->data can be float or int
1109 * band->stepsize can be float or int
1110 * depending on the type of DWT transformation.
1111 * see ISO/IEC 15444-1:2002 A.6.1 */
1113 /* Float dequantization of a codeblock.*/
1114 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1115 Jpeg2000Component *comp,
1116 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1119 int w = cblk->coord[0][1] - cblk->coord[0][0];
1120 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1121 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1122 int *src = t1->data[j];
1123 for (i = 0; i < w; ++i)
1124 datap[i] = src[i] * band->f_stepsize;
1128 /* Integer dequantization of a codeblock.*/
1129 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1130 Jpeg2000Component *comp,
1131 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1134 int w = cblk->coord[0][1] - cblk->coord[0][0];
1135 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1136 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1137 int *src = t1->data[j];
1138 for (i = 0; i < w; ++i)
1139 datap[i] = (src[i] * band->i_stepsize + (1 << 14)) >> 15;
1143 /* Inverse ICT parameters in float and integer.
1144 * int value = (float value) * (1<<16) */
1145 static const float f_ict_params[4] = {
1151 static const int i_ict_params[4] = {
1158 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1161 int32_t *src[3], i0, i1, i2;
1162 float *srcf[3], i0f, i1f, i2f;
1164 for (i = 1; i < 3; i++)
1165 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1166 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1170 for (i = 0; i < 3; i++)
1171 if (tile->codsty[0].transform == FF_DWT97)
1172 srcf[i] = tile->comp[i].f_data;
1174 src [i] = tile->comp[i].i_data;
1176 for (i = 0; i < 2; i++)
1177 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1179 switch (tile->codsty[0].transform) {
1181 for (i = 0; i < csize; i++) {
1182 i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
1183 i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
1184 - (f_ict_params[2] * *srcf[2]);
1185 i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
1192 for (i = 0; i < csize; i++) {
1193 i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1194 i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1195 - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
1196 i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1203 for (i = 0; i < csize; i++) {
1204 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1215 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1218 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1219 int compno, reslevelno, bandno;
1221 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
1222 int pixelsize = planar ? 1 : pixdesc->nb_components;
1225 Jpeg2000T1Context t1;
1227 /* Loop on tile components */
1228 for (compno = 0; compno < s->ncomponents; compno++) {
1229 Jpeg2000Component *comp = tile->comp + compno;
1230 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1232 /* Loop on resolution levels */
1233 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1234 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1236 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1237 int nb_precincts, precno;
1238 Jpeg2000Band *band = rlevel->band + bandno;
1239 int cblkno = 0, bandpos;
1241 bandpos = bandno + (reslevelno > 0);
1243 if (band->coord[0][0] == band->coord[0][1] ||
1244 band->coord[1][0] == band->coord[1][1])
1247 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1248 /* Loop on precincts */
1249 for (precno = 0; precno < nb_precincts; precno++) {
1250 Jpeg2000Prec *prec = band->prec + precno;
1252 /* Loop on codeblocks */
1253 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1255 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1256 decode_cblk(s, codsty, &t1, cblk,
1257 cblk->coord[0][1] - cblk->coord[0][0],
1258 cblk->coord[1][1] - cblk->coord[1][0],
1261 x = cblk->coord[0][0];
1262 y = cblk->coord[1][0];
1264 if (codsty->transform == FF_DWT97)
1265 dequantization_float(x, y, cblk, comp, &t1, band);
1267 dequantization_int(x, y, cblk, comp, &t1, band);
1271 } /* end reslevel */
1274 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1277 /* inverse MCT transformation */
1278 if (tile->codsty[0].mct)
1279 mct_decode(s, tile);
1281 if (s->cdef[0] < 0) {
1282 for (x = 0; x < s->ncomponents; x++)
1284 if ((s->ncomponents & 1) == 0)
1285 s->cdef[s->ncomponents-1] = 0;
1288 if (s->precision <= 8) {
1289 for (compno = 0; compno < s->ncomponents; compno++) {
1290 Jpeg2000Component *comp = tile->comp + compno;
1291 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1292 float *datap = comp->f_data;
1293 int32_t *i_datap = comp->i_data;
1294 int cbps = s->cbps[compno];
1295 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1299 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1302 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1303 line = picture->data[plane] + y / s->cdy[compno] * picture->linesize[plane];
1304 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1307 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1308 dst = line + x / s->cdx[compno] * pixelsize + compno*!planar;
1310 if (codsty->transform == FF_DWT97) {
1311 for (; x < w; x += s->cdx[compno]) {
1312 int val = lrintf(*datap) + (1 << (cbps - 1));
1313 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1314 val = av_clip(val, 0, (1 << cbps) - 1);
1315 *dst = val << (8 - cbps);
1320 for (; x < w; x += s->cdx[compno]) {
1321 int val = *i_datap + (1 << (cbps - 1));
1322 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1323 val = av_clip(val, 0, (1 << cbps) - 1);
1324 *dst = val << (8 - cbps);
1329 line += picture->linesize[plane];
1333 for (compno = 0; compno < s->ncomponents; compno++) {
1334 Jpeg2000Component *comp = tile->comp + compno;
1335 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1336 float *datap = comp->f_data;
1337 int32_t *i_datap = comp->i_data;
1339 int cbps = s->cbps[compno];
1340 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1344 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1346 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1347 linel = (uint16_t *)picture->data[plane] + y / s->cdy[compno] * (picture->linesize[plane] >> 1);
1348 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1351 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1352 dst = linel + (x / s->cdx[compno] * pixelsize + compno*!planar);
1353 if (codsty->transform == FF_DWT97) {
1354 for (; x < w; x += s-> cdx[compno]) {
1355 int val = lrintf(*datap) + (1 << (cbps - 1));
1356 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1357 val = av_clip(val, 0, (1 << cbps) - 1);
1358 /* align 12 bit values in little-endian mode */
1359 *dst = val << (16 - cbps);
1364 for (; x < w; x += s-> cdx[compno]) {
1365 int val = *i_datap + (1 << (cbps - 1));
1366 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1367 val = av_clip(val, 0, (1 << cbps) - 1);
1368 /* align 12 bit values in little-endian mode */
1369 *dst = val << (16 - cbps);
1374 linel += picture->linesize[plane] >> 1;
1382 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1385 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1386 if (s->tile[tileno].comp) {
1387 for (compno = 0; compno < s->ncomponents; compno++) {
1388 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1389 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1391 ff_jpeg2000_cleanup(comp, codsty);
1393 av_freep(&s->tile[tileno].comp);
1397 memset(s->codsty, 0, sizeof(s->codsty));
1398 memset(s->qntsty, 0, sizeof(s->qntsty));
1399 s->numXtiles = s->numYtiles = 0;
1402 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1404 Jpeg2000CodingStyle *codsty = s->codsty;
1405 Jpeg2000QuantStyle *qntsty = s->qntsty;
1406 uint8_t *properties = s->properties;
1413 if (bytestream2_get_bytes_left(&s->g) < 2) {
1414 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1418 marker = bytestream2_get_be16u(&s->g);
1419 oldpos = bytestream2_tell(&s->g);
1421 if (marker == JPEG2000_SOD) {
1423 Jpeg2000TilePart *tp;
1426 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1427 return AVERROR_INVALIDDATA;
1429 if (s->curtileno < 0) {
1430 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1431 return AVERROR_INVALIDDATA;
1434 tile = s->tile + s->curtileno;
1435 tp = tile->tile_part + tile->tp_idx;
1436 if (tp->tp_end < s->g.buffer) {
1437 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1438 return AVERROR_INVALIDDATA;
1440 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1441 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1445 if (marker == JPEG2000_EOC)
1448 len = bytestream2_get_be16(&s->g);
1449 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1450 return AVERROR_INVALIDDATA;
1456 s->numXtiles = s->numYtiles = 0;
1459 ret = get_coc(s, codsty, properties);
1462 ret = get_cod(s, codsty, properties);
1465 ret = get_qcc(s, len, qntsty, properties);
1468 ret = get_qcd(s, len, qntsty, properties);
1471 if (!(ret = get_sot(s, len))) {
1472 av_assert1(s->curtileno >= 0);
1473 codsty = s->tile[s->curtileno].codsty;
1474 qntsty = s->tile[s->curtileno].qntsty;
1475 properties = s->tile[s->curtileno].properties;
1479 // the comment is ignored
1480 bytestream2_skip(&s->g, len - 2);
1483 // Tile-part lengths
1484 ret = get_tlm(s, len);
1487 av_log(s->avctx, AV_LOG_ERROR,
1488 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1489 marker, bytestream2_tell(&s->g) - 4);
1490 bytestream2_skip(&s->g, len - 2);
1493 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1494 av_log(s->avctx, AV_LOG_ERROR,
1495 "error during processing marker segment %.4"PRIx16"\n",
1497 return ret ? ret : -1;
1503 /* Read bit stream packets --> T2 operation. */
1504 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1509 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1510 Jpeg2000Tile *tile = s->tile + tileno;
1512 if (ret = init_tile(s, tileno))
1515 s->g = tile->tile_part[0].tpg;
1516 if (ret = jpeg2000_decode_packets(s, tile))
1523 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1525 uint32_t atom_size, atom, atom_end;
1526 int search_range = 10;
1530 bytestream2_get_bytes_left(&s->g) >= 8) {
1531 atom_size = bytestream2_get_be32u(&s->g);
1532 atom = bytestream2_get_be32u(&s->g);
1533 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
1535 if (atom == JP2_CODESTREAM)
1538 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
1541 if (atom == JP2_HEADER &&
1543 uint32_t atom2_size, atom2, atom2_end;
1545 atom2_size = bytestream2_get_be32u(&s->g);
1546 atom2 = bytestream2_get_be32u(&s->g);
1547 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
1548 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1550 if (atom2 == JP2_CODESTREAM) {
1552 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
1553 int method = bytestream2_get_byteu(&s->g);
1554 bytestream2_skipu(&s->g, 2);
1556 s->colour_space = bytestream2_get_be32u(&s->g);
1558 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
1559 int i, size, colour_count, colour_channels, colour_depth[3];
1561 colour_count = bytestream2_get_be16u(&s->g);
1562 colour_channels = bytestream2_get_byteu(&s->g);
1563 // FIXME: Do not ignore channel_sign
1564 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1565 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1566 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1567 size = (colour_depth[0] + 7 >> 3) * colour_count +
1568 (colour_depth[1] + 7 >> 3) * colour_count +
1569 (colour_depth[2] + 7 >> 3) * colour_count;
1570 if (colour_count > 256 ||
1571 colour_channels != 3 ||
1572 colour_depth[0] > 16 ||
1573 colour_depth[1] > 16 ||
1574 colour_depth[2] > 16 ||
1575 atom2_size < size) {
1576 avpriv_request_sample(s->avctx, "Unknown palette");
1577 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1581 for (i = 0; i < colour_count; i++) {
1582 if (colour_depth[0] <= 8) {
1583 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
1584 r |= r >> colour_depth[0];
1586 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
1588 if (colour_depth[1] <= 8) {
1589 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
1590 r |= r >> colour_depth[1];
1592 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
1594 if (colour_depth[2] <= 8) {
1595 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
1596 r |= r >> colour_depth[2];
1598 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
1600 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
1602 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
1603 int n = bytestream2_get_be16u(&s->g);
1605 int cn = bytestream2_get_be16(&s->g);
1606 int av_unused typ = bytestream2_get_be16(&s->g);
1607 int asoc = bytestream2_get_be16(&s->g);
1608 if (cn < 4 || asoc < 4)
1612 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1613 } while (atom_end - atom2_end >= 8);
1617 bytestream2_seek(&s->g, atom_end, SEEK_SET);
1623 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1624 int *got_frame, AVPacket *avpkt)
1626 Jpeg2000DecoderContext *s = avctx->priv_data;
1627 ThreadFrame frame = { .f = data };
1628 AVFrame *picture = data;
1632 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1634 memset(s->cdef, -1, sizeof(s->cdef));
1636 if (bytestream2_get_bytes_left(&s->g) < 2) {
1637 ret = AVERROR_INVALIDDATA;
1641 // check if the image is in jp2 format
1642 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1643 (bytestream2_get_be32u(&s->g) == 12) &&
1644 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1645 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1646 if (!jp2_find_codestream(s)) {
1647 av_log(avctx, AV_LOG_ERROR,
1648 "Could not find Jpeg2000 codestream atom.\n");
1649 ret = AVERROR_INVALIDDATA;
1653 bytestream2_seek(&s->g, 0, SEEK_SET);
1656 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
1657 bytestream2_skip(&s->g, 1);
1659 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1660 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1661 ret = AVERROR_INVALIDDATA;
1664 if (ret = jpeg2000_read_main_headers(s))
1667 /* get picture buffer */
1668 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1670 picture->pict_type = AV_PICTURE_TYPE_I;
1671 picture->key_frame = 1;
1673 if (ret = jpeg2000_read_bitstream_packets(s))
1676 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1677 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1680 jpeg2000_dec_cleanup(s);
1684 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
1685 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
1687 return bytestream2_tell(&s->g);
1690 jpeg2000_dec_cleanup(s);
1694 static void jpeg2000_init_static_data(AVCodec *codec)
1696 ff_jpeg2000_init_tier1_luts();
1697 ff_mqc_init_context_tables();
1700 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1701 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1703 static const AVOption options[] = {
1704 { "lowres", "Lower the decoding resolution by a power of two",
1705 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1709 static const AVProfile profiles[] = {
1710 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1711 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1712 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1713 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1714 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1715 { FF_PROFILE_UNKNOWN },
1718 static const AVClass jpeg2000_class = {
1719 .class_name = "jpeg2000",
1720 .item_name = av_default_item_name,
1722 .version = LIBAVUTIL_VERSION_INT,
1725 AVCodec ff_jpeg2000_decoder = {
1727 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1728 .type = AVMEDIA_TYPE_VIDEO,
1729 .id = AV_CODEC_ID_JPEG2000,
1730 .capabilities = CODEC_CAP_FRAME_THREADS,
1731 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1732 .init_static_data = jpeg2000_init_static_data,
1733 .decode = jpeg2000_decode_frame,
1734 .priv_class = &jpeg2000_class,
1736 .profiles = NULL_IF_CONFIG_SMALL(profiles)