Coverage Report

Created: 2026-10-28 08:13

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