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 == PrescaleTable) {
521 for (i = 0; i < 8; i++)
522 s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
523 av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
524 } else if (tag == BandEncoding) {
525 if (!data || data > 5) {
526 av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
527 ret = AVERROR(EINVAL);
530 s->band_encoding = data;
531 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
532 } else if (tag == LowpassWidth) {
533 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
534 s->plane[s->channel_num].band[0][0].width = data;
535 s->plane[s->channel_num].band[0][0].stride = data;
536 } else if (tag == LowpassHeight) {
537 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
538 s->plane[s->channel_num].band[0][0].height = data;
539 } else if (tag == SampleType) {
540 s->sample_type = data;
541 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
542 } else if (tag == TransformType) {
544 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
545 ret = AVERROR(EINVAL);
548 s->transform_type = data;
549 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
550 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
551 if (abstag == 0x4001)
553 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
554 bytestream2_skipu(&gb, data * 4);
555 } else if (tag == FrameIndex) {
556 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
557 s->frame_index = data;
558 } else if (tag == SampleIndexTable) {
559 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
560 if (data > bytestream2_get_bytes_left(&gb) / 4) {
561 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
562 ret = AVERROR_INVALIDDATA;
565 for (i = 0; i < data; i++) {
566 uint16_t tag2 = bytestream2_get_be16(&gb);
567 uint16_t val2 = bytestream2_get_be16(&gb);
568 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
570 } else if (tag == HighpassWidth) {
571 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 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
574 ret = AVERROR(EINVAL);
577 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
578 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
579 } else if (tag == HighpassHeight) {
580 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
582 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
583 ret = AVERROR(EINVAL);
586 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
587 } else if (tag == BandWidth) {
588 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
590 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
591 ret = AVERROR(EINVAL);
594 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
595 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
596 } else if (tag == BandHeight) {
597 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
599 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
600 ret = AVERROR(EINVAL);
603 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
604 } else if (tag == InputFormat) {
605 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
606 if (s->coded_format == AV_PIX_FMT_NONE ||
607 s->coded_format == AV_PIX_FMT_YUV422P10) {
608 if (data >= 100 && data <= 105) {
609 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
610 } else if (data >= 122 && data <= 128) {
611 s->coded_format = AV_PIX_FMT_GBRP12;
612 } else if (data == 30) {
613 s->coded_format = AV_PIX_FMT_GBRAP12;
615 s->coded_format = AV_PIX_FMT_YUV422P10;
617 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
619 } else if (tag == BandCodingFlags) {
620 s->codebook = data & 0xf;
621 s->difference_coding = (data >> 4) & 1;
622 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
623 } else if (tag == Precision) {
624 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
625 if (!(data == 10 || data == 12)) {
626 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
627 ret = AVERROR(EINVAL);
630 avctx->bits_per_raw_sample = s->bpc = data;
631 } else if (tag == EncodedFormat) {
632 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
634 s->coded_format = AV_PIX_FMT_YUV422P10;
635 } else if (data == 2) {
636 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
637 } else if (data == 3) {
638 s->coded_format = AV_PIX_FMT_GBRP12;
639 } else if (data == 4) {
640 s->coded_format = AV_PIX_FMT_GBRAP12;
642 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
643 ret = AVERROR_PATCHWELCOME;
646 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
647 } else if (tag == -85) {
648 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
649 s->cropped_height = data;
650 } else if (tag == -75) {
651 s->peak.offset &= ~0xffff;
652 s->peak.offset |= (data & 0xffff);
655 } else if (tag == -76) {
656 s->peak.offset &= 0xffff;
657 s->peak.offset |= (data & 0xffffU)<<16;
660 } else if (tag == -74 && s->peak.offset) {
661 s->peak.level = data;
662 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
664 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
666 if (tag == BitstreamMarker && data == 0xf0f &&
667 s->coded_format != AV_PIX_FMT_NONE) {
668 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
669 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
670 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
672 if (s->coded_width) {
673 s->coded_width *= factor;
676 if (s->coded_height) {
677 s->coded_height *= factor;
680 if (!s->a_width && !s->coded_width) {
681 s->coded_width = lowpass_width * factor * 8;
684 if (!s->a_height && !s->coded_height) {
685 s->coded_height = lowpass_height * factor * 8;
688 if (s->a_width && !s->coded_width)
689 s->coded_width = s->a_width;
690 if (s->a_height && !s->coded_height)
691 s->coded_height = s->a_height;
693 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
694 s->a_format != s->coded_format) {
696 if ((ret = alloc_buffers(avctx)) < 0) {
701 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
704 if (s->cropped_height) {
705 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
706 if (avctx->height < height)
707 return AVERROR_INVALIDDATA;
708 avctx->height = height;
713 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
718 s->coded_format = AV_PIX_FMT_NONE;
720 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
724 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
728 s->coded_format = AV_PIX_FMT_NONE;
732 if (s->subband_num_actual == 255)
734 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
736 /* Lowpass coefficients */
737 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
738 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
739 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
740 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
741 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
743 if (lowpass_width < 3 ||
744 lowpass_width > lowpass_a_width) {
745 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
746 ret = AVERROR(EINVAL);
750 if (lowpass_height < 3 ||
751 lowpass_height > lowpass_a_height) {
752 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
753 ret = AVERROR(EINVAL);
758 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
759 ret = AVERROR(EINVAL);
763 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
764 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
765 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
766 ret = AVERROR(EINVAL);
770 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
771 for (i = 0; i < lowpass_height; i++) {
772 for (j = 0; j < lowpass_width; j++)
773 coeff_data[j] = bytestream2_get_be16u(&gb);
775 coeff_data += lowpass_width;
778 /* Align to mod-4 position to continue reading tags */
779 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
781 /* Copy last line of coefficients if odd height */
782 if (lowpass_height & 1) {
783 memcpy(&coeff_data[lowpass_height * lowpass_width],
784 &coeff_data[(lowpass_height - 1) * lowpass_width],
785 lowpass_width * sizeof(*coeff_data));
788 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
791 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
792 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
793 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
794 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
795 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
796 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
798 int a_expected = highpass_a_height * highpass_a_width;
799 int level, run, coeff;
800 int count = 0, bytes;
803 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
804 ret = AVERROR(EINVAL);
808 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
809 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
810 ret = AVERROR(EINVAL);
813 expected = highpass_height * highpass_stride;
815 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 ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
821 OPEN_READER(re, &s->gb);
823 const int lossless = s->band_encoding == 5;
825 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
829 UPDATE_CACHE(re, &s->gb);
830 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
839 if (count > expected)
843 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
846 if (tag == BandSecondPass) {
847 const uint16_t q = s->quantisation;
849 for (i = 0; i < run; i++) {
850 *coeff_data |= coeff << 8;
854 for (i = 0; i < run; i++)
855 *coeff_data++ = coeff;
860 UPDATE_CACHE(re, &s->gb);
861 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
865 if (level == 255 && run == 2)
870 if (count > expected)
874 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
877 if (tag == BandSecondPass) {
878 const uint16_t q = s->quantisation;
880 for (i = 0; i < run; i++) {
881 *coeff_data |= coeff << 8;
885 for (i = 0; i < run; i++)
886 *coeff_data++ = coeff;
890 CLOSE_READER(re, &s->gb);
893 if (count > expected) {
894 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
895 ret = AVERROR(EINVAL);
899 peak_table(coeff_data - count, &s->peak, count);
900 if (s->difference_coding)
901 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
903 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
904 if (bytes > bytestream2_get_bytes_left(&gb)) {
905 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
906 ret = AVERROR(EINVAL);
909 bytestream2_seek(&gb, bytes, SEEK_CUR);
911 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
913 if (s->subband_num_actual != 255)
916 /* Copy last line of coefficients if odd height */
917 if (highpass_height & 1) {
918 memcpy(&coeff_data[highpass_height * highpass_stride],
919 &coeff_data[(highpass_height - 1) * highpass_stride],
920 highpass_stride * sizeof(*coeff_data));
925 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
926 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
931 ff_thread_finish_setup(avctx);
933 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
934 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
935 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
936 ret = AVERROR(EINVAL);
941 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
942 ret = AVERROR(EINVAL);
946 if (s->transform_type == 0 && s->sample_type != 1) {
947 for (plane = 0; plane < s->planes && !ret; plane++) {
949 int lowpass_height = s->plane[plane].band[0][0].height;
950 int lowpass_width = s->plane[plane].band[0][0].width;
951 int highpass_stride = s->plane[plane].band[0][1].stride;
952 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
953 ptrdiff_t dst_linesize;
954 int16_t *low, *high, *output, *dst;
956 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
958 dst_linesize = pic->linesize[act_plane];
960 dst_linesize = pic->linesize[act_plane] / 2;
963 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
964 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
965 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
966 ret = AVERROR(EINVAL);
970 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
972 low = s->plane[plane].subband[0];
973 high = s->plane[plane].subband[2];
974 output = s->plane[plane].l_h[0];
975 for (i = 0; i < lowpass_width; i++) {
976 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
982 low = s->plane[plane].subband[1];
983 high = s->plane[plane].subband[3];
984 output = s->plane[plane].l_h[1];
986 for (i = 0; i < lowpass_width; i++) {
987 // note the stride of "low" is highpass_stride
988 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
994 low = s->plane[plane].l_h[0];
995 high = s->plane[plane].l_h[1];
996 output = s->plane[plane].subband[0];
997 for (i = 0; i < lowpass_height * 2; i++) {
998 horiz_filter(output, low, high, lowpass_width);
999 low += lowpass_width;
1000 high += lowpass_width;
1001 output += lowpass_width * 2;
1004 output = s->plane[plane].subband[0];
1005 for (i = 0; i < lowpass_height * 2; i++) {
1006 for (j = 0; j < lowpass_width * 2; j++)
1009 output += lowpass_width * 2;
1014 lowpass_height = s->plane[plane].band[1][1].height;
1015 lowpass_width = s->plane[plane].band[1][1].width;
1016 highpass_stride = s->plane[plane].band[1][1].stride;
1018 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1019 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1020 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1021 ret = AVERROR(EINVAL);
1025 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1027 low = s->plane[plane].subband[0];
1028 high = s->plane[plane].subband[5];
1029 output = s->plane[plane].l_h[3];
1030 for (i = 0; i < lowpass_width; i++) {
1031 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1037 low = s->plane[plane].subband[4];
1038 high = s->plane[plane].subband[6];
1039 output = s->plane[plane].l_h[4];
1040 for (i = 0; i < lowpass_width; i++) {
1041 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1047 low = s->plane[plane].l_h[3];
1048 high = s->plane[plane].l_h[4];
1049 output = s->plane[plane].subband[0];
1050 for (i = 0; i < lowpass_height * 2; i++) {
1051 horiz_filter(output, low, high, lowpass_width);
1052 low += lowpass_width;
1053 high += lowpass_width;
1054 output += lowpass_width * 2;
1057 output = s->plane[plane].subband[0];
1058 for (i = 0; i < lowpass_height * 2; i++) {
1059 for (j = 0; j < lowpass_width * 2; j++)
1062 output += lowpass_width * 2;
1066 lowpass_height = s->plane[plane].band[2][1].height;
1067 lowpass_width = s->plane[plane].band[2][1].width;
1068 highpass_stride = s->plane[plane].band[2][1].stride;
1070 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1071 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
1072 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1073 ret = AVERROR(EINVAL);
1077 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1078 if (s->progressive) {
1079 low = s->plane[plane].subband[0];
1080 high = s->plane[plane].subband[8];
1081 output = s->plane[plane].l_h[6];
1082 for (i = 0; i < lowpass_width; i++) {
1083 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1089 low = s->plane[plane].subband[7];
1090 high = s->plane[plane].subband[9];
1091 output = s->plane[plane].l_h[7];
1092 for (i = 0; i < lowpass_width; i++) {
1093 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1099 dst = (int16_t *)pic->data[act_plane];
1100 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1104 dst += pic->linesize[act_plane] >> 1;
1106 low = s->plane[plane].l_h[6];
1107 high = s->plane[plane].l_h[7];
1109 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1110 (lowpass_height * 2 > avctx->coded_height / 2 ||
1111 lowpass_width * 2 > avctx->coded_width / 2 )
1113 ret = AVERROR_INVALIDDATA;
1117 for (i = 0; i < lowpass_height * 2; i++) {
1118 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1119 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1121 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1122 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1123 process_alpha(dst, lowpass_width * 2);
1124 low += lowpass_width;
1125 high += lowpass_width;
1126 dst += dst_linesize;
1129 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1130 pic->interlaced_frame = 1;
1131 low = s->plane[plane].subband[0];
1132 high = s->plane[plane].subband[7];
1133 output = s->plane[plane].l_h[6];
1134 for (i = 0; i < lowpass_height; i++) {
1135 horiz_filter(output, low, high, lowpass_width);
1136 low += lowpass_width;
1137 high += lowpass_width;
1138 output += lowpass_width * 2;
1141 low = s->plane[plane].subband[8];
1142 high = s->plane[plane].subband[9];
1143 output = s->plane[plane].l_h[7];
1144 for (i = 0; i < lowpass_height; i++) {
1145 horiz_filter(output, low, high, lowpass_width);
1146 low += lowpass_width;
1147 high += lowpass_width;
1148 output += lowpass_width * 2;
1151 dst = (int16_t *)pic->data[act_plane];
1152 low = s->plane[plane].l_h[6];
1153 high = s->plane[plane].l_h[7];
1154 for (i = 0; i < lowpass_height; i++) {
1155 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1156 low += lowpass_width * 2;
1157 high += lowpass_width * 2;
1158 dst += pic->linesize[act_plane];
1162 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1163 for (plane = 0; plane < s->planes && !ret; plane++) {
1164 int lowpass_height = s->plane[plane].band[0][0].height;
1165 int lowpass_width = s->plane[plane].band[0][0].width;
1166 int highpass_stride = s->plane[plane].band[0][1].stride;
1167 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1168 int16_t *low, *high, *output, *dst;
1169 ptrdiff_t dst_linesize;
1171 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1173 dst_linesize = pic->linesize[act_plane];
1175 dst_linesize = pic->linesize[act_plane] / 2;
1178 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1179 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
1180 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1181 ret = AVERROR(EINVAL);
1185 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1187 low = s->plane[plane].subband[0];
1188 high = s->plane[plane].subband[2];
1189 output = s->plane[plane].l_h[0];
1190 for (i = 0; i < lowpass_width; i++) {
1191 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1197 low = s->plane[plane].subband[1];
1198 high = s->plane[plane].subband[3];
1199 output = s->plane[plane].l_h[1];
1200 for (i = 0; i < lowpass_width; i++) {
1201 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1207 low = s->plane[plane].l_h[0];
1208 high = s->plane[plane].l_h[1];
1209 output = s->plane[plane].l_h[7];
1210 for (i = 0; i < lowpass_height * 2; i++) {
1211 horiz_filter(output, low, high, lowpass_width);
1212 low += lowpass_width;
1213 high += lowpass_width;
1214 output += lowpass_width * 2;
1217 output = s->plane[plane].l_h[7];
1218 for (i = 0; i < lowpass_height * 2; i++) {
1219 for (j = 0; j < lowpass_width * 2; j++)
1222 output += lowpass_width * 2;
1226 lowpass_height = s->plane[plane].band[1][1].height;
1227 lowpass_width = s->plane[plane].band[1][1].width;
1228 highpass_stride = s->plane[plane].band[1][1].stride;
1230 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1231 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1232 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1233 ret = AVERROR(EINVAL);
1237 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1239 low = s->plane[plane].l_h[7];
1240 high = s->plane[plane].subband[5];
1241 output = s->plane[plane].l_h[3];
1242 for (i = 0; i < lowpass_width; i++) {
1243 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1249 low = s->plane[plane].subband[4];
1250 high = s->plane[plane].subband[6];
1251 output = s->plane[plane].l_h[4];
1252 for (i = 0; i < lowpass_width; i++) {
1253 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1259 low = s->plane[plane].l_h[3];
1260 high = s->plane[plane].l_h[4];
1261 output = s->plane[plane].l_h[7];
1262 for (i = 0; i < lowpass_height * 2; i++) {
1263 horiz_filter(output, low, high, lowpass_width);
1264 low += lowpass_width;
1265 high += lowpass_width;
1266 output += lowpass_width * 2;
1269 output = s->plane[plane].l_h[7];
1270 for (i = 0; i < lowpass_height * 2; i++) {
1271 for (j = 0; j < lowpass_width * 2; j++)
1273 output += lowpass_width * 2;
1276 low = s->plane[plane].subband[7];
1277 high = s->plane[plane].subband[9];
1278 output = s->plane[plane].l_h[3];
1279 for (i = 0; i < lowpass_width; i++) {
1280 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1286 low = s->plane[plane].subband[8];
1287 high = s->plane[plane].subband[10];
1288 output = s->plane[plane].l_h[4];
1289 for (i = 0; i < lowpass_width; i++) {
1290 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1296 low = s->plane[plane].l_h[3];
1297 high = s->plane[plane].l_h[4];
1298 output = s->plane[plane].l_h[9];
1299 for (i = 0; i < lowpass_height * 2; i++) {
1300 horiz_filter(output, low, high, lowpass_width);
1301 low += lowpass_width;
1302 high += lowpass_width;
1303 output += lowpass_width * 2;
1306 lowpass_height = s->plane[plane].band[4][1].height;
1307 lowpass_width = s->plane[plane].band[4][1].width;
1308 highpass_stride = s->plane[plane].band[4][1].stride;
1309 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1311 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1312 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
1313 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1314 ret = AVERROR(EINVAL);
1318 low = s->plane[plane].l_h[7];
1319 high = s->plane[plane].l_h[9];
1320 output = s->plane[plane].l_h[7];
1321 for (i = 0; i < lowpass_height; i++) {
1322 inverse_temporal_filter(output, low, high, lowpass_width);
1323 low += lowpass_width;
1324 high += lowpass_width;
1326 if (s->progressive) {
1327 low = s->plane[plane].l_h[7];
1328 high = s->plane[plane].subband[15];
1329 output = s->plane[plane].l_h[6];
1330 for (i = 0; i < lowpass_width; i++) {
1331 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1337 low = s->plane[plane].subband[14];
1338 high = s->plane[plane].subband[16];
1339 output = s->plane[plane].l_h[7];
1340 for (i = 0; i < lowpass_width; i++) {
1341 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1347 low = s->plane[plane].l_h[9];
1348 high = s->plane[plane].subband[12];
1349 output = s->plane[plane].l_h[8];
1350 for (i = 0; i < lowpass_width; i++) {
1351 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1357 low = s->plane[plane].subband[11];
1358 high = s->plane[plane].subband[13];
1359 output = s->plane[plane].l_h[9];
1360 for (i = 0; i < lowpass_width; i++) {
1361 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1367 if (s->sample_type == 1)
1370 dst = (int16_t *)pic->data[act_plane];
1371 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1375 dst += pic->linesize[act_plane] >> 1;
1378 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1379 (lowpass_height * 2 > avctx->coded_height / 2 ||
1380 lowpass_width * 2 > avctx->coded_width / 2 )
1382 ret = AVERROR_INVALIDDATA;
1386 low = s->plane[plane].l_h[6];
1387 high = s->plane[plane].l_h[7];
1388 for (i = 0; i < lowpass_height * 2; i++) {
1389 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1390 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1392 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1393 low += lowpass_width;
1394 high += lowpass_width;
1395 dst += dst_linesize;
1398 pic->interlaced_frame = 1;
1399 low = s->plane[plane].l_h[7];
1400 high = s->plane[plane].subband[14];
1401 output = s->plane[plane].l_h[6];
1402 for (i = 0; i < lowpass_height; i++) {
1403 horiz_filter(output, low, high, lowpass_width);
1404 low += lowpass_width;
1405 high += lowpass_width;
1406 output += lowpass_width * 2;
1409 low = s->plane[plane].subband[15];
1410 high = s->plane[plane].subband[16];
1411 output = s->plane[plane].l_h[7];
1412 for (i = 0; i < lowpass_height; i++) {
1413 horiz_filter(output, low, high, lowpass_width);
1414 low += lowpass_width;
1415 high += lowpass_width;
1416 output += lowpass_width * 2;
1419 low = s->plane[plane].l_h[9];
1420 high = s->plane[plane].subband[11];
1421 output = s->plane[plane].l_h[8];
1422 for (i = 0; i < lowpass_height; i++) {
1423 horiz_filter(output, low, high, lowpass_width);
1424 low += lowpass_width;
1425 high += lowpass_width;
1426 output += lowpass_width * 2;
1429 low = s->plane[plane].subband[12];
1430 high = s->plane[plane].subband[13];
1431 output = s->plane[plane].l_h[9];
1432 for (i = 0; i < lowpass_height; i++) {
1433 horiz_filter(output, low, high, lowpass_width);
1434 low += lowpass_width;
1435 high += lowpass_width;
1436 output += lowpass_width * 2;
1439 if (s->sample_type == 1)
1442 dst = (int16_t *)pic->data[act_plane];
1443 low = s->plane[plane].l_h[6];
1444 high = s->plane[plane].l_h[7];
1445 for (i = 0; i < lowpass_height; i++) {
1446 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1447 low += lowpass_width * 2;
1448 high += lowpass_width * 2;
1449 dst += pic->linesize[act_plane];
1455 if (s->transform_type == 2 && s->sample_type == 1) {
1456 int16_t *low, *high, *dst;
1457 int lowpass_height, lowpass_width, highpass_stride;
1458 ptrdiff_t dst_linesize;
1460 for (plane = 0; plane < s->planes; plane++) {
1461 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1463 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1465 dst_linesize = pic->linesize[act_plane];
1467 dst_linesize = pic->linesize[act_plane] / 2;
1470 lowpass_height = s->plane[plane].band[4][1].height;
1471 lowpass_width = s->plane[plane].band[4][1].width;
1472 highpass_stride = s->plane[plane].band[4][1].stride;
1474 if (s->progressive) {
1475 dst = (int16_t *)pic->data[act_plane];
1476 low = s->plane[plane].l_h[8];
1477 high = s->plane[plane].l_h[9];
1479 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1483 dst += pic->linesize[act_plane] >> 1;
1486 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1487 (lowpass_height * 2 > avctx->coded_height / 2 ||
1488 lowpass_width * 2 > avctx->coded_width / 2 )
1490 ret = AVERROR_INVALIDDATA;
1494 for (i = 0; i < lowpass_height * 2; i++) {
1495 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1496 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1498 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1499 low += lowpass_width;
1500 high += lowpass_width;
1501 dst += dst_linesize;
1504 dst = (int16_t *)pic->data[act_plane];
1505 low = s->plane[plane].l_h[8];
1506 high = s->plane[plane].l_h[9];
1507 for (i = 0; i < lowpass_height; i++) {
1508 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1509 low += lowpass_width * 2;
1510 high += lowpass_width * 2;
1511 dst += pic->linesize[act_plane];
1517 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1518 process_bayer(pic, s->bpc);
1527 static av_cold int cfhd_close(AVCodecContext *avctx)
1529 CFHDContext *s = avctx->priv_data;
1533 ff_free_vlc(&s->vlc_9);
1534 ff_free_vlc(&s->vlc_18);
1540 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1542 CFHDContext *psrc = src->priv_data;
1543 CFHDContext *pdst = dst->priv_data;
1546 if (dst == src || psrc->transform_type == 0)
1549 pdst->a_format = psrc->a_format;
1550 pdst->a_width = psrc->a_width;
1551 pdst->a_height = psrc->a_height;
1552 pdst->transform_type = psrc->transform_type;
1553 pdst->progressive = psrc->progressive;
1554 pdst->planes = psrc->planes;
1556 if (!pdst->plane[0].idwt_buf) {
1557 pdst->coded_width = pdst->a_width;
1558 pdst->coded_height = pdst->a_height;
1559 pdst->coded_format = pdst->a_format;
1560 ret = alloc_buffers(dst);
1565 for (int plane = 0; plane < pdst->planes; plane++) {
1566 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1567 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1568 pdst->plane[plane].idwt_size * sizeof(int16_t));
1575 AVCodec ff_cfhd_decoder = {
1577 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1578 .type = AVMEDIA_TYPE_VIDEO,
1579 .id = AV_CODEC_ID_CFHD,
1580 .priv_data_size = sizeof(CFHDContext),
1582 .close = cfhd_close,
1583 .decode = cfhd_decode,
1584 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1585 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1586 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,