FFmpeg coverage


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