FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000dec.c
Date: 2021-09-24 03:35:22
Exec Total Coverage
Lines: 834 1469 56.8%
Branches: 529 1115 47.4%

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