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/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/common.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
36 #include "bytestream.h"
41 #define JP2_SIG_TYPE 0x6A502020
42 #define JP2_SIG_VALUE 0x0D0A870A
43 #define JP2_CODESTREAM 0x6A703263
44 #define JP2_HEADER 0x6A703268
49 typedef struct Jpeg2000TilePart {
50 uint8_t tile_index; // Tile index who refers the tile-part
51 const uint8_t *tp_end;
52 GetByteContext tpg; // bit stream in tile-part
55 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
56 * one per component, so tile_part elements have a size of 3 */
57 typedef struct Jpeg2000Tile {
58 Jpeg2000Component *comp;
59 uint8_t properties[4];
60 Jpeg2000CodingStyle codsty[4];
61 Jpeg2000QuantStyle qntsty[4];
62 Jpeg2000TilePart tile_part[4];
63 uint16_t tp_idx; // Tile-part index
66 typedef struct Jpeg2000DecoderContext {
68 AVCodecContext *avctx;
72 int image_offset_x, image_offset_y;
73 int tile_offset_x, tile_offset_y;
74 uint8_t cbps[4]; // bits per sample in particular components
75 uint8_t sgnd[4]; // if a component is signed
76 uint8_t properties[4];
81 uint32_t palette[256];
84 int tile_width, tile_height;
85 unsigned numXtiles, numYtiles;
88 Jpeg2000CodingStyle codsty[4];
89 Jpeg2000QuantStyle qntsty[4];
97 /*options parameters*/
99 } Jpeg2000DecoderContext;
101 /* get_bits functions for JPEG2000 packet bitstream
102 * It is a get_bit function with a bit-stuffing routine. If the value of the
103 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
104 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
105 static int get_bits(Jpeg2000DecoderContext *s, int n)
111 if (s->bit_index == 0) {
112 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
115 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
120 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
122 if (bytestream2_get_byte(&s->g) == 0xff)
123 bytestream2_skip(&s->g, 1);
127 /* decode the value stored in node */
128 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
131 Jpeg2000TgtNode *stack[30];
132 int sp = -1, curval = 0;
135 return AVERROR_INVALIDDATA;
137 while (node && !node->vis) {
145 curval = stack[sp]->val;
147 while (curval < threshold && sp >= 0) {
148 if (curval < stack[sp]->val)
149 curval = stack[sp]->val;
150 while (curval < threshold) {
152 if ((ret = get_bits(s, 1)) > 0) {
160 stack[sp]->val = curval;
166 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
167 int bpc, uint32_t log2_chroma_wh, int pal8)
170 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
172 if (desc->nb_components != components) {
176 switch (components) {
178 match = match && desc->comp[3].depth_minus1 + 1 >= bpc &&
179 (log2_chroma_wh >> 14 & 3) == 0 &&
180 (log2_chroma_wh >> 12 & 3) == 0;
182 match = match && desc->comp[2].depth_minus1 + 1 >= bpc &&
183 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
184 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
186 match = match && desc->comp[1].depth_minus1 + 1 >= bpc &&
187 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
188 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
191 match = match && desc->comp[0].depth_minus1 + 1 >= bpc &&
192 (log2_chroma_wh >> 2 & 3) == 0 &&
193 (log2_chroma_wh & 3) == 0 &&
194 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
199 // pix_fmts with lower bpp have to be listed before
200 // similar pix_fmts with higher bpp.
201 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
202 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
203 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
204 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
205 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
206 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
207 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
208 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
209 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
210 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
211 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
212 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
213 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
214 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
216 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
217 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
218 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
219 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS};
220 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
225 /* marker segments */
226 /* get sizes and offsets of image, tiles; number of components */
227 static int get_siz(Jpeg2000DecoderContext *s)
231 uint32_t log2_chroma_wh = 0;
232 const enum AVPixelFormat *possible_fmts = NULL;
233 int possible_fmts_nb = 0;
235 if (bytestream2_get_bytes_left(&s->g) < 36)
236 return AVERROR_INVALIDDATA;
238 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
239 s->width = bytestream2_get_be32u(&s->g); // Width
240 s->height = bytestream2_get_be32u(&s->g); // Height
241 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
242 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
243 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
244 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
245 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
246 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
247 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
249 if (s->image_offset_x || s->image_offset_y) {
250 avpriv_request_sample(s->avctx, "Support for image offsets");
251 return AVERROR_PATCHWELCOME;
254 if (ncomponents <= 0) {
255 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
257 return AVERROR_INVALIDDATA;
260 if (ncomponents > 4) {
261 avpriv_request_sample(s->avctx, "Support for %d components",
263 return AVERROR_PATCHWELCOME;
266 s->ncomponents = ncomponents;
268 if (s->tile_width <= 0 || s->tile_height <= 0) {
269 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
270 s->tile_width, s->tile_height);
271 return AVERROR_INVALIDDATA;
274 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
275 return AVERROR_INVALIDDATA;
277 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
278 uint8_t x = bytestream2_get_byteu(&s->g);
279 s->cbps[i] = (x & 0x7f) + 1;
280 s->precision = FFMAX(s->cbps[i], s->precision);
281 s->sgnd[i] = !!(x & 0x80);
282 s->cdx[i] = bytestream2_get_byteu(&s->g);
283 s->cdy[i] = bytestream2_get_byteu(&s->g);
284 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
285 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
286 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
287 return AVERROR_INVALIDDATA;
289 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
292 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
293 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
295 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile)) {
296 s->numXtiles = s->numYtiles = 0;
297 return AVERROR(EINVAL);
300 s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
302 s->numXtiles = s->numYtiles = 0;
303 return AVERROR(ENOMEM);
306 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
307 Jpeg2000Tile *tile = s->tile + i;
309 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
311 return AVERROR(ENOMEM);
314 /* compute image size with reduction factor */
315 s->avctx->width = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
316 s->reduction_factor);
317 s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
318 s->reduction_factor);
320 if (s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_2K ||
321 s->avctx->profile == FF_PROFILE_JPEG2000_DCINEMA_4K) {
322 possible_fmts = xyz_pix_fmts;
323 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
325 switch (s->colour_space) {
327 possible_fmts = rgb_pix_fmts;
328 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
331 possible_fmts = gray_pix_fmts;
332 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
335 possible_fmts = yuv_pix_fmts;
336 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
339 possible_fmts = all_pix_fmts;
340 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
344 for (i = 0; i < possible_fmts_nb; ++i) {
345 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
346 s->avctx->pix_fmt = possible_fmts[i];
350 if (i == possible_fmts_nb) {
351 av_log(s->avctx, AV_LOG_ERROR,
352 "Unknown pix_fmt, profile: %d, colour_space: %d, "
353 "components: %d, precision: %d, "
354 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
355 s->avctx->profile, s->colour_space, ncomponents, s->precision,
356 ncomponents > 2 ? s->cdx[1] : 0,
357 ncomponents > 2 ? s->cdy[1] : 0,
358 ncomponents > 2 ? s->cdx[2] : 0,
359 ncomponents > 2 ? s->cdy[2] : 0);
360 return AVERROR_PATCHWELCOME;
362 s->avctx->bits_per_raw_sample = s->precision;
366 /* get common part for COD and COC segments */
367 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
371 if (bytestream2_get_bytes_left(&s->g) < 5)
372 return AVERROR_INVALIDDATA;
374 /* nreslevels = number of resolution levels
375 = number of decomposition level +1 */
376 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
377 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
378 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
379 return AVERROR_INVALIDDATA;
382 if (c->nreslevels <= s->reduction_factor) {
383 /* we are forced to update reduction_factor as its requested value is
384 not compatible with this bitstream, and as we might have used it
385 already in setup earlier we have to fail this frame until
386 reinitialization is implemented */
387 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
388 s->reduction_factor = c->nreslevels - 1;
389 return AVERROR(EINVAL);
392 /* compute number of resolution levels to decode */
393 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
395 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
396 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
398 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
399 c->log2_cblk_width + c->log2_cblk_height > 12) {
400 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
401 return AVERROR_INVALIDDATA;
404 if (c->log2_cblk_width > 6 || c->log2_cblk_height > 6) {
405 avpriv_request_sample(s->avctx, "cblk size > 64");
406 return AVERROR_PATCHWELCOME;
409 c->cblk_style = bytestream2_get_byteu(&s->g);
410 if (c->cblk_style != 0) { // cblk style
411 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
413 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
414 /* set integer 9/7 DWT in case of BITEXACT flag */
415 if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
416 c->transform = FF_DWT97_INT;
418 if (c->csty & JPEG2000_CSTY_PREC) {
420 for (i = 0; i < c->nreslevels; i++) {
421 byte = bytestream2_get_byte(&s->g);
422 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
423 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
426 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
427 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
432 /* get coding parameters for a particular tile or whole image*/
433 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
436 Jpeg2000CodingStyle tmp;
439 if (bytestream2_get_bytes_left(&s->g) < 5)
440 return AVERROR_INVALIDDATA;
442 tmp.csty = bytestream2_get_byteu(&s->g);
444 // get progression order
445 tmp.prog_order = bytestream2_get_byteu(&s->g);
447 tmp.nlayers = bytestream2_get_be16u(&s->g);
448 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
450 if (tmp.mct && s->ncomponents < 3) {
451 av_log(s->avctx, AV_LOG_ERROR,
452 "MCT %"PRIu8" with too few components (%d)\n",
453 tmp.mct, s->ncomponents);
454 return AVERROR_INVALIDDATA;
457 if ((ret = get_cox(s, &tmp)) < 0)
460 for (compno = 0; compno < s->ncomponents; compno++)
461 if (!(properties[compno] & HAD_COC))
462 memcpy(c + compno, &tmp, sizeof(tmp));
466 /* Get coding parameters for a component in the whole image or a
467 * particular tile. */
468 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
473 if (bytestream2_get_bytes_left(&s->g) < 2)
474 return AVERROR_INVALIDDATA;
476 compno = bytestream2_get_byteu(&s->g);
478 if (compno >= s->ncomponents) {
479 av_log(s->avctx, AV_LOG_ERROR,
480 "Invalid compno %d. There are %d components in the image.\n",
481 compno, s->ncomponents);
482 return AVERROR_INVALIDDATA;
486 c->csty = bytestream2_get_byteu(&s->g);
488 if ((ret = get_cox(s, c)) < 0)
491 properties[compno] |= HAD_COC;
495 /* Get common part for QCD and QCC segments. */
496 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
500 if (bytestream2_get_bytes_left(&s->g) < 1)
501 return AVERROR_INVALIDDATA;
503 x = bytestream2_get_byteu(&s->g); // Sqcd
505 q->nguardbits = x >> 5;
506 q->quantsty = x & 0x1f;
508 if (q->quantsty == JPEG2000_QSTY_NONE) {
510 if (bytestream2_get_bytes_left(&s->g) < n ||
511 n > JPEG2000_MAX_DECLEVELS*3)
512 return AVERROR_INVALIDDATA;
513 for (i = 0; i < n; i++)
514 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
515 } else if (q->quantsty == JPEG2000_QSTY_SI) {
516 if (bytestream2_get_bytes_left(&s->g) < 2)
517 return AVERROR_INVALIDDATA;
518 x = bytestream2_get_be16u(&s->g);
519 q->expn[0] = x >> 11;
520 q->mant[0] = x & 0x7ff;
521 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
522 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
523 q->expn[i] = curexpn;
524 q->mant[i] = q->mant[0];
528 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
529 n > JPEG2000_MAX_DECLEVELS*3)
530 return AVERROR_INVALIDDATA;
531 for (i = 0; i < n; i++) {
532 x = bytestream2_get_be16u(&s->g);
533 q->expn[i] = x >> 11;
534 q->mant[i] = x & 0x7ff;
540 /* Get quantization parameters for a particular tile or a whole image. */
541 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
544 Jpeg2000QuantStyle tmp;
547 memset(&tmp, 0, sizeof(tmp));
549 if ((ret = get_qcx(s, n, &tmp)) < 0)
551 for (compno = 0; compno < s->ncomponents; compno++)
552 if (!(properties[compno] & HAD_QCC))
553 memcpy(q + compno, &tmp, sizeof(tmp));
557 /* Get quantization parameters for a component in the whole image
558 * on in a particular tile. */
559 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
564 if (bytestream2_get_bytes_left(&s->g) < 1)
565 return AVERROR_INVALIDDATA;
567 compno = bytestream2_get_byteu(&s->g);
569 if (compno >= s->ncomponents) {
570 av_log(s->avctx, AV_LOG_ERROR,
571 "Invalid compno %d. There are %d components in the image.\n",
572 compno, s->ncomponents);
573 return AVERROR_INVALIDDATA;
576 properties[compno] |= HAD_QCC;
577 return get_qcx(s, n - 1, q + compno);
580 /* Get start of tile segment. */
581 static int get_sot(Jpeg2000DecoderContext *s, int n)
583 Jpeg2000TilePart *tp;
588 if (bytestream2_get_bytes_left(&s->g) < 8)
589 return AVERROR_INVALIDDATA;
592 Isot = bytestream2_get_be16u(&s->g); // Isot
593 if (Isot >= s->numXtiles * s->numYtiles)
594 return AVERROR_INVALIDDATA;
597 Psot = bytestream2_get_be32u(&s->g); // Psot
598 TPsot = bytestream2_get_byteu(&s->g); // TPsot
600 /* Read TNSot but not used */
601 bytestream2_get_byteu(&s->g); // TNsot
603 if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
604 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
605 return AVERROR_INVALIDDATA;
608 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
609 avpriv_request_sample(s->avctx, "Support for %"PRIu8" components", TPsot);
610 return AVERROR_PATCHWELCOME;
613 s->tile[Isot].tp_idx = TPsot;
614 tp = s->tile[Isot].tile_part + TPsot;
615 tp->tile_index = Isot;
616 tp->tp_end = s->g.buffer + Psot - n - 2;
619 Jpeg2000Tile *tile = s->tile + s->curtileno;
622 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
623 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
629 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
630 * Used to know the number of tile parts and lengths.
631 * There may be multiple TLMs in the header.
632 * TODO: The function is not used for tile-parts management, nor anywhere else.
633 * It can be useful to allocate memory for tile parts, before managing the SOT
634 * markers. Parsing the TLM header is needed to increment the input header
636 * This marker is mandatory for DCI. */
637 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
639 uint8_t Stlm, ST, SP, tile_tlm, i;
640 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
641 Stlm = bytestream2_get_byte(&s->g);
643 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
644 ST = (Stlm >> 4) & 0x03;
645 // TODO: Manage case of ST = 0b11 --> raise error
646 SP = (Stlm >> 6) & 0x01;
647 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
648 for (i = 0; i < tile_tlm; i++) {
653 bytestream2_get_byte(&s->g);
656 bytestream2_get_be16(&s->g);
659 bytestream2_get_be32(&s->g);
663 bytestream2_get_be16(&s->g);
665 bytestream2_get_be32(&s->g);
671 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
674 int tilex = tileno % s->numXtiles;
675 int tiley = tileno / s->numXtiles;
676 Jpeg2000Tile *tile = s->tile + tileno;
679 return AVERROR(ENOMEM);
681 for (compno = 0; compno < s->ncomponents; compno++) {
682 Jpeg2000Component *comp = tile->comp + compno;
683 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
684 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
685 int ret; // global bandno
687 comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
688 comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
689 comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
690 comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
692 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
693 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
694 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
695 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
697 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
698 s->cbps[compno], s->cdx[compno],
699 s->cdy[compno], s->avctx))
705 /* Read the number of coding passes. */
706 static int getnpasses(Jpeg2000DecoderContext *s)
713 if ((num = get_bits(s, 2)) != 3)
714 return num < 0 ? num : 3 + num;
715 if ((num = get_bits(s, 5)) != 31)
716 return num < 0 ? num : 6 + num;
717 num = get_bits(s, 7);
718 return num < 0 ? num : 37 + num;
721 static int getlblockinc(Jpeg2000DecoderContext *s)
724 while (ret = get_bits(s, 1)) {
732 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
733 Jpeg2000CodingStyle *codsty,
734 Jpeg2000ResLevel *rlevel, int precno,
735 int layno, uint8_t *expn, int numgbits)
737 int bandno, cblkno, ret, nb_code_blocks;
739 if (!(ret = get_bits(s, 1))) {
745 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
746 Jpeg2000Band *band = rlevel->band + bandno;
747 Jpeg2000Prec *prec = band->prec + precno;
749 if (band->coord[0][0] == band->coord[0][1] ||
750 band->coord[1][0] == band->coord[1][1])
752 nb_code_blocks = prec->nb_codeblocks_height *
753 prec->nb_codeblocks_width;
754 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
755 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
756 int incl, newpasses, llen;
759 incl = get_bits(s, 1);
761 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
767 if (!cblk->npasses) {
768 int v = expn[bandno] + numgbits - 1 -
769 tag_tree_decode(s, prec->zerobits + cblkno, 100);
771 av_log(s->avctx, AV_LOG_ERROR,
772 "nonzerobits %d invalid\n", v);
773 return AVERROR_INVALIDDATA;
775 cblk->nonzerobits = v;
777 if ((newpasses = getnpasses(s)) < 0)
779 if ((llen = getlblockinc(s)) < 0)
781 cblk->lblock += llen;
782 if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
784 if (ret > sizeof(cblk->data)) {
785 avpriv_request_sample(s->avctx,
786 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
788 return AVERROR_PATCHWELCOME;
790 cblk->lengthinc = ret;
791 cblk->npasses += newpasses;
796 if (codsty->csty & JPEG2000_CSTY_EPH) {
797 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
798 bytestream2_skip(&s->g, 2);
800 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
803 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
804 Jpeg2000Band *band = rlevel->band + bandno;
805 Jpeg2000Prec *prec = band->prec + precno;
807 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
808 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
809 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
810 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
811 || sizeof(cblk->data) < cblk->length + cblk->lengthinc + 2
813 av_log(s->avctx, AV_LOG_ERROR,
814 "Block length %"PRIu16" or lengthinc %d is too large\n",
815 cblk->length, cblk->lengthinc);
816 return AVERROR_INVALIDDATA;
819 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc);
820 cblk->length += cblk->lengthinc;
827 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
830 int layno, reslevelno, compno, precno, ok_reslevel;
834 switch (tile->codsty[0].prog_order) {
835 case JPEG2000_PGOD_RLCP:
836 avpriv_request_sample(s->avctx, "Progression order RLCP");
838 case JPEG2000_PGOD_LRCP:
839 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
841 for (reslevelno = 0; ok_reslevel; reslevelno++) {
843 for (compno = 0; compno < s->ncomponents; compno++) {
844 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
845 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
846 if (reslevelno < codsty->nreslevels) {
847 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
850 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
851 if ((ret = jpeg2000_decode_packet(s,
854 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
855 qntsty->nguardbits)) < 0)
863 case JPEG2000_PGOD_CPRL:
864 for (compno = 0; compno < s->ncomponents; compno++) {
865 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
866 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
868 /* Set bit stream buffer address according to tile-part.
869 * For DCinema one tile-part per component, so can be
870 * indexed by component. */
871 s->g = tile->tile_part[compno].tpg;
873 /* Position loop (y axis)
874 * TODO: Automate computing of step 256.
875 * Fixed here, but to be computed before entering here. */
876 for (y = 0; y < s->height; y += 256) {
877 /* Position loop (y axis)
878 * TODO: automate computing of step 256.
879 * Fixed here, but to be computed before entering here. */
880 for (x = 0; x < s->width; x += 256) {
881 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
883 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
884 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
886 if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
887 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
890 if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
891 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
894 // check if a precinct exists
895 prcx = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
896 prcy = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
897 precno = prcx + rlevel->num_precincts_x * prcy;
899 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y)
900 return AVERROR_PATCHWELCOME;
902 for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
903 if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
905 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
906 qntsty->nguardbits)) < 0)
915 case JPEG2000_PGOD_RPCL:
916 avpriv_request_sample(s->avctx, "Progression order RPCL");
917 ret = AVERROR_PATCHWELCOME;
920 case JPEG2000_PGOD_PCRL:
921 avpriv_request_sample(s->avctx, "Progression order PCRL");
922 ret = AVERROR_PATCHWELCOME;
929 /* EOC marker reached */
930 bytestream2_skip(&s->g, 2);
935 /* TIER-1 routines */
936 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
937 int bpno, int bandno, int bpass_csty_symbol,
938 int vert_causal_ctx_csty_symbol)
940 int mask = 3 << (bpno - 1), y0, x, y;
942 for (y0 = 0; y0 < height; y0 += 4)
943 for (x = 0; x < width; x++)
944 for (y = y0; y < height && y < y0 + 4; y++) {
945 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
946 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
948 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
949 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
950 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
951 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
952 if (bpass_csty_symbol)
953 t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
955 t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
958 ff_jpeg2000_set_significance(t1, x, y,
961 t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
966 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
972 phalf = 1 << (bpno - 1);
975 for (y0 = 0; y0 < height; y0 += 4)
976 for (x = 0; x < width; x++)
977 for (y = y0; y < height && y < y0 + 4; y++)
978 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
979 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
980 int r = ff_mqc_decode(&t1->mqc,
981 t1->mqc.cx_states + ctxno)
983 t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
984 t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
988 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
989 int width, int height, int bpno, int bandno,
990 int seg_symbols, int vert_causal_ctx_csty_symbol)
992 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
994 for (y0 = 0; y0 < height; y0 += 4) {
995 for (x = 0; x < width; x++) {
996 if (y0 + 3 < height &&
997 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
998 (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
999 (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1000 (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
1001 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1003 runlen = ff_mqc_decode(&t1->mqc,
1004 t1->mqc.cx_states + MQC_CX_UNI);
1005 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1014 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1016 if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1017 int flags_mask = -1;
1018 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1019 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
1020 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
1026 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
1028 t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
1029 t1->mqc.cx_states + ctxno) ^
1032 ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
1035 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
1041 val = 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);
1044 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1046 av_log(s->avctx, AV_LOG_ERROR,
1047 "Segmentation symbol value incorrect\n");
1051 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1052 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1053 int width, int height, int bandpos)
1055 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
1056 int clnpass_cnt = 0;
1057 int bpass_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
1058 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1060 av_assert0(width <= JPEG2000_MAX_CBLKW);
1061 av_assert0(height <= JPEG2000_MAX_CBLKH);
1063 for (y = 0; y < height; y++)
1064 memset(t1->data[y], 0, width * sizeof(**t1->data));
1066 /* If code-block contains no compressed data: nothing to do. */
1070 for (y = 0; y < height + 2; y++)
1071 memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
1073 cblk->data[cblk->length] = 0xff;
1074 cblk->data[cblk->length+1] = 0xff;
1075 ff_mqc_initdec(&t1->mqc, cblk->data);
1080 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1081 bpass_csty_symbol && (clnpass_cnt >= 4),
1082 vert_causal_ctx_csty_symbol);
1085 decode_refpass(t1, width, height, bpno + 1);
1086 if (bpass_csty_symbol && clnpass_cnt >= 4)
1087 ff_mqc_initdec(&t1->mqc, cblk->data);
1090 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1091 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1092 vert_causal_ctx_csty_symbol);
1093 clnpass_cnt = clnpass_cnt + 1;
1094 if (bpass_csty_symbol && clnpass_cnt >= 4)
1095 ff_mqc_initdec(&t1->mqc, cblk->data);
1108 /* TODO: Verify dequantization for lossless case
1109 * comp->data can be float or int
1110 * band->stepsize can be float or int
1111 * depending on the type of DWT transformation.
1112 * see ISO/IEC 15444-1:2002 A.6.1 */
1114 /* Float dequantization of a codeblock.*/
1115 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1116 Jpeg2000Component *comp,
1117 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1120 int w = cblk->coord[0][1] - cblk->coord[0][0];
1121 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1122 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1123 int *src = t1->data[j];
1124 for (i = 0; i < w; ++i)
1125 datap[i] = src[i] * band->f_stepsize;
1129 /* Integer dequantization of a codeblock.*/
1130 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1131 Jpeg2000Component *comp,
1132 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1135 int w = cblk->coord[0][1] - cblk->coord[0][0];
1136 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1137 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1138 int *src = t1->data[j];
1139 for (i = 0; i < w; ++i)
1140 datap[i] = (src[i] * band->i_stepsize + (1 << 14)) >> 15;
1144 /* Inverse ICT parameters in float and integer.
1145 * int value = (float value) * (1<<16) */
1146 static const float f_ict_params[4] = {
1152 static const int i_ict_params[4] = {
1159 static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1162 int32_t *src[3], i0, i1, i2;
1163 float *srcf[3], i0f, i1f, i2f;
1165 for (i = 1; i < 3; i++)
1166 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1167 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1171 for (i = 0; i < 3; i++)
1172 if (tile->codsty[0].transform == FF_DWT97)
1173 srcf[i] = tile->comp[i].f_data;
1175 src [i] = tile->comp[i].i_data;
1177 for (i = 0; i < 2; i++)
1178 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1180 switch (tile->codsty[0].transform) {
1182 for (i = 0; i < csize; i++) {
1183 i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
1184 i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
1185 - (f_ict_params[2] * *srcf[2]);
1186 i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
1193 for (i = 0; i < csize; i++) {
1194 i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
1195 i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
1196 - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
1197 i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
1204 for (i = 0; i < csize; i++) {
1205 i1 = *src[0] - (*src[2] + *src[1] >> 2);
1216 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1219 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1220 int compno, reslevelno, bandno;
1222 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
1223 int pixelsize = planar ? 1 : pixdesc->nb_components;
1226 Jpeg2000T1Context t1;
1228 /* Loop on tile components */
1229 for (compno = 0; compno < s->ncomponents; compno++) {
1230 Jpeg2000Component *comp = tile->comp + compno;
1231 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1233 /* Loop on resolution levels */
1234 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1235 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1237 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1238 int nb_precincts, precno;
1239 Jpeg2000Band *band = rlevel->band + bandno;
1240 int cblkno = 0, bandpos;
1242 bandpos = bandno + (reslevelno > 0);
1244 if (band->coord[0][0] == band->coord[0][1] ||
1245 band->coord[1][0] == band->coord[1][1])
1248 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1249 /* Loop on precincts */
1250 for (precno = 0; precno < nb_precincts; precno++) {
1251 Jpeg2000Prec *prec = band->prec + precno;
1253 /* Loop on codeblocks */
1254 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
1256 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1257 decode_cblk(s, codsty, &t1, cblk,
1258 cblk->coord[0][1] - cblk->coord[0][0],
1259 cblk->coord[1][1] - cblk->coord[1][0],
1262 x = cblk->coord[0][0];
1263 y = cblk->coord[1][0];
1265 if (codsty->transform == FF_DWT97)
1266 dequantization_float(x, y, cblk, comp, &t1, band);
1268 dequantization_int(x, y, cblk, comp, &t1, band);
1272 } /* end reslevel */
1275 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1278 /* inverse MCT transformation */
1279 if (tile->codsty[0].mct)
1280 mct_decode(s, tile);
1282 if (s->cdef[0] < 0) {
1283 for (x = 0; x < s->ncomponents; x++)
1285 if ((s->ncomponents & 1) == 0)
1286 s->cdef[s->ncomponents-1] = 0;
1289 if (s->precision <= 8) {
1290 for (compno = 0; compno < s->ncomponents; compno++) {
1291 Jpeg2000Component *comp = tile->comp + compno;
1292 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1293 float *datap = comp->f_data;
1294 int32_t *i_datap = comp->i_data;
1295 int cbps = s->cbps[compno];
1296 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1300 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1303 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1304 line = picture->data[plane] + y / s->cdy[compno] * picture->linesize[plane];
1305 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1308 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1309 dst = line + x / s->cdx[compno] * pixelsize + compno*!planar;
1311 if (codsty->transform == FF_DWT97) {
1312 for (; x < w; x += s->cdx[compno]) {
1313 int val = lrintf(*datap) + (1 << (cbps - 1));
1314 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1315 val = av_clip(val, 0, (1 << cbps) - 1);
1316 *dst = val << (8 - cbps);
1321 for (; x < w; x += s->cdx[compno]) {
1322 int val = *i_datap + (1 << (cbps - 1));
1323 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1324 val = av_clip(val, 0, (1 << cbps) - 1);
1325 *dst = val << (8 - cbps);
1330 line += picture->linesize[plane];
1334 for (compno = 0; compno < s->ncomponents; compno++) {
1335 Jpeg2000Component *comp = tile->comp + compno;
1336 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1337 float *datap = comp->f_data;
1338 int32_t *i_datap = comp->i_data;
1340 int cbps = s->cbps[compno];
1341 int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
1345 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
1347 y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1348 linel = (uint16_t *)picture->data[plane] + y / s->cdy[compno] * (picture->linesize[plane] >> 1);
1349 for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1352 x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1353 dst = linel + (x / s->cdx[compno] * pixelsize + compno*!planar);
1354 if (codsty->transform == FF_DWT97) {
1355 for (; x < w; x += s-> cdx[compno]) {
1356 int val = lrintf(*datap) + (1 << (cbps - 1));
1357 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1358 val = av_clip(val, 0, (1 << cbps) - 1);
1359 /* align 12 bit values in little-endian mode */
1360 *dst = val << (16 - cbps);
1365 for (; x < w; x += s-> cdx[compno]) {
1366 int val = *i_datap + (1 << (cbps - 1));
1367 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1368 val = av_clip(val, 0, (1 << cbps) - 1);
1369 /* align 12 bit values in little-endian mode */
1370 *dst = val << (16 - cbps);
1375 linel += picture->linesize[plane] >> 1;
1383 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1386 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1387 if (s->tile[tileno].comp) {
1388 for (compno = 0; compno < s->ncomponents; compno++) {
1389 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1390 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1392 ff_jpeg2000_cleanup(comp, codsty);
1394 av_freep(&s->tile[tileno].comp);
1398 memset(s->codsty, 0, sizeof(s->codsty));
1399 memset(s->qntsty, 0, sizeof(s->qntsty));
1400 s->numXtiles = s->numYtiles = 0;
1403 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1405 Jpeg2000CodingStyle *codsty = s->codsty;
1406 Jpeg2000QuantStyle *qntsty = s->qntsty;
1407 uint8_t *properties = s->properties;
1414 if (bytestream2_get_bytes_left(&s->g) < 2) {
1415 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1419 marker = bytestream2_get_be16u(&s->g);
1420 oldpos = bytestream2_tell(&s->g);
1422 if (marker == JPEG2000_SOD) {
1424 Jpeg2000TilePart *tp;
1427 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1428 return AVERROR_INVALIDDATA;
1430 if (s->curtileno < 0) {
1431 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1432 return AVERROR_INVALIDDATA;
1435 tile = s->tile + s->curtileno;
1436 tp = tile->tile_part + tile->tp_idx;
1437 if (tp->tp_end < s->g.buffer) {
1438 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
1439 return AVERROR_INVALIDDATA;
1441 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1442 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1446 if (marker == JPEG2000_EOC)
1449 len = bytestream2_get_be16(&s->g);
1450 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1451 return AVERROR_INVALIDDATA;
1457 s->numXtiles = s->numYtiles = 0;
1460 ret = get_coc(s, codsty, properties);
1463 ret = get_cod(s, codsty, properties);
1466 ret = get_qcc(s, len, qntsty, properties);
1469 ret = get_qcd(s, len, qntsty, properties);
1472 if (!(ret = get_sot(s, len))) {
1473 av_assert1(s->curtileno >= 0);
1474 codsty = s->tile[s->curtileno].codsty;
1475 qntsty = s->tile[s->curtileno].qntsty;
1476 properties = s->tile[s->curtileno].properties;
1480 // the comment is ignored
1481 bytestream2_skip(&s->g, len - 2);
1484 // Tile-part lengths
1485 ret = get_tlm(s, len);
1488 av_log(s->avctx, AV_LOG_ERROR,
1489 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1490 marker, bytestream2_tell(&s->g) - 4);
1491 bytestream2_skip(&s->g, len - 2);
1494 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1495 av_log(s->avctx, AV_LOG_ERROR,
1496 "error during processing marker segment %.4"PRIx16"\n",
1498 return ret ? ret : -1;
1504 /* Read bit stream packets --> T2 operation. */
1505 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1510 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1511 Jpeg2000Tile *tile = s->tile + tileno;
1513 if (ret = init_tile(s, tileno))
1516 s->g = tile->tile_part[0].tpg;
1517 if (ret = jpeg2000_decode_packets(s, tile))
1524 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1526 uint32_t atom_size, atom, atom_end;
1527 int search_range = 10;
1531 bytestream2_get_bytes_left(&s->g) >= 8) {
1532 atom_size = bytestream2_get_be32u(&s->g);
1533 atom = bytestream2_get_be32u(&s->g);
1534 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
1536 if (atom == JP2_CODESTREAM)
1539 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
1542 if (atom == JP2_HEADER &&
1544 uint32_t atom2_size, atom2, atom2_end;
1546 atom2_size = bytestream2_get_be32u(&s->g);
1547 atom2 = bytestream2_get_be32u(&s->g);
1548 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
1549 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1551 if (atom2 == JP2_CODESTREAM) {
1553 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
1554 int method = bytestream2_get_byteu(&s->g);
1555 bytestream2_skipu(&s->g, 2);
1557 s->colour_space = bytestream2_get_be32u(&s->g);
1559 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
1560 int i, size, colour_count, colour_channels, colour_depth[3];
1562 colour_count = bytestream2_get_be16u(&s->g);
1563 colour_channels = bytestream2_get_byteu(&s->g);
1564 // FIXME: Do not ignore channel_sign
1565 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1566 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1567 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
1568 size = (colour_depth[0] + 7 >> 3) * colour_count +
1569 (colour_depth[1] + 7 >> 3) * colour_count +
1570 (colour_depth[2] + 7 >> 3) * colour_count;
1571 if (colour_count > 256 ||
1572 colour_channels != 3 ||
1573 colour_depth[0] > 16 ||
1574 colour_depth[1] > 16 ||
1575 colour_depth[2] > 16 ||
1576 atom2_size < size) {
1577 avpriv_request_sample(s->avctx, "Unknown palette");
1578 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1582 for (i = 0; i < colour_count; i++) {
1583 if (colour_depth[0] <= 8) {
1584 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
1585 r |= r >> colour_depth[0];
1587 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
1589 if (colour_depth[1] <= 8) {
1590 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
1591 r |= r >> colour_depth[1];
1593 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
1595 if (colour_depth[2] <= 8) {
1596 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
1597 r |= r >> colour_depth[2];
1599 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
1601 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
1603 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
1604 int n = bytestream2_get_be16u(&s->g);
1606 int cn = bytestream2_get_be16(&s->g);
1607 int av_unused typ = bytestream2_get_be16(&s->g);
1608 int asoc = bytestream2_get_be16(&s->g);
1609 if (cn < 4 || asoc < 4)
1613 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
1614 } while (atom_end - atom2_end >= 8);
1618 bytestream2_seek(&s->g, atom_end, SEEK_SET);
1624 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1625 int *got_frame, AVPacket *avpkt)
1627 Jpeg2000DecoderContext *s = avctx->priv_data;
1628 ThreadFrame frame = { .f = data };
1629 AVFrame *picture = data;
1633 bytestream2_init(&s->g, avpkt->data, avpkt->size);
1635 memset(s->cdef, -1, sizeof(s->cdef));
1637 if (bytestream2_get_bytes_left(&s->g) < 2) {
1638 ret = AVERROR_INVALIDDATA;
1642 // check if the image is in jp2 format
1643 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1644 (bytestream2_get_be32u(&s->g) == 12) &&
1645 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1646 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1647 if (!jp2_find_codestream(s)) {
1648 av_log(avctx, AV_LOG_ERROR,
1649 "Could not find Jpeg2000 codestream atom.\n");
1650 ret = AVERROR_INVALIDDATA;
1654 bytestream2_seek(&s->g, 0, SEEK_SET);
1657 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
1658 bytestream2_skip(&s->g, 1);
1660 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1661 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1662 ret = AVERROR_INVALIDDATA;
1665 if (ret = jpeg2000_read_main_headers(s))
1668 /* get picture buffer */
1669 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1671 picture->pict_type = AV_PICTURE_TYPE_I;
1672 picture->key_frame = 1;
1674 if (ret = jpeg2000_read_bitstream_packets(s))
1677 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1678 if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1681 jpeg2000_dec_cleanup(s);
1685 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
1686 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
1688 return bytestream2_tell(&s->g);
1691 jpeg2000_dec_cleanup(s);
1695 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
1697 ff_jpeg2000_init_tier1_luts();
1698 ff_mqc_init_context_tables();
1701 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1702 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1704 static const AVOption options[] = {
1705 { "lowres", "Lower the decoding resolution by a power of two",
1706 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1710 static const AVProfile profiles[] = {
1711 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1712 { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1713 { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1714 { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1715 { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1716 { FF_PROFILE_UNKNOWN },
1719 static const AVClass jpeg2000_class = {
1720 .class_name = "jpeg2000",
1721 .item_name = av_default_item_name,
1723 .version = LIBAVUTIL_VERSION_INT,
1726 AVCodec ff_jpeg2000_decoder = {
1728 .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1729 .type = AVMEDIA_TYPE_VIDEO,
1730 .id = AV_CODEC_ID_JPEG2000,
1731 .capabilities = CODEC_CAP_FRAME_THREADS,
1732 .priv_data_size = sizeof(Jpeg2000DecoderContext),
1733 .init_static_data = jpeg2000_init_static_data,
1734 .decode = jpeg2000_decode_frame,
1735 .priv_class = &jpeg2000_class,
1737 .profiles = NULL_IF_CONFIG_SMALL(profiles)