FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000dec.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 1210 1778 68.1%
Functions: 45 48 93.8%
Branches: 801 1356 59.1%

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