GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cfhd.c Lines: 464 704 65.9 %
Date: 2019-11-18 18:00:01 Branches: 260 414 62.8 %

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
enum CFHDParam {
44
    ChannelCount     =  12,
45
    SubbandCount     =  14,
46
    ImageWidth       =  20,
47
    ImageHeight      =  21,
48
    LowpassPrecision =  35,
49
    SubbandNumber    =  48,
50
    Quantization     =  53,
51
    ChannelNumber    =  62,
52
    SampleFlags      =  68,
53
    BitsPerComponent = 101,
54
    ChannelWidth     = 104,
55
    ChannelHeight    = 105,
56
    PrescaleShift    = 109,
57
};
58
59
60
61
6
static av_cold int cfhd_init(AVCodecContext *avctx)
62
{
63
6
    CFHDContext *s = avctx->priv_data;
64
65
6
    avctx->bits_per_raw_sample = 10;
66
6
    s->avctx                   = avctx;
67
68
6
    return ff_cfhd_init_vlcs(s);
69
}
70
71
99
static void init_plane_defaults(CFHDContext *s)
72
{
73
99
    s->subband_num        = 0;
74
99
    s->level              = 0;
75
99
    s->subband_num_actual = 0;
76
99
}
77
78
33
static void init_peak_table_defaults(CFHDContext *s)
79
{
80
33
    s->peak.level  = 0;
81
33
    s->peak.offset = 0;
82
33
    memset(&s->peak.base, 0, sizeof(s->peak.base));
83
33
}
84
85
33
static void init_frame_defaults(CFHDContext *s)
86
{
87
33
    s->coded_width       = 0;
88
33
    s->coded_height      = 0;
89
33
    s->cropped_height    = 0;
90
33
    s->bpc               = 10;
91
33
    s->channel_cnt       = 4;
92
33
    s->subband_cnt       = SUBBAND_COUNT;
93
33
    s->channel_num       = 0;
94
33
    s->lowpass_precision = 16;
95
33
    s->quantisation      = 1;
96
33
    s->wavelet_depth     = 3;
97
33
    s->pshift            = 1;
98
33
    s->codebook          = 0;
99
33
    s->difference_coding = 0;
100
33
    s->progressive       = 0;
101
33
    init_plane_defaults(s);
102
33
    init_peak_table_defaults(s);
103
33
}
104
105
/* TODO: merge with VLC tables or use LUT */
106
3522977
static inline int dequant_and_decompand(int level, int quantisation, int codebook)
107
{
108

3522977
    if (codebook == 0 || codebook == 1) {
109
3522977
        int64_t abslevel = abs(level);
110
3522977
        if (level < 264)
111
3522977
            return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
112
3522977
               FFSIGN(level) * quantisation;
113
        else
114
            return level * quantisation;
115
    } else
116
        return level * quantisation;
117
}
118
119
static inline void difference_coding(int16_t *band, int width, int height)
120
{
121
122
    int i,j;
123
    for (i = 0; i < height; i++) {
124
        for (j = 1; j < width; j++) {
125
          band[j] += band[j-1];
126
        }
127
        band += width;
128
    }
129
}
130
131
static inline void peak_table(int16_t *band, Peak *peak, int length)
132
{
133
    int i;
134
    for (i = 0; i < length; i++)
135
        if (abs(band[i]) > peak->level)
136
            band[i] = bytestream2_get_le16(&peak->base);
137
}
138
139
static inline void process_alpha(int16_t *alpha, int width)
140
{
141
    int i, channel;
142
    for (i = 0; i < width; i++) {
143
        channel   = alpha[i];
144
        channel  -= ALPHA_COMPAND_DC_OFFSET;
145
        channel <<= 3;
146
        channel  *= ALPHA_COMPAND_GAIN;
147
        channel >>= 16;
148
        channel   = av_clip_uintp2(channel, 12);
149
        alpha[i]  = channel;
150
    }
151
}
152
153
static inline void process_bayer(AVFrame *frame)
154
{
155
    const int linesize = frame->linesize[0];
156
    uint16_t *r = (uint16_t *)frame->data[0];
157
    uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
158
    uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
159
    uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
160
    const int mid = 2048;
161
162
    for (int y = 0; y < frame->height >> 1; y++) {
163
        for (int x = 0; x < frame->width; x += 2) {
164
            int R, G1, G2, B;
165
            int g, rg, bg, gd;
166
167
            g  = r[x];
168
            rg = g1[x];
169
            bg = g2[x];
170
            gd = b[x];
171
            gd -= mid;
172
173
            R  = (rg - mid) * 2 + g;
174
            G1 = g + gd;
175
            G2 = g - gd;
176
            B  = (bg - mid) * 2 + g;
177
178
            R  = av_clip_uintp2(R  * 16, 16);
179
            G1 = av_clip_uintp2(G1 * 16, 16);
180
            G2 = av_clip_uintp2(G2 * 16, 16);
181
            B  = av_clip_uintp2(B  * 16, 16);
182
183
            r[x]  = R;
184
            g1[x] = G1;
185
            g2[x] = G2;
186
            b[x]  = B;
187
        }
188
189
        r  += linesize;
190
        g1 += linesize;
191
        g2 += linesize;
192
        b  += linesize;
193
    }
194
}
195
196
158158
static inline void filter(int16_t *output, ptrdiff_t out_stride,
197
                          int16_t *low, ptrdiff_t low_stride,
198
                          int16_t *high, ptrdiff_t high_stride,
199
                          int len, int clip)
200
{
201
    int16_t tmp;
202
    int i;
203
204
28658014
    for (i = 0; i < len; i++) {
205
28499856
        if (i == 0) {
206
158158
            tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
207
158158
            output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
208
158158
            if (clip)
209
39864
                output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
210
211
158158
            tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
212
158158
            output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
213
158158
            if (clip)
214
39864
                output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
215
28341698
        } else if (i == len-1) {
216
158158
            tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
217
158158
            output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
218
158158
            if (clip)
219
39864
                output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
220
221
158158
            tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
222
158158
            output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
223
158158
            if (clip)
224
39864
                output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
225
        } else {
226
28183540
            tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
227
28183540
            output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
228
28183540
            if (clip)
229
10777360
                output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
230
231
28183540
            tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
232
28183540
            output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
233
28183540
            if (clip)
234
10777360
                output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
235
        }
236
    }
237
158158
}
238
239
static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
240
                         int width, int linesize, int plane)
241
{
242
    int i;
243
    int16_t even, odd;
244
    for (i = 0; i < width; i++) {
245
        even = (low[i] - high[i])/2;
246
        odd  = (low[i] + high[i])/2;
247
        output[i]            = av_clip_uintp2(even, 10);
248
        output[i + linesize] = av_clip_uintp2(odd, 10);
249
    }
250
}
251
29898
static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
252
                         int width)
253
{
254
29898
    filter(output, 1, low, 1, high, 1, width, 0);
255
29898
}
256
257
39864
static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
258
                              int width, int clip)
259
{
260
39864
    filter(output, 1, low, 1, high, 1, width, clip);
261
39864
}
262
263
static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
264
                                    int width, int clip)
265
{
266
    filter(output, 2, low, 1, high, 1, width, clip);
267
}
268
269
88396
static void vert_filter(int16_t *output, ptrdiff_t out_stride,
270
                        int16_t *low, ptrdiff_t low_stride,
271
                        int16_t *high, ptrdiff_t high_stride, int len)
272
{
273
88396
    filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
274
88396
}
275
276
12
static void free_buffers(CFHDContext *s)
277
{
278
    int i, j;
279
280
60
    for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
281
48
        av_freep(&s->plane[i].idwt_buf);
282
48
        av_freep(&s->plane[i].idwt_tmp);
283
284
480
        for (j = 0; j < 9; j++)
285
432
            s->plane[i].subband[j] = NULL;
286
287
432
        for (j = 0; j < 8; j++)
288
384
            s->plane[i].l_h[j] = NULL;
289
    }
290
12
    s->a_height = 0;
291
12
    s->a_width  = 0;
292
12
}
293
294
6
static int alloc_buffers(AVCodecContext *avctx)
295
{
296
6
    CFHDContext *s = avctx->priv_data;
297
    int i, j, ret, planes;
298
    int chroma_x_shift, chroma_y_shift;
299
    unsigned k;
300
301
6
    if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
302
        s->coded_width *= 2;
303
        s->coded_height *= 2;
304
    }
305
306
6
    if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
307
        return ret;
308
6
    avctx->pix_fmt = s->coded_format;
309
310
6
    if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
311
                                                &chroma_x_shift,
312
                                                &chroma_y_shift)) < 0)
313
        return ret;
314
6
    planes = av_pix_fmt_count_planes(s->coded_format);
315
6
    if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
316
        planes = 4;
317
        chroma_x_shift = 1;
318
        chroma_y_shift = 1;
319
    }
320
321
24
    for (i = 0; i < planes; i++) {
322
        int w8, h8, w4, h4, w2, h2;
323
18
        int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
324
18
        int height = i ? avctx->height >> chroma_y_shift : avctx->height;
325
18
        ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
326
18
        if (chroma_y_shift)
327
            height = FFALIGN(height / 8, 2) * 8;
328
18
        s->plane[i].width  = width;
329
18
        s->plane[i].height = height;
330
18
        s->plane[i].stride = stride;
331
332
18
        w8 = FFALIGN(s->plane[i].width  / 8, 8);
333
18
        h8 = height / 8;
334
18
        w4 = w8 * 2;
335
18
        h4 = h8 * 2;
336
18
        w2 = w4 * 2;
337
18
        h2 = h4 * 2;
338
339
18
        s->plane[i].idwt_buf =
340
18
            av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
341
18
        s->plane[i].idwt_tmp =
342
18
            av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
343

18
        if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
344
            return AVERROR(ENOMEM);
345
346
18
        s->plane[i].subband[0] = s->plane[i].idwt_buf;
347
18
        s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
348
18
        s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
349
18
        s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
350
18
        s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
351
18
        s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
352
18
        s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
353
18
        s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
354
18
        s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
355
18
        s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
356
357
72
        for (j = 0; j < DWT_LEVELS; j++) {
358
270
            for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
359
216
                s->plane[i].band[j][k].a_width  = w8 << j;
360
216
                s->plane[i].band[j][k].a_height = h8 << j;
361
            }
362
        }
363
364
        /* ll2 and ll1 commented out because they are done in-place */
365
18
        s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
366
18
        s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
367
        // s->plane[i].l_h[2] = ll2;
368
18
        s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
369
18
        s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
370
        // s->plane[i].l_h[5] = ll1;
371
18
        s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
372
18
        s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
373
    }
374
375
6
    s->a_height = s->coded_height;
376
6
    s->a_width  = s->coded_width;
377
6
    s->a_format = s->coded_format;
378
379
6
    return 0;
380
}
381
382
33
static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
383
                       AVPacket *avpkt)
384
{
385
33
    CFHDContext *s = avctx->priv_data;
386
    GetByteContext gb;
387
33
    ThreadFrame frame = { .f = data };
388
33
    AVFrame *pic = data;
389
33
    int ret = 0, i, j, planes, plane, got_buffer = 0;
390
    int16_t *coeff_data;
391
392
33
    s->coded_format = AV_PIX_FMT_YUV422P10;
393
33
    init_frame_defaults(s);
394
33
    planes = av_pix_fmt_count_planes(s->coded_format);
395
396
33
    bytestream2_init(&gb, avpkt->data, avpkt->size);
397
398
17193
    while (bytestream2_get_bytes_left(&gb) > 4) {
399
        /* Bit weird but implement the tag parsing as the spec says */
400
17160
        uint16_t tagu   = bytestream2_get_be16(&gb);
401
17160
        int16_t tag     = (int16_t)tagu;
402
17160
        int8_t tag8     = (int8_t)(tagu >> 8);
403
17160
        uint16_t abstag = abs(tag);
404
17160
        int8_t abs_tag8 = abs(tag8);
405
17160
        uint16_t data   = bytestream2_get_be16(&gb);
406

17160
        if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
407
            av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
408
17160
        } else if (tag == SampleFlags) {
409
33
            av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
410
33
            s->progressive = data & 0x0001;
411
17127
        } else if (tag == ImageWidth) {
412
33
            av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
413
33
            s->coded_width = data;
414
17094
        } else if (tag == ImageHeight) {
415
33
            av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
416
33
            s->coded_height = data;
417
17061
        } else if (tag == 101) {
418
            av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
419
            if (data < 1 || data > 31) {
420
                av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
421
                ret = AVERROR(EINVAL);
422
                break;
423
            }
424
            s->bpc = data;
425
17061
        } else if (tag == ChannelCount) {
426
33
            av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
427
33
            s->channel_cnt = data;
428
33
            if (data > 4) {
429
                av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
430
                ret = AVERROR_PATCHWELCOME;
431
                break;
432
            }
433
17028
        } else if (tag == SubbandCount) {
434
33
            av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
435
33
            if (data != SUBBAND_COUNT) {
436
                av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
437
                ret = AVERROR_PATCHWELCOME;
438
                break;
439
            }
440
16995
        } else if (tag == ChannelNumber) {
441
66
            s->channel_num = data;
442
66
            av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
443
66
            if (s->channel_num >= planes) {
444
                av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
445
                ret = AVERROR(EINVAL);
446
                break;
447
            }
448
66
            init_plane_defaults(s);
449
16929
        } else if (tag == SubbandNumber) {
450

891
            if (s->subband_num != 0 && data == 1)  // hack
451
198
                s->level++;
452
891
            av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
453
891
            s->subband_num = data;
454
891
            if (s->level >= DWT_LEVELS) {
455
                av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
456
                ret = AVERROR(EINVAL);
457
                break;
458
            }
459
891
            if (s->subband_num > 3) {
460
                av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
461
                ret = AVERROR(EINVAL);
462
                break;
463
            }
464
16038
        } else if (tag == 51) {
465
891
            av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
466
891
            s->subband_num_actual = data;
467
891
            if (s->subband_num_actual >= 10) {
468
                av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
469
                ret = AVERROR(EINVAL);
470
                break;
471
            }
472
15147
        } else if (tag == LowpassPrecision)
473
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
474
15048
        else if (tag == Quantization) {
475
891
            s->quantisation = data;
476
891
            av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
477
14157
        } else if (tag == PrescaleShift) {
478
            s->prescale_shift[0] = (data >> 0) & 0x7;
479
            s->prescale_shift[1] = (data >> 3) & 0x7;
480
            s->prescale_shift[2] = (data >> 6) & 0x7;
481
            av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
482
14157
        } else if (tag == 27) {
483
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
484

99
            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
485
                av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
486
                ret = AVERROR(EINVAL);
487
                break;
488
            }
489
99
            s->plane[s->channel_num].band[0][0].width  = data;
490
99
            s->plane[s->channel_num].band[0][0].stride = data;
491
14058
        } else if (tag == 28) {
492
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
493

99
            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
494
                av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
495
                ret = AVERROR(EINVAL);
496
                break;
497
            }
498
99
            s->plane[s->channel_num].band[0][0].height = data;
499
13959
        } else if (tag == 1)
500
99
            av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
501
13860
        else if (tag == 10) {
502
33
            if (data != 0) {
503
                avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
504
                ret = AVERROR_PATCHWELCOME;
505
                break;
506
            }
507
33
            av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
508

13827
        } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
509
66
            if (abstag == 0x4001)
510
                s->peak.level = 0;
511
66
            av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
512
66
            bytestream2_skipu(&gb, data * 4);
513
13761
        } else if (tag == 23) {
514
            av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
515
            avpriv_report_missing_feature(avctx, "Skip frame");
516
            ret = AVERROR_PATCHWELCOME;
517
            break;
518
13761
        } else if (tag == 2) {
519
33
            av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
520
33
            if (data > bytestream2_get_bytes_left(&gb) / 4) {
521
                av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
522
                ret = AVERROR_INVALIDDATA;
523
                break;
524
            }
525
132
            for (i = 0; i < data; i++) {
526
99
                uint16_t tag2 = bytestream2_get_be16(&gb);
527
99
                uint16_t val2 = bytestream2_get_be16(&gb);
528
99
                av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
529
            }
530
13728
        } else if (tag == 41) {
531
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);
532
297
            if (data < 3) {
533
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
534
                ret = AVERROR(EINVAL);
535
                break;
536
            }
537
297
            s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
538
297
            s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
539
13431
        } else if (tag == 42) {
540
297
            av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
541
297
            if (data < 3) {
542
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
543
                ret = AVERROR(EINVAL);
544
                break;
545
            }
546
297
            s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
547
13134
        } else if (tag == 49) {
548
891
            av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
549
891
            if (data < 3) {
550
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
551
                ret = AVERROR(EINVAL);
552
                break;
553
            }
554
891
            s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
555
891
            s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
556
12243
        } else if (tag == 50) {
557
891
            av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
558
891
            if (data < 3) {
559
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
560
                ret = AVERROR(EINVAL);
561
                break;
562
            }
563
891
            s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
564
11352
        } else if (tag == 71) {
565
            s->codebook = data;
566
            av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
567
11352
        } else if (tag == 72) {
568
891
            s->codebook = data & 0xf;
569
891
            s->difference_coding = (data >> 4) & 1;
570
891
            av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
571
10461
        } else if (tag == 70) {
572
33
            av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
573

33
            if (!(data == 10 || data == 12)) {
574
                av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
575
                ret = AVERROR(EINVAL);
576
                break;
577
            }
578
33
            s->bpc = data;
579
10428
        } else if (tag == 84) {
580
33
            av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
581
33
            if (data == 1) {
582
22
                s->coded_format = AV_PIX_FMT_YUV422P10;
583
11
            } else if (data == 2) {
584
                s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
585
11
            } else if (data == 3) {
586
11
                s->coded_format = AV_PIX_FMT_GBRP12;
587
            } else if (data == 4) {
588
                s->coded_format = AV_PIX_FMT_GBRAP12;
589
            } else {
590
                avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
591
                ret = AVERROR_PATCHWELCOME;
592
                break;
593
            }
594
33
            planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
595
10395
        } else if (tag == -85) {
596
33
            av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
597
33
            s->cropped_height = data;
598
10362
        } else if (tag == -75) {
599
            s->peak.offset &= ~0xffff;
600
            s->peak.offset |= (data & 0xffff);
601
            s->peak.base    = gb;
602
            s->peak.level   = 0;
603
10362
        } else if (tag == -76) {
604
            s->peak.offset &= 0xffff;
605
            s->peak.offset |= (data & 0xffffU)<<16;
606
            s->peak.base    = gb;
607
            s->peak.level   = 0;
608

10362
        } else if (tag == -74 && s->peak.offset) {
609
            s->peak.level = data;
610
            bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
611
        } else
612
10362
            av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
613
614
        /* Some kind of end of header tag */
615


17160
        if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
616
33
            s->coded_format != AV_PIX_FMT_NONE) {
617

33
            if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
618
27
                s->a_format != s->coded_format) {
619
6
                free_buffers(s);
620
6
                if ((ret = alloc_buffers(avctx)) < 0) {
621
                    free_buffers(s);
622
                    return ret;
623
                }
624
            }
625
33
            ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
626
33
            if (ret < 0)
627
                return ret;
628
33
            if (s->cropped_height) {
629
33
                unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
630
33
                if (avctx->height < height)
631
                    return AVERROR_INVALIDDATA;
632
33
                avctx->height = height;
633
            }
634
33
            frame.f->width =
635
33
            frame.f->height = 0;
636
637
33
            if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
638
                return ret;
639
640
33
            s->coded_width = 0;
641
33
            s->coded_height = 0;
642
33
            s->coded_format = AV_PIX_FMT_NONE;
643
33
            got_buffer = 1;
644
        }
645
17160
        coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
646
647
        /* Lowpass coefficients */
648


17160
        if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
649
99
            int lowpass_height = s->plane[s->channel_num].band[0][0].height;
650
99
            int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
651
99
            int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
652
99
            int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
653
654
99
            if (!got_buffer) {
655
                av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
656
                ret = AVERROR(EINVAL);
657
                goto end;
658
            }
659
660

99
            if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
661
99
                lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
662
                av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
663
                ret = AVERROR(EINVAL);
664
                goto end;
665
            }
666
667
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);
668
5082
            for (i = 0; i < lowpass_height; i++) {
669
344267
                for (j = 0; j < lowpass_width; j++)
670
339284
                    coeff_data[j] = bytestream2_get_be16u(&gb);
671
672
4983
                coeff_data += lowpass_width;
673
            }
674
675
            /* Align to mod-4 position to continue reading tags */
676
99
            bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
677
678
            /* Copy last line of coefficients if odd height */
679
99
            if (lowpass_height & 1) {
680
33
                memcpy(&coeff_data[lowpass_height * lowpass_width],
681
33
                       &coeff_data[(lowpass_height - 1) * lowpass_width],
682
                       lowpass_width * sizeof(*coeff_data));
683
            }
684
685
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
686
        }
687
688


17160
        if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
689
891
            int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
690
891
            int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
691
891
            int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
692
891
            int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
693
891
            int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
694
            int expected;
695
891
            int a_expected = highpass_a_height * highpass_a_width;
696
            int level, run, coeff;
697
891
            int count = 0, bytes;
698
699
891
            if (!got_buffer) {
700
                av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
701
                ret = AVERROR(EINVAL);
702
                goto end;
703
            }
704
705

891
            if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
706
                av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
707
                ret = AVERROR(EINVAL);
708
                goto end;
709
            }
710
891
            expected = highpass_height * highpass_stride;
711
712
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);
713
714
891
            init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
715
            {
716
891
                OPEN_READER(re, &s->gb);
717
891
                if (!s->codebook) {
718
                    while (1) {
719
                        UPDATE_CACHE(re, &s->gb);
720
                        GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
721
                                   VLC_BITS, 3, 1);
722
723
                        /* escape */
724
                        if (level == 64)
725
                            break;
726
727
                        count += run;
728
729
                        if (count > expected)
730
                            break;
731
732
                        coeff = dequant_and_decompand(level, s->quantisation, 0);
733
                        for (i = 0; i < run; i++)
734
                            *coeff_data++ = coeff;
735
                    }
736
                } else {
737
                    while (1) {
738
3523868
                        UPDATE_CACHE(re, &s->gb);
739

3523868
                        GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
740
                                   VLC_BITS, 3, 1);
741
742
                        /* escape */
743

3523868
                        if (level == 255 && run == 2)
744
891
                            break;
745
746
3522977
                        count += run;
747
748
3522977
                        if (count > expected)
749
                            break;
750
751
3522977
                        coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
752
25184705
                        for (i = 0; i < run; i++)
753
21661728
                            *coeff_data++ = coeff;
754
                    }
755
                }
756
891
                CLOSE_READER(re, &s->gb);
757
            }
758
759
891
            if (count > expected) {
760
                av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
761
                ret = AVERROR(EINVAL);
762
                goto end;
763
            }
764
891
            if (s->peak.level)
765
                peak_table(coeff_data - count, &s->peak, count);
766
891
            if (s->difference_coding)
767
                difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
768
769
891
            bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
770
891
            if (bytes > bytestream2_get_bytes_left(&gb)) {
771
                av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
772
                ret = AVERROR(EINVAL);
773
                goto end;
774
            } else
775
891
                bytestream2_seek(&gb, bytes, SEEK_CUR);
776
777
891
            av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
778
891
            s->codebook = 0;
779
780
            /* Copy last line of coefficients if odd height */
781
891
            if (highpass_height & 1) {
782
99
                memcpy(&coeff_data[highpass_height * highpass_stride],
783
99
                       &coeff_data[(highpass_height - 1) * highpass_stride],
784
                       highpass_stride * sizeof(*coeff_data));
785
            }
786
        }
787
    }
788
789

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

33
        s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
791
        av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
792
        ret = AVERROR(EINVAL);
793
        goto end;
794
    }
795
796
33
    if (!got_buffer) {
797
        av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
798
        ret = AVERROR(EINVAL);
799
        goto end;
800
    }
801
802
33
    planes = av_pix_fmt_count_planes(avctx->pix_fmt);
803
33
    if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
804
        if (!s->progressive)
805
            return AVERROR_INVALIDDATA;
806
        planes = 4;
807
    }
808
809

132
    for (plane = 0; plane < planes && !ret; plane++) {
810
        /* level 1 */
811
99
        int lowpass_height  = s->plane[plane].band[0][0].height;
812
99
        int lowpass_width   = s->plane[plane].band[0][0].width;
813
99
        int highpass_stride = s->plane[plane].band[0][1].stride;
814

99
        int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
815
        ptrdiff_t dst_linesize;
816
        int16_t *low, *high, *output, *dst;
817
818
99
        if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
819
            act_plane = 0;
820
            dst_linesize = pic->linesize[act_plane];
821
        } else {
822
99
            dst_linesize = pic->linesize[act_plane] / 2;
823
        }
824
825

99
        if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
826
99
            !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
827
            av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
828
            ret = AVERROR(EINVAL);
829
            goto end;
830
        }
831
832
99
        av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
833
834
99
        low    = s->plane[plane].subband[0];
835
99
        high   = s->plane[plane].subband[2];
836
99
        output = s->plane[plane].l_h[0];
837
6413
        for (i = 0; i < lowpass_width; i++) {
838
6314
            vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
839
6314
            low++;
840
6314
            high++;
841
6314
            output++;
842
        }
843
844
99
        low    = s->plane[plane].subband[1];
845
99
        high   = s->plane[plane].subband[3];
846
99
        output = s->plane[plane].l_h[1];
847
848
6413
        for (i = 0; i < lowpass_width; i++) {
849
            // note the stride of "low" is highpass_stride
850
6314
            vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
851
6314
            low++;
852
6314
            high++;
853
6314
            output++;
854
        }
855
856
99
        low    = s->plane[plane].l_h[0];
857
99
        high   = s->plane[plane].l_h[1];
858
99
        output = s->plane[plane].subband[0];
859
10065
        for (i = 0; i < lowpass_height * 2; i++) {
860
9966
            horiz_filter(output, low, high, lowpass_width);
861
9966
            low    += lowpass_width;
862
9966
            high   += lowpass_width;
863
9966
            output += lowpass_width * 2;
864
        }
865
99
        if (s->bpc == 12) {
866
33
            output = s->plane[plane].subband[0];
867
3993
            for (i = 0; i < lowpass_height * 2; i++) {
868
716760
                for (j = 0; j < lowpass_width * 2; j++)
869
712800
                    output[j] *= 4;
870
871
3960
                output += lowpass_width * 2;
872
            }
873
        }
874
875
        /* level 2 */
876
99
        lowpass_height  = s->plane[plane].band[1][1].height;
877
99
        lowpass_width   = s->plane[plane].band[1][1].width;
878
99
        highpass_stride = s->plane[plane].band[1][1].stride;
879
880

99
        if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
881
99
            !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
882
            av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
883
            ret = AVERROR(EINVAL);
884
            goto end;
885
        }
886
887
99
        av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
888
889
99
        low    = s->plane[plane].subband[0];
890
99
        high   = s->plane[plane].subband[5];
891
99
        output = s->plane[plane].l_h[3];
892
12727
        for (i = 0; i < lowpass_width; i++) {
893
12628
            vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
894
12628
            low++;
895
12628
            high++;
896
12628
            output++;
897
        }
898
899
99
        low    = s->plane[plane].subband[4];
900
99
        high   = s->plane[plane].subband[6];
901
99
        output = s->plane[plane].l_h[4];
902
12727
        for (i = 0; i < lowpass_width; i++) {
903
12628
            vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
904
12628
            low++;
905
12628
            high++;
906
12628
            output++;
907
        }
908
909
99
        low    = s->plane[plane].l_h[3];
910
99
        high   = s->plane[plane].l_h[4];
911
99
        output = s->plane[plane].subband[0];
912
20031
        for (i = 0; i < lowpass_height * 2; i++) {
913
19932
            horiz_filter(output, low, high, lowpass_width);
914
19932
            low    += lowpass_width;
915
19932
            high   += lowpass_width;
916
19932
            output += lowpass_width * 2;
917
        }
918
919
99
        output = s->plane[plane].subband[0];
920
20031
        for (i = 0; i < lowpass_height * 2; i++) {
921
5448476
            for (j = 0; j < lowpass_width * 2; j++)
922
5428544
                output[j] *= 4;
923
924
19932
            output += lowpass_width * 2;
925
        }
926
927
        /* level 3 */
928
99
        lowpass_height  = s->plane[plane].band[2][1].height;
929
99
        lowpass_width   = s->plane[plane].band[2][1].width;
930
99
        highpass_stride = s->plane[plane].band[2][1].stride;
931
932

99
        if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
933
99
            !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
934
            av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
935
            ret = AVERROR(EINVAL);
936
            goto end;
937
        }
938
939
99
        av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
940
99
        if (s->progressive) {
941
99
            low    = s->plane[plane].subband[0];
942
99
            high   = s->plane[plane].subband[8];
943
99
            output = s->plane[plane].l_h[6];
944
25355
            for (i = 0; i < lowpass_width; i++) {
945
25256
                vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
946
25256
                low++;
947
25256
                high++;
948
25256
                output++;
949
            }
950
951
99
            low    = s->plane[plane].subband[7];
952
99
            high   = s->plane[plane].subband[9];
953
99
            output = s->plane[plane].l_h[7];
954
25355
            for (i = 0; i < lowpass_width; i++) {
955
25256
                vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
956
25256
                low++;
957
25256
                high++;
958
25256
                output++;
959
            }
960
961
99
            dst = (int16_t *)pic->data[act_plane];
962
99
            if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
963
                if (plane & 1)
964
                    dst++;
965
                if (plane > 1)
966
                    dst += pic->linesize[act_plane] >> 1;
967
            }
968
99
            low  = s->plane[plane].l_h[6];
969
99
            high = s->plane[plane].l_h[7];
970
971
99
            if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
972
                (lowpass_height * 2 > avctx->coded_height / 2 ||
973
                 lowpass_width  * 2 > avctx->coded_width  / 2    )
974
                ) {
975
                ret = AVERROR_INVALIDDATA;
976
                goto end;
977
            }
978
979
39963
            for (i = 0; i < lowpass_height * 2; i++) {
980
39864
                if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
981
                    horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
982
                else
983
39864
                    horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
984

39864
                if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
985
                    process_alpha(dst, lowpass_width * 2);
986
39864
                low  += lowpass_width;
987
39864
                high += lowpass_width;
988
39864
                dst  += dst_linesize;
989
            }
990
        } else {
991
            av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
992
            pic->interlaced_frame = 1;
993
            low    = s->plane[plane].subband[0];
994
            high   = s->plane[plane].subband[7];
995
            output = s->plane[plane].l_h[6];
996
            for (i = 0; i < lowpass_height; i++) {
997
                horiz_filter(output, low, high, lowpass_width);
998
                low    += lowpass_width;
999
                high   += lowpass_width;
1000
                output += lowpass_width * 2;
1001
            }
1002
1003
            low    = s->plane[plane].subband[8];
1004
            high   = s->plane[plane].subband[9];
1005
            output = s->plane[plane].l_h[7];
1006
            for (i = 0; i < lowpass_height; i++) {
1007
                horiz_filter(output, low, high, lowpass_width);
1008
                low    += lowpass_width;
1009
                high   += lowpass_width;
1010
                output += lowpass_width * 2;
1011
            }
1012
1013
            dst  = (int16_t *)pic->data[act_plane];
1014
            low  = s->plane[plane].l_h[6];
1015
            high = s->plane[plane].l_h[7];
1016
            for (i = 0; i < lowpass_height; i++) {
1017
                interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1018
                low  += lowpass_width * 2;
1019
                high += lowpass_width * 2;
1020
                dst  += pic->linesize[act_plane];
1021
            }
1022
        }
1023
    }
1024
1025
1026
33
    if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1027
        process_bayer(pic);
1028
33
end:
1029
33
    if (ret < 0)
1030
        return ret;
1031
1032
33
    *got_frame = 1;
1033
33
    return avpkt->size;
1034
}
1035
1036
6
static av_cold int cfhd_close(AVCodecContext *avctx)
1037
{
1038
6
    CFHDContext *s = avctx->priv_data;
1039
1040
6
    free_buffers(s);
1041
1042
6
    if (!avctx->internal->is_copy) {
1043
6
        ff_free_vlc(&s->vlc_9);
1044
6
        ff_free_vlc(&s->vlc_18);
1045
    }
1046
1047
6
    return 0;
1048
}
1049
1050
AVCodec ff_cfhd_decoder = {
1051
    .name             = "cfhd",
1052
    .long_name        = NULL_IF_CONFIG_SMALL("Cineform HD"),
1053
    .type             = AVMEDIA_TYPE_VIDEO,
1054
    .id               = AV_CODEC_ID_CFHD,
1055
    .priv_data_size   = sizeof(CFHDContext),
1056
    .init             = cfhd_init,
1057
    .close            = cfhd_close,
1058
    .decode           = cfhd_decode,
1059
    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1060
    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1061
};