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