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