FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000dec.c
Date: 2026-05-02 21:46:34
Exec Total Coverage
Lines: 1179 1760 67.0%
Functions: 43 47 91.5%
Branches: 784 1346 58.2%

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