]> git.sesse.net Git - ffmpeg/blob - libavcodec/cfhd.c
avcodec/cfhd: Require valid setup before Lowpass coefficients, BandHeader and BandSec...
[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->coded_format      = AV_PIX_FMT_YUV422P10;
92     s->cropped_height    = 0;
93     s->bpc               = 10;
94     s->channel_cnt       = 3;
95     s->subband_cnt       = SUBBAND_COUNT;
96     s->channel_num       = 0;
97     s->lowpass_precision = 16;
98     s->quantisation      = 1;
99     s->codebook          = 0;
100     s->difference_coding = 0;
101     s->frame_type        = 0;
102     s->sample_type       = 0;
103     if (s->transform_type != 2)
104         s->transform_type = -1;
105     init_plane_defaults(s);
106     init_peak_table_defaults(s);
107 }
108
109 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
110 {
111     if (codebook == 0 || codebook == 1) {
112         return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
113     } else
114         return level * quantisation;
115 }
116
117 static inline void difference_coding(int16_t *band, int width, int height)
118 {
119
120     int i,j;
121     for (i = 0; i < height; i++) {
122         for (j = 1; j < width; j++) {
123           band[j] += band[j-1];
124         }
125         band += width;
126     }
127 }
128
129 static inline void peak_table(int16_t *band, Peak *peak, int length)
130 {
131     int i;
132     for (i = 0; i < length; i++)
133         if (abs(band[i]) > peak->level)
134             band[i] = bytestream2_get_le16(&peak->base);
135 }
136
137 static inline void process_alpha(int16_t *alpha, int width)
138 {
139     int i, channel;
140     for (i = 0; i < width; i++) {
141         channel   = alpha[i];
142         channel  -= ALPHA_COMPAND_DC_OFFSET;
143         channel <<= 3;
144         channel  *= ALPHA_COMPAND_GAIN;
145         channel >>= 16;
146         channel   = av_clip_uintp2(channel, 12);
147         alpha[i]  = channel;
148     }
149 }
150
151 static inline void process_bayer(AVFrame *frame, int bpc)
152 {
153     const int linesize = frame->linesize[0];
154     uint16_t *r = (uint16_t *)frame->data[0];
155     uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
156     uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
157     uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
158     const int mid = 1 << (bpc - 1);
159     const int factor = 1 << (16 - bpc);
160
161     for (int y = 0; y < frame->height >> 1; y++) {
162         for (int x = 0; x < frame->width; x += 2) {
163             int R, G1, G2, B;
164             int g, rg, bg, gd;
165
166             g  = r[x];
167             rg = g1[x];
168             bg = g2[x];
169             gd = b[x];
170             gd -= mid;
171
172             R  = (rg - mid) * 2 + g;
173             G1 = g + gd;
174             G2 = g - gd;
175             B  = (bg - mid) * 2 + g;
176
177             R  = av_clip_uintp2(R  * factor, 16);
178             G1 = av_clip_uintp2(G1 * factor, 16);
179             G2 = av_clip_uintp2(G2 * factor, 16);
180             B  = av_clip_uintp2(B  * factor, 16);
181
182             r[x]  = R;
183             g1[x] = G1;
184             g2[x] = G2;
185             b[x]  = B;
186         }
187
188         r  += linesize;
189         g1 += linesize;
190         g2 += linesize;
191         b  += linesize;
192     }
193 }
194
195 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
196                          int width, int linesize, int plane)
197 {
198     int i;
199     int16_t even, odd;
200     for (i = 0; i < width; i++) {
201         even = (low[i] - high[i])/2;
202         odd  = (low[i] + high[i])/2;
203         output[i]            = av_clip_uintp2(even, 10);
204         output[i + linesize] = av_clip_uintp2(odd, 10);
205     }
206 }
207
208 static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
209 {
210     for (int i = 0; i < width; i++) {
211         int even = (low[i] - high[i]) / 2;
212         int odd  = (low[i] + high[i]) / 2;
213
214         low[i]  = even;
215         high[i] = odd;
216     }
217 }
218
219 static void free_buffers(CFHDContext *s)
220 {
221     int i, j;
222
223     for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
224         av_freep(&s->plane[i].idwt_buf);
225         av_freep(&s->plane[i].idwt_tmp);
226         s->plane[i].idwt_size = 0;
227
228         for (j = 0; j < SUBBAND_COUNT_3D; j++)
229             s->plane[i].subband[j] = NULL;
230
231         for (j = 0; j < 10; j++)
232             s->plane[i].l_h[j] = NULL;
233     }
234     s->a_height = 0;
235     s->a_width  = 0;
236     s->a_transform_type = INT_MIN;
237 }
238
239 static int alloc_buffers(AVCodecContext *avctx)
240 {
241     CFHDContext *s = avctx->priv_data;
242     int i, j, ret, planes, bayer = 0;
243     int chroma_x_shift, chroma_y_shift;
244     unsigned k;
245
246     if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
247         return ret;
248     avctx->pix_fmt = s->coded_format;
249
250     ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
251
252     if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
253                                                 &chroma_x_shift,
254                                                 &chroma_y_shift)) < 0)
255         return ret;
256     planes = av_pix_fmt_count_planes(s->coded_format);
257     if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
258         planes = 4;
259         chroma_x_shift = 1;
260         chroma_y_shift = 1;
261         bayer = 1;
262     }
263
264     for (i = 0; i < planes; i++) {
265         int w8, h8, w4, h4, w2, h2;
266         int width  = (i || bayer) ? s->coded_width  >> chroma_x_shift : s->coded_width;
267         int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
268         ptrdiff_t stride = (FFALIGN(width  / 8, 8) + 64) * 8;
269
270         if (chroma_y_shift && !bayer)
271             height = FFALIGN(height / 8, 2) * 8;
272         s->plane[i].width  = width;
273         s->plane[i].height = height;
274         s->plane[i].stride = stride;
275
276         w8 = FFALIGN(s->plane[i].width  / 8, 8) + 64;
277         h8 = FFALIGN(height, 8) / 8;
278         w4 = w8 * 2;
279         h4 = h8 * 2;
280         w2 = w4 * 2;
281         h2 = h4 * 2;
282
283         if (s->transform_type == 0) {
284             s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
285             s->plane[i].idwt_buf =
286                 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
287             s->plane[i].idwt_tmp =
288                 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
289         } else {
290             s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
291             s->plane[i].idwt_buf =
292                 av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
293             s->plane[i].idwt_tmp =
294                 av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
295         }
296
297         if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
298             return AVERROR(ENOMEM);
299
300         s->plane[i].subband[0] = s->plane[i].idwt_buf;
301         s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
302         s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
303         s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
304         s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
305         s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
306         s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
307         if (s->transform_type == 0) {
308             s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
309             s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
310             s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
311         } else {
312             int16_t *frame2 =
313             s->plane[i].subband[7]  = s->plane[i].idwt_buf + 4 * w2 * h2;
314             s->plane[i].subband[8]  = frame2 + 2 * w4 * h4;
315             s->plane[i].subband[9]  = frame2 + 1 * w4 * h4;
316             s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
317             s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
318             s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
319             s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
320             s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
321             s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
322             s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
323         }
324
325         if (s->transform_type == 0) {
326             for (j = 0; j < DWT_LEVELS; j++) {
327                 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
328                     s->plane[i].band[j][k].a_width  = w8 << j;
329                     s->plane[i].band[j][k].a_height = h8 << j;
330                 }
331             }
332         } else {
333             for (j = 0; j < DWT_LEVELS_3D; j++) {
334                 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
335
336                 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
337                     s->plane[i].band[j][k].a_width  = w8 << t;
338                     s->plane[i].band[j][k].a_height = h8 << t;
339                 }
340             }
341         }
342
343         /* ll2 and ll1 commented out because they are done in-place */
344         s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
345         s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
346         // s->plane[i].l_h[2] = ll2;
347         s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
348         s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
349         // s->plane[i].l_h[5] = ll1;
350         s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
351         s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
352         if (s->transform_type != 0) {
353             int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
354
355             s->plane[i].l_h[8] = frame2;
356             s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
357         }
358     }
359
360     s->a_transform_type = s->transform_type;
361     s->a_height = s->coded_height;
362     s->a_width  = s->coded_width;
363     s->a_format = s->coded_format;
364
365     return 0;
366 }
367
368 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
369                        AVPacket *avpkt)
370 {
371     CFHDContext *s = avctx->priv_data;
372     CFHDDSPContext *dsp = &s->dsp;
373     GetByteContext gb;
374     ThreadFrame frame = { .f = data };
375     AVFrame *pic = data;
376     int ret = 0, i, j, plane, got_buffer = 0;
377     int16_t *coeff_data;
378
379     init_frame_defaults(s);
380     s->planes = av_pix_fmt_count_planes(s->coded_format);
381
382     bytestream2_init(&gb, avpkt->data, avpkt->size);
383
384     while (bytestream2_get_bytes_left(&gb) >= 4) {
385         /* Bit weird but implement the tag parsing as the spec says */
386         uint16_t tagu   = bytestream2_get_be16(&gb);
387         int16_t tag     = (int16_t)tagu;
388         int8_t tag8     = (int8_t)(tagu >> 8);
389         uint16_t abstag = abs(tag);
390         int8_t abs_tag8 = abs(tag8);
391         uint16_t data   = bytestream2_get_be16(&gb);
392         if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
393             av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
394         } else if (tag == SampleFlags) {
395             av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
396             s->progressive = data & 0x0001;
397         } else if (tag == FrameType) {
398             s->frame_type = data;
399             av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
400         } else if (abstag == VersionMajor) {
401             av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
402         } else if (abstag == VersionMinor) {
403             av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
404         } else if (abstag == VersionRevision) {
405             av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
406         } else if (abstag == VersionEdit) {
407             av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
408         } else if (abstag == Version) {
409             av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
410         } else if (tag == ImageWidth) {
411             av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
412             s->coded_width = data;
413         } else if (tag == ImageHeight) {
414             av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
415             s->coded_height = data;
416         } else if (tag == ChannelCount) {
417             av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
418             s->channel_cnt = data;
419             if (data > 4) {
420                 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
421                 ret = AVERROR_PATCHWELCOME;
422                 goto end;
423             }
424         } else if (tag == SubbandCount) {
425             av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
426             if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
427                 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
428                 ret = AVERROR_PATCHWELCOME;
429                 goto end;
430             }
431         } else if (tag == ChannelNumber) {
432             s->channel_num = data;
433             av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
434             if (s->channel_num >= s->planes) {
435                 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
436                 ret = AVERROR(EINVAL);
437                 goto end;
438             }
439             init_plane_defaults(s);
440         } else if (tag == SubbandNumber) {
441             if (s->subband_num != 0 && data == 1 && (s->transform_type == 0 || s->transform_type == 2))  // hack
442                 s->level++;
443             av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
444             s->subband_num = data;
445             if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
446                 (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
447                 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
448                 ret = AVERROR(EINVAL);
449                 goto end;
450             }
451             if (s->subband_num > 3) {
452                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
453                 ret = AVERROR(EINVAL);
454                 goto end;
455             }
456         } else if (tag == SubbandBand) {
457             av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
458             if ((s->transform_type == 0 && data >= SUBBAND_COUNT) ||
459                 (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) {
460                 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
461                 ret = AVERROR(EINVAL);
462                 goto end;
463             }
464             if (s->transform_type == 0 || s->transform_type == 2)
465                 s->subband_num_actual = data;
466             else
467                 av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data);
468         } else if (tag == LowpassPrecision)
469             av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
470         else if (tag == Quantization) {
471             s->quantisation = data;
472             av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
473         } else if (tag == PrescaleTable) {
474             for (i = 0; i < 8; i++)
475                 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
476             av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
477         } else if (tag == BandEncoding) {
478             if (!data || data > 5) {
479                 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
480                 ret = AVERROR(EINVAL);
481                 goto end;
482             }
483             s->band_encoding = data;
484             av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
485         } else if (tag == LowpassWidth) {
486             av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
487             s->plane[s->channel_num].band[0][0].width  = data;
488             s->plane[s->channel_num].band[0][0].stride = data;
489         } else if (tag == LowpassHeight) {
490             av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
491             s->plane[s->channel_num].band[0][0].height = data;
492         } else if (tag == SampleType) {
493             s->sample_type = data;
494             av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
495         } else if (tag == TransformType) {
496             if (data > 2) {
497                 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
498                 ret = AVERROR(EINVAL);
499                 goto end;
500             } else if (data == 1) {
501                 av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
502                 ret = AVERROR_PATCHWELCOME;
503                 goto end;
504             }
505             if (s->transform_type == -1) {
506                 s->transform_type = data;
507                 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
508             } else {
509                 av_log(avctx, AV_LOG_DEBUG, "Ignoring additional transform type %"PRIu16"\n", data);
510             }
511         } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
512             if (abstag == 0x4001)
513                 s->peak.level = 0;
514             av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
515             bytestream2_skipu(&gb, data * 4);
516         } else if (tag == FrameIndex) {
517             av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
518             s->frame_index = data;
519         } else if (tag == SampleIndexTable) {
520             av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
521             if (data > bytestream2_get_bytes_left(&gb) / 4) {
522                 av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
523                 ret = AVERROR_INVALIDDATA;
524                 goto end;
525             }
526             for (i = 0; i < data; i++) {
527                 uint32_t offset = bytestream2_get_be32(&gb);
528                 av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
529             }
530         } else if (tag == HighpassWidth) {
531             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);
532             if (data < 3) {
533                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
534                 ret = AVERROR(EINVAL);
535                 goto end;
536             }
537             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
538             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
539         } else if (tag == HighpassHeight) {
540             av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
541             if (data < 3) {
542                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
543                 ret = AVERROR(EINVAL);
544                 goto end;
545             }
546             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
547         } else if (tag == BandWidth) {
548             av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
549             if (data < 3) {
550                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
551                 ret = AVERROR(EINVAL);
552                 goto end;
553             }
554             s->plane[s->channel_num].band[s->level][s->subband_num].width  = data;
555             s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
556         } else if (tag == BandHeight) {
557             av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
558             if (data < 3) {
559                 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
560                 ret = AVERROR(EINVAL);
561                 goto end;
562             }
563             s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
564         } else if (tag == InputFormat) {
565             av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
566             if (s->coded_format == AV_PIX_FMT_NONE ||
567                 s->coded_format == AV_PIX_FMT_YUV422P10) {
568                 if (data >= 100 && data <= 105) {
569                     s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
570                 } else if (data >= 122 && data <= 128) {
571                     s->coded_format = AV_PIX_FMT_GBRP12;
572                 } else if (data == 30) {
573                     s->coded_format = AV_PIX_FMT_GBRAP12;
574                 } else {
575                     s->coded_format = AV_PIX_FMT_YUV422P10;
576                 }
577                 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
578             }
579         } else if (tag == BandCodingFlags) {
580             s->codebook = data & 0xf;
581             s->difference_coding = (data >> 4) & 1;
582             av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
583         } else if (tag == Precision) {
584             av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
585             if (!(data == 10 || data == 12)) {
586                 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
587                 ret = AVERROR(EINVAL);
588                 goto end;
589             }
590             avctx->bits_per_raw_sample = s->bpc = data;
591         } else if (tag == EncodedFormat) {
592             av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
593             if (data == 1) {
594                 s->coded_format = AV_PIX_FMT_YUV422P10;
595             } else if (data == 2) {
596                 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
597             } else if (data == 3) {
598                 s->coded_format = AV_PIX_FMT_GBRP12;
599             } else if (data == 4) {
600                 s->coded_format = AV_PIX_FMT_GBRAP12;
601             } else {
602                 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
603                 ret = AVERROR_PATCHWELCOME;
604                 goto end;
605             }
606             s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
607         } else if (tag == -DisplayHeight) {
608             av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
609             s->cropped_height = data;
610         } else if (tag == -PeakOffsetLow) {
611             s->peak.offset &= ~0xffff;
612             s->peak.offset |= (data & 0xffff);
613             s->peak.base    = gb;
614             s->peak.level   = 0;
615         } else if (tag == -PeakOffsetHigh) {
616             s->peak.offset &= 0xffff;
617             s->peak.offset |= (data & 0xffffU)<<16;
618             s->peak.base    = gb;
619             s->peak.level   = 0;
620         } else if (tag == -PeakLevel && s->peak.offset) {
621             s->peak.level = data;
622             if (s->peak.offset < 4 - bytestream2_tell(&s->peak.base) ||
623                 s->peak.offset > 4 + bytestream2_get_bytes_left(&s->peak.base)
624             ) {
625                 ret = AVERROR_INVALIDDATA;
626                 goto end;
627             }
628             bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
629         } else
630             av_log(avctx, AV_LOG_DEBUG,  "Unknown tag %i data %x\n", tag, data);
631
632         if (tag == BitstreamMarker && data == 0xf0f &&
633             s->coded_format != AV_PIX_FMT_NONE) {
634             int lowpass_height = s->plane[s->channel_num].band[0][0].height;
635             int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
636             int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
637
638             if (s->coded_width) {
639                 s->coded_width *= factor;
640             }
641
642             if (s->coded_height) {
643                 s->coded_height *= factor;
644             }
645
646             if (!s->a_width && !s->coded_width) {
647                 s->coded_width = lowpass_width * factor * 8;
648             }
649
650             if (!s->a_height && !s->coded_height) {
651                 s->coded_height = lowpass_height * factor * 8;
652             }
653
654             if (s->a_width && !s->coded_width)
655                 s->coded_width = s->a_width;
656             if (s->a_height && !s->coded_height)
657                 s->coded_height = s->a_height;
658
659             if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
660                 s->a_format != s->coded_format ||
661                 s->transform_type != s->a_transform_type) {
662                 free_buffers(s);
663                 if ((ret = alloc_buffers(avctx)) < 0) {
664                     free_buffers(s);
665                     return ret;
666                 }
667             }
668             ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
669             if (ret < 0)
670                 return ret;
671             if (s->cropped_height) {
672                 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
673                 if (avctx->height < height)
674                     return AVERROR_INVALIDDATA;
675                 avctx->height = height;
676             }
677             frame.f->width =
678             frame.f->height = 0;
679
680             if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
681                 return ret;
682
683             s->coded_width = 0;
684             s->coded_height = 0;
685             s->coded_format = AV_PIX_FMT_NONE;
686             got_buffer = 1;
687         } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
688             frame.f->width =
689             frame.f->height = 0;
690
691             if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
692                 return ret;
693             s->coded_width = 0;
694             s->coded_height = 0;
695             s->coded_format = AV_PIX_FMT_NONE;
696             got_buffer = 1;
697         }
698
699         if (s->subband_num_actual == 255)
700             goto finish;
701         coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
702
703         /* Lowpass coefficients */
704         if (tag == BitstreamMarker && data == 0xf0f) {
705             int lowpass_height, lowpass_width, lowpass_a_height, lowpass_a_width;
706
707             if (!s->a_width || !s->a_height) {
708                 ret = AVERROR_INVALIDDATA;
709                 goto end;
710             }
711
712             lowpass_height = s->plane[s->channel_num].band[0][0].height;
713             lowpass_width  = s->plane[s->channel_num].band[0][0].width;
714             lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
715             lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
716
717             if (lowpass_width < 3 ||
718                 lowpass_width > lowpass_a_width) {
719                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
720                 ret = AVERROR(EINVAL);
721                 goto end;
722             }
723
724             if (lowpass_height < 3 ||
725                 lowpass_height > lowpass_a_height) {
726                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
727                 ret = AVERROR(EINVAL);
728                 goto end;
729             }
730
731             if (!got_buffer) {
732                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
733                 ret = AVERROR(EINVAL);
734                 goto end;
735             }
736
737             if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
738                 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
739                 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
740                 ret = AVERROR(EINVAL);
741                 goto end;
742             }
743
744             av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
745             for (i = 0; i < lowpass_height; i++) {
746                 for (j = 0; j < lowpass_width; j++)
747                     coeff_data[j] = bytestream2_get_be16u(&gb);
748
749                 coeff_data += lowpass_width;
750             }
751
752             /* Align to mod-4 position to continue reading tags */
753             bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
754
755             /* Copy last line of coefficients if odd height */
756             if (lowpass_height & 1) {
757                 memcpy(&coeff_data[lowpass_height * lowpass_width],
758                        &coeff_data[(lowpass_height - 1) * lowpass_width],
759                        lowpass_width * sizeof(*coeff_data));
760             }
761
762             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
763         }
764
765         av_assert0(s->subband_num_actual != 255);
766         if (tag == BandHeader || tag == BandSecondPass) {
767             int highpass_height, highpass_width, highpass_a_width, highpass_a_height, highpass_stride, a_expected;
768             int expected;
769             int level, run, coeff;
770             int count = 0, bytes;
771
772             if (!s->a_width || !s->a_height) {
773                 ret = AVERROR_INVALIDDATA;
774                 goto end;
775             }
776
777             highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
778             highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
779             highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
780             highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
781             highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
782             a_expected = highpass_a_height * highpass_a_width;
783
784             if (!got_buffer) {
785                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
786                 ret = AVERROR(EINVAL);
787                 goto end;
788             }
789
790             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
791                 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
792                 ret = AVERROR(EINVAL);
793                 goto end;
794             }
795             expected = highpass_height * highpass_stride;
796
797             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);
798
799             ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
800             if (ret < 0)
801                 goto end;
802             {
803                 OPEN_READER(re, &s->gb);
804
805                 const int lossless = s->band_encoding == 5;
806
807                 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
808                     s->codebook = 1;
809                 if (!s->codebook) {
810                     while (1) {
811                         UPDATE_CACHE(re, &s->gb);
812                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
813                                    VLC_BITS, 3, 1);
814
815                         /* escape */
816                         if (level == 64)
817                             break;
818
819                         count += run;
820
821                         if (count > expected)
822                             break;
823
824                         if (!lossless)
825                             coeff = dequant_and_decompand(s, level, s->quantisation, 0);
826                         else
827                             coeff = level;
828                         if (tag == BandSecondPass) {
829                             const uint16_t q = s->quantisation;
830
831                             for (i = 0; i < run; i++) {
832                                 *coeff_data |= coeff * 256;
833                                 *coeff_data++ *= q;
834                             }
835                         } else {
836                             for (i = 0; i < run; i++)
837                                 *coeff_data++ = coeff;
838                         }
839                     }
840                 } else {
841                     while (1) {
842                         UPDATE_CACHE(re, &s->gb);
843                         GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
844                                    VLC_BITS, 3, 1);
845
846                         /* escape */
847                         if (level == 255 && run == 2)
848                             break;
849
850                         count += run;
851
852                         if (count > expected)
853                             break;
854
855                         if (!lossless)
856                             coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
857                         else
858                             coeff = level;
859                         if (tag == BandSecondPass) {
860                             const uint16_t q = s->quantisation;
861
862                             for (i = 0; i < run; i++) {
863                                 *coeff_data |= coeff * 256;
864                                 *coeff_data++ *= q;
865                             }
866                         } else {
867                             for (i = 0; i < run; i++)
868                                 *coeff_data++ = coeff;
869                         }
870                     }
871                 }
872                 CLOSE_READER(re, &s->gb);
873             }
874
875             if (count > expected) {
876                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
877                 ret = AVERROR(EINVAL);
878                 goto end;
879             }
880             if (s->peak.level)
881                 peak_table(coeff_data - count, &s->peak, count);
882             if (s->difference_coding)
883                 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
884
885             bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
886             if (bytes > bytestream2_get_bytes_left(&gb)) {
887                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
888                 ret = AVERROR(EINVAL);
889                 goto end;
890             } else
891                 bytestream2_seek(&gb, bytes, SEEK_CUR);
892
893             av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
894 finish:
895             if (s->subband_num_actual != 255)
896                 s->codebook = 0;
897         }
898     }
899
900     s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
901     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
902         s->progressive = 1;
903         s->planes = 4;
904     }
905
906     ff_thread_finish_setup(avctx);
907
908     if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
909         s->a_transform_type == INT_MIN ||
910         s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
911         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
912         ret = AVERROR(EINVAL);
913         goto end;
914     }
915
916     if (!got_buffer) {
917         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
918         ret = AVERROR(EINVAL);
919         goto end;
920     }
921
922     if (s->transform_type == 0 && s->sample_type != 1) {
923         for (plane = 0; plane < s->planes && !ret; plane++) {
924             /* level 1 */
925             int lowpass_height  = s->plane[plane].band[0][0].height;
926             int output_stride   = s->plane[plane].band[0][0].a_width;
927             int lowpass_width   = s->plane[plane].band[0][0].width;
928             int highpass_stride = s->plane[plane].band[0][1].stride;
929             int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
930             ptrdiff_t dst_linesize;
931             int16_t *low, *high, *output, *dst;
932
933             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
934                 act_plane = 0;
935                 dst_linesize = pic->linesize[act_plane];
936             } else {
937                 dst_linesize = pic->linesize[act_plane] / 2;
938             }
939
940             if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
941                 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
942                 lowpass_width < 3 || lowpass_height < 3) {
943                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
944                 ret = AVERROR(EINVAL);
945                 goto end;
946             }
947
948             av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
949
950             low    = s->plane[plane].subband[0];
951             high   = s->plane[plane].subband[2];
952             output = s->plane[plane].l_h[0];
953             dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
954
955             low    = s->plane[plane].subband[1];
956             high   = s->plane[plane].subband[3];
957             output = s->plane[plane].l_h[1];
958
959             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
960
961             low    = s->plane[plane].l_h[0];
962             high   = s->plane[plane].l_h[1];
963             output = s->plane[plane].subband[0];
964             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
965             if (s->bpc == 12) {
966                 output = s->plane[plane].subband[0];
967                 for (i = 0; i < lowpass_height * 2; i++) {
968                     for (j = 0; j < lowpass_width * 2; j++)
969                         output[j] *= 4;
970
971                     output += output_stride * 2;
972                 }
973             }
974
975             /* level 2 */
976             lowpass_height  = s->plane[plane].band[1][1].height;
977             output_stride   = s->plane[plane].band[1][1].a_width;
978             lowpass_width   = s->plane[plane].band[1][1].width;
979             highpass_stride = s->plane[plane].band[1][1].stride;
980
981             if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
982                 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
983                 lowpass_width < 3 || lowpass_height < 3) {
984                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
985                 ret = AVERROR(EINVAL);
986                 goto end;
987             }
988
989             av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
990
991             low    = s->plane[plane].subband[0];
992             high   = s->plane[plane].subband[5];
993             output = s->plane[plane].l_h[3];
994             dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
995
996             low    = s->plane[plane].subband[4];
997             high   = s->plane[plane].subband[6];
998             output = s->plane[plane].l_h[4];
999             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1000
1001             low    = s->plane[plane].l_h[3];
1002             high   = s->plane[plane].l_h[4];
1003             output = s->plane[plane].subband[0];
1004             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1005
1006             output = s->plane[plane].subband[0];
1007             for (i = 0; i < lowpass_height * 2; i++) {
1008                 for (j = 0; j < lowpass_width * 2; j++)
1009                     output[j] *= 4;
1010
1011                 output += output_stride * 2;
1012             }
1013
1014             /* level 3 */
1015             lowpass_height  = s->plane[plane].band[2][1].height;
1016             output_stride   = s->plane[plane].band[2][1].a_width;
1017             lowpass_width   = s->plane[plane].band[2][1].width;
1018             highpass_stride = s->plane[plane].band[2][1].stride;
1019
1020             if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1021                 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
1022                 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
1023                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1024                 ret = AVERROR(EINVAL);
1025                 goto end;
1026             }
1027
1028             av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1029             if (s->progressive) {
1030                 low    = s->plane[plane].subband[0];
1031                 high   = s->plane[plane].subband[8];
1032                 output = s->plane[plane].l_h[6];
1033                 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1034
1035                 low    = s->plane[plane].subband[7];
1036                 high   = s->plane[plane].subband[9];
1037                 output = s->plane[plane].l_h[7];
1038                 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1039
1040                 dst = (int16_t *)pic->data[act_plane];
1041                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1042                     if (plane & 1)
1043                         dst++;
1044                     if (plane > 1)
1045                         dst += pic->linesize[act_plane] >> 1;
1046                 }
1047                 low  = s->plane[plane].l_h[6];
1048                 high = s->plane[plane].l_h[7];
1049
1050                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1051                     (lowpass_height * 2 > avctx->coded_height / 2 ||
1052                      lowpass_width  * 2 > avctx->coded_width  / 2    )
1053                     ) {
1054                     ret = AVERROR_INVALIDDATA;
1055                     goto end;
1056                 }
1057
1058                 for (i = 0; i < s->plane[act_plane].height; i++) {
1059                     dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1060                     if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1061                         process_alpha(dst, lowpass_width * 2);
1062                     low  += output_stride;
1063                     high += output_stride;
1064                     dst  += dst_linesize;
1065                 }
1066             } else {
1067                 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1068                 pic->interlaced_frame = 1;
1069                 low    = s->plane[plane].subband[0];
1070                 high   = s->plane[plane].subband[7];
1071                 output = s->plane[plane].l_h[6];
1072                 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1073
1074                 low    = s->plane[plane].subband[8];
1075                 high   = s->plane[plane].subband[9];
1076                 output = s->plane[plane].l_h[7];
1077                 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1078
1079                 dst  = (int16_t *)pic->data[act_plane];
1080                 low  = s->plane[plane].l_h[6];
1081                 high = s->plane[plane].l_h[7];
1082                 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1083                     interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1084                     low  += output_stride * 2;
1085                     high += output_stride * 2;
1086                     dst  += pic->linesize[act_plane];
1087                 }
1088             }
1089         }
1090     } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1091         for (plane = 0; plane < s->planes && !ret; plane++) {
1092             int lowpass_height  = s->plane[plane].band[0][0].height;
1093             int output_stride   = s->plane[plane].band[0][0].a_width;
1094             int lowpass_width   = s->plane[plane].band[0][0].width;
1095             int highpass_stride = s->plane[plane].band[0][1].stride;
1096             int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1097             int16_t *low, *high, *output, *dst;
1098             ptrdiff_t dst_linesize;
1099
1100             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1101                 act_plane = 0;
1102                 dst_linesize = pic->linesize[act_plane];
1103             } else {
1104                 dst_linesize = pic->linesize[act_plane] / 2;
1105             }
1106
1107             if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1108                 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1109                 lowpass_width < 3 || lowpass_height < 3) {
1110                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1111                 ret = AVERROR(EINVAL);
1112                 goto end;
1113             }
1114
1115             av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1116
1117             low    = s->plane[plane].subband[0];
1118             high   = s->plane[plane].subband[2];
1119             output = s->plane[plane].l_h[0];
1120             dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1121
1122             low    = s->plane[plane].subband[1];
1123             high   = s->plane[plane].subband[3];
1124             output = s->plane[plane].l_h[1];
1125             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1126
1127             low    = s->plane[plane].l_h[0];
1128             high   = s->plane[plane].l_h[1];
1129             output = s->plane[plane].l_h[7];
1130             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1131             if (s->bpc == 12) {
1132                 output = s->plane[plane].l_h[7];
1133                 for (i = 0; i < lowpass_height * 2; i++) {
1134                     for (j = 0; j < lowpass_width * 2; j++)
1135                         output[j] *= 4;
1136
1137                     output += output_stride * 2;
1138                 }
1139             }
1140
1141             lowpass_height  = s->plane[plane].band[1][1].height;
1142             output_stride   = s->plane[plane].band[1][1].a_width;
1143             lowpass_width   = s->plane[plane].band[1][1].width;
1144             highpass_stride = s->plane[plane].band[1][1].stride;
1145
1146             if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1147                 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1148                 lowpass_width < 3 || lowpass_height < 3) {
1149                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1150                 ret = AVERROR(EINVAL);
1151                 goto end;
1152             }
1153
1154             av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1155
1156             low    = s->plane[plane].l_h[7];
1157             high   = s->plane[plane].subband[5];
1158             output = s->plane[plane].l_h[3];
1159             dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1160
1161             low    = s->plane[plane].subband[4];
1162             high   = s->plane[plane].subband[6];
1163             output = s->plane[plane].l_h[4];
1164             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1165
1166             low    = s->plane[plane].l_h[3];
1167             high   = s->plane[plane].l_h[4];
1168             output = s->plane[plane].l_h[7];
1169             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1170
1171             output = s->plane[plane].l_h[7];
1172             for (i = 0; i < lowpass_height * 2; i++) {
1173                 for (j = 0; j < lowpass_width * 2; j++)
1174                     output[j] *= 4;
1175                 output += output_stride * 2;
1176             }
1177
1178             low    = s->plane[plane].subband[7];
1179             high   = s->plane[plane].subband[9];
1180             output = s->plane[plane].l_h[3];
1181             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1182
1183             low    = s->plane[plane].subband[8];
1184             high   = s->plane[plane].subband[10];
1185             output = s->plane[plane].l_h[4];
1186             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1187
1188             low    = s->plane[plane].l_h[3];
1189             high   = s->plane[plane].l_h[4];
1190             output = s->plane[plane].l_h[9];
1191             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1192
1193             lowpass_height  = s->plane[plane].band[4][1].height;
1194             output_stride   = s->plane[plane].band[4][1].a_width;
1195             lowpass_width   = s->plane[plane].band[4][1].width;
1196             highpass_stride = s->plane[plane].band[4][1].stride;
1197             av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1198
1199             if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1200                 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1201                 lowpass_width < 3 || lowpass_height < 3) {
1202                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1203                 ret = AVERROR(EINVAL);
1204                 goto end;
1205             }
1206
1207             low    = s->plane[plane].l_h[7];
1208             high   = s->plane[plane].l_h[9];
1209             output = s->plane[plane].l_h[7];
1210             for (i = 0; i < lowpass_height; i++) {
1211                 inverse_temporal_filter(low, high, lowpass_width);
1212                 low    += output_stride;
1213                 high   += output_stride;
1214             }
1215             if (s->progressive) {
1216                 low    = s->plane[plane].l_h[7];
1217                 high   = s->plane[plane].subband[15];
1218                 output = s->plane[plane].l_h[6];
1219                 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1220
1221                 low    = s->plane[plane].subband[14];
1222                 high   = s->plane[plane].subband[16];
1223                 output = s->plane[plane].l_h[7];
1224                 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1225
1226                 low    = s->plane[plane].l_h[9];
1227                 high   = s->plane[plane].subband[12];
1228                 output = s->plane[plane].l_h[8];
1229                 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1230
1231                 low    = s->plane[plane].subband[11];
1232                 high   = s->plane[plane].subband[13];
1233                 output = s->plane[plane].l_h[9];
1234                 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1235
1236                 if (s->sample_type == 1)
1237                     continue;
1238
1239                 dst = (int16_t *)pic->data[act_plane];
1240                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1241                     if (plane & 1)
1242                         dst++;
1243                     if (plane > 1)
1244                         dst += pic->linesize[act_plane] >> 1;
1245                 }
1246
1247                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1248                     (lowpass_height * 2 > avctx->coded_height / 2 ||
1249                      lowpass_width  * 2 > avctx->coded_width  / 2    )
1250                     ) {
1251                     ret = AVERROR_INVALIDDATA;
1252                     goto end;
1253                 }
1254
1255                 low  = s->plane[plane].l_h[6];
1256                 high = s->plane[plane].l_h[7];
1257                 for (i = 0; i < s->plane[act_plane].height; i++) {
1258                     dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1259                     low  += output_stride;
1260                     high += output_stride;
1261                     dst  += dst_linesize;
1262                 }
1263             } else {
1264                 pic->interlaced_frame = 1;
1265                 low    = s->plane[plane].l_h[7];
1266                 high   = s->plane[plane].subband[14];
1267                 output = s->plane[plane].l_h[6];
1268                 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1269
1270                 low    = s->plane[plane].subband[15];
1271                 high   = s->plane[plane].subband[16];
1272                 output = s->plane[plane].l_h[7];
1273                 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1274
1275                 low    = s->plane[plane].l_h[9];
1276                 high   = s->plane[plane].subband[11];
1277                 output = s->plane[plane].l_h[8];
1278                 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1279
1280                 low    = s->plane[plane].subband[12];
1281                 high   = s->plane[plane].subband[13];
1282                 output = s->plane[plane].l_h[9];
1283                 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1284
1285                 if (s->sample_type == 1)
1286                     continue;
1287
1288                 dst  = (int16_t *)pic->data[act_plane];
1289                 low  = s->plane[plane].l_h[6];
1290                 high = s->plane[plane].l_h[7];
1291                 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1292                     interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1293                     low  += output_stride * 2;
1294                     high += output_stride * 2;
1295                     dst  += pic->linesize[act_plane];
1296                 }
1297             }
1298         }
1299     }
1300
1301     if (s->transform_type == 2 && s->sample_type == 1) {
1302         int16_t *low, *high, *dst;
1303         int output_stride, lowpass_height, lowpass_width;
1304         ptrdiff_t dst_linesize;
1305
1306         for (plane = 0; plane < s->planes; plane++) {
1307             int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1308
1309             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1310                 act_plane = 0;
1311                 dst_linesize = pic->linesize[act_plane];
1312             } else {
1313                 dst_linesize = pic->linesize[act_plane] / 2;
1314             }
1315
1316             lowpass_height  = s->plane[plane].band[4][1].height;
1317             output_stride   = s->plane[plane].band[4][1].a_width;
1318             lowpass_width   = s->plane[plane].band[4][1].width;
1319
1320             if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1321                 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1322                 lowpass_width < 3 || lowpass_height < 3) {
1323                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1324                 ret = AVERROR(EINVAL);
1325                 goto end;
1326             }
1327
1328             if (s->progressive) {
1329                 dst = (int16_t *)pic->data[act_plane];
1330                 low  = s->plane[plane].l_h[8];
1331                 high = s->plane[plane].l_h[9];
1332
1333                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1334                     if (plane & 1)
1335                         dst++;
1336                     if (plane > 1)
1337                         dst += pic->linesize[act_plane] >> 1;
1338                 }
1339
1340                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1341                     (lowpass_height * 2 > avctx->coded_height / 2 ||
1342                      lowpass_width  * 2 > avctx->coded_width  / 2    )
1343                     ) {
1344                     ret = AVERROR_INVALIDDATA;
1345                     goto end;
1346                 }
1347
1348                 for (i = 0; i < s->plane[act_plane].height; i++) {
1349                     dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1350                     low  += output_stride;
1351                     high += output_stride;
1352                     dst  += dst_linesize;
1353                 }
1354             } else {
1355                 dst  = (int16_t *)pic->data[act_plane];
1356                 low  = s->plane[plane].l_h[8];
1357                 high = s->plane[plane].l_h[9];
1358                 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1359                     interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1360                     low  += output_stride * 2;
1361                     high += output_stride * 2;
1362                     dst  += pic->linesize[act_plane];
1363                 }
1364             }
1365         }
1366     }
1367
1368     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1369         process_bayer(pic, s->bpc);
1370 end:
1371     if (ret < 0)
1372         return ret;
1373
1374     *got_frame = 1;
1375     return avpkt->size;
1376 }
1377
1378 static av_cold int cfhd_close(AVCodecContext *avctx)
1379 {
1380     CFHDContext *s = avctx->priv_data;
1381
1382     free_buffers(s);
1383
1384     ff_free_vlc(&s->vlc_9);
1385     ff_free_vlc(&s->vlc_18);
1386
1387     return 0;
1388 }
1389
1390 #if HAVE_THREADS
1391 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1392 {
1393     CFHDContext *psrc = src->priv_data;
1394     CFHDContext *pdst = dst->priv_data;
1395     int ret;
1396
1397     if (dst == src || psrc->transform_type == 0)
1398         return 0;
1399
1400     if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
1401         pdst->a_format != psrc->a_format ||
1402         pdst->a_width != psrc->a_width ||
1403         pdst->a_height != psrc->a_height ||
1404         pdst->a_transform_type != psrc->a_transform_type)
1405         free_buffers(pdst);
1406
1407     pdst->a_format = psrc->a_format;
1408     pdst->a_width  = psrc->a_width;
1409     pdst->a_height = psrc->a_height;
1410     pdst->a_transform_type = psrc->a_transform_type;
1411     pdst->transform_type = psrc->transform_type;
1412     pdst->progressive = psrc->progressive;
1413     pdst->planes = psrc->planes;
1414
1415     if (!pdst->plane[0].idwt_buf) {
1416         pdst->coded_width  = pdst->a_width;
1417         pdst->coded_height = pdst->a_height;
1418         pdst->coded_format = pdst->a_format;
1419         pdst->transform_type = pdst->a_transform_type;
1420         ret = alloc_buffers(dst);
1421         if (ret < 0)
1422             return ret;
1423     }
1424
1425     for (int plane = 0; plane < pdst->planes; plane++) {
1426         memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1427         memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1428                pdst->plane[plane].idwt_size * sizeof(int16_t));
1429     }
1430
1431     return 0;
1432 }
1433 #endif
1434
1435 AVCodec ff_cfhd_decoder = {
1436     .name             = "cfhd",
1437     .long_name        = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1438     .type             = AVMEDIA_TYPE_VIDEO,
1439     .id               = AV_CODEC_ID_CFHD,
1440     .priv_data_size   = sizeof(CFHDContext),
1441     .init             = cfhd_init,
1442     .close            = cfhd_close,
1443     .decode           = cfhd_decode,
1444     .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1445     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1446     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1447 };