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