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