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