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