FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cfhd.c
Date: 2026-09-18 03:39:54
Exec Total Coverage
Lines: 474 1023 46.3%
Functions: 9 16 56.2%
Branches: 313 722 43.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Cineform HD video decoder
24 */
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/common.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/pixdesc.h"
32
33 #include "avcodec.h"
34 #include "bytestream.h"
35 #include "codec_internal.h"
36 #include "decode.h"
37 #include "get_bits.h"
38 #include "internal.h"
39 #include "thread.h"
40 #include "cfhd.h"
41
42 #define ALPHA_COMPAND_DC_OFFSET 256
43 #define ALPHA_COMPAND_GAIN 9400
44
45 6 static av_cold int cfhd_init(AVCodecContext *avctx)
46 {
47 6 CFHDContext *s = avctx->priv_data;
48
49 6 s->avctx = avctx;
50
51
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 6 times.
390 for (int i = 0; i < 64; i++) {
52 384 int val = i;
53
54
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 240 times.
384 if (val >= 40) {
55
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 84 times.
144 if (val >= 54) {
56 60 val -= 54;
57 60 val <<= 2;
58 60 val += 54;
59 }
60
61 144 val -= 40;
62 144 val <<= 2;
63 144 val += 40;
64 }
65
66 384 s->lut[0][i] = val;
67 }
68
69
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 6 times.
1542 for (int i = 0; i < 256; i++)
70 1536 s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
71
72 6 return ff_cfhd_init_vlcs(s);
73 }
74
75 99 static void init_plane_defaults(CFHDContext *s)
76 {
77 99 s->subband_num = 0;
78 99 s->level = 0;
79 99 s->subband_num_actual = 0;
80 99 }
81
82 33 static void init_peak_table_defaults(CFHDContext *s)
83 {
84 33 s->peak.level = 0;
85 33 s->peak.offset = 0;
86 33 memset(&s->peak.base, 0, sizeof(s->peak.base));
87 33 }
88
89 33 static void init_frame_defaults(CFHDContext *s)
90 {
91 33 s->coded_width = 0;
92 33 s->coded_height = 0;
93 33 s->coded_format = AV_PIX_FMT_YUV422P10;
94 33 s->cropped_height = 0;
95 33 s->bpc = 10;
96 33 s->channel_cnt = 3;
97 33 s->subband_cnt = SUBBAND_COUNT;
98 33 s->channel_num = 0;
99 33 s->lowpass_precision = 16;
100 33 s->quantisation = 1;
101 33 s->codebook = 0;
102 33 s->difference_coding = 0;
103 33 s->frame_type = 0;
104 33 s->sample_type = 0;
105
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (s->transform_type != 2)
106 33 s->transform_type = -1;
107 33 init_plane_defaults(s);
108 33 init_peak_table_defaults(s);
109 33 }
110
111 3522977 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
112 {
113
2/4
✓ Branch 0 taken 3522977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3522977 times.
✗ Branch 3 not taken.
3522977 if (codebook == 0 || codebook == 1) {
114
2/2
✓ Branch 0 taken 856386 times.
✓ Branch 1 taken 2666591 times.
3522977 return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
115 } else
116 return level * quantisation;
117 }
118
119 static inline void difference_coding(int16_t *band, int width, int height)
120 {
121 for (int i = 0; i < height; i++) {
122 for (int j = 1; j < width; j++) {
123 band[j] += band[j-1];
124 }
125 band += width;
126 }
127 }
128
129 static inline void peak_table(int16_t *band, Peak *peak, int length)
130 {
131 for (int i = 0; i < length; i++)
132 if (abs(band[i]) > peak->level)
133 band[i] = bytestream2_get_le16(&peak->base);
134 }
135
136 static inline void process_alpha(int16_t *alpha, int width)
137 {
138 for (int i = 0; i < width; i++) {
139 int channel = alpha[i];
140 channel -= ALPHA_COMPAND_DC_OFFSET;
141 channel <<= 3;
142 channel *= ALPHA_COMPAND_GAIN;
143 channel >>= 16;
144 channel = av_clip_uintp2(channel, 12);
145 alpha[i] = channel;
146 }
147 }
148
149 static inline void process_bayer(AVFrame *frame, int bpc)
150 {
151 const int linesize = frame->linesize[0];
152 uint16_t *r = (uint16_t *)frame->data[0];
153 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
154 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
155 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
156 const int mid = 1 << (bpc - 1);
157 const int factor = 1 << (16 - bpc);
158
159 for (int y = 0; y < frame->height >> 1; y++) {
160 for (int x = 0; x < frame->width; x += 2) {
161 int R, G1, G2, B;
162 int g, rg, bg, gd;
163
164 g = r[x];
165 rg = g1[x];
166 bg = g2[x];
167 gd = b[x];
168 gd -= mid;
169
170 R = (rg - mid) * 2 + g;
171 G1 = g + gd;
172 G2 = g - gd;
173 B = (bg - mid) * 2 + g;
174
175 R = av_clip_uintp2(R * factor, 16);
176 G1 = av_clip_uintp2(G1 * factor, 16);
177 G2 = av_clip_uintp2(G2 * factor, 16);
178 B = av_clip_uintp2(B * factor, 16);
179
180 r[x] = R;
181 g1[x] = G1;
182 g2[x] = G2;
183 b[x] = B;
184 }
185
186 r += linesize;
187 g1 += linesize;
188 g2 += linesize;
189 b += linesize;
190 }
191 }
192
193 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
194 int width, int linesize, int plane)
195 {
196 for (int i = 0; i < width; i++) {
197 int16_t even = (low[i] - high[i])/2;
198 int16_t odd = (low[i] + high[i])/2;
199 output[i] = av_clip_uintp2(even, 10);
200 output[i + linesize] = av_clip_uintp2(odd, 10);
201 }
202 }
203
204 static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
205 {
206 for (int i = 0; i < width; i++) {
207 int even = (low[i] - high[i]) / 2;
208 int odd = (low[i] + high[i]) / 2;
209
210 low[i] = even;
211 high[i] = odd;
212 }
213 }
214
215 12 static void free_buffers(CFHDContext *s)
216 {
217
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 for (size_t i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
218 48 Plane *p = &s->plane[i];
219 48 av_freep(&s->plane[i].idwt_buf);
220 48 av_freep(&s->plane[i].idwt_tmp);
221 48 s->plane[i].idwt_size = 0;
222
223
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 48 times.
864 for (int j = 0; j < SUBBAND_COUNT_3D; j++)
224 816 s->plane[i].subband[j] = NULL;
225
226
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 48 times.
528 for (int j = 0; j < 10; j++)
227 480 s->plane[i].l_h[j] = NULL;
228
229
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 48 times.
336 for (int j = 0; j < DWT_LEVELS_3D; j++)
230
2/2
✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 288 times.
1440 for (unsigned k = 0; k < FF_ARRAY_ELEMS(p->band[j]); k++) {
231 1152 p->band[j][k].a_width = 0;
232 1152 p->band[j][k].a_height = 0;
233 1152 p->band[j][k].read_ok = 0;
234 }
235 }
236 12 s->a_height = 0;
237 12 s->a_width = 0;
238 12 s->a_transform_type = INT_MIN;
239 12 }
240
241 6 static int alloc_buffers(AVCodecContext *avctx)
242 {
243 6 CFHDContext *s = avctx->priv_data;
244 6 int ret, planes, bayer = 0;
245 int chroma_x_shift, chroma_y_shift;
246
247
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
248 return ret;
249 6 avctx->pix_fmt = s->coded_format;
250
251 6 ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
252
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
254 &chroma_x_shift,
255 &chroma_y_shift)) < 0)
256 return ret;
257 6 planes = av_pix_fmt_count_planes(s->coded_format);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
259 planes = 4;
260 chroma_x_shift = 1;
261 chroma_y_shift = 1;
262 bayer = 1;
263 }
264
265
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 for (int i = 0; i < planes; i++) {
266 int w8, h8, w4, h4, w2, h2;
267
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
18 int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
268
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
18 int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
269 18 ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
270
271
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = av_image_check_size2(stride, height, avctx->max_pixels, s->coded_format, 0, avctx)) < 0)
272 return ret;
273
274
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18 if (chroma_y_shift && !bayer)
275 height = FFALIGN(height / 8, 2) * 8;
276 18 s->plane[i].width = width;
277 18 s->plane[i].height = height;
278 18 s->plane[i].stride = stride;
279
280 18 w8 = FFALIGN(s->plane[i].width / 8, 8) + 64;
281 18 h8 = FFALIGN(height, 8) / 8;
282 18 w4 = w8 * 2;
283 18 h4 = h8 * 2;
284 18 w2 = w4 * 2;
285 18 h2 = h4 * 2;
286
287
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (s->transform_type == 0) {
288 18 s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
289 18 s->plane[i].idwt_buf =
290 18 av_calloc(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
291 18 s->plane[i].idwt_tmp =
292 18 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
293 } else {
294 s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
295 s->plane[i].idwt_buf =
296 av_calloc(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
297 s->plane[i].idwt_tmp =
298 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
299 }
300
301
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
302 return AVERROR(ENOMEM);
303
304 18 s->plane[i].subband[0] = s->plane[i].idwt_buf;
305 18 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
306 18 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
307 18 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
308 18 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
309 18 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
310 18 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
311
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (s->transform_type == 0) {
312 18 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
313 18 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
314 18 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
315 } else {
316 int16_t *frame2 =
317 s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
318 s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
319 s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
320 s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
321 s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
322 s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
323 s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
324 s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
325 s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
326 s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
327 }
328
329
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (s->transform_type == 0) {
330
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 18 times.
72 for (int j = 0; j < DWT_LEVELS; j++) {
331
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 54 times.
270 for (unsigned k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
332 216 s->plane[i].band[j][k].a_width = w8 << j;
333 216 s->plane[i].band[j][k].a_height = h8 << j;
334 }
335 }
336 } else {
337 for (int j = 0; j < DWT_LEVELS_3D; j++) {
338 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
339
340 for (unsigned k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
341 s->plane[i].band[j][k].a_width = w8 << t;
342 s->plane[i].band[j][k].a_height = h8 << t;
343 }
344 }
345 }
346
347 /* ll2 and ll1 commented out because they are done in-place */
348 18 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
349 18 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
350 // s->plane[i].l_h[2] = ll2;
351 18 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
352 18 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
353 // s->plane[i].l_h[5] = ll1;
354 18 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
355 18 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (s->transform_type != 0) {
357 int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
358
359 s->plane[i].l_h[8] = frame2;
360 s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
361 }
362 }
363
364 6 s->a_transform_type = s->transform_type;
365 6 s->a_height = s->coded_height;
366 6 s->a_width = s->coded_width;
367 6 s->a_format = s->coded_format;
368
369 6 return 0;
370 }
371
372 33 static int cfhd_decode(AVCodecContext *avctx, AVFrame *pic,
373 int *got_frame, AVPacket *avpkt)
374 {
375 33 CFHDContext *s = avctx->priv_data;
376 33 CFHDDSPContext *dsp = &s->dsp;
377 GetByteContext gb;
378 33 int ret = 0, got_buffer = 0;
379
380 33 init_frame_defaults(s);
381 33 s->planes = av_pix_fmt_count_planes(s->coded_format);
382
383 33 bytestream2_init(&gb, avpkt->data, avpkt->size);
384
385
2/2
✓ Branch 1 taken 17193 times.
✓ Branch 2 taken 33 times.
17226 while (bytestream2_get_bytes_left(&gb) >= 4) {
386 /* Bit weird but implement the tag parsing as the spec says */
387 17193 uint16_t tagu = bytestream2_get_be16(&gb);
388 17193 int16_t tag = (int16_t)tagu;
389 17193 int8_t tag8 = (int8_t)(tagu >> 8);
390 17193 uint16_t abstag = abs(tag);
391 17193 int8_t abs_tag8 = abs(tag8);
392 17193 uint16_t data = bytestream2_get_be16(&gb);
393 int16_t *coeff_data;
394
395
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 17193 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
17193 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
396 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
397
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17160 times.
17193 } else if (tag == SampleFlags) {
398 33 av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
399 33 s->progressive = data & 0x0001;
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17160 times.
17160 } else if (tag == FrameType) {
401 s->frame_type = data;
402 av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17160 times.
17160 } else if (abstag == VersionMajor) {
404 av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17160 times.
17160 } else if (abstag == VersionMinor) {
406 av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17160 times.
17160 } else if (abstag == VersionRevision) {
408 av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17160 times.
17160 } else if (abstag == VersionEdit) {
410 av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
411
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17127 times.
17160 } else if (abstag == Version) {
412 33 av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
413
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17094 times.
17127 } else if (tag == ImageWidth) {
414 33 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
415 33 s->coded_width = data;
416
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17061 times.
17094 } else if (tag == ImageHeight) {
417 33 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
418 33 s->coded_height = data;
419
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17028 times.
17061 } else if (tag == ChannelCount) {
420 33 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
421 33 s->channel_cnt = data;
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (data > 4) {
423 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
424 ret = AVERROR_PATCHWELCOME;
425 goto end;
426 }
427
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 16995 times.
17028 } else if (tag == SubbandCount) {
428 33 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
429
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
430 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
431 ret = AVERROR_PATCHWELCOME;
432 goto end;
433 }
434
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 16929 times.
16995 } else if (tag == ChannelNumber) {
435 66 s->channel_num = data;
436 66 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
437 66 init_plane_defaults(s);
438
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 16038 times.
16929 } else if (tag == SubbandNumber) {
439
5/8
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 594 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 198 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
891 if (s->subband_num != 0 && data == 1 && (s->transform_type == 0 || s->transform_type == 2)) // hack
440 198 s->level++;
441 891 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
442 891 s->subband_num = data;
443
2/4
✓ Branch 0 taken 891 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 891 times.
✗ Branch 3 not taken.
891 if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
444
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
891 (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
445 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
446 ret = AVERROR(EINVAL);
447 goto end;
448 }
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (s->subband_num > 3) {
450 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
451 ret = AVERROR(EINVAL);
452 goto end;
453 }
454
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 15147 times.
16038 } else if (tag == SubbandBand) {
455 891 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
456
2/4
✓ Branch 0 taken 891 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 891 times.
✗ Branch 3 not taken.
891 if ((s->transform_type == 0 && data >= SUBBAND_COUNT) ||
457
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
891 (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) {
458 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
459 ret = AVERROR(EINVAL);
460 goto end;
461 }
462
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
891 if (s->transform_type == 0 || s->transform_type == 2)
463 891 s->subband_num_actual = data;
464 else
465 av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data);
466
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 15048 times.
15147 } else if (tag == LowpassPrecision)
467 99 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
468
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 14157 times.
15048 else if (tag == Quantization) {
469 891 s->quantisation = data;
470 891 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
471
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 14146 times.
14157 } else if (tag == PrescaleTable) {
472
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 11 times.
99 for (int i = 0; i < 8; i++)
473 88 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
474 11 av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
475
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 13255 times.
14146 } else if (tag == BandEncoding) {
476
2/4
✓ Branch 0 taken 891 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 891 times.
891 if (!data || data > 5) {
477 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
478 ret = AVERROR(EINVAL);
479 goto end;
480 }
481 891 s->band_encoding = data;
482 891 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
483
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 13156 times.
13255 } else if (tag == LowpassWidth) {
484 99 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
485 99 s->plane[s->channel_num].band[0][0].width = data;
486 99 s->plane[s->channel_num].band[0][0].stride = data;
487
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 13057 times.
13156 } else if (tag == LowpassHeight) {
488 99 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
489 99 s->plane[s->channel_num].band[0][0].height = data;
490
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 12958 times.
13057 } else if (tag == SampleType) {
491 99 s->sample_type = data;
492 99 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
493
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 12925 times.
12958 } else if (tag == TransformType) {
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (data > 2) {
495 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
496 ret = AVERROR(EINVAL);
497 goto end;
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 } else if (data == 1) {
499 av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
500 ret = AVERROR_PATCHWELCOME;
501 goto end;
502 }
503
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (s->transform_type == -1) {
504 33 s->transform_type = data;
505 33 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
506 } else {
507 av_log(avctx, AV_LOG_DEBUG, "Ignoring additional transform type %"PRIu16"\n", data);
508 }
509
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 12859 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
12925 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (abstag == 0x4001)
511 s->peak.level = 0;
512
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
513 66 bytestream2_skipu(&gb, data * 4);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12859 times.
12859 } else if (tag == FrameIndex) {
515 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
516 s->frame_index = data;
517
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 12826 times.
12859 } else if (tag == SampleIndexTable) {
518 33 av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (data > bytestream2_get_bytes_left(&gb) / 4) {
520 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
521 ret = AVERROR_INVALIDDATA;
522 goto end;
523 }
524
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 33 times.
132 for (int i = 0; i < data; i++) {
525 99 uint32_t offset = bytestream2_get_be32(&gb);
526 99 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
527 }
528
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 12529 times.
12826 } else if (tag == HighpassWidth) {
529 297 av_log(avctx, AV_LOG_DEBUG, "Highpass width %i channel %i level %i subband %i\n", data, s->channel_num, s->level, s->subband_num);
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (data < 3) {
531 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
532 ret = AVERROR(EINVAL);
533 goto end;
534 }
535 297 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
536 297 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
537
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 12232 times.
12529 } else if (tag == HighpassHeight) {
538 297 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (data < 3) {
540 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
541 ret = AVERROR(EINVAL);
542 goto end;
543 }
544 297 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
545
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 11341 times.
12232 } else if (tag == BandWidth) {
546 891 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (data < 3) {
548 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
549 ret = AVERROR(EINVAL);
550 goto end;
551 }
552 891 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
553 891 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
554
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 10450 times.
11341 } else if (tag == BandHeight) {
555 891 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (data < 3) {
557 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
558 ret = AVERROR(EINVAL);
559 goto end;
560 }
561 891 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10450 times.
10450 } else if (tag == InputFormat) {
563 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
564 if (s->coded_format == AV_PIX_FMT_NONE ||
565 s->coded_format == AV_PIX_FMT_YUV422P10) {
566 if (data >= 100 && data <= 105) {
567 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
568 } else if (data >= 122 && data <= 128) {
569 s->coded_format = AV_PIX_FMT_GBRP12;
570 } else if (data == 30) {
571 s->coded_format = AV_PIX_FMT_GBRAP12;
572 } else {
573 s->coded_format = AV_PIX_FMT_YUV422P10;
574 }
575 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
576 }
577
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 9559 times.
10450 } else if (tag == BandCodingFlags) {
578 891 s->codebook = data & 0xf;
579 891 s->difference_coding = (data >> 4) & 1;
580 891 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
581
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9526 times.
9559 } else if (tag == Precision) {
582 33 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
583
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
33 if (!(data == 10 || data == 12)) {
584 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
585 ret = AVERROR(EINVAL);
586 goto end;
587 }
588 33 avctx->bits_per_raw_sample = s->bpc = data;
589
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9493 times.
9526 } else if (tag == EncodedFormat) {
590 33 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
591
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 11 times.
33 if (data == 1) {
592 22 s->coded_format = AV_PIX_FMT_YUV422P10;
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 } else if (data == 2) {
594 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
595
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 } else if (data == 3) {
596 11 s->coded_format = AV_PIX_FMT_GBRP12;
597 } else if (data == 4) {
598 s->coded_format = AV_PIX_FMT_GBRAP12;
599 } else {
600 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
601 ret = AVERROR_PATCHWELCOME;
602 goto end;
603 }
604
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
605
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9460 times.
9493 } else if (tag == -DisplayHeight) {
606 33 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
607 33 s->cropped_height = data;
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9460 times.
9460 } else if (tag == -PeakOffsetLow) {
609 s->peak.offset &= ~0xffff;
610 s->peak.offset |= (data & 0xffff);
611 s->peak.base = gb;
612 s->peak.level = 0;
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9460 times.
9460 } else if (tag == -PeakOffsetHigh) {
614 s->peak.offset &= 0xffff;
615 s->peak.offset |= (data & 0xffffU)<<16;
616 s->peak.base = gb;
617 s->peak.level = 0;
618
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9460 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9460 } else if (tag == -PeakLevel && s->peak.offset) {
619 s->peak.level = data;
620 if (s->peak.offset < 4 - bytestream2_tell(&s->peak.base) ||
621 s->peak.offset > 4 + bytestream2_get_bytes_left(&s->peak.base)
622 ) {
623 ret = AVERROR_INVALIDDATA;
624 goto end;
625 }
626 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
627 } else
628 9460 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
629
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17193 times.
17193 if (s->channel_num >= s->planes) {
631 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
632 ret = AVERROR(EINVAL);
633 goto end;
634 }
635
636
5/8
✓ Branch 0 taken 15741 times.
✓ Branch 1 taken 1452 times.
✓ Branch 2 taken 15741 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15741 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 15741 times.
17193 if (got_buffer && (s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE)) {
637 av_log(avctx, AV_LOG_ERROR, "Header tag after end of header\n");
638 ret = AVERROR(EINVAL);
639 goto end;
640 }
641
642
4/4
✓ Branch 0 taken 1782 times.
✓ Branch 1 taken 15411 times.
✓ Branch 2 taken 99 times.
✓ Branch 3 taken 1683 times.
17193 if (tag == BitstreamMarker && data == CoefficientSegment &&
643
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 66 times.
99 s->coded_format != AV_PIX_FMT_NONE) {
644 33 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
645 33 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
647
648
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (s->coded_width) {
649 33 s->coded_width *= factor;
650 }
651
652
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (s->coded_height) {
653 33 s->coded_height *= factor;
654 }
655
656
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
33 if (!s->a_width && !s->coded_width) {
657 s->coded_width = lowpass_width * factor * 8;
658 }
659
660
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
33 if (!s->a_height && !s->coded_height) {
661 s->coded_height = lowpass_height * factor * 8;
662 }
663
664
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
33 if (s->a_width && !s->coded_width)
665 s->coded_width = s->a_width;
666
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
33 if (s->a_height && !s->coded_height)
667 s->coded_height = s->a_height;
668
669
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
33 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
670
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 s->a_format != s->coded_format ||
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 s->transform_type != s->a_transform_type) {
672 6 free_buffers(s);
673
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = alloc_buffers(avctx)) < 0) {
674 free_buffers(s);
675 return ret;
676 }
677 }
678 33 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
680 return ret;
681
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (s->cropped_height) {
682 33 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (avctx->height < height)
684 return AVERROR_INVALIDDATA;
685 33 avctx->height = height;
686 }
687 33 pic->width = pic->height = 0;
688
689
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if ((ret = ff_thread_get_buffer(avctx, pic, 0)) < 0)
690 return ret;
691
692 33 s->coded_width = 0;
693 33 s->coded_height = 0;
694 33 s->coded_format = AV_PIX_FMT_NONE;
695 33 got_buffer = 1;
696
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 17160 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
17160 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
697 pic->width = pic->height = 0;
698
699 if ((ret = ff_thread_get_buffer(avctx, pic, 0)) < 0)
700 return ret;
701 s->coded_width = 0;
702 s->coded_height = 0;
703 s->coded_format = AV_PIX_FMT_NONE;
704 got_buffer = 1;
705 }
706
707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17193 times.
17193 if (s->subband_num_actual == 255)
708 goto finish;
709
710
8/10
✓ Branch 0 taken 1782 times.
✓ Branch 1 taken 15411 times.
✓ Branch 2 taken 1683 times.
✓ Branch 3 taken 99 times.
✓ Branch 4 taken 16203 times.
✓ Branch 5 taken 891 times.
✓ Branch 6 taken 16203 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 16203 times.
17193 if (tag == BitstreamMarker && data == CoefficientSegment || tag == BandHeader || tag == BandSecondPass || s->peak.level)
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 990 times.
990 if (s->transform_type != s->a_transform_type)
712 return AVERROR_PATCHWELCOME;
713
714 17193 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
715
716 /* Lowpass coefficients */
717
4/4
✓ Branch 0 taken 1782 times.
✓ Branch 1 taken 15411 times.
✓ Branch 2 taken 99 times.
✓ Branch 3 taken 1683 times.
17193 if (tag == BitstreamMarker && data == CoefficientSegment) {
718 int lowpass_height, lowpass_width, lowpass_a_height, lowpass_a_width;
719
720
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 99 times.
99 if (!s->a_width || !s->a_height) {
721 ret = AVERROR_INVALIDDATA;
722 goto end;
723 }
724
725 99 lowpass_height = s->plane[s->channel_num].band[0][0].height;
726 99 lowpass_width = s->plane[s->channel_num].band[0][0].width;
727 99 lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
728 99 lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
729
730
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 99 times.
99 if (lowpass_width < 3 ||
731 lowpass_width > lowpass_a_width) {
732 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
733 ret = AVERROR(EINVAL);
734 goto end;
735 }
736
737
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 99 times.
99 if (lowpass_height < 3 ||
738 lowpass_height > lowpass_a_height) {
739 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
740 ret = AVERROR(EINVAL);
741 goto end;
742 }
743
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (!got_buffer) {
745 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
746 ret = AVERROR(EINVAL);
747 goto end;
748 }
749
750
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
99 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
751
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
99 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
752 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
753 ret = AVERROR(EINVAL);
754 goto end;
755 }
756
757 99 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
758
2/2
✓ Branch 0 taken 4983 times.
✓ Branch 1 taken 99 times.
5082 for (int i = 0; i < lowpass_height; i++) {
759
2/2
✓ Branch 0 taken 339284 times.
✓ Branch 1 taken 4983 times.
344267 for (int j = 0; j < lowpass_width; j++)
760 339284 coeff_data[j] = bytestream2_get_be16u(&gb);
761
762 4983 coeff_data += lowpass_width;
763 }
764
765 /* Align to mod-4 position to continue reading tags */
766 99 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
767
768 /* Copy last line of coefficients if odd height */
769
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 66 times.
99 if (lowpass_height & 1) {
770 33 memcpy(&coeff_data[lowpass_height * lowpass_width],
771 33 &coeff_data[(lowpass_height - 1) * lowpass_width],
772 lowpass_width * sizeof(*coeff_data));
773 }
774
775 99 s->plane[s->channel_num].band[0][0].read_ok = 1;
776
777 99 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
778 }
779
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17193 times.
17193 av_assert0(s->subband_num_actual != 255);
781
3/4
✓ Branch 0 taken 16302 times.
✓ Branch 1 taken 891 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16302 times.
17193 if (tag == BandHeader || tag == BandSecondPass) {
782 int highpass_height, highpass_width, highpass_a_width, highpass_a_height, highpass_stride, a_expected;
783 int expected;
784 GetBitContext gbit;
785 891 int count = 0, bytes;
786
787
2/4
✓ Branch 0 taken 891 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 891 times.
891 if (!s->a_width || !s->a_height) {
788 ret = AVERROR_INVALIDDATA;
789 goto end;
790 }
791
792 891 highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
793 891 highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
794 891 highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
795 891 highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
796 891 highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
797 891 a_expected = highpass_a_height * highpass_a_width;
798
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (!got_buffer) {
800 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
801 ret = AVERROR(EINVAL);
802 goto end;
803 }
804
805
3/6
✓ Branch 0 taken 891 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 891 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 891 times.
891 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
806 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
807 ret = AVERROR(EINVAL);
808 goto end;
809 }
810 891 expected = highpass_height * highpass_stride;
811
812 891 av_log(avctx, AV_LOG_DEBUG, "Start subband coeffs plane %i level %i codebook %i expected %i\n", s->channel_num, s->level, s->codebook, expected);
813
814 891 ret = init_get_bits8(&gbit, gb.buffer, bytestream2_get_bytes_left(&gb));
815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (ret < 0)
816 goto end;
817 {
818 891 OPEN_READER(re, &gbit);
819
820 891 const int lossless = s->band_encoding == 5;
821
822
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
891 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
823 s->codebook = 1;
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (!s->codebook) {
825 while (1) {
826 int level, run, coeff;
827
828 UPDATE_CACHE(re, &gbit);
829 GET_RL_VLC(level, run, re, &gbit, s->table_9_rl_vlc,
830 VLC_BITS, 3, 1);
831
832 /* escape */
833 if (!run)
834 break;
835
836 count += run;
837
838 if (count > expected)
839 break;
840
841 if (!lossless)
842 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
843 else
844 coeff = level;
845 if (tag == BandSecondPass) {
846 const uint16_t q = s->quantisation;
847
848 for (int i = 0; i < run; i++) {
849 *coeff_data |= coeff * 256U;
850 *coeff_data++ *= q;
851 }
852 } else {
853 for (int i = 0; i < run; i++)
854 *coeff_data++ = coeff;
855 }
856 }
857 } else {
858 3522977 while (1) {
859 int level, run, coeff;
860
861 3523868 UPDATE_CACHE(re, &gbit);
862
4/4
✓ Branch 1 taken 268732 times.
✓ Branch 2 taken 3255136 times.
✓ Branch 4 taken 17281 times.
✓ Branch 5 taken 251451 times.
3523868 GET_RL_VLC(level, run, re, &gbit, s->table_18_rl_vlc,
863 VLC_BITS, 3, 1);
864
865 /* escape */
866
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 3522977 times.
3523868 if (!run)
867 891 break;
868
869 3522977 count += run;
870
871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3522977 times.
3522977 if (count > expected)
872 break;
873
874
1/2
✓ Branch 0 taken 3522977 times.
✗ Branch 1 not taken.
3522977 if (!lossless)
875 3522977 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
876 else
877 coeff = level;
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3522977 times.
3522977 if (tag == BandSecondPass) {
879 const uint16_t q = s->quantisation;
880
881 for (int i = 0; i < run; i++) {
882 *coeff_data |= coeff * 256U;
883 *coeff_data++ *= q;
884 }
885 } else {
886
2/2
✓ Branch 0 taken 21661728 times.
✓ Branch 1 taken 3522977 times.
25184705 for (int i = 0; i < run; i++)
887 21661728 *coeff_data++ = coeff;
888 }
889 }
890 }
891 891 CLOSE_READER(re, &gbit);
892 }
893
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (count > expected) {
895 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
896 ret = AVERROR(EINVAL);
897 goto end;
898 }
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (s->peak.level)
900 peak_table(coeff_data - count, &s->peak, count);
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 891 times.
891 if (s->difference_coding)
902 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
903
904 891 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&gbit), 3), 4);
905
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 891 times.
891 if (bytes > bytestream2_get_bytes_left(&gb)) {
906 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
907 ret = AVERROR(EINVAL);
908 goto end;
909 } else
910 891 bytestream2_seek(&gb, bytes, SEEK_CUR);
911
912 891 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
913 891 s->plane[s->channel_num].band[s->level][s->subband_num].read_ok = 1;
914 891 finish:
915
1/2
✓ Branch 0 taken 891 times.
✗ Branch 1 not taken.
891 if (s->subband_num_actual != 255)
916 891 s->codebook = 0;
917 }
918 }
919
920 33 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
922 s->progressive = 1;
923 s->planes = 4;
924 }
925
926 33 ff_thread_finish_setup(avctx);
927
928
3/6
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
33 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 s->a_transform_type == INT_MIN) {
930 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
931 ret = AVERROR(EINVAL);
932 goto end;
933 }
934
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!got_buffer) {
936 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
937 ret = AVERROR(EINVAL);
938 goto end;
939 }
940
941
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 33 times.
132 for (int plane = 0; plane < s->planes; plane++) {
942
3/4
✓ Branch 0 taken 396 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 297 times.
✓ Branch 3 taken 99 times.
396 for (int level = 0; level < (s->transform_type == 0 ? DWT_LEVELS : DWT_LEVELS_3D) ; level++) {
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (s->transform_type == 2)
944 if (level == 2 || level == 5)
945 continue;
946
2/2
✓ Branch 0 taken 990 times.
✓ Branch 1 taken 297 times.
1287 for (int o = !!level; o < 4 ; o++) {
947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 990 times.
990 if (!s->plane[plane].band[level][o].read_ok) {
948 ret = AVERROR_INVALIDDATA;
949 goto end;
950 }
951 }
952 }
953 }
954
955
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 if (s->transform_type == 0 && s->sample_type != 1) {
956
3/4
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
132 for (int plane = 0; plane < s->planes && !ret; plane++) {
957 /* level 1 */
958 99 int lowpass_height = s->plane[plane].band[0][0].height;
959 99 int output_stride = s->plane[plane].band[0][0].a_width;
960 99 int lowpass_width = s->plane[plane].band[0][0].width;
961 99 int highpass_stride = s->plane[plane].band[0][1].stride;
962
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 33 times.
99 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
963 ptrdiff_t dst_linesize;
964 int16_t *low, *high, *output, *dst;
965
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
967 act_plane = 0;
968 dst_linesize = pic->linesize[act_plane];
969 } else {
970 99 dst_linesize = pic->linesize[act_plane] / 2;
971 }
972
973
3/6
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 99 times.
✗ Branch 5 not taken.
99 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
974
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
99 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 lowpass_width < 3 || lowpass_height < 3) {
976 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
977 ret = AVERROR(EINVAL);
978 goto end;
979 }
980
981 99 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
982
983 99 low = s->plane[plane].subband[0];
984 99 high = s->plane[plane].subband[2];
985 99 output = s->plane[plane].l_h[0];
986 99 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
987
988 99 low = s->plane[plane].subband[1];
989 99 high = s->plane[plane].subband[3];
990 99 output = s->plane[plane].l_h[1];
991
992 99 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
993
994 99 low = s->plane[plane].l_h[0];
995 99 high = s->plane[plane].l_h[1];
996 99 output = s->plane[plane].subband[0];
997 99 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
998
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 66 times.
99 if (s->bpc == 12) {
999 33 output = s->plane[plane].subband[0];
1000
2/2
✓ Branch 0 taken 3960 times.
✓ Branch 1 taken 33 times.
3993 for (int i = 0; i < lowpass_height * 2; i++) {
1001
2/2
✓ Branch 0 taken 712800 times.
✓ Branch 1 taken 3960 times.
716760 for (int j = 0; j < lowpass_width * 2; j++)
1002 712800 output[j] *= 4;
1003
1004 3960 output += output_stride * 2;
1005 }
1006 }
1007
1008 /* level 2 */
1009 99 lowpass_height = s->plane[plane].band[1][1].height;
1010 99 output_stride = s->plane[plane].band[1][1].a_width;
1011 99 lowpass_width = s->plane[plane].band[1][1].width;
1012 99 highpass_stride = s->plane[plane].band[1][1].stride;
1013
1014
3/6
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 99 times.
✗ Branch 5 not taken.
99 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1015
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
99 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 lowpass_width < 3 || lowpass_height < 3) {
1017 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1018 ret = AVERROR(EINVAL);
1019 goto end;
1020 }
1021
1022 99 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1023
1024 99 low = s->plane[plane].subband[0];
1025 99 high = s->plane[plane].subband[5];
1026 99 output = s->plane[plane].l_h[3];
1027 99 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1028
1029 99 low = s->plane[plane].subband[4];
1030 99 high = s->plane[plane].subband[6];
1031 99 output = s->plane[plane].l_h[4];
1032 99 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1033
1034 99 low = s->plane[plane].l_h[3];
1035 99 high = s->plane[plane].l_h[4];
1036 99 output = s->plane[plane].subband[0];
1037 99 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1038
1039 99 output = s->plane[plane].subband[0];
1040
2/2
✓ Branch 0 taken 19932 times.
✓ Branch 1 taken 99 times.
20031 for (int i = 0; i < lowpass_height * 2; i++) {
1041
2/2
✓ Branch 0 taken 5428544 times.
✓ Branch 1 taken 19932 times.
5448476 for (int j = 0; j < lowpass_width * 2; j++)
1042 5428544 output[j] *= 4;
1043
1044 19932 output += output_stride * 2;
1045 }
1046
1047 /* level 3 */
1048 99 lowpass_height = s->plane[plane].band[2][1].height;
1049 99 output_stride = s->plane[plane].band[2][1].a_width;
1050 99 lowpass_width = s->plane[plane].band[2][1].width;
1051 99 highpass_stride = s->plane[plane].band[2][1].stride;
1052
1053
3/6
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 99 times.
✗ Branch 5 not taken.
99 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1054
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
99 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
1055
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 99 times.
99 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
1056 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1057 ret = AVERROR(EINVAL);
1058 goto end;
1059 }
1060
1061 99 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1062
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 if (s->progressive) {
1063 99 low = s->plane[plane].subband[0];
1064 99 high = s->plane[plane].subband[8];
1065 99 output = s->plane[plane].l_h[6];
1066 99 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1067
1068 99 low = s->plane[plane].subband[7];
1069 99 high = s->plane[plane].subband[9];
1070 99 output = s->plane[plane].l_h[7];
1071 99 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1072
1073 99 dst = (int16_t *)pic->data[act_plane];
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1075 if (plane & 1)
1076 dst++;
1077 if (plane > 1)
1078 dst += pic->linesize[act_plane] >> 1;
1079 }
1080 99 low = s->plane[plane].l_h[6];
1081 99 high = s->plane[plane].l_h[7];
1082
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1084 (lowpass_height * 2 > avctx->coded_height / 2 ||
1085 lowpass_width * 2 > avctx->coded_width / 2 )
1086 ) {
1087 ret = AVERROR_INVALIDDATA;
1088 goto end;
1089 }
1090
1091
2/2
✓ Branch 0 taken 39864 times.
✓ Branch 1 taken 99 times.
39963 for (int i = 0; i < s->plane[act_plane].height; i++) {
1092 39864 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1093
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39864 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
39864 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1094 process_alpha(dst, lowpass_width * 2);
1095 39864 low += output_stride;
1096 39864 high += output_stride;
1097 39864 dst += dst_linesize;
1098 }
1099 } else {
1100 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", !!(pic->flags & AV_FRAME_FLAG_INTERLACED));
1101 pic->flags |= AV_FRAME_FLAG_INTERLACED;
1102 low = s->plane[plane].subband[0];
1103 high = s->plane[plane].subband[7];
1104 output = s->plane[plane].l_h[6];
1105 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1106
1107 low = s->plane[plane].subband[8];
1108 high = s->plane[plane].subband[9];
1109 output = s->plane[plane].l_h[7];
1110 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1111
1112 dst = (int16_t *)pic->data[act_plane];
1113 low = s->plane[plane].l_h[6];
1114 high = s->plane[plane].l_h[7];
1115 for (int i = 0; i < s->plane[act_plane].height / 2; i++) {
1116 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1117 low += output_stride * 2;
1118 high += output_stride * 2;
1119 dst += pic->linesize[act_plane];
1120 }
1121 }
1122 }
1123 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1124 for (int plane = 0; plane < s->planes && !ret; plane++) {
1125 int lowpass_height = s->plane[plane].band[0][0].height;
1126 int output_stride = s->plane[plane].band[0][0].a_width;
1127 int lowpass_width = s->plane[plane].band[0][0].width;
1128 int highpass_stride = s->plane[plane].band[0][1].stride;
1129 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1130 int16_t *low, *high, *output, *dst;
1131 ptrdiff_t dst_linesize;
1132
1133 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1134 act_plane = 0;
1135 dst_linesize = pic->linesize[act_plane];
1136 } else {
1137 dst_linesize = pic->linesize[act_plane] / 2;
1138 }
1139
1140 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1141 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1142 lowpass_width < 3 || lowpass_height < 3) {
1143 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1144 ret = AVERROR(EINVAL);
1145 goto end;
1146 }
1147
1148 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1149
1150 low = s->plane[plane].subband[0];
1151 high = s->plane[plane].subband[2];
1152 output = s->plane[plane].l_h[0];
1153 dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1154
1155 low = s->plane[plane].subband[1];
1156 high = s->plane[plane].subband[3];
1157 output = s->plane[plane].l_h[1];
1158 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1159
1160 low = s->plane[plane].l_h[0];
1161 high = s->plane[plane].l_h[1];
1162 output = s->plane[plane].l_h[7];
1163 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1164 if (s->bpc == 12) {
1165 output = s->plane[plane].l_h[7];
1166 for (int i = 0; i < lowpass_height * 2; i++) {
1167 for (int j = 0; j < lowpass_width * 2; j++)
1168 output[j] *= 4;
1169
1170 output += output_stride * 2;
1171 }
1172 }
1173
1174 lowpass_height = s->plane[plane].band[1][1].height;
1175 output_stride = s->plane[plane].band[1][1].a_width;
1176 lowpass_width = s->plane[plane].band[1][1].width;
1177 highpass_stride = s->plane[plane].band[1][1].stride;
1178
1179 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1180 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1181 lowpass_width < 3 || lowpass_height < 3) {
1182 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1183 ret = AVERROR(EINVAL);
1184 goto end;
1185 }
1186
1187 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1188
1189 low = s->plane[plane].l_h[7];
1190 high = s->plane[plane].subband[5];
1191 output = s->plane[plane].l_h[3];
1192 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1193
1194 low = s->plane[plane].subband[4];
1195 high = s->plane[plane].subband[6];
1196 output = s->plane[plane].l_h[4];
1197 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1198
1199 low = s->plane[plane].l_h[3];
1200 high = s->plane[plane].l_h[4];
1201 output = s->plane[plane].l_h[7];
1202 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1203
1204 output = s->plane[plane].l_h[7];
1205 for (int i = 0; i < lowpass_height * 2; i++) {
1206 for (int j = 0; j < lowpass_width * 2; j++)
1207 output[j] *= 4;
1208 output += output_stride * 2;
1209 }
1210
1211 low = s->plane[plane].subband[7];
1212 high = s->plane[plane].subband[9];
1213 output = s->plane[plane].l_h[3];
1214 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1215
1216 low = s->plane[plane].subband[8];
1217 high = s->plane[plane].subband[10];
1218 output = s->plane[plane].l_h[4];
1219 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1220
1221 low = s->plane[plane].l_h[3];
1222 high = s->plane[plane].l_h[4];
1223 output = s->plane[plane].l_h[9];
1224 dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1225
1226 lowpass_height = s->plane[plane].band[4][1].height;
1227 output_stride = s->plane[plane].band[4][1].a_width;
1228 lowpass_width = s->plane[plane].band[4][1].width;
1229 highpass_stride = s->plane[plane].band[4][1].stride;
1230 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1231
1232 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1233 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1234 lowpass_width < 3 || lowpass_height < 3 || lowpass_width * 2 > s->plane[plane].width) {
1235 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1236 ret = AVERROR(EINVAL);
1237 goto end;
1238 }
1239
1240 low = s->plane[plane].l_h[7];
1241 high = s->plane[plane].l_h[9];
1242 output = s->plane[plane].l_h[7];
1243 for (int i = 0; i < lowpass_height; i++) {
1244 inverse_temporal_filter(low, high, lowpass_width);
1245 low += output_stride;
1246 high += output_stride;
1247 }
1248 if (s->progressive) {
1249 low = s->plane[plane].l_h[7];
1250 high = s->plane[plane].subband[15];
1251 output = s->plane[plane].l_h[6];
1252 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1253
1254 low = s->plane[plane].subband[14];
1255 high = s->plane[plane].subband[16];
1256 output = s->plane[plane].l_h[7];
1257 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1258
1259 low = s->plane[plane].l_h[9];
1260 high = s->plane[plane].subband[12];
1261 output = s->plane[plane].l_h[8];
1262 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1263
1264 low = s->plane[plane].subband[11];
1265 high = s->plane[plane].subband[13];
1266 output = s->plane[plane].l_h[9];
1267 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1268
1269 if (s->sample_type == 1)
1270 continue;
1271
1272 dst = (int16_t *)pic->data[act_plane];
1273 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1274 if (plane & 1)
1275 dst++;
1276 if (plane > 1)
1277 dst += pic->linesize[act_plane] >> 1;
1278 }
1279
1280 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1281 (lowpass_height * 2 > avctx->coded_height / 2 ||
1282 lowpass_width * 2 > avctx->coded_width / 2 )
1283 ) {
1284 ret = AVERROR_INVALIDDATA;
1285 goto end;
1286 }
1287
1288 low = s->plane[plane].l_h[6];
1289 high = s->plane[plane].l_h[7];
1290 for (int i = 0; i < s->plane[act_plane].height; i++) {
1291 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1292 low += output_stride;
1293 high += output_stride;
1294 dst += dst_linesize;
1295 }
1296 } else {
1297 pic->flags |= AV_FRAME_FLAG_INTERLACED;
1298 low = s->plane[plane].l_h[7];
1299 high = s->plane[plane].subband[14];
1300 output = s->plane[plane].l_h[6];
1301 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1302
1303 low = s->plane[plane].subband[15];
1304 high = s->plane[plane].subband[16];
1305 output = s->plane[plane].l_h[7];
1306 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1307
1308 low = s->plane[plane].l_h[9];
1309 high = s->plane[plane].subband[11];
1310 output = s->plane[plane].l_h[8];
1311 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1312
1313 low = s->plane[plane].subband[12];
1314 high = s->plane[plane].subband[13];
1315 output = s->plane[plane].l_h[9];
1316 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1317
1318 if (s->sample_type == 1)
1319 continue;
1320
1321 dst = (int16_t *)pic->data[act_plane];
1322 low = s->plane[plane].l_h[6];
1323 high = s->plane[plane].l_h[7];
1324 for (int i = 0; i < s->plane[act_plane].height / 2; i++) {
1325 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1326 low += output_stride * 2;
1327 high += output_stride * 2;
1328 dst += pic->linesize[act_plane];
1329 }
1330 }
1331 }
1332 }
1333
1334
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 if (s->transform_type == 2 && s->sample_type == 1) {
1335 int16_t *low, *high, *dst;
1336 int output_stride, lowpass_height, lowpass_width;
1337 ptrdiff_t dst_linesize;
1338
1339 for (int plane = 0; plane < s->planes; plane++) {
1340 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1341
1342 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1343 act_plane = 0;
1344 dst_linesize = pic->linesize[act_plane];
1345 } else {
1346 dst_linesize = pic->linesize[act_plane] / 2;
1347 }
1348
1349 lowpass_height = s->plane[plane].band[4][1].height;
1350 output_stride = s->plane[plane].band[4][1].a_width;
1351 lowpass_width = s->plane[plane].band[4][1].width;
1352
1353 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1354 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1355 lowpass_width < 3 || lowpass_height < 3 || lowpass_width * 2 > s->plane[plane].width) {
1356 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1357 ret = AVERROR(EINVAL);
1358 goto end;
1359 }
1360
1361 if (s->progressive) {
1362 dst = (int16_t *)pic->data[act_plane];
1363 low = s->plane[plane].l_h[8];
1364 high = s->plane[plane].l_h[9];
1365
1366 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1367 if (plane & 1)
1368 dst++;
1369 if (plane > 1)
1370 dst += pic->linesize[act_plane] >> 1;
1371 }
1372
1373 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1374 (lowpass_height * 2 > avctx->coded_height / 2 ||
1375 lowpass_width * 2 > avctx->coded_width / 2 )
1376 ) {
1377 ret = AVERROR_INVALIDDATA;
1378 goto end;
1379 }
1380
1381 for (int i = 0; i < s->plane[act_plane].height; i++) {
1382 dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1383 low += output_stride;
1384 high += output_stride;
1385 dst += dst_linesize;
1386 }
1387 } else {
1388 dst = (int16_t *)pic->data[act_plane];
1389 low = s->plane[plane].l_h[8];
1390 high = s->plane[plane].l_h[9];
1391 for (int i = 0; i < s->plane[act_plane].height / 2; i++) {
1392 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1393 low += output_stride * 2;
1394 high += output_stride * 2;
1395 dst += pic->linesize[act_plane];
1396 }
1397 }
1398 }
1399 }
1400
1401
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1402 process_bayer(pic, s->bpc);
1403 33 end:
1404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
1405 return ret;
1406
1407 33 *got_frame = 1;
1408 33 return avpkt->size;
1409 }
1410
1411 6 static av_cold int cfhd_close(AVCodecContext *avctx)
1412 {
1413 6 CFHDContext *s = avctx->priv_data;
1414
1415 6 free_buffers(s);
1416
1417 6 return 0;
1418 }
1419
1420 #if HAVE_THREADS
1421 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1422 {
1423 CFHDContext *psrc = src->priv_data;
1424 CFHDContext *pdst = dst->priv_data;
1425 int ret;
1426
1427 if (dst == src || psrc->transform_type == 0)
1428 return 0;
1429
1430 if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
1431 pdst->a_format != psrc->a_format ||
1432 pdst->a_width != psrc->a_width ||
1433 pdst->a_height != psrc->a_height ||
1434 pdst->a_transform_type != psrc->a_transform_type)
1435 free_buffers(pdst);
1436
1437 pdst->a_format = psrc->a_format;
1438 pdst->a_width = psrc->a_width;
1439 pdst->a_height = psrc->a_height;
1440 pdst->a_transform_type = psrc->a_transform_type;
1441 pdst->transform_type = psrc->transform_type;
1442 pdst->progressive = psrc->progressive;
1443 pdst->planes = psrc->planes;
1444
1445 if (!pdst->plane[0].idwt_buf) {
1446 pdst->coded_width = pdst->a_width;
1447 pdst->coded_height = pdst->a_height;
1448 pdst->coded_format = pdst->a_format;
1449 pdst->transform_type = pdst->a_transform_type;
1450 ret = alloc_buffers(dst);
1451 if (ret < 0)
1452 return ret;
1453 }
1454
1455 for (int plane = 0; plane < pdst->planes; plane++) {
1456 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1457 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1458 pdst->plane[plane].idwt_size * sizeof(int16_t));
1459 }
1460
1461 return 0;
1462 }
1463 #endif
1464
1465 const FFCodec ff_cfhd_decoder = {
1466 .p.name = "cfhd",
1467 CODEC_LONG_NAME("GoPro CineForm HD"),
1468 .p.type = AVMEDIA_TYPE_VIDEO,
1469 .p.id = AV_CODEC_ID_CFHD,
1470 .priv_data_size = sizeof(CFHDContext),
1471 .init = cfhd_init,
1472 .close = cfhd_close,
1473 FF_CODEC_DECODE_CB(cfhd_decode),
1474 UPDATE_THREAD_CONTEXT(update_thread_context),
1475 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1476 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1477 };
1478