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