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