]> git.sesse.net Git - ffmpeg/blob - libavcodec/cfhd.c
avcodec/cfhd: Check transform_type consistently
[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 && s->a_width && s->a_height) {
705             int lowpass_height = s->plane[s->channel_num].band[0][0].height;
706             int lowpass_width  = s->plane[s->channel_num].band[0][0].width;
707             int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
708             int lowpass_a_width  = s->plane[s->channel_num].band[0][0].a_width;
709
710             if (lowpass_width < 3 ||
711                 lowpass_width > lowpass_a_width) {
712                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
713                 ret = AVERROR(EINVAL);
714                 goto end;
715             }
716
717             if (lowpass_height < 3 ||
718                 lowpass_height > lowpass_a_height) {
719                 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
720                 ret = AVERROR(EINVAL);
721                 goto end;
722             }
723
724             if (!got_buffer) {
725                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
726                 ret = AVERROR(EINVAL);
727                 goto end;
728             }
729
730             if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
731                 lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
732                 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
733                 ret = AVERROR(EINVAL);
734                 goto end;
735             }
736
737             av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
738             for (i = 0; i < lowpass_height; i++) {
739                 for (j = 0; j < lowpass_width; j++)
740                     coeff_data[j] = bytestream2_get_be16u(&gb);
741
742                 coeff_data += lowpass_width;
743             }
744
745             /* Align to mod-4 position to continue reading tags */
746             bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
747
748             /* Copy last line of coefficients if odd height */
749             if (lowpass_height & 1) {
750                 memcpy(&coeff_data[lowpass_height * lowpass_width],
751                        &coeff_data[(lowpass_height - 1) * lowpass_width],
752                        lowpass_width * sizeof(*coeff_data));
753             }
754
755             av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
756         }
757
758         if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
759             int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
760             int highpass_width  = s->plane[s->channel_num].band[s->level][s->subband_num].width;
761             int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
762             int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
763             int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
764             int expected;
765             int a_expected = highpass_a_height * highpass_a_width;
766             int level, run, coeff;
767             int count = 0, bytes;
768
769             if (!got_buffer) {
770                 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
771                 ret = AVERROR(EINVAL);
772                 goto end;
773             }
774
775             if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
776                 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
777                 ret = AVERROR(EINVAL);
778                 goto end;
779             }
780             expected = highpass_height * highpass_stride;
781
782             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);
783
784             ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
785             if (ret < 0)
786                 goto end;
787             {
788                 OPEN_READER(re, &s->gb);
789
790                 const int lossless = s->band_encoding == 5;
791
792                 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
793                     s->codebook = 1;
794                 if (!s->codebook) {
795                     while (1) {
796                         UPDATE_CACHE(re, &s->gb);
797                         GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
798                                    VLC_BITS, 3, 1);
799
800                         /* escape */
801                         if (level == 64)
802                             break;
803
804                         count += run;
805
806                         if (count > expected)
807                             break;
808
809                         if (!lossless)
810                             coeff = dequant_and_decompand(s, level, s->quantisation, 0);
811                         else
812                             coeff = level;
813                         if (tag == BandSecondPass) {
814                             const uint16_t q = s->quantisation;
815
816                             for (i = 0; i < run; i++) {
817                                 *coeff_data |= coeff * 256;
818                                 *coeff_data++ *= q;
819                             }
820                         } else {
821                             for (i = 0; i < run; i++)
822                                 *coeff_data++ = coeff;
823                         }
824                     }
825                 } else {
826                     while (1) {
827                         UPDATE_CACHE(re, &s->gb);
828                         GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
829                                    VLC_BITS, 3, 1);
830
831                         /* escape */
832                         if (level == 255 && run == 2)
833                             break;
834
835                         count += run;
836
837                         if (count > expected)
838                             break;
839
840                         if (!lossless)
841                             coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
842                         else
843                             coeff = level;
844                         if (tag == BandSecondPass) {
845                             const uint16_t q = s->quantisation;
846
847                             for (i = 0; i < run; i++) {
848                                 *coeff_data |= coeff * 256;
849                                 *coeff_data++ *= q;
850                             }
851                         } else {
852                             for (i = 0; i < run; i++)
853                                 *coeff_data++ = coeff;
854                         }
855                     }
856                 }
857                 CLOSE_READER(re, &s->gb);
858             }
859
860             if (count > expected) {
861                 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
862                 ret = AVERROR(EINVAL);
863                 goto end;
864             }
865             if (s->peak.level)
866                 peak_table(coeff_data - count, &s->peak, count);
867             if (s->difference_coding)
868                 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
869
870             bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
871             if (bytes > bytestream2_get_bytes_left(&gb)) {
872                 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
873                 ret = AVERROR(EINVAL);
874                 goto end;
875             } else
876                 bytestream2_seek(&gb, bytes, SEEK_CUR);
877
878             av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
879 finish:
880             if (s->subband_num_actual != 255)
881                 s->codebook = 0;
882         }
883     }
884
885     s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
886     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
887         s->progressive = 1;
888         s->planes = 4;
889     }
890
891     ff_thread_finish_setup(avctx);
892
893     if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
894         s->a_transform_type == INT_MIN ||
895         s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
896         av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
897         ret = AVERROR(EINVAL);
898         goto end;
899     }
900
901     if (!got_buffer) {
902         av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
903         ret = AVERROR(EINVAL);
904         goto end;
905     }
906
907     if (s->transform_type == 0 && s->sample_type != 1) {
908         for (plane = 0; plane < s->planes && !ret; plane++) {
909             /* level 1 */
910             int lowpass_height  = s->plane[plane].band[0][0].height;
911             int output_stride   = s->plane[plane].band[0][0].a_width;
912             int lowpass_width   = s->plane[plane].band[0][0].width;
913             int highpass_stride = s->plane[plane].band[0][1].stride;
914             int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
915             ptrdiff_t dst_linesize;
916             int16_t *low, *high, *output, *dst;
917
918             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
919                 act_plane = 0;
920                 dst_linesize = pic->linesize[act_plane];
921             } else {
922                 dst_linesize = pic->linesize[act_plane] / 2;
923             }
924
925             if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
926                 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
927                 lowpass_width < 3 || lowpass_height < 3) {
928                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
929                 ret = AVERROR(EINVAL);
930                 goto end;
931             }
932
933             av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
934
935             low    = s->plane[plane].subband[0];
936             high   = s->plane[plane].subband[2];
937             output = s->plane[plane].l_h[0];
938             dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
939
940             low    = s->plane[plane].subband[1];
941             high   = s->plane[plane].subband[3];
942             output = s->plane[plane].l_h[1];
943
944             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
945
946             low    = s->plane[plane].l_h[0];
947             high   = s->plane[plane].l_h[1];
948             output = s->plane[plane].subband[0];
949             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
950             if (s->bpc == 12) {
951                 output = s->plane[plane].subband[0];
952                 for (i = 0; i < lowpass_height * 2; i++) {
953                     for (j = 0; j < lowpass_width * 2; j++)
954                         output[j] *= 4;
955
956                     output += output_stride * 2;
957                 }
958             }
959
960             /* level 2 */
961             lowpass_height  = s->plane[plane].band[1][1].height;
962             output_stride   = s->plane[plane].band[1][1].a_width;
963             lowpass_width   = s->plane[plane].band[1][1].width;
964             highpass_stride = s->plane[plane].band[1][1].stride;
965
966             if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
967                 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
968                 lowpass_width < 3 || lowpass_height < 3) {
969                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
970                 ret = AVERROR(EINVAL);
971                 goto end;
972             }
973
974             av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
975
976             low    = s->plane[plane].subband[0];
977             high   = s->plane[plane].subband[5];
978             output = s->plane[plane].l_h[3];
979             dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
980
981             low    = s->plane[plane].subband[4];
982             high   = s->plane[plane].subband[6];
983             output = s->plane[plane].l_h[4];
984             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
985
986             low    = s->plane[plane].l_h[3];
987             high   = s->plane[plane].l_h[4];
988             output = s->plane[plane].subband[0];
989             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
990
991             output = s->plane[plane].subband[0];
992             for (i = 0; i < lowpass_height * 2; i++) {
993                 for (j = 0; j < lowpass_width * 2; j++)
994                     output[j] *= 4;
995
996                 output += output_stride * 2;
997             }
998
999             /* level 3 */
1000             lowpass_height  = s->plane[plane].band[2][1].height;
1001             output_stride   = s->plane[plane].band[2][1].a_width;
1002             lowpass_width   = s->plane[plane].band[2][1].width;
1003             highpass_stride = s->plane[plane].band[2][1].stride;
1004
1005             if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1006                 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
1007                 lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
1008                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1009                 ret = AVERROR(EINVAL);
1010                 goto end;
1011             }
1012
1013             av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1014             if (s->progressive) {
1015                 low    = s->plane[plane].subband[0];
1016                 high   = s->plane[plane].subband[8];
1017                 output = s->plane[plane].l_h[6];
1018                 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1019
1020                 low    = s->plane[plane].subband[7];
1021                 high   = s->plane[plane].subband[9];
1022                 output = s->plane[plane].l_h[7];
1023                 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1024
1025                 dst = (int16_t *)pic->data[act_plane];
1026                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1027                     if (plane & 1)
1028                         dst++;
1029                     if (plane > 1)
1030                         dst += pic->linesize[act_plane] >> 1;
1031                 }
1032                 low  = s->plane[plane].l_h[6];
1033                 high = s->plane[plane].l_h[7];
1034
1035                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1036                     (lowpass_height * 2 > avctx->coded_height / 2 ||
1037                      lowpass_width  * 2 > avctx->coded_width  / 2    )
1038                     ) {
1039                     ret = AVERROR_INVALIDDATA;
1040                     goto end;
1041                 }
1042
1043                 for (i = 0; i < s->plane[act_plane].height; i++) {
1044                     dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1045                     if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1046                         process_alpha(dst, lowpass_width * 2);
1047                     low  += output_stride;
1048                     high += output_stride;
1049                     dst  += dst_linesize;
1050                 }
1051             } else {
1052                 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1053                 pic->interlaced_frame = 1;
1054                 low    = s->plane[plane].subband[0];
1055                 high   = s->plane[plane].subband[7];
1056                 output = s->plane[plane].l_h[6];
1057                 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1058
1059                 low    = s->plane[plane].subband[8];
1060                 high   = s->plane[plane].subband[9];
1061                 output = s->plane[plane].l_h[7];
1062                 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1063
1064                 dst  = (int16_t *)pic->data[act_plane];
1065                 low  = s->plane[plane].l_h[6];
1066                 high = s->plane[plane].l_h[7];
1067                 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1068                     interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1069                     low  += output_stride * 2;
1070                     high += output_stride * 2;
1071                     dst  += pic->linesize[act_plane];
1072                 }
1073             }
1074         }
1075     } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1076         for (plane = 0; plane < s->planes && !ret; plane++) {
1077             int lowpass_height  = s->plane[plane].band[0][0].height;
1078             int output_stride   = s->plane[plane].band[0][0].a_width;
1079             int lowpass_width   = s->plane[plane].band[0][0].width;
1080             int highpass_stride = s->plane[plane].band[0][1].stride;
1081             int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1082             int16_t *low, *high, *output, *dst;
1083             ptrdiff_t dst_linesize;
1084
1085             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1086                 act_plane = 0;
1087                 dst_linesize = pic->linesize[act_plane];
1088             } else {
1089                 dst_linesize = pic->linesize[act_plane] / 2;
1090             }
1091
1092             if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1093                 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
1094                 lowpass_width < 3 || lowpass_height < 3) {
1095                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1096                 ret = AVERROR(EINVAL);
1097                 goto end;
1098             }
1099
1100             av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1101
1102             low    = s->plane[plane].subband[0];
1103             high   = s->plane[plane].subband[2];
1104             output = s->plane[plane].l_h[0];
1105             dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
1106
1107             low    = s->plane[plane].subband[1];
1108             high   = s->plane[plane].subband[3];
1109             output = s->plane[plane].l_h[1];
1110             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1111
1112             low    = s->plane[plane].l_h[0];
1113             high   = s->plane[plane].l_h[1];
1114             output = s->plane[plane].l_h[7];
1115             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1116             if (s->bpc == 12) {
1117                 output = s->plane[plane].l_h[7];
1118                 for (i = 0; i < lowpass_height * 2; i++) {
1119                     for (j = 0; j < lowpass_width * 2; j++)
1120                         output[j] *= 4;
1121
1122                     output += output_stride * 2;
1123                 }
1124             }
1125
1126             lowpass_height  = s->plane[plane].band[1][1].height;
1127             output_stride   = s->plane[plane].band[1][1].a_width;
1128             lowpass_width   = s->plane[plane].band[1][1].width;
1129             highpass_stride = s->plane[plane].band[1][1].stride;
1130
1131             if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1132                 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
1133                 lowpass_width < 3 || lowpass_height < 3) {
1134                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1135                 ret = AVERROR(EINVAL);
1136                 goto end;
1137             }
1138
1139             av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1140
1141             low    = s->plane[plane].l_h[7];
1142             high   = s->plane[plane].subband[5];
1143             output = s->plane[plane].l_h[3];
1144             dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1145
1146             low    = s->plane[plane].subband[4];
1147             high   = s->plane[plane].subband[6];
1148             output = s->plane[plane].l_h[4];
1149             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1150
1151             low    = s->plane[plane].l_h[3];
1152             high   = s->plane[plane].l_h[4];
1153             output = s->plane[plane].l_h[7];
1154             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1155
1156             output = s->plane[plane].l_h[7];
1157             for (i = 0; i < lowpass_height * 2; i++) {
1158                 for (j = 0; j < lowpass_width * 2; j++)
1159                     output[j] *= 4;
1160                 output += output_stride * 2;
1161             }
1162
1163             low    = s->plane[plane].subband[7];
1164             high   = s->plane[plane].subband[9];
1165             output = s->plane[plane].l_h[3];
1166             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1167
1168             low    = s->plane[plane].subband[8];
1169             high   = s->plane[plane].subband[10];
1170             output = s->plane[plane].l_h[4];
1171             dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1172
1173             low    = s->plane[plane].l_h[3];
1174             high   = s->plane[plane].l_h[4];
1175             output = s->plane[plane].l_h[9];
1176             dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
1177
1178             lowpass_height  = s->plane[plane].band[4][1].height;
1179             output_stride   = s->plane[plane].band[4][1].a_width;
1180             lowpass_width   = s->plane[plane].band[4][1].width;
1181             highpass_stride = s->plane[plane].band[4][1].stride;
1182             av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1183
1184             if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1185                 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1186                 lowpass_width < 3 || lowpass_height < 3) {
1187                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1188                 ret = AVERROR(EINVAL);
1189                 goto end;
1190             }
1191
1192             low    = s->plane[plane].l_h[7];
1193             high   = s->plane[plane].l_h[9];
1194             output = s->plane[plane].l_h[7];
1195             for (i = 0; i < lowpass_height; i++) {
1196                 inverse_temporal_filter(low, high, lowpass_width);
1197                 low    += output_stride;
1198                 high   += output_stride;
1199             }
1200             if (s->progressive) {
1201                 low    = s->plane[plane].l_h[7];
1202                 high   = s->plane[plane].subband[15];
1203                 output = s->plane[plane].l_h[6];
1204                 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1205
1206                 low    = s->plane[plane].subband[14];
1207                 high   = s->plane[plane].subband[16];
1208                 output = s->plane[plane].l_h[7];
1209                 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1210
1211                 low    = s->plane[plane].l_h[9];
1212                 high   = s->plane[plane].subband[12];
1213                 output = s->plane[plane].l_h[8];
1214                 dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1215
1216                 low    = s->plane[plane].subband[11];
1217                 high   = s->plane[plane].subband[13];
1218                 output = s->plane[plane].l_h[9];
1219                 dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1220
1221                 if (s->sample_type == 1)
1222                     continue;
1223
1224                 dst = (int16_t *)pic->data[act_plane];
1225                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1226                     if (plane & 1)
1227                         dst++;
1228                     if (plane > 1)
1229                         dst += pic->linesize[act_plane] >> 1;
1230                 }
1231
1232                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1233                     (lowpass_height * 2 > avctx->coded_height / 2 ||
1234                      lowpass_width  * 2 > avctx->coded_width  / 2    )
1235                     ) {
1236                     ret = AVERROR_INVALIDDATA;
1237                     goto end;
1238                 }
1239
1240                 low  = s->plane[plane].l_h[6];
1241                 high = s->plane[plane].l_h[7];
1242                 for (i = 0; i < s->plane[act_plane].height; i++) {
1243                     dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1244                     low  += output_stride;
1245                     high += output_stride;
1246                     dst  += dst_linesize;
1247                 }
1248             } else {
1249                 pic->interlaced_frame = 1;
1250                 low    = s->plane[plane].l_h[7];
1251                 high   = s->plane[plane].subband[14];
1252                 output = s->plane[plane].l_h[6];
1253                 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1254
1255                 low    = s->plane[plane].subband[15];
1256                 high   = s->plane[plane].subband[16];
1257                 output = s->plane[plane].l_h[7];
1258                 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1259
1260                 low    = s->plane[plane].l_h[9];
1261                 high   = s->plane[plane].subband[11];
1262                 output = s->plane[plane].l_h[8];
1263                 dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
1264
1265                 low    = s->plane[plane].subband[12];
1266                 high   = s->plane[plane].subband[13];
1267                 output = s->plane[plane].l_h[9];
1268                 dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
1269
1270                 if (s->sample_type == 1)
1271                     continue;
1272
1273                 dst  = (int16_t *)pic->data[act_plane];
1274                 low  = s->plane[plane].l_h[6];
1275                 high = s->plane[plane].l_h[7];
1276                 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1277                     interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1278                     low  += output_stride * 2;
1279                     high += output_stride * 2;
1280                     dst  += pic->linesize[act_plane];
1281                 }
1282             }
1283         }
1284     }
1285
1286     if (s->transform_type == 2 && s->sample_type == 1) {
1287         int16_t *low, *high, *dst;
1288         int output_stride, lowpass_height, lowpass_width;
1289         ptrdiff_t dst_linesize;
1290
1291         for (plane = 0; plane < s->planes; plane++) {
1292             int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1293
1294             if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1295                 act_plane = 0;
1296                 dst_linesize = pic->linesize[act_plane];
1297             } else {
1298                 dst_linesize = pic->linesize[act_plane] / 2;
1299             }
1300
1301             lowpass_height  = s->plane[plane].band[4][1].height;
1302             output_stride   = s->plane[plane].band[4][1].a_width;
1303             lowpass_width   = s->plane[plane].band[4][1].width;
1304
1305             if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1306                 s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
1307                 lowpass_width < 3 || lowpass_height < 3) {
1308                 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1309                 ret = AVERROR(EINVAL);
1310                 goto end;
1311             }
1312
1313             if (s->progressive) {
1314                 dst = (int16_t *)pic->data[act_plane];
1315                 low  = s->plane[plane].l_h[8];
1316                 high = s->plane[plane].l_h[9];
1317
1318                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1319                     if (plane & 1)
1320                         dst++;
1321                     if (plane > 1)
1322                         dst += pic->linesize[act_plane] >> 1;
1323                 }
1324
1325                 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1326                     (lowpass_height * 2 > avctx->coded_height / 2 ||
1327                      lowpass_width  * 2 > avctx->coded_width  / 2    )
1328                     ) {
1329                     ret = AVERROR_INVALIDDATA;
1330                     goto end;
1331                 }
1332
1333                 for (i = 0; i < s->plane[act_plane].height; i++) {
1334                     dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1335                     low  += output_stride;
1336                     high += output_stride;
1337                     dst  += dst_linesize;
1338                 }
1339             } else {
1340                 dst  = (int16_t *)pic->data[act_plane];
1341                 low  = s->plane[plane].l_h[8];
1342                 high = s->plane[plane].l_h[9];
1343                 for (i = 0; i < s->plane[act_plane].height / 2; i++) {
1344                     interlaced_vertical_filter(dst, low, high, lowpass_width * 2,  pic->linesize[act_plane]/2, act_plane);
1345                     low  += output_stride * 2;
1346                     high += output_stride * 2;
1347                     dst  += pic->linesize[act_plane];
1348                 }
1349             }
1350         }
1351     }
1352
1353     if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1354         process_bayer(pic, s->bpc);
1355 end:
1356     if (ret < 0)
1357         return ret;
1358
1359     *got_frame = 1;
1360     return avpkt->size;
1361 }
1362
1363 static av_cold int cfhd_close(AVCodecContext *avctx)
1364 {
1365     CFHDContext *s = avctx->priv_data;
1366
1367     free_buffers(s);
1368
1369     ff_free_vlc(&s->vlc_9);
1370     ff_free_vlc(&s->vlc_18);
1371
1372     return 0;
1373 }
1374
1375 #if HAVE_THREADS
1376 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1377 {
1378     CFHDContext *psrc = src->priv_data;
1379     CFHDContext *pdst = dst->priv_data;
1380     int ret;
1381
1382     if (dst == src || psrc->transform_type == 0)
1383         return 0;
1384
1385     if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
1386         pdst->a_format != psrc->a_format ||
1387         pdst->a_width != psrc->a_width ||
1388         pdst->a_height != psrc->a_height ||
1389         pdst->a_transform_type != psrc->a_transform_type)
1390         free_buffers(pdst);
1391
1392     pdst->a_format = psrc->a_format;
1393     pdst->a_width  = psrc->a_width;
1394     pdst->a_height = psrc->a_height;
1395     pdst->a_transform_type = psrc->a_transform_type;
1396     pdst->transform_type = psrc->transform_type;
1397     pdst->progressive = psrc->progressive;
1398     pdst->planes = psrc->planes;
1399
1400     if (!pdst->plane[0].idwt_buf) {
1401         pdst->coded_width  = pdst->a_width;
1402         pdst->coded_height = pdst->a_height;
1403         pdst->coded_format = pdst->a_format;
1404         pdst->transform_type = pdst->a_transform_type;
1405         ret = alloc_buffers(dst);
1406         if (ret < 0)
1407             return ret;
1408     }
1409
1410     for (int plane = 0; plane < pdst->planes; plane++) {
1411         memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1412         memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1413                pdst->plane[plane].idwt_size * sizeof(int16_t));
1414     }
1415
1416     return 0;
1417 }
1418 #endif
1419
1420 AVCodec ff_cfhd_decoder = {
1421     .name             = "cfhd",
1422     .long_name        = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
1423     .type             = AVMEDIA_TYPE_VIDEO,
1424     .id               = AV_CODEC_ID_CFHD,
1425     .priv_data_size   = sizeof(CFHDContext),
1426     .init             = cfhd_init,
1427     .close            = cfhd_close,
1428     .decode           = cfhd_decode,
1429     .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1430     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1431     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1432 };