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