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 (zbp >= 100 || v > 30 ||
1210 (v < 0 && (cblk->modes & JPEG2000_CTSY_HTJ2K_F))) {
1211 av_log(s->avctx, AV_LOG_ERROR,
1212 "nonzerobits %d invalid or unsupported\n", v);
1213 return AVERROR_INVALIDDATA;
1214 }
1215 cblk->incl = 1;
1216 cblk->nonzerobits = FFMAX(v, 0);
1217 cblk->zbp = zbp;
1218 cblk->lblock = 3;
1219 }
1220 } else {
1221 incl = get_bits(s, 1);
1222 }
1223
1224 if (incl) {
1225 uint8_t bypass_term_threshold = 0;
1226 uint8_t bits_to_read = 0;
1227 uint32_t segment_bytes = 0;
1228 int32_t segment_passes = 0;
1229 uint8_t next_segment_passes = 0;
1230 int32_t href_passes, pass_bound;
1231 uint32_t tmp_length = 0;
1232 int32_t newpasses_copy, npasses_copy;
1233
1234 if ((newpasses = getnpasses(s)) <= 0)
1235 return newpasses;
1236 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1237 avpriv_request_sample(s->avctx, "Too many passes");
1238 return AVERROR_PATCHWELCOME;
1239 }
1240 if ((llen = getlblockinc(s)) < 0)
1241 return llen;
1242 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1243 avpriv_request_sample(s->avctx,
1244 "Block with length beyond 16 bits");
1245 return AVERROR_PATCHWELCOME;
1246 }
1247 cblk->nb_lengthinc = 0;
1248 cblk->nb_terminationsinc = 0;
1249 av_free(cblk->lengthinc);
1250 cblk->lengthinc = av_calloc(newpasses, sizeof(*cblk->lengthinc));
1251 if (!cblk->lengthinc)
1252 return AVERROR(ENOMEM);
1253 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1,
1254 sizeof(*cblk->data_start));
1255 if (!tmp)
1256 return AVERROR(ENOMEM);
1257 cblk->data_start = tmp;
1258 cblk->lblock += llen;
1259
1260 // Count number of necessary terminations for non HT code block
1261 newpasses_copy = newpasses;
1262 npasses_copy = cblk->npasses;
1263 if (!(cblk->modes & JPEG2000_CTSY_HTJ2K_F)) {
1264 do {
1265 int newpasses1 = 0;
1266
1267 while (newpasses1 < newpasses_copy) {
1268 newpasses1++;
1269 if (needs_termination(codsty->cblk_style, npasses_copy + newpasses1 - 1)) {
1270 cblk->nb_terminationsinc++;
1271 break;
1272 }
1273 }
1274 npasses_copy += newpasses1;
1275 newpasses_copy -= newpasses1;
1276 } while (newpasses_copy);
1277 }
1278
1279 if (cblk->ht_plhd) {
1280 href_passes = (cblk->npasses + newpasses - 1) % 3;
1281 segment_passes = newpasses - href_passes;
1282 pass_bound = 2;
1283 bits_to_read = cblk->lblock;
1284 if (segment_passes < 1) {
1285 // No possible HT Cleanup pass here; may have placeholder passes
1286 // or an original J2K block bit-stream (in MIXED mode).
1287 segment_passes = newpasses;
1288 while (pass_bound <= segment_passes) {
1289 bits_to_read++;
1290 pass_bound += pass_bound;
1291 }
1292 segment_bytes = get_bits(s, bits_to_read);
1293 if (segment_bytes) {
1294 if (cblk->modes & HT_MIXED) {
1295 cblk->ht_plhd = HT_PLHD_OFF;
1296 cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1297 }
1298 else {
1299 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1300 }
1301 }
1302 } else {
1303 while (pass_bound <= segment_passes) {
1304 bits_to_read++;
1305 pass_bound += pass_bound;
1306 }
1307 segment_bytes = get_bits(s, bits_to_read);
1308 if (segment_bytes) {
1309 // No more placeholder passes
1310 if (!(cblk->modes & HT_MIXED)) {
1311 // Must be the first HT Cleanup pass
1312 if (segment_bytes < 2)
1313 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1314 next_segment_passes = 2;
1315 cblk->ht_plhd = HT_PLHD_OFF;
1316 // Write length information for HT CleanUp segment
1317 cblk->pass_lengths[0] = segment_bytes;
1318 } else if (cblk->lblock > 3 && segment_bytes > 1
1319 && (segment_bytes >> (bits_to_read - 1)) == 0) {
1320 // Must be the first HT Cleanup pass, since length MSB is 0
1321 next_segment_passes = 2;
1322 cblk->ht_plhd = HT_PLHD_OFF;
1323 // Write length information for HT CleanUp segment
1324 cblk->pass_lengths[0] = segment_bytes;
1325 } else {
1326 // Must have an original (non-HT) block coding pass
1327 cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1328 cblk->ht_plhd = HT_PLHD_OFF;
1329 segment_passes = newpasses;
1330 while (pass_bound <= segment_passes) {
1331 bits_to_read++;
1332 pass_bound += pass_bound;
1333 segment_bytes <<= 1;
1334 segment_bytes += get_bits(s, 1);
1335 }
1336 }
1337 } else {
1338 // Probably parsing placeholder passes, but we need to read an
1339 // extra length bit to verify this, since prior to the first
1340 // HT Cleanup pass, the number of length bits read for a
1341 // contributing code-block is dependent on the number of passes
1342 // being included, as if it were a non-HT code-block.
1343 segment_passes = newpasses;
1344 if (pass_bound <= segment_passes) {
1345 while (1) {
1346 bits_to_read++;
1347 pass_bound += pass_bound;
1348 segment_bytes <<= 1;
1349 segment_bytes += get_bits(s, 1);
1350 if (pass_bound > segment_passes)
1351 break;
1352 }
1353 if (segment_bytes) {
1354 if (cblk->modes & HT_MIXED) {
1355 cblk->modes &= (uint8_t) (~(JPEG2000_CTSY_HTJ2K_F));
1356 cblk->ht_plhd = HT_PLHD_OFF;
1357 } else {
1358 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1359 }
1360 }
1361 }
1362 }
1363 }
1364 } else if (cblk->modes & JPEG2000_CTSY_HTJ2K_F) {
1365 // Quality layer commences with a non-initial HT coding pass
1366 if(bits_to_read != 0)
1367 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1368 segment_passes = cblk->npasses % 3;
1369 if (segment_passes == 0) {
1370 // newpasses is a HT Cleanup pass; next segment has refinement passes
1371 segment_passes = 1;
1372 next_segment_passes = 2;
1373 if (segment_bytes == 1)
1374 av_log(s->avctx, AV_LOG_WARNING, "Length information for a HT-codeblock is invalid\n");
1375 } else {
1376 // newpasses = 1 means npasses is HT SigProp; 2 means newpasses is
1377 // HT MagRef pass
1378 segment_passes = newpasses > 1 ? 3 - segment_passes : 1;
1379 next_segment_passes = 1;
1380 bits_to_read = av_log2(segment_passes);
1381 }
1382 bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1383 segment_bytes = get_bits(s, bits_to_read);
1384 // Write length information for HT Refinement segment
1385 cblk->pass_lengths[1] += segment_bytes;
1386 } else if (!(cblk->modes & (JPEG2000_CBLK_TERMALL | JPEG2000_CBLK_BYPASS))) {
1387 // Common case for non-HT code-blocks; we have only one segment
1388 bits_to_read = (uint8_t) cblk->lblock + av_log2((uint8_t) newpasses);
1389 segment_bytes = get_bits(s, bits_to_read);
1390 segment_passes = newpasses;
1391 } else if (cblk->modes & JPEG2000_CBLK_TERMALL) {
1392 // RESTART MODE
1393 bits_to_read = cblk->lblock;
1394 segment_bytes = get_bits(s, bits_to_read);
1395 segment_passes = 1;
1396 next_segment_passes = 1;
1397 } else {
1398 // BYPASS MODE
1399 bypass_term_threshold = 10;
1400 if(bits_to_read != 0)
1401 av_log(s->avctx, AV_LOG_WARNING, "Length information for a codeblock is invalid\n");
1402 if (cblk->npasses < bypass_term_threshold) {
1403 // May have from 1 to 10 uninterrupted passes before 1st RAW SigProp
1404 segment_passes = bypass_term_threshold - cblk->npasses;
1405 if (segment_passes > newpasses)
1406 segment_passes = newpasses;
1407 while ((2 << bits_to_read) <= segment_passes)
1408 bits_to_read++;
1409 next_segment_passes = 2;
1410 } else if ((cblk->npasses - bypass_term_threshold) % 3 < 2) {
1411 // 0 means newpasses is a RAW SigProp; 1 means newpasses is a RAW MagRef pass
1412 segment_passes = newpasses > 1 ? 2 - (cblk->npasses - bypass_term_threshold) % 3 : 1;
1413 bits_to_read = av_log2(segment_passes);
1414 next_segment_passes = 1;
1415 } else {
1416 // newpasses is an isolated Cleanup pass that precedes a RAW SigProp pass
1417 segment_passes = 1;
1418 next_segment_passes = 2;
1419 }
1420 bits_to_read = (uint8_t) (bits_to_read + cblk->lblock);
1421 segment_bytes = get_bits(s, bits_to_read);
1422 }
1423 // Update cblk->npasses and write length information
1424 cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1425 cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1426
1427 if ((cblk->modes & JPEG2000_CTSY_HTJ2K_F) && cblk->ht_plhd == HT_PLHD_OFF) {
1428 newpasses -= (uint8_t) segment_passes;
1429 while (newpasses > 0) {
1430 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1431 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1432 bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1433 segment_bytes = get_bits(s, bits_to_read);
1434 newpasses -= (uint8_t) (segment_passes);
1435 // This is a FAST Refinement pass
1436 // Write length information for HT Refinement segment
1437 cblk->pass_lengths[1] += segment_bytes;
1438 // Update cblk->npasses and write length information
1439 cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1440 cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1441 }
1442 } else {
1443 newpasses -= (uint8_t) (segment_passes);
1444 while (newpasses > 0) {
1445 if (bypass_term_threshold != 0) {
1446 segment_passes = newpasses > 1 ? next_segment_passes : 1;
1447 next_segment_passes = (uint8_t) (3 - next_segment_passes);
1448 bits_to_read = (uint8_t) (cblk->lblock + av_log2(segment_passes));
1449 } else {
1450 if ((cblk->modes & JPEG2000_CBLK_TERMALL) == 0)
1451 av_log(s->avctx, AV_LOG_WARNING, "Corrupted packet header is found.\n");
1452 segment_passes = 1;
1453 bits_to_read = cblk->lblock;
1454 }
1455 segment_bytes = get_bits(s, bits_to_read);
1456 newpasses -= (uint8_t) (segment_passes);
1457
1458 // Update cblk->npasses and write length information
1459 cblk->npasses = (uint8_t) (cblk->npasses + segment_passes);
1460 cblk->lengthinc[cblk->nb_lengthinc++] = segment_bytes;
1461 }
1462 }
1463
1464 for (int i = 0; i < cblk->nb_lengthinc; ++i)
1465 tmp_length = (tmp_length < cblk->lengthinc[i]) ? cblk->lengthinc[i] : tmp_length;
1466
1467 if (tmp_length > cblk->data_allocated) {
1468 size_t new_size = FFMAX(2 * cblk->data_allocated, tmp_length);
1469 void *new = av_realloc(cblk->data, new_size);
1470 if (new) {
1471 cblk->data = new;
1472 cblk->data_allocated = new_size;
1473 }
1474 }
1475 if (tmp_length > cblk->data_allocated) {
1476 avpriv_request_sample(s->avctx,
1477 "Block with lengthinc greater than %zu",
1478 cblk->data_allocated);
1479 return AVERROR_PATCHWELCOME;
1480 }
1481 } else {
1482 // This codeblock has no contribution to the current packet
1483 continue;
1484 }
1485 }
1486 }
1488
1489 if (codsty->csty & JPEG2000_CSTY_EPH) {
1490 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1491 bytestream2_skip(&s->g, 2);
1492 else
1493 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1494 }
1495
1496 // Save state of stream
1497 if (s->has_ppm) {
1498 tile->tile_part[*tp_index].header_tpg = s->g;
1499 select_stream(s, tile, tp_index, codsty);
1500 } else if (tile->has_ppt) {
1501 tile->packed_headers_stream = s->g;
1502 select_stream(s, tile, tp_index, codsty);
1503 }
1504 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1505 Jpeg2000Band *band = rlevel->band + bandno;
1506 Jpeg2000Prec *prec = band->prec + precno;
1507
1508 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1509 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1510 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1511 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1512 continue;
1513 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1514 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1515 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1516 void *new = av_realloc(cblk->data, new_size);
1517 if (new) {
1518 cblk->data = new;
1519 cblk->data_allocated = new_size;
1520 }
1521 }
1522 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1523 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1524 ) {
1525 av_log(s->avctx, AV_LOG_ERROR,
1526 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1527 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1528 return AVERROR_INVALIDDATA;
1529 }
1530
1531 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1532 cblk->length += cblk->lengthinc[cwsno];
1533 memset(cblk->data + cblk->length, 0, 4);
1534 cblk->lengthinc[cwsno] = 0;
1535 if (cblk->nb_terminationsinc) {
1536 cblk->nb_terminationsinc--;
1537 cblk->nb_terminations++;
1538 cblk->data[cblk->length++] = 0xFF;
1539 cblk->data[cblk->length++] = 0xFF;
1540 cblk->data_start[cblk->nb_terminations] = cblk->length;
1541 }
1542 }
1543 av_freep(&cblk->lengthinc);
1544 cblk->nb_lengthinc = 0;
1545 }
1546 }
1547 // Save state of stream
1548 tile->tile_part[*tp_index].tpg = s->g;
1549 return 0;
1550
1551skip_data:
1552 if (codsty->csty & JPEG2000_CSTY_EPH) {
1553 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1554 bytestream2_skip(&s->g, 2);
1555 else
1556 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1557 }
1558 if (s->has_ppm) {
1559 tile->tile_part[*tp_index].header_tpg = s->g;
1560 select_stream(s, tile, tp_index, codsty);
1561 } else if (tile->has_ppt) {
1562 tile->packed_headers_stream = s->g;
1563 select_stream(s, tile, tp_index, codsty);
1564 }
1565 tile->tile_part[*tp_index].tpg = s->g;
1566 return 0;
1567}
1568
1570 int RSpoc, int CSpoc,
1571 int LYEpoc, int REpoc, int CEpoc,
1572 int Ppoc, int *tp_index)
1573{
1574 int ret = 0;
1575 int layno, reslevelno, compno, precno, ok_reslevel;
1576 int x, y;
1577 int step_x, step_y;
1578
1579 switch (Ppoc) {
1580 case JPEG2000_PGOD_RLCP:
1581 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1582 ok_reslevel = 1;
1583 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1584 ok_reslevel = 0;
1585 for (layno = 0; layno < LYEpoc; layno++) {
1586 for (compno = CSpoc; compno < CEpoc; compno++) {
1587 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1588 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1589 if (reslevelno < codsty->nreslevels) {
1590 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1591 reslevelno;
1592 ok_reslevel = 1;
1593 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1594 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1595 codsty, rlevel,
1596 precno, layno,
1597 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1598 qntsty->nguardbits)) < 0)
1599 return ret;
1600 }
1601 }
1602 }
1603 }
1604 break;
1605
1606 case JPEG2000_PGOD_LRCP:
1607 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1608 for (layno = 0; layno < LYEpoc; layno++) {
1609 ok_reslevel = 1;
1610 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1611 ok_reslevel = 0;
1612 for (compno = CSpoc; compno < CEpoc; compno++) {
1613 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1614 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1615 if (reslevelno < codsty->nreslevels) {
1616 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1617 reslevelno;
1618 ok_reslevel = 1;
1619 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1620 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1621 codsty, rlevel,
1622 precno, layno,
1623 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1624 qntsty->nguardbits)) < 0)
1625 return ret;
1626 }
1627 }
1628 }
1629 }
1630 break;
1631
1632 case JPEG2000_PGOD_CPRL:
1633 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1634 for (compno = CSpoc; compno < CEpoc; compno++) {
1635 Jpeg2000Component *comp = tile->comp + compno;
1636 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1637 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1638 step_x = 32;
1639 step_y = 32;
1640
1641 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1642 continue;
1643
1644 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1645 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1646 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1647 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1648 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1649 }
1650 if (step_x >= 31 || step_y >= 31){
1651 avpriv_request_sample(s->avctx, "CPRL with large step");
1652 return AVERROR_PATCHWELCOME;
1653 }
1654 step_x = 1<<step_x;
1655 step_y = 1<<step_y;
1656
1657 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1658 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1659 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1660 unsigned prcx, prcy;
1661 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1662 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1663 int xc = x / s->cdx[compno];
1664 int yc = y / s->cdy[compno];
1665
1666 if (yc % (1LL << (rlevel->log2_prec_height + reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
1667 continue;
1668
1669 if (xc % (1LL << (rlevel->log2_prec_width + reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
1670 continue;
1671
1672 // check if a precinct exists
1673 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1674 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1675 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1676 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1677
1678 precno = prcx + rlevel->num_precincts_x * prcy;
1679
1680 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1681 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1682 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1683 continue;
1684 }
1685
1686 for (layno = 0; layno < LYEpoc; layno++) {
1687 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1688 precno, layno,
1689 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1690 qntsty->nguardbits)) < 0)
1691 return ret;
1692 }
1693 }
1694 }
1695 }
1696 }
1697 break;
1698
1699 case JPEG2000_PGOD_RPCL:
1700 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1701 ok_reslevel = 1;
1702 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1703 ok_reslevel = 0;
1704 step_x = 30;
1705 step_y = 30;
1706 for (compno = CSpoc; compno < CEpoc; compno++) {
1707 Jpeg2000Component *comp = tile->comp + compno;
1708 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1709
1710 if (reslevelno < codsty->nreslevels) {
1711 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1712 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1713 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1714 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1715 }
1716 }
1717 step_x = 1<<step_x;
1718 step_y = 1<<step_y;
1719
1720 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1721 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1722 for (compno = CSpoc; compno < CEpoc; compno++) {
1723 Jpeg2000Component *comp = tile->comp + compno;
1724 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1725 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1726 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1727 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1728 unsigned prcx, prcy;
1729 int trx0, try0;
1730
1731 if (!s->cdx[compno] || !s->cdy[compno])
1732 return AVERROR_INVALIDDATA;
1733
1734 if (reslevelno >= codsty->nreslevels)
1735 continue;
1736
1737 trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1738 try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1739
1740 if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1741 (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1742 continue;
1743
1744 if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1745 (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1746 continue;
1747
1748 // check if a precinct exists
1749 prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1750 prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1751 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1752 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1753
1754 precno = prcx + rlevel->num_precincts_x * prcy;
1755
1756 ok_reslevel = 1;
1757 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1758 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1759 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1760 continue;
1761 }
1762
1763 for (layno = 0; layno < LYEpoc; layno++) {
1764 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1765 codsty, rlevel,
1766 precno, layno,
1767 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1768 qntsty->nguardbits)) < 0)
1769 return ret;
1770 }
1771 }
1772 }
1773 }
1774 }
1775 break;
1776
1777 case JPEG2000_PGOD_PCRL:
1778 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1779 step_x = 32;
1780 step_y = 32;
1781 for (compno = CSpoc; compno < CEpoc; compno++) {
1782 Jpeg2000Component *comp = tile->comp + compno;
1783 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1784
1785 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1786 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1787 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1788 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1789 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1790 }
1791 }
1792 if (step_x >= 31 || step_y >= 31){
1793 avpriv_request_sample(s->avctx, "PCRL with large step");
1794 return AVERROR_PATCHWELCOME;
1795 }
1796 step_x = 1<<step_x;
1797 step_y = 1<<step_y;
1798
1799 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1800 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1801 for (compno = CSpoc; compno < CEpoc; compno++) {
1802 Jpeg2000Component *comp = tile->comp + compno;
1803 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1804 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1805
1806 if (!s->cdx[compno] || !s->cdy[compno])
1807 return AVERROR_INVALIDDATA;
1808
1809 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1810 unsigned prcx, prcy;
1811 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1812 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1813 int trx0, try0;
1814
1815 trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1816 try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1817
1818 if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1819 (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1820 continue;
1821
1822 if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1823 (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1824 continue;
1825
1826 // check if a precinct exists
1827 prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1828 prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1829 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1830 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1831
1832 precno = prcx + rlevel->num_precincts_x * prcy;
1833
1834 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1835 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1836 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1837 continue;
1838 }
1839
1840 for (layno = 0; layno < LYEpoc; layno++) {
1841 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1842 precno, layno,
1843 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1844 qntsty->nguardbits)) < 0)
1845 return ret;
1846 }
1847 }
1848 }
1849 }
1850 }
1851 break;
1852
1853 default:
1854 break;
1855 }
1856
1857 return ret;
1858}
1859
1861{
1862 int ret = AVERROR_BUG;
1863 int i;
1864 int tp_index = 0;
1865
1866 s->bit_index = 8;
1867 if (tile->poc.nb_poc) {
1868 for (i=0; i<tile->poc.nb_poc; i++) {
1869 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1871 e->RSpoc, e->CSpoc,
1872 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1873 e->REpoc,
1874 FFMIN(e->CEpoc, s->ncomponents),
1875 e->Ppoc, &tp_index
1876 );
1877 if (ret < 0)
1878 return ret;
1879 }
1880 } else {
1882 0, 0,
1883 tile->codsty[0].nlayers,
1884 33,
1885 s->ncomponents,
1886 tile->codsty[0].prog_order,
1887 &tp_index
1888 );
1889 }
1890 /* EOC marker reached */
1891 bytestream2_skip(&s->g, 2);
1892
1893 return ret;
1894}
1895
1896/* TIER-1 routines */
1898 int bpno, int bandno,
1899 int vert_causal_ctx_csty_symbol)
1900{
1901 int mask = (3u << bpno)>>1, y0, x, y;
1902
1903 for (y0 = 0; y0 < height; y0 += 4)
1904 for (x = 0; x < width; x++)
1905 for (y = y0; y < height && y < y0 + 4; y++) {
1906 int flags_mask = -1;
1907 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1909 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1910 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1911 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1912 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1913 if (t1->mqc.raw) {
1914 t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno)) << 31;
1915 t1->data[(y) * t1->stride + x] |= mask;
1916 } else {
1917 t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) << 31;
1918 t1->data[(y) * t1->stride + x] |= mask;
1919 }
1921 t1->data[(y) * t1->stride + x] & INT32_MIN);
1922 }
1923 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1924 }
1925 }
1926}
1927
1929 int bpno, int vert_causal_ctx_csty_symbol)
1930{
1931 int phalf;
1932 int y0, x, y;
1933
1934 phalf = 1 << bpno;
1935
1936 for (y0 = 0; y0 < height; y0 += 4)
1937 for (x = 0; x < width; x++)
1938 for (y = y0; y < height && y < y0 + 4; y++)
1939 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1940 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1942 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1943 t1->data[(y) * t1->stride + x] |= phalf >> 1;
1944 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno))
1945 t1->data[(y) * t1->stride + x] |= phalf;
1946 else {
1947 t1->data[(y) * t1->stride + x] &= ~(phalf);
1948
1949 }
1950 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1951 }
1952}
1953
1955 int width, int height, int bpno, int bandno,
1956 int seg_symbols, int vert_causal_ctx_csty_symbol)
1957{
1958 int mask = (3u << bpno)>>1, y0, x, y, runlen, dec;
1959
1960 for (y0 = 0; y0 < height; y0 += 4) {
1961 for (x = 0; x < width; x++) {
1962 int flags_mask = -1;
1963 if (vert_causal_ctx_csty_symbol)
1965 if (y0 + 3 < height &&
1966 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1967 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1968 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1969 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1970 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1971 continue;
1972 runlen = ff_mqc_decode(&t1->mqc,
1973 t1->mqc.cx_states + MQC_CX_UNI);
1974 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1975 t1->mqc.cx_states +
1976 MQC_CX_UNI);
1977 dec = 1;
1978 } else {
1979 runlen = 0;
1980 dec = 0;
1981 }
1982
1983 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1984 int flags_mask = -1;
1985 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1987 if (!dec) {
1988 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1989 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1990 bandno));
1991 }
1992 }
1993 if (dec) {
1994 int xorbit;
1995 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1996 &xorbit);
1997 t1->data[(y) * t1->stride + x] |= (uint32_t)(ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) << 31;
1998 t1->data[(y) * t1->stride + x] |= mask;
1999 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] & INT32_MIN);
2000 }
2001 dec = 0;
2002 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
2003 }
2004 }
2005 }
2006 if (seg_symbols) {
2007 int val;
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 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
2012 if (val != 0xa)
2013 av_log(s->avctx, AV_LOG_ERROR,
2014 "Segmentation symbol value incorrect\n");
2015 }
2016}
2017
2020 int width, int height, int bandpos, uint8_t roi_shift, const int M_b)
2021{
2022 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + 31 - M_b - 1 - roi_shift;
2023 int pass_cnt = 0;
2024 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
2025 int term_cnt = 0;
2026 int coder_type;
2027
2028 av_assert0(width <= 1024U && height <= 1024U);
2029 av_assert0(width*height <= 4096);
2030
2031 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
2032
2033 /* If code-block contains no compressed data or no coded bit-planes: nothing to do. */
2034 if (!cblk->length || !cblk->nonzerobits)
2035 return 0;
2036
2037 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
2038
2039 cblk->data[cblk->length] = 0xff;
2040 cblk->data[cblk->length+1] = 0xff;
2041 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
2042
2043 while (passno--) {
2044 if (bpno < -1 || bpno > 29) {
2045 av_log(s->avctx, AV_LOG_ERROR, "bpno (%d) became invalid\n", bpno);
2046 return AVERROR_INVALIDDATA;
2047 }
2048 switch(pass_t) {
2049 case 0:
2050 decode_sigpass(t1, width, height, bpno + 1, bandpos,
2051 vert_causal_ctx_csty_symbol);
2052 break;
2053 case 1:
2054 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
2055 break;
2056 case 2:
2057 av_assert2(!t1->mqc.raw);
2058 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
2060 vert_causal_ctx_csty_symbol);
2061 break;
2062 }
2063 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
2065
2066 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
2067 if (term_cnt >= cblk->nb_terminations) {
2068 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
2069 return AVERROR_INVALIDDATA;
2070 }
2071 if (FFABS(cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp) > 0) {
2072 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %td in pass %d of %d\n",
2073 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
2074 pass_cnt, cblk->npasses);
2075 }
2076
2077 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
2078 }
2079
2080 pass_t++;
2081 if (pass_t == 3) {
2082 bpno--;
2083 pass_t = 0;
2084 }
2085 pass_cnt ++;
2086 }
2087
2088 if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
2089 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %td\n",
2090 cblk->data + cblk->length - 2 - t1->mqc.bp);
2091 }
2092
2093 if (cblk->data + cblk->length < t1->mqc.bp) {
2094 av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker Read.\n");
2095 }
2096
2097 /* Reconstruct the sample values */
2098 for (int y = 0; y < height; y++) {
2099 for (int x = 0; x < width; x++) {
2100 int32_t sign, n, val;
2101 const uint32_t mask = (int64_t)UINT32_MAX >> (M_b + 1); // bit mask for ROI detection
2102
2103 n = x + (y * t1->stride);
2104 val = t1->data[n];
2105 sign = val & INT32_MIN;
2106 val &= INT32_MAX;
2107 /* ROI shift, if necessary */
2108 if (roi_shift && (((uint32_t)val & ~mask) == 0))
2109 val = (uint32_t)val << roi_shift;
2110 t1->data[n] = val | sign; /* NOTE: Binary point for reconstruction value is located in 31 - M_b */
2111 }
2112 }
2113 return 1;
2114}
2115
2116/* TODO: Verify dequantization for lossless case
2117 * comp->data can be float or int
2118 * band->stepsize can be float or int
2119 * depending on the type of DWT transformation.
2120 * see ISO/IEC 15444-1:2002 A.6.1 */
2121
2122/* Float dequantization of a codeblock.*/
2123static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
2125 Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2126{
2127 int i, j;
2128 int w = cblk->coord[0][1] - cblk->coord[0][0];
2129 const int downshift = 31 - M_b;
2130 float fscale = band->f_stepsize;
2131 fscale /= (float)(1LL << downshift);
2132 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2133 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2134 int *src = t1->data + j*t1->stride;
2135 for (i = 0; i < w; ++i) {
2136 int val = src[i];
2137 if (val < 0) // Convert sign-magnitude to two's complement
2138 val = -(val & INT32_MAX);
2139 datap[i] = (float)val * fscale;
2140 }
2141 }
2142}
2143
2144/* Integer dequantization of a codeblock.*/
2145static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
2147 Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2148{
2149 int i, j;
2150 const int downshift = FFMIN(31 - M_b, 31);
2151 int w = cblk->coord[0][1] - cblk->coord[0][0];
2152 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2153 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2154 int *src = t1->data + j*t1->stride;
2155 if (band->i_stepsize == 32768) {
2156 for (i = 0; i < w; ++i) {
2157 int val = src[i];
2158 if (val < 0) // Convert sign-magnitude to two's complement
2159 val = -((val & INT32_MAX) >> downshift);
2160 else
2161 val >>= downshift;
2162 datap[i] = val;
2163 }
2164 } else {
2165 // This should be VERY uncommon
2166 for (i = 0; i < w; ++i) {
2167 int val = src[i];
2168 if (val < 0) // Convert sign-magnitude to two's complement
2169 val = -((val & INT32_MAX) >> downshift);
2170 else
2171 val >>= downshift;
2172 datap[i] = (val * (int64_t)band->i_stepsize) / 65536;
2173 }
2174 }
2175 }
2176}
2177
2178static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
2180 Jpeg2000T1Context *t1, Jpeg2000Band *band, const int M_b)
2181{
2182 int i, j;
2183 int w = cblk->coord[0][1] - cblk->coord[0][0];
2184 float fscale = band->f_stepsize;
2185 const int downshift = 31 - M_b;
2186 const int PRESCALE = 6; // At least 6 is required to pass the conformance tests in ISO/IEC 15444-4
2187 int scale;
2188
2189 fscale /= (float)(1LL << downshift);
2190 fscale *= (float)(1 << PRESCALE);
2191 fscale *= (float)(1 << (16 + I_PRESHIFT));
2192 scale = (int)(fscale + 0.5);
2193 band->i_stepsize = scale;
2194 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
2195 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
2196 int *src = t1->data + j*t1->stride;
2197 for (i = 0; i < w; ++i) {
2198 int val = src[i];
2199 if (val < 0) // Convert sign-magnitude to two's complement
2200 val = -(val & INT32_MAX);
2201 // Shifting down to prevent overflow in dequantization
2202 val = (val + (1LL << (PRESCALE - 1))) >> PRESCALE;
2203 datap[i] = RSHIFT(val * (int64_t)band->i_stepsize, 16);
2204 }
2205 }
2206}
2207
2209{
2210 int i, csize = 1;
2211 void *src[3];
2212
2213 for (i = 1; i < 3; i++) {
2214 if (tile->codsty[0].transform != tile->codsty[i].transform) {
2215 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
2216 return;
2217 }
2218 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
2219 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
2220 return;
2221 }
2222 }
2223
2224 for (i = 0; i < 3; i++)
2225 if (tile->codsty[0].transform == FF_DWT97)
2226 src[i] = tile->comp[i].f_data;
2227 else
2228 src[i] = tile->comp[i].i_data;
2229
2230 for (i = 0; i < 2; i++)
2231 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
2232
2233 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
2234}
2235
2236
2238{
2240
2241 int compno, reslevelno, bandno;
2242
2243 /* Loop on tile components */
2244 for (compno = 0; compno < s->ncomponents; compno++) {
2245 Jpeg2000Component *comp = tile->comp + compno;
2246 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
2247 Jpeg2000QuantStyle *quantsty = tile->qntsty + compno;
2248
2249 int coded = 0;
2250 int subbandno = 0;
2251
2252 t1.stride = (1<<codsty->log2_cblk_width) + 2;
2253
2254 /* Loop on resolution levels */
2255 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
2256 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
2257 /* Loop on bands */
2258 for (bandno = 0; bandno < rlevel->nbands; bandno++, subbandno++) {
2259 int nb_precincts, precno;
2260 Jpeg2000Band *band = rlevel->band + bandno;
2261 int cblkno = 0, bandpos;
2262 /* See Rec. ITU-T T.800, Equation E-2 */
2263 int M_b = quantsty->expn[subbandno] + quantsty->nguardbits - 1;
2264
2265 bandpos = bandno + (reslevelno > 0);
2266
2267 if (band->coord[0][0] == band->coord[0][1] ||
2268 band->coord[1][0] == band->coord[1][1])
2269 continue;
2270
2271 if (M_b > 31) {
2272 avpriv_request_sample(s->avctx, "M_b (%d) > 31", M_b);
2273 return AVERROR_PATCHWELCOME;
2274 }
2275
2276 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
2277 /* Loop on precincts */
2278 for (precno = 0; precno < nb_precincts; precno++) {
2279 Jpeg2000Prec *prec = band->prec + precno;
2280
2281 /* Loop on codeblocks */
2282 for (cblkno = 0;
2283 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
2284 cblkno++) {
2285 int x, y, ret;
2286
2287 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
2288
2289 if (cblk->modes & JPEG2000_CTSY_HTJ2K_F)
2290 ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
2291 cblk->coord[0][1] - cblk->coord[0][0],
2292 cblk->coord[1][1] - cblk->coord[1][0],
2293 M_b, comp->roi_shift);
2294 else
2295 ret = decode_cblk(s, codsty, &t1, cblk,
2296 cblk->coord[0][1] - cblk->coord[0][0],
2297 cblk->coord[1][1] - cblk->coord[1][0],
2298 bandpos, comp->roi_shift, M_b);
2299
2300 if (ret)
2301 coded = 1;
2302 else
2303 continue;
2304 x = cblk->coord[0][0] - band->coord[0][0];
2305 y = cblk->coord[1][0] - band->coord[1][0];
2306
2307 if (codsty->transform == FF_DWT97)
2308 dequantization_float(x, y, cblk, comp, &t1, band, M_b);
2309 else if (codsty->transform == FF_DWT97_INT)
2310 dequantization_int_97(x, y, cblk, comp, &t1, band, M_b);
2311 else
2312 dequantization_int(x, y, cblk, comp, &t1, band, M_b);
2313 } /* end cblk */
2314 } /*end prec */
2315 } /* end band */
2316 } /* end reslevel */
2317
2318 /* inverse DWT */
2319 if (coded)
2320 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
2321
2322 } /*end comp */
2323 return 0;
2324}
2325
2326#define WRITE_FRAME(D, PIXEL) \
2327 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
2328 AVFrame * picture, int precision) \
2329 { \
2330 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
2331 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
2332 int pixelsize = planar ? 1 : pixdesc->nb_components; \
2333 \
2334 int compno; \
2335 int x, y; \
2336 \
2337 for (compno = 0; compno < s->ncomponents; compno++) { \
2338 Jpeg2000Component *comp = tile->comp + compno; \
2339 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
2340 PIXEL *line; \
2341 float *datap = comp->f_data; \
2342 int32_t *i_datap = comp->i_data; \
2343 int cbps = s->cbps[compno]; \
2344 int w = tile->comp[compno].coord[0][1] - \
2345 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2346 int h = tile->comp[compno].coord[1][1] - \
2347 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2348 int plane = 0; \
2349 ptrdiff_t dstoffset = 0; \
2350 \
2351 if (planar) \
2352 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
2353 else \
2354 dstoffset = s->cdef[compno] ? s->cdef[compno] - 1 : compno; \
2355 \
2356 y = tile->comp[compno].coord[1][0] - \
2357 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2358 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2359 for (; y < h; y++) { \
2360 PIXEL *dst; \
2361 \
2362 x = tile->comp[compno].coord[0][0] - \
2363 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2364 dst = line + x * pixelsize + dstoffset; \
2365 \
2366 if (codsty->transform == FF_DWT97) { \
2367 for (; x < w; x++) { \
2368 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2369 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2370 val = av_clip(val, 0, (1 << cbps) - 1); \
2371 *dst = val << (precision - cbps); \
2372 datap++; \
2373 dst += pixelsize; \
2374 } \
2375 } else { \
2376 for (; x < w; x++) { \
2377 int val = *i_datap + (1 << (cbps - 1)); \
2378 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2379 val = av_clip(val, 0, (1 << cbps) - 1); \
2380 *dst = val << (precision - cbps); \
2381 i_datap++; \
2382 dst += pixelsize; \
2383 } \
2384 } \
2385 line += picture->linesize[plane] / sizeof(PIXEL); \
2386 } \
2387 } \
2388 \
2389 }
2390
2391WRITE_FRAME(8, uint8_t)
2392WRITE_FRAME(16, uint16_t)
2393
2394#undef WRITE_FRAME
2395
2396static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2397 int jobnr, int threadnr)
2398{
2399 const Jpeg2000DecoderContext *s = avctx->priv_data;
2400 AVFrame *picture = td;
2401 Jpeg2000Tile *tile = s->tile + jobnr;
2402
2403 int ret = tile_codeblocks(s, tile);
2404 if (ret < 0)
2405 return ret;
2406
2407 /* inverse MCT transformation */
2408 if (tile->codsty[0].mct)
2409 mct_decode(s, tile);
2410
2411 if (s->precision <= 8) {
2412 write_frame_8(s, tile, picture, 8);
2413 } else {
2414 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2415 picture->format == AV_PIX_FMT_RGB48 ||
2416 picture->format == AV_PIX_FMT_RGBA64 ||
2417 picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
2418
2419 write_frame_16(s, tile, picture, precision);
2420 }
2421
2422 return 0;
2423}
2424
2426{
2427 int tileno, compno;
2428 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2429 if (s->tile[tileno].comp) {
2430 for (compno = 0; compno < s->ncomponents; compno++) {
2431 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2432 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2433
2434 ff_jpeg2000_cleanup(comp, codsty);
2435 }
2436 av_freep(&s->tile[tileno].comp);
2437 av_freep(&s->tile[tileno].packed_headers);
2438 s->tile[tileno].packed_headers_size = 0;
2439 }
2440 }
2441 av_freep(&s->packed_headers);
2442 s->packed_headers_size = 0;
2443 memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
2444 av_freep(&s->tile);
2445 memset(s->codsty, 0, sizeof(s->codsty));
2446 memset(s->qntsty, 0, sizeof(s->qntsty));
2447 memset(s->properties, 0, sizeof(s->properties));
2448 memset(&s->poc , 0, sizeof(s->poc));
2449 memset(s->roi_shift, 0, sizeof(s->roi_shift));
2450 s->numXtiles = s->numYtiles = 0;
2451 s->ncomponents = 0;
2452 s->has_ppm = 0;
2453 s->isHT = 0;
2454 s->precision = 0;
2455 s->colour_space = 0;
2456 s->pal8 = 0;
2457}
2458
2460{
2461 Jpeg2000CodingStyle *codsty = s->codsty;
2462 Jpeg2000QuantStyle *qntsty = s->qntsty;
2463 Jpeg2000POC *poc = &s->poc;
2464 uint8_t *properties = s->properties;
2465 uint8_t in_tile_headers = 0;
2466
2467 for (;;) {
2468 int len, ret = 0;
2469 uint16_t marker;
2470 int oldpos;
2471
2472 if (bytestream2_get_bytes_left(&s->g) < 2) {
2473 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2474 break;
2475 }
2476
2477 marker = bytestream2_get_be16u(&s->g);
2478 oldpos = bytestream2_tell(&s->g);
2479 if (marker >= 0xFF30 && marker <= 0xFF3F)
2480 continue;
2481 if (marker == JPEG2000_SOD) {
2483 Jpeg2000TilePart *tp;
2484
2485 if (!s->tile) {
2486 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2487 return AVERROR_INVALIDDATA;
2488 }
2489 if (s->curtileno < 0) {
2490 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2491 return AVERROR_INVALIDDATA;
2492 }
2493
2494 tile = s->tile + s->curtileno;
2495 tp = tile->tile_part + tile->tp_idx;
2496 if (tp->tp_end < s->g.buffer) {
2497 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2498 return AVERROR_INVALIDDATA;
2499 }
2500
2501 if (s->has_ppm) {
2502 uint32_t tp_header_size = bytestream2_get_be32(&s->packed_headers_stream);
2503 if (bytestream2_get_bytes_left(&s->packed_headers_stream) < tp_header_size)
2504 return AVERROR_INVALIDDATA;
2505 bytestream2_init(&tp->header_tpg, s->packed_headers_stream.buffer, tp_header_size);
2506 bytestream2_skip(&s->packed_headers_stream, tp_header_size);
2507 }
2508 if (tile->has_ppt && tile->tp_idx == 0) {
2509 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2510 }
2511
2512 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2513 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2514
2515 continue;
2516 }
2517 if (marker == JPEG2000_EOC)
2518 break;
2519
2520 len = bytestream2_get_be16(&s->g);
2521 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2522 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2523 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2524 return AVERROR_INVALIDDATA;
2525 }
2526 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2527 break;
2528 }
2529
2530 switch (marker) {
2531 case JPEG2000_SIZ:
2532 if (s->ncomponents) {
2533 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2534 return AVERROR_INVALIDDATA;
2535 }
2536 ret = get_siz(s);
2537 if (!s->tile)
2538 s->numXtiles = s->numYtiles = 0;
2539 break;
2540 case JPEG2000_CAP:
2541 if (!s->ncomponents) {
2542 av_log(s->avctx, AV_LOG_ERROR, "CAP marker segment shall come after SIZ\n");
2543 return AVERROR_INVALIDDATA;
2544 }
2545 ret = get_cap(s, codsty);
2546 break;
2547 case JPEG2000_COC:
2548 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2549 av_log(s->avctx, AV_LOG_ERROR,
2550 "COC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2551 return AVERROR_INVALIDDATA;
2552 }
2553 ret = get_coc(s, codsty, properties);
2554 break;
2555 case JPEG2000_COD:
2556 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2557 av_log(s->avctx, AV_LOG_ERROR,
2558 "COD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2559 return AVERROR_INVALIDDATA;
2560 }
2561 ret = get_cod(s, codsty, properties);
2562 break;
2563 case JPEG2000_RGN:
2564 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2565 av_log(s->avctx, AV_LOG_ERROR,
2566 "RGN marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2567 return AVERROR_INVALIDDATA;
2568 }
2569 ret = get_rgn(s, len);
2570 if ((!s->Ccap15_b12) && s->isHT) {
2571 av_log(s->avctx, AV_LOG_ERROR, "RGN marker found but the codestream belongs to the RGNFREE set\n");
2572 return AVERROR_INVALIDDATA;
2573 }
2574 break;
2575 case JPEG2000_QCC:
2576 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2577 av_log(s->avctx, AV_LOG_ERROR,
2578 "QCC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2579 return AVERROR_INVALIDDATA;
2580 }
2581 ret = get_qcc(s, len, qntsty, properties);
2582 break;
2583 case JPEG2000_QCD:
2584 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2585 av_log(s->avctx, AV_LOG_ERROR,
2586 "QCD marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2587 return AVERROR_INVALIDDATA;
2588 }
2589 ret = get_qcd(s, len, qntsty, properties);
2590 break;
2591 case JPEG2000_POC:
2592 if (in_tile_headers == 1 && s->isHT && (!s->Ccap15_b11)) {
2593 av_log(s->avctx, AV_LOG_ERROR,
2594 "POC marker found in a tile header but the codestream belongs to the HOMOGENEOUS set\n");
2595 return AVERROR_INVALIDDATA;
2596 }
2597 ret = get_poc(s, len, poc);
2598 break;
2599 case JPEG2000_SOT:
2600 if (!in_tile_headers) {
2601 in_tile_headers = 1;
2602 if (s->has_ppm) {
2603 bytestream2_init(&s->packed_headers_stream, s->packed_headers, s->packed_headers_size);
2604 }
2605 }
2606 if (!(ret = get_sot(s, len))) {
2607 av_assert1(s->curtileno >= 0);
2608 codsty = s->tile[s->curtileno].codsty;
2609 qntsty = s->tile[s->curtileno].qntsty;
2610 poc = &s->tile[s->curtileno].poc;
2611 properties = s->tile[s->curtileno].properties;
2612 }
2613 break;
2614 case JPEG2000_PLM:
2615 // the PLM marker is ignored
2616 case JPEG2000_COM:
2617 // the comment is ignored
2618 bytestream2_skip(&s->g, len - 2);
2619 break;
2620 case JPEG2000_CRG:
2621 ret = read_crg(s, len);
2622 break;
2623 case JPEG2000_TLM:
2624 // Tile-part lengths
2625 ret = get_tlm(s, len);
2626 break;
2627 case JPEG2000_PLT:
2628 // Packet length, tile-part header
2629 ret = get_plt(s, len);
2630 break;
2631 case JPEG2000_PPM:
2632 // Packed headers, main header
2633 if (in_tile_headers) {
2634 av_log(s->avctx, AV_LOG_ERROR, "PPM Marker can only be in Main header\n");
2635 return AVERROR_INVALIDDATA;
2636 }
2637 ret = get_ppm(s, len);
2638 break;
2639 case JPEG2000_PPT:
2640 // Packed headers, tile-part header
2641 if (s->has_ppm) {
2642 av_log(s->avctx, AV_LOG_ERROR,
2643 "Cannot have both PPT and PPM marker.\n");
2644 return AVERROR_INVALIDDATA;
2645 }
2646 if ((!s->Ccap15_b11) && s->isHT) {
2647 av_log(s->avctx, AV_LOG_ERROR, "PPT marker found but the codestream belongs to the HOMOGENEOUS set\n");
2648 return AVERROR_INVALIDDATA;
2649 }
2650 ret = get_ppt(s, len);
2651 break;
2652 case JPEG2000_CPF:
2653 // Corresponding profile marker
2654 ret = read_cpf(s, len);
2655 break;
2656 default:
2657 av_log(s->avctx, AV_LOG_ERROR,
2658 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2659 marker, bytestream2_tell(&s->g) - 4);
2660 bytestream2_skip(&s->g, len - 2);
2661 break;
2662 }
2663 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2664 av_log(s->avctx, AV_LOG_ERROR,
2665 "error during processing marker segment %.4"PRIx16"\n",
2666 marker);
2667 return ret ? ret : -1;
2668 }
2669 }
2670 return 0;
2671}
2672
2673/* Read bit stream packets --> T2 operation. */
2675{
2676 int ret = 0;
2677 int tileno;
2678
2679 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2680 Jpeg2000Tile *tile = s->tile + tileno;
2681
2682 if ((ret = init_tile(s, tileno)) < 0)
2683 return ret;
2684
2685 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2686 return ret;
2687 }
2688
2689 return 0;
2690}
2691
2693{
2694 uint32_t atom_size, atom, atom_end;
2695 int search_range = 10;
2696
2697 while (search_range
2698 &&
2699 bytestream2_get_bytes_left(&s->g) >= 8) {
2700 atom_size = bytestream2_get_be32u(&s->g);
2701 atom = bytestream2_get_be32u(&s->g);
2702 if (atom_size == 1) {
2703 if (bytestream2_get_be32u(&s->g)) {
2704 avpriv_request_sample(s->avctx, "Huge atom");
2705 return 0;
2706 }
2707 atom_size = bytestream2_get_be32u(&s->g);
2708 if (atom_size < 16 || (int64_t)bytestream2_tell(&s->g) + atom_size - 16 > INT_MAX)
2709 return AVERROR_INVALIDDATA;
2710 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2711 } else {
2712 if (atom_size < 8 || (int64_t)bytestream2_tell(&s->g) + atom_size - 8 > INT_MAX)
2713 return AVERROR_INVALIDDATA;
2714 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2715 }
2716
2717 if (atom == JP2_CODESTREAM)
2718 return 1;
2719
2720 if (bytestream2_get_bytes_left(&s->g) < atom_size || atom_end < atom_size)
2721 return 0;
2722
2723 if (atom == JP2_HEADER &&
2724 atom_size >= 16) {
2725 uint32_t atom2_size, atom2, atom2_end;
2726 do {
2727 if (bytestream2_get_bytes_left(&s->g) < 8)
2728 break;
2729 atom2_size = bytestream2_get_be32u(&s->g);
2730 atom2 = bytestream2_get_be32u(&s->g);
2731 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2732 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
2733 break;
2734 atom2_size -= 8;
2735 if (atom2 == JP2_CODESTREAM) {
2736 return 1;
2737 } else if (atom2 == MKBETAG('c','o','l','r') && atom2_size >= 7) {
2738 int method = bytestream2_get_byteu(&s->g);
2739 bytestream2_skipu(&s->g, 2);
2740 if (method == 1) {
2741 s->colour_space = bytestream2_get_be32u(&s->g);
2742 }
2743 } else if (atom2 == MKBETAG('p','c','l','r') && atom2_size >= 6) {
2744 int i, size, colour_count, colour_channels, colour_depth[3];
2745 colour_count = bytestream2_get_be16u(&s->g);
2746 colour_channels = bytestream2_get_byteu(&s->g);
2747 // FIXME: Do not ignore channel_sign
2748 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2749 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2750 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2751 size = (colour_depth[0] + 7 >> 3) * colour_count +
2752 (colour_depth[1] + 7 >> 3) * colour_count +
2753 (colour_depth[2] + 7 >> 3) * colour_count;
2754 if (colour_count > AVPALETTE_COUNT ||
2755 colour_channels != 3 ||
2756 colour_depth[0] > 16 ||
2757 colour_depth[1] > 16 ||
2758 colour_depth[2] > 16 ||
2759 atom2_size < size) {
2760 avpriv_request_sample(s->avctx, "Unknown palette");
2761 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2762 continue;
2763 }
2764 s->pal8 = 1;
2765 for (i = 0; i < colour_count; i++) {
2766 uint32_t r, g, b;
2767 if (colour_depth[0] <= 8) {
2768 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2769 r |= r >> colour_depth[0];
2770 } else {
2771 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2772 }
2773 if (colour_depth[1] <= 8) {
2774 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2775 g |= g >> colour_depth[1];
2776 } else {
2777 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2778 }
2779 if (colour_depth[2] <= 8) {
2780 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2781 b |= b >> colour_depth[2];
2782 } else {
2783 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2784 }
2785 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2786 }
2787 } else if (atom2 == MKBETAG('c','d','e','f') && atom2_size >= 2) {
2788 int n = bytestream2_get_be16u(&s->g);
2789 for (; n>0; n--) {
2790 int cn = bytestream2_get_be16(&s->g);
2791 av_unused int typ = bytestream2_get_be16(&s->g);
2792 int asoc = bytestream2_get_be16(&s->g);
2793 if (cn < 4 && asoc < 4)
2794 s->cdef[cn] = asoc;
2795 }
2796 } else if (atom2 == MKBETAG('r','e','s',' ') && atom2_size >= 18) {
2797 int64_t vnum, vden, hnum, hden, vexp, hexp;
2798 uint32_t resx;
2799 bytestream2_skip(&s->g, 4);
2800 resx = bytestream2_get_be32u(&s->g);
2801 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2802 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2803 continue;
2804 }
2805 vnum = bytestream2_get_be16u(&s->g);
2806 vden = bytestream2_get_be16u(&s->g);
2807 hnum = bytestream2_get_be16u(&s->g);
2808 hden = bytestream2_get_be16u(&s->g);
2809 vexp = bytestream2_get_byteu(&s->g);
2810 hexp = bytestream2_get_byteu(&s->g);
2811 if (!vnum || !vden || !hnum || !hden) {
2812 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2813 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2814 continue;
2815 }
2816 if (vexp > hexp) {
2817 vexp -= hexp;
2818 hexp = 0;
2819 } else {
2820 hexp -= vexp;
2821 vexp = 0;
2822 }
2823 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2824 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2825 av_reduce(&s->sar.den, &s->sar.num,
2826 hnum * vden * pow(10, hexp),
2827 vnum * hden * pow(10, vexp),
2828 INT32_MAX);
2829 }
2830 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2831 } while (atom_end - atom2_end >= 8);
2832 } else {
2833 search_range--;
2834 }
2835 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2836 }
2837
2838 return 0;
2839}
2840
2842{
2844
2845 if (avctx->lowres)
2846 av_log(avctx, AV_LOG_WARNING, "lowres is overridden by reduction_factor but set anyway\n");
2847 if (!s->reduction_factor && avctx->lowres < JPEG2000_MAX_RESLEVELS) {
2848 s->reduction_factor = avctx->lowres;
2849 }
2850 if (avctx->lowres != s->reduction_factor && avctx->lowres)
2851 return AVERROR(EINVAL);
2852
2853 ff_jpeg2000dsp_init(&s->dsp);
2855
2856 return 0;
2857}
2858
2860 const AVPacket *avpkt)
2861{
2863 int ret;
2864
2865 s->avctx = avctx;
2866 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2867 s->curtileno = -1;
2868 memset(s->cdef, -1, sizeof(s->cdef));
2869
2870 if (bytestream2_get_bytes_left(&s->g) < 2) {
2872 goto end;
2873 }
2874
2875 // check if the image is in jp2 format
2876 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2877 (bytestream2_get_be32u(&s->g) == 12) &&
2878 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2879 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2880 if (!jp2_find_codestream(s)) {
2881 av_log(avctx, AV_LOG_ERROR,
2882 "Could not find Jpeg2000 codestream atom.\n");
2884 goto end;
2885 }
2886 } else {
2887 bytestream2_seek(&s->g, 0, SEEK_SET);
2888 }
2889
2890 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2891 bytestream2_skip(&s->g, 1);
2892
2893 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2894 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2896 goto end;
2897 }
2899 goto end;
2900
2901 if (s->sar.num && s->sar.den)
2902 avctx->sample_aspect_ratio = s->sar;
2903 s->sar.num = s->sar.den = 0;
2904
2905 if (avctx->skip_frame >= AVDISCARD_ALL) {
2907 return 0;
2908 }
2909
2910 /* get picture buffer */
2911 if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
2912 goto end;
2913
2915 goto end;
2916
2917 for (int x = 0; x < s->ncomponents && s->codsty[x].transform == FF_DWT53;)
2918 if (++x == s->ncomponents)
2919 picture->flags |= AV_FRAME_FLAG_LOSSLESS;
2920
2921 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL,
2922 s->numXtiles * s->numYtiles);
2923
2925
2926 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2927 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2928
2929 return 0;
2930
2931end:
2932 /* Do not leave a partially initialized frame on the output. */
2933 av_frame_unref(picture);
2935 return ret;
2936}
2937
2939{
2940 AVPacket *const avpkt = avctx->internal->in_pkt;
2941 int ret;
2942
2943 /* One codestream per packet. Loop so that skipped frames (produce no
2944 * output) transparently pull the next packet instead of returning to the
2945 * caller empty-handed. */
2946 do {
2947 ret = ff_decode_get_packet(avctx, avpkt);
2948 if (ret < 0)
2949 return ret;
2950
2951 ret = jpeg2000_decode_frame(avctx, frame, avpkt);
2952 frame->pkt_dts = avpkt->dts;
2953 av_packet_unref(avpkt);
2954 if (ret < 0)
2955 return ret;
2956 } while (!frame->buf[0]);
2957
2958 return 0;
2959}
2960
2961#define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2962#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2963
2964static const AVOption options[] = {
2965 { "lowres", "Lower the decoding resolution by a power of two",
2966 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2967 { NULL },
2968};
2969
2970static const AVClass jpeg2000_class = {
2971 .class_name = "jpeg2000",
2972 .item_name = av_default_item_name,
2973 .option = options,
2974 .version = LIBAVUTIL_VERSION_INT,
2975};
2976
2978 .p.name = "jpeg2000",
2979 CODEC_LONG_NAME("JPEG 2000"),
2980 .p.type = AVMEDIA_TYPE_VIDEO,
2981 .p.id = AV_CODEC_ID_JPEG2000,
2983 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2986 .p.priv_class = &jpeg2000_class,
2987 .p.max_lowres = 5,
2989 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2990};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_jpeg2000_decoder
#define VD
Definition amfdec.c:807
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:255
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:613
int ret
Definition ffplay.c:391
@ 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:248
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:499
#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:247
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
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
misc image utilities
#define r
Definition input.c:42
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
#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:110
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:121
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:629
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:1638
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:1712
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1677
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
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:609
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
Definition format.c:918
#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]