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
54 LowpassPrecision = 35,
63 BitsPerComponent = 101,
71 static av_cold int cfhd_init(AVCodecContext *avctx)
73 CFHDContext *s = avctx->priv_data;
75 avctx->bits_per_raw_sample = 10;
78 return ff_cfhd_init_vlcs(s);
81 static void init_plane_defaults(CFHDContext *s)
85 s->subband_num_actual = 0;
88 static void init_peak_table_defaults(CFHDContext *s)
92 memset(&s->peak.base, 0, sizeof(s->peak.base));
95 static void init_frame_defaults(CFHDContext *s)
99 s->cropped_height = 0;
102 s->subband_cnt = SUBBAND_COUNT;
104 s->lowpass_precision = 16;
106 s->wavelet_depth = 3;
109 s->difference_coding = 0;
111 init_plane_defaults(s);
112 init_peak_table_defaults(s);
115 /* TODO: merge with VLC tables or use LUT */
116 static inline int dequant_and_decompand(int level, int quantisation, int codebook)
118 if (codebook == 0 || codebook == 1) {
119 int64_t abslevel = abs(level);
121 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
122 FFSIGN(level) * quantisation;
124 return level * quantisation;
126 return level * quantisation;
129 static inline void difference_coding(int16_t *band, int width, int height)
133 for (i = 0; i < height; i++) {
134 for (j = 1; j < width; j++) {
135 band[j] += band[j-1];
141 static inline void peak_table(int16_t *band, Peak *peak, int length)
144 for (i = 0; i < length; i++)
145 if (abs(band[i]) > peak->level)
146 band[i] = bytestream2_get_le16(&peak->base);
149 static inline void process_alpha(int16_t *alpha, int width)
152 for (i = 0; i < width; i++) {
154 channel -= ALPHA_COMPAND_DC_OFFSET;
156 channel *= ALPHA_COMPAND_GAIN;
158 channel = av_clip_uintp2(channel, 12);
163 static inline void process_bayer(AVFrame *frame)
165 const int linesize = frame->linesize[0];
166 uint16_t *r = (uint16_t *)frame->data[0];
167 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
168 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
169 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
170 const int mid = 2048;
172 for (int y = 0; y < frame->height >> 1; y++) {
173 for (int x = 0; x < frame->width; x += 2) {
183 R = (rg - mid) * 2 + g;
186 B = (bg - mid) * 2 + g;
188 R = av_clip_uintp2(R * 16, 16);
189 G1 = av_clip_uintp2(G1 * 16, 16);
190 G2 = av_clip_uintp2(G2 * 16, 16);
191 B = av_clip_uintp2(B * 16, 16);
206 static inline void filter(int16_t *output, ptrdiff_t out_stride,
207 int16_t *low, ptrdiff_t low_stride,
208 int16_t *high, ptrdiff_t high_stride,
214 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
215 output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
217 output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
219 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
220 output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
222 output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
224 for (i = 1; i < len - 1; i++) {
225 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
226 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
228 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
230 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
231 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
233 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
236 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
237 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
239 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
241 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
242 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
244 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
247 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
248 int width, int linesize, int plane)
252 for (i = 0; i < width; i++) {
253 even = (low[i] - high[i])/2;
254 odd = (low[i] + high[i])/2;
255 output[i] = av_clip_uintp2(even, 10);
256 output[i + linesize] = av_clip_uintp2(odd, 10);
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);
292 for (j = 0; j < 9; j++)
293 s->plane[i].subband[j] = NULL;
295 for (j = 0; j < 8; j++)
296 s->plane[i].l_h[j] = NULL;
302 static int alloc_buffers(AVCodecContext *avctx)
304 CFHDContext *s = avctx->priv_data;
305 int i, j, ret, planes;
306 int chroma_x_shift, chroma_y_shift;
309 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
311 s->coded_height *= 2;
314 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
316 avctx->pix_fmt = s->coded_format;
318 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
320 &chroma_y_shift)) < 0)
322 planes = av_pix_fmt_count_planes(s->coded_format);
323 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
329 for (i = 0; i < planes; i++) {
330 int w8, h8, w4, h4, w2, h2;
331 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
332 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
333 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
335 height = FFALIGN(height / 8, 2) * 8;
336 s->plane[i].width = width;
337 s->plane[i].height = height;
338 s->plane[i].stride = stride;
340 w8 = FFALIGN(s->plane[i].width / 8, 8);
341 h8 = FFALIGN(height, 8) / 8;
347 s->plane[i].idwt_buf =
348 av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
349 s->plane[i].idwt_tmp =
350 av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
351 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
352 return AVERROR(ENOMEM);
354 s->plane[i].subband[0] = s->plane[i].idwt_buf;
355 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
356 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
357 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
358 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
359 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
360 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
361 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
362 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
363 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
365 for (j = 0; j < DWT_LEVELS; j++) {
366 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
367 s->plane[i].band[j][k].a_width = w8 << j;
368 s->plane[i].band[j][k].a_height = h8 << j;
372 /* ll2 and ll1 commented out because they are done in-place */
373 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
374 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
375 // s->plane[i].l_h[2] = ll2;
376 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
377 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
378 // s->plane[i].l_h[5] = ll1;
379 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
380 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
383 s->a_height = s->coded_height;
384 s->a_width = s->coded_width;
385 s->a_format = s->coded_format;
390 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
393 CFHDContext *s = avctx->priv_data;
395 ThreadFrame frame = { .f = data };
397 int ret = 0, i, j, planes, plane, got_buffer = 0;
400 s->coded_format = AV_PIX_FMT_YUV422P10;
401 init_frame_defaults(s);
402 planes = av_pix_fmt_count_planes(s->coded_format);
404 bytestream2_init(&gb, avpkt->data, avpkt->size);
406 while (bytestream2_get_bytes_left(&gb) > 4) {
407 /* Bit weird but implement the tag parsing as the spec says */
408 uint16_t tagu = bytestream2_get_be16(&gb);
409 int16_t tag = (int16_t)tagu;
410 int8_t tag8 = (int8_t)(tagu >> 8);
411 uint16_t abstag = abs(tag);
412 int8_t abs_tag8 = abs(tag8);
413 uint16_t data = bytestream2_get_be16(&gb);
414 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
415 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
416 } else if (tag == SampleFlags) {
417 av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
418 s->progressive = data & 0x0001;
419 } else if (tag == ImageWidth) {
420 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
421 s->coded_width = data;
422 } else if (tag == ImageHeight) {
423 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
424 s->coded_height = data;
425 } else if (tag == 101) {
426 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
427 if (data < 1 || data > 31) {
428 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
429 ret = AVERROR(EINVAL);
433 } else if (tag == ChannelCount) {
434 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
435 s->channel_cnt = data;
437 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
438 ret = AVERROR_PATCHWELCOME;
441 } else if (tag == SubbandCount) {
442 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
443 if (data != SUBBAND_COUNT) {
444 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
445 ret = AVERROR_PATCHWELCOME;
448 } else if (tag == ChannelNumber) {
449 s->channel_num = data;
450 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
451 if (s->channel_num >= planes) {
452 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
453 ret = AVERROR(EINVAL);
456 init_plane_defaults(s);
457 } else if (tag == SubbandNumber) {
458 if (s->subband_num != 0 && data == 1) // hack
460 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
461 s->subband_num = data;
462 if (s->level >= DWT_LEVELS) {
463 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
464 ret = AVERROR(EINVAL);
467 if (s->subband_num > 3) {
468 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
469 ret = AVERROR(EINVAL);
472 } else if (tag == 51) {
473 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
474 s->subband_num_actual = data;
475 if (s->subband_num_actual >= 10) {
476 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
477 ret = AVERROR(EINVAL);
480 } else if (tag == LowpassPrecision)
481 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
482 else if (tag == Quantization) {
483 s->quantisation = data;
484 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
485 } else if (tag == PrescaleShift) {
486 s->prescale_shift[0] = (data >> 0) & 0x7;
487 s->prescale_shift[1] = (data >> 3) & 0x7;
488 s->prescale_shift[2] = (data >> 6) & 0x7;
489 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
490 } else if (tag == LowpassWidth) {
491 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
492 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
493 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
494 ret = AVERROR(EINVAL);
497 s->plane[s->channel_num].band[0][0].width = data;
498 s->plane[s->channel_num].band[0][0].stride = data;
499 } else if (tag == LowpassHeight) {
500 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
501 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
502 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
503 ret = AVERROR(EINVAL);
506 s->plane[s->channel_num].band[0][0].height = data;
507 } else if (tag == SampleType)
508 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
509 else if (tag == TransformType) {
511 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
512 ret = AVERROR_PATCHWELCOME;
515 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
516 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
517 if (abstag == 0x4001)
519 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
520 bytestream2_skipu(&gb, data * 4);
521 } else if (tag == 23) {
522 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
523 avpriv_report_missing_feature(avctx, "Skip frame");
524 ret = AVERROR_PATCHWELCOME;
526 } else if (tag == SampleIndexTable) {
527 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
528 if (data > bytestream2_get_bytes_left(&gb) / 4) {
529 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
530 ret = AVERROR_INVALIDDATA;
533 for (i = 0; i < data; i++) {
534 uint16_t tag2 = bytestream2_get_be16(&gb);
535 uint16_t val2 = bytestream2_get_be16(&gb);
536 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
538 } else if (tag == HighpassWidth) {
539 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);
541 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
542 ret = AVERROR(EINVAL);
545 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
546 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
547 } else if (tag == HighpassHeight) {
548 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
550 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
551 ret = AVERROR(EINVAL);
554 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
555 } else if (tag == 49) {
556 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
558 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
559 ret = AVERROR(EINVAL);
562 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
563 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
564 } else if (tag == 50) {
565 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
567 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
568 ret = AVERROR(EINVAL);
571 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
572 } else if (tag == 71) {
574 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
575 } else if (tag == 72) {
576 s->codebook = data & 0xf;
577 s->difference_coding = (data >> 4) & 1;
578 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
579 } else if (tag == 70) {
580 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
581 if (!(data == 10 || data == 12)) {
582 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
583 ret = AVERROR(EINVAL);
587 } else if (tag == EncodedFormat) {
588 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
590 s->coded_format = AV_PIX_FMT_YUV422P10;
591 } else if (data == 2) {
592 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
593 } else if (data == 3) {
594 s->coded_format = AV_PIX_FMT_GBRP12;
595 } else if (data == 4) {
596 s->coded_format = AV_PIX_FMT_GBRAP12;
598 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
599 ret = AVERROR_PATCHWELCOME;
602 planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
603 } else if (tag == -85) {
604 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
605 s->cropped_height = data;
606 } else if (tag == -75) {
607 s->peak.offset &= ~0xffff;
608 s->peak.offset |= (data & 0xffff);
611 } else if (tag == -76) {
612 s->peak.offset &= 0xffff;
613 s->peak.offset |= (data & 0xffffU)<<16;
616 } else if (tag == -74 && s->peak.offset) {
617 s->peak.level = data;
618 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
620 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
622 /* Some kind of end of header tag */
623 if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
624 s->coded_format != AV_PIX_FMT_NONE) {
625 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
626 s->a_format != s->coded_format) {
628 if ((ret = alloc_buffers(avctx)) < 0) {
633 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
636 if (s->cropped_height) {
637 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
638 if (avctx->height < height)
639 return AVERROR_INVALIDDATA;
640 avctx->height = height;
645 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
650 s->coded_format = AV_PIX_FMT_NONE;
653 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
655 /* Lowpass coefficients */
656 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
657 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
658 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
659 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
660 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
663 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
664 ret = AVERROR(EINVAL);
668 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
669 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
670 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
671 ret = AVERROR(EINVAL);
675 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
676 for (i = 0; i < lowpass_height; i++) {
677 for (j = 0; j < lowpass_width; j++)
678 coeff_data[j] = bytestream2_get_be16u(&gb);
680 coeff_data += lowpass_width;
683 /* Align to mod-4 position to continue reading tags */
684 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
686 /* Copy last line of coefficients if odd height */
687 if (lowpass_height & 1) {
688 memcpy(&coeff_data[lowpass_height * lowpass_width],
689 &coeff_data[(lowpass_height - 1) * lowpass_width],
690 lowpass_width * sizeof(*coeff_data));
693 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
696 if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
697 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
698 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
699 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
700 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
701 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
703 int a_expected = highpass_a_height * highpass_a_width;
704 int level, run, coeff;
705 int count = 0, bytes;
708 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
709 ret = AVERROR(EINVAL);
713 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
714 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
715 ret = AVERROR(EINVAL);
718 expected = highpass_height * highpass_stride;
720 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);
722 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
724 OPEN_READER(re, &s->gb);
727 UPDATE_CACHE(re, &s->gb);
728 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
737 if (count > expected)
740 coeff = dequant_and_decompand(level, s->quantisation, 0);
741 for (i = 0; i < run; i++)
742 *coeff_data++ = coeff;
746 UPDATE_CACHE(re, &s->gb);
747 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
751 if (level == 255 && run == 2)
756 if (count > expected)
759 coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
760 for (i = 0; i < run; i++)
761 *coeff_data++ = coeff;
764 CLOSE_READER(re, &s->gb);
767 if (count > expected) {
768 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
769 ret = AVERROR(EINVAL);
773 peak_table(coeff_data - count, &s->peak, count);
774 if (s->difference_coding)
775 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
777 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
778 if (bytes > bytestream2_get_bytes_left(&gb)) {
779 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
780 ret = AVERROR(EINVAL);
783 bytestream2_seek(&gb, bytes, SEEK_CUR);
785 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
788 /* Copy last line of coefficients if odd height */
789 if (highpass_height & 1) {
790 memcpy(&coeff_data[highpass_height * highpass_stride],
791 &coeff_data[(highpass_height - 1) * highpass_stride],
792 highpass_stride * sizeof(*coeff_data));
797 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
798 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
799 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
800 ret = AVERROR(EINVAL);
805 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
806 ret = AVERROR(EINVAL);
810 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
811 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
813 return AVERROR_INVALIDDATA;
817 for (plane = 0; plane < planes && !ret; plane++) {
819 int lowpass_height = s->plane[plane].band[0][0].height;
820 int lowpass_width = s->plane[plane].band[0][0].width;
821 int highpass_stride = s->plane[plane].band[0][1].stride;
822 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
823 ptrdiff_t dst_linesize;
824 int16_t *low, *high, *output, *dst;
826 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
828 dst_linesize = pic->linesize[act_plane];
830 dst_linesize = pic->linesize[act_plane] / 2;
833 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
834 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
835 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
836 ret = AVERROR(EINVAL);
840 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
842 low = s->plane[plane].subband[0];
843 high = s->plane[plane].subband[2];
844 output = s->plane[plane].l_h[0];
845 for (i = 0; i < lowpass_width; i++) {
846 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
852 low = s->plane[plane].subband[1];
853 high = s->plane[plane].subband[3];
854 output = s->plane[plane].l_h[1];
856 for (i = 0; i < lowpass_width; i++) {
857 // note the stride of "low" is highpass_stride
858 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
864 low = s->plane[plane].l_h[0];
865 high = s->plane[plane].l_h[1];
866 output = s->plane[plane].subband[0];
867 for (i = 0; i < lowpass_height * 2; i++) {
868 horiz_filter(output, low, high, lowpass_width);
869 low += lowpass_width;
870 high += lowpass_width;
871 output += lowpass_width * 2;
874 output = s->plane[plane].subband[0];
875 for (i = 0; i < lowpass_height * 2; i++) {
876 for (j = 0; j < lowpass_width * 2; j++)
879 output += lowpass_width * 2;
884 lowpass_height = s->plane[plane].band[1][1].height;
885 lowpass_width = s->plane[plane].band[1][1].width;
886 highpass_stride = s->plane[plane].band[1][1].stride;
888 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
889 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
890 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
891 ret = AVERROR(EINVAL);
895 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
897 low = s->plane[plane].subband[0];
898 high = s->plane[plane].subband[5];
899 output = s->plane[plane].l_h[3];
900 for (i = 0; i < lowpass_width; i++) {
901 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
907 low = s->plane[plane].subband[4];
908 high = s->plane[plane].subband[6];
909 output = s->plane[plane].l_h[4];
910 for (i = 0; i < lowpass_width; i++) {
911 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
917 low = s->plane[plane].l_h[3];
918 high = s->plane[plane].l_h[4];
919 output = s->plane[plane].subband[0];
920 for (i = 0; i < lowpass_height * 2; i++) {
921 horiz_filter(output, low, high, lowpass_width);
922 low += lowpass_width;
923 high += lowpass_width;
924 output += lowpass_width * 2;
927 output = s->plane[plane].subband[0];
928 for (i = 0; i < lowpass_height * 2; i++) {
929 for (j = 0; j < lowpass_width * 2; j++)
932 output += lowpass_width * 2;
936 lowpass_height = s->plane[plane].band[2][1].height;
937 lowpass_width = s->plane[plane].band[2][1].width;
938 highpass_stride = s->plane[plane].band[2][1].stride;
940 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
941 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
942 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
943 ret = AVERROR(EINVAL);
947 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
948 if (s->progressive) {
949 low = s->plane[plane].subband[0];
950 high = s->plane[plane].subband[8];
951 output = s->plane[plane].l_h[6];
952 for (i = 0; i < lowpass_width; i++) {
953 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
959 low = s->plane[plane].subband[7];
960 high = s->plane[plane].subband[9];
961 output = s->plane[plane].l_h[7];
962 for (i = 0; i < lowpass_width; i++) {
963 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
969 dst = (int16_t *)pic->data[act_plane];
970 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
974 dst += pic->linesize[act_plane] >> 1;
976 low = s->plane[plane].l_h[6];
977 high = s->plane[plane].l_h[7];
979 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
980 (lowpass_height * 2 > avctx->coded_height / 2 ||
981 lowpass_width * 2 > avctx->coded_width / 2 )
983 ret = AVERROR_INVALIDDATA;
987 for (i = 0; i < lowpass_height * 2; i++) {
988 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
989 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
991 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
992 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
993 process_alpha(dst, lowpass_width * 2);
994 low += lowpass_width;
995 high += lowpass_width;
999 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1000 pic->interlaced_frame = 1;
1001 low = s->plane[plane].subband[0];
1002 high = s->plane[plane].subband[7];
1003 output = s->plane[plane].l_h[6];
1004 for (i = 0; i < lowpass_height; i++) {
1005 horiz_filter(output, low, high, lowpass_width);
1006 low += lowpass_width;
1007 high += lowpass_width;
1008 output += lowpass_width * 2;
1011 low = s->plane[plane].subband[8];
1012 high = s->plane[plane].subband[9];
1013 output = s->plane[plane].l_h[7];
1014 for (i = 0; i < lowpass_height; i++) {
1015 horiz_filter(output, low, high, lowpass_width);
1016 low += lowpass_width;
1017 high += lowpass_width;
1018 output += lowpass_width * 2;
1021 dst = (int16_t *)pic->data[act_plane];
1022 low = s->plane[plane].l_h[6];
1023 high = s->plane[plane].l_h[7];
1024 for (i = 0; i < lowpass_height; i++) {
1025 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1026 low += lowpass_width * 2;
1027 high += lowpass_width * 2;
1028 dst += pic->linesize[act_plane];
1034 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1044 static av_cold int cfhd_close(AVCodecContext *avctx)
1046 CFHDContext *s = avctx->priv_data;
1050 ff_free_vlc(&s->vlc_9);
1051 ff_free_vlc(&s->vlc_18);
1056 AVCodec ff_cfhd_decoder = {
1058 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1059 .type = AVMEDIA_TYPE_VIDEO,
1060 .id = AV_CODEC_ID_CFHD,
1061 .priv_data_size = sizeof(CFHDContext),
1063 .close = cfhd_close,
1064 .decode = cfhd_decode,
1065 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1066 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,