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