FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000dec.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 1060 1509 70.2%
Functions: 43 47 91.5%
Branches: 704 1159 60.7%

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/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "codec_internal.h"
40 #include "decode.h"
41 #include "thread.h"
42 #include "jpeg2000.h"
43 #include "jpeg2000dsp.h"
44 #include "profiles.h"
45 #include "jpeg2000dec.h"
46 #include "jpeg2000htdec.h"
47
48 #define JP2_SIG_TYPE 0x6A502020
49 #define JP2_SIG_VALUE 0x0D0A870A
50 #define JP2_CODESTREAM 0x6A703263
51 #define JP2_HEADER 0x6A703268
52
53 #define HAD_COC 0x01
54 #define HAD_QCC 0x02
55
56 /* get_bits functions for JPEG2000 packet bitstream
57 * It is a get_bit function with a bit-stuffing routine. If the value of the
58 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
59 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
60 8802537 static int get_bits(Jpeg2000DecoderContext *s, int n)
61 {
62 8802537 int res = 0;
63
64
2/2
✓ Branch 0 taken 17397477 times.
✓ Branch 1 taken 8802537 times.
26200014 while (--n >= 0) {
65 17397477 res <<= 1;
66
2/2
✓ Branch 0 taken 2134532 times.
✓ Branch 1 taken 15262945 times.
17397477 if (s->bit_index == 0) {
67
2/2
✓ Branch 1 taken 2074683 times.
✓ Branch 2 taken 59849 times.
2134532 s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
68 }
69 17397477 s->bit_index--;
70 17397477 res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
71 }
72 8802537 return res;
73 }
74
75 87096 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
76 {
77
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 87094 times.
87096 if (bytestream2_get_byte(&s->g) == 0xff)
78 2 bytestream2_skip(&s->g, 1);
79 87096 s->bit_index = 8;
80 87096 }
81
82 /* decode the value stored in node */
83 1726930 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
84 int threshold)
85 {
86 Jpeg2000TgtNode *stack[30];
87 1726930 int sp = -1, curval = 0;
88
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1726930 times.
1726930 if (!node) {
90 av_log(s->avctx, AV_LOG_ERROR, "missing node\n");
91 return AVERROR_INVALIDDATA;
92 }
93
94
4/4
✓ Branch 0 taken 3964053 times.
✓ Branch 1 taken 347175 times.
✓ Branch 2 taken 2584298 times.
✓ Branch 3 taken 1379755 times.
4311228 while (node && !node->vis) {
95 2584298 stack[++sp] = node;
96 2584298 node = node->parent;
97 }
98
99
2/2
✓ Branch 0 taken 1379755 times.
✓ Branch 1 taken 347175 times.
1726930 if (node)
100 1379755 curval = node->val;
101 else
102 347175 curval = stack[sp]->val;
103
104
4/4
✓ Branch 0 taken 3934844 times.
✓ Branch 1 taken 100742 times.
✓ Branch 2 taken 2308656 times.
✓ Branch 3 taken 1626188 times.
4035586 while (curval < threshold && sp >= 0) {
105
2/2
✓ Branch 0 taken 23655 times.
✓ Branch 1 taken 2285001 times.
2308656 if (curval < stack[sp]->val)
106 23655 curval = stack[sp]->val;
107
2/2
✓ Branch 0 taken 3145186 times.
✓ Branch 1 taken 59246 times.
3204432 while (curval < threshold) {
108 int ret;
109
2/2
✓ Branch 1 taken 2249410 times.
✓ Branch 2 taken 895776 times.
3145186 if ((ret = get_bits(s, 1)) > 0) {
110 2249410 stack[sp]->vis++;
111 2249410 break;
112
1/2
✓ Branch 0 taken 895776 times.
✗ Branch 1 not taken.
895776 } else if (!ret)
113 895776 curval++;
114 else
115 return ret;
116 }
117 2308656 stack[sp]->val = curval;
118 2308656 sp--;
119 }
120 1726930 return curval;
121 }
122
123 1222 static int pix_fmt_match(enum AVPixelFormat pix_fmt, int components,
124 int bpc, uint32_t log2_chroma_wh, int pal8)
125 {
126 1222 int match = 1;
127 1222 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
128
129 av_assert2(desc);
130
131
2/2
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 941 times.
1222 if (desc->nb_components != components) {
132 281 return 0;
133 }
134
135
3/5
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 631 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
✗ Branch 4 not taken.
941 switch (components) {
136 279 case 4:
137
2/2
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 37 times.
558 match = match && desc->comp[3].depth >= bpc &&
138
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 &&
139
1/2
✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
213 (log2_chroma_wh >> 12 & 3) == 0;
140 910 case 3:
141
2/2
✓ Branch 0 taken 837 times.
✓ Branch 1 taken 7 times.
1754 match = match && desc->comp[2].depth >= bpc &&
142
4/4
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 829 times.
✓ Branch 3 taken 8 times.
2583 (log2_chroma_wh >> 10 & 3) == desc->log2_chroma_w &&
143
1/2
✓ Branch 0 taken 829 times.
✗ Branch 1 not taken.
829 (log2_chroma_wh >> 8 & 3) == desc->log2_chroma_h;
144 910 case 2:
145
1/2
✓ Branch 0 taken 829 times.
✗ Branch 1 not taken.
1739 match = match && desc->comp[1].depth >= bpc &&
146
3/4
✓ Branch 0 taken 829 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 829 times.
✗ Branch 3 not taken.
2568 (log2_chroma_wh >> 6 & 3) == desc->log2_chroma_w &&
147
1/2
✓ Branch 0 taken 829 times.
✗ Branch 1 not taken.
829 (log2_chroma_wh >> 4 & 3) == desc->log2_chroma_h;
148
149 941 case 1:
150
1/2
✓ Branch 0 taken 860 times.
✗ Branch 1 not taken.
1801 match = match && desc->comp[0].depth >= bpc &&
151
2/2
✓ Branch 0 taken 853 times.
✓ Branch 1 taken 7 times.
860 (log2_chroma_wh >> 2 & 3) == 0 &&
152
3/4
✓ Branch 0 taken 860 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 853 times.
✗ Branch 3 not taken.
2654 (log2_chroma_wh & 3) == 0 &&
153
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;
154 }
155 941 return match;
156 }
157
158 // pix_fmts with lower bpp have to be listed before
159 // similar pix_fmts with higher bpp.
160 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
161 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
162 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
163 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
164 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
165 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
166 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
167 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
168 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
169 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
170 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
171 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
172 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
173 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
174
175 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
176 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
177 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
178 static const enum AVPixelFormat xyz_pix_fmts[] = {XYZ_PIXEL_FORMATS,
179 YUV_PIXEL_FORMATS};
180 static const enum AVPixelFormat all_pix_fmts[] = {RGB_PIXEL_FORMATS,
181 GRAY_PIXEL_FORMATS,
182 YUV_PIXEL_FORMATS,
183 XYZ_PIXEL_FORMATS};
184
185 /* marker segments */
186 /* get sizes and offsets of image, tiles; number of components */
187 849 static int get_siz(Jpeg2000DecoderContext *s)
188 {
189 int i;
190 int ncomponents;
191 849 uint32_t log2_chroma_wh = 0;
192 849 const enum AVPixelFormat *possible_fmts = NULL;
193 849 int possible_fmts_nb = 0;
194 int ret;
195 int o_dimx, o_dimy; //original image dimensions.
196 int dimx, dimy;
197
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if (bytestream2_get_bytes_left(&s->g) < 36) {
199 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
200 return AVERROR_INVALIDDATA;
201 }
202
203 849 s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
204 849 s->width = bytestream2_get_be32u(&s->g); // Width
205 849 s->height = bytestream2_get_be32u(&s->g); // Height
206 849 s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
207 849 s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
208 849 s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
209 849 s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
210 849 s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
211 849 s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
212 849 ncomponents = bytestream2_get_be16u(&s->g); // CSiz
213
214
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx)) {
215 avpriv_request_sample(s->avctx, "Large Dimensions");
216 return AVERROR_PATCHWELCOME;
217 }
218
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 if (ncomponents <= 0) {
220 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
221 s->ncomponents);
222 return AVERROR_INVALIDDATA;
223 }
224
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 if (ncomponents > 4) {
226 avpriv_request_sample(s->avctx, "Support for %d components",
227 ncomponents);
228 return AVERROR_PATCHWELCOME;
229 }
230
231
2/4
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
✗ Branch 3 not taken.
849 if (s->tile_offset_x < 0 || s->tile_offset_y < 0 ||
232
1/2
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
849 s->image_offset_x < s->tile_offset_x ||
233
1/2
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
849 s->image_offset_y < s->tile_offset_y ||
234
1/2
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
849 s->tile_width + (int64_t)s->tile_offset_x <= s->image_offset_x ||
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 s->tile_height + (int64_t)s->tile_offset_y <= s->image_offset_y
236 ) {
237 av_log(s->avctx, AV_LOG_ERROR, "Tile offsets are invalid\n");
238 return AVERROR_INVALIDDATA;
239 }
240
241
2/4
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 849 times.
849 if (s->image_offset_x >= s->width || s->image_offset_y >= s->height) {
242 av_log(s->avctx, AV_LOG_ERROR, "image offsets outside image");
243 return AVERROR_INVALIDDATA;
244 }
245
246
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 848 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
849 if (s->reduction_factor && (s->image_offset_x || s->image_offset_y) ){
247 av_log(s->avctx, AV_LOG_ERROR, "reduction factor with image offsets is not fully implemented");
248 return AVERROR_PATCHWELCOME;
249 }
250
251 849 s->ncomponents = ncomponents;
252
253
2/4
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 849 times.
849 if (s->tile_width <= 0 || s->tile_height <= 0) {
254 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
255 s->tile_width, s->tile_height);
256 return AVERROR_INVALIDDATA;
257 }
258
259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents) {
260 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for %d components in SIZ\n", s->ncomponents);
261 return AVERROR_INVALIDDATA;
262 }
263
264
2/2
✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 849 times.
3567 for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
265 2718 uint8_t x = bytestream2_get_byteu(&s->g);
266 2718 s->cbps[i] = (x & 0x7f) + 1;
267 2718 s->precision = FFMAX(s->cbps[i], s->precision);
268 2718 s->sgnd[i] = !!(x & 0x80);
269 2718 s->cdx[i] = bytestream2_get_byteu(&s->g);
270 2718 s->cdy[i] = bytestream2_get_byteu(&s->g);
271
3/6
✓ Branch 0 taken 2718 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2718 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2718 times.
✗ Branch 5 not taken.
2718 if ( !s->cdx[i] || s->cdx[i] == 3 || s->cdx[i] > 4
272
3/6
✓ Branch 0 taken 2718 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2718 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2718 times.
2718 || !s->cdy[i] || s->cdy[i] == 3 || s->cdy[i] > 4) {
273 av_log(s->avctx, AV_LOG_ERROR, "Invalid sample separation %d/%d\n", s->cdx[i], s->cdy[i]);
274 return AVERROR_INVALIDDATA;
275 }
276 2718 log2_chroma_wh |= s->cdy[i] >> 1 << i * 4 | s->cdx[i] >> 1 << i * 4 + 2;
277 }
278
279 849 s->numXtiles = ff_jpeg2000_ceildiv(s->width - s->tile_offset_x, s->tile_width);
280 849 s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
281
282 // There must be at least a SOT and SOD per tile, their minimum size is 14
283
1/2
✓ Branch 0 taken 849 times.
✗ Branch 1 not taken.
849 if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(*s->tile) ||
284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 s->numXtiles * s->numYtiles * 14LL > bytestream2_size(&s->g)
285 ) {
286 s->numXtiles = s->numYtiles = 0;
287 return AVERROR(EINVAL);
288 }
289
290 849 s->tile = av_calloc(s->numXtiles * s->numYtiles, sizeof(*s->tile));
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 if (!s->tile) {
292 s->numXtiles = s->numYtiles = 0;
293 return AVERROR(ENOMEM);
294 }
295
296
2/2
✓ Branch 0 taken 3208 times.
✓ Branch 1 taken 849 times.
4057 for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
297 3208 Jpeg2000Tile *tile = s->tile + i;
298
299 3208 tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3208 times.
3208 if (!tile->comp)
301 return AVERROR(ENOMEM);
302 }
303
304 /* compute image size with reduction factor */
305 849 o_dimx = ff_jpeg2000_ceildivpow2(s->width - s->image_offset_x,
306 s->reduction_factor);
307 849 o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
308 s->reduction_factor);
309 849 dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
310 849 dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
311
2/2
✓ Branch 0 taken 1869 times.
✓ Branch 1 taken 849 times.
2718 for (i = 1; i < s->ncomponents; i++) {
312
2/2
✓ Branch 1 taken 1865 times.
✓ Branch 2 taken 4 times.
1869 dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
313
2/2
✓ Branch 1 taken 1865 times.
✓ Branch 2 taken 4 times.
1869 dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
314 }
315
316 849 ret = ff_set_dimensions(s->avctx, dimx << s->avctx->lowres, dimy << s->avctx->lowres);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 if (ret < 0)
318 return ret;
319
320
2/2
✓ Branch 0 taken 846 times.
✓ Branch 1 taken 3 times.
849 if (s->avctx->profile == AV_PROFILE_JPEG2000_DCINEMA_2K ||
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 846 times.
846 s->avctx->profile == AV_PROFILE_JPEG2000_DCINEMA_4K) {
322 3 possible_fmts = xyz_pix_fmts;
323 3 possible_fmts_nb = FF_ARRAY_ELEMS(xyz_pix_fmts);
324 } else {
325
3/4
✓ Branch 0 taken 613 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 29 times.
846 switch (s->colour_space) {
326 613 case 16:
327 613 possible_fmts = rgb_pix_fmts;
328 613 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
329 613 break;
330 case 17:
331 possible_fmts = gray_pix_fmts;
332 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
333 break;
334 204 case 18:
335 204 possible_fmts = yuv_pix_fmts;
336 204 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
337 204 break;
338 29 default:
339 29 possible_fmts = all_pix_fmts;
340 29 possible_fmts_nb = FF_ARRAY_ELEMS(all_pix_fmts);
341 29 break;
342 }
343 }
344
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 33 times.
849 if ( s->avctx->pix_fmt != AV_PIX_FMT_NONE
345
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 814 times.
816 && !pix_fmt_match(s->avctx->pix_fmt, ncomponents, s->precision, log2_chroma_wh, s->pal8))
346 2 s->avctx->pix_fmt = AV_PIX_FMT_NONE;
347
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 814 times.
849 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE)
348
2/2
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 4 times.
410 for (i = 0; i < possible_fmts_nb; ++i) {
349
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 375 times.
406 if (pix_fmt_match(possible_fmts[i], ncomponents, s->precision, log2_chroma_wh, s->pal8)) {
350 31 s->avctx->pix_fmt = possible_fmts[i];
351 31 break;
352 }
353 }
354
355
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 845 times.
849 if (i == possible_fmts_nb) {
356
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (ncomponents == 4 &&
357
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 &&
358
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 &&
359
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]) {
360
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) {
361 2 s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
362 2 s->cdef[0] = 0;
363 2 s->cdef[1] = 1;
364 2 s->cdef[2] = 2;
365 2 s->cdef[3] = 3;
366 2 i = 0;
367 }
368
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 } else if (ncomponents == 3 && s->precision == 8 &&
369 s->cdx[0] == s->cdx[1] && s->cdx[0] == s->cdx[2] &&
370 s->cdy[0] == s->cdy[1] && s->cdy[0] == s->cdy[2]) {
371 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
372 i = 0;
373
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 &&
374 s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
375 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
376 i = 0;
377
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 &&
378 s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
379 s->avctx->pix_fmt = AV_PIX_FMT_YA16;
380 i = 0;
381
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) {
382 2 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
383 2 i = 0;
384 }
385 }
386
387
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 if (i == possible_fmts_nb) {
389 av_log(s->avctx, AV_LOG_ERROR,
390 "Unknown pix_fmt, profile: %d, colour_space: %d, "
391 "components: %d, precision: %d\n"
392 "cdx[0]: %d, cdy[0]: %d\n"
393 "cdx[1]: %d, cdy[1]: %d\n"
394 "cdx[2]: %d, cdy[2]: %d\n"
395 "cdx[3]: %d, cdy[3]: %d\n",
396 s->avctx->profile, s->colour_space, ncomponents, s->precision,
397 s->cdx[0],
398 s->cdy[0],
399 ncomponents > 1 ? s->cdx[1] : 0,
400 ncomponents > 1 ? s->cdy[1] : 0,
401 ncomponents > 2 ? s->cdx[2] : 0,
402 ncomponents > 2 ? s->cdy[2] : 0,
403 ncomponents > 3 ? s->cdx[3] : 0,
404 ncomponents > 3 ? s->cdy[3] : 0);
405 return AVERROR_PATCHWELCOME;
406 }
407 849 s->avctx->bits_per_raw_sample = s->precision;
408 849 return 0;
409 }
410
411 /* get common part for COD and COC segments */
412 867 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
413 {
414 uint8_t byte;
415
416
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 867 times.
867 if (bytestream2_get_bytes_left(&s->g) < 5) {
417 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COX\n");
418 return AVERROR_INVALIDDATA;
419 }
420
421 /* nreslevels = number of resolution levels
422 = number of decomposition level +1 */
423 867 c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 867 times.
867 if (c->nreslevels >= JPEG2000_MAX_RESLEVELS) {
425 av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
426 return AVERROR_INVALIDDATA;
427 }
428
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 867 times.
867 if (c->nreslevels <= s->reduction_factor) {
430 /* we are forced to update reduction_factor as its requested value is
431 not compatible with this bitstream, and as we might have used it
432 already in setup earlier we have to fail this frame until
433 reinitialization is implemented */
434 av_log(s->avctx, AV_LOG_ERROR, "reduction_factor too large for this bitstream, max is %d\n", c->nreslevels - 1);
435 s->reduction_factor = c->nreslevels - 1;
436 return AVERROR(EINVAL);
437 }
438
439 /* compute number of resolution levels to decode */
440 867 c->nreslevels2decode = c->nreslevels - s->reduction_factor;
441
442 867 c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
443 867 c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
444
445
2/4
✓ Branch 0 taken 867 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 867 times.
✗ Branch 3 not taken.
867 if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 867 times.
867 c->log2_cblk_width + c->log2_cblk_height > 12) {
447 av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
448 return AVERROR_INVALIDDATA;
449 }
450
451 867 c->cblk_style = bytestream2_get_byteu(&s->g);
452
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 855 times.
867 if (c->cblk_style != 0) { // cblk style
453
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) {
454 2 av_log(s->avctx,AV_LOG_TRACE,"High Throughput jpeg 2000 codestream.\n");
455 } else {
456 10 av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (c->cblk_style & JPEG2000_CBLK_BYPASS)
458 av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
459 }
460 }
461 867 c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
462 /* set integer 9/7 DWT in case of BITEXACT flag */
463
4/4
✓ Branch 0 taken 865 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 642 times.
867 if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
464 223 c->transform = FF_DWT97_INT;
465
2/2
✓ Branch 0 taken 643 times.
✓ Branch 1 taken 1 times.
644 else if (c->transform == FF_DWT53) {
466 643 s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
467 }
468
469
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 851 times.
867 if (c->csty & JPEG2000_CSTY_PREC) {
470 int i;
471
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 16 times.
100 for (i = 0; i < c->nreslevels; i++) {
472 84 byte = bytestream2_get_byte(&s->g);
473 84 c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
474 84 c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
475
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 16 times.
84 if (i)
476
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) {
477 av_log(s->avctx, AV_LOG_ERROR, "PPx %d PPy %d invalid\n",
478 c->log2_prec_widths[i], c->log2_prec_heights[i]);
479 c->log2_prec_widths[i] = c->log2_prec_heights[i] = 1;
480 return AVERROR_INVALIDDATA;
481 }
482 }
483 } else {
484 851 memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
485 851 memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
486 }
487 867 return 0;
488 }
489
490 /* get coding parameters for a particular tile or whole image*/
491 849 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
492 const uint8_t *properties)
493 {
494 Jpeg2000CodingStyle tmp;
495 int compno, ret;
496
497
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if (bytestream2_get_bytes_left(&s->g) < 5) {
498 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COD\n");
499 return AVERROR_INVALIDDATA;
500 }
501
502 849 tmp.csty = bytestream2_get_byteu(&s->g);
503
504 // get progression order
505 849 tmp.prog_order = bytestream2_get_byteu(&s->g);
506
507 849 tmp.nlayers = bytestream2_get_be16u(&s->g);
508 849 tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
509
510
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 841 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
849 if (tmp.mct && s->ncomponents < 3) {
511 av_log(s->avctx, AV_LOG_ERROR,
512 "MCT %"PRIu8" with too few components (%d)\n",
513 tmp.mct, s->ncomponents);
514 return AVERROR_INVALIDDATA;
515 }
516
517
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if ((ret = get_cox(s, &tmp)) < 0)
518 return ret;
519 849 tmp.init = 1;
520
2/2
✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 849 times.
3567 for (compno = 0; compno < s->ncomponents; compno++)
521
1/2
✓ Branch 0 taken 2718 times.
✗ Branch 1 not taken.
2718 if (!(properties[compno] & HAD_COC))
522 2718 memcpy(c + compno, &tmp, sizeof(tmp));
523 849 return 0;
524 }
525
526 /* Get coding parameters for a component in the whole image or a
527 * particular tile. */
528 18 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
529 uint8_t *properties)
530 {
531 int compno, ret;
532 uint8_t has_eph, has_sop;
533
534
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (bytestream2_get_bytes_left(&s->g) < 2) {
535 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
536 return AVERROR_INVALIDDATA;
537 }
538
539 18 compno = bytestream2_get_byteu(&s->g);
540
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (compno >= s->ncomponents) {
542 av_log(s->avctx, AV_LOG_ERROR,
543 "Invalid compno %d. There are %d components in the image.\n",
544 compno, s->ncomponents);
545 return AVERROR_INVALIDDATA;
546 }
547
548 18 c += compno;
549 18 has_eph = c->csty & JPEG2000_CSTY_EPH;
550 18 has_sop = c->csty & JPEG2000_CSTY_SOP;
551 18 c->csty = bytestream2_get_byteu(&s->g);
552 18 c->csty |= has_eph; //do not override eph present bits from COD
553 18 c->csty |= has_sop; //do not override sop present bits from COD
554
555
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = get_cox(s, c)) < 0)
556 return ret;
557
558 18 properties[compno] |= HAD_COC;
559 18 c->init = 1;
560 18 return 0;
561 }
562
563 4 static int get_rgn(Jpeg2000DecoderContext *s, int n)
564 {
565 uint16_t compno;
566
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
567 bytestream2_get_be16u(&s->g);
568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (bytestream2_get_byte(&s->g)) {
569 av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
570 return AVERROR_INVALIDDATA; // SRgn field value is 0
571 }
572 // SPrgn field
573 // Currently compno cannot be greater than 4.
574 // However, future implementation should support compno up to 65536
575
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (compno < s->ncomponents) {
576 int v;
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->curtileno == -1) {
578 v = bytestream2_get_byte(&s->g);
579 if (v > 30)
580 return AVERROR_PATCHWELCOME;
581 s->roi_shift[compno] = v;
582 } else {
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->tile[s->curtileno].tp_idx != 0)
584 return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
585 4 v = bytestream2_get_byte(&s->g);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (v > 30)
587 return AVERROR_PATCHWELCOME;
588 4 s->tile[s->curtileno].comp[compno].roi_shift = v;
589 }
590 4 return 0;
591 }
592 return AVERROR_INVALIDDATA;
593 }
594
595 /* Get common part for QCD and QCC segments. */
596 874 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
597 {
598 int i, x;
599
600
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 874 times.
874 if (bytestream2_get_bytes_left(&s->g) < 1)
601 return AVERROR_INVALIDDATA;
602
603 874 x = bytestream2_get_byteu(&s->g); // Sqcd
604
605 874 q->nguardbits = x >> 5;
606 874 q->quantsty = x & 0x1f;
607
608
2/2
✓ Branch 0 taken 641 times.
✓ Branch 1 taken 233 times.
874 if (q->quantsty == JPEG2000_QSTY_NONE) {
609 641 n -= 3;
610
2/4
✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 641 times.
641 if (bytestream2_get_bytes_left(&s->g) < n ||
611 n > JPEG2000_MAX_DECLEVELS*3)
612 return AVERROR_INVALIDDATA;
613
2/2
✓ Branch 0 taken 11984 times.
✓ Branch 1 taken 641 times.
12625 for (i = 0; i < n; i++)
614 11984 q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
615
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 227 times.
233 } else if (q->quantsty == JPEG2000_QSTY_SI) {
616
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (bytestream2_get_bytes_left(&s->g) < 2)
617 return AVERROR_INVALIDDATA;
618 6 x = bytestream2_get_be16u(&s->g);
619 6 q->expn[0] = x >> 11;
620 6 q->mant[0] = x & 0x7ff;
621
2/2
✓ Branch 0 taken 588 times.
✓ Branch 1 taken 6 times.
594 for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
622 588 int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
623 588 q->expn[i] = curexpn;
624 588 q->mant[i] = q->mant[0];
625 }
626 } else {
627 227 n = (n - 3) >> 1;
628
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 ||
629 n > JPEG2000_MAX_DECLEVELS*3)
630 return AVERROR_INVALIDDATA;
631
2/2
✓ Branch 0 taken 4268 times.
✓ Branch 1 taken 227 times.
4495 for (i = 0; i < n; i++) {
632 4268 x = bytestream2_get_be16u(&s->g);
633 4268 q->expn[i] = x >> 11;
634 4268 q->mant[i] = x & 0x7ff;
635 }
636 }
637 874 return 0;
638 }
639
640 /* Get quantization parameters for a particular tile or a whole image. */
641 849 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
642 const uint8_t *properties)
643 {
644 Jpeg2000QuantStyle tmp;
645 int compno, ret;
646
647 849 memset(&tmp, 0, sizeof(tmp));
648
649
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if ((ret = get_qcx(s, n, &tmp)) < 0)
650 return ret;
651
2/2
✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 849 times.
3567 for (compno = 0; compno < s->ncomponents; compno++)
652
1/2
✓ Branch 0 taken 2718 times.
✗ Branch 1 not taken.
2718 if (!(properties[compno] & HAD_QCC))
653 2718 memcpy(q + compno, &tmp, sizeof(tmp));
654 849 return 0;
655 }
656
657 /* Get quantization parameters for a component in the whole image
658 * on in a particular tile. */
659 25 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
660 uint8_t *properties)
661 {
662 int compno;
663
664
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
25 if (bytestream2_get_bytes_left(&s->g) < 1)
665 return AVERROR_INVALIDDATA;
666
667 25 compno = bytestream2_get_byteu(&s->g);
668
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (compno >= s->ncomponents) {
670 av_log(s->avctx, AV_LOG_ERROR,
671 "Invalid compno %d. There are %d components in the image.\n",
672 compno, s->ncomponents);
673 return AVERROR_INVALIDDATA;
674 }
675
676 25 properties[compno] |= HAD_QCC;
677 25 return get_qcx(s, n - 1, q + compno);
678 }
679
680 8 static int get_poc(Jpeg2000DecoderContext *s, int size, Jpeg2000POC *p)
681 {
682 int i;
683
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 int elem_size = s->ncomponents <= 257 ? 7 : 9;
684 8 Jpeg2000POC tmp = {{{0}}};
685
686
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) {
687 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
688 return AVERROR_INVALIDDATA;
689 }
690
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (elem_size > 7) {
692 avpriv_request_sample(s->avctx, "Fat POC not supported");
693 return AVERROR_PATCHWELCOME;
694 }
695
696 8 tmp.nb_poc = (size - 2) / elem_size;
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (tmp.nb_poc > MAX_POCS) {
698 avpriv_request_sample(s->avctx, "Too many POCs (%d)", tmp.nb_poc);
699 return AVERROR_PATCHWELCOME;
700 }
701
702
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 for (i = 0; i<tmp.nb_poc; i++) {
703 8 Jpeg2000POCEntry *e = &tmp.poc[i];
704 8 e->RSpoc = bytestream2_get_byteu(&s->g);
705 8 e->CSpoc = bytestream2_get_byteu(&s->g);
706 8 e->LYEpoc = bytestream2_get_be16u(&s->g);
707 8 e->REpoc = bytestream2_get_byteu(&s->g);
708 8 e->CEpoc = bytestream2_get_byteu(&s->g);
709 8 e->Ppoc = bytestream2_get_byteu(&s->g);
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!e->CEpoc)
711 e->CEpoc = 256;
712
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (e->CEpoc > s->ncomponents)
713 4 e->CEpoc = s->ncomponents;
714
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
715
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
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 || !e->LYEpoc) {
717 av_log(s->avctx, AV_LOG_ERROR, "POC Entry %d is invalid (%d, %d, %d, %d, %d, %d)\n", i,
718 e->RSpoc, e->CSpoc, e->LYEpoc, e->REpoc, e->CEpoc, e->Ppoc
719 );
720 return AVERROR_INVALIDDATA;
721 }
722 }
723
724
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) {
725 6 *p = tmp;
726 } else {
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (p->nb_poc + tmp.nb_poc > MAX_POCS) {
728 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for POC\n");
729 return AVERROR_INVALIDDATA;
730 }
731 2 memcpy(p->poc + p->nb_poc, tmp.poc, tmp.nb_poc * sizeof(tmp.poc[0]));
732 2 p->nb_poc += tmp.nb_poc;
733 }
734
735 8 p->is_default = 0;
736
737 8 return 0;
738 }
739
740
741 /* Get start of tile segment. */
742 3224 static int get_sot(Jpeg2000DecoderContext *s, int n)
743 {
744 Jpeg2000TilePart *tp;
745 uint16_t Isot;
746 uint32_t Psot;
747 unsigned TPsot;
748
749
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3224 times.
3224 if (bytestream2_get_bytes_left(&s->g) < 8)
750 return AVERROR_INVALIDDATA;
751
752 3224 s->curtileno = 0;
753 3224 Isot = bytestream2_get_be16u(&s->g); // Isot
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
3224 if (Isot >= s->numXtiles * s->numYtiles)
755 return AVERROR_INVALIDDATA;
756
757 3224 s->curtileno = Isot;
758 3224 Psot = bytestream2_get_be32u(&s->g); // Psot
759 3224 TPsot = bytestream2_get_byteu(&s->g); // TPsot
760
761 /* Read TNSot but not used */
762 3224 bytestream2_get_byteu(&s->g); // TNsot
763
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
3224 if (!Psot)
765 Psot = bytestream2_get_bytes_left(&s->g) - 2 + n + 2;
766
767
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3224 times.
3224 if (Psot > bytestream2_get_bytes_left(&s->g) - 2 + n + 2) {
768 av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
769 return AVERROR_INVALIDDATA;
770 }
771
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
3224 if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
773 avpriv_request_sample(s->avctx, "Too many tile parts");
774 return AVERROR_PATCHWELCOME;
775 }
776
777 3224 s->tile[Isot].tp_idx = TPsot;
778 3224 tp = s->tile[Isot].tile_part + TPsot;
779 3224 tp->tile_index = Isot;
780 3224 tp->tp_end = s->g.buffer + Psot - n - 2;
781
782
2/2
✓ Branch 0 taken 3208 times.
✓ Branch 1 taken 16 times.
3224 if (!TPsot) {
783 3208 Jpeg2000Tile *tile = s->tile + s->curtileno;
784
785 /* copy defaults */
786 3208 memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
787 3208 memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
788 3208 memcpy(&tile->poc , &s->poc , sizeof(tile->poc));
789 3208 tile->poc.is_default = 1;
790 }
791
792 3224 return 0;
793 }
794
795 4 static int read_crg(Jpeg2000DecoderContext *s, int n)
796 {
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->ncomponents*4 != n - 2) {
798 av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
799 return AVERROR_INVALIDDATA;
800 }
801 4 bytestream2_skip(&s->g, n - 2);
802 4 return 0;
803 }
804 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
805 * Used to know the number of tile parts and lengths.
806 * There may be multiple TLMs in the header.
807 * TODO: The function is not used for tile-parts management, nor anywhere else.
808 * It can be useful to allocate memory for tile parts, before managing the SOT
809 * markers. Parsing the TLM header is needed to increment the input header
810 * buffer.
811 * This marker is mandatory for DCI. */
812 11 static int get_tlm(Jpeg2000DecoderContext *s, int n)
813 {
814 uint8_t Stlm, ST, SP, tile_tlm, i;
815 11 bytestream2_get_byte(&s->g); /* Ztlm: skipped */
816 11 Stlm = bytestream2_get_byte(&s->g);
817
818 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
819 11 ST = (Stlm >> 4) & 0x03;
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ST == 0x03) {
821 av_log(s->avctx, AV_LOG_ERROR, "TLM marker contains invalid ST value.\n");
822 return AVERROR_INVALIDDATA;
823 }
824
825 11 SP = (Stlm >> 6) & 0x01;
826 11 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
827
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 11 times.
49 for (i = 0; i < tile_tlm; i++) {
828
2/5
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
38 switch (ST) {
829 case 0:
830 break;
831 17 case 1:
832 17 bytestream2_get_byte(&s->g);
833 17 break;
834 21 case 2:
835 21 bytestream2_get_be16(&s->g);
836 21 break;
837 case 3:
838 bytestream2_get_be32(&s->g);
839 break;
840 }
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (SP == 0) {
842 bytestream2_get_be16(&s->g);
843 } else {
844 38 bytestream2_get_be32(&s->g);
845 }
846 }
847 11 return 0;
848 }
849
850 514 static int get_plt(Jpeg2000DecoderContext *s, int n)
851 {
852 int i;
853 int v;
854
855 514 av_log(s->avctx, AV_LOG_DEBUG,
856 514 "PLT marker at pos 0x%X\n", bytestream2_tell(&s->g) - 4);
857
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514 times.
514 if (n < 4)
859 return AVERROR_INVALIDDATA;
860
861 514 /*Zplt =*/ bytestream2_get_byte(&s->g);
862
863
2/2
✓ Branch 0 taken 73452 times.
✓ Branch 1 taken 514 times.
73966 for (i = 0; i < n - 3; i++) {
864 73452 v = bytestream2_get_byte(&s->g);
865 }
866
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514 times.
514 if (v & 0x80)
867 return AVERROR_INVALIDDATA;
868
869 514 return 0;
870 }
871
872 static int get_ppm(Jpeg2000DecoderContext *s, int n)
873 {
874 void *new;
875
876 if (n < 3) {
877 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
878 return AVERROR_INVALIDDATA;
879 }
880 bytestream2_get_byte(&s->g); //Zppm is skipped and not used
881 new = av_realloc(s->packed_headers,
882 s->packed_headers_size + n - 3);
883 if (new) {
884 s->packed_headers = new;
885 } else
886 return AVERROR(ENOMEM);
887 s->has_ppm = 1;
888 memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
889 bytestream2_get_bufferu(&s->g, s->packed_headers + s->packed_headers_size,
890 n - 3);
891 s->packed_headers_size += n - 3;
892
893 return 0;
894 }
895
896 static int get_ppt(Jpeg2000DecoderContext *s, int n)
897 {
898 Jpeg2000Tile *tile;
899 void *new;
900
901 if (n < 3) {
902 av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPT data.\n");
903 return AVERROR_INVALIDDATA;
904 }
905 if (s->curtileno < 0)
906 return AVERROR_INVALIDDATA;
907
908 tile = &s->tile[s->curtileno];
909 if (tile->tp_idx != 0) {
910 av_log(s->avctx, AV_LOG_ERROR,
911 "PPT marker can occur only on first tile part of a tile.\n");
912 return AVERROR_INVALIDDATA;
913 }
914
915 tile->has_ppt = 1; // this tile has a ppt marker
916 bytestream2_get_byte(&s->g); // Zppt is skipped and not used
917 new = av_realloc(tile->packed_headers,
918 tile->packed_headers_size + n - 3);
919 if (new) {
920 tile->packed_headers = new;
921 } else
922 return AVERROR(ENOMEM);
923 memset(&tile->packed_headers_stream, 0, sizeof(tile->packed_headers_stream));
924 bytestream2_get_bufferu(&s->g, tile->packed_headers + tile->packed_headers_size, n - 3);
925 tile->packed_headers_size += n - 3;
926
927 return 0;
928 }
929
930 2877 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
931 {
932 int compno;
933 2877 int tilex = tileno % s->numXtiles;
934 2877 int tiley = tileno / s->numXtiles;
935 2877 Jpeg2000Tile *tile = s->tile + tileno;
936
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2877 times.
2877 if (!tile->comp)
938 return AVERROR(ENOMEM);
939
940 2877 tile->coord[0][0] = av_clip(tilex * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
941 2877 tile->coord[0][1] = av_clip((tilex + 1) * (int64_t)s->tile_width + s->tile_offset_x, s->image_offset_x, s->width);
942 2877 tile->coord[1][0] = av_clip(tiley * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
943 2877 tile->coord[1][1] = av_clip((tiley + 1) * (int64_t)s->tile_height + s->tile_offset_y, s->image_offset_y, s->height);
944
945
2/2
✓ Branch 0 taken 9252 times.
✓ Branch 1 taken 2877 times.
12129 for (compno = 0; compno < s->ncomponents; compno++) {
946 9252 Jpeg2000Component *comp = tile->comp + compno;
947 9252 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
948 9252 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
949 int ret; // global bandno
950
951 9252 comp->coord_o[0][0] = tile->coord[0][0];
952 9252 comp->coord_o[0][1] = tile->coord[0][1];
953 9252 comp->coord_o[1][0] = tile->coord[1][0];
954 9252 comp->coord_o[1][1] = tile->coord[1][1];
955
956 9252 comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], s->cdx[compno]);
957 9252 comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], s->cdx[compno]);
958 9252 comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], s->cdy[compno]);
959 9252 comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], s->cdy[compno]);
960
961 9252 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
962 9252 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
963 9252 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
964 9252 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
965
966
2/2
✓ Branch 0 taken 9250 times.
✓ Branch 1 taken 2 times.
9252 if (!comp->roi_shift)
967 9250 comp->roi_shift = s->roi_shift[compno];
968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9252 times.
9252 if (!codsty->init)
969 return AVERROR_INVALIDDATA;
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9252 times.
9252 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
971 9252 s->cbps[compno], s->cdx[compno],
972 s->cdy[compno], s->avctx))
973 return ret;
974 }
975 2877 return 0;
976 }
977
978 /* Read the number of coding passes. */
979 890514 static int getnpasses(Jpeg2000DecoderContext *s)
980 {
981 int num;
982
2/2
✓ Branch 1 taken 61437 times.
✓ Branch 2 taken 829077 times.
890514 if (!get_bits(s, 1))
983 61437 return 1;
984
2/2
✓ Branch 1 taken 28166 times.
✓ Branch 2 taken 800911 times.
829077 if (!get_bits(s, 1))
985 28166 return 2;
986
2/2
✓ Branch 1 taken 165539 times.
✓ Branch 2 taken 635372 times.
800911 if ((num = get_bits(s, 2)) != 3)
987
1/2
✓ Branch 0 taken 165539 times.
✗ Branch 1 not taken.
165539 return num < 0 ? num : 3 + num;
988
2/2
✓ Branch 1 taken 592751 times.
✓ Branch 2 taken 42621 times.
635372 if ((num = get_bits(s, 5)) != 31)
989
1/2
✓ Branch 0 taken 592751 times.
✗ Branch 1 not taken.
592751 return num < 0 ? num : 6 + num;
990 42621 num = get_bits(s, 7);
991
1/2
✓ Branch 0 taken 42621 times.
✗ Branch 1 not taken.
42621 return num < 0 ? num : 37 + num;
992 }
993
994 890514 static int getlblockinc(Jpeg2000DecoderContext *s)
995 {
996 890514 int res = 0, ret;
997
2/2
✓ Branch 1 taken 500727 times.
✓ Branch 2 taken 890514 times.
1391241 while (ret = get_bits(s, 1)) {
998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 500727 times.
500727 if (ret < 0)
999 return ret;
1000 500727 res++;
1001 }
1002 890514 return res;
1003 }
1004
1005 static inline void select_header(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile,
1006 int *tp_index)
1007 {
1008 s->g = tile->tile_part[*tp_index].header_tpg;
1009 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1010 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1011 s->g = tile->tile_part[++(*tp_index)].tpg;
1012 }
1013 }
1014 }
1015
1016 87096 static inline void select_stream(Jpeg2000DecoderContext *s, const Jpeg2000Tile *tile,
1017 int *tp_index, const Jpeg2000CodingStyle *codsty)
1018 {
1019 87096 s->g = tile->tile_part[*tp_index].tpg;
1020
3/4
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 87091 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
87096 if (bytestream2_get_bytes_left(&s->g) == 0 && s->bit_index == 8) {
1021
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
1022 5 s->g = tile->tile_part[++(*tp_index)].tpg;
1023 }
1024 }
1025
2/2
✓ Branch 0 taken 25452 times.
✓ Branch 1 taken 61644 times.
87096 if (codsty->csty & JPEG2000_CSTY_SOP) {
1026
1/2
✓ Branch 1 taken 25452 times.
✗ Branch 2 not taken.
25452 if (bytestream2_peek_be32(&s->g) == JPEG2000_SOP_FIXED_BYTES)
1027 25452 bytestream2_skip(&s->g, JPEG2000_SOP_BYTE_LENGTH);
1028 else
1029 av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1030 }
1031 87096 }
1032
1033 87168 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, int *tp_index,
1034 const Jpeg2000CodingStyle *codsty,
1035 Jpeg2000ResLevel *rlevel, int precno,
1036 int layno, const uint8_t *expn, int numgbits)
1037 {
1038 int bandno, cblkno, ret, nb_code_blocks;
1039 int cwsno;
1040
1041
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 87096 times.
87168 if (layno < rlevel->band[0].prec[precno].decoded_layers)
1042 72 return 0;
1043 87096 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
1044 // Select stream to read from
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87096 times.
87096 if (s->has_ppm)
1046 select_header(s, tile, tp_index);
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87096 times.
87096 else if (tile->has_ppt)
1048 s->g = tile->packed_headers_stream;
1049 else
1050 87096 select_stream(s, tile, tp_index, codsty);
1051
1052
2/2
✓ Branch 1 taken 3926 times.
✓ Branch 2 taken 83170 times.
87096 if (!(ret = get_bits(s, 1))) {
1053 3926 jpeg2000_flush(s);
1054 3926 goto skip_data;
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83170 times.
83170 } else if (ret < 0)
1056 return ret;
1057
1058
2/2
✓ Branch 0 taken 219812 times.
✓ Branch 1 taken 83170 times.
302982 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1059 219812 Jpeg2000Band *band = rlevel->band + bandno;
1060 219812 Jpeg2000Prec *prec = band->prec + precno;
1061
1062
2/2
✓ Branch 0 taken 219810 times.
✓ Branch 1 taken 2 times.
219812 if (band->coord[0][0] == band->coord[0][1] ||
1063
2/2
✓ Branch 0 taken 7174 times.
✓ Branch 1 taken 212636 times.
219810 band->coord[1][0] == band->coord[1][1])
1064 7176 continue;
1065 212636 nb_code_blocks = prec->nb_codeblocks_height *
1066 212636 prec->nb_codeblocks_width;
1067
2/2
✓ Branch 0 taken 1001313 times.
✓ Branch 1 taken 212636 times.
1213949 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1068 1001313 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1069 int incl, newpasses, llen;
1070 void *tmp;
1071
1072
2/2
✓ Branch 0 taken 87477 times.
✓ Branch 1 taken 913836 times.
1001313 if (cblk->npasses)
1073 87477 incl = get_bits(s, 1);
1074 else
1075 913836 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
1076
2/2
✓ Branch 0 taken 110799 times.
✓ Branch 1 taken 890514 times.
1001313 if (!incl)
1077 110799 continue;
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890514 times.
890514 else if (incl < 0)
1079 return incl;
1080
1081
2/2
✓ Branch 0 taken 813094 times.
✓ Branch 1 taken 77420 times.
890514 if (!cblk->npasses) {
1082 813094 int zbp = tag_tree_decode(s, prec->zerobits + cblkno, 100);
1083 813094 int v = expn[bandno] + numgbits - 1 - zbp;
1084
1085
2/4
✓ Branch 0 taken 813094 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 813094 times.
813094 if (v < 0 || v > 30) {
1086 av_log(s->avctx, AV_LOG_ERROR,
1087 "nonzerobits %d invalid or unsupported\n", v);
1088 return AVERROR_INVALIDDATA;
1089 }
1090 813094 cblk->zbp = zbp;
1091 813094 cblk->nonzerobits = v;
1092 }
1093
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 890514 times.
890514 if ((newpasses = getnpasses(s)) < 0)
1094 return newpasses;
1095 av_assert2(newpasses > 0);
1096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890514 times.
890514 if (cblk->npasses + newpasses >= JPEG2000_MAX_PASSES) {
1097 avpriv_request_sample(s->avctx, "Too many passes");
1098 return AVERROR_PATCHWELCOME;
1099 }
1100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 890514 times.
890514 if ((llen = getlblockinc(s)) < 0)
1101 return llen;
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890514 times.
890514 if (cblk->lblock + llen + av_log2(newpasses) > 16) {
1103 avpriv_request_sample(s->avctx,
1104 "Block with length beyond 16 bits");
1105 return AVERROR_PATCHWELCOME;
1106 }
1107
1108 890514 cblk->lblock += llen;
1109
1110 890514 cblk->nb_lengthinc = 0;
1111 890514 cblk->nb_terminationsinc = 0;
1112 890514 av_free(cblk->lengthinc);
1113 890514 cblk->lengthinc = av_calloc(newpasses, sizeof(*cblk->lengthinc));
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890514 times.
890514 if (!cblk->lengthinc)
1115 return AVERROR(ENOMEM);
1116 890514 tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start));
1117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890514 times.
890514 if (!tmp)
1118 return AVERROR(ENOMEM);
1119 890514 cblk->data_start = tmp;
1120 do {
1121 893042 int newpasses1 = 0;
1122
1123
2/2
✓ Branch 0 taken 11804370 times.
✓ Branch 1 taken 888880 times.
12693250 while (newpasses1 < newpasses) {
1124 11804370 newpasses1 ++;
1125
2/2
✓ Branch 1 taken 4162 times.
✓ Branch 2 taken 11800208 times.
11804370 if (needs_termination(codsty->cblk_style, cblk->npasses + newpasses1 - 1)) {
1126 4162 cblk->nb_terminationsinc ++;
1127 4162 break;
1128 }
1129 }
1130
1131
3/4
✓ Branch 0 taken 830447 times.
✓ Branch 1 taken 62595 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 830447 times.
893042 if (newpasses > 1 && (codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F)) {
1132 // Retrieve pass lengths for each pass
1133 int href_passes = (cblk->npasses + newpasses - 1) % 3;
1134 int eb = av_log2(newpasses - href_passes);
1135 int extra_bit = newpasses > 2 ? 1 : 0;
1136 if ((ret = get_bits(s, llen + eb + 3)) < 0)
1137 return ret;
1138 cblk->pass_lengths[0] = ret;
1139 if ((ret = get_bits(s, llen + 3 + extra_bit)) < 0)
1140 return ret;
1141 cblk->pass_lengths[1] = ret;
1142 ret = cblk->pass_lengths[0] + cblk->pass_lengths[1];
1143 } else {
1144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 893042 times.
893042 if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
1145 return ret;
1146 893042 cblk->pass_lengths[0] = ret;
1147 }
1148
2/2
✓ Branch 0 taken 822065 times.
✓ Branch 1 taken 70977 times.
893042 if (ret > cblk->data_allocated) {
1149 822065 size_t new_size = FFMAX(2*cblk->data_allocated, ret);
1150 822065 void *new = av_realloc(cblk->data, new_size);
1151
1/2
✓ Branch 0 taken 822065 times.
✗ Branch 1 not taken.
822065 if (new) {
1152 822065 cblk->data = new;
1153 822065 cblk->data_allocated = new_size;
1154 }
1155 }
1156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 893042 times.
893042 if (ret > cblk->data_allocated) {
1157 avpriv_request_sample(s->avctx,
1158 "Block with lengthinc greater than %"SIZE_SPECIFIER"",
1159 cblk->data_allocated);
1160 return AVERROR_PATCHWELCOME;
1161 }
1162 893042 cblk->lengthinc[cblk->nb_lengthinc++] = ret;
1163 893042 cblk->npasses += newpasses1;
1164 893042 newpasses -= newpasses1;
1165
2/2
✓ Branch 0 taken 2528 times.
✓ Branch 1 taken 890514 times.
893042 } while(newpasses);
1166 }
1167 }
1168 83170 jpeg2000_flush(s);
1169
1170
2/2
✓ Branch 0 taken 25321 times.
✓ Branch 1 taken 57849 times.
83170 if (codsty->csty & JPEG2000_CSTY_EPH) {
1171
1/2
✓ Branch 1 taken 25321 times.
✗ Branch 2 not taken.
25321 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1172 25321 bytestream2_skip(&s->g, 2);
1173 else
1174 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1175 }
1176
1177 // Save state of stream
1178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83170 times.
83170 if (s->has_ppm) {
1179 tile->tile_part[*tp_index].header_tpg = s->g;
1180 select_stream(s, tile, tp_index, codsty);
1181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83170 times.
83170 } else if (tile->has_ppt) {
1182 tile->packed_headers_stream = s->g;
1183 select_stream(s, tile, tp_index, codsty);
1184 }
1185
2/2
✓ Branch 0 taken 219812 times.
✓ Branch 1 taken 83170 times.
302982 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1186 219812 Jpeg2000Band *band = rlevel->band + bandno;
1187 219812 Jpeg2000Prec *prec = band->prec + precno;
1188
1189 219812 nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
1190
2/2
✓ Branch 0 taken 1008487 times.
✓ Branch 1 taken 219812 times.
1228299 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
1191 1008487 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1192
4/4
✓ Branch 0 taken 1006853 times.
✓ Branch 1 taken 1634 times.
✓ Branch 2 taken 117973 times.
✓ Branch 3 taken 888880 times.
1008487 if (!cblk->nb_terminationsinc && !cblk->lengthinc)
1193 117973 continue;
1194
2/2
✓ Branch 0 taken 893042 times.
✓ Branch 1 taken 890514 times.
1783556 for (cwsno = 0; cwsno < cblk->nb_lengthinc; cwsno ++) {
1195
2/2
✓ Branch 0 taken 849413 times.
✓ Branch 1 taken 43629 times.
893042 if (cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4) {
1196 849413 size_t new_size = FFMAX(2*cblk->data_allocated, cblk->length + cblk->lengthinc[cwsno] + 4);
1197 849413 void *new = av_realloc(cblk->data, new_size);
1198
1/2
✓ Branch 0 taken 849413 times.
✗ Branch 1 not taken.
849413 if (new) {
1199 849413 cblk->data = new;
1200 849413 cblk->data_allocated = new_size;
1201 }
1202 }
1203
1/2
✓ Branch 1 taken 893042 times.
✗ Branch 2 not taken.
893042 if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc[cwsno]
1204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 893042 times.
893042 || cblk->data_allocated < cblk->length + cblk->lengthinc[cwsno] + 4
1205 ) {
1206 av_log(s->avctx, AV_LOG_ERROR,
1207 "Block length %"PRIu16" or lengthinc %d is too large, left %d\n",
1208 cblk->length, cblk->lengthinc[cwsno], bytestream2_get_bytes_left(&s->g));
1209 return AVERROR_INVALIDDATA;
1210 }
1211
1212 893042 bytestream2_get_bufferu(&s->g, cblk->data + cblk->length, cblk->lengthinc[cwsno]);
1213 893042 cblk->length += cblk->lengthinc[cwsno];
1214 893042 cblk->lengthinc[cwsno] = 0;
1215
2/2
✓ Branch 0 taken 4162 times.
✓ Branch 1 taken 888880 times.
893042 if (cblk->nb_terminationsinc) {
1216 4162 cblk->nb_terminationsinc--;
1217 4162 cblk->nb_terminations++;
1218 4162 cblk->data[cblk->length++] = 0xFF;
1219 4162 cblk->data[cblk->length++] = 0xFF;
1220 4162 cblk->data_start[cblk->nb_terminations] = cblk->length;
1221 }
1222 }
1223 890514 av_freep(&cblk->lengthinc);
1224 }
1225 }
1226 // Save state of stream
1227 83170 tile->tile_part[*tp_index].tpg = s->g;
1228 83170 return 0;
1229
1230 3926 skip_data:
1231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3926 times.
3926 if (codsty->csty & JPEG2000_CSTY_EPH) {
1232 if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
1233 bytestream2_skip(&s->g, 2);
1234 else
1235 av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found. instead %X\n", bytestream2_peek_be32(&s->g));
1236 }
1237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3926 times.
3926 if (s->has_ppm) {
1238 tile->tile_part[*tp_index].header_tpg = s->g;
1239 select_stream(s, tile, tp_index, codsty);
1240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3926 times.
3926 } else if (tile->has_ppt) {
1241 tile->packed_headers_stream = s->g;
1242 select_stream(s, tile, tp_index, codsty);
1243 }
1244 3926 tile->tile_part[*tp_index].tpg = s->g;
1245 3926 return 0;
1246 }
1247
1248 2878 static int jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1249 int RSpoc, int CSpoc,
1250 int LYEpoc, int REpoc, int CEpoc,
1251 int Ppoc, int *tp_index)
1252 {
1253 2878 int ret = 0;
1254 int layno, reslevelno, compno, precno, ok_reslevel;
1255 int x, y;
1256 int step_x, step_y;
1257
1258
4/6
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 2615 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2878 switch (Ppoc) {
1259 259 case JPEG2000_PGOD_RLCP:
1260 259 av_log(s->avctx, AV_LOG_DEBUG, "Progression order RLCP\n");
1261 259 ok_reslevel = 1;
1262
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++) {
1263 1298 ok_reslevel = 0;
1264
2/2
✓ Branch 0 taken 10385 times.
✓ Branch 1 taken 1298 times.
11683 for (layno = 0; layno < LYEpoc; layno++) {
1265
2/2
✓ Branch 0 taken 31105 times.
✓ Branch 1 taken 10385 times.
41490 for (compno = CSpoc; compno < CEpoc; compno++) {
1266 31105 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1267 31105 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1268
2/2
✓ Branch 0 taken 24920 times.
✓ Branch 1 taken 6185 times.
31105 if (reslevelno < codsty->nreslevels) {
1269 24920 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1270 reslevelno;
1271 24920 ok_reslevel = 1;
1272
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++)
1273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26420 times.
26420 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1274 codsty, rlevel,
1275 precno, layno,
1276 26420 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1277
2/2
✓ Branch 0 taken 20235 times.
✓ Branch 1 taken 6185 times.
26420 qntsty->nguardbits)) < 0)
1278 return ret;
1279 }
1280 }
1281 }
1282 }
1283 259 break;
1284
1285 2615 case JPEG2000_PGOD_LRCP:
1286 2615 av_log(s->avctx, AV_LOG_DEBUG, "Progression order LRCP\n");
1287
2/2
✓ Branch 0 taken 2690 times.
✓ Branch 1 taken 2615 times.
5305 for (layno = 0; layno < LYEpoc; layno++) {
1288 2690 ok_reslevel = 1;
1289
4/4
✓ Branch 0 taken 21115 times.
✓ Branch 1 taken 2682 times.
✓ Branch 2 taken 21107 times.
✓ Branch 3 taken 8 times.
23797 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1290 21107 ok_reslevel = 0;
1291
2/2
✓ Branch 0 taken 68049 times.
✓ Branch 1 taken 21107 times.
89156 for (compno = CSpoc; compno < CEpoc; compno++) {
1292 68049 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1293 68049 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1294
2/2
✓ Branch 0 taken 59499 times.
✓ Branch 1 taken 8550 times.
68049 if (reslevelno < codsty->nreslevels) {
1295 59499 Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
1296 reslevelno;
1297 59499 ok_reslevel = 1;
1298
2/2
✓ Branch 0 taken 59499 times.
✓ Branch 1 taken 59499 times.
118998 for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
1299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 59499 times.
59499 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1300 codsty, rlevel,
1301 precno, layno,
1302 59499 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1303
2/2
✓ Branch 0 taken 50925 times.
✓ Branch 1 taken 8574 times.
59499 qntsty->nguardbits)) < 0)
1304 return ret;
1305 }
1306 }
1307 }
1308 }
1309 2615 break;
1310
1311 3 case JPEG2000_PGOD_CPRL:
1312 3 av_log(s->avctx, AV_LOG_DEBUG, "Progression order CPRL\n");
1313
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (compno = CSpoc; compno < CEpoc; compno++) {
1314 9 Jpeg2000Component *comp = tile->comp + compno;
1315 9 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1316 9 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1317 9 step_x = 32;
1318 9 step_y = 32;
1319
1320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (RSpoc >= FFMIN(codsty->nreslevels, REpoc))
1321 continue;
1322
1323
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 9 times.
69 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1324 60 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1325 60 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1326 60 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1327 60 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1328 }
1329
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){
1330 avpriv_request_sample(s->avctx, "CPRL with large step");
1331 return AVERROR_PATCHWELCOME;
1332 }
1333 9 step_x = 1<<step_x;
1334 9 step_y = 1<<step_y;
1335
1336
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) {
1337
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) {
1338
2/2
✓ Branch 0 taken 1464 times.
✓ Branch 1 taken 243 times.
1707 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1339 unsigned prcx, prcy;
1340 1464 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1341 1464 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1342 1464 int xc = x / s->cdx[compno];
1343 1464 int yc = y / s->cdy[compno];
1344
1345
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
1346 816 continue;
1347
1348
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
1349 270 continue;
1350
1351 // check if a precinct exists
1352 378 prcx = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> rlevel->log2_prec_width;
1353 378 prcy = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> rlevel->log2_prec_height;
1354 378 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1355 378 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1356
1357 378 precno = prcx + rlevel->num_precincts_x * prcy;
1358
1359
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) {
1360 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1361 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1362 continue;
1363 }
1364
1365
2/2
✓ Branch 0 taken 1074 times.
✓ Branch 1 taken 378 times.
1452 for (layno = 0; layno < LYEpoc; layno++) {
1366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1074 times.
1074 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1367 precno, layno,
1368 1074 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1369
2/2
✓ Branch 0 taken 978 times.
✓ Branch 1 taken 96 times.
1074 qntsty->nguardbits)) < 0)
1370 return ret;
1371 }
1372 }
1373 }
1374 }
1375 }
1376 3 break;
1377
1378 case JPEG2000_PGOD_RPCL:
1379 av_log(s->avctx, AV_LOG_WARNING, "Progression order RPCL\n");
1380 ok_reslevel = 1;
1381 for (reslevelno = RSpoc; ok_reslevel && reslevelno < REpoc; reslevelno++) {
1382 ok_reslevel = 0;
1383 step_x = 30;
1384 step_y = 30;
1385 for (compno = CSpoc; compno < CEpoc; compno++) {
1386 Jpeg2000Component *comp = tile->comp + compno;
1387 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1388
1389 if (reslevelno < codsty->nreslevels) {
1390 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1391 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1392 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1393 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1394 }
1395 }
1396 step_x = 1<<step_x;
1397 step_y = 1<<step_y;
1398
1399 for (y = tile->coord[1][0]; y < tile->coord[1][1]; y = (y/step_y + 1)*step_y) {
1400 for (x = tile->coord[0][0]; x < tile->coord[0][1]; x = (x/step_x + 1)*step_x) {
1401 for (compno = CSpoc; compno < CEpoc; compno++) {
1402 Jpeg2000Component *comp = tile->comp + compno;
1403 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1404 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1405 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1406 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1407 unsigned prcx, prcy;
1408 int trx0, try0;
1409
1410 if (!s->cdx[compno] || !s->cdy[compno])
1411 return AVERROR_INVALIDDATA;
1412
1413 if (reslevelno >= codsty->nreslevels)
1414 continue;
1415
1416 trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1417 try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1418
1419 if (!(y % ((uint64_t)s->cdy[compno] << (rlevel->log2_prec_height + reducedresno)) == 0 ||
1420 (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1421 continue;
1422
1423 if (!(x % ((uint64_t)s->cdx[compno] << (rlevel->log2_prec_width + reducedresno)) == 0 ||
1424 (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1425 continue;
1426
1427 // check if a precinct exists
1428 prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1429 prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1430 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1431 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1432
1433 precno = prcx + rlevel->num_precincts_x * prcy;
1434
1435 ok_reslevel = 1;
1436 if (prcx >= rlevel->num_precincts_x || prcy >= rlevel->num_precincts_y) {
1437 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1438 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1439 continue;
1440 }
1441
1442 for (layno = 0; layno < LYEpoc; layno++) {
1443 if ((ret = jpeg2000_decode_packet(s, tile, tp_index,
1444 codsty, rlevel,
1445 precno, layno,
1446 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1447 qntsty->nguardbits)) < 0)
1448 return ret;
1449 }
1450 }
1451 }
1452 }
1453 }
1454 break;
1455
1456 1 case JPEG2000_PGOD_PCRL:
1457 1 av_log(s->avctx, AV_LOG_WARNING, "Progression order PCRL\n");
1458 1 step_x = 32;
1459 1 step_y = 32;
1460
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (compno = CSpoc; compno < CEpoc; compno++) {
1461 4 Jpeg2000Component *comp = tile->comp + compno;
1462 4 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1463
1464
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
29 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1465 25 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1466 25 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1467 25 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1468 25 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1469 }
1470 }
1471
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){
1472 avpriv_request_sample(s->avctx, "PCRL with large step");
1473 return AVERROR_PATCHWELCOME;
1474 }
1475 1 step_x = 1<<step_x;
1476 1 step_y = 1<<step_y;
1477
1478
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) {
1479
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) {
1480
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (compno = CSpoc; compno < CEpoc; compno++) {
1481 4 Jpeg2000Component *comp = tile->comp + compno;
1482 4 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1483 4 Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
1484
1485
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])
1486 return AVERROR_INVALIDDATA;
1487
1488
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
29 for (reslevelno = RSpoc; reslevelno < FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
1489 unsigned prcx, prcy;
1490 25 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1491 25 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1492 int trx0, try0;
1493
1494 25 trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], (int64_t)s->cdx[compno] << reducedresno);
1495 25 try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], (int64_t)s->cdy[compno] << reducedresno);
1496
1497
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 ||
1498 (y == tile->coord[1][0] && ((int64_t)try0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_height)))))
1499 continue;
1500
1501
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 ||
1502 (x == tile->coord[0][0] && ((int64_t)trx0 << reducedresno) % (1ULL << (reducedresno + rlevel->log2_prec_width)))))
1503 continue;
1504
1505 // check if a precinct exists
1506 25 prcx = ff_jpeg2000_ceildiv(x, (int64_t)s->cdx[compno] << reducedresno) >> rlevel->log2_prec_width;
1507 25 prcy = ff_jpeg2000_ceildiv(y, (int64_t)s->cdy[compno] << reducedresno) >> rlevel->log2_prec_height;
1508 25 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> rlevel->log2_prec_width;
1509 25 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> rlevel->log2_prec_height;
1510
1511 25 precno = prcx + rlevel->num_precincts_x * prcy;
1512
1513
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) {
1514 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1515 prcx, prcy, rlevel->num_precincts_x, rlevel->num_precincts_y);
1516 continue;
1517 }
1518
1519
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 25 times.
200 for (layno = 0; layno < LYEpoc; layno++) {
1520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 175 times.
175 if ((ret = jpeg2000_decode_packet(s, tile, tp_index, codsty, rlevel,
1521 precno, layno,
1522 175 qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
1523
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 28 times.
175 qntsty->nguardbits)) < 0)
1524 return ret;
1525 }
1526 }
1527 }
1528 }
1529 }
1530 1 break;
1531
1532 default:
1533 break;
1534 }
1535
1536 2878 return ret;
1537 }
1538
1539 2877 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1540 {
1541 2877 int ret = AVERROR_BUG;
1542 int i;
1543 2877 int tp_index = 0;
1544
1545 2877 s->bit_index = 8;
1546
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2868 times.
2877 if (tile->poc.nb_poc) {
1547
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
19 for (i=0; i<tile->poc.nb_poc; i++) {
1548 10 Jpeg2000POCEntry *e = &tile->poc.poc[i];
1549 30 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1550 10 e->RSpoc, e->CSpoc,
1551 10 FFMIN(e->LYEpoc, tile->codsty[0].nlayers),
1552 10 e->REpoc,
1553 10 FFMIN(e->CEpoc, s->ncomponents),
1554
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 e->Ppoc, &tp_index
1555 );
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
1557 return ret;
1558 }
1559 } else {
1560 2868 ret = jpeg2000_decode_packets_po_iteration(s, tile,
1561 0, 0,
1562 2868 tile->codsty[0].nlayers,
1563 33,
1564 s->ncomponents,
1565 2868 tile->codsty[0].prog_order,
1566 &tp_index
1567 );
1568 }
1569 /* EOC marker reached */
1570 2877 bytestream2_skip(&s->g, 2);
1571
1572 2877 return ret;
1573 }
1574
1575 /* TIER-1 routines */
1576 3878124 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
1577 int bpno, int bandno,
1578 int vert_causal_ctx_csty_symbol)
1579 {
1580 3878124 int mask = 3 << (bpno - 1), y0, x, y;
1581
1582
2/2
✓ Branch 0 taken 13793633 times.
✓ Branch 1 taken 3878124 times.
17671757 for (y0 = 0; y0 < height; y0 += 4)
1583
2/2
✓ Branch 0 taken 229120958 times.
✓ Branch 1 taken 13793633 times.
242914591 for (x = 0; x < width; x++)
1584
4/4
✓ Branch 0 taken 1085097638 times.
✓ Branch 1 taken 55798227 times.
✓ Branch 2 taken 911774907 times.
✓ Branch 3 taken 173322731 times.
1140895865 for (y = y0; y < height && y < y0 + 4; y++) {
1585 911774907 int flags_mask = -1;
1586
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 911774907 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
911774907 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1587 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1588
2/2
✓ Branch 0 taken 554874401 times.
✓ Branch 1 taken 356900506 times.
911774907 if ((t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB & flags_mask)
1589
2/2
✓ Branch 0 taken 246564267 times.
✓ Branch 1 taken 308310134 times.
554874401 && !(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1590
2/2
✓ Branch 2 taken 77812673 times.
✓ Branch 3 taken 168751594 times.
246564267 if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, bandno))) {
1591 77812673 int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask, &xorbit);
1592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77812673 times.
77812673 if (t1->mqc.raw)
1593 t1->data[(y) * t1->stride + x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
1594 else
1595 155625346 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
1596
2/2
✓ Branch 0 taken 38719809 times.
✓ Branch 1 taken 39092864 times.
77812673 -mask : mask;
1597
1598 77812673 ff_jpeg2000_set_significance(t1, x, y,
1599 77812673 t1->data[(y) * t1->stride + x] < 0);
1600 }
1601 246564267 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_VIS;
1602 }
1603 }
1604 3878124 }
1605
1606 3580941 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
1607 int bpno, int vert_causal_ctx_csty_symbol)
1608 {
1609 int phalf, nhalf;
1610 int y0, x, y;
1611
1612 3580941 phalf = 1 << (bpno - 1);
1613 3580941 nhalf = -phalf;
1614
1615
2/2
✓ Branch 0 taken 12763261 times.
✓ Branch 1 taken 3580941 times.
16344202 for (y0 = 0; y0 < height; y0 += 4)
1616
2/2
✓ Branch 0 taken 213513015 times.
✓ Branch 1 taken 12763261 times.
226276276 for (x = 0; x < width; x++)
1617
4/4
✓ Branch 0 taken 1011710926 times.
✓ Branch 1 taken 51580914 times.
✓ Branch 2 taken 849778825 times.
✓ Branch 3 taken 161932101 times.
1063291840 for (y = y0; y < height && y < y0 + 4; y++)
1618
2/2
✓ Branch 0 taken 273561064 times.
✓ Branch 1 taken 576217761 times.
849778825 if ((t1->flags[(y + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
1619 int flags_mask = (vert_causal_ctx_csty_symbol && y == y0 + 3) ?
1620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273561064 times.
273561064 ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S) : -1;
1621 273561064 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask);
1622 547122128 int r = ff_mqc_decode(&t1->mqc,
1623 273561064 t1->mqc.cx_states + ctxno)
1624
2/2
✓ Branch 0 taken 102221148 times.
✓ Branch 1 taken 171339916 times.
273561064 ? phalf : nhalf;
1625
2/2
✓ Branch 0 taken 135875687 times.
✓ Branch 1 taken 137685377 times.
273561064 t1->data[(y) * t1->stride + x] += t1->data[(y) * t1->stride + x] < 0 ? -r : r;
1626 273561064 t1->flags[(y + 1) * t1->stride + x + 1] |= JPEG2000_T1_REF;
1627 }
1628 3580941 }
1629
1630 4295453 static void decode_clnpass(const Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
1631 int width, int height, int bpno, int bandno,
1632 int seg_symbols, int vert_causal_ctx_csty_symbol)
1633 {
1634 4295453 int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
1635
1636
2/2
✓ Branch 0 taken 15396453 times.
✓ Branch 1 taken 4295453 times.
19691906 for (y0 = 0; y0 < height; y0 += 4) {
1637
2/2
✓ Branch 0 taken 256819347 times.
✓ Branch 1 taken 15396453 times.
272215800 for (x = 0; x < width; x++) {
1638 256819347 int flags_mask = -1;
1639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256819347 times.
256819347 if (vert_causal_ctx_csty_symbol)
1640 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1641
2/2
✓ Branch 0 taken 254843641 times.
✓ Branch 1 taken 1975706 times.
256819347 if (y0 + 3 < height &&
1642
2/2
✓ Branch 0 taken 115297245 times.
✓ Branch 1 taken 139546396 times.
254843641 !((t1->flags[(y0 + 1) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1643
2/2
✓ Branch 0 taken 106962695 times.
✓ Branch 1 taken 8334550 times.
115297245 (t1->flags[(y0 + 2) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1644
2/2
✓ Branch 0 taken 99726636 times.
✓ Branch 1 taken 7236059 times.
106962695 (t1->flags[(y0 + 3) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
1645
2/2
✓ Branch 0 taken 96289243 times.
✓ Branch 1 taken 3437393 times.
99726636 (t1->flags[(y0 + 4) * t1->stride + x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG) & flags_mask))) {
1646
2/2
✓ Branch 1 taken 90019668 times.
✓ Branch 2 taken 6269575 times.
96289243 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
1647 90019668 continue;
1648 6269575 runlen = ff_mqc_decode(&t1->mqc,
1649 6269575 t1->mqc.cx_states + MQC_CX_UNI);
1650 12539150 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
1651 6269575 t1->mqc.cx_states +
1652 MQC_CX_UNI);
1653 6269575 dec = 1;
1654 } else {
1655 160530104 runlen = 0;
1656 160530104 dec = 0;
1657 }
1658
1659
4/4
✓ Branch 0 taken 654281302 times.
✓ Branch 1 taken 164823973 times.
✓ Branch 2 taken 652305596 times.
✓ Branch 3 taken 1975706 times.
819105275 for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
1660 652305596 int flags_mask = -1;
1661
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 652305596 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
652305596 if (vert_causal_ctx_csty_symbol && y == y0 + 3)
1662 flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE | JPEG2000_T1_SGN_S);
1663
2/2
✓ Branch 0 taken 646036021 times.
✓ Branch 1 taken 6269575 times.
652305596 if (!dec) {
1664
2/2
✓ Branch 0 taken 152280614 times.
✓ Branch 1 taken 493755407 times.
646036021 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
1665 152280614 dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1] & flags_mask,
1666 bandno));
1667 }
1668 }
1669
2/2
✓ Branch 0 taken 23858387 times.
✓ Branch 1 taken 628447209 times.
652305596 if (dec) {
1670 int xorbit;
1671 23858387 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y + 1) * t1->stride + x + 1] & flags_mask,
1672 &xorbit);
1673 47716774 t1->data[(y) * t1->stride + x] = (ff_mqc_decode(&t1->mqc,
1674 23858387 t1->mqc.cx_states + ctxno) ^
1675 xorbit)
1676
2/2
✓ Branch 0 taken 11716974 times.
✓ Branch 1 taken 12141413 times.
23858387 ? -mask : mask;
1677 23858387 ff_jpeg2000_set_significance(t1, x, y, t1->data[(y) * t1->stride + x] < 0);
1678 }
1679 652305596 dec = 0;
1680 652305596 t1->flags[(y + 1) * t1->stride + x + 1] &= ~JPEG2000_T1_VIS;
1681 }
1682 }
1683 }
1684
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 4295349 times.
4295453 if (seg_symbols) {
1685 int val;
1686 104 val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1687 104 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1688 104 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1689 104 val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
1690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (val != 0xa)
1691 av_log(s->avctx, AV_LOG_ERROR,
1692 "Segmentation symbol value incorrect\n");
1693 }
1694 4295453 }
1695
1696 919717 static int decode_cblk(const Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
1697 Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
1698 int width, int height, int bandpos, uint8_t roi_shift)
1699 {
1700 919717 int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
1701 919717 int pass_cnt = 0;
1702 919717 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
1703 919717 int term_cnt = 0;
1704 int coder_type;
1705
1706
2/4
✓ Branch 0 taken 919717 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 919717 times.
919717 av_assert0(width <= 1024U && height <= 1024U);
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 919717 times.
919717 av_assert0(width*height <= 4096);
1708
1709 919717 memset(t1->data, 0, t1->stride * height * sizeof(*t1->data));
1710
1711 /* If code-block contains no compressed data: nothing to do. */
1712
2/2
✓ Branch 0 taken 108457 times.
✓ Branch 1 taken 811260 times.
919717 if (!cblk->length)
1713 108457 return 0;
1714
1715 811260 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
1716
1717 811260 cblk->data[cblk->length] = 0xff;
1718 811260 cblk->data[cblk->length+1] = 0xff;
1719 811260 ff_mqc_initdec(&t1->mqc, cblk->data, 0, 1);
1720
1721
2/2
✓ Branch 0 taken 11754518 times.
✓ Branch 1 taken 811260 times.
12565778 while (passno--) {
1722
2/4
✓ Branch 0 taken 11754518 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11754518 times.
11754518 if (bpno < 0 || bpno > 29) {
1723 av_log(s->avctx, AV_LOG_ERROR, "bpno became invalid\n");
1724 return AVERROR_INVALIDDATA;
1725 }
1726
3/4
✓ Branch 0 taken 3878124 times.
✓ Branch 1 taken 3580941 times.
✓ Branch 2 taken 4295453 times.
✗ Branch 3 not taken.
11754518 switch(pass_t) {
1727 3878124 case 0:
1728 3878124 decode_sigpass(t1, width, height, bpno + 1, bandpos,
1729 vert_causal_ctx_csty_symbol);
1730 3878124 break;
1731 3580941 case 1:
1732 3580941 decode_refpass(t1, width, height, bpno + 1, vert_causal_ctx_csty_symbol);
1733 3580941 break;
1734 4295453 case 2:
1735 av_assert2(!t1->mqc.raw);
1736 4295453 decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1737 4295453 codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1738 vert_causal_ctx_csty_symbol);
1739 4295453 break;
1740 }
1741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11754518 times.
11754518 if (codsty->cblk_style & JPEG2000_CBLK_RESET) // XXX no testcase for just this
1742 ff_mqc_init_contexts(&t1->mqc);
1743
1744
4/4
✓ Branch 0 taken 10943258 times.
✓ Branch 1 taken 811260 times.
✓ Branch 3 taken 3861 times.
✓ Branch 4 taken 10939397 times.
11754518 if (passno && (coder_type = needs_termination(codsty->cblk_style, pass_cnt))) {
1745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3861 times.
3861 if (term_cnt >= cblk->nb_terminations) {
1746 av_log(s->avctx, AV_LOG_ERROR, "Missing needed termination \n");
1747 return AVERROR_INVALIDDATA;
1748 }
1749
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) {
1750 av_log(s->avctx, AV_LOG_WARNING, "Mid mismatch %"PTRDIFF_SPECIFIER" in pass %d of %d\n",
1751 cblk->data + cblk->data_start[term_cnt + 1] - 2 - t1->mqc.bp,
1752 pass_cnt, cblk->npasses);
1753 }
1754
1755 3861 ff_mqc_initdec(&t1->mqc, cblk->data + cblk->data_start[++term_cnt], coder_type == 2, 0);
1756 }
1757
1758 11754518 pass_t++;
1759
2/2
✓ Branch 0 taken 4295453 times.
✓ Branch 1 taken 7459065 times.
11754518 if (pass_t == 3) {
1760 4295453 bpno--;
1761 4295453 pass_t = 0;
1762 }
1763 11754518 pass_cnt ++;
1764 }
1765
1766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 811260 times.
811260 if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
1767 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
1768 cblk->data + cblk->length - 2 - t1->mqc.bp);
1769 }
1770
1771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 811260 times.
811260 if (cblk->data + cblk->length < t1->mqc.bp) {
1772 av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker Read.\n");
1773 }
1774
1775 811260 return 1;
1776 }
1777
1778 32768 static inline int roi_shift_param(Jpeg2000Component *comp,
1779 int quan_parameter)
1780 {
1781 uint8_t roi_shift;
1782 int val;
1783 32768 roi_shift = comp->roi_shift;
1784 32768 val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
1785
1786
2/2
✓ Branch 0 taken 2304 times.
✓ Branch 1 taken 30464 times.
32768 if (val > (1 << roi_shift))
1787
2/2
✓ Branch 0 taken 1110 times.
✓ Branch 1 taken 1194 times.
2304 return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
1788 30464 return quan_parameter;
1789 }
1790
1791 /* TODO: Verify dequantization for lossless case
1792 * comp->data can be float or int
1793 * band->stepsize can be float or int
1794 * depending on the type of DWT transformation.
1795 * see ISO/IEC 15444-1:2002 A.6.1 */
1796
1797 /* Float dequantization of a codeblock.*/
1798 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1799 Jpeg2000Component *comp,
1800 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1801 {
1802 int i, j;
1803 int w = cblk->coord[0][1] - cblk->coord[0][0];
1804 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1805 float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1806 int *src = t1->data + j*t1->stride;
1807 for (i = 0; i < w; ++i)
1808 datap[i] = src[i] * band->f_stepsize;
1809 }
1810 }
1811
1812 /* Integer dequantization of a codeblock.*/
1813 606247 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1814 Jpeg2000Component *comp,
1815 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1816 {
1817 int i, j;
1818 606247 int w = cblk->coord[0][1] - cblk->coord[0][0];
1819
2/2
✓ Branch 0 taken 8881317 times.
✓ Branch 1 taken 606247 times.
9487564 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1820 8881317 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1821 8881317 int *src = t1->data + j*t1->stride;
1822
1/2
✓ Branch 0 taken 8881317 times.
✗ Branch 1 not taken.
8881317 if (band->i_stepsize == 32768) {
1823
2/2
✓ Branch 0 taken 145644039 times.
✓ Branch 1 taken 8881317 times.
154525356 for (i = 0; i < w; ++i)
1824 145644039 datap[i] = src[i] / 2;
1825 } else {
1826 // This should be VERY uncommon
1827 for (i = 0; i < w; ++i)
1828 datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
1829 }
1830 }
1831 606247 }
1832
1833 205023 static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
1834 Jpeg2000Component *comp,
1835 Jpeg2000T1Context *t1, Jpeg2000Band *band)
1836 {
1837 int i, j;
1838 205023 int w = cblk->coord[0][1] - cblk->coord[0][0];
1839
2/2
✓ Branch 0 taken 3079523 times.
✓ Branch 1 taken 205023 times.
3284546 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1840 3079523 int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1841 3079523 int *src = t1->data + j*t1->stride;
1842
2/2
✓ Branch 0 taken 51820361 times.
✓ Branch 1 taken 3079523 times.
54899884 for (i = 0; i < w; ++i)
1843 51820361 datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
1844 }
1845 205023 }
1846
1847 4 static inline void mct_decode(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1848 {
1849 4 int i, csize = 1;
1850 void *src[3];
1851
1852
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 1; i < 3; i++) {
1853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (tile->codsty[0].transform != tile->codsty[i].transform) {
1854 av_log(s->avctx, AV_LOG_ERROR, "Transforms mismatch, MCT not supported\n");
1855 return;
1856 }
1857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (memcmp(tile->comp[0].coord, tile->comp[i].coord, sizeof(tile->comp[0].coord))) {
1858 av_log(s->avctx, AV_LOG_ERROR, "Coords mismatch, MCT not supported\n");
1859 return;
1860 }
1861 }
1862
1863
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (i = 0; i < 3; i++)
1864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (tile->codsty[0].transform == FF_DWT97)
1865 src[i] = tile->comp[i].f_data;
1866 else
1867 12 src[i] = tile->comp[i].i_data;
1868
1869
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < 2; i++)
1870 8 csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1871
1872 4 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1873 }
1874
1875 8 static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
1876 Jpeg2000Component *comp,
1877 Jpeg2000T1Context *t1)
1878 {
1879 int i, j;
1880 8 int w = cblk->coord[0][1] - cblk->coord[0][0];
1881
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 8 times.
520 for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1882 512 int *src = t1->data + j*t1->stride;
1883
2/2
✓ Branch 0 taken 32768 times.
✓ Branch 1 taken 512 times.
33280 for (i = 0; i < w; ++i)
1884 32768 src[i] = roi_shift_param(comp, src[i]);
1885 }
1886 8 }
1887
1888 2877 static inline void tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1889 {
1890 Jpeg2000T1Context t1;
1891
1892 int compno, reslevelno, bandno;
1893
1894 /* Loop on tile components */
1895
2/2
✓ Branch 0 taken 9252 times.
✓ Branch 1 taken 2877 times.
12129 for (compno = 0; compno < s->ncomponents; compno++) {
1896 9252 Jpeg2000Component *comp = tile->comp + compno;
1897 9252 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1898 9252 Jpeg2000QuantStyle *quantsty = tile->qntsty + compno;
1899
1900 9252 int coded = 0;
1901 9252 int subbandno = 0;
1902
1903 9252 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1904
1905 /* Loop on resolution levels */
1906
2/2
✓ Branch 0 taken 62386 times.
✓ Branch 1 taken 9252 times.
71638 for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1907 62386 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1908 /* Loop on bands */
1909
2/2
✓ Branch 0 taken 168654 times.
✓ Branch 1 taken 62386 times.
231040 for (bandno = 0; bandno < rlevel->nbands; bandno++, subbandno++) {
1910 int nb_precincts, precno;
1911 168654 Jpeg2000Band *band = rlevel->band + bandno;
1912 168654 int cblkno = 0, bandpos;
1913
1914 168654 bandpos = bandno + (reslevelno > 0);
1915
1916
2/2
✓ Branch 0 taken 168652 times.
✓ Branch 1 taken 2 times.
168654 if (band->coord[0][0] == band->coord[0][1] ||
1917
2/2
✓ Branch 0 taken 7800 times.
✓ Branch 1 taken 160852 times.
168652 band->coord[1][0] == band->coord[1][1])
1918 7802 continue;
1919
1920 160852 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1921 /* Loop on precincts */
1922
2/2
✓ Branch 0 taken 162031 times.
✓ Branch 1 taken 160852 times.
322883 for (precno = 0; precno < nb_precincts; precno++) {
1923 162031 Jpeg2000Prec *prec = band->prec + precno;
1924
1925 /* Loop on codeblocks */
1926 162031 for (cblkno = 0;
1927
2/2
✓ Branch 0 taken 919727 times.
✓ Branch 1 taken 162031 times.
1081758 cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1928 919727 cblkno++) {
1929 int x, y, ret;
1930 /* See Rec. ITU-T T.800, Equation E-2 */
1931 919727 int magp = quantsty->expn[subbandno] + quantsty->nguardbits - 1;
1932
1933 919727 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1934
1935
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 919717 times.
919727 if (codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F)
1936 10 ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
1937 10 cblk->coord[0][1] - cblk->coord[0][0],
1938 10 cblk->coord[1][1] - cblk->coord[1][0],
1939 10 magp, comp->roi_shift);
1940 else
1941 919717 ret = decode_cblk(s, codsty, &t1, cblk,
1942 919717 cblk->coord[0][1] - cblk->coord[0][0],
1943 919717 cblk->coord[1][1] - cblk->coord[1][0],
1944 919717 bandpos, comp->roi_shift);
1945
1946
2/2
✓ Branch 0 taken 811270 times.
✓ Branch 1 taken 108457 times.
919727 if (ret)
1947 811270 coded = 1;
1948 else
1949 108457 continue;
1950 811270 x = cblk->coord[0][0] - band->coord[0][0];
1951 811270 y = cblk->coord[1][0] - band->coord[1][0];
1952
1953
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 811262 times.
811270 if (comp->roi_shift)
1954 8 roi_scale_cblk(cblk, comp, &t1);
1955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 811270 times.
811270 if (codsty->transform == FF_DWT97)
1956 dequantization_float(x, y, cblk, comp, &t1, band);
1957
2/2
✓ Branch 0 taken 205023 times.
✓ Branch 1 taken 606247 times.
811270 else if (codsty->transform == FF_DWT97_INT)
1958 205023 dequantization_int_97(x, y, cblk, comp, &t1, band);
1959 else
1960 606247 dequantization_int(x, y, cblk, comp, &t1, band);
1961 } /* end cblk */
1962 } /*end prec */
1963 } /* end band */
1964 } /* end reslevel */
1965
1966 /* inverse DWT */
1967
1/2
✓ Branch 0 taken 9252 times.
✗ Branch 1 not taken.
9252 if (coded)
1968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9252 times.
9252 ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1969
1970 } /*end comp */
1971 2877 }
1972
1973 #define WRITE_FRAME(D, PIXEL) \
1974 static inline void write_frame_ ## D(const Jpeg2000DecoderContext * s, Jpeg2000Tile * tile, \
1975 AVFrame * picture, int precision) \
1976 { \
1977 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt); \
1978 int planar = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR); \
1979 int pixelsize = planar ? 1 : pixdesc->nb_components; \
1980 \
1981 int compno; \
1982 int x, y; \
1983 \
1984 for (compno = 0; compno < s->ncomponents; compno++) { \
1985 Jpeg2000Component *comp = tile->comp + compno; \
1986 Jpeg2000CodingStyle *codsty = tile->codsty + compno; \
1987 PIXEL *line; \
1988 float *datap = comp->f_data; \
1989 int32_t *i_datap = comp->i_data; \
1990 int cbps = s->cbps[compno]; \
1991 int w = tile->comp[compno].coord[0][1] - \
1992 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
1993 int h = tile->comp[compno].coord[1][1] - \
1994 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
1995 int plane = 0; \
1996 \
1997 if (planar) \
1998 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1); \
1999 \
2000 y = tile->comp[compno].coord[1][0] - \
2001 ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); \
2002 line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
2003 for (; y < h; y++) { \
2004 PIXEL *dst; \
2005 \
2006 x = tile->comp[compno].coord[0][0] - \
2007 ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]); \
2008 dst = line + x * pixelsize + compno*!planar; \
2009 \
2010 if (codsty->transform == FF_DWT97) { \
2011 for (; x < w; x++) { \
2012 int val = lrintf(*datap) + (1 << (cbps - 1)); \
2013 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2014 val = av_clip(val, 0, (1 << cbps) - 1); \
2015 *dst = val << (precision - cbps); \
2016 datap++; \
2017 dst += pixelsize; \
2018 } \
2019 } else { \
2020 for (; x < w; x++) { \
2021 int val = *i_datap + (1 << (cbps - 1)); \
2022 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */ \
2023 val = av_clip(val, 0, (1 << cbps) - 1); \
2024 *dst = val << (precision - cbps); \
2025 i_datap++; \
2026 dst += pixelsize; \
2027 } \
2028 } \
2029 line += picture->linesize[plane] / sizeof(PIXEL); \
2030 } \
2031 } \
2032 \
2033 }
2034
2035
13/16
✓ Branch 1 taken 1317 times.
✓ Branch 2 taken 1 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 3921 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 534836 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 95324503 times.
✓ Branch 16 taken 534836 times.
✓ Branch 17 taken 534836 times.
✓ Branch 18 taken 3925 times.
✓ Branch 19 taken 3925 times.
✓ Branch 20 taken 1318 times.
95864582 WRITE_FRAME(8, uint8_t)
2036
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)
2037
2038 #undef WRITE_FRAME
2039
2040 2877 static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
2041 int jobnr, int threadnr)
2042 {
2043 2877 const Jpeg2000DecoderContext *s = avctx->priv_data;
2044 2877 AVFrame *picture = td;
2045 2877 Jpeg2000Tile *tile = s->tile + jobnr;
2046
2047 2877 tile_codeblocks(s, tile);
2048
2049 /* inverse MCT transformation */
2050
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2873 times.
2877 if (tile->codsty[0].mct)
2051 4 mct_decode(s, tile);
2052
2053
2/2
✓ Branch 0 taken 1318 times.
✓ Branch 1 taken 1559 times.
2877 if (s->precision <= 8) {
2054 1318 write_frame_8(s, tile, picture, 8);
2055 } else {
2056 4675 int precision = picture->format == AV_PIX_FMT_XYZ12 ||
2057
2/2
✓ Branch 0 taken 650 times.
✓ Branch 1 taken 907 times.
1557 picture->format == AV_PIX_FMT_RGB48 ||
2058
1/2
✓ Branch 0 taken 650 times.
✗ Branch 1 not taken.
650 picture->format == AV_PIX_FMT_RGBA64 ||
2059
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;
2060
2061 1559 write_frame_16(s, tile, picture, precision);
2062 }
2063
2064 2877 return 0;
2065 }
2066
2067 849 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
2068 {
2069 int tileno, compno;
2070
2/2
✓ Branch 0 taken 3208 times.
✓ Branch 1 taken 849 times.
4057 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2071
1/2
✓ Branch 0 taken 3208 times.
✗ Branch 1 not taken.
3208 if (s->tile[tileno].comp) {
2072
2/2
✓ Branch 0 taken 10231 times.
✓ Branch 1 taken 3208 times.
13439 for (compno = 0; compno < s->ncomponents; compno++) {
2073 10231 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
2074 10231 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
2075
2076 10231 ff_jpeg2000_cleanup(comp, codsty);
2077 }
2078 3208 av_freep(&s->tile[tileno].comp);
2079 3208 av_freep(&s->tile[tileno].packed_headers);
2080 3208 s->tile[tileno].packed_headers_size = 0;
2081 }
2082 }
2083 849 av_freep(&s->packed_headers);
2084 849 s->packed_headers_size = 0;
2085 849 memset(&s->packed_headers_stream, 0, sizeof(s->packed_headers_stream));
2086 849 av_freep(&s->tile);
2087 849 memset(s->codsty, 0, sizeof(s->codsty));
2088 849 memset(s->qntsty, 0, sizeof(s->qntsty));
2089 849 memset(s->properties, 0, sizeof(s->properties));
2090 849 memset(&s->poc , 0, sizeof(s->poc));
2091 849 s->numXtiles = s->numYtiles = 0;
2092 849 s->ncomponents = 0;
2093 849 }
2094
2095 849 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
2096 {
2097 849 Jpeg2000CodingStyle *codsty = s->codsty;
2098 849 Jpeg2000QuantStyle *qntsty = s->qntsty;
2099 849 Jpeg2000POC *poc = &s->poc;
2100 849 uint8_t *properties = s->properties;
2101
2102 9629 for (;;) {
2103 10478 int len, ret = 0;
2104 uint16_t marker;
2105 int oldpos;
2106
2107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10478 times.
10478 if (bytestream2_get_bytes_left(&s->g) < 2) {
2108 av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
2109 break;
2110 }
2111
2112 10478 marker = bytestream2_get_be16u(&s->g);
2113 10478 oldpos = bytestream2_tell(&s->g);
2114
3/4
✓ Branch 0 taken 10478 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 10476 times.
10478 if (marker >= 0xFF30 && marker <= 0xFF3F)
2115 2 continue;
2116
2/2
✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 7252 times.
10476 if (marker == JPEG2000_SOD) {
2117 Jpeg2000Tile *tile;
2118 Jpeg2000TilePart *tp;
2119
2120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
3224 if (!s->tile) {
2121 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
2122 return AVERROR_INVALIDDATA;
2123 }
2124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
3224 if (s->curtileno < 0) {
2125 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
2126 return AVERROR_INVALIDDATA;
2127 }
2128
2129 3224 tile = s->tile + s->curtileno;
2130 3224 tp = tile->tile_part + tile->tp_idx;
2131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
3224 if (tp->tp_end < s->g.buffer) {
2132 av_log(s->avctx, AV_LOG_ERROR, "Invalid tpend\n");
2133 return AVERROR_INVALIDDATA;
2134 }
2135
2136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
3224 if (s->has_ppm) {
2137 uint32_t tp_header_size = bytestream2_get_be32(&s->packed_headers_stream);
2138 if (bytestream2_get_bytes_left(&s->packed_headers_stream) < tp_header_size)
2139 return AVERROR_INVALIDDATA;
2140 bytestream2_init(&tp->header_tpg, s->packed_headers_stream.buffer, tp_header_size);
2141 bytestream2_skip(&s->packed_headers_stream, tp_header_size);
2142 }
2143
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3224 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3224 if (tile->has_ppt && tile->tp_idx == 0) {
2144 bytestream2_init(&tile->packed_headers_stream, tile->packed_headers, tile->packed_headers_size);
2145 }
2146
2147 3224 bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
2148 3224 bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
2149
2150 3224 continue;
2151 }
2152
2/2
✓ Branch 0 taken 849 times.
✓ Branch 1 taken 6403 times.
7252 if (marker == JPEG2000_EOC)
2153 849 break;
2154
2155 6403 len = bytestream2_get_be16(&s->g);
2156
2/4
✓ Branch 0 taken 6403 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6403 times.
6403 if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2) {
2157 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
2158 av_log(s->avctx, AV_LOG_ERROR, "Invalid len %d left=%d\n", len, bytestream2_get_bytes_left(&s->g));
2159 return AVERROR_INVALIDDATA;
2160 }
2161 av_log(s->avctx, AV_LOG_WARNING, "Missing EOC Marker.\n");
2162 break;
2163 }
2164
2165
13/15
✓ Branch 0 taken 849 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 849 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 25 times.
✓ Branch 5 taken 849 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 3224 times.
✓ Branch 8 taken 44 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 11 times.
✓ Branch 11 taken 514 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 4 times.
6403 switch (marker) {
2166 849 case JPEG2000_SIZ:
2167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 if (s->ncomponents) {
2168 av_log(s->avctx, AV_LOG_ERROR, "Duplicate SIZ\n");
2169 return AVERROR_INVALIDDATA;
2170 }
2171 849 ret = get_siz(s);
2172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
849 if (!s->tile)
2173 s->numXtiles = s->numYtiles = 0;
2174 849 break;
2175 18 case JPEG2000_COC:
2176 18 ret = get_coc(s, codsty, properties);
2177 18 break;
2178 849 case JPEG2000_COD:
2179 849 ret = get_cod(s, codsty, properties);
2180 849 break;
2181 4 case JPEG2000_RGN:
2182 4 ret = get_rgn(s, len);
2183 4 break;
2184 25 case JPEG2000_QCC:
2185 25 ret = get_qcc(s, len, qntsty, properties);
2186 25 break;
2187 849 case JPEG2000_QCD:
2188 849 ret = get_qcd(s, len, qntsty, properties);
2189 849 break;
2190 8 case JPEG2000_POC:
2191 8 ret = get_poc(s, len, poc);
2192 8 break;
2193 3224 case JPEG2000_SOT:
2194
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 3160 times.
3224 if (!s->in_tile_headers) {
2195 64 s->in_tile_headers = 1;
2196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (s->has_ppm) {
2197 bytestream2_init(&s->packed_headers_stream, s->packed_headers, s->packed_headers_size);
2198 }
2199 }
2200
1/2
✓ Branch 1 taken 3224 times.
✗ Branch 2 not taken.
3224 if (!(ret = get_sot(s, len))) {
2201 av_assert1(s->curtileno >= 0);
2202 3224 codsty = s->tile[s->curtileno].codsty;
2203 3224 qntsty = s->tile[s->curtileno].qntsty;
2204 3224 poc = &s->tile[s->curtileno].poc;
2205 3224 properties = s->tile[s->curtileno].properties;
2206 }
2207 3224 break;
2208 44 case JPEG2000_PLM:
2209 // the PLM marker is ignored
2210 case JPEG2000_COM:
2211 // the comment is ignored
2212 44 bytestream2_skip(&s->g, len - 2);
2213 44 break;
2214 4 case JPEG2000_CRG:
2215 4 ret = read_crg(s, len);
2216 4 break;
2217 11 case JPEG2000_TLM:
2218 // Tile-part lengths
2219 11 ret = get_tlm(s, len);
2220 11 break;
2221 514 case JPEG2000_PLT:
2222 // Packet length, tile-part header
2223 514 ret = get_plt(s, len);
2224 514 break;
2225 case JPEG2000_PPM:
2226 // Packed headers, main header
2227 if (s->in_tile_headers) {
2228 av_log(s->avctx, AV_LOG_ERROR, "PPM Marker can only be in Main header\n");
2229 return AVERROR_INVALIDDATA;
2230 }
2231 ret = get_ppm(s, len);
2232 break;
2233 case JPEG2000_PPT:
2234 // Packed headers, tile-part header
2235 if (s->has_ppm) {
2236 av_log(s->avctx, AV_LOG_ERROR,
2237 "Cannot have both PPT and PPM marker.\n");
2238 return AVERROR_INVALIDDATA;
2239 }
2240
2241 ret = get_ppt(s, len);
2242 break;
2243 4 default:
2244 4 av_log(s->avctx, AV_LOG_ERROR,
2245 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
2246 4 marker, bytestream2_tell(&s->g) - 4);
2247 4 bytestream2_skip(&s->g, len - 2);
2248 4 break;
2249 }
2250
2/4
✓ Branch 1 taken 6403 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6403 times.
6403 if (bytestream2_tell(&s->g) - oldpos != len || ret) {
2251 av_log(s->avctx, AV_LOG_ERROR,
2252 "error during processing marker segment %.4"PRIx16"\n",
2253 marker);
2254 return ret ? ret : -1;
2255 }
2256 }
2257 849 return 0;
2258 }
2259
2260 /* Read bit stream packets --> T2 operation. */
2261 816 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
2262 {
2263 816 int ret = 0;
2264 int tileno;
2265
2266
2/2
✓ Branch 0 taken 2877 times.
✓ Branch 1 taken 816 times.
3693 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
2267 2877 Jpeg2000Tile *tile = s->tile + tileno;
2268
2269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2877 times.
2877 if ((ret = init_tile(s, tileno)) < 0)
2270 return ret;
2271
2272
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2877 times.
2877 if ((ret = jpeg2000_decode_packets(s, tile)) < 0)
2273 return ret;
2274 }
2275
2276 816 return 0;
2277 }
2278
2279 817 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
2280 {
2281 uint32_t atom_size, atom, atom_end;
2282 817 int search_range = 10;
2283
2284 817 while (search_range
2285
2/4
✓ Branch 0 taken 2452 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2452 times.
✗ Branch 3 not taken.
4904 &&
2286 2452 bytestream2_get_bytes_left(&s->g) >= 8) {
2287 2452 atom_size = bytestream2_get_be32u(&s->g);
2288 2452 atom = bytestream2_get_be32u(&s->g);
2289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2452 times.
2452 if (atom_size == 1) {
2290 if (bytestream2_get_be32u(&s->g)) {
2291 avpriv_request_sample(s->avctx, "Huge atom");
2292 return 0;
2293 }
2294 atom_size = bytestream2_get_be32u(&s->g);
2295 if (atom_size < 16 || (int64_t)bytestream2_tell(&s->g) + atom_size - 16 > INT_MAX)
2296 return AVERROR_INVALIDDATA;
2297 atom_end = bytestream2_tell(&s->g) + atom_size - 16;
2298 } else {
2299
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)
2300 1 return AVERROR_INVALIDDATA;
2301 2451 atom_end = bytestream2_tell(&s->g) + atom_size - 8;
2302 }
2303
2304
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 1635 times.
2451 if (atom == JP2_CODESTREAM)
2305 816 return 1;
2306
2307
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)
2308 return 0;
2309
2310
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 &&
2311 817 atom_size >= 16) {
2312 uint32_t atom2_size, atom2, atom2_end;
2313 do {
2314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1634 times.
1634 if (bytestream2_get_bytes_left(&s->g) < 8)
2315 break;
2316 1634 atom2_size = bytestream2_get_be32u(&s->g);
2317 1634 atom2 = bytestream2_get_be32u(&s->g);
2318 1634 atom2_end = bytestream2_tell(&s->g) + atom2_size - 8;
2319
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)
2320 break;
2321 1634 atom2_size -= 8;
2322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1634 times.
1634 if (atom2 == JP2_CODESTREAM) {
2323 return 1;
2324
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) {
2325 817 int method = bytestream2_get_byteu(&s->g);
2326 817 bytestream2_skipu(&s->g, 2);
2327
1/2
✓ Branch 0 taken 817 times.
✗ Branch 1 not taken.
817 if (method == 1) {
2328 817 s->colour_space = bytestream2_get_be32u(&s->g);
2329 }
2330
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) {
2331 int i, size, colour_count, colour_channels, colour_depth[3];
2332 colour_count = bytestream2_get_be16u(&s->g);
2333 colour_channels = bytestream2_get_byteu(&s->g);
2334 // FIXME: Do not ignore channel_sign
2335 colour_depth[0] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2336 colour_depth[1] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2337 colour_depth[2] = (bytestream2_get_byteu(&s->g) & 0x7f) + 1;
2338 size = (colour_depth[0] + 7 >> 3) * colour_count +
2339 (colour_depth[1] + 7 >> 3) * colour_count +
2340 (colour_depth[2] + 7 >> 3) * colour_count;
2341 if (colour_count > AVPALETTE_COUNT ||
2342 colour_channels != 3 ||
2343 colour_depth[0] > 16 ||
2344 colour_depth[1] > 16 ||
2345 colour_depth[2] > 16 ||
2346 atom2_size < size) {
2347 avpriv_request_sample(s->avctx, "Unknown palette");
2348 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2349 continue;
2350 }
2351 s->pal8 = 1;
2352 for (i = 0; i < colour_count; i++) {
2353 uint32_t r, g, b;
2354 if (colour_depth[0] <= 8) {
2355 r = bytestream2_get_byteu(&s->g) << 8 - colour_depth[0];
2356 r |= r >> colour_depth[0];
2357 } else {
2358 r = bytestream2_get_be16u(&s->g) >> colour_depth[0] - 8;
2359 }
2360 if (colour_depth[1] <= 8) {
2361 g = bytestream2_get_byteu(&s->g) << 8 - colour_depth[1];
2362 g |= g >> colour_depth[1];
2363 } else {
2364 g = bytestream2_get_be16u(&s->g) >> colour_depth[1] - 8;
2365 }
2366 if (colour_depth[2] <= 8) {
2367 b = bytestream2_get_byteu(&s->g) << 8 - colour_depth[2];
2368 b |= b >> colour_depth[2];
2369 } else {
2370 b = bytestream2_get_be16u(&s->g) >> colour_depth[2] - 8;
2371 }
2372 s->palette[i] = 0xffu << 24 | r << 16 | g << 8 | b;
2373 }
2374
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) {
2375 int n = bytestream2_get_be16u(&s->g);
2376 for (; n>0; n--) {
2377 int cn = bytestream2_get_be16(&s->g);
2378 int av_unused typ = bytestream2_get_be16(&s->g);
2379 int asoc = bytestream2_get_be16(&s->g);
2380 if (cn < 4 && asoc < 4)
2381 s->cdef[cn] = asoc;
2382 }
2383
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) {
2384 int64_t vnum, vden, hnum, hden, vexp, hexp;
2385 uint32_t resx;
2386 bytestream2_skip(&s->g, 4);
2387 resx = bytestream2_get_be32u(&s->g);
2388 if (resx != MKBETAG('r','e','s','c') && resx != MKBETAG('r','e','s','d')) {
2389 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2390 continue;
2391 }
2392 vnum = bytestream2_get_be16u(&s->g);
2393 vden = bytestream2_get_be16u(&s->g);
2394 hnum = bytestream2_get_be16u(&s->g);
2395 hden = bytestream2_get_be16u(&s->g);
2396 vexp = bytestream2_get_byteu(&s->g);
2397 hexp = bytestream2_get_byteu(&s->g);
2398 if (!vnum || !vden || !hnum || !hden) {
2399 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2400 av_log(s->avctx, AV_LOG_WARNING, "RES box invalid\n");
2401 continue;
2402 }
2403 if (vexp > hexp) {
2404 vexp -= hexp;
2405 hexp = 0;
2406 } else {
2407 hexp -= vexp;
2408 vexp = 0;
2409 }
2410 if ( INT64_MAX / (hnum * vden) > pow(10, hexp)
2411 && INT64_MAX / (vnum * hden) > pow(10, vexp))
2412 av_reduce(&s->sar.den, &s->sar.num,
2413 hnum * vden * pow(10, hexp),
2414 vnum * hden * pow(10, vexp),
2415 INT32_MAX);
2416 }
2417 1634 bytestream2_seek(&s->g, atom2_end, SEEK_SET);
2418
2/2
✓ Branch 0 taken 817 times.
✓ Branch 1 taken 817 times.
1634 } while (atom_end - atom2_end >= 8);
2419 } else {
2420 818 search_range--;
2421 }
2422 1635 bytestream2_seek(&s->g, atom_end, SEEK_SET);
2423 }
2424
2425 return 0;
2426 }
2427
2428 65 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
2429 {
2430 65 Jpeg2000DecoderContext *s = avctx->priv_data;
2431
2432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (avctx->lowres)
2433 av_log(avctx, AV_LOG_WARNING, "lowres is overriden by reduction_factor but set anyway\n");
2434
3/4
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
65 if (!s->reduction_factor && avctx->lowres < JPEG2000_MAX_RESLEVELS) {
2435 64 s->reduction_factor = avctx->lowres;
2436 }
2437
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
65 if (avctx->lowres != s->reduction_factor && avctx->lowres)
2438 return AVERROR(EINVAL);
2439
2440 65 ff_jpeg2000dsp_init(&s->dsp);
2441 65 ff_jpeg2000_init_tier1_luts();
2442
2443 65 return 0;
2444 }
2445
2446 849 static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
2447 int *got_frame, AVPacket *avpkt)
2448 {
2449 849 Jpeg2000DecoderContext *s = avctx->priv_data;
2450 int ret;
2451
2452 849 s->avctx = avctx;
2453 849 bytestream2_init(&s->g, avpkt->data, avpkt->size);
2454 849 s->curtileno = -1;
2455 849 memset(s->cdef, -1, sizeof(s->cdef));
2456
2457
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if (bytestream2_get_bytes_left(&s->g) < 2) {
2458 ret = AVERROR_INVALIDDATA;
2459 goto end;
2460 }
2461
2462 // check if the image is in jp2 format
2463
3/4
✓ Branch 1 taken 849 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 817 times.
✓ Branch 4 taken 32 times.
1698 if (bytestream2_get_bytes_left(&s->g) >= 12 &&
2464
1/2
✓ Branch 1 taken 817 times.
✗ Branch 2 not taken.
1666 (bytestream2_get_be32u(&s->g) == 12) &&
2465
1/2
✓ Branch 1 taken 817 times.
✗ Branch 2 not taken.
1634 (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
2466 817 (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
2467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 817 times.
817 if (!jp2_find_codestream(s)) {
2468 av_log(avctx, AV_LOG_ERROR,
2469 "Could not find Jpeg2000 codestream atom.\n");
2470 ret = AVERROR_INVALIDDATA;
2471 goto end;
2472 }
2473 } else {
2474 32 bytestream2_seek(&s->g, 0, SEEK_SET);
2475 }
2476
2477
2/4
✓ Branch 1 taken 849 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 849 times.
849 while (bytestream2_get_bytes_left(&s->g) >= 3 && bytestream2_peek_be16(&s->g) != JPEG2000_SOC)
2478 bytestream2_skip(&s->g, 1);
2479
2480
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
2481 av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
2482 ret = AVERROR_INVALIDDATA;
2483 goto end;
2484 }
2485
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 849 times.
849 if (ret = jpeg2000_read_main_headers(s))
2486 goto end;
2487
2488
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 849 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
849 if (s->sar.num && s->sar.den)
2489 avctx->sample_aspect_ratio = s->sar;
2490 849 s->sar.num = s->sar.den = 0;
2491
2492
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 816 times.
849 if (avctx->skip_frame >= AVDISCARD_ALL) {
2493 33 jpeg2000_dec_cleanup(s);
2494 33 return avpkt->size;
2495 }
2496
2497 /* get picture buffer */
2498
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 816 times.
816 if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
2499 goto end;
2500 816 picture->pict_type = AV_PICTURE_TYPE_I;
2501 816 picture->flags |= AV_FRAME_FLAG_KEY;
2502
2503
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 816 times.
816 if (ret = jpeg2000_read_bitstream_packets(s))
2504 goto end;
2505
2506
2/2
✓ Branch 0 taken 819 times.
✓ Branch 1 taken 1 times.
820 for (int x = 0; x < s->ncomponents; x++) {
2507
2/2
✓ Branch 0 taken 815 times.
✓ Branch 1 taken 4 times.
819 if (s->cdef[x] < 0) {
2508
2/2
✓ Branch 0 taken 2627 times.
✓ Branch 1 taken 815 times.
3442 for (x = 0; x < s->ncomponents; x++) {
2509 2627 s->cdef[x] = x + 1;
2510 }
2511
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 615 times.
815 if ((s->ncomponents & 1) == 0)
2512 200 s->cdef[s->ncomponents-1] = 0;
2513 815 break;
2514 }
2515 }
2516
2517 816 avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
2518
2519 816 jpeg2000_dec_cleanup(s);
2520
2521 816 *got_frame = 1;
2522
2523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 816 times.
816 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
2524 memcpy(picture->data[1], s->palette, 256 * sizeof(uint32_t));
2525
2526 816 return bytestream2_tell(&s->g);
2527
2528 end:
2529 jpeg2000_dec_cleanup(s);
2530 return ret;
2531 }
2532
2533 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
2534 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2535
2536 static const AVOption options[] = {
2537 { "lowres", "Lower the decoding resolution by a power of two",
2538 OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
2539 { NULL },
2540 };
2541
2542 static const AVClass jpeg2000_class = {
2543 .class_name = "jpeg2000",
2544 .item_name = av_default_item_name,
2545 .option = options,
2546 .version = LIBAVUTIL_VERSION_INT,
2547 };
2548
2549 const FFCodec ff_jpeg2000_decoder = {
2550 .p.name = "jpeg2000",
2551 CODEC_LONG_NAME("JPEG 2000"),
2552 .p.type = AVMEDIA_TYPE_VIDEO,
2553 .p.id = AV_CODEC_ID_JPEG2000,
2554 .p.capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
2555 .priv_data_size = sizeof(Jpeg2000DecoderContext),
2556 .init = jpeg2000_decode_init,
2557 FF_CODEC_DECODE_CB(jpeg2000_decode_frame),
2558 .p.priv_class = &jpeg2000_class,
2559 .p.max_lowres = 5,
2560 .p.profiles = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles),
2561 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2562 };
2563