]> git.sesse.net Git - ffmpeg/blob - libavcodec/cfhd.c
avcodec/cfhd: fix overflow in multiplication in LUT calculation
[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  * Cineform HD video decoder
24  */
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/buffer.h"
28 #include "libavutil/common.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
32
33 #include "avcodec.h"
34 #include "bytestream.h"
35 #include "get_bits.h"
36 #include "internal.h"
37 #include "thread.h"
38 #include "cfhd.h"
39
40 #define ALPHA_COMPAND_DC_OFFSET 256
41 #define ALPHA_COMPAND_GAIN 9400
42
43 static av_cold int cfhd_init(AVCodecContext *avctx)
44 {
45     CFHDContext *s = avctx->priv_data;
46
47     s->avctx                   = avctx;
48
49     for (int i = 0; i < 64; i++) {
50         int val = i;
51
52         if (val >= 40) {
53             if (val >= 54) {
54                 val -= 54;
55                 val <<= 2;
56                 val += 54;
57             }
58
59             val -= 40;
60             val <<= 2;
61             val += 40;
62         }
63
64         s->lut[0][i] = val;
65     }
66
67     for (int i = 0; i < 256; i++)
68         s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
69
70     return ff_cfhd_init_vlcs(s);
71 }
72
73 static void init_plane_defaults(CFHDContext *s)
74 {
75     s->subband_num        = 0;
76     s->level              = 0;
77     s->subband_num_actual = 0;
78 }
79
80 static void init_peak_table_defaults(CFHDContext *s)
81 {
82     s->peak.level  = 0;
83     s->peak.offset = 0;
84     memset(&s->peak.base, 0, sizeof(s->peak.base));
85 }
86
87 static void init_frame_defaults(CFHDContext *s)
88 {
89     s->coded_width       = 0;
90     s->coded_height      = 0;
91     s->cropped_height    = 0;
92     s->bpc               = 10;
93     s->channel_cnt       = 4;
94     s->subband_cnt       = SUBBAND_COUNT;
95     s->channel_num       = 0;
96     s->lowpass_precision = 16;
97     s->quantisation      = 1;
98     s->wavelet_depth     = 3;
99     s->pshift            = 1;
100     s->codebook          = 0;
101     s->difference_coding = 0;
102     s->progressive       = 0;
103     init_plane_defaults(s);
104     init_peak_table_defaults(s);
105 }
106
107 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
108 {
109     if (codebook == 0 || codebook == 1) {
110         return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
111     } else
112         return level * quantisation;
113 }
114
115 static inline void difference_coding(int16_t *band, int width, int height)
116 {
117
118     int i,j;
119     for (i = 0; i < height; i++) {
120         for (j = 1; j < width; j++) {
121           band[j] += band[j-1];
122         }
123         band += width;
124     }
125 }
126
127 static inline void peak_table(int16_t *band, Peak *peak, int length)
128 {
129     int i;
130     for (i = 0; i < length; i++)
131         if (abs(band[i]) > peak->level)
132             band[i] = bytestream2_get_le16(&peak->base);
133 }
134
135 static inline void process_alpha(int16_t *alpha, int width)
136 {
137     int i, channel;
138     for (i = 0; i < width; i++) {
139         channel   = alpha[i];
140         channel  -= ALPHA_COMPAND_DC_OFFSET;
141         channel <<= 3;
142         channel  *= ALPHA_COMPAND_GAIN;
143         channel >>= 16;
144         channel   = av_clip_uintp2(channel, 12);
145         alpha[i]  = channel;
146     }
147 }
148
149 static inline void process_bayer(AVFrame *frame)
150 {
151     const int linesize = frame->linesize[0];
152     uint16_t *r = (uint16_t *)frame->data[0];
153     uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
154     uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
155     uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
156     const int mid = 2048;
157
158     for (int y = 0; y < frame->height >> 1; y++) {
159         for (int x = 0; x < frame->width; x += 2) {
160             int R, G1, G2, B;
161             int g, rg, bg, gd;
162
163             g  = r[x];
164             rg = g1[x];
165             bg = g2[x];
166             gd = b[x];
167             gd -= mid;
168
169             R  = (rg - mid) * 2 + g;
170             G1 = g + gd;
171             G2 = g - gd;
172             B  = (bg - mid) * 2 + g;
173
174             R  = av_clip_uintp2(R  * 16, 16);
175             G1 = av_clip_uintp2(G1 * 16, 16);
176             G2 = av_clip_uintp2(G2 * 16, 16);
177             B  = av_clip_uintp2(B  * 16, 16);
178
179             r[x]  = R;
180             g1[x] = G1;
181             g2[x] = G2;
182             b[x]  = B;
183         }
184
185         r  += linesize;
186         g1 += linesize;
187         g2 += linesize;
188         b  += linesize;
189     }
190 }
191
192 static inline void filter(int16_t *output, ptrdiff_t out_stride,
193                           int16_t *low, ptrdiff_t low_stride,
194                           int16_t *high, ptrdiff_t high_stride,
195                           int len, int clip)
196 {
197     int16_t tmp;
198     int i;
199
200     tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
201     output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
202     if (clip)
203         output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
204
205     tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
206     output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
207     if (clip)
208         output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
209
210     for (i = 1; i < len - 1; i++) {
211         tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
212         output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
213         if (clip)
214             output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
215
216         tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
217         output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
218         if (clip)
219             output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
220     }
221
222     tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
223     output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
224     if (clip)
225         output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
226
227     tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
228     output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
229     if (clip)
230         output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
231 }
232
233 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
234                          int width, int linesize, int plane)
235 {
236     int i;
237     int16_t even, odd;
238     for (i = 0; i < width; i++) {
239         even = (low[i] - high[i])/2;
240         odd  = (low[i] + high[i])/2;
241         output[i]            = av_clip_uintp2(even, 10);
242         output[i + linesize] = av_clip_uintp2(odd, 10);
243     }
244 }
245 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
246                          int width)
247 {
248     filter(output, 1, low, 1, high, 1, width, 0);
249 }
250
251 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
252                               int width, int clip)
253 {
254     filter(output, 1, low, 1, high, 1, width, clip);
255 }
256
257 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
258                                     int width, int clip)
259 {
260     filter(output, 2, low, 1, high, 1, width, clip);
261 }
262
263 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
264                         int16_t *low, ptrdiff_t low_stride,
265                         int16_t *high, ptrdiff_t high_stride, int len)
266 {
267     filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
268 }
269
270 static void free_buffers(CFHDContext *s)
271 {
272     int i, j;
273
274     for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
275         av_freep(&s->plane[i].idwt_buf);
276         av_freep(&s->plane[i].idwt_tmp);
277
278         for (j = 0; j < 9; j++)
279             s->plane[i].subband[j] = NULL;
280
281         for (j = 0; j < 8; j++)
282             s->plane[i].l_h[j] = NULL;
283     }
284     s->a_height = 0;
285     s->a_width  = 0;
286 }
287
288 static int alloc_buffers(AVCodecContext *avctx)
289 {
290     CFHDContext *s = avctx->priv_data;
291     int i, j, ret, planes;
292     int chroma_x_shift, chroma_y_shift;
293     unsigned k;
294
295     if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
296         s->coded_width *= 2;
297         s->coded_height *= 2;
298     }
299
300     if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
301         return ret;
302     avctx->pix_fmt = s->coded_format;
303
304     if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
305                                                 &chroma_x_shift,
306                                                 &chroma_y_shift)) < 0)
307         return ret;
308     planes = av_pix_fmt_count_planes(s->coded_format);
309     if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
310         planes = 4;
311         chroma_x_shift = 1;
312         chroma_y_shift = 1;
313     }
314
315     for (i = 0; i < planes; i++) {
316         int w8, h8, w4, h4, w2, h2;
317         int width  = i ? avctx->width  >> chroma_x_shift : avctx->width;
318         int height = i ? avctx->height >> chroma_y_shift : avctx->height;
319         ptrdiff_t stride = FFALIGN(width  / 8, 8) * 8;
320         if (chroma_y_shift)
321             height = FFALIGN(height / 8, 2) * 8;
322         s->plane[i].width  = width;
323         s->plane[i].height = height;
324         s->plane[i].stride = stride;
325
326         w8 = FFALIGN(s->plane[i].width  / 8, 8);
327         h8 = FFALIGN(height, 8) / 8;
328         w4 = w8 * 2;
329         h4 = h8 * 2;
330         w2 = w4 * 2;
331         h2 = h4 * 2;
332
333         s->plane[i].idwt_buf =
334             av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
335         s->plane[i].idwt_tmp =
336             av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
337         if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
338             return AVERROR(ENOMEM);
339
340         s->plane[i].subband[0] = s->plane[i].idwt_buf;
341         s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
342         s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
343         s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
344         s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
345         s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
346         s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
347         s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
348         s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
349         s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
350
351         for (j = 0; j < DWT_LEVELS; j++) {
352             for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
353                 s->plane[i].band[j][k].a_width  = w8 << j;
354                 s->plane[i].band[j][k].a_height = h8 << j;
355             }
356         }
357
358         /* ll2 and ll1 commented out because they are done in-place */
359         s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
360         s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
361         // s->plane[i].l_h[2] = ll2;
362         s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
363         s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
364         // s->plane[i].l_h[5] = ll1;
365         s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
366         s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
367     }
368
369     s->a_height = s->coded_height;
370     s->a_width  = s->coded_width;
371     s->a_format = s->coded_format;
372
373     return 0;
374 }
375
376 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
377                        AVPacket *avpkt)
378 {
379     CFHDContext *s = avctx->priv_data;
380     GetByteContext gb;
381     ThreadFrame frame = { .f = data };
382     AVFrame *pic = data;
383     int ret = 0, i, j, planes, plane, got_buffer = 0;
384     int16_t *coeff_data;
385
386     s->coded_format = AV_PIX_FMT_YUV422P10;
387     init_frame_defaults(s);
388     planes = av_pix_fmt_count_planes(s->coded_format);
389
390     bytestream2_init(&gb, avpkt->data, avpkt->size);
391
392     while (bytestream2_get_bytes_left(&gb) > 4) {
393         /* Bit weird but implement the tag parsing as the spec says */
394         uint16_t tagu   = bytestream2_get_be16(&gb);
395         int16_t tag     = (int16_t)tagu;
396         int8_t tag8     = (int8_t)(tagu >> 8);
397         uint16_t abstag = abs(tag);
398         int8_t abs_tag8 = abs(tag8);
399         uint16_t data   = bytestream2_get_be16(&gb);
400         if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
401             av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
402         } else if (tag == SampleFlags) {
403             av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
404             s->progressive = data & 0x0001;
405         } else if (tag == ImageWidth) {
406             av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
407             s->coded_width = data;
408         } else if (tag == ImageHeight) {
409             av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
410             s->coded_height = data;
411         } else if (tag == BitsPerComponent) {
412             av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
413             if (data < 1 || data > 31) {
414                 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
415                 ret = AVERROR(EINVAL);
416                 break;
417             }
418             s->bpc = data;
419         } else if (tag == ChannelCount) {
420             av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
421             s->channel_cnt = data;
422             if (data > 4) {
423                 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
424                 ret = AVERROR_PATCHWELCOME;
425                 break;
426             }
427         } else if (tag == SubbandCount) {
428             av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
429             if (data != SUBBAND_COUNT) {
430                 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
431                 ret = AVERROR_PATCHWELCOME;
432                 break;
433             }
434         } else if (tag == ChannelNumber) {
435             s->channel_num = data;
436             av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
437             if (s->channel_num >= planes) {
438                 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
439                 ret = AVERROR(EINVAL);
440                 break;
441             }
442             init_plane_defaults(s);
443         } else if (tag == SubbandNumber) {
444             if (s->subband_num != 0 && data == 1)  // hack
445                 s->level++;
446             av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
447             s->subband_num = data;
448             if (s->level >= DWT_LEVELS) {
449                 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
450                 ret = AVERROR(EINVAL);
451                 break;
452             }
453             if (s->subband_num > 3) {
454                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
455                 ret = AVERROR(EINVAL);
456                 break;
457             }
458         } else if (tag == SubbandBand) {
459             av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
460             s->subband_num_actual = data;
461             if (s->subband_num_actual >= 10) {
462                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
463                 ret = AVERROR(EINVAL);
464                 break;
465             }
466         } else if (tag == LowpassPrecision)
467             av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
468         else if (tag == Quantization) {
469             s->quantisation = data;
470             av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
471         } else if (tag == PrescaleShift) {
472             s->prescale_shift[0] = (data >> 0) & 0x7;
473             s->prescale_shift[1] = (data >> 3) & 0x7;
474             s->prescale_shift[2] = (data >> 6) & 0x7;
475             av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
476         } else if (tag == LowpassWidth) {
477             av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
478             if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
479                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
480                 ret = AVERROR(EINVAL);
481                 break;
482             }
483             s->plane[s->channel_num].band[0][0].width  = data;
484             s->plane[s->channel_num].band[0][0].stride = data;
485         } else if (tag == LowpassHeight) {
486             av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
487             if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
488                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
489                 ret = AVERROR(EINVAL);
490                 break;
491             }
492             s->plane[s->channel_num].band[0][0].height = data;
493         } else if (tag == SampleType)
494             av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
495         else if (tag == TransformType) {
496             if (data != 0) {
497                 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
498                 ret = AVERROR_PATCHWELCOME;
499                 break;
500             }
501             av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
502         } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
503             if (abstag == 0x4001)
504                 s->peak.level = 0;
505             av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
506             bytestream2_skipu(&gb, data * 4);
507         } else if (tag == 23) {
508             av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
509             avpriv_report_missing_feature(avctx, "Skip frame");
510             ret = AVERROR_PATCHWELCOME;
511             break;
512         } else if (tag == SampleIndexTable) {
513             av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
514             if (data > bytestream2_get_bytes_left(&gb) / 4) {
515                 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
516                 ret = AVERROR_INVALIDDATA;
517                 break;
518             }
519             for (i = 0; i < data; i++) {
520                 uint16_t tag2 = bytestream2_get_be16(&gb);
521                 uint16_t val2 = bytestream2_get_be16(&gb);
522                 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
523             }
524         } else if (tag == HighpassWidth) {
525             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);
526             if (data < 3) {
527                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
528                 ret = AVERROR(EINVAL);
529                 break;
530             }
531             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
532             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
533         } else if (tag == HighpassHeight) {
534             av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
535             if (data < 3) {
536                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
537                 ret = AVERROR(EINVAL);
538                 break;
539             }
540             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
541         } else if (tag == BandWidth) {
542             av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
543             if (data < 3) {
544                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
545                 ret = AVERROR(EINVAL);
546                 break;
547             }
548             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
549             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
550         } else if (tag == BandHeight) {
551             av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
552             if (data < 3) {
553                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
554                 ret = AVERROR(EINVAL);
555                 break;
556             }
557             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
558         } else if (tag == InputFormat) {
559             av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
560         } else if (tag == BandCodingFlags) {
561             s->codebook = data & 0xf;
562             s->difference_coding = (data >> 4) & 1;
563             av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
564         } else if (tag == Precision) {
565             av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
566             if (!(data == 10 || data == 12)) {
567                 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
568                 ret = AVERROR(EINVAL);
569                 break;
570             }
571             avctx->bits_per_raw_sample = s->bpc = data;
572         } else if (tag == EncodedFormat) {
573             av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
574             if (data == 1) {
575                 s->coded_format = AV_PIX_FMT_YUV422P10;
576             } else if (data == 2) {
577                 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
578             } else if (data == 3) {
579                 s->coded_format = AV_PIX_FMT_GBRP12;
580             } else if (data == 4) {
581                 s->coded_format = AV_PIX_FMT_GBRAP12;
582             } else {
583                 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
584                 ret = AVERROR_PATCHWELCOME;
585                 break;
586             }
587             planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
588         } else if (tag == -85) {
589             av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
590             s->cropped_height = data;
591         } else if (tag == -75) {
592             s->peak.offset &= ~0xffff;
593             s->peak.offset |= (data & 0xffff);
594             s->peak.base    = gb;
595             s->peak.level   = 0;
596         } else if (tag == -76) {
597             s->peak.offset &= 0xffff;
598             s->peak.offset |= (data & 0xffffU)<<16;
599             s->peak.base    = gb;
600             s->peak.level   = 0;
601         } else if (tag == -74 && s->peak.offset) {
602             s->peak.level = data;
603             bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
604         } else
605             av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
606
607         /* Some kind of end of header tag */
608         if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
609             s->coded_format != AV_PIX_FMT_NONE) {
610             if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
611                 s->a_format != s->coded_format) {
612                 free_buffers(s);
613                 if ((ret = alloc_buffers(avctx)) < 0) {
614                     free_buffers(s);
615                     return ret;
616                 }
617             }
618             ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
619             if (ret < 0)
620                 return ret;
621             if (s->cropped_height) {
622                 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
623                 if (avctx->height < height)
624                     return AVERROR_INVALIDDATA;
625                 avctx->height = height;
626             }
627             frame.f->width =
628             frame.f->height = 0;
629
630             if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
631                 return ret;
632
633             s->coded_width = 0;
634             s->coded_height = 0;
635             s->coded_format = AV_PIX_FMT_NONE;
636             got_buffer = 1;
637         }
638         coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
639
640         /* Lowpass coefficients */
641         if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
642             int lowpass_height = s->plane[s->channel_num].band[0][0].height;
643             int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
644             int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
645             int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
646
647             if (!got_buffer) {
648                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
649                 ret = AVERROR(EINVAL);
650                 goto end;
651             }
652
653             if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
654                 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
655                 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
656                 ret = AVERROR(EINVAL);
657                 goto end;
658             }
659
660             av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
661             for (i = 0; i < lowpass_height; i++) {
662                 for (j = 0; j < lowpass_width; j++)
663                     coeff_data[j] = bytestream2_get_be16u(&gb);
664
665                 coeff_data += lowpass_width;
666             }
667
668             /* Align to mod-4 position to continue reading tags */
669             bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
670
671             /* Copy last line of coefficients if odd height */
672             if (lowpass_height & 1) {
673                 memcpy(&coeff_data[lowpass_height * lowpass_width],
674                        &coeff_data[(lowpass_height - 1) * lowpass_width],
675                        lowpass_width * sizeof(*coeff_data));
676             }
677
678             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
679         }
680
681         if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
682             int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
683             int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
684             int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
685             int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
686             int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
687             int expected;
688             int a_expected = highpass_a_height * highpass_a_width;
689             int level, run, coeff;
690             int count = 0, bytes;
691
692             if (!got_buffer) {
693                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
694                 ret = AVERROR(EINVAL);
695                 goto end;
696             }
697
698             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
699                 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
700                 ret = AVERROR(EINVAL);
701                 goto end;
702             }
703             expected = highpass_height * highpass_stride;
704
705             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);
706
707             init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
708             {
709                 OPEN_READER(re, &s->gb);
710                 if (!s->codebook) {
711                     while (1) {
712                         UPDATE_CACHE(re, &s->gb);
713                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
714                                    VLC_BITS, 3, 1);
715
716                         /* escape */
717                         if (level == 64)
718                             break;
719
720                         count += run;
721
722                         if (count > expected)
723                             break;
724
725                         coeff = dequant_and_decompand(s, level, s->quantisation, 0);
726                         for (i = 0; i < run; i++)
727                             *coeff_data++ = coeff;
728                     }
729                 } else {
730                     while (1) {
731                         UPDATE_CACHE(re, &s->gb);
732                         GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
733                                    VLC_BITS, 3, 1);
734
735                         /* escape */
736                         if (level == 255 && run == 2)
737                             break;
738
739                         count += run;
740
741                         if (count > expected)
742                             break;
743
744                         coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
745                         for (i = 0; i < run; i++)
746                             *coeff_data++ = coeff;
747                     }
748                 }
749                 CLOSE_READER(re, &s->gb);
750             }
751
752             if (count > expected) {
753                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
754                 ret = AVERROR(EINVAL);
755                 goto end;
756             }
757             if (s->peak.level)
758                 peak_table(coeff_data - count, &s->peak, count);
759             if (s->difference_coding)
760                 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
761
762             bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
763             if (bytes > bytestream2_get_bytes_left(&gb)) {
764                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
765                 ret = AVERROR(EINVAL);
766                 goto end;
767             } else
768                 bytestream2_seek(&gb, bytes, SEEK_CUR);
769
770             av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
771             s->codebook = 0;
772
773             /* Copy last line of coefficients if odd height */
774             if (highpass_height & 1) {
775                 memcpy(&coeff_data[highpass_height * highpass_stride],
776                        &coeff_data[(highpass_height - 1) * highpass_stride],
777                        highpass_stride * sizeof(*coeff_data));
778             }
779         }
780     }
781
782     if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
783         s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
784         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
785         ret = AVERROR(EINVAL);
786         goto end;
787     }
788
789     if (!got_buffer) {
790         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
791         ret = AVERROR(EINVAL);
792         goto end;
793     }
794
795     planes = av_pix_fmt_count_planes(avctx->pix_fmt);
796     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
797         if (!s->progressive)
798             return AVERROR_INVALIDDATA;
799         planes = 4;
800     }
801
802     for (plane = 0; plane < planes && !ret; plane++) {
803         /* level 1 */
804         int lowpass_height  = s->plane[plane].band[0][0].height;
805         int lowpass_width   = s->plane[plane].band[0][0].width;
806         int highpass_stride = s->plane[plane].band[0][1].stride;
807         int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
808         ptrdiff_t dst_linesize;
809         int16_t *low, *high, *output, *dst;
810
811         if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
812             act_plane = 0;
813             dst_linesize = pic->linesize[act_plane];
814         } else {
815             dst_linesize = pic->linesize[act_plane] / 2;
816         }
817
818         if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
819             !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
820             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
821             ret = AVERROR(EINVAL);
822             goto end;
823         }
824
825         av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
826
827         low    = s->plane[plane].subband[0];
828         high   = s->plane[plane].subband[2];
829         output = s->plane[plane].l_h[0];
830         for (i = 0; i < lowpass_width; i++) {
831             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
832             low++;
833             high++;
834             output++;
835         }
836
837         low    = s->plane[plane].subband[1];
838         high   = s->plane[plane].subband[3];
839         output = s->plane[plane].l_h[1];
840
841         for (i = 0; i < lowpass_width; i++) {
842             // note the stride of "low" is highpass_stride
843             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
844             low++;
845             high++;
846             output++;
847         }
848
849         low    = s->plane[plane].l_h[0];
850         high   = s->plane[plane].l_h[1];
851         output = s->plane[plane].subband[0];
852         for (i = 0; i < lowpass_height * 2; i++) {
853             horiz_filter(output, low, high, lowpass_width);
854             low    += lowpass_width;
855             high   += lowpass_width;
856             output += lowpass_width * 2;
857         }
858         if (s->bpc == 12) {
859             output = s->plane[plane].subband[0];
860             for (i = 0; i < lowpass_height * 2; i++) {
861                 for (j = 0; j < lowpass_width * 2; j++)
862                     output[j] *= 4;
863
864                 output += lowpass_width * 2;
865             }
866         }
867
868         /* level 2 */
869         lowpass_height  = s->plane[plane].band[1][1].height;
870         lowpass_width   = s->plane[plane].band[1][1].width;
871         highpass_stride = s->plane[plane].band[1][1].stride;
872
873         if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
874             !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
875             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
876             ret = AVERROR(EINVAL);
877             goto end;
878         }
879
880         av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
881
882         low    = s->plane[plane].subband[0];
883         high   = s->plane[plane].subband[5];
884         output = s->plane[plane].l_h[3];
885         for (i = 0; i < lowpass_width; i++) {
886             vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
887             low++;
888             high++;
889             output++;
890         }
891
892         low    = s->plane[plane].subband[4];
893         high   = s->plane[plane].subband[6];
894         output = s->plane[plane].l_h[4];
895         for (i = 0; i < lowpass_width; i++) {
896             vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
897             low++;
898             high++;
899             output++;
900         }
901
902         low    = s->plane[plane].l_h[3];
903         high   = s->plane[plane].l_h[4];
904         output = s->plane[plane].subband[0];
905         for (i = 0; i < lowpass_height * 2; i++) {
906             horiz_filter(output, low, high, lowpass_width);
907             low    += lowpass_width;
908             high   += lowpass_width;
909             output += lowpass_width * 2;
910         }
911
912         output = s->plane[plane].subband[0];
913         for (i = 0; i < lowpass_height * 2; i++) {
914             for (j = 0; j < lowpass_width * 2; j++)
915                 output[j] *= 4;
916
917             output += lowpass_width * 2;
918         }
919
920         /* level 3 */
921         lowpass_height  = s->plane[plane].band[2][1].height;
922         lowpass_width   = s->plane[plane].band[2][1].width;
923         highpass_stride = s->plane[plane].band[2][1].stride;
924
925         if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
926             !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
927             av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
928             ret = AVERROR(EINVAL);
929             goto end;
930         }
931
932         av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
933         if (s->progressive) {
934             low    = s->plane[plane].subband[0];
935             high   = s->plane[plane].subband[8];
936             output = s->plane[plane].l_h[6];
937             for (i = 0; i < lowpass_width; i++) {
938                 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
939                 low++;
940                 high++;
941                 output++;
942             }
943
944             low    = s->plane[plane].subband[7];
945             high   = s->plane[plane].subband[9];
946             output = s->plane[plane].l_h[7];
947             for (i = 0; i < lowpass_width; i++) {
948                 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
949                 low++;
950                 high++;
951                 output++;
952             }
953
954             dst = (int16_t *)pic->data[act_plane];
955             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
956                 if (plane & 1)
957                     dst++;
958                 if (plane > 1)
959                     dst += pic->linesize[act_plane] >> 1;
960             }
961             low  = s->plane[plane].l_h[6];
962             high = s->plane[plane].l_h[7];
963
964             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
965                 (lowpass_height * 2 > avctx->coded_height / 2 ||
966                  lowpass_width  * 2 > avctx->coded_width  / 2    )
967                 ) {
968                 ret = AVERROR_INVALIDDATA;
969                 goto end;
970             }
971
972             for (i = 0; i < lowpass_height * 2; i++) {
973                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
974                     horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
975                 else
976                     horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
977                 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
978                     process_alpha(dst, lowpass_width * 2);
979                 low  += lowpass_width;
980                 high += lowpass_width;
981                 dst  += dst_linesize;
982             }
983         } else {
984             av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
985             pic->interlaced_frame = 1;
986             low    = s->plane[plane].subband[0];
987             high   = s->plane[plane].subband[7];
988             output = s->plane[plane].l_h[6];
989             for (i = 0; i < lowpass_height; i++) {
990                 horiz_filter(output, low, high, lowpass_width);
991                 low    += lowpass_width;
992                 high   += lowpass_width;
993                 output += lowpass_width * 2;
994             }
995
996             low    = s->plane[plane].subband[8];
997             high   = s->plane[plane].subband[9];
998             output = s->plane[plane].l_h[7];
999             for (i = 0; i < lowpass_height; i++) {
1000                 horiz_filter(output, low, high, lowpass_width);
1001                 low    += lowpass_width;
1002                 high   += lowpass_width;
1003                 output += lowpass_width * 2;
1004             }
1005
1006             dst  = (int16_t *)pic->data[act_plane];
1007             low  = s->plane[plane].l_h[6];
1008             high = s->plane[plane].l_h[7];
1009             for (i = 0; i < lowpass_height; i++) {
1010                 interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1011                 low  += lowpass_width * 2;
1012                 high += lowpass_width * 2;
1013                 dst  += pic->linesize[act_plane];
1014             }
1015         }
1016     }
1017
1018
1019     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1020         process_bayer(pic);
1021 end:
1022     if (ret < 0)
1023         return ret;
1024
1025     *got_frame = 1;
1026     return avpkt->size;
1027 }
1028
1029 static av_cold int cfhd_close(AVCodecContext *avctx)
1030 {
1031     CFHDContext *s = avctx->priv_data;
1032
1033     free_buffers(s);
1034
1035     ff_free_vlc(&s->vlc_9);
1036     ff_free_vlc(&s->vlc_18);
1037
1038     return 0;
1039 }
1040
1041 AVCodec ff_cfhd_decoder = {
1042     .name             = "cfhd",
1043     .long_name        = NULL_IF_CONFIG_SMALL("Cineform HD"),
1044     .type             = AVMEDIA_TYPE_VIDEO,
1045     .id               = AV_CODEC_ID_CFHD,
1046     .priv_data_size   = sizeof(CFHDContext),
1047     .init             = cfhd_init,
1048     .close            = cfhd_close,
1049     .decode           = cfhd_decode,
1050     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1051     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1052 };