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