FFmpeg
Loading...
Searching...
No Matches
jpeg2000dec.c
Go to the documentation of this file.
1/*
2 * JPEG 2000 image decoder
3 * Copyright (c) 2007 Kamil Nowosad
4 * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23/**
24 * @file
25 * JPEG 2000 image decoder
26 */
27
28#include <inttypes.h>
29#include <math.h>
30
32#include "libavutil/avassert.h"
33#include "libavutil/common.h"
34#include "libavutil/imgutils.h"
35#include "libavutil/mem.h"
36#include "libavutil/opt.h"
37#include "libavutil/pixdesc.h"
38#include "avcodec.h"
39#include "bytestream.h"
40#include "codec_internal.h"
41#include "decode.h"
42#include "internal.h"
43#include "thread.h"
44#include "jpeg2000.h"
45#include "jpeg2000dsp.h"
46#include "profiles.h"
47#include "jpeg2000dec.h"
48#include "jpeg2000htdec.h"
49
50#define JP2_SIG_TYPE 0x6A502020
51#define JP2_SIG_VALUE 0x0D0A870A
52#define JP2_CODESTREAM 0x6A703263
53#define JP2_HEADER 0x6A703268
54
55#define HAD_COC 0x01
56#define HAD_QCC 0x02
57
58// Values of flag for placeholder passes
63
64#define HT_MIXED 0x80 // bit 7 of SPcod/SPcoc
65
66
67/* get_bits functions for JPEG2000 packet bitstream
68 * It is a get_bit function with a bit-stuffing routine. If the value of the
69 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
70 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
72{
73 int res = 0;
74
75 while (--n >= 0) {
76 res <<= 1;
77 if (s->bit_index == 0) {
78 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
79 }
80 s->bit_index--;
81 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
82 }
83 return res;
84}
85
87{
88 if (bytestream2_get_byte(&s->g) == 0xff)
89 bytestream2_skip(&s->g, 1);
90 s->bit_index = 8;
91}
92
93/* decode the value stored in node */
95 int threshold)
96{
97 Jpeg2000TgtNode *stack[30];
98 int sp = -1, curval = 0;
99
100 if (!node) {
101 av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
102 return AVERROR_INVALIDDATA;
103 }
104
105 while (node && !node->vis) {
106 stack[++sp] = node;
107 node = node->parent;
108 }
109
110 if (node)
111 curval = node->val;
112 else
113 curval = stack[sp]->val;
114
115 while (curval < threshold && sp >= 0) {
116 if (curval < stack[sp]->val)
117 curval = stack[sp]->val;
118 while (curval < threshold) {
119 int ret;
120 if ((ret = get_bits(s, 1)) > 0) {
121 stack[sp]->vis++;
122 break;
123 } else if (!ret)
124 curval++;
125 else
126 return ret;
127 }
128 stack[sp]->val = curval;
129 sp--;
130 }
131 return curval;
132}
133
134static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
135 int bpc, uint32_t log2_chroma_wh, int pal8)
136{
137 int match = 1;
139
141
142 if (desc->nb_components != components) {
143 return 0;
144 }
145
146 switch (components) {
147 case 4:
148 match = match && desc->comp[3].depth >= bpc &&
149 (log2_chroma_wh >> 14 & 3) == 0 &&
150 (log2_chroma_wh >> 12 & 3) == 0;
152 case 3:
153 match = match && desc->comp[2].depth >= bpc &&
154 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
155 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
157 case 2:
158 match = match && desc->comp[1].depth >= bpc &&
159 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
160 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
162
163 case 1:
164 match = match && desc->comp[0].depth >= bpc &&
165 (log2_chroma_wh >> 2 & 3) == 0 &&
166 (log2_chroma_wh & 3) == 0 &&
167 (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * AV_PIX_FMT_FLAG_PAL;
168 }
169 return match;
170}
171
172// pix_fmts with lower bpp have to be listed before
173// similar pix_fmts with higher bpp.
174#define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
175#define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
176#define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
177 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
178 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
179 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
180 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
181 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
182 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
183 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
184 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
185 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
186 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
187#define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
188
198
199/* marker segments */
200/* get sizes and offsets of image, tiles; number of components */
202{
203 int i;
204 int ncomponents;
205 uint32_t log2_chroma_wh = 0;
206 const enum AVPixelFormat *possible_fmts = NULL;
207 int possible_fmts_nb = 0;
208 int ret;
209 int o_dimx, o_dimy; //original image dimensions.
210 int dimx, dimy;
211
212 if (bytestream2_get_bytes_left(&s->g) < 36) {
213 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
214 return AVERROR_INVALIDDATA;
215 }
216
217 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
218 s->width = bytestream2_get_be32u(&s->g); // Width
219 s->height = bytestream2_get_be32u(&s->g); // Height
220 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
221 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
222 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
223 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
224 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
225 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
226 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
227
228 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
229 avpriv_request_sample(s->avctx, "Large Dimensions");
231 }
232
233 if (ncomponents <= 0) {
234 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
235 s->ncomponents);
236 return AVERROR_INVALIDDATA;
237 }
238
239 if (ncomponents > 4) {
240 avpriv_request_sample(s->avctx, "Support for %d components",
241 ncomponents);
243 }
244
245 if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
246 s->image_offset_x < s->tile_offset_x ||
247 s->image_offset_y < s->tile_offset_y ||
248 s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
249 s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
250 ) {
251 av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
252 return AVERROR_INVALIDDATA;
253 }
254
255 if (s->image_offset_x >= s->width || s->image_offset_y >= s->height) {
256 av_log(s->avctx, AV_LOG_ERROR, "image offsets outside image");
257 return AVERROR_INVALIDDATA;
258 }
259
260 if (s->reduction_factor && (s->image_offset_x || s->image_offset_y) ){
261 av_log(s->avctx, AV_LOG_ERROR, "reduction factor with image offsets is not fully implemented");
263 }
264
265 s->ncomponents = ncomponents;
266
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;
271 }
272
273 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
274 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
275 return AVERROR_INVALIDDATA;
276 }
277
278 for (i = 0; i < s->ncomponents; i++) {
279 if (s->cdef[i] < 0) {
280 for (i = 0; i < s->ncomponents; i++) {
281 s->cdef[i] = i + 1;
282 }
283 if ((s->ncomponents & 1) == 0)
284 s->cdef[s->ncomponents-1] = 0;
285 }
286 }
287 // after here we no longer have to consider negative cdef
288
289 int cdef_used = 0;
290 for (i = 0; i < s->ncomponents; i++)
291 cdef_used |= 1<<s->cdef[i];
292
293 // Check that the channels we have are what we expect for the number of components
294 if (cdef_used != ((int[]){0,2,3,14,15})[s->ncomponents])
295 return AVERROR_INVALIDDATA;
296
297 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
298 uint8_t x = bytestream2_get_byteu(&s->g);
299 s->cbps[i] = (x & 0x7f) + 1;
300 s->precision = FFMAX(s->cbps[i], s->precision);
301 s->sgnd[i] = !!(x & 0x80);
302 s->cdx[i] = bytestream2_get_byteu(&s->g);
303 s->cdy[i] = bytestream2_get_byteu(&s->g);
304 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
305 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
306 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
307 return AVERROR_INVALIDDATA;
308 }
309 int i_remapped = s->cdef[i] ? s->cdef[i]-1 : (s->ncomponents-1);
310
311 log2_chroma_wh |= s->cdy[i] >> 1 << i_remapped * 4 | s->cdx[i] >> 1 << i_remapped * 4 + 2;
312 }
313
314 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
315 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
316
317 // There must be at least a SOT and SOD per tile, their minimum size is 14
318 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
319 s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
320 ) {
321 s->numXtiles = s->numYtiles = 0;
322 return AVERROR(EINVAL);
323 }
324
325 s->tile = av_calloc(s->numXtiles * s->numYtiles, sizeof(*s->tile));
326 if (!s->tile) {
327 s->numXtiles = s->numYtiles = 0;
328 return AVERROR(ENOMEM);
329 }
330
331 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
332 Jpeg2000Tile *tile = s->tile + i;
333
334 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
335 if (!tile->comp)
336 return AVERROR(ENOMEM);
337 }
338
339 /* compute image size with reduction factor */
340 o_dimx = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
341 s->reduction_factor);
342 o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
343 s->reduction_factor);
344 dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
345 dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
346 for (i = 1; i < s->ncomponents; i++) {
347 dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
348 dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
349 }
350
351 ret = ff_set_dimensions(s->avctx, dimx << s->avctx->lowres, dimy << s->avctx->lowres);
352 if (ret < 0)
353 return ret;
354
355 if (s->avctx->profile == AV_PROFILE_JPEG2000_DCINEMA_2K ||
356 s->avctx->profile == AV_PROFILE_JPEG2000_DCINEMA_4K) {
357 possible_fmts = xyz_pix_fmts;
358 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
359 } else {
360 switch (s->colour_space) {
361 case 16:
362 possible_fmts = rgb_pix_fmts;
363 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
364 break;
365 case 17:
366 possible_fmts = gray_pix_fmts;
367 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
368 break;
369 case 18:
370 possible_fmts = yuv_pix_fmts;
371 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
372 break;
373 default:
374 possible_fmts = all_pix_fmts;
375 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
376 break;
377 }
378 }
379 if ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
380 && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
381 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
382 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
383 for (i = 0; i < possible_fmts_nb; ++i) {
384 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
385 s->avctx->pix_fmt = possible_fmts[i];
386 break;
387 }
388 }
389
390 if (i == possible_fmts_nb) {
391 if (ncomponents == 4 &&
392 s->cdy[0] == 1 && s->cdx[0] == 1 &&
393 s->cdy[1] == 1 && s->cdx[1] == 1 &&
394 s->cdy[2] == s->cdy[3] && s->cdx[2] == s->cdx[3]) {
395 if (s->precision == 8 && s->cdy[2] == 2 && s->cdx[2] == 2 && !s->pal8) {
396 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
397 s->cdef[0] = 0;
398 s->cdef[1] = 1;
399 s->cdef[2] = 2;
400 s->cdef[3] = 3;
401 i = 0;
402 }
403 } else if (ncomponents == 3 && s->precision == 8 &&
404 s->cdx[0] == s->cdx[1] && s->cdx[0] == s->cdx[2] &&
405 s->cdy[0] == s->cdy[1] && s->cdy[0] == s->cdy[2]) {
406 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
407 i = 0;
408 } else if (ncomponents == 2 && s->precision == 8 &&
409 s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
410 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
411 i = 0;
412 } else if (ncomponents == 2 && s->precision == 16 &&
413 s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
414 s->avctx->pix_fmt = AV_PIX_FMT_YA16;
415 i = 0;
416 } else if (ncomponents == 1 && s->precision == 8) {
417 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
418 i = 0;
419 } else if (ncomponents == 1 && s->precision == 12) {
420 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
421 i = 0;
422 }
423 }
424
425
426 if (i == possible_fmts_nb) {
427 av_log(s->avctx, AV_LOG_ERROR,
428 "Unknown pix_fmt, profile: %d, colour_space: %d, "
429 "components: %d, precision: %d\n"
430 "cdx[0]: %d, cdy[0]: %d\n"
431 "cdx[1]: %d, cdy[1]: %d\n"
432 "cdx[2]: %d, cdy[2]: %d\n"
433 "cdx[3]: %d, cdy[3]: %d\n",
434 s->avctx->profile, s->colour_space, ncomponents, s->precision,
435 s->cdx[0],
436 s->cdy[0],
437 ncomponents > 1 ? s->cdx[1] : 0,
438 ncomponents > 1 ? s->cdy[1] : 0,
439 ncomponents > 2 ? s->cdx[2] : 0,
440 ncomponents > 2 ? s->cdy[2] : 0,
441 ncomponents > 3 ? s->cdx[3] : 0,
442 ncomponents > 3 ? s->cdy[3] : 0);
444 }
445 s->avctx->bits_per_raw_sample = s->precision;
446 return 0;
447}
448/* get extended capabilities (CAP) marker segment */
450{
451 uint32_t Pcap;
452 uint16_t Ccap_i[32] = { 0 };
453 uint16_t Ccap_15;
454 uint8_t P;
455
456 if (bytestream2_get_bytes_left(&s->g) < 6) {
457 av_log(s->avctx, AV_LOG_ERROR, "Underflow while parsing the CAP marker\n");
458 return AVERROR_INVALIDDATA;
459 }
460
461 Pcap = bytestream2_get_be32u(&s->g);
462 s->isHT = (Pcap >> (31 - (15 - 1))) & 1;
463 for (int i = 0; i < 32; i++) {
464 if ((Pcap >> (31 - i)) & 1)
465 Ccap_i[i] = bytestream2_get_be16u(&s->g);
466 }
467 Ccap_15 = Ccap_i[14];
468 if (s->isHT == 1) {
469 av_log(s->avctx, AV_LOG_INFO, "This codestream uses the HT block coder.\n");
470 // Bits 14-15
471 switch ((Ccap_15 >> 14) & 0x3) {
472 case 0x3:
473 s->Ccap15_b14_15 = HTJ2K_MIXED;
474 break;
475 case 0x1:
476 s->Ccap15_b14_15 = HTJ2K_HTDECLARED;
477 break;
478 case 0x0:
479 s->Ccap15_b14_15 = HTJ2K_HTONLY;
480 break;
481 default:
482 av_log(s->avctx, AV_LOG_ERROR, "Unknown CCap value.\n");
483 return AVERROR(EINVAL);
484 break;
485 }
486 // Bit 13
487 if ((Ccap_15 >> 13) & 1) {
488 av_log(s->avctx, AV_LOG_ERROR, "MULTIHT set is not supported.\n");
490 }
491 // Bit 12
492 s->Ccap15_b12 = (Ccap_15 >> 12) & 1;
493 // Bit 11
494 s->Ccap15_b11 = (Ccap_15 >> 11) & 1;
495 // Bit 5
496 s->Ccap15_b05 = (Ccap_15 >> 5) & 1;
497 // Bit 0-4
498 P = Ccap_15 & 0x1F;
499 if (!P)
500 s->HT_B = 8;
501 else if (P < 20)
502 s->HT_B = P + 8;
503 else if (P < 31)
504 s->HT_B = 4 * (P - 19) + 27;
505 else
506 s->HT_B = 74;
507
508 if (s->HT_B > 31) {
509 av_log(s->avctx, AV_LOG_ERROR, "Codestream exceeds available precision (B > 31).\n");
511 }
512 }
513 return 0;
514}
515
516/* get common part for COD and COC segments */
518{
519 uint8_t byte;
520
521 if (bytestream2_get_bytes_left(&s->g) < 5) {
522 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
523 return AVERROR_INVALIDDATA;
524 }
525
526 /* nreslevels = number of resolution levels
527 = number of decomposition level +1 */
528 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
529 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
530 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
531 return AVERROR_INVALIDDATA;
532 }
533
534 if (c->nreslevels <= s->reduction_factor) {
535 /* we are forced to update reduction_factor as its requested value is
536 not compatible with this bitstream, and as we might have used it
537 already in setup earlier we have to fail this frame until
538 reinitialization is implemented */
539 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
540 s->reduction_factor = c->nreslevels - 1;
541 return AVERROR(EINVAL);
542 }
543
544 /* compute number of resolution levels to decode */
545 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
546
547 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
548 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
549
550 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
551 c->log2_cblk_width + c->log2_cblk_height > 12) {
552 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
553 return AVERROR_INVALIDDATA;
554 }
555
556 c->cblk_style = bytestream2_get_byteu(&s->g);
557 if (c->cblk_style != 0) { // cblk style
558 if (c->cblk_style & JPEG2000_CTSY_HTJ2K_M || c->cblk_style & JPEG2000_CTSY_HTJ2K_F) {
559 av_log(s->avctx,AV_LOG_TRACE,"High Throughput jpeg 2000 codestream.\n");
560 } else {
561 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
562 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
563 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
564 }
565 }
566 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
567 /* set integer 9/7 DWT in case of BITEXACT flag */
568 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
569 c->transform = FF_DWT97_INT;
570
571 if (c->csty & JPEG2000_CSTY_PREC) {
572 int i;
573 for (i = 0; i < c->nreslevels; i++) {
574 byte = bytestream2_get_byte(&s->g);
575 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
576 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
577 if (i)
578 if (c->log2_prec_widths[i] == 0 || c->log2_prec_heights[i] == 0) {
579 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
580 c->log2_prec_widths[i], c->log2_prec_heights[i]);
581 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
582 return AVERROR_INVALIDDATA;
583 }
584 }
585 } else {
586 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
587 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
588 }
589 return 0;
590}
591
592/* get coding parameters for a particular tile or whole image*/
594 const uint8_t *properties)
595{
597 int compno, ret;
598
599 if (bytestream2_get_bytes_left(&s->g) < 5) {
600 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
601 return AVERROR_INVALIDDATA;
602 }
603
604 tmp.csty = bytestream2_get_byteu(&s->g);
605
606 // get progression order
607 tmp.prog_order = bytestream2_get_byteu(&s->g);
608
609 tmp.nlayers = bytestream2_get_be16u(&s->g);
610 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
611
612 if (tmp.mct && s->ncomponents < 3) {
613 av_log(s->avctx, AV_LOG_ERROR,
614 "MCT %"PRIu8" with too few components (%d)\n",
615 tmp.mct, s->ncomponents);
616 return AVERROR_INVALIDDATA;
617 }
618
619 if ((ret = get_cox(s, &tmp)) < 0)
620 return ret;
621 tmp.init = 1;
622 for (compno = 0; compno < s->ncomponents; compno++)
623 if (!(properties[compno] & HAD_COC))
624 memcpy(c + compno, &tmp, sizeof(tmp));
625 return 0;
626}
627
628/* Get coding parameters for a component in the whole image or a
629 * particular tile. */
631 uint8_t *properties)
632{
633 int compno, ret;
634 uint8_t has_eph, has_sop;
635
636 if (bytestream2_get_bytes_left(&s->g) < 2) {
637 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
638 return AVERROR_INVALIDDATA;
639 }
640
641 compno = bytestream2_get_byteu(&s->g);
642
643 if (compno >= s->ncomponents) {
644 av_log(s->avctx, AV_LOG_ERROR,
645 "Invalid compno %d. There are %d components in the image.\n",
646 compno, s->ncomponents);
647 return AVERROR_INVALIDDATA;
648 }
649
650 c += compno;
651 has_eph = c->csty & JPEG2000_CSTY_EPH;
652 has_sop = c->csty & JPEG2000_CSTY_SOP;
653 c->csty = bytestream2_get_byteu(&s->g);
654 c->csty |= has_eph; //do not override eph present bits from COD
655 c->csty |= has_sop; //do not override sop present bits from COD
656
657 if ((ret = get_cox(s, c)) < 0)
658 return ret;
659
660 properties[compno] |= HAD_COC;
661 c->init = 1;
662 return 0;
663}
664
666{
667 uint16_t compno;
668 compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
669 bytestream2_get_be16u(&s->g);
670 if (bytestream2_get_byte(&s->g)) {
671 av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
672 return AVERROR_INVALIDDATA; // SRgn field value is 0
673 }
674 // SPrgn field
675 // Currently compno cannot be greater than 4.
676 // However, future implementation should support compno up to 65536
677 if (compno < s->ncomponents) {
678 int v;
679 if (s->curtileno == -1) {
680 v = bytestream2_get_byte(&s->g);
681 if (v > 30)
683 s->roi_shift[compno] = v;
684 } else {
685 if (s->tile[s->curtileno].tp_idx != 0)
686 return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
687 v = bytestream2_get_byte(&s->g);
688 if (v > 30)
690 s->tile[s->curtileno].comp[compno].roi_shift = v;
691 }
692 return 0;
693 }
694 return AVERROR_INVALIDDATA;
695}
696
697/* Get common part for QCD and QCC segments. */
699{
700 int i, x;
701
702 if (bytestream2_get_bytes_left(&s->g) < 1)
703 return AVERROR_INVALIDDATA;
704
705 x = bytestream2_get_byteu(&s->g); // Sqcd
706
707 q->nguardbits = x >> 5;
708 q->quantsty = x & 0x1f;
709
710 if (q->quantsty == JPEG2000_QSTY_NONE) {
711 n -= 3;
712 if (bytestream2_get_bytes_left(&s->g) < n ||
714 return AVERROR_INVALIDDATA;
715 for (i = 0; i < n; i++)
716 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
717 } else if (q->quantsty == JPEG2000_QSTY_SI) {
718 if (bytestream2_get_bytes_left(&s->g) < 2)
719 return AVERROR_INVALIDDATA;
720 x = bytestream2_get_be16u(&s->g);
721 q->expn[0] = x >> 11;
722 q->mant[0] = x & 0x7ff;
723 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
724 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
725 q->expn[i] = curexpn;
726 q->mant[i] = q->mant[0];
727 }
728 } else {
729 n = (n - 3) >> 1;
730 if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
732 return AVERROR_INVALIDDATA;
733 for (i = 0; i < n; i++) {
734 x = bytestream2_get_be16u(&s->g);
735 q->expn[i] = x >> 11;
736 q->mant[i] = x & 0x7ff;
737 }
738 }
739 return 0;
740}
741
742/* Get quantization parameters for a particular tile or a whole image. */
744 const uint8_t *properties)
745{
747 int compno, ret;
748
749 memset(&tmp, 0, sizeof(tmp));
750
751 if ((ret = get_qcx(s, n, &tmp)) < 0)
752 return ret;
753 for (compno = 0; compno < s->ncomponents; compno++)
754 if (!(properties[compno] & HAD_QCC))
755 memcpy(q + compno, &tmp, sizeof(tmp));
756 return 0;
757}
758
759/* Get quantization parameters for a component in the whole image
760 * on in a particular tile. */
762 uint8_t *properties)
763{
764 int compno;
765
766 if (bytestream2_get_bytes_left(&s->g) < 1)
767 return AVERROR_INVALIDDATA;
768
769 compno = bytestream2_get_byteu(&s->g);
770
771 if (compno >= s->ncomponents) {
772 av_log(s->avctx, AV_LOG_ERROR,
773 "Invalid compno %d. There are %d components in the image.\n",
774 compno, s->ncomponents);
775 return AVERROR_INVALIDDATA;
776 }
777
778 properties[compno] |= HAD_QCC;
779 return get_qcx(s, n - 1, q + compno);
780}
781
783{
784 int i;
785 int elem_size = s->ncomponents <= 257 ? 7 : 9;
786 Jpeg2000POC tmp = {{{0}}};
787
788 if (bytestream2_get_bytes_left(&s->g) < 5 || size < 2 + elem_size) {
789 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
790 return AVERROR_INVALIDDATA;
791 }
792
793 if (elem_size > 7) {
794 avpriv_request_sample(s->avctx, "Fat POC not supported");
796 }
797
798 tmp.nb_poc = (size - 2) / elem_size;
799 if (tmp.nb_poc > MAX_POCS) {
800 avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
802 }
803
804 for (i = 0; i<tmp.nb_poc; i++) {
805 Jpeg2000POCEntry *e = &tmp.poc[i];
806 e->RSpoc = bytestream2_get_byteu(&s->g);
807 e->CSpoc = bytestream2_get_byteu(&s->g);
808 e->LYEpoc = bytestream2_get_be16u(&s->g);
809 e->REpoc = bytestream2_get_byteu(&s->g);
810 e->CEpoc = bytestream2_get_byteu(&s->g);
811 e->Ppoc = bytestream2_get_byteu(&s->g);
812 if (!e->CEpoc)
813 e->CEpoc = 256;
814 if (e->CEpoc > s->ncomponents)
815 e->CEpoc = s->ncomponents;
816 if ( e->RSpoc >= e->REpoc || e->REpoc > 33
817 || e->CSpoc >= e->CEpoc || e->CEpoc > s->ncomponents
818 || !e->LYEpoc) {
819 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
820 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
821 );
822 return AVERROR_INVALIDDATA;
823 }
824 }
825
826 if (!p->nb_poc || p->is_default) {
827 *p = tmp;
828 } else {
829 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
830 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
831 return AVERROR_INVALIDDATA;
832 }
833 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
834 p->nb_poc += tmp.nb_poc;
835 }
836
837 p->is_default = 0;
838
839 return 0;
840}
841
842
843/* Get start of tile segment. */
845{
847 uint16_t Isot;
848 uint32_t Psot;
849 unsigned TPsot;
850
851 if (bytestream2_get_bytes_left(&s->g) < 8)
852 return AVERROR_INVALIDDATA;
853
854 s->curtileno = 0;
855 Isot = bytestream2_get_be16u(&s->g); // Isot
856 if (Isot >= s->numXtiles * s->numYtiles)
857 return AVERROR_INVALIDDATA;
858
859 s->curtileno = Isot;
860 Psot = bytestream2_get_be32u(&s->g); // Psot
861 TPsot = bytestream2_get_byteu(&s->g); // TPsot
862
863 /* Read TNSot but not used */
864 bytestream2_get_byteu(&s->g); // TNsot
865
866 if (!Psot)
867 Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
868
869 if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
870 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
871 return AVERROR_INVALIDDATA;
872 }
873
874 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
875 avpriv_request_sample(s->avctx, "Too many tile parts");
877 }
878
879 s->tile[Isot].tp_idx = TPsot;
880 tp = s->tile[Isot].tile_part + TPsot;
881 tp->tile_index = Isot;
882 tp->tp_end = s->g.buffer + Psot - n - 2;
883
884 if (!TPsot) {
885 Jpeg2000Tile *tile = s->tile + s->curtileno;
886
887 /* copy defaults */
888 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
889 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
890 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
891 tile->poc.is_default = 1;
892 }
893
894 return 0;
895}
896
898{
899 if (s->ncomponents*4 != n - 2) {
900 av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
901 return AVERROR_INVALIDDATA;
902 }
903 bytestream2_skip(&s->g, n - 2);
904 return 0;
905}
906
908{
909 if (bytestream2_get_bytes_left(&s->g) < (n - 2))
910 return AVERROR_INVALIDDATA;
911 bytestream2_skip(&s->g, n - 2);
912 return 0;
913}
914
915/* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
916 * Used to know the number of tile parts and lengths.
917 * There may be multiple TLMs in the header.
918 * TODO: The function is not used for tile-parts management, nor anywhere else.
919 * It can be useful to allocate memory for tile parts, before managing the SOT
920 * markers. Parsing the TLM header is needed to increment the input header
921 * buffer.
922 * This marker is mandatory for DCI. */
924{
925 uint8_t Stlm, ST, SP, tile_tlm, i;
926 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
927 Stlm = bytestream2_get_byte(&s->g);
928
929 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
930 ST = (Stlm >> 4) & 0x03;
931 if (ST == 0x03) {
932 av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
933 return AVERROR_INVALIDDATA;
934 }
935
936 SP = (Stlm >> 6) & 0x01;
937 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
938 for (i = 0; i < tile_tlm; i++) {
939 switch (ST) {
940 case 0:
941 break;
942 case 1:
943 bytestream2_get_byte(&s->g);
944 break;
945 case 2:
946 bytestream2_get_be16(&s->g);
947 break;
948 }
949 if (SP == 0) {
950 bytestream2_get_be16(&s->g);
951 } else {
952 bytestream2_get_be32(&s->g);
953 }
954 }
955 return 0;
956}
957
959{
960 int i;
961 int v;
962
963 av_log(s->avctx, AV_LOG_DEBUG,
964 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
965
966 if (n < 4)
967 return AVERROR_INVALIDDATA;
968
969 /*Zplt =*/ bytestream2_get_byte(&s->g);
970
971 for (i = 0; i < n - 3; i++) {
972 v = bytestream2_get_byte(&s->g);
973 }
974 if (v & 0x80)
975 return AVERROR_INVALIDDATA;
976
977 return 0;
978}
979
981{
982 void *new;
983
984 if (n < 3) {
985 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
986 return AVERROR_INVALIDDATA;
987 }
988 bytestream2_get_byte(&s->g); //Zppm is skipped and not used
989 new = av_realloc(s->packed_headers,
990 s->packed_headers_size + n - 3);
991 if (new) {
992 s->packed_headers = new;
993 } else
994 return AVERROR(ENOMEM);
995 s->has_ppm = 1;
996 memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
997 bytestream2_get_bufferu(&s->g, s->packed_headers + s->packed_headers_size,
998 n - 3);
999 s->packed_headers_size += n - 3;
1000
1001 return 0;
1002}
1003
1005{
1007 void *new;
1008
1009 if (n < 3) {
1010 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
1011 return AVERROR_INVALIDDATA;
1012 }
1013 if (s->curtileno < 0)
1014 return AVERROR_INVALIDDATA;
1015
1016 tile = &s->tile[s->curtileno];
1017 if (tile->tp_idx != 0) {
1018 av_log(s->avctx, AV_LOG_ERROR,
1019 "PPT marker can occur only on first tile part of a tile.\n");
1020 return AVERROR_INVALIDDATA;
1021 }
1022
1023 tile->has_ppt = 1; // this tile has a ppt marker
1024 bytestream2_get_byte(&s->g); // Zppt is skipped and not used
1025 new = av_realloc(tile->packed_headers,
1026 tile->packed_headers_size + n - 3);
1027 if (new) {
1028 tile->packed_headers = new;
1029 } else
1030 return AVERROR(ENOMEM);
1031 memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
1032 bytestream2_get_bufferu(&s->g, tile->packed_headers + tile->packed_headers_size, n - 3);
1033 tile->packed_headers_size += n - 3;
1034
1035 return 0;
1036}
1037
1038static int init_tile(Jpeg2000DecoderContext *s, int tileno)
1039{
1040 int compno;
1041 int tilex = tileno % s->numXtiles;
1042 int tiley = tileno / s->numXtiles;
1043 Jpeg2000Tile *tile = s->tile + tileno;
1044
1045 if (!tile->comp)
1046 return AVERROR(ENOMEM);
1047
1048 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
1049 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
1050 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
1051 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
1052
1053 for (compno = 0; compno < s->ncomponents; compno++) {
1054 Jpeg2000Component *comp = tile->comp + compno;
1055 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1056 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1057 int ret; // global bandno
1058
1059 comp->coord_o[0][0] = tile->coord[0][0];
1060 comp->coord_o[0][1] = tile->coord[0][1];
1061 comp->coord_o[1][0] = tile->coord[1][0];
1062 comp->coord_o[1][1] = tile->coord[1][1];
1063
1064 comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
1065 comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
1066 comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
1067 comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
1068
1069 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
1070 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
1071 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
1072 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
1073
1074 if (!comp->roi_shift)
1075 comp->roi_shift = s->roi_shift[compno];
1076 if (!codsty->init)
1077 return AVERROR_INVALIDDATA;
1078 if (s->isHT && (!s->Ccap15_b05) && (!codsty->transform)) {
1079 av_log(s->avctx, AV_LOG_ERROR, "Transformation = 0 (lossy DWT) is found in HTREV HT set\n");
1080 return AVERROR_INVALIDDATA;
1081 }
1082 if (s->isHT && s->Ccap15_b14_15 != (codsty->cblk_style >> 6) && s->Ccap15_b14_15 != HTJ2K_HTONLY) {
1083 av_log(s->avctx, AV_LOG_ERROR, "SPcod/SPcoc value does not match bit 14-15 values of Ccap15\n");
1084 return AVERROR_INVALIDDATA;
1085 }
1086 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
1087 s->cbps[compno], s->cdx[compno],
1088 s->cdy[compno], s->avctx))
1089 return ret;
1090 }
1091 return 0;
1092}
1093
1094/* Read the number of coding passes. */
1096{
1097 int num;
1098 if (!get_bits(s, 1))
1099 return 1;
1100 if (!get_bits(s, 1))
1101 return 2;
1102 if ((num = get_bits(s, 2)) != 3)
1103 return num < 0 ? num : 3 + num;
1104 if ((num = get_bits(s, 5)) != 31)
1105 return num < 0 ? num : 6 + num;
1106 num = get_bits(s, 7);
1107 return num < 0 ? num : 37 + num;
1108}
1109
1111{
1112 int res = 0, ret;
1113 while (ret = get_bits(s, 1)) {
1114 if (ret < 0)
1115 return ret;
1116 res++;
1117 }
1118 return res;
1119}
1120
1122 int *tp_index)
1123{
1124 s->g = tile->tile_part[*tp_index].header_tpg;
1125 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1126 av_log(s->avctx, AV_LOG_WARNING, "Packet header bytes in PPM marker segment is too short.\n");
1127 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1128 s->g = tile->tile_part[++(*tp_index)].tpg;
1129 }
1130 }
1131}
1132
1134 int *tp_index, const Jpeg2000CodingStyle *codsty)
1135{
1136 int32_t is_endof_tp;
1137
1138 s->g = tile->tile_part[*tp_index].tpg;
1139 is_endof_tp = bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8;
1140 // Following while loop is necessary because a tilepart may include only SOD marker.
1141 // Such a tilepart has neither packet header nor compressed data.
1142 while (is_endof_tp) {
1143 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1144 s->g = tile->tile_part[++(*tp_index)].tpg;
1145 is_endof_tp = bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8;
1146 } else {
1147 is_endof_tp = 0;
1148 }
1149 }
1150 if (codsty->csty & JPEG2000_CSTY_SOP) {
1151 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1153 else
1154 av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1155 }
1156}
1157
1159 const Jpeg2000CodingStyle *codsty,
1160 Jpeg2000ResLevel *rlevel, int precno,
1161 int layno, const uint8_t *expn, int numgbits)
1162{
1163 int bandno, cblkno, ret, nb_code_blocks;
1164 int cwsno;
1165
1166 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1167 return 0;
1168 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1169 // Select stream to read from
1170 if (s->has_ppm)
1171 select_header(s, tile, tp_index);
1172 else if (tile->has_ppt)
1173 s->g = tile->packed_headers_stream;
1174 else
1175 select_stream(s, tile, tp_index, codsty);
1176
1177 if (!(ret = get_bits(s, 1))) {
1179 goto skip_data;
1180 } else if (ret < 0)
1181 return ret;
1182
1183 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1184 Jpeg2000Band *band = rlevel->band + bandno;
1185 Jpeg2000Prec *prec = band->prec + precno;
1186
1187 if (band->coord[0][0] == band->coord[0][1] ||
1188 band->coord[1][0] == band->coord[1][1])
1189 continue;
1190 nb_code_blocks = prec->nb_codeblocks_height *
1191 prec->nb_codeblocks_width;
1192 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1193 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1194 int incl, newpasses, llen;
1195 void *tmp;
1196
1197 if (!cblk->incl) {
1198 incl = 0;
1199 cblk->modes = codsty->cblk_style;
1200 if (cblk->modes >= JPEG2000_CTSY_HTJ2K_F)
1201 cblk->ht_plhd = HT_PLHD_ON;
1202 if (layno > 0)
1203 incl = tag_tree_decode(s, prec->cblkincl + cblkno, 0 + 1) == 0;
1204 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1205
1206 if (incl) {
1207 int zbp = tag_tree_decode(s, prec->zerobits + cblkno, 100);
1208 int v = expn[bandno] + numgbits - 1 - (zbp - tile->comp->roi_shift);
1209 if (v < 0 || v > 30) {
1210 av_log(s->avctx, AV_LOG_ERROR,
1211 "nonzerobits %d invalid or unsupported\n", v);
1212 return AVERROR_INVALIDDATA;
1213 }
1214 cblk->incl = 1;
1215 cblk->nonzerobits = v;
1216 cblk->zbp = zbp;
1217 cblk->lblock = 3;
1218 }
1219 } else {
1220 incl = get_bits(s, 1);
1221 }
1222
1223 if (incl) {
1224 uint8_t bypass_term_threshold = 0;
1225 uint8_t bits_to_read = 0;
1226 uint32_t segment_bytes = 0;
1227 int32_t segment_passes = 0;
1228 uint8_t next_segment_passes = 0;
1229 int32_t href_passes, pass_bound;
1230 uint32_t tmp_length = 0;
1231 int32_t newpasses_copy, npasses_copy;
1232
1233 if ((newpasses = getnpasses(s)) <= 0)
1234 return newpasses;
1235 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1236 avpriv_request_sample(s->avctx, "Too many passes");
1237 return AVERROR_PATCHWELCOME;
1238 }
1239 if ((llen = getlblockinc(s)) < 0)
1240 return llen;
1241 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1242 avpriv_request_sample(s->avctx,
1243 "Block with length beyond 16 bits");
1244 return AVERROR_PATCHWELCOME;
1245 }
1246 cblk->nb_lengthinc = 0;
1247 cblk->nb_terminationsinc = 0;
1248 av_free(cblk->lengthinc);
1249 cblk->lengthinc = av_calloc(newpasses, sizeof(*cblk->lengthinc));
1250 if (!cblk->lengthinc)
1251 return AVERROR(ENOMEM);
1252 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1,
1253 sizeof(*cblk->data_start));
1254 if (!tmp)
1255 return AVERROR(ENOMEM);
1256 cblk->data_start = tmp;
1257 cblk->lblock += llen;
1258
1259 // Count number of necessary terminations for non HT code block
1260 newpasses_copy = newpasses;
1261 npasses_copy = cblk->npasses;
1262 if (!(cblk->modes & JPEG2000_CTSY_HTJ2K_F)) {
1263 do {
1264 int newpasses1 = 0;
1265
1266 while (newpasses1 < newpasses_copy) {
1267 newpasses1++;
1268 if (needs_termination(codsty->cblk_style, npasses_copy + newpasses1 - 1)) {
1269 cblk->nb_terminationsinc++;
1270 break;
1271 }
1272 }
1273 npasses_copy += newpasses1;
1274 newpasses_copy -= newpasses1;
1275 } while (newpasses_copy);
1276 }
1277
1278 if (cblk->ht_plhd) {
1279 href_passes = (cblk->npasses + newpasses - 1) % 3;
1280 segment_passes = newpasses - href_passes;
1281 pass_bound = 2;
1282 bits_to_read = cblk->lblock;
1283 if (segment_passes < 1) {
1284 // No possible HT Cleanup pass here; may have placeholder passes
1285 // or an original J2K block bit-stream (in MIXED mode).
1286 segment_passes = newpasses;
1287 while (pass_bound <= segment_passes) {
1288 bits_to_read++;
1289 pass_bound += pass_bound;
1290 }
1291 segment_bytes = get_bits(s, bits_to_read);
1292 if (segment_bytes) {
1293 if (cblk->modes & HT_MIXED) {
1294 cblk->ht_plhd = HT_PLHD_OFF;
1295 cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1296 }
1297 else {
1298 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1299 }
1300 }
1301 } else {
1302 while (pass_bound <= segment_passes) {
1303 bits_to_read++;
1304 pass_bound += pass_bound;
1305 }
1306 segment_bytes = get_bits(s, bits_to_read);
1307 if (segment_bytes) {
1308 // No more placeholder passes
1309 if (!(cblk->modes & HT_MIXED)) {
1310 // Must be the first HT Cleanup pass
1311 if (segment_bytes < 2)
1312 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1313 next_segment_passes = 2;
1314 cblk->ht_plhd = HT_PLHD_OFF;
1315 // Write length information for HT CleanUp segment
1316 cblk->pass_lengths[0] = segment_bytes;
1317 } else if (cblk->lblock > 3 && segment_bytes > 1
1318 && (segment_bytes >> (bits_to_read - 1)) == 0) {
1319 // Must be the first HT Cleanup pass, since length MSB is 0
1320 next_segment_passes = 2;
1321 cblk->ht_plhd = HT_PLHD_OFF;
1322 // Write length information for HT CleanUp segment
1323 cblk->pass_lengths[0] = segment_bytes;
1324 } else {
1325 // Must have an original (non-HT) block coding pass
1326 cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1327 cblk->ht_plhd = HT_PLHD_OFF;
1328 segment_passes = newpasses;
1329 while (pass_bound <= segment_passes) {
1330 bits_to_read++;
1331 pass_bound += pass_bound;
1332 segment_bytes <<= 1;
1333 segment_bytes += get_bits(s, 1);
1334 }
1335 }
1336 } else {
1337 // Probably parsing placeholder passes, but we need to read an
1338 // extra length bit to verify this, since prior to the first
1339 // HT Cleanup pass, the number of length bits read for a
1340 // contributing code-block is dependent on the number of passes
1341 // being included, as if it were a non-HT code-block.
1342 segment_passes = newpasses;
1343 if (pass_bound <= segment_passes) {
1344 while (1) {
1345 bits_to_read++;
1346 pass_bound += pass_bound;
1347 segment_bytes <<= 1;
1348 segment_bytes += get_bits(s, 1);
1349 if (pass_bound > segment_passes)
1350 break;
1351 }
1352 if (segment_bytes) {
1353 if (cblk->modes & HT_MIXED) {
1354 cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1355 cblk->ht_plhd = HT_PLHD_OFF;
1356 } else {
1357 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1358 }
1359 }
1360 }
1361 }
1362 }
1363 } else if (cblk->modes & JPEG2000_CTSY_HTJ2K_F) {
1364 // Quality layer commences with a non-initial HT coding pass
1365 if(bits_to_read != 0)
1366 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1367 segment_passes = cblk->npasses % 3;
1368 if (segment_passes == 0) {
1369 // newpasses is a HT Cleanup pass; next segment has refinement passes
1370 segment_passes = 1;
1371 next_segment_passes = 2;
1372 if (segment_bytes == 1)
1373 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1374 } else {
1375 // newpasses = 1 means npasses is HT SigProp; 2 means newpasses is
1376 // HT MagRef pass
1377 segment_passes = newpasses > 1 ? 3 - segment_passes : 1;
1378 next_segment_passes = 1;
1379 bits_to_read = av_log2(segment_passes);
1380 }
1381 bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1382 segment_bytes = get_bits(s, bits_to_read);
1383 // Write length information for HT Refinement segment
1384 cblk->pass_lengths[1] += segment_bytes;
1385 } else if (!(cblk->modes & (JPEG2000_CBLK_TERMALL | JPEG2000_CBLK_BYPASS))) {
1386 // Common case for non-HT code-blocks; we have only one segment
1387 bits_to_read = (uint8_t) cblk->lblock + av_log2((uint8_t) newpasses);
1388 segment_bytes = get_bits(s, bits_to_read);
1389 segment_passes = newpasses;
1390 } else if (cblk->modes & JPEG2000_CBLK_TERMALL) {
1391 // RESTART MODE
1392 bits_to_read = cblk->lblock;
1393 segment_bytes = get_bits(s, bits_to_read);
1394 segment_passes = 1;
1395 next_segment_passes = 1;
1396 } else {
1397 // BYPASS MODE
1398 bypass_term_threshold = 10;
1399 if(bits_to_read != 0)
1400 av_log(s->avctx, AV_LOG_WARNING, "Length information for a codeblock is invalid\n");
1401 if (cblk->npasses < bypass_term_threshold) {
1402 // May have from 1 to 10 uninterrupted passes before 1st RAW SigProp
1403 segment_passes = bypass_term_threshold - cblk->npasses;
1404 if (segment_passes > newpasses)
1405 segment_passes = newpasses;
1406 while ((2 << bits_to_read) <= segment_passes)
1407 bits_to_read++;
1408 next_segment_passes = 2;
1409 } else if ((cblk->npasses - bypass_term_threshold) % 3 < 2) {
1410 // 0 means newpasses is a RAW SigProp; 1 means newpasses is a RAW MagRef pass
1411 segment_passes = newpasses > 1 ? 2 - (cblk->npasses - bypass_term_threshold) % 3 : 1;
1412 bits_to_read = av_log2(segment_passes);
1413 next_segment_passes = 1;
1414 } else {
1415 // newpasses is an isolated Cleanup pass that precedes a RAW SigProp pass
1416 segment_passes = 1;
1417 next_segment_passes = 2;
1418 }
1419 bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1420 segment_bytes = get_bits(s, bits_to_read);
1421 }
1422 // Update cblk->npasses and write length information
1423 cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1424 cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1425
1426 if ((cblk->modes & JPEG2000_CTSY_HTJ2K_F) && cblk->ht_plhd == HT_PLHD_OFF) {
1427 newpasses -= (uint8_t) segment_passes;
1428 while (newpasses > 0) {
1429 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1430 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1431 bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1432 segment_bytes = get_bits(s, bits_to_read);
1433 newpasses -= (uint8_t) (segment_passes);
1434 // This is a FAST Refinement pass
1435 // Write length information for HT Refinement segment
1436 cblk->pass_lengths[1] += segment_bytes;
1437 // Update cblk->npasses and write length information
1438 cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1439 cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1440 }
1441 } else {
1442 newpasses -= (uint8_t) (segment_passes);
1443 while (newpasses > 0) {
1444 if (bypass_term_threshold != 0) {
1445 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1446 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1447 bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1448 } else {
1449 if ((cblk->modes & JPEG2000_CBLK_TERMALL) == 0)
1450 av_log(s->avctx, AV_LOG_WARNING, "Corrupted packet header is found.\n");
1451 segment_passes = 1;
1452 bits_to_read = cblk->lblock;
1453 }
1454 segment_bytes = get_bits(s, bits_to_read);
1455 newpasses -= (uint8_t) (segment_passes);
1456
1457 // Update cblk->npasses and write length information
1458 cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1459 cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1460 }
1461 }
1462
1463 for (int i = 0; i < cblk->nb_lengthinc; ++i)
1464 tmp_length = (tmp_length < cblk->lengthinc[i]) ? cblk->lengthinc[i] : tmp_length;
1465
1466 if (tmp_length > cblk->data_allocated) {
1467 size_t new_size = FFMAX(2 * cblk->data_allocated, tmp_length);
1468 void *new = av_realloc(cblk->data, new_size);
1469 if (new) {
1470 cblk->data = new;
1471 cblk->data_allocated = new_size;
1472 }
1473 }
1474 if (tmp_length > cblk->data_allocated) {
1475 avpriv_request_sample(s->avctx,
1476 "Block with lengthinc greater than %zu",
1477 cblk->data_allocated);
1478 return AVERROR_PATCHWELCOME;
1479 }
1480 } else {
1481 // This codeblock has no contribution to the current packet
1482 continue;
1483 }
1484 }
1485 }
1487
1488 if (codsty->csty & JPEG2000_CSTY_EPH) {
1489 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1490 bytestream2_skip(&s->g, 2);
1491 else
1492 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1493 }
1494
1495 // Save state of stream
1496 if (s->has_ppm) {
1497 tile->tile_part[*tp_index].header_tpg = s->g;
1498 select_stream(s, tile, tp_index, codsty);
1499 } else if (tile->has_ppt) {
1500 tile->packed_headers_stream = s->g;
1501 select_stream(s, tile, tp_index, codsty);
1502 }
1503 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1504 Jpeg2000Band *band = rlevel->band + bandno;
1505 Jpeg2000Prec *prec = band->prec + precno;
1506
1507 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1508 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1509 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1510 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1511 continue;
1512 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1513 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1514 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1515 void *new = av_realloc(cblk->data, new_size);
1516 if (new) {
1517 cblk->data = new;
1518 cblk->data_allocated = new_size;
1519 }
1520 }
1521 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1522 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1523 ) {
1524 av_log(s->avctx, AV_LOG_ERROR,
1525 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1526 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1527 return AVERROR_INVALIDDATA;
1528 }
1529
1530 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1531 cblk->length += cblk->lengthinc[cwsno];
1532 memset(cblk->data + cblk->length, 0, 4);
1533 cblk->lengthinc[cwsno] = 0;
1534 if (cblk->nb_terminationsinc) {
1535 cblk->nb_terminationsinc--;
1536 cblk->nb_terminations++;
1537 cblk->data[cblk->length++] = 0xFF;
1538 cblk->data[cblk->length++] = 0xFF;
1539 cblk->data_start[cblk->nb_terminations] = cblk->length;
1540 }
1541 }
1542 av_freep(&cblk->lengthinc);
1543 cblk->nb_lengthinc = 0;
1544 }
1545 }
1546 // Save state of stream
1547 tile->tile_part[*tp_index].tpg = s->g;
1548 return 0;
1549
1550skip_data:
1551 if (codsty->csty & JPEG2000_CSTY_EPH) {
1552 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1553 bytestream2_skip(&s->g, 2);
1554 else
1555 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1556 }
1557 if (s->has_ppm) {
1558 tile->tile_part[*tp_index].header_tpg = s->g;
1559 select_stream(s, tile, tp_index, codsty);
1560 } else if (tile->has_ppt) {
1561 tile->packed_headers_stream = s->g;
1562 select_stream(s, tile, tp_index, codsty);
1563 }
1564 tile->tile_part[*tp_index].tpg = s->g;
1565 return 0;
1566}
1567
1569 int RSpoc, int CSpoc,
1570 int LYEpoc, int REpoc, int CEpoc,
1571 int Ppoc, int *tp_index)
1572{
1573 int ret = 0;
1574 int layno, reslevelno, compno, precno, ok_reslevel;
1575 int x, y;
1576 int step_x, step_y;
1577
1578 switch (Ppoc) {
1579 case JPEG2000_PGOD_RLCP:
1580 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1581 ok_reslevel = 1;
1582 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1583 ok_reslevel = 0;
1584 for (layno = 0; layno < LYEpoc; layno++) {
1585 for (compno = CSpoc; compno < CEpoc; compno++) {
1586 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1587 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1588 if (reslevelno < codsty->nreslevels) {
1589 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1590 reslevelno;
1591 ok_reslevel = 1;
1592 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1593 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1594 codsty, rlevel,
1595 precno, layno,
1596 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1597 qntsty->nguardbits)) < 0)
1598 return ret;
1599 }
1600 }
1601 }
1602 }
1603 break;
1604
1605 case JPEG2000_PGOD_LRCP:
1606 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1607 for (layno = 0; layno < LYEpoc; layno++) {
1608 ok_reslevel = 1;
1609 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1610 ok_reslevel = 0;
1611 for (compno = CSpoc; compno < CEpoc; compno++) {
1612 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1613 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1614 if (reslevelno < codsty->nreslevels) {
1615 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1616 reslevelno;
1617 ok_reslevel = 1;
1618 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1619 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1620 codsty, rlevel,
1621 precno, layno,
1622 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1623 qntsty->nguardbits)) < 0)
1624 return ret;
1625 }
1626 }
1627 }
1628 }
1629 break;
1630
1631 case JPEG2000_PGOD_CPRL:
1632 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1633 for (compno = CSpoc; compno < CEpoc; compno++) {
1634 Jpeg2000Component *comp = tile->comp + compno;
1635 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1636 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1637 step_x = 32;
1638 step_y = 32;
1639
1640 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1641 continue;
1642
1643 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1644 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1645 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1646 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1647 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1648 }
1649 if (step_x >= 31 || step_y >= 31){
1650 avpriv_request_sample(s->avctx, "CPRL with large step");
1651 return AVERROR_PATCHWELCOME;
1652 }
1653 step_x = 1<<step_x;
1654 step_y = 1<<step_y;
1655
1656 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1657 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1658 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1659 unsigned prcx, prcy;
1660 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1661 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1662 int xc = x / s->cdx[compno];
1663 int yc = y / s->cdy[compno];
1664
1665 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1666 continue;
1667
1668 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1669 continue;
1670
1671 // check if a precinct exists
1672 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1673 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1674 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1675 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1676
1677 precno = prcx + rlevel->num_precincts_x * prcy;
1678
1679 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1680 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1681 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1682 continue;
1683 }
1684
1685 for (layno = 0; layno < LYEpoc; layno++) {
1686 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1687 precno, layno,
1688 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1689 qntsty->nguardbits)) < 0)
1690 return ret;
1691 }
1692 }
1693 }
1694 }
1695 }
1696 break;
1697
1698 case JPEG2000_PGOD_RPCL:
1699 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1700 ok_reslevel = 1;
1701 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1702 ok_reslevel = 0;
1703 step_x = 30;
1704 step_y = 30;
1705 for (compno = CSpoc; compno < CEpoc; compno++) {
1706 Jpeg2000Component *comp = tile->comp + compno;
1707 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1708
1709 if (reslevelno < codsty->nreslevels) {
1710 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1711 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1712 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1713 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1714 }
1715 }
1716 step_x = 1<<step_x;
1717 step_y = 1<<step_y;
1718
1719 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1720 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1721 for (compno = CSpoc; compno < CEpoc; compno++) {
1722 Jpeg2000Component *comp = tile->comp + compno;
1723 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1724 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1725 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1726 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1727 unsigned prcx, prcy;
1728 int trx0, try0;
1729
1730 if (!s->cdx[compno] || !s->cdy[compno])
1731 return AVERROR_INVALIDDATA;
1732
1733 if (reslevelno >= codsty->nreslevels)
1734 continue;
1735
1736 trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1737 try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1738
1739 if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1740 (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1741 continue;
1742
1743 if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1744 (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1745 continue;
1746
1747 // check if a precinct exists
1748 prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1749 prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1750 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1751 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1752
1753 precno = prcx + rlevel->num_precincts_x * prcy;
1754
1755 ok_reslevel = 1;
1756 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1757 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1758 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1759 continue;
1760 }
1761
1762 for (layno = 0; layno < LYEpoc; layno++) {
1763 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1764 codsty, rlevel,
1765 precno, layno,
1766 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1767 qntsty->nguardbits)) < 0)
1768 return ret;
1769 }
1770 }
1771 }
1772 }
1773 }
1774 break;
1775
1776 case JPEG2000_PGOD_PCRL:
1777 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1778 step_x = 32;
1779 step_y = 32;
1780 for (compno = CSpoc; compno < CEpoc; compno++) {
1781 Jpeg2000Component *comp = tile->comp + compno;
1782 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1783
1784 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1785 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1786 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1787 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1788 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1789 }
1790 }
1791 if (step_x >= 31 || step_y >= 31){
1792 avpriv_request_sample(s->avctx, "PCRL with large step");
1793 return AVERROR_PATCHWELCOME;
1794 }
1795 step_x = 1<<step_x;
1796 step_y = 1<<step_y;
1797
1798 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1799 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1800 for (compno = CSpoc; compno < CEpoc; compno++) {
1801 Jpeg2000Component *comp = tile->comp + compno;
1802 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1803 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1804
1805 if (!s->cdx[compno] || !s->cdy[compno])
1806 return AVERROR_INVALIDDATA;
1807
1808 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1809 unsigned prcx, prcy;
1810 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1811 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1812 int trx0, try0;
1813
1814 trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1815 try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1816
1817 if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1818 (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1819 continue;
1820
1821 if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1822 (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1823 continue;
1824
1825 // check if a precinct exists
1826 prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1827 prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1828 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1829 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1830
1831 precno = prcx + rlevel->num_precincts_x * prcy;
1832
1833 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1834 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1835 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1836 continue;
1837 }
1838
1839 for (layno = 0; layno < LYEpoc; layno++) {
1840 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1841 precno, layno,
1842 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1843 qntsty->nguardbits)) < 0)
1844 return ret;
1845 }
1846 }
1847 }
1848 }
1849 }
1850 break;
1851
1852 default:
1853 break;
1854 }
1855
1856 return ret;
1857}
1858
1860{
1861 int ret = AVERROR_BUG;
1862 int i;
1863 int tp_index = 0;
1864
1865 s->bit_index = 8;
1866 if (tile->poc.nb_poc) {
1867 for (i=0; i<tile->poc.nb_poc; i++) {
1868 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1870 e->RSpoc, e->CSpoc,
1871 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1872 e->REpoc,
1873 FFMIN(e->CEpoc, s->ncomponents),
1874 e->Ppoc, &tp_index
1875 );
1876 if (ret < 0)
1877 return ret;
1878 }
1879 } else {
1881 0, 0,
1882 tile->codsty[0].nlayers,
1883 33,
1884 s->ncomponents,
1885 tile->codsty[0].prog_order,
1886 &tp_index
1887 );
1888 }
1889 /* EOC marker reached */
1890 bytestream2_skip(&s->g, 2);
1891
1892 return ret;
1893}
1894
1895/* TIER-1 routines */
1897 int bpno, int bandno,
1898 int vert_causal_ctx_csty_symbol)
1899{
1900 int mask = (3u << bpno)>>1, y0, x, y;
1901
1902 for (y0 = 0; y0 < height; y0 += 4)
1903 for (x = 0; x < width; x++)
1904 for (y = y0; y < height && y < y0 + 4; y++) {
1905 int flags_mask = -1;
1906 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1908 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1909 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1910 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1911 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1912 if (t1->mqc.raw) {
1913 t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno)) << 31;
1914 t1->data[(y) * t1->stride + x] |= mask;
1915 } else {
1916 t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) << 31;
1917 t1->data[(y) * t1->stride + x] |= mask;
1918 }
1920 t1->data[(y) * t1->stride + x] & INT32_MIN);
1921 }
1922 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1923 }
1924 }
1925}
1926
1928 int bpno, int vert_causal_ctx_csty_symbol)
1929{
1930 int phalf;
1931 int y0, x, y;
1932
1933 phalf = 1 << bpno;
1934
1935 for (y0 = 0; y0 < height; y0 += 4)
1936 for (x = 0; x < width; x++)
1937 for (y = y0; y < height && y < y0 + 4; y++)
1938 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1939 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1941 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1942 t1->data[(y) * t1->stride + x] |= phalf >> 1;
1943 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno))
1944 t1->data[(y) * t1->stride + x] |= phalf;
1945 else {
1946 t1->data[(y) * t1->stride + x] &= ~(phalf);
1947
1948 }
1949 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1950 }
1951}
1952
1954 int width, int height, int bpno, int bandno,
1955 int seg_symbols, int vert_causal_ctx_csty_symbol)
1956{
1957 int mask = (3u << bpno)>>1, y0, x, y, runlen, dec;
1958
1959 for (y0 = 0; y0 < height; y0 += 4) {
1960 for (x = 0; x < width; x++) {
1961 int flags_mask = -1;
1962 if (vert_causal_ctx_csty_symbol)
1964 if (y0 + 3 < height &&
1965 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1966 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1967 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1968 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1969 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1970 continue;
1971 runlen = ff_mqc_decode(&t1->mqc,
1972 t1->mqc.cx_states + MQC_CX_UNI);
1973 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1974 t1->mqc.cx_states +
1975 MQC_CX_UNI);
1976 dec = 1;
1977 } else {
1978 runlen = 0;
1979 dec = 0;
1980 }
1981
1982 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1983 int flags_mask = -1;
1984 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1986 if (!dec) {
1987 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1988 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1989 bandno));
1990 }
1991 }
1992 if (dec) {
1993 int xorbit;
1994 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1995 &xorbit);
1996 t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) << 31;
1997 t1->data[(y) * t1->stride + x] |= mask;
1998 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] & INT32_MIN);
1999 }
2000 dec = 0;
2001 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
2002 }
2003 }
2004 }
2005 if (seg_symbols) {
2006 int val;
2008 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
2009 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
2010 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
2011 if (val != 0xa)
2012 av_log(s->avctx, AV_LOG_ERROR,
2013 "Segmentation symbol value incorrect\n");
2014 }
2015}
2016
2019 int width, int height, int bandpos, uint8_t roi_shift, const int M_b)
2020{
2021 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + 31 - M_b - 1 - roi_shift;
2022 int pass_cnt = 0;
2023 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
2024 int term_cnt = 0;
2025 int coder_type;
2026
2027 av_assert0(width <= 1024U && height <= 1024U);
2028 av_assert0(width*height <= 4096);
2029
2030 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
2031
2032 /* If code-block contains no compressed data: nothing to do. */
2033 if (!cblk->length)
2034 return 0;
2035
2036 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
2037
2038 cblk->data[cblk->length] = 0xff;
2039 cblk->data[cblk->length+1] = 0xff;
2040 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
2041
2042 while (passno--) {
2043 if (bpno < -1 || bpno > 29) {
2044 av_log(s->avctx, AV_LOG_ERROR, "bpno (%d) became invalid\n", bpno);
2045 return AVERROR_INVALIDDATA;
2046 }
2047 switch(pass_t) {
2048 case 0:
2049 decode_sigpass(t1, width, height, bpno + 1, bandpos,
2050 vert_causal_ctx_csty_symbol);
2051 break;
2052 case 1:
2053 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
2054 break;
2055 case 2:
2056 av_assert2(!t1->mqc.raw);
2057 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
2059 vert_causal_ctx_csty_symbol);
2060 break;
2061 }
2062 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
2064
2065 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
2066 if (term_cnt >= cblk->nb_terminations) {
2067 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
2068 return AVERROR_INVALIDDATA;
2069 }
2070 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
2071 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %td in pass %d of %d\n",
2072 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
2073 pass_cnt, cblk->npasses);
2074 }
2075
2076 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
2077 }
2078
2079 pass_t++;
2080 if (pass_t == 3) {
2081 bpno--;
2082 pass_t = 0;
2083 }
2084 pass_cnt ++;
2085 }
2086
2087 if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
2088 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %td\n",
2089 cblk->data + cblk->length - 2 - t1->mqc.bp);
2090 }
2091
2092 if (cblk->data + cblk->length < t1->mqc.bp) {
2093 av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker Read.\n");
2094 }
2095
2096 /* Reconstruct the sample values */
2097 for (int y = 0; y < height; y++) {
2098 for (int x = 0; x < width; x++) {
2099 int32_t sign, n, val;
2100 const uint32_t mask = (int64_t)UINT32_MAX >> (M_b + 1); // bit mask for ROI detection
2101
2102 n = x + (y * t1->stride);
2103 val = t1->data[n];
2104 sign = val & INT32_MIN;
2105 val &= INT32_MAX;
2106 /* ROI shift, if necessary */
2107 if (roi_shift && (((uint32_t)val & ~mask) == 0))
2108 val = (uint32_t)val << roi_shift;
2109 t1->data[n] = val | sign; /* NOTE: Binary point for reconstruction value is located in 31 - M_b */
2110 }
2111 }
2112 return 1;
2113}
2114
2115/* TODO: Verify dequantization for lossless case
2116 * comp->data can be float or int
2117 * band->stepsize can be float or int
2118 * depending on the type of DWT transformation.
2119 * see ISO/IEC 15444-1:2002 A.6.1 */
2120
2121/* Float dequantization of a codeblock.*/
2122static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
2124 Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2125{
2126 int i, j;
2127 int w = cblk->coord[0][1] - cblk->coord[0][0];
2128 const int downshift = 31 - M_b;
2129 float fscale = band->f_stepsize;
2130 fscale /= (float)(1LL << downshift);
2131 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2132 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2133 int *src = t1->data + j*t1->stride;
2134 for (i = 0; i < w; ++i) {
2135 int val = src[i];
2136 if (val < 0) // Convert sign-magnitude to two's complement
2137 val = -(val & INT32_MAX);
2138 datap[i] = (float)val * fscale;
2139 }
2140 }
2141}
2142
2143/* Integer dequantization of a codeblock.*/
2144static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
2146 Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2147{
2148 int i, j;
2149 const int downshift = FFMIN(31 - M_b, 31);
2150 int w = cblk->coord[0][1] - cblk->coord[0][0];
2151 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2152 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2153 int *src = t1->data + j*t1->stride;
2154 if (band->i_stepsize == 32768) {
2155 for (i = 0; i < w; ++i) {
2156 int val = src[i];
2157 if (val < 0) // Convert sign-magnitude to two's complement
2158 val = -((val & INT32_MAX) >> downshift);
2159 else
2160 val >>= downshift;
2161 datap[i] = val;
2162 }
2163 } else {
2164 // This should be VERY uncommon
2165 for (i = 0; i < w; ++i) {
2166 int val = src[i];
2167 if (val < 0) // Convert sign-magnitude to two's complement
2168 val = -((val & INT32_MAX) >> downshift);
2169 else
2170 val >>= downshift;
2171 datap[i] = (val * (int64_t)band->i_stepsize) / 65536;
2172 }
2173 }
2174 }
2175}
2176
2177static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
2179 Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2180{
2181 int i, j;
2182 int w = cblk->coord[0][1] - cblk->coord[0][0];
2183 float fscale = band->f_stepsize;
2184 const int downshift = 31 - M_b;
2185 const int PRESCALE = 6; // At least 6 is required to pass the conformance tests in ISO/IEC 15444-4
2186 int scale;
2187
2188 fscale /= (float)(1LL << downshift);
2189 fscale *= (float)(1 << PRESCALE);
2190 fscale *= (float)(1 << (16 + I_PRESHIFT));
2191 scale = (int)(fscale + 0.5);
2192 band->i_stepsize = scale;
2193 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2194 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2195 int *src = t1->data + j*t1->stride;
2196 for (i = 0; i < w; ++i) {
2197 int val = src[i];
2198 if (val < 0) // Convert sign-magnitude to two's complement
2199 val = -(val & INT32_MAX);
2200 // Shifting down to prevent overflow in dequantization
2201 val = (val + (1LL << (PRESCALE - 1))) >> PRESCALE;
2202 datap[i] = RSHIFT(val * (int64_t)band->i_stepsize, 16);
2203 }
2204 }
2205}
2206
2208{
2209 int i, csize = 1;
2210 void *src[3];
2211
2212 for (i = 1; i < 3; i++) {
2213 if (tile->codsty[0].transform != tile->codsty[i].transform) {
2214 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
2215 return;
2216 }
2217 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
2218 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
2219 return;
2220 }
2221 }
2222
2223 for (i = 0; i < 3; i++)
2224 if (tile->codsty[0].transform == FF_DWT97)
2225 src[i] = tile->comp[i].f_data;
2226 else
2227 src[i] = tile->comp[i].i_data;
2228
2229 for (i = 0; i < 2; i++)
2230 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
2231
2232 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
2233}
2234
2235
2237{
2239
2240 int compno, reslevelno, bandno;
2241
2242 /* Loop on tile components */
2243 for (compno = 0; compno < s->ncomponents; compno++) {
2244 Jpeg2000Component *comp = tile->comp + compno;
2245 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
2246 Jpeg2000QuantStyle *quantsty = tile->qntsty + compno;
2247
2248 int coded = 0;
2249 int subbandno = 0;
2250
2251 t1.stride = (1<<codsty->log2_cblk_width) + 2;
2252
2253 /* Loop on resolution levels */
2254 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
2255 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
2256 /* Loop on bands */
2257 for (bandno = 0; bandno < rlevel->nbands; bandno++, subbandno++) {
2258 int nb_precincts, precno;
2259 Jpeg2000Band *band = rlevel->band + bandno;
2260 int cblkno = 0, bandpos;
2261 /* See Rec. ITU-T T.800, Equation E-2 */
2262 int M_b = quantsty->expn[subbandno] + quantsty->nguardbits - 1;
2263
2264 bandpos = bandno + (reslevelno > 0);
2265
2266 if (band->coord[0][0] == band->coord[0][1] ||
2267 band->coord[1][0] == band->coord[1][1])
2268 continue;
2269
2270 if (M_b > 31) {
2271 avpriv_request_sample(s->avctx, "M_b (%d) > 31", M_b);
2272 return AVERROR_PATCHWELCOME;
2273 }
2274
2275 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
2276 /* Loop on precincts */
2277 for (precno = 0; precno < nb_precincts; precno++) {
2278 Jpeg2000Prec *prec = band->prec + precno;
2279
2280 /* Loop on codeblocks */
2281 for (cblkno = 0;
2282 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
2283 cblkno++) {
2284 int x, y, ret;
2285
2286 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
2287
2288 if (cblk->modes & JPEG2000_CTSY_HTJ2K_F)
2289 ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
2290 cblk->coord[0][1] - cblk->coord[0][0],
2291 cblk->coord[1][1] - cblk->coord[1][0],
2292 M_b, comp->roi_shift);
2293 else
2294 ret = decode_cblk(s, codsty, &t1, cblk,
2295 cblk->coord[0][1] - cblk->coord[0][0],
2296 cblk->coord[1][1] - cblk->coord[1][0],
2297 bandpos, comp->roi_shift, M_b);
2298
2299 if (ret)
2300 coded = 1;
2301 else
2302 continue;
2303 x = cblk->coord[0][0] - band->coord[0][0];
2304 y = cblk->coord[1][0] - band->coord[1][0];
2305
2306 if (codsty->transform == FF_DWT97)
2307 dequantization_float(x, y, cblk, comp, &t1, band, M_b);
2308 else if (codsty->transform == FF_DWT97_INT)
2309 dequantization_int_97(x, y, cblk, comp, &t1, band, M_b);
2310 else
2311 dequantization_int(x, y, cblk, comp, &t1, band, M_b);
2312 } /* end cblk */
2313 } /*end prec */
2314 } /* end band */
2315 } /* end reslevel */
2316
2317 /* inverse DWT */
2318 if (coded)
2319 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
2320
2321 } /*end comp */
2322 return 0;
2323}
2324
2325#define WRITE_FRAME(D, PIXEL) \
2326 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2327 AVFrame * picture, int precision) \
2328 { \
2329 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2330 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2331 int pixelsize = planar ? 1 : pixdesc->nb_components; \
2332 \
2333 int compno; \
2334 int x, y; \
2335 \
2336 for (compno = 0; compno < s->ncomponents; compno++) { \
2337 Jpeg2000Component *comp = tile->comp + compno; \
2338 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2339 PIXEL *line; \
2340 float *datap = comp->f_data; \
2341 int32_t *i_datap = comp->i_data; \
2342 int cbps = s->cbps[compno]; \
2343 int w = tile->comp[compno].coord[0][1] - \
2344 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2345 int h = tile->comp[compno].coord[1][1] - \
2346 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2347 int plane = 0; \
2348 ptrdiff_t dstoffset = 0; \
2349 \
2350 if (planar) \
2351 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2352 else \
2353 dstoffset = s->cdef[compno] ? s->cdef[compno] - 1 : compno; \
2354 \
2355 y = tile->comp[compno].coord[1][0] - \
2356 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2357 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2358 for (; y < h; y++) { \
2359 PIXEL *dst; \
2360 \
2361 x = tile->comp[compno].coord[0][0] - \
2362 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2363 dst = line + x * pixelsize + dstoffset; \
2364 \
2365 if (codsty->transform == FF_DWT97) { \
2366 for (; x < w; x++) { \
2367 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2368 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2369 val = av_clip(val, 0, (1 << cbps) - 1); \
2370 *dst = val << (precision - cbps); \
2371 datap++; \
2372 dst += pixelsize; \
2373 } \
2374 } else { \
2375 for (; x < w; x++) { \
2376 int val = *i_datap + (1 << (cbps - 1)); \
2377 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2378 val = av_clip(val, 0, (1 << cbps) - 1); \
2379 *dst = val << (precision - cbps); \
2380 i_datap++; \
2381 dst += pixelsize; \
2382 } \
2383 } \
2384 line += picture->linesize[plane] / sizeof(PIXEL); \
2385 } \
2386 } \
2387 \
2388 }
2389
2390WRITE_FRAME(8, uint8_t)
2391WRITE_FRAME(16, uint16_t)
2392
2393#undef WRITE_FRAME
2394
2395static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2396 int jobnr, int threadnr)
2397{
2398 const Jpeg2000DecoderContext *s = avctx->priv_data;
2399 AVFrame *picture = td;
2400 Jpeg2000Tile *tile = s->tile + jobnr;
2401
2402 int ret = tile_codeblocks(s, tile);
2403 if (ret < 0)
2404 return ret;
2405
2406 /* inverse MCT transformation */
2407 if (tile->codsty[0].mct)
2408 mct_decode(s, tile);
2409
2410 if (s->precision <= 8) {
2411 write_frame_8(s, tile, picture, 8);
2412 } else {
2413 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2414 picture->format == AV_PIX_FMT_RGB48 ||
2415 picture->format == AV_PIX_FMT_RGBA64 ||
2416 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2417
2418 write_frame_16(s, tile, picture, precision);
2419 }
2420
2421 return 0;
2422}
2423
2425{
2426 int tileno, compno;
2427 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2428 if (s->tile[tileno].comp) {
2429 for (compno = 0; compno < s->ncomponents; compno++) {
2430 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2431 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2432
2433 ff_jpeg2000_cleanup(comp, codsty);
2434 }
2435 av_freep(&s->tile[tileno].comp);
2436 av_freep(&s->tile[tileno].packed_headers);
2437 s->tile[tileno].packed_headers_size = 0;
2438 }
2439 }
2440 av_freep(&s->packed_headers);
2441 s->packed_headers_size = 0;
2442 memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
2443 av_freep(&s->tile);
2444 memset(s->codsty, 0, sizeof(s->codsty));
2445 memset(s->qntsty, 0, sizeof(s->qntsty));
2446 memset(s->properties, 0, sizeof(s->properties));
2447 memset(&s->poc , 0, sizeof(s->poc));
2448 memset(s->roi_shift, 0, sizeof(s->roi_shift));
2449 s->numXtiles = s->numYtiles = 0;
2450 s->ncomponents = 0;
2451 s->has_ppm = 0;
2452 s->isHT = 0;
2453 s->precision = 0;
2454 s->colour_space = 0;
2455 s->pal8 = 0;
2456}
2457
2459{
2460 Jpeg2000CodingStyle *codsty = s->codsty;
2461 Jpeg2000QuantStyle *qntsty = s->qntsty;
2462 Jpeg2000POC *poc = &s->poc;
2463 uint8_t *properties = s->properties;
2464 uint8_t in_tile_headers = 0;
2465
2466 for (;;) {
2467 int len, ret = 0;
2468 uint16_t marker;
2469 int oldpos;
2470
2471 if (bytestream2_get_bytes_left(&s->g) < 2) {
2472 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2473 break;
2474 }
2475
2476 marker = bytestream2_get_be16u(&s->g);
2477 oldpos = bytestream2_tell(&s->g);
2478 if (marker >= 0xFF30 && marker <= 0xFF3F)
2479 continue;
2480 if (marker == JPEG2000_SOD) {
2482 Jpeg2000TilePart *tp;
2483
2484 if (!s->tile) {
2485 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2486 return AVERROR_INVALIDDATA;
2487 }
2488 if (s->curtileno < 0) {
2489 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2490 return AVERROR_INVALIDDATA;
2491 }
2492
2493 tile = s->tile + s->curtileno;
2494 tp = tile->tile_part + tile->tp_idx;
2495 if (tp->tp_end < s->g.buffer) {
2496 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2497 return AVERROR_INVALIDDATA;
2498 }
2499
2500 if (s->has_ppm) {
2501 uint32_t tp_header_size = bytestream2_get_be32(&s->packed_headers_stream);
2502 if (bytestream2_get_bytes_left(&s->packed_headers_stream) < tp_header_size)
2503 return AVERROR_INVALIDDATA;
2504 bytestream2_init(&tp->header_tpg, s->packed_headers_stream.buffer, tp_header_size);
2505 bytestream2_skip(&s->packed_headers_stream, tp_header_size);
2506 }
2507 if (tile->has_ppt && tile->tp_idx == 0) {
2508 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2509 }
2510
2511 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2512 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2513
2514 continue;
2515 }
2516 if (marker == JPEG2000_EOC)
2517 break;
2518
2519 len = bytestream2_get_be16(&s->g);
2520 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2521 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2522 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2523 return AVERROR_INVALIDDATA;
2524 }
2525 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2526 break;
2527 }
2528
2529 switch (marker) {
2530 case JPEG2000_SIZ:
2531 if (s->ncomponents) {
2532 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2533 return AVERROR_INVALIDDATA;
2534 }
2535 ret = get_siz(s);
2536 if (!s->tile)
2537 s->numXtiles = s->numYtiles = 0;
2538 break;
2539 case JPEG2000_CAP:
2540 if (!s->ncomponents) {
2541 av_log(s->avctx, AV_LOG_ERROR, "CAP marker segment shall come after SIZ\n");
2542 return AVERROR_INVALIDDATA;
2543 }
2544 ret = get_cap(s, codsty);
2545 break;
2546 case JPEG2000_COC:
2547 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2548 av_log(s->avctx, AV_LOG_ERROR,
2549 "COC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2550 return AVERROR_INVALIDDATA;
2551 }
2552 ret = get_coc(s, codsty, properties);
2553 break;
2554 case JPEG2000_COD:
2555 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2556 av_log(s->avctx, AV_LOG_ERROR,
2557 "COD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2558 return AVERROR_INVALIDDATA;
2559 }
2560 ret = get_cod(s, codsty, properties);
2561 break;
2562 case JPEG2000_RGN:
2563 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2564 av_log(s->avctx, AV_LOG_ERROR,
2565 "RGN marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2566 return AVERROR_INVALIDDATA;
2567 }
2568 ret = get_rgn(s, len);
2569 if ((!s->Ccap15_b12) && s->isHT) {
2570 av_log(s->avctx, AV_LOG_ERROR, "RGN marker found but the codestream belongs to the RGNFREE set\n");
2571 return AVERROR_INVALIDDATA;
2572 }
2573 break;
2574 case JPEG2000_QCC:
2575 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2576 av_log(s->avctx, AV_LOG_ERROR,
2577 "QCC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2578 return AVERROR_INVALIDDATA;
2579 }
2580 ret = get_qcc(s, len, qntsty, properties);
2581 break;
2582 case JPEG2000_QCD:
2583 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2584 av_log(s->avctx, AV_LOG_ERROR,
2585 "QCD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2586 return AVERROR_INVALIDDATA;
2587 }
2588 ret = get_qcd(s, len, qntsty, properties);
2589 break;
2590 case JPEG2000_POC:
2591 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2592 av_log(s->avctx, AV_LOG_ERROR,
2593 "POC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2594 return AVERROR_INVALIDDATA;
2595 }
2596 ret = get_poc(s, len, poc);
2597 break;
2598 case JPEG2000_SOT:
2599 if (!in_tile_headers) {
2600 in_tile_headers = 1;
2601 if (s->has_ppm) {
2602 bytestream2_init(&s->packed_headers_stream, s->packed_headers, s->packed_headers_size);
2603 }
2604 }
2605 if (!(ret = get_sot(s, len))) {
2606 av_assert1(s->curtileno >= 0);
2607 codsty = s->tile[s->curtileno].codsty;
2608 qntsty = s->tile[s->curtileno].qntsty;
2609 poc = &s->tile[s->curtileno].poc;
2610 properties = s->tile[s->curtileno].properties;
2611 }
2612 break;
2613 case JPEG2000_PLM:
2614 // the PLM marker is ignored
2615 case JPEG2000_COM:
2616 // the comment is ignored
2617 bytestream2_skip(&s->g, len - 2);
2618 break;
2619 case JPEG2000_CRG:
2620 ret = read_crg(s, len);
2621 break;
2622 case JPEG2000_TLM:
2623 // Tile-part lengths
2624 ret = get_tlm(s, len);
2625 break;
2626 case JPEG2000_PLT:
2627 // Packet length, tile-part header
2628 ret = get_plt(s, len);
2629 break;
2630 case JPEG2000_PPM:
2631 // Packed headers, main header
2632 if (in_tile_headers) {
2633 av_log(s->avctx, AV_LOG_ERROR, "PPM Marker can only be in Main header\n");
2634 return AVERROR_INVALIDDATA;
2635 }
2636 ret = get_ppm(s, len);
2637 break;
2638 case JPEG2000_PPT:
2639 // Packed headers, tile-part header
2640 if (s->has_ppm) {
2641 av_log(s->avctx, AV_LOG_ERROR,
2642 "Cannot have both PPT and PPM marker.\n");
2643 return AVERROR_INVALIDDATA;
2644 }
2645 if ((!s->Ccap15_b11) && s->isHT) {
2646 av_log(s->avctx, AV_LOG_ERROR, "PPT marker found but the codestream belongs to the HOMOGENEOUS set\n");
2647 return AVERROR_INVALIDDATA;
2648 }
2649 ret = get_ppt(s, len);
2650 break;
2651 case JPEG2000_CPF:
2652 // Corresponding profile marker
2653 ret = read_cpf(s, len);
2654 break;
2655 default:
2656 av_log(s->avctx, AV_LOG_ERROR,
2657 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2658 marker, bytestream2_tell(&s->g) - 4);
2659 bytestream2_skip(&s->g, len - 2);
2660 break;
2661 }
2662 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2663 av_log(s->avctx, AV_LOG_ERROR,
2664 "error during processing marker segment %.4"PRIx16"\n",
2665 marker);
2666 return ret ? ret : -1;
2667 }
2668 }
2669 return 0;
2670}
2671
2672/* Read bit stream packets --> T2 operation. */
2674{
2675 int ret = 0;
2676 int tileno;
2677
2678 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2679 Jpeg2000Tile *tile = s->tile + tileno;
2680
2681 if ((ret = init_tile(s, tileno)) < 0)
2682 return ret;
2683
2684 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2685 return ret;
2686 }
2687
2688 return 0;
2689}
2690
2692{
2693 uint32_t atom_size, atom, atom_end;
2694 int search_range = 10;
2695
2696 while (search_range
2697 &&
2698 bytestream2_get_bytes_left(&s->g) >= 8) {
2699 atom_size = bytestream2_get_be32u(&s->g);
2700 atom = bytestream2_get_be32u(&s->g);
2701 if (atom_size == 1) {
2702 if (bytestream2_get_be32u(&s->g)) {
2703 avpriv_request_sample(s->avctx, "Huge atom");
2704 return 0;
2705 }
2706 atom_size = bytestream2_get_be32u(&s->g);
2707 if (atom_size < 16 || (int64_t)bytestream2_tell(&s->g) + atom_size - 16 > INT_MAX)
2708 return AVERROR_INVALIDDATA;
2709 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2710 } else {
2711 if (atom_size < 8 || (int64_t)bytestream2_tell(&s->g) + atom_size - 8 > INT_MAX)
2712 return AVERROR_INVALIDDATA;
2713 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2714 }
2715
2716 if (atom == JP2_CODESTREAM)
2717 return 1;
2718
2719 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2720 return 0;
2721
2722 if (atom == JP2_HEADER &&
2723 atom_size >= 16) {
2724 uint32_t atom2_size, atom2, atom2_end;
2725 do {
2726 if (bytestream2_get_bytes_left(&s->g) < 8)
2727 break;
2728 atom2_size = bytestream2_get_be32u(&s->g);
2729 atom2 = bytestream2_get_be32u(&s->g);
2730 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2731 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2732 break;
2733 atom2_size -= 8;
2734 if (atom2 == JP2_CODESTREAM) {
2735 return 1;
2736 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2737 int method = bytestream2_get_byteu(&s->g);
2738 bytestream2_skipu(&s->g, 2);
2739 if (method == 1) {
2740 s->colour_space = bytestream2_get_be32u(&s->g);
2741 }
2742 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2743 int i, size, colour_count, colour_channels, colour_depth[3];
2744 colour_count = bytestream2_get_be16u(&s->g);
2745 colour_channels = bytestream2_get_byteu(&s->g);
2746 // FIXME: Do not ignore channel_sign
2747 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2748 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2749 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2750 size = (colour_depth[0] + 7 >> 3) * colour_count +
2751 (colour_depth[1] + 7 >> 3) * colour_count +
2752 (colour_depth[2] + 7 >> 3) * colour_count;
2753 if (colour_count > AVPALETTE_COUNT ||
2754 colour_channels != 3 ||
2755 colour_depth[0] > 16 ||
2756 colour_depth[1] > 16 ||
2757 colour_depth[2] > 16 ||
2758 atom2_size < size) {
2759 avpriv_request_sample(s->avctx, "Unknown palette");
2760 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2761 continue;
2762 }
2763 s->pal8 = 1;
2764 for (i = 0; i < colour_count; i++) {
2765 uint32_t r, g, b;
2766 if (colour_depth[0] <= 8) {
2767 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2768 r |= r >> colour_depth[0];
2769 } else {
2770 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2771 }
2772 if (colour_depth[1] <= 8) {
2773 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2774 g |= g >> colour_depth[1];
2775 } else {
2776 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2777 }
2778 if (colour_depth[2] <= 8) {
2779 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2780 b |= b >> colour_depth[2];
2781 } else {
2782 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2783 }
2784 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2785 }
2786 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2787 int n = bytestream2_get_be16u(&s->g);
2788 for (; n>0; n--) {
2789 int cn = bytestream2_get_be16(&s->g);
2790 av_unused int typ = bytestream2_get_be16(&s->g);
2791 int asoc = bytestream2_get_be16(&s->g);
2792 if (cn < 4 && asoc < 4)
2793 s->cdef[cn] = asoc;
2794 }
2795 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2796 int64_t vnum, vden, hnum, hden, vexp, hexp;
2797 uint32_t resx;
2798 bytestream2_skip(&s->g, 4);
2799 resx = bytestream2_get_be32u(&s->g);
2800 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2801 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2802 continue;
2803 }
2804 vnum = bytestream2_get_be16u(&s->g);
2805 vden = bytestream2_get_be16u(&s->g);
2806 hnum = bytestream2_get_be16u(&s->g);
2807 hden = bytestream2_get_be16u(&s->g);
2808 vexp = bytestream2_get_byteu(&s->g);
2809 hexp = bytestream2_get_byteu(&s->g);
2810 if (!vnum || !vden || !hnum || !hden) {
2811 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2812 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2813 continue;
2814 }
2815 if (vexp > hexp) {
2816 vexp -= hexp;
2817 hexp = 0;
2818 } else {
2819 hexp -= vexp;
2820 vexp = 0;
2821 }
2822 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2823 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2824 av_reduce(&s->sar.den, &s->sar.num,
2825 hnum * vden * pow(10, hexp),
2826 vnum * hden * pow(10, vexp),
2827 INT32_MAX);
2828 }
2829 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2830 } while (atom_end - atom2_end >= 8);
2831 } else {
2832 search_range--;
2833 }
2834 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2835 }
2836
2837 return 0;
2838}
2839
2841{
2843
2844 if (avctx->lowres)
2845 av_log(avctx, AV_LOG_WARNING, "lowres is overridden by reduction_factor but set anyway\n");
2846 if (!s->reduction_factor && avctx->lowres < JPEG2000_MAX_RESLEVELS) {
2847 s->reduction_factor = avctx->lowres;
2848 }
2849 if (avctx->lowres != s->reduction_factor && avctx->lowres)
2850 return AVERROR(EINVAL);
2851
2852 ff_jpeg2000dsp_init(&s->dsp);
2854
2855 return 0;
2856}
2857
2859 const AVPacket *avpkt)
2860{
2862 int ret;
2863
2864 s->avctx = avctx;
2865 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2866 s->curtileno = -1;
2867 memset(s->cdef, -1, sizeof(s->cdef));
2868
2869 if (bytestream2_get_bytes_left(&s->g) < 2) {
2870 ret = AVERROR_INVALIDDATA;
2871 goto end;
2872 }
2873
2874 // check if the image is in jp2 format
2875 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2876 (bytestream2_get_be32u(&s->g) == 12) &&
2877 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2878 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2879 if (!jp2_find_codestream(s)) {
2880 av_log(avctx, AV_LOG_ERROR,
2881 "Could not find Jpeg2000 codestream atom.\n");
2882 ret = AVERROR_INVALIDDATA;
2883 goto end;
2884 }
2885 } else {
2886 bytestream2_seek(&s->g, 0, SEEK_SET);
2887 }
2888
2889 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2890 bytestream2_skip(&s->g, 1);
2891
2892 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2893 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2894 ret = AVERROR_INVALIDDATA;
2895 goto end;
2896 }
2897 if (ret = jpeg2000_read_main_headers(s))
2898 goto end;
2899
2900 if (s->sar.num && s->sar.den)
2901 avctx->sample_aspect_ratio = s->sar;
2902 s->sar.num = s->sar.den = 0;
2903
2904 if (avctx->skip_frame >= AVDISCARD_ALL) {
2906 return 0;
2907 }
2908
2909 /* get picture buffer */
2910 if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
2911 goto end;
2912
2914 goto end;
2915
2916 for (int x = 0; x < s->ncomponents && s->codsty[x].transform == FF_DWT53;)
2917 if (++x == s->ncomponents)
2918 picture->flags |= AV_FRAME_FLAG_LOSSLESS;
2919
2920 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL,
2921 s->numXtiles * s->numYtiles);
2922
2924
2925 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2926 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2927
2928 return 0;
2929
2930end:
2931 /* Do not leave a partially initialized frame on the output. */
2932 av_frame_unref(picture);
2934 return ret;
2935}
2936
2938{
2939 AVPacket *const avpkt = avctx->internal->in_pkt;
2940 int ret;
2941
2942 /* One codestream per packet. Loop so that skipped frames (produce no
2943 * output) transparently pull the next packet instead of returning to the
2944 * caller empty-handed. */
2945 do {
2946 ret = ff_decode_get_packet(avctx, avpkt);
2947 if (ret < 0)
2948 return ret;
2949
2950 ret = jpeg2000_decode_frame(avctx, frame, avpkt);
2951 av_packet_unref(avpkt);
2952 if (ret < 0)
2953 return ret;
2954 } while (!frame->buf[0]);
2955
2956 return 0;
2957}
2958
2959#define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2960#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2961
2962static const AVOption options[] = {
2963 { "lowres", "Lower the decoding resolution by a power of two",
2964 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2965 { NULL },
2966};
2967
2968static const AVClass jpeg2000_class = {
2969 .class_name = "jpeg2000",
2970 .item_name = av_default_item_name,
2971 .option = options,
2972 .version = LIBAVUTIL_VERSION_INT,
2973};
2974
2976 .p.name = "jpeg2000",
2977 CODEC_LONG_NAME("JPEG 2000"),
2978 .p.type = AVMEDIA_TYPE_VIDEO,
2979 .p.id = AV_CODEC_ID_JPEG2000,
2981 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2984 .p.priv_class = &jpeg2000_class,
2985 .p.max_lowres = 5,
2987 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2988};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_jpeg2000_decoder
#define VD
Definition amfdec.c:787
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition bytestream.h:99
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
static av_always_inline int bytestream2_size(const GetByteContext *g)
Definition bytestream.h:202
static uint64_t SP[8][256]
Definition camellia.c:44
static int FUNC tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_RECEIVE_FRAME_CB(func)
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters if all frames are skipped due to the skip_frame setting...
#define CODEC_LONG_NAME(str)
common internal and external API header
#define av_clip
Definition common.h:100
#define RSHIFT(a, b)
Definition common.h:56
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition decode.c:254
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
#define AV_PROFILE_JPEG2000_DCINEMA_2K
Definition defs.h:165
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition defs.h:59
#define AV_PROFILE_JPEG2000_DCINEMA_4K
Definition defs.h:166
static enum AVPixelFormat pix_fmt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
@ AVDISCARD_ALL
discard all
Definition defs.h:246
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_LOSSLESS
A decoder can use this flag to mark frames which were originally encoded losslessly.
Definition frame.h:715
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition imgutils.c:289
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
misc image utilities
#define r
Definition input.c:42
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define ST
Definition iterm2enc.c:43
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition jpeg2000.c:161
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition jpeg2000.c:590
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, const int cbps, int dx, int dy, AVCodecContext *avctx)
Definition jpeg2000.c:459
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition jpeg2000.c:167
JPEG 2000 structures and defines common to encoder and decoder.
#define JPEG2000_T1_SIG_NB
Definition jpeg2000.h:96
#define JPEG2000_CBLK_VSC
Definition jpeg2000.h:116
#define JPEG2000_MAX_RESLEVELS
Definition jpeg2000.h:82
#define JPEG2000_CSTY_SOP
Definition jpeg2000.h:122
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition jpeg2000.h:247
#define JPEG2000_PGOD_PCRL
Definition jpeg2000.h:131
#define JPEG2000_T1_REF
Definition jpeg2000.h:108
@ JPEG2000_QSTY_NONE
Definition jpeg2000.h:76
@ JPEG2000_QSTY_SI
Definition jpeg2000.h:77
#define JPEG2000_T1_SGN_S
Definition jpeg2000.h:102
#define JPEG2000_CTSY_HTJ2K_F
Definition jpeg2000.h:124
#define JPEG2000_CBLK_RESET
Definition jpeg2000.h:114
#define JPEG2000_T1_VIS
Definition jpeg2000.h:106
static int ff_jpeg2000_ceildiv(int a, int64_t b)
Definition jpeg2000.h:252
#define JPEG2000_CSTY_PREC
Definition jpeg2000.h:121
#define JPEG2000_CBLK_BYPASS
Definition jpeg2000.h:113
#define JPEG2000_CSTY_EPH
Definition jpeg2000.h:123
#define JPEG2000_PGOD_CPRL
Definition jpeg2000.h:132
#define JPEG2000_SOP_BYTE_LENGTH
Definition jpeg2000.h:73
#define JPEG2000_SOP_FIXED_BYTES
Definition jpeg2000.h:72
#define JPEG2000_T1_SIG_SW
Definition jpeg2000.h:95
#define JPEG2000_PGOD_RPCL
Definition jpeg2000.h:130
#define JPEG2000_CBLK_SEGSYM
Definition jpeg2000.h:118
#define JPEG2000_MAX_PASSES
Definition jpeg2000.h:84
#define JPEG2000_CBLK_TERMALL
Definition jpeg2000.h:115
#define JPEG2000_T1_SIG_SE
Definition jpeg2000.h:94
#define JPEG2000_PGOD_RLCP
Definition jpeg2000.h:129
#define JPEG2000_CTSY_HTJ2K_M
Definition jpeg2000.h:125
#define JPEG2000_T1_SIG_S
Definition jpeg2000.h:91
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition jpeg2000.h:289
static int needs_termination(int style, int passno)
Definition jpeg2000.h:338
static int ff_jpeg2000_getrefctxno(int flag)
Definition jpeg2000.h:280
@ JPEG2000_SOT
Definition jpeg2000.h:59
@ JPEG2000_COM
Definition jpeg2000.h:58
@ JPEG2000_CPF
Definition jpeg2000.h:50
@ JPEG2000_PPM
Definition jpeg2000.h:55
@ JPEG2000_QCD
Definition jpeg2000.h:51
@ JPEG2000_PPT
Definition jpeg2000.h:56
@ JPEG2000_PLM
Definition jpeg2000.h:48
@ JPEG2000_CRG
Definition jpeg2000.h:57
@ JPEG2000_QCC
Definition jpeg2000.h:52
@ JPEG2000_SOD
Definition jpeg2000.h:62
@ JPEG2000_COC
Definition jpeg2000.h:46
@ JPEG2000_EPH
Definition jpeg2000.h:61
@ JPEG2000_COD
Definition jpeg2000.h:45
@ JPEG2000_TLM
Definition jpeg2000.h:47
@ JPEG2000_CAP
Definition jpeg2000.h:43
@ JPEG2000_RGN
Definition jpeg2000.h:53
@ JPEG2000_SIZ
Definition jpeg2000.h:44
@ JPEG2000_POC
Definition jpeg2000.h:54
@ JPEG2000_PLT
Definition jpeg2000.h:49
@ JPEG2000_EOC
Definition jpeg2000.h:63
@ JPEG2000_SOC
Definition jpeg2000.h:42
@ HTJ2K_HTONLY
Definition jpeg2000.h:67
@ HTJ2K_HTDECLARED
Definition jpeg2000.h:68
@ HTJ2K_MIXED
Definition jpeg2000.h:69
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition jpeg2000.h:271
#define JPEG2000_T1_SIG
Definition jpeg2000.h:107
#define JPEG2000_PGOD_LRCP
Definition jpeg2000.h:128
#define JPEG2000_MAX_DECLEVELS
Definition jpeg2000.h:81
static enum AVPixelFormat all_pix_fmts[]
static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture, const AVPacket *avpkt)
#define WRITE_FRAME(D, PIXEL)
static enum AVPixelFormat xyz_pix_fmts[]
#define JP2_HEADER
Definition jpeg2000dec.c:53
#define JP2_SIG_TYPE
Definition jpeg2000dec.c:50
static int decode_cblk(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int bandpos, uint8_t roi_shift, const int M_b)
static int get_tlm(Jpeg2000DecoderContext *s, int n)
static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
static int getlblockinc(Jpeg2000DecoderContext *s)
static void decode_refpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int vert_causal_ctx_csty_symbol)
#define GRAY_PIXEL_FORMATS
static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components, int bpc, uint32_t log2_chroma_wh, int pal8)
static int getnpasses(Jpeg2000DecoderContext *s)
static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, const uint8_t *properties)
static void jpeg2000_flush(Jpeg2000DecoderContext *s)
Definition jpeg2000dec.c:86
static enum AVPixelFormat rgb_pix_fmts[]
static int get_siz(Jpeg2000DecoderContext *s)
static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int vert_causal_ctx_csty_symbol)
static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q, uint8_t *properties)
#define YUV_PIXEL_FORMATS
static void decode_clnpass(const Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1, int width, int height, int bpno, int bandno, int seg_symbols, int vert_causal_ctx_csty_symbol)
static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
#define HAD_COC
Definition jpeg2000dec.c:55
static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, const uint8_t *properties)
static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td, int jobnr, int threadnr)
#define RGB_PIXEL_FORMATS
static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
#define HT_MIXED
Definition jpeg2000dec.c:64
static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk, Jpeg2000Component *comp, Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
static const AVClass jpeg2000_class
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int RSpoc, int CSpoc, int LYEpoc, int REpoc, int CEpoc, int Ppoc, int *tp_index)
HT_PLHD_STATUS
Definition jpeg2000dec.c:59
@ HT_PLHD_ON
Definition jpeg2000dec.c:61
@ HT_PLHD_OFF
Definition jpeg2000dec.c:60
static int init_tile(Jpeg2000DecoderContext *s, int tileno)
#define JP2_CODESTREAM
Definition jpeg2000dec.c:52
static void mct_decode(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
static int get_sot(Jpeg2000DecoderContext *s, int n)
static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node, int threshold)
Definition jpeg2000dec.c:94
static void select_header(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile, int *tp_index)
static int jp2_find_codestream(Jpeg2000DecoderContext *s)
static enum AVPixelFormat gray_pix_fmts[]
#define OFFSET(x)
static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
static int get_cap(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
#define XYZ_PIXEL_FORMATS
static int get_ppt(Jpeg2000DecoderContext *s, int n)
static int read_crg(Jpeg2000DecoderContext *s, int n)
static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index, const Jpeg2000CodingStyle *codsty, Jpeg2000ResLevel *rlevel, int precno, int layno, const uint8_t *expn, int numgbits)
static int get_ppm(Jpeg2000DecoderContext *s, int n)
#define JP2_SIG_VALUE
Definition jpeg2000dec.c:51
static int read_cpf(Jpeg2000DecoderContext *s, int n)
static int get_rgn(Jpeg2000DecoderContext *s, int n)
static void select_stream(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile, int *tp_index, const Jpeg2000CodingStyle *codsty)
static int get_bits(Jpeg2000DecoderContext *s, int n)
Definition jpeg2000dec.c:71
static int jpeg2000_receive_frame(AVCodecContext *avctx, AVFrame *frame)
static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
static enum AVPixelFormat yuv_pix_fmts[]
#define HAD_QCC
Definition jpeg2000dec.c:56
static int tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
static int get_plt(Jpeg2000DecoderContext *s, int n)
static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c, uint8_t *properties)
#define MAX_POCS
Definition jpeg2000dec.h:32
int ff_dwt_decode(DWTContext *s, void *t)
@ FF_DWT97_INT
Definition jpeg2000dwt.h:40
@ FF_DWT97
Definition jpeg2000dwt.h:38
@ FF_DWT53
Definition jpeg2000dwt.h:39
#define I_PRESHIFT
Definition jpeg2000dwt.h:35
int ff_jpeg2000_decode_htj2k(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, int width, int height, int M_b, uint8_t roi_shift)
HT Block decoder as specified in Rec.
common internal api header.
av_cold void ff_jpeg2000dsp_init(Jpeg2000DSPContext *c)
Definition jpeg2000dsp.c:93
Multithreading API for decoders.
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_unused
Definition attributes.h:164
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
#define P
void ff_mqc_init_contexts(MqcState *mqc)
MQ-coder context initialisations.
Definition mqc.c:64
int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
MQ decoder.
Definition mqcdec.c:93
#define MQC_CX_UNI
Definition mqc.h:33
#define MQC_CX_RL
Definition mqc.h:34
void ff_mqc_initdec(MqcState *mqc, uint8_t *bp, int raw, int reset)
Initialize MQ-decoder.
Definition mqcdec.c:71
#define av_realloc(p, s)
Definition ops_static.c:54
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
#define AV_PIX_FMT_YA16
Definition pixfmt.h:533
#define AV_PIX_FMT_XYZ12
Definition pixfmt.h:608
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:538
#define AV_PIX_FMT_RGB48
Definition pixfmt.h:534
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition pixfmt.h:105
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition pixfmt.h:140
#define AVPALETTE_COUNT
Definition pixfmt.h:33
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:531
const AVProfile ff_jpeg2000_profiles[]
Definition profiles.c:115
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition avcodec.h:628
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition avcodec.h:1633
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition avcodec.h:1707
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1672
AVPacket * in_pkt
This packet is used to hold the packet given to decoders implementing the .decode API; it is unused b...
Definition internal.h:83
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition frame.h:723
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:566
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Jpeg2000Prec * prec
Definition jpeg2000.h:225
int coord[2][2]
Definition jpeg2000.h:221
float f_stepsize
Definition jpeg2000.h:224
uint8_t modes
Definition jpeg2000.h:206
int nb_terminations
Definition jpeg2000.h:197
uint16_t * lengthinc
Definition jpeg2000.h:192
uint8_t ht_plhd
Definition jpeg2000.h:207
uint16_t length
Definition jpeg2000.h:191
int nb_terminationsinc
Definition jpeg2000.h:198
int coord[2][2]
Definition jpeg2000.h:202
uint8_t * data
Definition jpeg2000.h:195
uint8_t nonzerobits
Definition jpeg2000.h:189
uint8_t nb_lengthinc
Definition jpeg2000.h:193
int pass_lengths[2]
Definition jpeg2000.h:205
uint8_t npasses
Definition jpeg2000.h:187
int * data_start
Definition jpeg2000.h:199
uint8_t lblock
Definition jpeg2000.h:194
size_t data_allocated
Definition jpeg2000.h:196
uint8_t incl
Definition jpeg2000.h:190
uint8_t log2_cblk_width
Definition jpeg2000.h:151
int decoded_layers
Definition jpeg2000.h:216
Jpeg2000Cblk * cblk
Definition jpeg2000.h:215
Jpeg2000TgtNode * zerobits
Definition jpeg2000.h:213
Jpeg2000TgtNode * cblkincl
Definition jpeg2000.h:214
int nb_codeblocks_height
Definition jpeg2000.h:212
int nb_codeblocks_width
Definition jpeg2000.h:211
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition jpeg2000.h:166
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition jpeg2000.h:165
uint8_t log2_prec_width
Definition jpeg2000.h:232
Jpeg2000Band * band
Definition jpeg2000.h:233
uint8_t log2_prec_height
Definition jpeg2000.h:232
int data[6144]
Definition jpeg2000.h:135
uint16_t flags[6156]
Definition jpeg2000.h:136
struct Jpeg2000TgtNode * parent
Definition jpeg2000.h:145
const uint8_t * tp_end
Definition jpeg2000dec.h:51
GetByteContext header_tpg
Definition jpeg2000dec.h:52
GetByteContext tpg
Definition jpeg2000dec.h:53
uint8_t cx_states[19]
Definition mqc.h:45
int raw
Definition mqc.h:46
uint8_t * bp
Definition mqc.h:41
#define av_free(p)
#define av_mallocz(s)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
int num
Definition error.c:23
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
int size
const char * g
Definition vf_curves.c:128
int len
static double c[64]