GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cfhd.c Lines: 455 973 46.8 %
Date: 2020-11-28 20:53:16 Branches: 286 662 43.2 %

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/buffer.h"
28
#include "libavutil/common.h"
29
#include "libavutil/imgutils.h"
30
#include "libavutil/intreadwrite.h"
31
#include "libavutil/opt.h"
32
33
#include "avcodec.h"
34
#include "bytestream.h"
35
#include "get_bits.h"
36
#include "internal.h"
37
#include "thread.h"
38
#include "cfhd.h"
39
40
#define ALPHA_COMPAND_DC_OFFSET 256
41
#define ALPHA_COMPAND_GAIN 9400
42
43
6
static av_cold int cfhd_init(AVCodecContext *avctx)
44
{
45
6
    CFHDContext *s = avctx->priv_data;
46
47
6
    s->avctx                   = avctx;
48
49
390
    for (int i = 0; i < 64; i++) {
50
384
        int val = i;
51
52
384
        if (val >= 40) {
53
144
            if (val >= 54) {
54
60
                val -= 54;
55
60
                val <<= 2;
56
60
                val += 54;
57
            }
58
59
144
            val -= 40;
60
144
            val <<= 2;
61
144
            val += 40;
62
        }
63
64
384
        s->lut[0][i] = val;
65
    }
66
67
1542
    for (int i = 0; i < 256; i++)
68
1536
        s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
69
70
6
    return ff_cfhd_init_vlcs(s);
71
}
72
73
99
static void init_plane_defaults(CFHDContext *s)
74
{
75
99
    s->subband_num        = 0;
76
99
    s->level              = 0;
77
99
    s->subband_num_actual = 0;
78
99
}
79
80
33
static void init_peak_table_defaults(CFHDContext *s)
81
{
82
33
    s->peak.level  = 0;
83
33
    s->peak.offset = 0;
84
33
    memset(&s->peak.base, 0, sizeof(s->peak.base));
85
33
}
86
87
33
static void init_frame_defaults(CFHDContext *s)
88
{
89
33
    s->coded_width       = 0;
90
33
    s->coded_height      = 0;
91
33
    s->coded_format      = AV_PIX_FMT_YUV422P10;
92
33
    s->cropped_height    = 0;
93
33
    s->bpc               = 10;
94
33
    s->channel_cnt       = 3;
95
33
    s->subband_cnt       = SUBBAND_COUNT;
96
33
    s->channel_num       = 0;
97
33
    s->lowpass_precision = 16;
98
33
    s->quantisation      = 1;
99
33
    s->codebook          = 0;
100
33
    s->difference_coding = 0;
101
33
    s->frame_type        = 0;
102
33
    s->sample_type       = 0;
103
33
    if (s->transform_type != 2)
104
33
        s->transform_type = -1;
105
33
    init_plane_defaults(s);
106
33
    init_peak_table_defaults(s);
107
33
}
108
109
3522977
static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
110
{
111

3522977
    if (codebook == 0 || codebook == 1) {
112
3522977
        return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
113
    } else
114
        return level * quantisation;
115
}
116
117
static inline void difference_coding(int16_t *band, int width, int height)
118
{
119
120
    int i,j;
121
    for (i = 0; i < height; i++) {
122
        for (j = 1; j < width; j++) {
123
          band[j] += band[j-1];
124
        }
125
        band += width;
126
    }
127
}
128
129
static inline void peak_table(int16_t *band, Peak *peak, int length)
130
{
131
    int i;
132
    for (i = 0; i < length; i++)
133
        if (abs(band[i]) > peak->level)
134
            band[i] = bytestream2_get_le16(&peak->base);
135
}
136
137
static inline void process_alpha(int16_t *alpha, int width)
138
{
139
    int i, channel;
140
    for (i = 0; i < width; i++) {
141
        channel   = alpha[i];
142
        channel  -= ALPHA_COMPAND_DC_OFFSET;
143
        channel <<= 3;
144
        channel  *= ALPHA_COMPAND_GAIN;
145
        channel >>= 16;
146
        channel   = av_clip_uintp2(channel, 12);
147
        alpha[i]  = channel;
148
    }
149
}
150
151
static inline void process_bayer(AVFrame *frame, int bpc)
152
{
153
    const int linesize = frame->linesize[0];
154
    uint16_t *r = (uint16_t *)frame->data[0];
155
    uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
156
    uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
157
    uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
158
    const int mid = 1 << (bpc - 1);
159
    const int factor = 1 << (16 - bpc);
160
161
    for (int y = 0; y < frame->height >> 1; y++) {
162
        for (int x = 0; x < frame->width; x += 2) {
163
            int R, G1, G2, B;
164
            int g, rg, bg, gd;
165
166
            g  = r[x];
167
            rg = g1[x];
168
            bg = g2[x];
169
            gd = b[x];
170
            gd -= mid;
171
172
            R  = (rg - mid) * 2 + g;
173
            G1 = g + gd;
174
            G2 = g - gd;
175
            B  = (bg - mid) * 2 + g;
176
177
            R  = av_clip_uintp2(R  * factor, 16);
178
            G1 = av_clip_uintp2(G1 * factor, 16);
179
            G2 = av_clip_uintp2(G2 * factor, 16);
180
            B  = av_clip_uintp2(B  * factor, 16);
181
182
            r[x]  = R;
183
            g1[x] = G1;
184
            g2[x] = G2;
185
            b[x]  = B;
186
        }
187
188
        r  += linesize;
189
        g1 += linesize;
190
        g2 += linesize;
191
        b  += linesize;
192
    }
193
}
194
195
static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
196
                         int width, int linesize, int plane)
197
{
198
    int i;
199
    int16_t even, odd;
200
    for (i = 0; i < width; i++) {
201
        even = (low[i] - high[i])/2;
202
        odd  = (low[i] + high[i])/2;
203
        output[i]            = av_clip_uintp2(even, 10);
204
        output[i + linesize] = av_clip_uintp2(odd, 10);
205
    }
206
}
207
208
static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
209
{
210
    for (int i = 0; i < width; i++) {
211
        int even = (low[i] - high[i]) / 2;
212
        int odd  = (low[i] + high[i]) / 2;
213
214
        low[i]  = even;
215
        high[i] = odd;
216
    }
217
}
218
219
12
static void free_buffers(CFHDContext *s)
220
{
221
    int i, j;
222
223
60
    for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
224
48
        av_freep(&s->plane[i].idwt_buf);
225
48
        av_freep(&s->plane[i].idwt_tmp);
226
48
        s->plane[i].idwt_size = 0;
227
228
864
        for (j = 0; j < SUBBAND_COUNT_3D; j++)
229
816
            s->plane[i].subband[j] = NULL;
230
231
528
        for (j = 0; j < 10; j++)
232
480
            s->plane[i].l_h[j] = NULL;
233
    }
234
12
    s->a_height = 0;
235
12
    s->a_width  = 0;
236
12
}
237
238
6
static int alloc_buffers(AVCodecContext *avctx)
239
{
240
6
    CFHDContext *s = avctx->priv_data;
241
6
    int i, j, ret, planes, bayer = 0;
242
    int chroma_x_shift, chroma_y_shift;
243
    unsigned k;
244
245
6
    if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
246
        return ret;
247
6
    avctx->pix_fmt = s->coded_format;
248
249
6
    ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
250
251
6
    if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
252
                                                &chroma_x_shift,
253
                                                &chroma_y_shift)) < 0)
254
        return ret;
255
6
    planes = av_pix_fmt_count_planes(s->coded_format);
256
6
    if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
257
        planes = 4;
258
        chroma_x_shift = 1;
259
        chroma_y_shift = 1;
260
        bayer = 1;
261
    }
262
263
24
    for (i = 0; i < planes; i++) {
264
        int w8, h8, w4, h4, w2, h2;
265

18
        int width  = (i || bayer) ? s->coded_width  >> chroma_x_shift : s->coded_width;
266

18
        int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
267
18
        ptrdiff_t stride = (FFALIGN(width  / 8, 8) + 64) * 8;
268
269

18
        if (chroma_y_shift && !bayer)
270
            height = FFALIGN(height / 8, 2) * 8;
271
18
        s->plane[i].width  = width;
272
18
        s->plane[i].height = height;
273
18
        s->plane[i].stride = stride;
274
275
18
        w8 = FFALIGN(s->plane[i].width  / 8, 8) + 64;
276
18
        h8 = FFALIGN(height, 8) / 8;
277
18
        w4 = w8 * 2;
278
18
        h4 = h8 * 2;
279
18
        w2 = w4 * 2;
280
18
        h2 = h4 * 2;
281
282
18
        if (s->transform_type == 0) {
283
18
            s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
284
18
            s->plane[i].idwt_buf =
285
18
                av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
286
18
            s->plane[i].idwt_tmp =
287
18
                av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
288
        } else {
289
            s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
290
            s->plane[i].idwt_buf =
291
                av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
292
            s->plane[i].idwt_tmp =
293
                av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
294
        }
295
296

18
        if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
297
            return AVERROR(ENOMEM);
298
299
18
        s->plane[i].subband[0] = s->plane[i].idwt_buf;
300
18
        s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
301
18
        s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
302
18
        s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
303
18
        s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
304
18
        s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
305
18
        s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
306
18
        if (s->transform_type == 0) {
307
18
            s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
308
18
            s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
309
18
            s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
310
        } else {
311
            int16_t *frame2 =
312
            s->plane[i].subband[7]  = s->plane[i].idwt_buf + 4 * w2 * h2;
313
            s->plane[i].subband[8]  = frame2 + 2 * w4 * h4;
314
            s->plane[i].subband[9]  = frame2 + 1 * w4 * h4;
315
            s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
316
            s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
317
            s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
318
            s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
319
            s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
320
            s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
321
            s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
322
        }
323
324
18
        if (s->transform_type == 0) {
325
72
            for (j = 0; j < DWT_LEVELS; j++) {
326
270
                for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
327
216
                    s->plane[i].band[j][k].a_width  = w8 << j;
328
216
                    s->plane[i].band[j][k].a_height = h8 << j;
329
                }
330
            }
331
        } else {
332
            for (j = 0; j < DWT_LEVELS_3D; j++) {
333
                int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
334
335
                for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
336
                    s->plane[i].band[j][k].a_width  = w8 << t;
337
                    s->plane[i].band[j][k].a_height = h8 << t;
338
                }
339
            }
340
        }
341
342
        /* ll2 and ll1 commented out because they are done in-place */
343
18
        s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
344
18
        s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
345
        // s->plane[i].l_h[2] = ll2;
346
18
        s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
347
18
        s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
348
        // s->plane[i].l_h[5] = ll1;
349
18
        s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
350
18
        s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
351
18
        if (s->transform_type != 0) {
352
            int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
353
354
            s->plane[i].l_h[8] = frame2;
355
            s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
356
        }
357
    }
358
359
6
    s->a_height = s->coded_height;
360
6
    s->a_width  = s->coded_width;
361
6
    s->a_format = s->coded_format;
362
363
6
    return 0;
364
}
365
366
33
static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
367
                       AVPacket *avpkt)
368
{
369
33
    CFHDContext *s = avctx->priv_data;
370
33
    CFHDDSPContext *dsp = &s->dsp;
371
    GetByteContext gb;
372
33
    ThreadFrame frame = { .f = data };
373
33
    AVFrame *pic = data;
374
33
    int ret = 0, i, j, plane, got_buffer = 0;
375
    int16_t *coeff_data;
376
377
33
    init_frame_defaults(s);
378
33
    s->planes = av_pix_fmt_count_planes(s->coded_format);
379
380
33
    bytestream2_init(&gb, avpkt->data, avpkt->size);
381
382
17226
    while (bytestream2_get_bytes_left(&gb) >= 4) {
383
        /* Bit weird but implement the tag parsing as the spec says */
384
17193
        uint16_t tagu   = bytestream2_get_be16(&gb);
385
17193
        int16_t tag     = (int16_t)tagu;
386
17193
        int8_t tag8     = (int8_t)(tagu >> 8);
387
17193
        uint16_t abstag = abs(tag);
388
17193
        int8_t abs_tag8 = abs(tag8);
389
17193
        uint16_t data   = bytestream2_get_be16(&gb);
390

17193
        if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
391
            av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
392
17193
        } else if (tag == SampleFlags) {
393
33
            av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
394
33
            s->progressive = data & 0x0001;
395
17160
        } else if (tag == FrameType) {
396
            s->frame_type = data;
397
            av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
398
17160
        } else if (abstag == VersionMajor) {
399
            av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
400
17160
        } else if (abstag == VersionMinor) {
401
            av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
402
17160
        } else if (abstag == VersionRevision) {
403
            av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
404
17160
        } else if (abstag == VersionEdit) {
405
            av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
406
17160
        } else if (abstag == Version) {
407
33
            av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
408
17127
        } else if (tag == ImageWidth) {
409
33
            av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
410
33
            s->coded_width = data;
411
17094
        } else if (tag == ImageHeight) {
412
33
            av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
413
33
            s->coded_height = data;
414
17061
        } else if (tag == ChannelCount) {
415
33
            av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
416
33
            s->channel_cnt = data;
417
33
            if (data > 4) {
418
                av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
419
                ret = AVERROR_PATCHWELCOME;
420
                goto end;
421
            }
422
17028
        } else if (tag == SubbandCount) {
423
33
            av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
424

33
            if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
425
                av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
426
                ret = AVERROR_PATCHWELCOME;
427
                goto end;
428
            }
429
16995
        } else if (tag == ChannelNumber) {
430
66
            s->channel_num = data;
431
66
            av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
432
66
            if (s->channel_num >= s->planes) {
433
                av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
434
                ret = AVERROR(EINVAL);
435
                goto end;
436
            }
437
66
            init_plane_defaults(s);
438
16929
        } else if (tag == SubbandNumber) {
439

891
            if (s->subband_num != 0 && data == 1)  // hack
440
198
                s->level++;
441
891
            av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
442
891
            s->subband_num = data;
443

891
            if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
444

891
                (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
445
                av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
446
                ret = AVERROR(EINVAL);
447
                goto end;
448
            }
449
891
            if (s->subband_num > 3) {
450
                av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
451
                ret = AVERROR(EINVAL);
452
                goto end;
453
            }
454
16038
        } else if (tag == SubbandBand) {
455
891
            av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
456

891
            if ((s->transform_type == 0 && data >= SUBBAND_COUNT) ||
457

891
                (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) {
458
                av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
459
                ret = AVERROR(EINVAL);
460
                goto end;
461
            }
462

891
            if (s->transform_type == 0 || s->transform_type == 2)
463
891
                s->subband_num_actual = data;
464
            else
465
                av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data);
466
15147
        } else if (tag == LowpassPrecision)
467
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
468
15048
        else if (tag == Quantization) {
469
891
            s->quantisation = data;
470
891
            av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
471
14157
        } else if (tag == PrescaleTable) {
472
99
            for (i = 0; i < 8; i++)
473
88
                s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
474
11
            av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
475
14146
        } else if (tag == BandEncoding) {
476

891
            if (!data || data > 5) {
477
                av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
478
                ret = AVERROR(EINVAL);
479
                goto end;
480
            }
481
891
            s->band_encoding = data;
482
891
            av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
483
13255
        } else if (tag == LowpassWidth) {
484
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
485
99
            s->plane[s->channel_num].band[0][0].width  = data;
486
99
            s->plane[s->channel_num].band[0][0].stride = data;
487
13156
        } else if (tag == LowpassHeight) {
488
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
489
99
            s->plane[s->channel_num].band[0][0].height = data;
490
13057
        } else if (tag == SampleType) {
491
99
            s->sample_type = data;
492
99
            av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
493
12958
        } else if (tag == TransformType) {
494
33
            if (data > 2) {
495
                av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
496
                ret = AVERROR(EINVAL);
497
                goto end;
498
33
            } else if (data == 1) {
499
                av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
500
                ret = AVERROR_PATCHWELCOME;
501
                goto end;
502
            }
503
33
            if (s->transform_type == -1) {
504
33
                s->transform_type = data;
505
33
                av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
506
            } else {
507
                av_log(avctx, AV_LOG_DEBUG, "Ignoring additional transform type %"PRIu16"\n", data);
508
            }
509

12925
        } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
510
66
            if (abstag == 0x4001)
511
                s->peak.level = 0;
512
66
            av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
513
66
            bytestream2_skipu(&gb, data * 4);
514
12859
        } else if (tag == FrameIndex) {
515
            av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
516
            s->frame_index = data;
517
12859
        } else if (tag == SampleIndexTable) {
518
33
            av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
519
33
            if (data > bytestream2_get_bytes_left(&gb) / 4) {
520
                av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
521
                ret = AVERROR_INVALIDDATA;
522
                goto end;
523
            }
524
132
            for (i = 0; i < data; i++) {
525
99
                uint32_t offset = bytestream2_get_be32(&gb);
526
99
                av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
527
            }
528
12826
        } else if (tag == HighpassWidth) {
529
297
            av_log(avctx, AV_LOG_DEBUG, "Highpass width %i channel %i level %i subband %i\n", data, s->channel_num, s->level, s->subband_num);
530
297
            if (data < 3) {
531
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
532
                ret = AVERROR(EINVAL);
533
                goto end;
534
            }
535
297
            s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
536
297
            s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
537
12529
        } else if (tag == HighpassHeight) {
538
297
            av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
539
297
            if (data < 3) {
540
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
541
                ret = AVERROR(EINVAL);
542
                goto end;
543
            }
544
297
            s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
545
12232
        } else if (tag == BandWidth) {
546
891
            av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
547
891
            if (data < 3) {
548
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
549
                ret = AVERROR(EINVAL);
550
                goto end;
551
            }
552
891
            s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
553
891
            s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
554
11341
        } else if (tag == BandHeight) {
555
891
            av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
556
891
            if (data < 3) {
557
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
558
                ret = AVERROR(EINVAL);
559
                goto end;
560
            }
561
891
            s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
562
10450
        } else if (tag == InputFormat) {
563
            av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
564
            if (s->coded_format == AV_PIX_FMT_NONE ||
565
                s->coded_format == AV_PIX_FMT_YUV422P10) {
566
                if (data >= 100 && data <= 105) {
567
                    s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
568
                } else if (data >= 122 && data <= 128) {
569
                    s->coded_format = AV_PIX_FMT_GBRP12;
570
                } else if (data == 30) {
571
                    s->coded_format = AV_PIX_FMT_GBRAP12;
572
                } else {
573
                    s->coded_format = AV_PIX_FMT_YUV422P10;
574
                }
575
                s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
576
            }
577
10450
        } else if (tag == BandCodingFlags) {
578
891
            s->codebook = data & 0xf;
579
891
            s->difference_coding = (data >> 4) & 1;
580
891
            av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
581
9559
        } else if (tag == Precision) {
582
33
            av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
583

33
            if (!(data == 10 || data == 12)) {
584
                av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
585
                ret = AVERROR(EINVAL);
586
                goto end;
587
            }
588
33
            avctx->bits_per_raw_sample = s->bpc = data;
589
9526
        } else if (tag == EncodedFormat) {
590
33
            av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
591
33
            if (data == 1) {
592
22
                s->coded_format = AV_PIX_FMT_YUV422P10;
593
11
            } else if (data == 2) {
594
                s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
595
11
            } else if (data == 3) {
596
11
                s->coded_format = AV_PIX_FMT_GBRP12;
597
            } else if (data == 4) {
598
                s->coded_format = AV_PIX_FMT_GBRAP12;
599
            } else {
600
                avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
601
                ret = AVERROR_PATCHWELCOME;
602
                goto end;
603
            }
604
33
            s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
605
9493
        } else if (tag == -DisplayHeight) {
606
33
            av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
607
33
            s->cropped_height = data;
608
9460
        } else if (tag == -PeakOffsetLow) {
609
            s->peak.offset &= ~0xffff;
610
            s->peak.offset |= (data & 0xffff);
611
            s->peak.base    = gb;
612
            s->peak.level   = 0;
613
9460
        } else if (tag == -PeakOffsetHigh) {
614
            s->peak.offset &= 0xffff;
615
            s->peak.offset |= (data & 0xffffU)<<16;
616
            s->peak.base    = gb;
617
            s->peak.level   = 0;
618

9460
        } else if (tag == -PeakLevel && s->peak.offset) {
619
            s->peak.level = data;
620
            bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
621
        } else
622
9460
            av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
623
624

17193
        if (tag == BitstreamMarker && data == 0xf0f &&
625
99
            s->coded_format != AV_PIX_FMT_NONE) {
626
33
            int lowpass_height = s->plane[s->channel_num].band[0][0].height;
627
33
            int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
628
33
            int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
629
630
33
            if (s->coded_width) {
631
33
                s->coded_width *= factor;
632
            }
633
634
33
            if (s->coded_height) {
635
33
                s->coded_height *= factor;
636
            }
637
638

33
            if (!s->a_width && !s->coded_width) {
639
                s->coded_width = lowpass_width * factor * 8;
640
            }
641
642

33
            if (!s->a_height && !s->coded_height) {
643
                s->coded_height = lowpass_height * factor * 8;
644
            }
645
646

33
            if (s->a_width && !s->coded_width)
647
                s->coded_width = s->a_width;
648

33
            if (s->a_height && !s->coded_height)
649
                s->coded_height = s->a_height;
650
651

33
            if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
652
27
                s->a_format != s->coded_format) {
653
6
                free_buffers(s);
654
6
                if ((ret = alloc_buffers(avctx)) < 0) {
655
                    free_buffers(s);
656
                    return ret;
657
                }
658
            }
659
33
            ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
660
33
            if (ret < 0)
661
                return ret;
662
33
            if (s->cropped_height) {
663
33
                unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
664
33
                if (avctx->height < height)
665
                    return AVERROR_INVALIDDATA;
666
33
                avctx->height = height;
667
            }
668
33
            frame.f->width =
669
33
            frame.f->height = 0;
670
671
33
            if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
672
                return ret;
673
674
33
            s->coded_width = 0;
675
33
            s->coded_height = 0;
676
33
            s->coded_format = AV_PIX_FMT_NONE;
677
33
            got_buffer = 1;
678


17160
        } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
679
            frame.f->width =
680
            frame.f->height = 0;
681
682
            if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
683
                return ret;
684
            s->coded_width = 0;
685
            s->coded_height = 0;
686
            s->coded_format = AV_PIX_FMT_NONE;
687
            got_buffer = 1;
688
        }
689
690
17193
        if (s->subband_num_actual == 255)
691
            goto finish;
692
17193
        coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
693
694
        /* Lowpass coefficients */
695


17193
        if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
696
99
            int lowpass_height = s->plane[s->channel_num].band[0][0].height;
697
99
            int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
698
99
            int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
699
99
            int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
700
701

99
            if (lowpass_width < 3 ||
702
                lowpass_width > lowpass_a_width) {
703
                av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
704
                ret = AVERROR(EINVAL);
705
                goto end;
706
            }
707
708

99
            if (lowpass_height < 3 ||
709
                lowpass_height > lowpass_a_height) {
710
                av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
711
                ret = AVERROR(EINVAL);
712
                goto end;
713
            }
714
715
99
            if (!got_buffer) {
716
                av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
717
                ret = AVERROR(EINVAL);
718
                goto end;
719
            }
720
721

99
            if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
722
99
                lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
723
                av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
724
                ret = AVERROR(EINVAL);
725
                goto end;
726
            }
727
728
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);
729
5082
            for (i = 0; i < lowpass_height; i++) {
730
344267
                for (j = 0; j < lowpass_width; j++)
731
339284
                    coeff_data[j] = bytestream2_get_be16u(&gb);
732
733
4983
                coeff_data += lowpass_width;
734
            }
735
736
            /* Align to mod-4 position to continue reading tags */
737
99
            bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
738
739
            /* Copy last line of coefficients if odd height */
740
99
            if (lowpass_height & 1) {
741
33
                memcpy(&coeff_data[lowpass_height * lowpass_width],
742
33
                       &coeff_data[(lowpass_height - 1) * lowpass_width],
743
                       lowpass_width * sizeof(*coeff_data));
744
            }
745
746
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
747
        }
748
749


17193
        if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
750
891
            int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
751
891
            int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
752
891
            int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
753
891
            int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
754
891
            int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
755
            int expected;
756
891
            int a_expected = highpass_a_height * highpass_a_width;
757
            int level, run, coeff;
758
891
            int count = 0, bytes;
759
760
891
            if (!got_buffer) {
761
                av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
762
                ret = AVERROR(EINVAL);
763
                goto end;
764
            }
765
766

891
            if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
767
                av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
768
                ret = AVERROR(EINVAL);
769
                goto end;
770
            }
771
891
            expected = highpass_height * highpass_stride;
772
773
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);
774
775
891
            ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
776
891
            if (ret < 0)
777
                goto end;
778
            {
779
891
                OPEN_READER(re, &s->gb);
780
781
891
                const int lossless = s->band_encoding == 5;
782
783

891
                if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
784
                    s->codebook = 1;
785
891
                if (!s->codebook) {
786
                    while (1) {
787
                        UPDATE_CACHE(re, &s->gb);
788
                        GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
789
                                   VLC_BITS, 3, 1);
790
791
                        /* escape */
792
                        if (level == 64)
793
                            break;
794
795
                        count += run;
796
797
                        if (count > expected)
798
                            break;
799
800
                        if (!lossless)
801
                            coeff = dequant_and_decompand(s, level, s->quantisation, 0);
802
                        else
803
                            coeff = level;
804
                        if (tag == BandSecondPass) {
805
                            const uint16_t q = s->quantisation;
806
807
                            for (i = 0; i < run; i++) {
808
                                *coeff_data |= coeff << 8;
809
                                *coeff_data++ *= q;
810
                            }
811
                        } else {
812
                            for (i = 0; i < run; i++)
813
                                *coeff_data++ = coeff;
814
                        }
815
                    }
816
                } else {
817
                    while (1) {
818
3523868
                        UPDATE_CACHE(re, &s->gb);
819

3523868
                        GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
820
                                   VLC_BITS, 3, 1);
821
822
                        /* escape */
823

3523868
                        if (level == 255 && run == 2)
824
891
                            break;
825
826
3522977
                        count += run;
827
828
3522977
                        if (count > expected)
829
                            break;
830
831
3522977
                        if (!lossless)
832
3522977
                            coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
833
                        else
834
                            coeff = level;
835
3522977
                        if (tag == BandSecondPass) {
836
                            const uint16_t q = s->quantisation;
837
838
                            for (i = 0; i < run; i++) {
839
                                *coeff_data |= coeff << 8;
840
                                *coeff_data++ *= q;
841
                            }
842
                        } else {
843
25184705
                            for (i = 0; i < run; i++)
844
21661728
                                *coeff_data++ = coeff;
845
                        }
846
                    }
847
                }
848
891
                CLOSE_READER(re, &s->gb);
849
            }
850
851
891
            if (count > expected) {
852
                av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
853
                ret = AVERROR(EINVAL);
854
                goto end;
855
            }
856
891
            if (s->peak.level)
857
                peak_table(coeff_data - count, &s->peak, count);
858
891
            if (s->difference_coding)
859
                difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
860
861
891
            bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
862
891
            if (bytes > bytestream2_get_bytes_left(&gb)) {
863
                av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
864
                ret = AVERROR(EINVAL);
865
                goto end;
866
            } else
867
891
                bytestream2_seek(&gb, bytes, SEEK_CUR);
868
869
891
            av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
870
891
finish:
871
891
            if (s->subband_num_actual != 255)
872
891
                s->codebook = 0;
873
        }
874
    }
875
876
33
    s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
877
33
    if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
878
        s->progressive = 1;
879
        s->planes = 4;
880
    }
881
882
33
    ff_thread_finish_setup(avctx);
883
884

33
    if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
885

33
        s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
886
        av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
887
        ret = AVERROR(EINVAL);
888
        goto end;
889
    }
890
891
33
    if (!got_buffer) {
892
        av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
893
        ret = AVERROR(EINVAL);
894
        goto end;
895
    }
896
897

33
    if (s->transform_type == 0 && s->sample_type != 1) {
898

132
        for (plane = 0; plane < s->planes && !ret; plane++) {
899
            /* level 1 */
900
99
            int lowpass_height  = s->plane[plane].band[0][0].height;
901
99
            int output_stride   = s->plane[plane].band[0][0].a_width;
902
99
            int lowpass_width   = s->plane[plane].band[0][0].width;
903
99
            int highpass_stride = s->plane[plane].band[0][1].stride;
904

99
            int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
905
            ptrdiff_t dst_linesize;
906
            int16_t *low, *high, *output, *dst;
907
908
99
            if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
909
                act_plane = 0;
910
                dst_linesize = pic->linesize[act_plane];
911
            } else {
912
99
                dst_linesize = pic->linesize[act_plane] / 2;
913
            }
914
915

99
            if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
916

99
                !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
917
99
                lowpass_width < 3 || lowpass_height < 3) {
918
                av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
919
                ret = AVERROR(EINVAL);
920
                goto end;
921
            }
922
923
99
            av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
924
925
99
            low    = s->plane[plane].subband[0];
926
99
            high   = s->plane[plane].subband[2];
927
99
            output = s->plane[plane].l_h[0];
928
99
            dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
929
930
99
            low    = s->plane[plane].subband[1];
931
99
            high   = s->plane[plane].subband[3];
932
99
            output = s->plane[plane].l_h[1];
933
934
99
            dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
935
936
99
            low    = s->plane[plane].l_h[0];
937
99
            high   = s->plane[plane].l_h[1];
938
99
            output = s->plane[plane].subband[0];
939
99
            dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
940
99
            if (s->bpc == 12) {
941
33
                output = s->plane[plane].subband[0];
942
3993
                for (i = 0; i < lowpass_height * 2; i++) {
943
716760
                    for (j = 0; j < lowpass_width * 2; j++)
944
712800
                        output[j] *= 4;
945
946
3960
                    output += output_stride * 2;
947
                }
948
            }
949
950
            /* level 2 */
951
99
            lowpass_height  = s->plane[plane].band[1][1].height;
952
99
            output_stride   = s->plane[plane].band[1][1].a_width;
953
99
            lowpass_width   = s->plane[plane].band[1][1].width;
954
99
            highpass_stride = s->plane[plane].band[1][1].stride;
955
956

99
            if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
957

99
                !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
958
99
                lowpass_width < 3 || lowpass_height < 3) {
959
                av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
960
                ret = AVERROR(EINVAL);
961
                goto end;
962
            }
963
964
99
            av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
965
966
99
            low    = s->plane[plane].subband[0];
967
99
            high   = s->plane[plane].subband[5];
968
99
            output = s->plane[plane].l_h[3];
969
99
            dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
970
971
99
            low    = s->plane[plane].subband[4];
972
99
            high   = s->plane[plane].subband[6];
973
99
            output = s->plane[plane].l_h[4];
974
99
            dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
975
976
99
            low    = s->plane[plane].l_h[3];
977
99
            high   = s->plane[plane].l_h[4];
978
99
            output = s->plane[plane].subband[0];
979
99
            dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
980
981
99
            output = s->plane[plane].subband[0];
982
20031
            for (i = 0; i < lowpass_height * 2; i++) {
983
5448476
                for (j = 0; j < lowpass_width * 2; j++)
984
5428544
                    output[j] *= 4;
985
986
19932
                output += output_stride * 2;
987
            }
988
989
            /* level 3 */
990
99
            lowpass_height  = s->plane[plane].band[2][1].height;
991
99
            output_stride   = s->plane[plane].band[2][1].a_width;
992
99
            lowpass_width   = s->plane[plane].band[2][1].width;
993
99
            highpass_stride = s->plane[plane].band[2][1].stride;
994
995

99
            if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
996

99
                !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
997

99
                lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
998
                av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
999
                ret = AVERROR(EINVAL);
1000
                goto end;
1001
            }
1002
1003
99
            av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1004
99
            if (s->progressive) {
1005
99
                low    = s->plane[plane].subband[0];
1006
99
                high   = s->plane[plane].subband[8];
1007
99
                output = s->plane[plane].l_h[6];
1008
99
                dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1009
1010
99
                low    = s->plane[plane].subband[7];
1011
99
                high   = s->plane[plane].subband[9];
1012
99
                output = s->plane[plane].l_h[7];
1013
99
                dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1014
1015
99
                dst = (int16_t *)pic->data[act_plane];
1016
99
                if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1017
                    if (plane & 1)
1018
                        dst++;
1019
                    if (plane > 1)
1020
                        dst += pic->linesize[act_plane] >> 1;
1021
                }
1022
99
                low  = s->plane[plane].l_h[6];
1023
99
                high = s->plane[plane].l_h[7];
1024
1025
99
                if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1026
                    (lowpass_height * 2 > avctx->coded_height / 2 ||
1027
                     lowpass_width  * 2 > avctx->coded_width  / 2    )
1028
                    ) {
1029
                    ret = AVERROR_INVALIDDATA;
1030
                    goto end;
1031
                }
1032
1033
39963
                for (i = 0; i < s->plane[act_plane].height; i++) {
1034
39864
                    dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1035

39864
                    if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1036
                        process_alpha(dst, lowpass_width * 2);
1037
39864
                    low  += output_stride;
1038
39864
                    high += output_stride;
1039
39864
                    dst  += dst_linesize;
1040
                }
1041
            } else {
1042
                av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1043
                pic->interlaced_frame = 1;
1044
                low    = s->plane[plane].subband[0];
1045
                high   = s->plane[plane].subband[7];
1046
                output = s->plane[plane].l_h[6];
1047
                dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1048
1049
                low    = s->plane[plane].subband[8];
1050
                high   = s->plane[plane].subband[9];
1051
                output = s->plane[plane].l_h[7];
1052
                dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1053
1054
                dst  = (int16_t *)pic->data[act_plane];
1055
                low  = s->plane[plane].l_h[6];
1056
                high = s->plane[plane].l_h[7];
1057
                for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1058
                    interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1059
                    low  += output_stride * 2;
1060
                    high += output_stride * 2;
1061
                    dst  += pic->linesize[act_plane];
1062
                }
1063
            }
1064
        }
1065
    } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1066
        for (plane = 0; plane < s->planes && !ret; plane++) {
1067
            int lowpass_height  = s->plane[plane].band[0][0].height;
1068
            int output_stride   = s->plane[plane].band[0][0].a_width;
1069
            int lowpass_width   = s->plane[plane].band[0][0].width;
1070
            int highpass_stride = s->plane[plane].band[0][1].stride;
1071
            int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1072
            int16_t *low, *high, *output, *dst;
1073
            ptrdiff_t dst_linesize;
1074
1075
            if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1076
                act_plane = 0;
1077
                dst_linesize = pic->linesize[act_plane];
1078
            } else {
1079
                dst_linesize = pic->linesize[act_plane] / 2;
1080
            }
1081
1082
            if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1083
                !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1084
                lowpass_width < 3 || lowpass_height < 3) {
1085
                av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1086
                ret = AVERROR(EINVAL);
1087
                goto end;
1088
            }
1089
1090
            av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1091
1092
            low    = s->plane[plane].subband[0];
1093
            high   = s->plane[plane].subband[2];
1094
            output = s->plane[plane].l_h[0];
1095
            dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1096
1097
            low    = s->plane[plane].subband[1];
1098
            high   = s->plane[plane].subband[3];
1099
            output = s->plane[plane].l_h[1];
1100
            dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1101
1102
            low    = s->plane[plane].l_h[0];
1103
            high   = s->plane[plane].l_h[1];
1104
            output = s->plane[plane].l_h[7];
1105
            dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1106
            if (s->bpc == 12) {
1107
                output = s->plane[plane].l_h[7];
1108
                for (i = 0; i < lowpass_height * 2; i++) {
1109
                    for (j = 0; j < lowpass_width * 2; j++)
1110
                        output[j] *= 4;
1111
1112
                    output += output_stride * 2;
1113
                }
1114
            }
1115
1116
            lowpass_height  = s->plane[plane].band[1][1].height;
1117
            output_stride   = s->plane[plane].band[1][1].a_width;
1118
            lowpass_width   = s->plane[plane].band[1][1].width;
1119
            highpass_stride = s->plane[plane].band[1][1].stride;
1120
1121
            if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1122
                !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1123
                lowpass_width < 3 || lowpass_height < 3) {
1124
                av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1125
                ret = AVERROR(EINVAL);
1126
                goto end;
1127
            }
1128
1129
            av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1130
1131
            low    = s->plane[plane].l_h[7];
1132
            high   = s->plane[plane].subband[5];
1133
            output = s->plane[plane].l_h[3];
1134
            dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1135
1136
            low    = s->plane[plane].subband[4];
1137
            high   = s->plane[plane].subband[6];
1138
            output = s->plane[plane].l_h[4];
1139
            dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1140
1141
            low    = s->plane[plane].l_h[3];
1142
            high   = s->plane[plane].l_h[4];
1143
            output = s->plane[plane].l_h[7];
1144
            dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1145
1146
            output = s->plane[plane].l_h[7];
1147
            for (i = 0; i < lowpass_height * 2; i++) {
1148
                for (j = 0; j < lowpass_width * 2; j++)
1149
                    output[j] *= 4;
1150
                output += output_stride * 2;
1151
            }
1152
1153
            low    = s->plane[plane].subband[7];
1154
            high   = s->plane[plane].subband[9];
1155
            output = s->plane[plane].l_h[3];
1156
            dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1157
1158
            low    = s->plane[plane].subband[8];
1159
            high   = s->plane[plane].subband[10];
1160
            output = s->plane[plane].l_h[4];
1161
            dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1162
1163
            low    = s->plane[plane].l_h[3];
1164
            high   = s->plane[plane].l_h[4];
1165
            output = s->plane[plane].l_h[9];
1166
            dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1167
1168
            lowpass_height  = s->plane[plane].band[4][1].height;
1169
            output_stride   = s->plane[plane].band[4][1].a_width;
1170
            lowpass_width   = s->plane[plane].band[4][1].width;
1171
            highpass_stride = s->plane[plane].band[4][1].stride;
1172
            av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1173
1174
            if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1175
                !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1176
                lowpass_width < 3 || lowpass_height < 3) {
1177
                av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1178
                ret = AVERROR(EINVAL);
1179
                goto end;
1180
            }
1181
1182
            low    = s->plane[plane].l_h[7];
1183
            high   = s->plane[plane].l_h[9];
1184
            output = s->plane[plane].l_h[7];
1185
            for (i = 0; i < lowpass_height; i++) {
1186
                inverse_temporal_filter(low, high, lowpass_width);
1187
                low    += output_stride;
1188
                high   += output_stride;
1189
            }
1190
            if (s->progressive) {
1191
                low    = s->plane[plane].l_h[7];
1192
                high   = s->plane[plane].subband[15];
1193
                output = s->plane[plane].l_h[6];
1194
                dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1195
1196
                low    = s->plane[plane].subband[14];
1197
                high   = s->plane[plane].subband[16];
1198
                output = s->plane[plane].l_h[7];
1199
                dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1200
1201
                low    = s->plane[plane].l_h[9];
1202
                high   = s->plane[plane].subband[12];
1203
                output = s->plane[plane].l_h[8];
1204
                dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1205
1206
                low    = s->plane[plane].subband[11];
1207
                high   = s->plane[plane].subband[13];
1208
                output = s->plane[plane].l_h[9];
1209
                dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1210
1211
                if (s->sample_type == 1)
1212
                    continue;
1213
1214
                dst = (int16_t *)pic->data[act_plane];
1215
                if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1216
                    if (plane & 1)
1217
                        dst++;
1218
                    if (plane > 1)
1219
                        dst += pic->linesize[act_plane] >> 1;
1220
                }
1221
1222
                if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1223
                    (lowpass_height * 2 > avctx->coded_height / 2 ||
1224
                     lowpass_width  * 2 > avctx->coded_width  / 2    )
1225
                    ) {
1226
                    ret = AVERROR_INVALIDDATA;
1227
                    goto end;
1228
                }
1229
1230
                low  = s->plane[plane].l_h[6];
1231
                high = s->plane[plane].l_h[7];
1232
                for (i = 0; i < s->plane[act_plane].height; i++) {
1233
                    dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1234
                    low  += output_stride;
1235
                    high += output_stride;
1236
                    dst  += dst_linesize;
1237
                }
1238
            } else {
1239
                pic->interlaced_frame = 1;
1240
                low    = s->plane[plane].l_h[7];
1241
                high   = s->plane[plane].subband[14];
1242
                output = s->plane[plane].l_h[6];
1243
                dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1244
1245
                low    = s->plane[plane].subband[15];
1246
                high   = s->plane[plane].subband[16];
1247
                output = s->plane[plane].l_h[7];
1248
                dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1249
1250
                low    = s->plane[plane].l_h[9];
1251
                high   = s->plane[plane].subband[11];
1252
                output = s->plane[plane].l_h[8];
1253
                dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1254
1255
                low    = s->plane[plane].subband[12];
1256
                high   = s->plane[plane].subband[13];
1257
                output = s->plane[plane].l_h[9];
1258
                dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1259
1260
                if (s->sample_type == 1)
1261
                    continue;
1262
1263
                dst  = (int16_t *)pic->data[act_plane];
1264
                low  = s->plane[plane].l_h[6];
1265
                high = s->plane[plane].l_h[7];
1266
                for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1267
                    interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1268
                    low  += output_stride * 2;
1269
                    high += output_stride * 2;
1270
                    dst  += pic->linesize[act_plane];
1271
                }
1272
            }
1273
        }
1274
    }
1275
1276

33
    if (s->transform_type == 2 && s->sample_type == 1) {
1277
        int16_t *low, *high, *dst;
1278
        int output_stride, lowpass_height, lowpass_width;
1279
        ptrdiff_t dst_linesize;
1280
1281
        for (plane = 0; plane < s->planes; plane++) {
1282
            int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1283
1284
            if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1285
                act_plane = 0;
1286
                dst_linesize = pic->linesize[act_plane];
1287
            } else {
1288
                dst_linesize = pic->linesize[act_plane] / 2;
1289
            }
1290
1291
            lowpass_height  = s->plane[plane].band[4][1].height;
1292
            output_stride   = s->plane[plane].band[4][1].a_width;
1293
            lowpass_width   = s->plane[plane].band[4][1].width;
1294
1295
            if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1296
                s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1297
                lowpass_width < 3 || lowpass_height < 3) {
1298
                av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1299
                ret = AVERROR(EINVAL);
1300
                goto end;
1301
            }
1302
1303
            if (s->progressive) {
1304
                dst = (int16_t *)pic->data[act_plane];
1305
                low  = s->plane[plane].l_h[8];
1306
                high = s->plane[plane].l_h[9];
1307
1308
                if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1309
                    if (plane & 1)
1310
                        dst++;
1311
                    if (plane > 1)
1312
                        dst += pic->linesize[act_plane] >> 1;
1313
                }
1314
1315
                if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1316
                    (lowpass_height * 2 > avctx->coded_height / 2 ||
1317
                     lowpass_width  * 2 > avctx->coded_width  / 2    )
1318
                    ) {
1319
                    ret = AVERROR_INVALIDDATA;
1320
                    goto end;
1321
                }
1322
1323
                for (i = 0; i < s->plane[act_plane].height; i++) {
1324
                    dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1325
                    low  += output_stride;
1326
                    high += output_stride;
1327
                    dst  += dst_linesize;
1328
                }
1329
            } else {
1330
                dst  = (int16_t *)pic->data[act_plane];
1331
                low  = s->plane[plane].l_h[8];
1332
                high = s->plane[plane].l_h[9];
1333
                for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1334
                    interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1335
                    low  += output_stride * 2;
1336
                    high += output_stride * 2;
1337
                    dst  += pic->linesize[act_plane];
1338
                }
1339
            }
1340
        }
1341
    }
1342
1343
33
    if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1344
        process_bayer(pic, s->bpc);
1345
33
end:
1346
33
    if (ret < 0)
1347
        return ret;
1348
1349
33
    *got_frame = 1;
1350
33
    return avpkt->size;
1351
}
1352
1353
6
static av_cold int cfhd_close(AVCodecContext *avctx)
1354
{
1355
6
    CFHDContext *s = avctx->priv_data;
1356
1357
6
    free_buffers(s);
1358
1359
6
    ff_free_vlc(&s->vlc_9);
1360
6
    ff_free_vlc(&s->vlc_18);
1361
1362
6
    return 0;
1363
}
1364
1365
#if HAVE_THREADS
1366
static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1367
{
1368
    CFHDContext *psrc = src->priv_data;
1369
    CFHDContext *pdst = dst->priv_data;
1370
    int ret;
1371
1372
    if (dst == src || psrc->transform_type == 0)
1373
        return 0;
1374
1375
    pdst->a_format = psrc->a_format;
1376
    pdst->a_width  = psrc->a_width;
1377
    pdst->a_height = psrc->a_height;
1378
    pdst->transform_type = psrc->transform_type;
1379
    pdst->progressive = psrc->progressive;
1380
    pdst->planes = psrc->planes;
1381
1382
    if (!pdst->plane[0].idwt_buf) {
1383
        pdst->coded_width  = pdst->a_width;
1384
        pdst->coded_height = pdst->a_height;
1385
        pdst->coded_format = pdst->a_format;
1386
        ret = alloc_buffers(dst);
1387
        if (ret < 0)
1388
            return ret;
1389
    }
1390
1391
    for (int plane = 0; plane < pdst->planes; plane++) {
1392
        memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1393
        memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1394
               pdst->plane[plane].idwt_size * sizeof(int16_t));
1395
    }
1396
1397
    return 0;
1398
}
1399
#endif
1400
1401
AVCodec ff_cfhd_decoder = {
1402
    .name             = "cfhd",
1403
    .long_name        = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1404
    .type             = AVMEDIA_TYPE_VIDEO,
1405
    .id               = AV_CODEC_ID_CFHD,
1406
    .priv_data_size   = sizeof(CFHDContext),
1407
    .init             = cfhd_init,
1408
    .close            = cfhd_close,
1409
    .decode           = cfhd_decode,
1410
    .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1411
    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1412
    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1413
};