]> git.sesse.net Git - ffmpeg/blob - libavcodec/cfhd.c
vc2enc: do not allocate packet until exact frame size is known
[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" is unsupported\n", 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 not supported\n");
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" is unsupported\n", 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             /* Copy last line of coefficients if odd height */
471             if (lowpass_height & 1) {
472                 memcpy(&coeff_data[lowpass_height * lowpass_width],
473                        &coeff_data[(lowpass_height - 1) * lowpass_width],
474                        lowpass_width * sizeof(*coeff_data));
475             }
476
477             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %"PRIu16"\n", lowpass_width * lowpass_height);
478         }
479
480         if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
481             int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
482             int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
483             int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
484             int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
485             int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
486             int expected = highpass_height * highpass_stride;
487             int a_expected = highpass_a_height * highpass_a_width;
488             int level, run, coeff;
489             int count = 0, bytes;
490
491             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < expected) {
492                 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficents\n");
493                 ret = AVERROR(EINVAL);
494                 goto end;
495             }
496
497             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);
498
499             init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
500             {
501                 OPEN_READER(re, &s->gb);
502                 if (!s->codebook) {
503                     while (1) {
504                         UPDATE_CACHE(re, &s->gb);
505                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
506                                    VLC_BITS, 3, 1);
507
508                         /* escape */
509                         if (level == 64)
510                             break;
511
512                         count += run;
513
514                         if (count > expected)
515                             break;
516
517                         coeff = dequant_and_decompand(level, s->quantisation);
518                         for (i = 0; i < run; i++)
519                             *coeff_data++ = coeff;
520                     }
521                 } else {
522                     while (1) {
523                         UPDATE_CACHE(re, &s->gb);
524                         GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
525                                    VLC_BITS, 3, 1);
526
527                         /* escape */
528                         if (level == 255 && run == 2)
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                 }
541                 CLOSE_READER(re, &s->gb);
542             }
543
544             if (count > expected) {
545                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
546                 ret = AVERROR(EINVAL);
547                 goto end;
548             }
549
550             bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
551             if (bytes > bytestream2_get_bytes_left(&gb)) {
552                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
553                 ret = AVERROR(EINVAL);
554                 goto end;
555             } else
556                 bytestream2_seek(&gb, bytes, SEEK_CUR);
557
558             av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
559             s->codebook = 0;
560
561             /* Copy last line of coefficients if odd height */
562             if (highpass_height & 1) {
563                 memcpy(&coeff_data[highpass_height * highpass_stride],
564                        &coeff_data[(highpass_height - 1) * highpass_stride],
565                        highpass_stride * sizeof(*coeff_data));
566             }
567         }
568     }
569
570     if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
571         s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
572         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
573         ret = AVERROR(EINVAL);
574         goto end;
575     }
576
577     if (!got_buffer) {
578         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
579         ret = AVERROR(EINVAL);
580         goto end;
581     }
582
583     planes = av_pix_fmt_count_planes(avctx->pix_fmt);
584     for (plane = 0; plane < planes && !ret; plane++) {
585         /* level 1 */
586         int lowpass_height  = s->plane[plane].band[0][0].height;
587         int lowpass_width   = s->plane[plane].band[0][0].width;
588         int highpass_stride = s->plane[plane].band[0][1].stride;
589         int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
590         int16_t *low, *high, *output, *dst;
591
592         if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
593             !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
594             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
595             ret = AVERROR(EINVAL);
596             goto end;
597         }
598
599         av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
600
601         low    = s->plane[plane].subband[0];
602         high   = s->plane[plane].subband[2];
603         output = s->plane[plane].l_h[0];
604         for (i = 0; i < lowpass_width; i++) {
605             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
606             low++;
607             high++;
608             output++;
609         }
610
611         low    = s->plane[plane].subband[1];
612         high   = s->plane[plane].subband[3];
613         output = s->plane[plane].l_h[1];
614
615         for (i = 0; i < lowpass_width; i++) {
616             // note the stride of "low" is highpass_stride
617             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
618             low++;
619             high++;
620             output++;
621         }
622
623         low    = s->plane[plane].l_h[0];
624         high   = s->plane[plane].l_h[1];
625         output = s->plane[plane].subband[0];
626         for (i = 0; i < lowpass_height * 2; i++) {
627             horiz_filter(output, low, high, lowpass_width);
628             low    += lowpass_width;
629             high   += lowpass_width;
630             output += lowpass_width * 2;
631         }
632         if (s->bpc == 12) {
633             output = s->plane[plane].subband[0];
634             for (i = 0; i < lowpass_height * 2; i++) {
635                 for (j = 0; j < lowpass_width * 2; j++)
636                     output[j] <<= 2;
637
638                 output += lowpass_width * 2;
639             }
640         }
641
642         /* level 2 */
643         lowpass_height  = s->plane[plane].band[1][1].height;
644         lowpass_width   = s->plane[plane].band[1][1].width;
645         highpass_stride = s->plane[plane].band[1][1].stride;
646
647         if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
648             !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
649             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
650             ret = AVERROR(EINVAL);
651             goto end;
652         }
653
654         av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
655
656         low    = s->plane[plane].subband[0];
657         high   = s->plane[plane].subband[5];
658         output = s->plane[plane].l_h[3];
659         for (i = 0; i < lowpass_width; i++) {
660             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
661             low++;
662             high++;
663             output++;
664         }
665
666         low    = s->plane[plane].subband[4];
667         high   = s->plane[plane].subband[6];
668         output = s->plane[plane].l_h[4];
669         for (i = 0; i < lowpass_width; i++) {
670             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
671             low++;
672             high++;
673             output++;
674         }
675
676         low    = s->plane[plane].l_h[3];
677         high   = s->plane[plane].l_h[4];
678         output = s->plane[plane].subband[0];
679         for (i = 0; i < lowpass_height * 2; i++) {
680             horiz_filter(output, low, high, lowpass_width);
681             low    += lowpass_width;
682             high   += lowpass_width;
683             output += lowpass_width * 2;
684         }
685
686         output = s->plane[plane].subband[0];
687         for (i = 0; i < lowpass_height * 2; i++) {
688             for (j = 0; j < lowpass_width * 2; j++)
689                 output[j] <<= 2;
690
691             output += lowpass_width * 2;
692         }
693
694         /* level 3 */
695         lowpass_height  = s->plane[plane].band[2][1].height;
696         lowpass_width   = s->plane[plane].band[2][1].width;
697         highpass_stride = s->plane[plane].band[2][1].stride;
698
699         if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
700             !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
701             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
702             ret = AVERROR(EINVAL);
703             goto end;
704         }
705
706         av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
707
708         low    = s->plane[plane].subband[0];
709         high   = s->plane[plane].subband[8];
710         output = s->plane[plane].l_h[6];
711         for (i = 0; i < lowpass_width; i++) {
712             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
713             low++;
714             high++;
715             output++;
716         }
717
718         low    = s->plane[plane].subband[7];
719         high   = s->plane[plane].subband[9];
720         output = s->plane[plane].l_h[7];
721         for (i = 0; i < lowpass_width; i++) {
722             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
723             low++;
724             high++;
725             output++;
726         }
727
728         dst = (int16_t *)pic->data[act_plane];
729         low  = s->plane[plane].l_h[6];
730         high = s->plane[plane].l_h[7];
731         for (i = 0; i < lowpass_height * 2; i++) {
732             horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
733             low  += lowpass_width;
734             high += lowpass_width;
735             dst  += pic->linesize[act_plane] / 2;
736         }
737     }
738
739
740 end:
741     if (ret < 0)
742         return ret;
743
744     *got_frame = 1;
745     return avpkt->size;
746 }
747
748 static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
749 {
750     CFHDContext *s = avctx->priv_data;
751
752     free_buffers(avctx);
753
754     if (!avctx->internal->is_copy) {
755         ff_free_vlc(&s->vlc_9);
756         ff_free_vlc(&s->vlc_18);
757     }
758
759     return 0;
760 }
761
762 AVCodec ff_cfhd_decoder = {
763     .name           = "cfhd",
764     .long_name      = NULL_IF_CONFIG_SMALL("Cineform HD"),
765     .type           = AVMEDIA_TYPE_VIDEO,
766     .id             = AV_CODEC_ID_CFHD,
767     .priv_data_size = sizeof(CFHDContext),
768     .init           = cfhd_decode_init,
769     .close          = cfhd_close_decoder,
770     .decode         = cfhd_decode,
771     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
772     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
773 };