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