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 + ((768 * 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->cropped_height = 0;
94 s->subband_cnt = SUBBAND_COUNT;
96 s->lowpass_precision = 16;
101 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)
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 = 2048;
158 for (int y = 0; y < frame->height >> 1; y++) {
159 for (int x = 0; x < frame->width; x += 2) {
169 R = (rg - mid) * 2 + g;
172 B = (bg - mid) * 2 + g;
174 R = av_clip_uintp2(R * 16, 16);
175 G1 = av_clip_uintp2(G1 * 16, 16);
176 G2 = av_clip_uintp2(G2 * 16, 16);
177 B = av_clip_uintp2(B * 16, 16);
192 static inline void filter(int16_t *output, ptrdiff_t out_stride,
193 int16_t *low, ptrdiff_t low_stride,
194 int16_t *high, ptrdiff_t high_stride,
200 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
201 output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
203 output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
205 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
206 output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
208 output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
210 for (i = 1; i < len - 1; i++) {
211 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
212 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
214 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
216 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
217 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
219 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
222 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
223 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
225 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
227 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
228 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
230 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
233 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
234 int width, int linesize, int plane)
238 for (i = 0; i < width; i++) {
239 even = (low[i] - high[i])/2;
240 odd = (low[i] + high[i])/2;
241 output[i] = av_clip_uintp2(even, 10);
242 output[i + linesize] = av_clip_uintp2(odd, 10);
245 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
248 filter(output, 1, low, 1, high, 1, width, 0);
251 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
254 filter(output, 1, low, 1, high, 1, width, clip);
257 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
260 filter(output, 2, low, 1, high, 1, width, clip);
263 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
264 int16_t *low, ptrdiff_t low_stride,
265 int16_t *high, ptrdiff_t high_stride, int len)
267 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
270 static void free_buffers(CFHDContext *s)
274 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
275 av_freep(&s->plane[i].idwt_buf);
276 av_freep(&s->plane[i].idwt_tmp);
278 for (j = 0; j < 9; j++)
279 s->plane[i].subband[j] = NULL;
281 for (j = 0; j < 8; j++)
282 s->plane[i].l_h[j] = NULL;
288 static int alloc_buffers(AVCodecContext *avctx)
290 CFHDContext *s = avctx->priv_data;
291 int i, j, ret, planes;
292 int chroma_x_shift, chroma_y_shift;
295 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
297 s->coded_height *= 2;
300 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
302 avctx->pix_fmt = s->coded_format;
304 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
306 &chroma_y_shift)) < 0)
308 planes = av_pix_fmt_count_planes(s->coded_format);
309 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
315 for (i = 0; i < planes; i++) {
316 int w8, h8, w4, h4, w2, h2;
317 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
318 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
319 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
321 height = FFALIGN(height / 8, 2) * 8;
322 s->plane[i].width = width;
323 s->plane[i].height = height;
324 s->plane[i].stride = stride;
326 w8 = FFALIGN(s->plane[i].width / 8, 8);
327 h8 = FFALIGN(height, 8) / 8;
333 s->plane[i].idwt_buf =
334 av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
335 s->plane[i].idwt_tmp =
336 av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
337 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
338 return AVERROR(ENOMEM);
340 s->plane[i].subband[0] = s->plane[i].idwt_buf;
341 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
342 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
343 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
344 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
345 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
346 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
347 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
348 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
349 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
351 for (j = 0; j < DWT_LEVELS; j++) {
352 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
353 s->plane[i].band[j][k].a_width = w8 << j;
354 s->plane[i].band[j][k].a_height = h8 << j;
358 /* ll2 and ll1 commented out because they are done in-place */
359 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
360 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
361 // s->plane[i].l_h[2] = ll2;
362 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
363 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
364 // s->plane[i].l_h[5] = ll1;
365 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
366 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
369 s->a_height = s->coded_height;
370 s->a_width = s->coded_width;
371 s->a_format = s->coded_format;
376 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
379 CFHDContext *s = avctx->priv_data;
381 ThreadFrame frame = { .f = data };
383 int ret = 0, i, j, planes, plane, got_buffer = 0;
386 s->coded_format = AV_PIX_FMT_YUV422P10;
387 init_frame_defaults(s);
388 planes = av_pix_fmt_count_planes(s->coded_format);
390 bytestream2_init(&gb, avpkt->data, avpkt->size);
392 while (bytestream2_get_bytes_left(&gb) > 4) {
393 /* Bit weird but implement the tag parsing as the spec says */
394 uint16_t tagu = bytestream2_get_be16(&gb);
395 int16_t tag = (int16_t)tagu;
396 int8_t tag8 = (int8_t)(tagu >> 8);
397 uint16_t abstag = abs(tag);
398 int8_t abs_tag8 = abs(tag8);
399 uint16_t data = bytestream2_get_be16(&gb);
400 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
401 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
402 } else if (tag == SampleFlags) {
403 av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
404 s->progressive = data & 0x0001;
405 } else if (tag == ImageWidth) {
406 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
407 s->coded_width = data;
408 } else if (tag == ImageHeight) {
409 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
410 s->coded_height = data;
411 } else if (tag == BitsPerComponent) {
412 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
413 if (data < 1 || data > 31) {
414 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
415 ret = AVERROR(EINVAL);
419 } else if (tag == ChannelCount) {
420 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
421 s->channel_cnt = data;
423 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
424 ret = AVERROR_PATCHWELCOME;
427 } else if (tag == SubbandCount) {
428 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
429 if (data != SUBBAND_COUNT) {
430 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
431 ret = AVERROR_PATCHWELCOME;
434 } else if (tag == ChannelNumber) {
435 s->channel_num = data;
436 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
437 if (s->channel_num >= planes) {
438 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
439 ret = AVERROR(EINVAL);
442 init_plane_defaults(s);
443 } else if (tag == SubbandNumber) {
444 if (s->subband_num != 0 && data == 1) // hack
446 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
447 s->subband_num = data;
448 if (s->level >= DWT_LEVELS) {
449 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
450 ret = AVERROR(EINVAL);
453 if (s->subband_num > 3) {
454 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
455 ret = AVERROR(EINVAL);
458 } else if (tag == SubbandBand) {
459 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
460 s->subband_num_actual = data;
461 if (s->subband_num_actual >= 10) {
462 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
463 ret = AVERROR(EINVAL);
466 } else if (tag == LowpassPrecision)
467 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
468 else if (tag == Quantization) {
469 s->quantisation = data;
470 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
471 } else if (tag == PrescaleShift) {
472 s->prescale_shift[0] = (data >> 0) & 0x7;
473 s->prescale_shift[1] = (data >> 3) & 0x7;
474 s->prescale_shift[2] = (data >> 6) & 0x7;
475 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
476 } else if (tag == LowpassWidth) {
477 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
478 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
479 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
480 ret = AVERROR(EINVAL);
483 s->plane[s->channel_num].band[0][0].width = data;
484 s->plane[s->channel_num].band[0][0].stride = data;
485 } else if (tag == LowpassHeight) {
486 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
487 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
488 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
489 ret = AVERROR(EINVAL);
492 s->plane[s->channel_num].band[0][0].height = data;
493 } else if (tag == SampleType)
494 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
495 else if (tag == TransformType) {
497 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
498 ret = AVERROR_PATCHWELCOME;
501 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
502 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
503 if (abstag == 0x4001)
505 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
506 bytestream2_skipu(&gb, data * 4);
507 } else if (tag == 23) {
508 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
509 avpriv_report_missing_feature(avctx, "Skip frame");
510 ret = AVERROR_PATCHWELCOME;
512 } else if (tag == SampleIndexTable) {
513 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
514 if (data > bytestream2_get_bytes_left(&gb) / 4) {
515 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
516 ret = AVERROR_INVALIDDATA;
519 for (i = 0; i < data; i++) {
520 uint16_t tag2 = bytestream2_get_be16(&gb);
521 uint16_t val2 = bytestream2_get_be16(&gb);
522 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
524 } else if (tag == HighpassWidth) {
525 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);
527 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
528 ret = AVERROR(EINVAL);
531 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
532 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
533 } else if (tag == HighpassHeight) {
534 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
536 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
537 ret = AVERROR(EINVAL);
540 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
541 } else if (tag == BandWidth) {
542 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
544 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
545 ret = AVERROR(EINVAL);
548 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
549 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
550 } else if (tag == BandHeight) {
551 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
553 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
554 ret = AVERROR(EINVAL);
557 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
558 } else if (tag == InputFormat) {
559 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
560 } else if (tag == BandCodingFlags) {
561 s->codebook = data & 0xf;
562 s->difference_coding = (data >> 4) & 1;
563 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
564 } else if (tag == Precision) {
565 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
566 if (!(data == 10 || data == 12)) {
567 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
568 ret = AVERROR(EINVAL);
571 avctx->bits_per_raw_sample = s->bpc = data;
572 } else if (tag == EncodedFormat) {
573 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
575 s->coded_format = AV_PIX_FMT_YUV422P10;
576 } else if (data == 2) {
577 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
578 } else if (data == 3) {
579 s->coded_format = AV_PIX_FMT_GBRP12;
580 } else if (data == 4) {
581 s->coded_format = AV_PIX_FMT_GBRAP12;
583 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
584 ret = AVERROR_PATCHWELCOME;
587 planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
588 } else if (tag == -85) {
589 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
590 s->cropped_height = data;
591 } else if (tag == -75) {
592 s->peak.offset &= ~0xffff;
593 s->peak.offset |= (data & 0xffff);
596 } else if (tag == -76) {
597 s->peak.offset &= 0xffff;
598 s->peak.offset |= (data & 0xffffU)<<16;
601 } else if (tag == -74 && s->peak.offset) {
602 s->peak.level = data;
603 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
605 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
607 /* Some kind of end of header tag */
608 if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
609 s->coded_format != AV_PIX_FMT_NONE) {
610 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
611 s->a_format != s->coded_format) {
613 if ((ret = alloc_buffers(avctx)) < 0) {
618 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
621 if (s->cropped_height) {
622 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
623 if (avctx->height < height)
624 return AVERROR_INVALIDDATA;
625 avctx->height = height;
630 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
635 s->coded_format = AV_PIX_FMT_NONE;
638 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
640 /* Lowpass coefficients */
641 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
642 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
643 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
644 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
645 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
648 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
649 ret = AVERROR(EINVAL);
653 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
654 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
655 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
656 ret = AVERROR(EINVAL);
660 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
661 for (i = 0; i < lowpass_height; i++) {
662 for (j = 0; j < lowpass_width; j++)
663 coeff_data[j] = bytestream2_get_be16u(&gb);
665 coeff_data += lowpass_width;
668 /* Align to mod-4 position to continue reading tags */
669 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
671 /* Copy last line of coefficients if odd height */
672 if (lowpass_height & 1) {
673 memcpy(&coeff_data[lowpass_height * lowpass_width],
674 &coeff_data[(lowpass_height - 1) * lowpass_width],
675 lowpass_width * sizeof(*coeff_data));
678 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
681 if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
682 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
683 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
684 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
685 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
686 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
688 int a_expected = highpass_a_height * highpass_a_width;
689 int level, run, coeff;
690 int count = 0, bytes;
693 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
694 ret = AVERROR(EINVAL);
698 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
699 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
700 ret = AVERROR(EINVAL);
703 expected = highpass_height * highpass_stride;
705 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);
707 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
709 OPEN_READER(re, &s->gb);
712 UPDATE_CACHE(re, &s->gb);
713 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
722 if (count > expected)
725 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
726 for (i = 0; i < run; i++)
727 *coeff_data++ = coeff;
731 UPDATE_CACHE(re, &s->gb);
732 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
736 if (level == 255 && run == 2)
741 if (count > expected)
744 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
745 for (i = 0; i < run; i++)
746 *coeff_data++ = coeff;
749 CLOSE_READER(re, &s->gb);
752 if (count > expected) {
753 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
754 ret = AVERROR(EINVAL);
758 peak_table(coeff_data - count, &s->peak, count);
759 if (s->difference_coding)
760 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
762 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
763 if (bytes > bytestream2_get_bytes_left(&gb)) {
764 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
765 ret = AVERROR(EINVAL);
768 bytestream2_seek(&gb, bytes, SEEK_CUR);
770 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
773 /* Copy last line of coefficients if odd height */
774 if (highpass_height & 1) {
775 memcpy(&coeff_data[highpass_height * highpass_stride],
776 &coeff_data[(highpass_height - 1) * highpass_stride],
777 highpass_stride * sizeof(*coeff_data));
782 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
783 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
784 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
785 ret = AVERROR(EINVAL);
790 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
791 ret = AVERROR(EINVAL);
795 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
796 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
798 return AVERROR_INVALIDDATA;
802 for (plane = 0; plane < planes && !ret; plane++) {
804 int lowpass_height = s->plane[plane].band[0][0].height;
805 int lowpass_width = s->plane[plane].band[0][0].width;
806 int highpass_stride = s->plane[plane].band[0][1].stride;
807 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
808 ptrdiff_t dst_linesize;
809 int16_t *low, *high, *output, *dst;
811 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
813 dst_linesize = pic->linesize[act_plane];
815 dst_linesize = pic->linesize[act_plane] / 2;
818 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
819 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
820 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
821 ret = AVERROR(EINVAL);
825 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
827 low = s->plane[plane].subband[0];
828 high = s->plane[plane].subband[2];
829 output = s->plane[plane].l_h[0];
830 for (i = 0; i < lowpass_width; i++) {
831 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
837 low = s->plane[plane].subband[1];
838 high = s->plane[plane].subband[3];
839 output = s->plane[plane].l_h[1];
841 for (i = 0; i < lowpass_width; i++) {
842 // note the stride of "low" is highpass_stride
843 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
849 low = s->plane[plane].l_h[0];
850 high = s->plane[plane].l_h[1];
851 output = s->plane[plane].subband[0];
852 for (i = 0; i < lowpass_height * 2; i++) {
853 horiz_filter(output, low, high, lowpass_width);
854 low += lowpass_width;
855 high += lowpass_width;
856 output += lowpass_width * 2;
859 output = s->plane[plane].subband[0];
860 for (i = 0; i < lowpass_height * 2; i++) {
861 for (j = 0; j < lowpass_width * 2; j++)
864 output += lowpass_width * 2;
869 lowpass_height = s->plane[plane].band[1][1].height;
870 lowpass_width = s->plane[plane].band[1][1].width;
871 highpass_stride = s->plane[plane].band[1][1].stride;
873 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
874 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
875 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
876 ret = AVERROR(EINVAL);
880 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
882 low = s->plane[plane].subband[0];
883 high = s->plane[plane].subband[5];
884 output = s->plane[plane].l_h[3];
885 for (i = 0; i < lowpass_width; i++) {
886 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
892 low = s->plane[plane].subband[4];
893 high = s->plane[plane].subband[6];
894 output = s->plane[plane].l_h[4];
895 for (i = 0; i < lowpass_width; i++) {
896 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
902 low = s->plane[plane].l_h[3];
903 high = s->plane[plane].l_h[4];
904 output = s->plane[plane].subband[0];
905 for (i = 0; i < lowpass_height * 2; i++) {
906 horiz_filter(output, low, high, lowpass_width);
907 low += lowpass_width;
908 high += lowpass_width;
909 output += lowpass_width * 2;
912 output = s->plane[plane].subband[0];
913 for (i = 0; i < lowpass_height * 2; i++) {
914 for (j = 0; j < lowpass_width * 2; j++)
917 output += lowpass_width * 2;
921 lowpass_height = s->plane[plane].band[2][1].height;
922 lowpass_width = s->plane[plane].band[2][1].width;
923 highpass_stride = s->plane[plane].band[2][1].stride;
925 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
926 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
927 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
928 ret = AVERROR(EINVAL);
932 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
933 if (s->progressive) {
934 low = s->plane[plane].subband[0];
935 high = s->plane[plane].subband[8];
936 output = s->plane[plane].l_h[6];
937 for (i = 0; i < lowpass_width; i++) {
938 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
944 low = s->plane[plane].subband[7];
945 high = s->plane[plane].subband[9];
946 output = s->plane[plane].l_h[7];
947 for (i = 0; i < lowpass_width; i++) {
948 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
954 dst = (int16_t *)pic->data[act_plane];
955 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
959 dst += pic->linesize[act_plane] >> 1;
961 low = s->plane[plane].l_h[6];
962 high = s->plane[plane].l_h[7];
964 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
965 (lowpass_height * 2 > avctx->coded_height / 2 ||
966 lowpass_width * 2 > avctx->coded_width / 2 )
968 ret = AVERROR_INVALIDDATA;
972 for (i = 0; i < lowpass_height * 2; i++) {
973 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
974 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
976 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
977 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
978 process_alpha(dst, lowpass_width * 2);
979 low += lowpass_width;
980 high += lowpass_width;
984 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
985 pic->interlaced_frame = 1;
986 low = s->plane[plane].subband[0];
987 high = s->plane[plane].subband[7];
988 output = s->plane[plane].l_h[6];
989 for (i = 0; i < lowpass_height; i++) {
990 horiz_filter(output, low, high, lowpass_width);
991 low += lowpass_width;
992 high += lowpass_width;
993 output += lowpass_width * 2;
996 low = s->plane[plane].subband[8];
997 high = s->plane[plane].subband[9];
998 output = s->plane[plane].l_h[7];
999 for (i = 0; i < lowpass_height; i++) {
1000 horiz_filter(output, low, high, lowpass_width);
1001 low += lowpass_width;
1002 high += lowpass_width;
1003 output += lowpass_width * 2;
1006 dst = (int16_t *)pic->data[act_plane];
1007 low = s->plane[plane].l_h[6];
1008 high = s->plane[plane].l_h[7];
1009 for (i = 0; i < lowpass_height; i++) {
1010 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1011 low += lowpass_width * 2;
1012 high += lowpass_width * 2;
1013 dst += pic->linesize[act_plane];
1019 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1029 static av_cold int cfhd_close(AVCodecContext *avctx)
1031 CFHDContext *s = avctx->priv_data;
1035 ff_free_vlc(&s->vlc_9);
1036 ff_free_vlc(&s->vlc_18);
1041 AVCodec ff_cfhd_decoder = {
1043 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1044 .type = AVMEDIA_TYPE_VIDEO,
1045 .id = AV_CODEC_ID_CFHD,
1046 .priv_data_size = sizeof(CFHDContext),
1048 .close = cfhd_close,
1049 .decode = cfhd_decode,
1050 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1051 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,