2 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
4 * This file is part of FFmpeg.
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.
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.
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
23 * Cineform HD video decoder
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"
34 #include "bytestream.h"
40 #define ALPHA_COMPAND_DC_OFFSET 256
41 #define ALPHA_COMPAND_GAIN 9400
43 static av_cold int cfhd_init(AVCodecContext *avctx)
45 CFHDContext *s = avctx->priv_data;
49 for (int i = 0; i < 64; i++) {
67 for (int i = 0; i < 256; i++)
68 s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
70 return ff_cfhd_init_vlcs(s);
73 static void init_plane_defaults(CFHDContext *s)
77 s->subband_num_actual = 0;
80 static void init_peak_table_defaults(CFHDContext *s)
84 memset(&s->peak.base, 0, sizeof(s->peak.base));
87 static void init_frame_defaults(CFHDContext *s)
91 s->coded_format = AV_PIX_FMT_YUV422P10;
92 s->cropped_height = 0;
95 s->subband_cnt = SUBBAND_COUNT;
97 s->lowpass_precision = 16;
100 s->difference_coding = 0;
103 init_plane_defaults(s);
104 init_peak_table_defaults(s);
107 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
109 if (codebook == 0 || codebook == 1) {
110 return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
112 return level * quantisation;
115 static inline void difference_coding(int16_t *band, int width, int height)
119 for (i = 0; i < height; i++) {
120 for (j = 1; j < width; j++) {
121 band[j] += band[j-1];
127 static inline void peak_table(int16_t *band, Peak *peak, int length)
130 for (i = 0; i < length; i++)
131 if (abs(band[i]) > peak->level)
132 band[i] = bytestream2_get_le16(&peak->base);
135 static inline void process_alpha(int16_t *alpha, int width)
138 for (i = 0; i < width; i++) {
140 channel -= ALPHA_COMPAND_DC_OFFSET;
142 channel *= ALPHA_COMPAND_GAIN;
144 channel = av_clip_uintp2(channel, 12);
149 static inline void process_bayer(AVFrame *frame, int bpc)
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);
159 for (int y = 0; y < frame->height >> 1; y++) {
160 for (int x = 0; x < frame->width; x += 2) {
170 R = (rg - mid) * 2 + g;
173 B = (bg - mid) * 2 + g;
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);
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,
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;
204 output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
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;
209 output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
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;
215 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
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;
220 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
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;
226 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
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;
231 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
234 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
235 int width, int linesize, int plane)
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);
247 static inline void inverse_temporal_filter(int16_t *output, int16_t *low, int16_t *high,
250 for (int i = 0; i < width; i++) {
251 int even = (low[i] - high[i]) / 2;
252 int odd = (low[i] + high[i]) / 2;
259 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
262 filter(output, 1, low, 1, high, 1, width, 0);
265 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
268 filter(output, 1, low, 1, high, 1, width, clip);
271 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
274 filter(output, 2, low, 1, high, 1, width, clip);
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)
281 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
284 static void free_buffers(CFHDContext *s)
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;
293 for (j = 0; j < SUBBAND_COUNT_3D; j++)
294 s->plane[i].subband[j] = NULL;
296 for (j = 0; j < 10; j++)
297 s->plane[i].l_h[j] = NULL;
303 static int alloc_buffers(AVCodecContext *avctx)
305 CFHDContext *s = avctx->priv_data;
306 int i, j, ret, planes, bayer = 0;
307 int chroma_x_shift, chroma_y_shift;
310 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
312 avctx->pix_fmt = s->coded_format;
314 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
316 &chroma_y_shift)) < 0)
318 planes = av_pix_fmt_count_planes(s->coded_format);
319 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
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;
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;
338 w8 = FFALIGN(s->plane[i].width / 8, 8);
339 h8 = FFALIGN(height, 8) / 8;
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));
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));
359 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
360 return AVERROR(ENOMEM);
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;
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;
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;
395 for (j = 0; j < DWT_LEVELS_3D; j++) {
396 int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
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;
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;
417 s->plane[i].l_h[8] = frame2;
418 s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
422 s->a_height = s->coded_height;
423 s->a_width = s->coded_width;
424 s->a_format = s->coded_format;
429 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
432 CFHDContext *s = avctx->priv_data;
434 ThreadFrame frame = { .f = data };
436 int ret = 0, i, j, plane, got_buffer = 0;
439 init_frame_defaults(s);
440 s->planes = av_pix_fmt_count_planes(s->coded_format);
442 bytestream2_init(&gb, avpkt->data, avpkt->size);
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;
470 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
471 ret = AVERROR_PATCHWELCOME;
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;
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);
489 init_plane_defaults(s);
490 } else if (tag == SubbandNumber) {
491 if (s->subband_num != 0 && data == 1) // hack
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);
501 if (s->subband_num > 3) {
502 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
503 ret = AVERROR(EINVAL);
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);
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);
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) {
545 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
546 ret = AVERROR(EINVAL);
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)
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;
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);
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);
574 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
575 ret = AVERROR(EINVAL);
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);
583 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
584 ret = AVERROR(EINVAL);
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);
591 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
592 ret = AVERROR(EINVAL);
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);
600 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
601 ret = AVERROR(EINVAL);
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;
616 s->coded_format = AV_PIX_FMT_YUV422P10;
618 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
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);
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);
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;
643 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
644 ret = AVERROR_PATCHWELCOME;
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);
656 } else if (tag == -76) {
657 s->peak.offset &= 0xffff;
658 s->peak.offset |= (data & 0xffffU)<<16;
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);
665 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
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;
673 if (s->coded_width) {
674 s->coded_width *= factor;
677 if (s->coded_height) {
678 s->coded_height *= factor;
681 if (!s->a_width && !s->coded_width) {
682 s->coded_width = lowpass_width * factor * 8;
685 if (!s->a_height && !s->coded_height) {
686 s->coded_height = lowpass_height * factor * 8;
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;
694 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
695 s->a_format != s->coded_format) {
697 if ((ret = alloc_buffers(avctx)) < 0) {
702 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
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;
714 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
719 s->coded_format = AV_PIX_FMT_NONE;
721 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
725 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
729 s->coded_format = AV_PIX_FMT_NONE;
733 if (s->subband_num_actual == 255)
735 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
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;
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);
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);
759 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
760 ret = AVERROR(EINVAL);
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);
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);
776 coeff_data += lowpass_width;
779 /* Align to mod-4 position to continue reading tags */
780 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
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));
789 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
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;
799 int a_expected = highpass_a_height * highpass_a_width;
800 int level, run, coeff;
801 int count = 0, bytes;
804 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
805 ret = AVERROR(EINVAL);
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);
814 expected = highpass_height * highpass_stride;
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);
818 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
820 OPEN_READER(re, &s->gb);
822 const int lossless = s->band_encoding == 5;
824 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
828 UPDATE_CACHE(re, &s->gb);
829 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
838 if (count > expected)
842 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
845 if (tag == BandSecondPass) {
846 const uint16_t q = s->quantisation;
848 for (i = 0; i < run; i++) {
849 *coeff_data |= coeff << 8;
853 for (i = 0; i < run; i++)
854 *coeff_data++ = coeff;
859 UPDATE_CACHE(re, &s->gb);
860 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
864 if (level == 255 && run == 2)
869 if (count > expected)
873 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
876 if (tag == BandSecondPass) {
877 const uint16_t q = s->quantisation;
879 for (i = 0; i < run; i++) {
880 *coeff_data |= coeff << 8;
884 for (i = 0; i < run; i++)
885 *coeff_data++ = coeff;
889 CLOSE_READER(re, &s->gb);
892 if (count > expected) {
893 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
894 ret = AVERROR(EINVAL);
898 peak_table(coeff_data - count, &s->peak, count);
899 if (s->difference_coding)
900 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
902 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
903 if (bytes > bytestream2_get_bytes_left(&gb)) {
904 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
905 ret = AVERROR(EINVAL);
908 bytestream2_seek(&gb, bytes, SEEK_CUR);
910 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
912 if (s->subband_num_actual != 255)
915 /* Copy last line of coefficients if odd height */
916 if (highpass_height & 1) {
917 memcpy(&coeff_data[highpass_height * highpass_stride],
918 &coeff_data[(highpass_height - 1) * highpass_stride],
919 highpass_stride * sizeof(*coeff_data));
924 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
925 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
930 ff_thread_finish_setup(avctx);
932 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
933 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
934 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
935 ret = AVERROR(EINVAL);
940 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
941 ret = AVERROR(EINVAL);
945 if (s->transform_type == 0 && s->sample_type != 1) {
946 for (plane = 0; plane < s->planes && !ret; plane++) {
948 int lowpass_height = s->plane[plane].band[0][0].height;
949 int lowpass_width = s->plane[plane].band[0][0].width;
950 int highpass_stride = s->plane[plane].band[0][1].stride;
951 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
952 ptrdiff_t dst_linesize;
953 int16_t *low, *high, *output, *dst;
955 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
957 dst_linesize = pic->linesize[act_plane];
959 dst_linesize = pic->linesize[act_plane] / 2;
962 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
963 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
964 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
965 ret = AVERROR(EINVAL);
969 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
971 low = s->plane[plane].subband[0];
972 high = s->plane[plane].subband[2];
973 output = s->plane[plane].l_h[0];
974 for (i = 0; i < lowpass_width; i++) {
975 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
981 low = s->plane[plane].subband[1];
982 high = s->plane[plane].subband[3];
983 output = s->plane[plane].l_h[1];
985 for (i = 0; i < lowpass_width; i++) {
986 // note the stride of "low" is highpass_stride
987 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
993 low = s->plane[plane].l_h[0];
994 high = s->plane[plane].l_h[1];
995 output = s->plane[plane].subband[0];
996 for (i = 0; i < lowpass_height * 2; i++) {
997 horiz_filter(output, low, high, lowpass_width);
998 low += lowpass_width;
999 high += lowpass_width;
1000 output += lowpass_width * 2;
1003 output = s->plane[plane].subband[0];
1004 for (i = 0; i < lowpass_height * 2; i++) {
1005 for (j = 0; j < lowpass_width * 2; j++)
1008 output += lowpass_width * 2;
1013 lowpass_height = s->plane[plane].band[1][1].height;
1014 lowpass_width = s->plane[plane].band[1][1].width;
1015 highpass_stride = s->plane[plane].band[1][1].stride;
1017 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1018 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1019 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1020 ret = AVERROR(EINVAL);
1024 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1026 low = s->plane[plane].subband[0];
1027 high = s->plane[plane].subband[5];
1028 output = s->plane[plane].l_h[3];
1029 for (i = 0; i < lowpass_width; i++) {
1030 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1036 low = s->plane[plane].subband[4];
1037 high = s->plane[plane].subband[6];
1038 output = s->plane[plane].l_h[4];
1039 for (i = 0; i < lowpass_width; i++) {
1040 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1046 low = s->plane[plane].l_h[3];
1047 high = s->plane[plane].l_h[4];
1048 output = s->plane[plane].subband[0];
1049 for (i = 0; i < lowpass_height * 2; i++) {
1050 horiz_filter(output, low, high, lowpass_width);
1051 low += lowpass_width;
1052 high += lowpass_width;
1053 output += lowpass_width * 2;
1056 output = s->plane[plane].subband[0];
1057 for (i = 0; i < lowpass_height * 2; i++) {
1058 for (j = 0; j < lowpass_width * 2; j++)
1061 output += lowpass_width * 2;
1065 lowpass_height = s->plane[plane].band[2][1].height;
1066 lowpass_width = s->plane[plane].band[2][1].width;
1067 highpass_stride = s->plane[plane].band[2][1].stride;
1069 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1070 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
1071 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1072 ret = AVERROR(EINVAL);
1076 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1077 if (s->progressive) {
1078 low = s->plane[plane].subband[0];
1079 high = s->plane[plane].subband[8];
1080 output = s->plane[plane].l_h[6];
1081 for (i = 0; i < lowpass_width; i++) {
1082 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1088 low = s->plane[plane].subband[7];
1089 high = s->plane[plane].subband[9];
1090 output = s->plane[plane].l_h[7];
1091 for (i = 0; i < lowpass_width; i++) {
1092 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1098 dst = (int16_t *)pic->data[act_plane];
1099 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1103 dst += pic->linesize[act_plane] >> 1;
1105 low = s->plane[plane].l_h[6];
1106 high = s->plane[plane].l_h[7];
1108 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1109 (lowpass_height * 2 > avctx->coded_height / 2 ||
1110 lowpass_width * 2 > avctx->coded_width / 2 )
1112 ret = AVERROR_INVALIDDATA;
1116 for (i = 0; i < lowpass_height * 2; i++) {
1117 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1118 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1120 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1121 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1122 process_alpha(dst, lowpass_width * 2);
1123 low += lowpass_width;
1124 high += lowpass_width;
1125 dst += dst_linesize;
1128 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1129 pic->interlaced_frame = 1;
1130 low = s->plane[plane].subband[0];
1131 high = s->plane[plane].subband[7];
1132 output = s->plane[plane].l_h[6];
1133 for (i = 0; i < lowpass_height; i++) {
1134 horiz_filter(output, low, high, lowpass_width);
1135 low += lowpass_width;
1136 high += lowpass_width;
1137 output += lowpass_width * 2;
1140 low = s->plane[plane].subband[8];
1141 high = s->plane[plane].subband[9];
1142 output = s->plane[plane].l_h[7];
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;
1150 dst = (int16_t *)pic->data[act_plane];
1151 low = s->plane[plane].l_h[6];
1152 high = s->plane[plane].l_h[7];
1153 for (i = 0; i < lowpass_height; i++) {
1154 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1155 low += lowpass_width * 2;
1156 high += lowpass_width * 2;
1157 dst += pic->linesize[act_plane];
1161 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1162 for (plane = 0; plane < s->planes && !ret; plane++) {
1163 int lowpass_height = s->plane[plane].band[0][0].height;
1164 int lowpass_width = s->plane[plane].band[0][0].width;
1165 int highpass_stride = s->plane[plane].band[0][1].stride;
1166 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1167 int16_t *low, *high, *output, *dst;
1168 ptrdiff_t dst_linesize;
1170 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1172 dst_linesize = pic->linesize[act_plane];
1174 dst_linesize = pic->linesize[act_plane] / 2;
1177 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1178 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
1179 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1180 ret = AVERROR(EINVAL);
1184 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1186 low = s->plane[plane].subband[0];
1187 high = s->plane[plane].subband[2];
1188 output = s->plane[plane].l_h[0];
1189 for (i = 0; i < lowpass_width; i++) {
1190 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1196 low = s->plane[plane].subband[1];
1197 high = s->plane[plane].subband[3];
1198 output = s->plane[plane].l_h[1];
1199 for (i = 0; i < lowpass_width; i++) {
1200 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1206 low = s->plane[plane].l_h[0];
1207 high = s->plane[plane].l_h[1];
1208 output = s->plane[plane].l_h[7];
1209 for (i = 0; i < lowpass_height * 2; i++) {
1210 horiz_filter(output, low, high, lowpass_width);
1211 low += lowpass_width;
1212 high += lowpass_width;
1213 output += lowpass_width * 2;
1216 output = s->plane[plane].l_h[7];
1217 for (i = 0; i < lowpass_height * 2; i++) {
1218 for (j = 0; j < lowpass_width * 2; j++)
1221 output += lowpass_width * 2;
1225 lowpass_height = s->plane[plane].band[1][1].height;
1226 lowpass_width = s->plane[plane].band[1][1].width;
1227 highpass_stride = s->plane[plane].band[1][1].stride;
1229 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1230 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1231 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1232 ret = AVERROR(EINVAL);
1236 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1238 low = s->plane[plane].l_h[7];
1239 high = s->plane[plane].subband[5];
1240 output = s->plane[plane].l_h[3];
1241 for (i = 0; i < lowpass_width; i++) {
1242 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1248 low = s->plane[plane].subband[4];
1249 high = s->plane[plane].subband[6];
1250 output = s->plane[plane].l_h[4];
1251 for (i = 0; i < lowpass_width; i++) {
1252 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1258 low = s->plane[plane].l_h[3];
1259 high = s->plane[plane].l_h[4];
1260 output = s->plane[plane].l_h[7];
1261 for (i = 0; i < lowpass_height * 2; i++) {
1262 horiz_filter(output, low, high, lowpass_width);
1263 low += lowpass_width;
1264 high += lowpass_width;
1265 output += lowpass_width * 2;
1268 output = s->plane[plane].l_h[7];
1269 for (i = 0; i < lowpass_height * 2; i++) {
1270 for (j = 0; j < lowpass_width * 2; j++)
1272 output += lowpass_width * 2;
1275 low = s->plane[plane].subband[7];
1276 high = s->plane[plane].subband[9];
1277 output = s->plane[plane].l_h[3];
1278 for (i = 0; i < lowpass_width; i++) {
1279 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1285 low = s->plane[plane].subband[8];
1286 high = s->plane[plane].subband[10];
1287 output = s->plane[plane].l_h[4];
1288 for (i = 0; i < lowpass_width; i++) {
1289 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1295 low = s->plane[plane].l_h[3];
1296 high = s->plane[plane].l_h[4];
1297 output = s->plane[plane].l_h[9];
1298 for (i = 0; i < lowpass_height * 2; i++) {
1299 horiz_filter(output, low, high, lowpass_width);
1300 low += lowpass_width;
1301 high += lowpass_width;
1302 output += lowpass_width * 2;
1305 lowpass_height = s->plane[plane].band[4][1].height;
1306 lowpass_width = s->plane[plane].band[4][1].width;
1307 highpass_stride = s->plane[plane].band[4][1].stride;
1308 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1310 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1311 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
1312 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1313 ret = AVERROR(EINVAL);
1317 low = s->plane[plane].l_h[7];
1318 high = s->plane[plane].l_h[9];
1319 output = s->plane[plane].l_h[7];
1320 for (i = 0; i < lowpass_height; i++) {
1321 inverse_temporal_filter(output, low, high, lowpass_width);
1322 low += lowpass_width;
1323 high += lowpass_width;
1325 if (s->progressive) {
1326 low = s->plane[plane].l_h[7];
1327 high = s->plane[plane].subband[15];
1328 output = s->plane[plane].l_h[6];
1329 for (i = 0; i < lowpass_width; i++) {
1330 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1336 low = s->plane[plane].subband[14];
1337 high = s->plane[plane].subband[16];
1338 output = s->plane[plane].l_h[7];
1339 for (i = 0; i < lowpass_width; i++) {
1340 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1346 low = s->plane[plane].l_h[9];
1347 high = s->plane[plane].subband[12];
1348 output = s->plane[plane].l_h[8];
1349 for (i = 0; i < lowpass_width; i++) {
1350 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1356 low = s->plane[plane].subband[11];
1357 high = s->plane[plane].subband[13];
1358 output = s->plane[plane].l_h[9];
1359 for (i = 0; i < lowpass_width; i++) {
1360 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1366 if (s->sample_type == 1)
1369 dst = (int16_t *)pic->data[act_plane];
1370 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1374 dst += pic->linesize[act_plane] >> 1;
1377 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1378 (lowpass_height * 2 > avctx->coded_height / 2 ||
1379 lowpass_width * 2 > avctx->coded_width / 2 )
1381 ret = AVERROR_INVALIDDATA;
1385 low = s->plane[plane].l_h[6];
1386 high = s->plane[plane].l_h[7];
1387 for (i = 0; i < lowpass_height * 2; i++) {
1388 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1389 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1391 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1392 low += lowpass_width;
1393 high += lowpass_width;
1394 dst += dst_linesize;
1397 pic->interlaced_frame = 1;
1398 low = s->plane[plane].l_h[7];
1399 high = s->plane[plane].subband[14];
1400 output = s->plane[plane].l_h[6];
1401 for (i = 0; i < lowpass_height; i++) {
1402 horiz_filter(output, low, high, lowpass_width);
1403 low += lowpass_width;
1404 high += lowpass_width;
1405 output += lowpass_width * 2;
1408 low = s->plane[plane].subband[15];
1409 high = s->plane[plane].subband[16];
1410 output = s->plane[plane].l_h[7];
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;
1418 low = s->plane[plane].l_h[9];
1419 high = s->plane[plane].subband[11];
1420 output = s->plane[plane].l_h[8];
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;
1428 low = s->plane[plane].subband[12];
1429 high = s->plane[plane].subband[13];
1430 output = s->plane[plane].l_h[9];
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;
1438 if (s->sample_type == 1)
1441 dst = (int16_t *)pic->data[act_plane];
1442 low = s->plane[plane].l_h[6];
1443 high = s->plane[plane].l_h[7];
1444 for (i = 0; i < lowpass_height; i++) {
1445 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1446 low += lowpass_width * 2;
1447 high += lowpass_width * 2;
1448 dst += pic->linesize[act_plane];
1454 if (s->transform_type == 2 && s->sample_type == 1) {
1455 int16_t *low, *high, *dst;
1456 int lowpass_height, lowpass_width, highpass_stride;
1457 ptrdiff_t dst_linesize;
1459 for (plane = 0; plane < s->planes; plane++) {
1460 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1462 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1464 dst_linesize = pic->linesize[act_plane];
1466 dst_linesize = pic->linesize[act_plane] / 2;
1469 lowpass_height = s->plane[plane].band[4][1].height;
1470 lowpass_width = s->plane[plane].band[4][1].width;
1471 highpass_stride = s->plane[plane].band[4][1].stride;
1473 if (s->progressive) {
1474 dst = (int16_t *)pic->data[act_plane];
1475 low = s->plane[plane].l_h[8];
1476 high = s->plane[plane].l_h[9];
1478 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1482 dst += pic->linesize[act_plane] >> 1;
1485 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1486 (lowpass_height * 2 > avctx->coded_height / 2 ||
1487 lowpass_width * 2 > avctx->coded_width / 2 )
1489 ret = AVERROR_INVALIDDATA;
1493 for (i = 0; i < lowpass_height * 2; i++) {
1494 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1495 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1497 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1498 low += lowpass_width;
1499 high += lowpass_width;
1500 dst += dst_linesize;
1503 dst = (int16_t *)pic->data[act_plane];
1504 low = s->plane[plane].l_h[8];
1505 high = s->plane[plane].l_h[9];
1506 for (i = 0; i < lowpass_height; i++) {
1507 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1508 low += lowpass_width * 2;
1509 high += lowpass_width * 2;
1510 dst += pic->linesize[act_plane];
1516 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1517 process_bayer(pic, s->bpc);
1526 static av_cold int cfhd_close(AVCodecContext *avctx)
1528 CFHDContext *s = avctx->priv_data;
1532 ff_free_vlc(&s->vlc_9);
1533 ff_free_vlc(&s->vlc_18);
1539 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1541 CFHDContext *psrc = src->priv_data;
1542 CFHDContext *pdst = dst->priv_data;
1545 if (dst == src || psrc->transform_type == 0)
1548 pdst->a_format = psrc->a_format;
1549 pdst->a_width = psrc->a_width;
1550 pdst->a_height = psrc->a_height;
1551 pdst->transform_type = psrc->transform_type;
1552 pdst->progressive = psrc->progressive;
1553 pdst->planes = psrc->planes;
1555 if (!pdst->plane[0].idwt_buf) {
1556 pdst->coded_width = pdst->a_width;
1557 pdst->coded_height = pdst->a_height;
1558 pdst->coded_format = pdst->a_format;
1559 ret = alloc_buffers(dst);
1564 for (int plane = 0; plane < pdst->planes; plane++) {
1565 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1566 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1567 pdst->plane[plane].idwt_size * sizeof(int16_t));
1574 AVCodec ff_cfhd_decoder = {
1576 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1577 .type = AVMEDIA_TYPE_VIDEO,
1578 .id = AV_CODEC_ID_CFHD,
1579 .priv_data_size = sizeof(CFHDContext),
1581 .close = cfhd_close,
1582 .decode = cfhd_decode,
1583 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1584 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1585 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,