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 s->band_encoding = data;
527 av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
528 } else if (tag == LowpassWidth) {
529 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
530 s->plane[s->channel_num].band[0][0].width = data;
531 s->plane[s->channel_num].band[0][0].stride = data;
532 } else if (tag == LowpassHeight) {
533 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
534 s->plane[s->channel_num].band[0][0].height = data;
535 } else if (tag == SampleType) {
536 s->sample_type = data;
537 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
538 } else if (tag == TransformType) {
540 av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
541 ret = AVERROR(EINVAL);
544 s->transform_type = data;
545 av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
546 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
547 if (abstag == 0x4001)
549 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
550 bytestream2_skipu(&gb, data * 4);
551 } else if (tag == FrameIndex) {
552 av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
553 s->frame_index = data;
554 } else if (tag == SampleIndexTable) {
555 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
556 if (data > bytestream2_get_bytes_left(&gb) / 4) {
557 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
558 ret = AVERROR_INVALIDDATA;
561 for (i = 0; i < data; i++) {
562 uint16_t tag2 = bytestream2_get_be16(&gb);
563 uint16_t val2 = bytestream2_get_be16(&gb);
564 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
566 } else if (tag == HighpassWidth) {
567 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);
569 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
570 ret = AVERROR(EINVAL);
573 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
574 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
575 } else if (tag == HighpassHeight) {
576 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
578 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
579 ret = AVERROR(EINVAL);
582 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
583 } else if (tag == BandWidth) {
584 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
586 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
587 ret = AVERROR(EINVAL);
590 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
591 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
592 } else if (tag == BandHeight) {
593 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
595 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
596 ret = AVERROR(EINVAL);
599 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
600 } else if (tag == InputFormat) {
601 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
602 if (s->coded_format == AV_PIX_FMT_NONE ||
603 s->coded_format == AV_PIX_FMT_YUV422P10) {
604 if (data >= 100 && data <= 105) {
605 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
606 } else if (data >= 122 && data <= 128) {
607 s->coded_format = AV_PIX_FMT_GBRP12;
608 } else if (data == 30) {
609 s->coded_format = AV_PIX_FMT_GBRAP12;
611 s->coded_format = AV_PIX_FMT_YUV422P10;
613 s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
615 } else if (tag == BandCodingFlags) {
616 s->codebook = data & 0xf;
617 s->difference_coding = (data >> 4) & 1;
618 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
619 } else if (tag == Precision) {
620 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
621 if (!(data == 10 || data == 12)) {
622 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
623 ret = AVERROR(EINVAL);
626 avctx->bits_per_raw_sample = s->bpc = data;
627 } else if (tag == EncodedFormat) {
628 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
630 s->coded_format = AV_PIX_FMT_YUV422P10;
631 } else if (data == 2) {
632 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
633 } else if (data == 3) {
634 s->coded_format = AV_PIX_FMT_GBRP12;
635 } else if (data == 4) {
636 s->coded_format = AV_PIX_FMT_GBRAP12;
638 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
639 ret = AVERROR_PATCHWELCOME;
642 s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
643 } else if (tag == -85) {
644 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
645 s->cropped_height = data;
646 } else if (tag == -75) {
647 s->peak.offset &= ~0xffff;
648 s->peak.offset |= (data & 0xffff);
651 } else if (tag == -76) {
652 s->peak.offset &= 0xffff;
653 s->peak.offset |= (data & 0xffffU)<<16;
656 } else if (tag == -74 && s->peak.offset) {
657 s->peak.level = data;
658 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
660 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
662 if (tag == BitstreamMarker && data == 0xf0f &&
663 s->coded_format != AV_PIX_FMT_NONE) {
664 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
665 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
666 int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
668 if (s->coded_width) {
669 s->coded_width *= factor;
672 if (s->coded_height) {
673 s->coded_height *= factor;
676 if (!s->a_width && !s->coded_width) {
677 s->coded_width = lowpass_width * factor * 8;
680 if (!s->a_height && !s->coded_height) {
681 s->coded_height = lowpass_height * factor * 8;
684 if (s->a_width && !s->coded_width)
685 s->coded_width = s->a_width;
686 if (s->a_height && !s->coded_height)
687 s->coded_height = s->a_height;
689 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
690 s->a_format != s->coded_format) {
692 if ((ret = alloc_buffers(avctx)) < 0) {
697 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
700 if (s->cropped_height) {
701 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
702 if (avctx->height < height)
703 return AVERROR_INVALIDDATA;
704 avctx->height = height;
709 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
714 s->coded_format = AV_PIX_FMT_NONE;
716 } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
720 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
724 s->coded_format = AV_PIX_FMT_NONE;
728 if (s->subband_num_actual == 255)
730 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
732 /* Lowpass coefficients */
733 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
734 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
735 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
736 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
737 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
739 if (lowpass_width < 3 ||
740 lowpass_width > lowpass_a_width) {
741 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
742 ret = AVERROR(EINVAL);
746 if (lowpass_height < 3 ||
747 lowpass_height > lowpass_a_height) {
748 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
749 ret = AVERROR(EINVAL);
754 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
755 ret = AVERROR(EINVAL);
759 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
760 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
761 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
762 ret = AVERROR(EINVAL);
766 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
767 for (i = 0; i < lowpass_height; i++) {
768 for (j = 0; j < lowpass_width; j++)
769 coeff_data[j] = bytestream2_get_be16u(&gb);
771 coeff_data += lowpass_width;
774 /* Align to mod-4 position to continue reading tags */
775 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
777 /* Copy last line of coefficients if odd height */
778 if (lowpass_height & 1) {
779 memcpy(&coeff_data[lowpass_height * lowpass_width],
780 &coeff_data[(lowpass_height - 1) * lowpass_width],
781 lowpass_width * sizeof(*coeff_data));
784 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
787 if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
788 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
789 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
790 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
791 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
792 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
794 int a_expected = highpass_a_height * highpass_a_width;
795 int level, run, coeff;
796 int count = 0, bytes;
799 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
800 ret = AVERROR(EINVAL);
804 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
805 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
806 ret = AVERROR(EINVAL);
809 expected = highpass_height * highpass_stride;
811 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);
813 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
815 OPEN_READER(re, &s->gb);
817 const int lossless = (s->sample_type == 2 || s->sample_type == 3 || s->frame_type) &&
818 s->subband_num_actual == 7 && s->band_encoding == 5;
820 if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
824 UPDATE_CACHE(re, &s->gb);
825 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
834 if (count > expected)
838 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
841 if (tag == BandSecondPass) {
842 const uint16_t q = s->quantisation;
844 for (i = 0; i < run; i++) {
845 *coeff_data |= coeff << 8;
849 for (i = 0; i < run; i++)
850 *coeff_data++ = coeff;
855 UPDATE_CACHE(re, &s->gb);
856 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
860 if (level == 255 && run == 2)
865 if (count > expected)
869 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
872 if (tag == BandSecondPass) {
873 const uint16_t q = s->quantisation;
875 for (i = 0; i < run; i++) {
876 *coeff_data |= coeff << 8;
880 for (i = 0; i < run; i++)
881 *coeff_data++ = coeff;
885 CLOSE_READER(re, &s->gb);
888 if (count > expected) {
889 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
890 ret = AVERROR(EINVAL);
894 peak_table(coeff_data - count, &s->peak, count);
895 if (s->difference_coding)
896 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
898 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
899 if (bytes > bytestream2_get_bytes_left(&gb)) {
900 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
901 ret = AVERROR(EINVAL);
904 bytestream2_seek(&gb, bytes, SEEK_CUR);
906 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
908 if (s->subband_num_actual != 255)
911 /* Copy last line of coefficients if odd height */
912 if (highpass_height & 1) {
913 memcpy(&coeff_data[highpass_height * highpass_stride],
914 &coeff_data[(highpass_height - 1) * highpass_stride],
915 highpass_stride * sizeof(*coeff_data));
920 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
921 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
926 ff_thread_finish_setup(avctx);
928 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
929 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
930 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
931 ret = AVERROR(EINVAL);
936 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
937 ret = AVERROR(EINVAL);
941 if (s->transform_type == 0 && s->sample_type != 1) {
942 for (plane = 0; plane < s->planes && !ret; plane++) {
944 int lowpass_height = s->plane[plane].band[0][0].height;
945 int lowpass_width = s->plane[plane].band[0][0].width;
946 int highpass_stride = s->plane[plane].band[0][1].stride;
947 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
948 ptrdiff_t dst_linesize;
949 int16_t *low, *high, *output, *dst;
951 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
953 dst_linesize = pic->linesize[act_plane];
955 dst_linesize = pic->linesize[act_plane] / 2;
958 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
959 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
960 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
961 ret = AVERROR(EINVAL);
965 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
967 low = s->plane[plane].subband[0];
968 high = s->plane[plane].subband[2];
969 output = s->plane[plane].l_h[0];
970 for (i = 0; i < lowpass_width; i++) {
971 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
977 low = s->plane[plane].subband[1];
978 high = s->plane[plane].subband[3];
979 output = s->plane[plane].l_h[1];
981 for (i = 0; i < lowpass_width; i++) {
982 // note the stride of "low" is highpass_stride
983 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
989 low = s->plane[plane].l_h[0];
990 high = s->plane[plane].l_h[1];
991 output = s->plane[plane].subband[0];
992 for (i = 0; i < lowpass_height * 2; i++) {
993 horiz_filter(output, low, high, lowpass_width);
994 low += lowpass_width;
995 high += lowpass_width;
996 output += lowpass_width * 2;
999 output = s->plane[plane].subband[0];
1000 for (i = 0; i < lowpass_height * 2; i++) {
1001 for (j = 0; j < lowpass_width * 2; j++)
1004 output += lowpass_width * 2;
1009 lowpass_height = s->plane[plane].band[1][1].height;
1010 lowpass_width = s->plane[plane].band[1][1].width;
1011 highpass_stride = s->plane[plane].band[1][1].stride;
1013 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1014 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1015 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1016 ret = AVERROR(EINVAL);
1020 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1022 low = s->plane[plane].subband[0];
1023 high = s->plane[plane].subband[5];
1024 output = s->plane[plane].l_h[3];
1025 for (i = 0; i < lowpass_width; i++) {
1026 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1032 low = s->plane[plane].subband[4];
1033 high = s->plane[plane].subband[6];
1034 output = s->plane[plane].l_h[4];
1035 for (i = 0; i < lowpass_width; i++) {
1036 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1042 low = s->plane[plane].l_h[3];
1043 high = s->plane[plane].l_h[4];
1044 output = s->plane[plane].subband[0];
1045 for (i = 0; i < lowpass_height * 2; i++) {
1046 horiz_filter(output, low, high, lowpass_width);
1047 low += lowpass_width;
1048 high += lowpass_width;
1049 output += lowpass_width * 2;
1052 output = s->plane[plane].subband[0];
1053 for (i = 0; i < lowpass_height * 2; i++) {
1054 for (j = 0; j < lowpass_width * 2; j++)
1057 output += lowpass_width * 2;
1061 lowpass_height = s->plane[plane].band[2][1].height;
1062 lowpass_width = s->plane[plane].band[2][1].width;
1063 highpass_stride = s->plane[plane].band[2][1].stride;
1065 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
1066 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
1067 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1068 ret = AVERROR(EINVAL);
1072 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1073 if (s->progressive) {
1074 low = s->plane[plane].subband[0];
1075 high = s->plane[plane].subband[8];
1076 output = s->plane[plane].l_h[6];
1077 for (i = 0; i < lowpass_width; i++) {
1078 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1084 low = s->plane[plane].subband[7];
1085 high = s->plane[plane].subband[9];
1086 output = s->plane[plane].l_h[7];
1087 for (i = 0; i < lowpass_width; i++) {
1088 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1094 dst = (int16_t *)pic->data[act_plane];
1095 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1099 dst += pic->linesize[act_plane] >> 1;
1101 low = s->plane[plane].l_h[6];
1102 high = s->plane[plane].l_h[7];
1104 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1105 (lowpass_height * 2 > avctx->coded_height / 2 ||
1106 lowpass_width * 2 > avctx->coded_width / 2 )
1108 ret = AVERROR_INVALIDDATA;
1112 for (i = 0; i < lowpass_height * 2; i++) {
1113 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1114 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1116 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1117 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
1118 process_alpha(dst, lowpass_width * 2);
1119 low += lowpass_width;
1120 high += lowpass_width;
1121 dst += dst_linesize;
1124 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1125 pic->interlaced_frame = 1;
1126 low = s->plane[plane].subband[0];
1127 high = s->plane[plane].subband[7];
1128 output = s->plane[plane].l_h[6];
1129 for (i = 0; i < lowpass_height; i++) {
1130 horiz_filter(output, low, high, lowpass_width);
1131 low += lowpass_width;
1132 high += lowpass_width;
1133 output += lowpass_width * 2;
1136 low = s->plane[plane].subband[8];
1137 high = s->plane[plane].subband[9];
1138 output = s->plane[plane].l_h[7];
1139 for (i = 0; i < lowpass_height; i++) {
1140 horiz_filter(output, low, high, lowpass_width);
1141 low += lowpass_width;
1142 high += lowpass_width;
1143 output += lowpass_width * 2;
1146 dst = (int16_t *)pic->data[act_plane];
1147 low = s->plane[plane].l_h[6];
1148 high = s->plane[plane].l_h[7];
1149 for (i = 0; i < lowpass_height; i++) {
1150 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1151 low += lowpass_width * 2;
1152 high += lowpass_width * 2;
1153 dst += pic->linesize[act_plane];
1157 } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
1158 for (plane = 0; plane < s->planes && !ret; plane++) {
1159 int lowpass_height = s->plane[plane].band[0][0].height;
1160 int lowpass_width = s->plane[plane].band[0][0].width;
1161 int highpass_stride = s->plane[plane].band[0][1].stride;
1162 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1163 int16_t *low, *high, *output, *dst;
1164 ptrdiff_t dst_linesize;
1166 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1168 dst_linesize = pic->linesize[act_plane];
1170 dst_linesize = pic->linesize[act_plane] / 2;
1173 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
1174 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
1175 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1176 ret = AVERROR(EINVAL);
1180 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1182 low = s->plane[plane].subband[0];
1183 high = s->plane[plane].subband[2];
1184 output = s->plane[plane].l_h[0];
1185 for (i = 0; i < lowpass_width; i++) {
1186 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1192 low = s->plane[plane].subband[1];
1193 high = s->plane[plane].subband[3];
1194 output = s->plane[plane].l_h[1];
1195 for (i = 0; i < lowpass_width; i++) {
1196 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1202 low = s->plane[plane].l_h[0];
1203 high = s->plane[plane].l_h[1];
1204 output = s->plane[plane].l_h[7];
1205 for (i = 0; i < lowpass_height * 2; i++) {
1206 horiz_filter(output, low, high, lowpass_width);
1207 low += lowpass_width;
1208 high += lowpass_width;
1209 output += lowpass_width * 2;
1212 output = s->plane[plane].l_h[7];
1213 for (i = 0; i < lowpass_height * 2; i++) {
1214 for (j = 0; j < lowpass_width * 2; j++)
1217 output += lowpass_width * 2;
1221 lowpass_height = s->plane[plane].band[1][1].height;
1222 lowpass_width = s->plane[plane].band[1][1].width;
1223 highpass_stride = s->plane[plane].band[1][1].stride;
1225 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
1226 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
1227 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1228 ret = AVERROR(EINVAL);
1232 av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1234 low = s->plane[plane].l_h[7];
1235 high = s->plane[plane].subband[5];
1236 output = s->plane[plane].l_h[3];
1237 for (i = 0; i < lowpass_width; i++) {
1238 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1244 low = s->plane[plane].subband[4];
1245 high = s->plane[plane].subband[6];
1246 output = s->plane[plane].l_h[4];
1247 for (i = 0; i < lowpass_width; i++) {
1248 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1254 low = s->plane[plane].l_h[3];
1255 high = s->plane[plane].l_h[4];
1256 output = s->plane[plane].l_h[7];
1257 for (i = 0; i < lowpass_height * 2; i++) {
1258 horiz_filter(output, low, high, lowpass_width);
1259 low += lowpass_width;
1260 high += lowpass_width;
1261 output += lowpass_width * 2;
1264 output = s->plane[plane].l_h[7];
1265 for (i = 0; i < lowpass_height * 2; i++) {
1266 for (j = 0; j < lowpass_width * 2; j++)
1268 output += lowpass_width * 2;
1271 low = s->plane[plane].subband[7];
1272 high = s->plane[plane].subband[9];
1273 output = s->plane[plane].l_h[3];
1274 for (i = 0; i < lowpass_width; i++) {
1275 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1281 low = s->plane[plane].subband[8];
1282 high = s->plane[plane].subband[10];
1283 output = s->plane[plane].l_h[4];
1284 for (i = 0; i < lowpass_width; i++) {
1285 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1291 low = s->plane[plane].l_h[3];
1292 high = s->plane[plane].l_h[4];
1293 output = s->plane[plane].l_h[9];
1294 for (i = 0; i < lowpass_height * 2; i++) {
1295 horiz_filter(output, low, high, lowpass_width);
1296 low += lowpass_width;
1297 high += lowpass_width;
1298 output += lowpass_width * 2;
1301 lowpass_height = s->plane[plane].band[4][1].height;
1302 lowpass_width = s->plane[plane].band[4][1].width;
1303 highpass_stride = s->plane[plane].band[4][1].stride;
1304 av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
1306 if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
1307 !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
1308 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
1309 ret = AVERROR(EINVAL);
1313 low = s->plane[plane].l_h[7];
1314 high = s->plane[plane].l_h[9];
1315 output = s->plane[plane].l_h[7];
1316 for (i = 0; i < lowpass_height; i++) {
1317 inverse_temporal_filter(output, low, high, lowpass_width);
1318 low += lowpass_width;
1319 high += lowpass_width;
1321 if (s->progressive) {
1322 low = s->plane[plane].l_h[7];
1323 high = s->plane[plane].subband[15];
1324 output = s->plane[plane].l_h[6];
1325 for (i = 0; i < lowpass_width; i++) {
1326 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1332 low = s->plane[plane].subband[14];
1333 high = s->plane[plane].subband[16];
1334 output = s->plane[plane].l_h[7];
1335 for (i = 0; i < lowpass_width; i++) {
1336 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1342 low = s->plane[plane].l_h[9];
1343 high = s->plane[plane].subband[12];
1344 output = s->plane[plane].l_h[8];
1345 for (i = 0; i < lowpass_width; i++) {
1346 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
1352 low = s->plane[plane].subband[11];
1353 high = s->plane[plane].subband[13];
1354 output = s->plane[plane].l_h[9];
1355 for (i = 0; i < lowpass_width; i++) {
1356 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
1362 if (s->sample_type == 1)
1365 dst = (int16_t *)pic->data[act_plane];
1366 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1370 dst += pic->linesize[act_plane] >> 1;
1373 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1374 (lowpass_height * 2 > avctx->coded_height / 2 ||
1375 lowpass_width * 2 > avctx->coded_width / 2 )
1377 ret = AVERROR_INVALIDDATA;
1381 low = s->plane[plane].l_h[6];
1382 high = s->plane[plane].l_h[7];
1383 for (i = 0; i < lowpass_height * 2; i++) {
1384 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1385 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1387 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1388 low += lowpass_width;
1389 high += lowpass_width;
1390 dst += dst_linesize;
1393 pic->interlaced_frame = 1;
1394 low = s->plane[plane].l_h[7];
1395 high = s->plane[plane].subband[14];
1396 output = s->plane[plane].l_h[6];
1397 for (i = 0; i < lowpass_height; i++) {
1398 horiz_filter(output, low, high, lowpass_width);
1399 low += lowpass_width;
1400 high += lowpass_width;
1401 output += lowpass_width * 2;
1404 low = s->plane[plane].subband[15];
1405 high = s->plane[plane].subband[16];
1406 output = s->plane[plane].l_h[7];
1407 for (i = 0; i < lowpass_height; i++) {
1408 horiz_filter(output, low, high, lowpass_width);
1409 low += lowpass_width;
1410 high += lowpass_width;
1411 output += lowpass_width * 2;
1414 low = s->plane[plane].l_h[9];
1415 high = s->plane[plane].subband[11];
1416 output = s->plane[plane].l_h[8];
1417 for (i = 0; i < lowpass_height; i++) {
1418 horiz_filter(output, low, high, lowpass_width);
1419 low += lowpass_width;
1420 high += lowpass_width;
1421 output += lowpass_width * 2;
1424 low = s->plane[plane].subband[12];
1425 high = s->plane[plane].subband[13];
1426 output = s->plane[plane].l_h[9];
1427 for (i = 0; i < lowpass_height; i++) {
1428 horiz_filter(output, low, high, lowpass_width);
1429 low += lowpass_width;
1430 high += lowpass_width;
1431 output += lowpass_width * 2;
1434 if (s->sample_type == 1)
1437 dst = (int16_t *)pic->data[act_plane];
1438 low = s->plane[plane].l_h[6];
1439 high = s->plane[plane].l_h[7];
1440 for (i = 0; i < lowpass_height; i++) {
1441 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1442 low += lowpass_width * 2;
1443 high += lowpass_width * 2;
1444 dst += pic->linesize[act_plane];
1450 if (s->transform_type == 2 && s->sample_type == 1) {
1451 int16_t *low, *high, *dst;
1452 int lowpass_height, lowpass_width, highpass_stride;
1453 ptrdiff_t dst_linesize;
1455 for (plane = 0; plane < s->planes; plane++) {
1456 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
1458 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1460 dst_linesize = pic->linesize[act_plane];
1462 dst_linesize = pic->linesize[act_plane] / 2;
1465 lowpass_height = s->plane[plane].band[4][1].height;
1466 lowpass_width = s->plane[plane].band[4][1].width;
1467 highpass_stride = s->plane[plane].band[4][1].stride;
1469 if (s->progressive) {
1470 dst = (int16_t *)pic->data[act_plane];
1471 low = s->plane[plane].l_h[8];
1472 high = s->plane[plane].l_h[9];
1474 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
1478 dst += pic->linesize[act_plane] >> 1;
1481 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
1482 (lowpass_height * 2 > avctx->coded_height / 2 ||
1483 lowpass_width * 2 > avctx->coded_width / 2 )
1485 ret = AVERROR_INVALIDDATA;
1489 for (i = 0; i < lowpass_height * 2; i++) {
1490 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1491 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
1493 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
1494 low += lowpass_width;
1495 high += lowpass_width;
1496 dst += dst_linesize;
1499 dst = (int16_t *)pic->data[act_plane];
1500 low = s->plane[plane].l_h[8];
1501 high = s->plane[plane].l_h[9];
1502 for (i = 0; i < lowpass_height; i++) {
1503 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1504 low += lowpass_width * 2;
1505 high += lowpass_width * 2;
1506 dst += pic->linesize[act_plane];
1512 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1513 process_bayer(pic, s->bpc);
1522 static av_cold int cfhd_close(AVCodecContext *avctx)
1524 CFHDContext *s = avctx->priv_data;
1528 ff_free_vlc(&s->vlc_9);
1529 ff_free_vlc(&s->vlc_18);
1535 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1537 CFHDContext *psrc = src->priv_data;
1538 CFHDContext *pdst = dst->priv_data;
1541 if (dst == src || psrc->transform_type == 0)
1544 pdst->a_format = psrc->a_format;
1545 pdst->a_width = psrc->a_width;
1546 pdst->a_height = psrc->a_height;
1547 pdst->transform_type = psrc->transform_type;
1548 pdst->progressive = psrc->progressive;
1549 pdst->planes = psrc->planes;
1551 if (!pdst->plane[0].idwt_buf) {
1552 pdst->coded_width = pdst->a_width;
1553 pdst->coded_height = pdst->a_height;
1554 pdst->coded_format = pdst->a_format;
1555 ret = alloc_buffers(dst);
1560 for (int plane = 0; plane < pdst->planes; plane++) {
1561 memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
1562 memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
1563 pdst->plane[plane].idwt_size * sizeof(int16_t));
1570 AVCodec ff_cfhd_decoder = {
1572 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1573 .type = AVMEDIA_TYPE_VIDEO,
1574 .id = AV_CODEC_ID_CFHD,
1575 .priv_data_size = sizeof(CFHDContext),
1577 .close = cfhd_close,
1578 .decode = cfhd_decode,
1579 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1580 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1581 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,