]> git.sesse.net Git - ffmpeg/blob - libavcodec/cfhd.c
avcodec/cfhd: set correct bits_per_raw_sample
[ffmpeg] / libavcodec / cfhd.c
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 static av_cold int cfhd_init(AVCodecContext *avctx)
44 {
45     CFHDContext *s = avctx->priv_data;
46
47     s->avctx                   = avctx;
48
49     return ff_cfhd_init_vlcs(s);
50 }
51
52 static void init_plane_defaults(CFHDContext *s)
53 {
54     s->subband_num        = 0;
55     s->level              = 0;
56     s->subband_num_actual = 0;
57 }
58
59 static void init_peak_table_defaults(CFHDContext *s)
60 {
61     s->peak.level  = 0;
62     s->peak.offset = 0;
63     memset(&s->peak.base, 0, sizeof(s->peak.base));
64 }
65
66 static void init_frame_defaults(CFHDContext *s)
67 {
68     s->coded_width       = 0;
69     s->coded_height      = 0;
70     s->cropped_height    = 0;
71     s->bpc               = 10;
72     s->channel_cnt       = 4;
73     s->subband_cnt       = SUBBAND_COUNT;
74     s->channel_num       = 0;
75     s->lowpass_precision = 16;
76     s->quantisation      = 1;
77     s->wavelet_depth     = 3;
78     s->pshift            = 1;
79     s->codebook          = 0;
80     s->difference_coding = 0;
81     s->progressive       = 0;
82     init_plane_defaults(s);
83     init_peak_table_defaults(s);
84 }
85
86 /* TODO: merge with VLC tables or use LUT */
87 static inline int dequant_and_decompand(int level, int quantisation, int codebook)
88 {
89     if (codebook == 0 || codebook == 1) {
90         int64_t abslevel = abs(level);
91         if (abslevel < 256)
92             return (abslevel + ((768 * abslevel * abslevel * abslevel) / (256 * 256 * 256))) *
93                FFSIGN(level) * quantisation;
94         else
95             return level * quantisation;
96     } else
97         return level * quantisation;
98 }
99
100 static inline void difference_coding(int16_t *band, int width, int height)
101 {
102
103     int i,j;
104     for (i = 0; i < height; i++) {
105         for (j = 1; j < width; j++) {
106           band[j] += band[j-1];
107         }
108         band += width;
109     }
110 }
111
112 static inline void peak_table(int16_t *band, Peak *peak, int length)
113 {
114     int i;
115     for (i = 0; i < length; i++)
116         if (abs(band[i]) > peak->level)
117             band[i] = bytestream2_get_le16(&peak->base);
118 }
119
120 static inline void process_alpha(int16_t *alpha, int width)
121 {
122     int i, channel;
123     for (i = 0; i < width; i++) {
124         channel   = alpha[i];
125         channel  -= ALPHA_COMPAND_DC_OFFSET;
126         channel <<= 3;
127         channel  *= ALPHA_COMPAND_GAIN;
128         channel >>= 16;
129         channel   = av_clip_uintp2(channel, 12);
130         alpha[i]  = channel;
131     }
132 }
133
134 static inline void process_bayer(AVFrame *frame)
135 {
136     const int linesize = frame->linesize[0];
137     uint16_t *r = (uint16_t *)frame->data[0];
138     uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
139     uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
140     uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
141     const int mid = 2048;
142
143     for (int y = 0; y < frame->height >> 1; y++) {
144         for (int x = 0; x < frame->width; x += 2) {
145             int R, G1, G2, B;
146             int g, rg, bg, gd;
147
148             g  = r[x];
149             rg = g1[x];
150             bg = g2[x];
151             gd = b[x];
152             gd -= mid;
153
154             R  = (rg - mid) * 2 + g;
155             G1 = g + gd;
156             G2 = g - gd;
157             B  = (bg - mid) * 2 + g;
158
159             R  = av_clip_uintp2(R  * 16, 16);
160             G1 = av_clip_uintp2(G1 * 16, 16);
161             G2 = av_clip_uintp2(G2 * 16, 16);
162             B  = av_clip_uintp2(B  * 16, 16);
163
164             r[x]  = R;
165             g1[x] = G1;
166             g2[x] = G2;
167             b[x]  = B;
168         }
169
170         r  += linesize;
171         g1 += linesize;
172         g2 += linesize;
173         b  += linesize;
174     }
175 }
176
177 static inline void filter(int16_t *output, ptrdiff_t out_stride,
178                           int16_t *low, ptrdiff_t low_stride,
179                           int16_t *high, ptrdiff_t high_stride,
180                           int len, int clip)
181 {
182     int16_t tmp;
183     int i;
184
185     tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
186     output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
187     if (clip)
188         output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
189
190     tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
191     output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
192     if (clip)
193         output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
194
195     for (i = 1; i < len - 1; i++) {
196         tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
197         output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
198         if (clip)
199             output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
200
201         tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
202         output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
203         if (clip)
204             output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
205     }
206
207     tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
208     output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
209     if (clip)
210         output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
211
212     tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
213     output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
214     if (clip)
215         output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
216 }
217
218 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
219                          int width, int linesize, int plane)
220 {
221     int i;
222     int16_t even, odd;
223     for (i = 0; i < width; i++) {
224         even = (low[i] - high[i])/2;
225         odd  = (low[i] + high[i])/2;
226         output[i]            = av_clip_uintp2(even, 10);
227         output[i + linesize] = av_clip_uintp2(odd, 10);
228     }
229 }
230 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
231                          int width)
232 {
233     filter(output, 1, low, 1, high, 1, width, 0);
234 }
235
236 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
237                               int width, int clip)
238 {
239     filter(output, 1, low, 1, high, 1, width, clip);
240 }
241
242 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
243                                     int width, int clip)
244 {
245     filter(output, 2, low, 1, high, 1, width, clip);
246 }
247
248 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
249                         int16_t *low, ptrdiff_t low_stride,
250                         int16_t *high, ptrdiff_t high_stride, int len)
251 {
252     filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
253 }
254
255 static void free_buffers(CFHDContext *s)
256 {
257     int i, j;
258
259     for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
260         av_freep(&s->plane[i].idwt_buf);
261         av_freep(&s->plane[i].idwt_tmp);
262
263         for (j = 0; j < 9; j++)
264             s->plane[i].subband[j] = NULL;
265
266         for (j = 0; j < 8; j++)
267             s->plane[i].l_h[j] = NULL;
268     }
269     s->a_height = 0;
270     s->a_width  = 0;
271 }
272
273 static int alloc_buffers(AVCodecContext *avctx)
274 {
275     CFHDContext *s = avctx->priv_data;
276     int i, j, ret, planes;
277     int chroma_x_shift, chroma_y_shift;
278     unsigned k;
279
280     if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
281         s->coded_width *= 2;
282         s->coded_height *= 2;
283     }
284
285     if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
286         return ret;
287     avctx->pix_fmt = s->coded_format;
288
289     if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
290                                                 &chroma_x_shift,
291                                                 &chroma_y_shift)) < 0)
292         return ret;
293     planes = av_pix_fmt_count_planes(s->coded_format);
294     if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
295         planes = 4;
296         chroma_x_shift = 1;
297         chroma_y_shift = 1;
298     }
299
300     for (i = 0; i < planes; i++) {
301         int w8, h8, w4, h4, w2, h2;
302         int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
303         int height = i ? avctx->height >> chroma_y_shift : avctx->height;
304         ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
305         if (chroma_y_shift)
306             height = FFALIGN(height / 8, 2) * 8;
307         s->plane[i].width  = width;
308         s->plane[i].height = height;
309         s->plane[i].stride = stride;
310
311         w8 = FFALIGN(s->plane[i].width  / 8, 8);
312         h8 = FFALIGN(height, 8) / 8;
313         w4 = w8 * 2;
314         h4 = h8 * 2;
315         w2 = w4 * 2;
316         h2 = h4 * 2;
317
318         s->plane[i].idwt_buf =
319             av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
320         s->plane[i].idwt_tmp =
321             av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
322         if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
323             return AVERROR(ENOMEM);
324
325         s->plane[i].subband[0] = s->plane[i].idwt_buf;
326         s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
327         s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
328         s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
329         s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
330         s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
331         s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
332         s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
333         s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
334         s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
335
336         for (j = 0; j < DWT_LEVELS; j++) {
337             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
338                 s->plane[i].band[j][k].a_width  = w8 << j;
339                 s->plane[i].band[j][k].a_height = h8 << j;
340             }
341         }
342
343         /* ll2 and ll1 commented out because they are done in-place */
344         s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
345         s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
346         // s->plane[i].l_h[2] = ll2;
347         s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
348         s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
349         // s->plane[i].l_h[5] = ll1;
350         s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
351         s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
352     }
353
354     s->a_height = s->coded_height;
355     s->a_width  = s->coded_width;
356     s->a_format = s->coded_format;
357
358     return 0;
359 }
360
361 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
362                        AVPacket *avpkt)
363 {
364     CFHDContext *s = avctx->priv_data;
365     GetByteContext gb;
366     ThreadFrame frame = { .f = data };
367     AVFrame *pic = data;
368     int ret = 0, i, j, planes, plane, got_buffer = 0;
369     int16_t *coeff_data;
370
371     s->coded_format = AV_PIX_FMT_YUV422P10;
372     init_frame_defaults(s);
373     planes = av_pix_fmt_count_planes(s->coded_format);
374
375     bytestream2_init(&gb, avpkt->data, avpkt->size);
376
377     while (bytestream2_get_bytes_left(&gb) > 4) {
378         /* Bit weird but implement the tag parsing as the spec says */
379         uint16_t tagu   = bytestream2_get_be16(&gb);
380         int16_t tag     = (int16_t)tagu;
381         int8_t tag8     = (int8_t)(tagu >> 8);
382         uint16_t abstag = abs(tag);
383         int8_t abs_tag8 = abs(tag8);
384         uint16_t data   = bytestream2_get_be16(&gb);
385         if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
386             av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
387         } else if (tag == SampleFlags) {
388             av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
389             s->progressive = data & 0x0001;
390         } else if (tag == ImageWidth) {
391             av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
392             s->coded_width = data;
393         } else if (tag == ImageHeight) {
394             av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
395             s->coded_height = data;
396         } else if (tag == BitsPerComponent) {
397             av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
398             if (data < 1 || data > 31) {
399                 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
400                 ret = AVERROR(EINVAL);
401                 break;
402             }
403             s->bpc = data;
404         } else if (tag == ChannelCount) {
405             av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
406             s->channel_cnt = data;
407             if (data > 4) {
408                 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
409                 ret = AVERROR_PATCHWELCOME;
410                 break;
411             }
412         } else if (tag == SubbandCount) {
413             av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
414             if (data != SUBBAND_COUNT) {
415                 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
416                 ret = AVERROR_PATCHWELCOME;
417                 break;
418             }
419         } else if (tag == ChannelNumber) {
420             s->channel_num = data;
421             av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
422             if (s->channel_num >= planes) {
423                 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
424                 ret = AVERROR(EINVAL);
425                 break;
426             }
427             init_plane_defaults(s);
428         } else if (tag == SubbandNumber) {
429             if (s->subband_num != 0 && data == 1)  // hack
430                 s->level++;
431             av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
432             s->subband_num = data;
433             if (s->level >= DWT_LEVELS) {
434                 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
435                 ret = AVERROR(EINVAL);
436                 break;
437             }
438             if (s->subband_num > 3) {
439                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
440                 ret = AVERROR(EINVAL);
441                 break;
442             }
443         } else if (tag == SubbandBand) {
444             av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
445             s->subband_num_actual = data;
446             if (s->subband_num_actual >= 10) {
447                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
448                 ret = AVERROR(EINVAL);
449                 break;
450             }
451         } else if (tag == LowpassPrecision)
452             av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
453         else if (tag == Quantization) {
454             s->quantisation = data;
455             av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
456         } else if (tag == PrescaleShift) {
457             s->prescale_shift[0] = (data >> 0) & 0x7;
458             s->prescale_shift[1] = (data >> 3) & 0x7;
459             s->prescale_shift[2] = (data >> 6) & 0x7;
460             av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
461         } else if (tag == LowpassWidth) {
462             av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
463             if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
464                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
465                 ret = AVERROR(EINVAL);
466                 break;
467             }
468             s->plane[s->channel_num].band[0][0].width  = data;
469             s->plane[s->channel_num].band[0][0].stride = data;
470         } else if (tag == LowpassHeight) {
471             av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
472             if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
473                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
474                 ret = AVERROR(EINVAL);
475                 break;
476             }
477             s->plane[s->channel_num].band[0][0].height = data;
478         } else if (tag == SampleType)
479             av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
480         else if (tag == TransformType) {
481             if (data != 0) {
482                 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
483                 ret = AVERROR_PATCHWELCOME;
484                 break;
485             }
486             av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
487         } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
488             if (abstag == 0x4001)
489                 s->peak.level = 0;
490             av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
491             bytestream2_skipu(&gb, data * 4);
492         } else if (tag == 23) {
493             av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
494             avpriv_report_missing_feature(avctx, "Skip frame");
495             ret = AVERROR_PATCHWELCOME;
496             break;
497         } else if (tag == SampleIndexTable) {
498             av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
499             if (data > bytestream2_get_bytes_left(&gb) / 4) {
500                 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
501                 ret = AVERROR_INVALIDDATA;
502                 break;
503             }
504             for (i = 0; i < data; i++) {
505                 uint16_t tag2 = bytestream2_get_be16(&gb);
506                 uint16_t val2 = bytestream2_get_be16(&gb);
507                 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
508             }
509         } else if (tag == HighpassWidth) {
510             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);
511             if (data < 3) {
512                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
513                 ret = AVERROR(EINVAL);
514                 break;
515             }
516             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
517             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
518         } else if (tag == HighpassHeight) {
519             av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
520             if (data < 3) {
521                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
522                 ret = AVERROR(EINVAL);
523                 break;
524             }
525             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
526         } else if (tag == BandWidth) {
527             av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
528             if (data < 3) {
529                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
530                 ret = AVERROR(EINVAL);
531                 break;
532             }
533             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
534             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
535         } else if (tag == BandHeight) {
536             av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
537             if (data < 3) {
538                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
539                 ret = AVERROR(EINVAL);
540                 break;
541             }
542             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
543         } else if (tag == 71) {
544             s->codebook = data;
545             av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
546         } else if (tag == BandCodingFlags) {
547             s->codebook = data & 0xf;
548             s->difference_coding = (data >> 4) & 1;
549             av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
550         } else if (tag == Precision) {
551             av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
552             if (!(data == 10 || data == 12)) {
553                 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
554                 ret = AVERROR(EINVAL);
555                 break;
556             }
557             avctx->bits_per_raw_sample = s->bpc = data;
558         } else if (tag == EncodedFormat) {
559             av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
560             if (data == 1) {
561                 s->coded_format = AV_PIX_FMT_YUV422P10;
562             } else if (data == 2) {
563                 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
564             } else if (data == 3) {
565                 s->coded_format = AV_PIX_FMT_GBRP12;
566             } else if (data == 4) {
567                 s->coded_format = AV_PIX_FMT_GBRAP12;
568             } else {
569                 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
570                 ret = AVERROR_PATCHWELCOME;
571                 break;
572             }
573             planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
574         } else if (tag == -85) {
575             av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
576             s->cropped_height = data;
577         } else if (tag == -75) {
578             s->peak.offset &= ~0xffff;
579             s->peak.offset |= (data & 0xffff);
580             s->peak.base    = gb;
581             s->peak.level   = 0;
582         } else if (tag == -76) {
583             s->peak.offset &= 0xffff;
584             s->peak.offset |= (data & 0xffffU)<<16;
585             s->peak.base    = gb;
586             s->peak.level   = 0;
587         } else if (tag == -74 && s->peak.offset) {
588             s->peak.level = data;
589             bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
590         } else
591             av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
592
593         /* Some kind of end of header tag */
594         if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
595             s->coded_format != AV_PIX_FMT_NONE) {
596             if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
597                 s->a_format != s->coded_format) {
598                 free_buffers(s);
599                 if ((ret = alloc_buffers(avctx)) < 0) {
600                     free_buffers(s);
601                     return ret;
602                 }
603             }
604             ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
605             if (ret < 0)
606                 return ret;
607             if (s->cropped_height) {
608                 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
609                 if (avctx->height < height)
610                     return AVERROR_INVALIDDATA;
611                 avctx->height = height;
612             }
613             frame.f->width =
614             frame.f->height = 0;
615
616             if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
617                 return ret;
618
619             s->coded_width = 0;
620             s->coded_height = 0;
621             s->coded_format = AV_PIX_FMT_NONE;
622             got_buffer = 1;
623         }
624         coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
625
626         /* Lowpass coefficients */
627         if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
628             int lowpass_height = s->plane[s->channel_num].band[0][0].height;
629             int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
630             int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
631             int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
632
633             if (!got_buffer) {
634                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
635                 ret = AVERROR(EINVAL);
636                 goto end;
637             }
638
639             if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
640                 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
641                 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
642                 ret = AVERROR(EINVAL);
643                 goto end;
644             }
645
646             av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
647             for (i = 0; i < lowpass_height; i++) {
648                 for (j = 0; j < lowpass_width; j++)
649                     coeff_data[j] = bytestream2_get_be16u(&gb);
650
651                 coeff_data += lowpass_width;
652             }
653
654             /* Align to mod-4 position to continue reading tags */
655             bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
656
657             /* Copy last line of coefficients if odd height */
658             if (lowpass_height & 1) {
659                 memcpy(&coeff_data[lowpass_height * lowpass_width],
660                        &coeff_data[(lowpass_height - 1) * lowpass_width],
661                        lowpass_width * sizeof(*coeff_data));
662             }
663
664             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
665         }
666
667         if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
668             int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
669             int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
670             int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
671             int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
672             int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
673             int expected;
674             int a_expected = highpass_a_height * highpass_a_width;
675             int level, run, coeff;
676             int count = 0, bytes;
677
678             if (!got_buffer) {
679                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
680                 ret = AVERROR(EINVAL);
681                 goto end;
682             }
683
684             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
685                 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
686                 ret = AVERROR(EINVAL);
687                 goto end;
688             }
689             expected = highpass_height * highpass_stride;
690
691             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);
692
693             init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
694             {
695                 OPEN_READER(re, &s->gb);
696                 if (!s->codebook) {
697                     while (1) {
698                         UPDATE_CACHE(re, &s->gb);
699                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
700                                    VLC_BITS, 3, 1);
701
702                         /* escape */
703                         if (level == 64)
704                             break;
705
706                         count += run;
707
708                         if (count > expected)
709                             break;
710
711                         coeff = dequant_and_decompand(level, s->quantisation, 0);
712                         for (i = 0; i < run; i++)
713                             *coeff_data++ = coeff;
714                     }
715                 } else {
716                     while (1) {
717                         UPDATE_CACHE(re, &s->gb);
718                         GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
719                                    VLC_BITS, 3, 1);
720
721                         /* escape */
722                         if (level == 255 && run == 2)
723                             break;
724
725                         count += run;
726
727                         if (count > expected)
728                             break;
729
730                         coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
731                         for (i = 0; i < run; i++)
732                             *coeff_data++ = coeff;
733                     }
734                 }
735                 CLOSE_READER(re, &s->gb);
736             }
737
738             if (count > expected) {
739                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
740                 ret = AVERROR(EINVAL);
741                 goto end;
742             }
743             if (s->peak.level)
744                 peak_table(coeff_data - count, &s->peak, count);
745             if (s->difference_coding)
746                 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
747
748             bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
749             if (bytes > bytestream2_get_bytes_left(&gb)) {
750                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
751                 ret = AVERROR(EINVAL);
752                 goto end;
753             } else
754                 bytestream2_seek(&gb, bytes, SEEK_CUR);
755
756             av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
757             s->codebook = 0;
758
759             /* Copy last line of coefficients if odd height */
760             if (highpass_height & 1) {
761                 memcpy(&coeff_data[highpass_height * highpass_stride],
762                        &coeff_data[(highpass_height - 1) * highpass_stride],
763                        highpass_stride * sizeof(*coeff_data));
764             }
765         }
766     }
767
768     if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
769         s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
770         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
771         ret = AVERROR(EINVAL);
772         goto end;
773     }
774
775     if (!got_buffer) {
776         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
777         ret = AVERROR(EINVAL);
778         goto end;
779     }
780
781     planes = av_pix_fmt_count_planes(avctx->pix_fmt);
782     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
783         if (!s->progressive)
784             return AVERROR_INVALIDDATA;
785         planes = 4;
786     }
787
788     for (plane = 0; plane < planes && !ret; plane++) {
789         /* level 1 */
790         int lowpass_height  = s->plane[plane].band[0][0].height;
791         int lowpass_width   = s->plane[plane].band[0][0].width;
792         int highpass_stride = s->plane[plane].band[0][1].stride;
793         int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
794         ptrdiff_t dst_linesize;
795         int16_t *low, *high, *output, *dst;
796
797         if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
798             act_plane = 0;
799             dst_linesize = pic->linesize[act_plane];
800         } else {
801             dst_linesize = pic->linesize[act_plane] / 2;
802         }
803
804         if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
805             !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
806             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
807             ret = AVERROR(EINVAL);
808             goto end;
809         }
810
811         av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
812
813         low    = s->plane[plane].subband[0];
814         high   = s->plane[plane].subband[2];
815         output = s->plane[plane].l_h[0];
816         for (i = 0; i < lowpass_width; i++) {
817             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
818             low++;
819             high++;
820             output++;
821         }
822
823         low    = s->plane[plane].subband[1];
824         high   = s->plane[plane].subband[3];
825         output = s->plane[plane].l_h[1];
826
827         for (i = 0; i < lowpass_width; i++) {
828             // note the stride of "low" is highpass_stride
829             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
830             low++;
831             high++;
832             output++;
833         }
834
835         low    = s->plane[plane].l_h[0];
836         high   = s->plane[plane].l_h[1];
837         output = s->plane[plane].subband[0];
838         for (i = 0; i < lowpass_height * 2; i++) {
839             horiz_filter(output, low, high, lowpass_width);
840             low    += lowpass_width;
841             high   += lowpass_width;
842             output += lowpass_width * 2;
843         }
844         if (s->bpc == 12) {
845             output = s->plane[plane].subband[0];
846             for (i = 0; i < lowpass_height * 2; i++) {
847                 for (j = 0; j < lowpass_width * 2; j++)
848                     output[j] *= 4;
849
850                 output += lowpass_width * 2;
851             }
852         }
853
854         /* level 2 */
855         lowpass_height  = s->plane[plane].band[1][1].height;
856         lowpass_width   = s->plane[plane].band[1][1].width;
857         highpass_stride = s->plane[plane].band[1][1].stride;
858
859         if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
860             !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
861             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
862             ret = AVERROR(EINVAL);
863             goto end;
864         }
865
866         av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
867
868         low    = s->plane[plane].subband[0];
869         high   = s->plane[plane].subband[5];
870         output = s->plane[plane].l_h[3];
871         for (i = 0; i < lowpass_width; i++) {
872             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
873             low++;
874             high++;
875             output++;
876         }
877
878         low    = s->plane[plane].subband[4];
879         high   = s->plane[plane].subband[6];
880         output = s->plane[plane].l_h[4];
881         for (i = 0; i < lowpass_width; i++) {
882             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
883             low++;
884             high++;
885             output++;
886         }
887
888         low    = s->plane[plane].l_h[3];
889         high   = s->plane[plane].l_h[4];
890         output = s->plane[plane].subband[0];
891         for (i = 0; i < lowpass_height * 2; i++) {
892             horiz_filter(output, low, high, lowpass_width);
893             low    += lowpass_width;
894             high   += lowpass_width;
895             output += lowpass_width * 2;
896         }
897
898         output = s->plane[plane].subband[0];
899         for (i = 0; i < lowpass_height * 2; i++) {
900             for (j = 0; j < lowpass_width * 2; j++)
901                 output[j] *= 4;
902
903             output += lowpass_width * 2;
904         }
905
906         /* level 3 */
907         lowpass_height  = s->plane[plane].band[2][1].height;
908         lowpass_width   = s->plane[plane].band[2][1].width;
909         highpass_stride = s->plane[plane].band[2][1].stride;
910
911         if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
912             !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
913             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
914             ret = AVERROR(EINVAL);
915             goto end;
916         }
917
918         av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
919         if (s->progressive) {
920             low    = s->plane[plane].subband[0];
921             high   = s->plane[plane].subband[8];
922             output = s->plane[plane].l_h[6];
923             for (i = 0; i < lowpass_width; i++) {
924                 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
925                 low++;
926                 high++;
927                 output++;
928             }
929
930             low    = s->plane[plane].subband[7];
931             high   = s->plane[plane].subband[9];
932             output = s->plane[plane].l_h[7];
933             for (i = 0; i < lowpass_width; i++) {
934                 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
935                 low++;
936                 high++;
937                 output++;
938             }
939
940             dst = (int16_t *)pic->data[act_plane];
941             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
942                 if (plane & 1)
943                     dst++;
944                 if (plane > 1)
945                     dst += pic->linesize[act_plane] >> 1;
946             }
947             low  = s->plane[plane].l_h[6];
948             high = s->plane[plane].l_h[7];
949
950             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
951                 (lowpass_height * 2 > avctx->coded_height / 2 ||
952                  lowpass_width  * 2 > avctx->coded_width  / 2    )
953                 ) {
954                 ret = AVERROR_INVALIDDATA;
955                 goto end;
956             }
957
958             for (i = 0; i < lowpass_height * 2; i++) {
959                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
960                     horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
961                 else
962                     horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
963                 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
964                     process_alpha(dst, lowpass_width * 2);
965                 low  += lowpass_width;
966                 high += lowpass_width;
967                 dst  += dst_linesize;
968             }
969         } else {
970             av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
971             pic->interlaced_frame = 1;
972             low    = s->plane[plane].subband[0];
973             high   = s->plane[plane].subband[7];
974             output = s->plane[plane].l_h[6];
975             for (i = 0; i < lowpass_height; i++) {
976                 horiz_filter(output, low, high, lowpass_width);
977                 low    += lowpass_width;
978                 high   += lowpass_width;
979                 output += lowpass_width * 2;
980             }
981
982             low    = s->plane[plane].subband[8];
983             high   = s->plane[plane].subband[9];
984             output = s->plane[plane].l_h[7];
985             for (i = 0; i < lowpass_height; i++) {
986                 horiz_filter(output, low, high, lowpass_width);
987                 low    += lowpass_width;
988                 high   += lowpass_width;
989                 output += lowpass_width * 2;
990             }
991
992             dst  = (int16_t *)pic->data[act_plane];
993             low  = s->plane[plane].l_h[6];
994             high = s->plane[plane].l_h[7];
995             for (i = 0; i < lowpass_height; i++) {
996                 interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
997                 low  += lowpass_width * 2;
998                 high += lowpass_width * 2;
999                 dst  += pic->linesize[act_plane];
1000             }
1001         }
1002     }
1003
1004
1005     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1006         process_bayer(pic);
1007 end:
1008     if (ret < 0)
1009         return ret;
1010
1011     *got_frame = 1;
1012     return avpkt->size;
1013 }
1014
1015 static av_cold int cfhd_close(AVCodecContext *avctx)
1016 {
1017     CFHDContext *s = avctx->priv_data;
1018
1019     free_buffers(s);
1020
1021     ff_free_vlc(&s->vlc_9);
1022     ff_free_vlc(&s->vlc_18);
1023
1024     return 0;
1025 }
1026
1027 AVCodec ff_cfhd_decoder = {
1028     .name             = "cfhd",
1029     .long_name        = NULL_IF_CONFIG_SMALL("Cineform HD"),
1030     .type             = AVMEDIA_TYPE_VIDEO,
1031     .id               = AV_CODEC_ID_CFHD,
1032     .priv_data_size   = sizeof(CFHDContext),
1033     .init             = cfhd_init,
1034     .close            = cfhd_close,
1035     .decode           = cfhd_decode,
1036     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1037     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1038 };