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