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