FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000dec.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 1174 1754 66.9%
Functions: 43 47 91.5%
Branches: 779 1336 58.3%

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