GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cfhd.c Lines: 463 703 65.9 %
Date: 2020-07-13 04:22:34 Branches: 259 412 62.9 %

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

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

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

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

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

99
            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
495
                av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
496
                ret = AVERROR(EINVAL);
497
                break;
498
            }
499
99
            s->plane[s->channel_num].band[0][0].width  = data;
500
99
            s->plane[s->channel_num].band[0][0].stride = data;
501
14058
        } else if (tag == LowpassHeight) {
502
99
            av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
503

99
            if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
504
                av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
505
                ret = AVERROR(EINVAL);
506
                break;
507
            }
508
99
            s->plane[s->channel_num].band[0][0].height = data;
509
13959
        } else if (tag == SampleType)
510
99
            av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
511
13860
        else if (tag == TransformType) {
512
33
            if (data != 0) {
513
                avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
514
                ret = AVERROR_PATCHWELCOME;
515
                break;
516
            }
517
33
            av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
518

13827
        } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
519
66
            if (abstag == 0x4001)
520
                s->peak.level = 0;
521
66
            av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
522
66
            bytestream2_skipu(&gb, data * 4);
523
13761
        } else if (tag == 23) {
524
            av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
525
            avpriv_report_missing_feature(avctx, "Skip frame");
526
            ret = AVERROR_PATCHWELCOME;
527
            break;
528
13761
        } else if (tag == SampleIndexTable) {
529
33
            av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
530
33
            if (data > bytestream2_get_bytes_left(&gb) / 4) {
531
                av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
532
                ret = AVERROR_INVALIDDATA;
533
                break;
534
            }
535
132
            for (i = 0; i < data; i++) {
536
99
                uint16_t tag2 = bytestream2_get_be16(&gb);
537
99
                uint16_t val2 = bytestream2_get_be16(&gb);
538
99
                av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
539
            }
540
13728
        } else if (tag == HighpassWidth) {
541
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);
542
297
            if (data < 3) {
543
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
544
                ret = AVERROR(EINVAL);
545
                break;
546
            }
547
297
            s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
548
297
            s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
549
13431
        } else if (tag == HighpassHeight) {
550
297
            av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
551
297
            if (data < 3) {
552
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
553
                ret = AVERROR(EINVAL);
554
                break;
555
            }
556
297
            s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
557
13134
        } else if (tag == 49) {
558
891
            av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
559
891
            if (data < 3) {
560
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
561
                ret = AVERROR(EINVAL);
562
                break;
563
            }
564
891
            s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
565
891
            s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
566
12243
        } else if (tag == 50) {
567
891
            av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
568
891
            if (data < 3) {
569
                av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
570
                ret = AVERROR(EINVAL);
571
                break;
572
            }
573
891
            s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
574
11352
        } else if (tag == 71) {
575
            s->codebook = data;
576
            av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
577
11352
        } else if (tag == 72) {
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
10461
        } else if (tag == 70) {
582
33
            av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %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
                break;
587
            }
588
33
            s->bpc = data;
589
10428
        } 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
                break;
603
            }
604
33
            planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
605
10395
        } else if (tag == -85) {
606
33
            av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
607
33
            s->cropped_height = data;
608
10362
        } else if (tag == -75) {
609
            s->peak.offset &= ~0xffff;
610
            s->peak.offset |= (data & 0xffff);
611
            s->peak.base    = gb;
612
            s->peak.level   = 0;
613
10362
        } else if (tag == -76) {
614
            s->peak.offset &= 0xffff;
615
            s->peak.offset |= (data & 0xffffU)<<16;
616
            s->peak.base    = gb;
617
            s->peak.level   = 0;
618

10362
        } else if (tag == -74 && s->peak.offset) {
619
            s->peak.level = data;
620
            bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
621
        } else
622
10362
            av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
623
624
        /* Some kind of end of header tag */
625


17160
        if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
626
33
            s->coded_format != AV_PIX_FMT_NONE) {
627

33
            if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
628
27
                s->a_format != s->coded_format) {
629
6
                free_buffers(s);
630
6
                if ((ret = alloc_buffers(avctx)) < 0) {
631
                    free_buffers(s);
632
                    return ret;
633
                }
634
            }
635
33
            ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
636
33
            if (ret < 0)
637
                return ret;
638
33
            if (s->cropped_height) {
639
33
                unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
640
33
                if (avctx->height < height)
641
                    return AVERROR_INVALIDDATA;
642
33
                avctx->height = height;
643
            }
644
33
            frame.f->width =
645
33
            frame.f->height = 0;
646
647
33
            if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
648
                return ret;
649
650
33
            s->coded_width = 0;
651
33
            s->coded_height = 0;
652
33
            s->coded_format = AV_PIX_FMT_NONE;
653
33
            got_buffer = 1;
654
        }
655
17160
        coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
656
657
        /* Lowpass coefficients */
658


17160
        if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
659
99
            int lowpass_height = s->plane[s->channel_num].band[0][0].height;
660
99
            int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
661
99
            int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
662
99
            int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
663
664
99
            if (!got_buffer) {
665
                av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
666
                ret = AVERROR(EINVAL);
667
                goto end;
668
            }
669
670

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


17160
        if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
699
891
            int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
700
891
            int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
701
891
            int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
702
891
            int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
703
891
            int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
704
            int expected;
705
891
            int a_expected = highpass_a_height * highpass_a_width;
706
            int level, run, coeff;
707
891
            int count = 0, bytes;
708
709
891
            if (!got_buffer) {
710
                av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
711
                ret = AVERROR(EINVAL);
712
                goto end;
713
            }
714
715

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

3523868
                        GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
750
                                   VLC_BITS, 3, 1);
751
752
                        /* escape */
753

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

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

33
        s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
801
        av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
802
        ret = AVERROR(EINVAL);
803
        goto end;
804
    }
805
806
33
    if (!got_buffer) {
807
        av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
808
        ret = AVERROR(EINVAL);
809
        goto end;
810
    }
811
812
33
    planes = av_pix_fmt_count_planes(avctx->pix_fmt);
813
33
    if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
814
        if (!s->progressive)
815
            return AVERROR_INVALIDDATA;
816
        planes = 4;
817
    }
818
819

132
    for (plane = 0; plane < planes && !ret; plane++) {
820
        /* level 1 */
821
99
        int lowpass_height  = s->plane[plane].band[0][0].height;
822
99
        int lowpass_width   = s->plane[plane].band[0][0].width;
823
99
        int highpass_stride = s->plane[plane].band[0][1].stride;
824

99
        int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
825
        ptrdiff_t dst_linesize;
826
        int16_t *low, *high, *output, *dst;
827
828
99
        if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
829
            act_plane = 0;
830
            dst_linesize = pic->linesize[act_plane];
831
        } else {
832
99
            dst_linesize = pic->linesize[act_plane] / 2;
833
        }
834
835

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

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

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

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