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->cropped_height = 0;
94 s->subband_cnt = SUBBAND_COUNT;
96 s->lowpass_precision = 16;
99 s->difference_coding = 0;
101 init_plane_defaults(s);
102 init_peak_table_defaults(s);
105 static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
107 if (codebook == 0 || codebook == 1) {
108 return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
110 return level * quantisation;
113 static inline void difference_coding(int16_t *band, int width, int height)
117 for (i = 0; i < height; i++) {
118 for (j = 1; j < width; j++) {
119 band[j] += band[j-1];
125 static inline void peak_table(int16_t *band, Peak *peak, int length)
128 for (i = 0; i < length; i++)
129 if (abs(band[i]) > peak->level)
130 band[i] = bytestream2_get_le16(&peak->base);
133 static inline void process_alpha(int16_t *alpha, int width)
136 for (i = 0; i < width; i++) {
138 channel -= ALPHA_COMPAND_DC_OFFSET;
140 channel *= ALPHA_COMPAND_GAIN;
142 channel = av_clip_uintp2(channel, 12);
147 static inline void process_bayer(AVFrame *frame)
149 const int linesize = frame->linesize[0];
150 uint16_t *r = (uint16_t *)frame->data[0];
151 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
152 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
153 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
154 const int mid = 2048;
156 for (int y = 0; y < frame->height >> 1; y++) {
157 for (int x = 0; x < frame->width; x += 2) {
167 R = (rg - mid) * 2 + g;
170 B = (bg - mid) * 2 + g;
172 R = av_clip_uintp2(R * 16, 16);
173 G1 = av_clip_uintp2(G1 * 16, 16);
174 G2 = av_clip_uintp2(G2 * 16, 16);
175 B = av_clip_uintp2(B * 16, 16);
190 static inline void filter(int16_t *output, ptrdiff_t out_stride,
191 int16_t *low, ptrdiff_t low_stride,
192 int16_t *high, ptrdiff_t high_stride,
198 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
199 output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
201 output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
203 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
204 output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
206 output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
208 for (i = 1; i < len - 1; i++) {
209 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
210 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
212 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
214 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
215 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
217 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
220 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
221 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
223 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
225 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
226 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
228 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
231 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
232 int width, int linesize, int plane)
236 for (i = 0; i < width; i++) {
237 even = (low[i] - high[i])/2;
238 odd = (low[i] + high[i])/2;
239 output[i] = av_clip_uintp2(even, 10);
240 output[i + linesize] = av_clip_uintp2(odd, 10);
243 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
246 filter(output, 1, low, 1, high, 1, width, 0);
249 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
252 filter(output, 1, low, 1, high, 1, width, clip);
255 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
258 filter(output, 2, low, 1, high, 1, width, clip);
261 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
262 int16_t *low, ptrdiff_t low_stride,
263 int16_t *high, ptrdiff_t high_stride, int len)
265 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
268 static void free_buffers(CFHDContext *s)
272 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
273 av_freep(&s->plane[i].idwt_buf);
274 av_freep(&s->plane[i].idwt_tmp);
276 for (j = 0; j < 9; j++)
277 s->plane[i].subband[j] = NULL;
279 for (j = 0; j < 8; j++)
280 s->plane[i].l_h[j] = NULL;
286 static int alloc_buffers(AVCodecContext *avctx)
288 CFHDContext *s = avctx->priv_data;
289 int i, j, ret, planes;
290 int chroma_x_shift, chroma_y_shift;
293 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
295 s->coded_height *= 2;
298 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
300 avctx->pix_fmt = s->coded_format;
302 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
304 &chroma_y_shift)) < 0)
306 planes = av_pix_fmt_count_planes(s->coded_format);
307 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
313 for (i = 0; i < planes; i++) {
314 int w8, h8, w4, h4, w2, h2;
315 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
316 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
317 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
319 height = FFALIGN(height / 8, 2) * 8;
320 s->plane[i].width = width;
321 s->plane[i].height = height;
322 s->plane[i].stride = stride;
324 w8 = FFALIGN(s->plane[i].width / 8, 8);
325 h8 = FFALIGN(height, 8) / 8;
331 s->plane[i].idwt_buf =
332 av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
333 s->plane[i].idwt_tmp =
334 av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
335 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
336 return AVERROR(ENOMEM);
338 s->plane[i].subband[0] = s->plane[i].idwt_buf;
339 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
340 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
341 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
342 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
343 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
344 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
345 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
346 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
347 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
349 for (j = 0; j < DWT_LEVELS; j++) {
350 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
351 s->plane[i].band[j][k].a_width = w8 << j;
352 s->plane[i].band[j][k].a_height = h8 << j;
356 /* ll2 and ll1 commented out because they are done in-place */
357 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
358 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
359 // s->plane[i].l_h[2] = ll2;
360 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
361 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
362 // s->plane[i].l_h[5] = ll1;
363 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
364 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
367 s->a_height = s->coded_height;
368 s->a_width = s->coded_width;
369 s->a_format = s->coded_format;
374 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
377 CFHDContext *s = avctx->priv_data;
379 ThreadFrame frame = { .f = data };
381 int ret = 0, i, j, planes, plane, got_buffer = 0;
384 s->coded_format = AV_PIX_FMT_YUV422P10;
385 init_frame_defaults(s);
386 planes = av_pix_fmt_count_planes(s->coded_format);
388 bytestream2_init(&gb, avpkt->data, avpkt->size);
390 while (bytestream2_get_bytes_left(&gb) > 4) {
391 /* Bit weird but implement the tag parsing as the spec says */
392 uint16_t tagu = bytestream2_get_be16(&gb);
393 int16_t tag = (int16_t)tagu;
394 int8_t tag8 = (int8_t)(tagu >> 8);
395 uint16_t abstag = abs(tag);
396 int8_t abs_tag8 = abs(tag8);
397 uint16_t data = bytestream2_get_be16(&gb);
398 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
399 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
400 } else if (tag == SampleFlags) {
401 av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
402 s->progressive = data & 0x0001;
403 } else if (tag == ImageWidth) {
404 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
405 s->coded_width = data;
406 } else if (tag == ImageHeight) {
407 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
408 s->coded_height = data;
409 } else if (tag == BitsPerComponent) {
410 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
411 if (data < 1 || data > 31) {
412 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
413 ret = AVERROR(EINVAL);
417 } else if (tag == ChannelCount) {
418 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
419 s->channel_cnt = data;
421 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
422 ret = AVERROR_PATCHWELCOME;
425 } else if (tag == SubbandCount) {
426 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
427 if (data != SUBBAND_COUNT) {
428 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
429 ret = AVERROR_PATCHWELCOME;
432 } else if (tag == ChannelNumber) {
433 s->channel_num = data;
434 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
435 if (s->channel_num >= planes) {
436 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
437 ret = AVERROR(EINVAL);
440 init_plane_defaults(s);
441 } else if (tag == SubbandNumber) {
442 if (s->subband_num != 0 && data == 1) // hack
444 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
445 s->subband_num = data;
446 if (s->level >= DWT_LEVELS) {
447 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
448 ret = AVERROR(EINVAL);
451 if (s->subband_num > 3) {
452 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
453 ret = AVERROR(EINVAL);
456 } else if (tag == SubbandBand) {
457 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
458 s->subband_num_actual = data;
459 if (s->subband_num_actual >= 10) {
460 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
461 ret = AVERROR(EINVAL);
464 } else if (tag == LowpassPrecision)
465 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
466 else if (tag == Quantization) {
467 s->quantisation = data;
468 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
469 } else if (tag == PrescaleShift) {
470 s->prescale_shift[0] = (data >> 0) & 0x7;
471 s->prescale_shift[1] = (data >> 3) & 0x7;
472 s->prescale_shift[2] = (data >> 6) & 0x7;
473 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
474 } else if (tag == LowpassWidth) {
475 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
476 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
477 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
478 ret = AVERROR(EINVAL);
481 s->plane[s->channel_num].band[0][0].width = data;
482 s->plane[s->channel_num].band[0][0].stride = data;
483 } else if (tag == LowpassHeight) {
484 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
485 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
486 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
487 ret = AVERROR(EINVAL);
490 s->plane[s->channel_num].band[0][0].height = data;
491 } else if (tag == SampleType)
492 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
493 else if (tag == TransformType) {
495 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
496 ret = AVERROR_PATCHWELCOME;
499 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
500 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
501 if (abstag == 0x4001)
503 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
504 bytestream2_skipu(&gb, data * 4);
505 } else if (tag == 23) {
506 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
507 avpriv_report_missing_feature(avctx, "Skip frame");
508 ret = AVERROR_PATCHWELCOME;
510 } else if (tag == SampleIndexTable) {
511 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
512 if (data > bytestream2_get_bytes_left(&gb) / 4) {
513 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
514 ret = AVERROR_INVALIDDATA;
517 for (i = 0; i < data; i++) {
518 uint16_t tag2 = bytestream2_get_be16(&gb);
519 uint16_t val2 = bytestream2_get_be16(&gb);
520 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
522 } else if (tag == HighpassWidth) {
523 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);
525 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
526 ret = AVERROR(EINVAL);
529 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
530 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
531 } else if (tag == HighpassHeight) {
532 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
534 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
535 ret = AVERROR(EINVAL);
538 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
539 } else if (tag == BandWidth) {
540 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
542 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
543 ret = AVERROR(EINVAL);
546 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
547 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
548 } else if (tag == BandHeight) {
549 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
551 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
552 ret = AVERROR(EINVAL);
555 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
556 } else if (tag == InputFormat) {
557 av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
558 } else if (tag == BandCodingFlags) {
559 s->codebook = data & 0xf;
560 s->difference_coding = (data >> 4) & 1;
561 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
562 } else if (tag == Precision) {
563 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
564 if (!(data == 10 || data == 12)) {
565 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
566 ret = AVERROR(EINVAL);
569 avctx->bits_per_raw_sample = s->bpc = data;
570 } else if (tag == EncodedFormat) {
571 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
573 s->coded_format = AV_PIX_FMT_YUV422P10;
574 } else if (data == 2) {
575 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
576 } else if (data == 3) {
577 s->coded_format = AV_PIX_FMT_GBRP12;
578 } else if (data == 4) {
579 s->coded_format = AV_PIX_FMT_GBRAP12;
581 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
582 ret = AVERROR_PATCHWELCOME;
585 planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
586 } else if (tag == -85) {
587 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
588 s->cropped_height = data;
589 } else if (tag == -75) {
590 s->peak.offset &= ~0xffff;
591 s->peak.offset |= (data & 0xffff);
594 } else if (tag == -76) {
595 s->peak.offset &= 0xffff;
596 s->peak.offset |= (data & 0xffffU)<<16;
599 } else if (tag == -74 && s->peak.offset) {
600 s->peak.level = data;
601 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
603 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
605 /* Some kind of end of header tag */
606 if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
607 s->coded_format != AV_PIX_FMT_NONE) {
608 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
609 s->a_format != s->coded_format) {
611 if ((ret = alloc_buffers(avctx)) < 0) {
616 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
619 if (s->cropped_height) {
620 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
621 if (avctx->height < height)
622 return AVERROR_INVALIDDATA;
623 avctx->height = height;
628 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
633 s->coded_format = AV_PIX_FMT_NONE;
636 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
638 /* Lowpass coefficients */
639 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
640 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
641 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
642 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
643 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
646 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
647 ret = AVERROR(EINVAL);
651 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
652 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
653 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
654 ret = AVERROR(EINVAL);
658 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
659 for (i = 0; i < lowpass_height; i++) {
660 for (j = 0; j < lowpass_width; j++)
661 coeff_data[j] = bytestream2_get_be16u(&gb);
663 coeff_data += lowpass_width;
666 /* Align to mod-4 position to continue reading tags */
667 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
669 /* Copy last line of coefficients if odd height */
670 if (lowpass_height & 1) {
671 memcpy(&coeff_data[lowpass_height * lowpass_width],
672 &coeff_data[(lowpass_height - 1) * lowpass_width],
673 lowpass_width * sizeof(*coeff_data));
676 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
679 if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
680 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
681 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
682 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
683 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
684 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
686 int a_expected = highpass_a_height * highpass_a_width;
687 int level, run, coeff;
688 int count = 0, bytes;
691 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
692 ret = AVERROR(EINVAL);
696 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
697 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
698 ret = AVERROR(EINVAL);
701 expected = highpass_height * highpass_stride;
703 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);
705 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
707 OPEN_READER(re, &s->gb);
710 UPDATE_CACHE(re, &s->gb);
711 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
720 if (count > expected)
723 coeff = dequant_and_decompand(s, level, s->quantisation, 0);
724 for (i = 0; i < run; i++)
725 *coeff_data++ = coeff;
729 UPDATE_CACHE(re, &s->gb);
730 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
734 if (level == 255 && run == 2)
739 if (count > expected)
742 coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
743 for (i = 0; i < run; i++)
744 *coeff_data++ = coeff;
747 CLOSE_READER(re, &s->gb);
750 if (count > expected) {
751 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
752 ret = AVERROR(EINVAL);
756 peak_table(coeff_data - count, &s->peak, count);
757 if (s->difference_coding)
758 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
760 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
761 if (bytes > bytestream2_get_bytes_left(&gb)) {
762 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
763 ret = AVERROR(EINVAL);
766 bytestream2_seek(&gb, bytes, SEEK_CUR);
768 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
771 /* Copy last line of coefficients if odd height */
772 if (highpass_height & 1) {
773 memcpy(&coeff_data[highpass_height * highpass_stride],
774 &coeff_data[(highpass_height - 1) * highpass_stride],
775 highpass_stride * sizeof(*coeff_data));
780 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
781 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
782 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
783 ret = AVERROR(EINVAL);
788 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
789 ret = AVERROR(EINVAL);
793 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
794 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
796 return AVERROR_INVALIDDATA;
800 for (plane = 0; plane < planes && !ret; plane++) {
802 int lowpass_height = s->plane[plane].band[0][0].height;
803 int lowpass_width = s->plane[plane].band[0][0].width;
804 int highpass_stride = s->plane[plane].band[0][1].stride;
805 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
806 ptrdiff_t dst_linesize;
807 int16_t *low, *high, *output, *dst;
809 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
811 dst_linesize = pic->linesize[act_plane];
813 dst_linesize = pic->linesize[act_plane] / 2;
816 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
817 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
818 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
819 ret = AVERROR(EINVAL);
823 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
825 low = s->plane[plane].subband[0];
826 high = s->plane[plane].subband[2];
827 output = s->plane[plane].l_h[0];
828 for (i = 0; i < lowpass_width; i++) {
829 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
835 low = s->plane[plane].subband[1];
836 high = s->plane[plane].subband[3];
837 output = s->plane[plane].l_h[1];
839 for (i = 0; i < lowpass_width; i++) {
840 // note the stride of "low" is highpass_stride
841 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
847 low = s->plane[plane].l_h[0];
848 high = s->plane[plane].l_h[1];
849 output = s->plane[plane].subband[0];
850 for (i = 0; i < lowpass_height * 2; i++) {
851 horiz_filter(output, low, high, lowpass_width);
852 low += lowpass_width;
853 high += lowpass_width;
854 output += lowpass_width * 2;
857 output = s->plane[plane].subband[0];
858 for (i = 0; i < lowpass_height * 2; i++) {
859 for (j = 0; j < lowpass_width * 2; j++)
862 output += lowpass_width * 2;
867 lowpass_height = s->plane[plane].band[1][1].height;
868 lowpass_width = s->plane[plane].band[1][1].width;
869 highpass_stride = s->plane[plane].band[1][1].stride;
871 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
872 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
873 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
874 ret = AVERROR(EINVAL);
878 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
880 low = s->plane[plane].subband[0];
881 high = s->plane[plane].subband[5];
882 output = s->plane[plane].l_h[3];
883 for (i = 0; i < lowpass_width; i++) {
884 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
890 low = s->plane[plane].subband[4];
891 high = s->plane[plane].subband[6];
892 output = s->plane[plane].l_h[4];
893 for (i = 0; i < lowpass_width; i++) {
894 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
900 low = s->plane[plane].l_h[3];
901 high = s->plane[plane].l_h[4];
902 output = s->plane[plane].subband[0];
903 for (i = 0; i < lowpass_height * 2; i++) {
904 horiz_filter(output, low, high, lowpass_width);
905 low += lowpass_width;
906 high += lowpass_width;
907 output += lowpass_width * 2;
910 output = s->plane[plane].subband[0];
911 for (i = 0; i < lowpass_height * 2; i++) {
912 for (j = 0; j < lowpass_width * 2; j++)
915 output += lowpass_width * 2;
919 lowpass_height = s->plane[plane].band[2][1].height;
920 lowpass_width = s->plane[plane].band[2][1].width;
921 highpass_stride = s->plane[plane].band[2][1].stride;
923 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
924 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
925 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
926 ret = AVERROR(EINVAL);
930 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
931 if (s->progressive) {
932 low = s->plane[plane].subband[0];
933 high = s->plane[plane].subband[8];
934 output = s->plane[plane].l_h[6];
935 for (i = 0; i < lowpass_width; i++) {
936 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
942 low = s->plane[plane].subband[7];
943 high = s->plane[plane].subband[9];
944 output = s->plane[plane].l_h[7];
945 for (i = 0; i < lowpass_width; i++) {
946 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
952 dst = (int16_t *)pic->data[act_plane];
953 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
957 dst += pic->linesize[act_plane] >> 1;
959 low = s->plane[plane].l_h[6];
960 high = s->plane[plane].l_h[7];
962 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
963 (lowpass_height * 2 > avctx->coded_height / 2 ||
964 lowpass_width * 2 > avctx->coded_width / 2 )
966 ret = AVERROR_INVALIDDATA;
970 for (i = 0; i < lowpass_height * 2; i++) {
971 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
972 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
974 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
975 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
976 process_alpha(dst, lowpass_width * 2);
977 low += lowpass_width;
978 high += lowpass_width;
982 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
983 pic->interlaced_frame = 1;
984 low = s->plane[plane].subband[0];
985 high = s->plane[plane].subband[7];
986 output = s->plane[plane].l_h[6];
987 for (i = 0; i < lowpass_height; i++) {
988 horiz_filter(output, low, high, lowpass_width);
989 low += lowpass_width;
990 high += lowpass_width;
991 output += lowpass_width * 2;
994 low = s->plane[plane].subband[8];
995 high = s->plane[plane].subband[9];
996 output = s->plane[plane].l_h[7];
997 for (i = 0; i < lowpass_height; i++) {
998 horiz_filter(output, low, high, lowpass_width);
999 low += lowpass_width;
1000 high += lowpass_width;
1001 output += lowpass_width * 2;
1004 dst = (int16_t *)pic->data[act_plane];
1005 low = s->plane[plane].l_h[6];
1006 high = s->plane[plane].l_h[7];
1007 for (i = 0; i < lowpass_height; i++) {
1008 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1009 low += lowpass_width * 2;
1010 high += lowpass_width * 2;
1011 dst += pic->linesize[act_plane];
1017 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1027 static av_cold int cfhd_close(AVCodecContext *avctx)
1029 CFHDContext *s = avctx->priv_data;
1033 ff_free_vlc(&s->vlc_9);
1034 ff_free_vlc(&s->vlc_18);
1039 AVCodec ff_cfhd_decoder = {
1041 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1042 .type = AVMEDIA_TYPE_VIDEO,
1043 .id = AV_CODEC_ID_CFHD,
1044 .priv_data_size = sizeof(CFHDContext),
1046 .close = cfhd_close,
1047 .decode = cfhd_decode,
1048 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1049 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,