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