FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000dec.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 1179 1760 67.0%
Functions: 43 47 91.5%
Branches: 783 1342 58.3%

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