FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cfhd.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 470 1013 46.4%
Functions: 9 16 56.2%
Branches: 299 700 42.7%

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