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 for (i = 0; i < len; i++) {
216 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
217 output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
219 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
221 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
222 output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
224 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
225 } else if (i == len-1) {
226 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
227 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
229 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
231 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
232 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
234 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
236 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
237 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + 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 = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
242 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
244 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
249 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
250 int width, int linesize, int plane)
254 for (i = 0; i < width; i++) {
255 even = (low[i] - high[i])/2;
256 odd = (low[i] + high[i])/2;
257 output[i] = av_clip_uintp2(even, 10);
258 output[i + linesize] = av_clip_uintp2(odd, 10);
261 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
264 filter(output, 1, low, 1, high, 1, width, 0);
267 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
270 filter(output, 1, low, 1, high, 1, width, clip);
273 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
276 filter(output, 2, low, 1, high, 1, width, clip);
279 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
280 int16_t *low, ptrdiff_t low_stride,
281 int16_t *high, ptrdiff_t high_stride, int len)
283 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
286 static void free_buffers(CFHDContext *s)
290 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
291 av_freep(&s->plane[i].idwt_buf);
292 av_freep(&s->plane[i].idwt_tmp);
294 for (j = 0; j < 9; j++)
295 s->plane[i].subband[j] = NULL;
297 for (j = 0; j < 8; j++)
298 s->plane[i].l_h[j] = NULL;
304 static int alloc_buffers(AVCodecContext *avctx)
306 CFHDContext *s = avctx->priv_data;
307 int i, j, ret, planes;
308 int chroma_x_shift, chroma_y_shift;
311 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
313 s->coded_height *= 2;
316 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
318 avctx->pix_fmt = s->coded_format;
320 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
322 &chroma_y_shift)) < 0)
324 planes = av_pix_fmt_count_planes(s->coded_format);
325 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
331 for (i = 0; i < planes; i++) {
332 int w8, h8, w4, h4, w2, h2;
333 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
334 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
335 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
337 height = FFALIGN(height / 8, 2) * 8;
338 s->plane[i].width = width;
339 s->plane[i].height = height;
340 s->plane[i].stride = stride;
342 w8 = FFALIGN(s->plane[i].width / 8, 8);
349 s->plane[i].idwt_buf =
350 av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
351 s->plane[i].idwt_tmp =
352 av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
353 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
354 return AVERROR(ENOMEM);
356 s->plane[i].subband[0] = s->plane[i].idwt_buf;
357 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
358 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
359 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
360 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
361 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
362 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
363 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
364 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
365 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
367 for (j = 0; j < DWT_LEVELS; j++) {
368 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
369 s->plane[i].band[j][k].a_width = w8 << j;
370 s->plane[i].band[j][k].a_height = h8 << j;
374 /* ll2 and ll1 commented out because they are done in-place */
375 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
376 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
377 // s->plane[i].l_h[2] = ll2;
378 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
379 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
380 // s->plane[i].l_h[5] = ll1;
381 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
382 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
385 s->a_height = s->coded_height;
386 s->a_width = s->coded_width;
387 s->a_format = s->coded_format;
392 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
395 CFHDContext *s = avctx->priv_data;
397 ThreadFrame frame = { .f = data };
399 int ret = 0, i, j, planes, plane, got_buffer = 0;
402 s->coded_format = AV_PIX_FMT_YUV422P10;
403 init_frame_defaults(s);
404 planes = av_pix_fmt_count_planes(s->coded_format);
406 bytestream2_init(&gb, avpkt->data, avpkt->size);
408 while (bytestream2_get_bytes_left(&gb) > 4) {
409 /* Bit weird but implement the tag parsing as the spec says */
410 uint16_t tagu = bytestream2_get_be16(&gb);
411 int16_t tag = (int16_t)tagu;
412 int8_t tag8 = (int8_t)(tagu >> 8);
413 uint16_t abstag = abs(tag);
414 int8_t abs_tag8 = abs(tag8);
415 uint16_t data = bytestream2_get_be16(&gb);
416 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
417 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
418 } else if (tag == SampleFlags) {
419 av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
420 s->progressive = data & 0x0001;
421 } else if (tag == ImageWidth) {
422 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
423 s->coded_width = data;
424 } else if (tag == ImageHeight) {
425 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
426 s->coded_height = data;
427 } else if (tag == 101) {
428 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
429 if (data < 1 || data > 31) {
430 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
431 ret = AVERROR(EINVAL);
435 } else if (tag == ChannelCount) {
436 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
437 s->channel_cnt = data;
439 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
440 ret = AVERROR_PATCHWELCOME;
443 } else if (tag == SubbandCount) {
444 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
445 if (data != SUBBAND_COUNT) {
446 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
447 ret = AVERROR_PATCHWELCOME;
450 } else if (tag == ChannelNumber) {
451 s->channel_num = data;
452 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
453 if (s->channel_num >= planes) {
454 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
455 ret = AVERROR(EINVAL);
458 init_plane_defaults(s);
459 } else if (tag == SubbandNumber) {
460 if (s->subband_num != 0 && data == 1) // hack
462 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
463 s->subband_num = data;
464 if (s->level >= DWT_LEVELS) {
465 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
466 ret = AVERROR(EINVAL);
469 if (s->subband_num > 3) {
470 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
471 ret = AVERROR(EINVAL);
474 } else if (tag == 51) {
475 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
476 s->subband_num_actual = data;
477 if (s->subband_num_actual >= 10) {
478 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
479 ret = AVERROR(EINVAL);
482 } else if (tag == LowpassPrecision)
483 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
484 else if (tag == Quantization) {
485 s->quantisation = data;
486 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
487 } else if (tag == PrescaleShift) {
488 s->prescale_shift[0] = (data >> 0) & 0x7;
489 s->prescale_shift[1] = (data >> 3) & 0x7;
490 s->prescale_shift[2] = (data >> 6) & 0x7;
491 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
492 } else if (tag == LowpassWidth) {
493 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
494 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
495 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
496 ret = AVERROR(EINVAL);
499 s->plane[s->channel_num].band[0][0].width = data;
500 s->plane[s->channel_num].band[0][0].stride = data;
501 } else if (tag == LowpassHeight) {
502 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
503 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
504 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
505 ret = AVERROR(EINVAL);
508 s->plane[s->channel_num].band[0][0].height = data;
509 } else if (tag == SampleType)
510 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
511 else if (tag == TransformType) {
513 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
514 ret = AVERROR_PATCHWELCOME;
517 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
518 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
519 if (abstag == 0x4001)
521 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
522 bytestream2_skipu(&gb, data * 4);
523 } else if (tag == 23) {
524 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
525 avpriv_report_missing_feature(avctx, "Skip frame");
526 ret = AVERROR_PATCHWELCOME;
528 } else if (tag == SampleIndexTable) {
529 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
530 if (data > bytestream2_get_bytes_left(&gb) / 4) {
531 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
532 ret = AVERROR_INVALIDDATA;
535 for (i = 0; i < data; i++) {
536 uint16_t tag2 = bytestream2_get_be16(&gb);
537 uint16_t val2 = bytestream2_get_be16(&gb);
538 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
540 } else if (tag == HighpassWidth) {
541 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);
543 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
544 ret = AVERROR(EINVAL);
547 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
548 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
549 } else if (tag == HighpassHeight) {
550 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
552 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
553 ret = AVERROR(EINVAL);
556 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
557 } else if (tag == 49) {
558 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
560 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
561 ret = AVERROR(EINVAL);
564 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
565 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
566 } else if (tag == 50) {
567 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
569 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
570 ret = AVERROR(EINVAL);
573 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
574 } else if (tag == 71) {
576 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
577 } else if (tag == 72) {
578 s->codebook = data & 0xf;
579 s->difference_coding = (data >> 4) & 1;
580 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
581 } else if (tag == 70) {
582 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
583 if (!(data == 10 || data == 12)) {
584 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
585 ret = AVERROR(EINVAL);
589 } else if (tag == EncodedFormat) {
590 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
592 s->coded_format = AV_PIX_FMT_YUV422P10;
593 } else if (data == 2) {
594 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
595 } else if (data == 3) {
596 s->coded_format = AV_PIX_FMT_GBRP12;
597 } else if (data == 4) {
598 s->coded_format = AV_PIX_FMT_GBRAP12;
600 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
601 ret = AVERROR_PATCHWELCOME;
604 planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
605 } else if (tag == -85) {
606 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
607 s->cropped_height = data;
608 } else if (tag == -75) {
609 s->peak.offset &= ~0xffff;
610 s->peak.offset |= (data & 0xffff);
613 } else if (tag == -76) {
614 s->peak.offset &= 0xffff;
615 s->peak.offset |= (data & 0xffffU)<<16;
618 } else if (tag == -74 && s->peak.offset) {
619 s->peak.level = data;
620 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
622 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
624 /* Some kind of end of header tag */
625 if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
626 s->coded_format != AV_PIX_FMT_NONE) {
627 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
628 s->a_format != s->coded_format) {
630 if ((ret = alloc_buffers(avctx)) < 0) {
635 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
638 if (s->cropped_height) {
639 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
640 if (avctx->height < height)
641 return AVERROR_INVALIDDATA;
642 avctx->height = height;
647 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
652 s->coded_format = AV_PIX_FMT_NONE;
655 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
657 /* Lowpass coefficients */
658 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
659 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
660 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
661 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
662 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
665 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
666 ret = AVERROR(EINVAL);
670 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
671 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
672 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
673 ret = AVERROR(EINVAL);
677 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
678 for (i = 0; i < lowpass_height; i++) {
679 for (j = 0; j < lowpass_width; j++)
680 coeff_data[j] = bytestream2_get_be16u(&gb);
682 coeff_data += lowpass_width;
685 /* Align to mod-4 position to continue reading tags */
686 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
688 /* Copy last line of coefficients if odd height */
689 if (lowpass_height & 1) {
690 memcpy(&coeff_data[lowpass_height * lowpass_width],
691 &coeff_data[(lowpass_height - 1) * lowpass_width],
692 lowpass_width * sizeof(*coeff_data));
695 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
698 if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
699 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
700 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
701 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
702 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
703 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
705 int a_expected = highpass_a_height * highpass_a_width;
706 int level, run, coeff;
707 int count = 0, bytes;
710 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
711 ret = AVERROR(EINVAL);
715 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
716 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
717 ret = AVERROR(EINVAL);
720 expected = highpass_height * highpass_stride;
722 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);
724 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
726 OPEN_READER(re, &s->gb);
729 UPDATE_CACHE(re, &s->gb);
730 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
739 if (count > expected)
742 coeff = dequant_and_decompand(level, s->quantisation, 0);
743 for (i = 0; i < run; i++)
744 *coeff_data++ = coeff;
748 UPDATE_CACHE(re, &s->gb);
749 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
753 if (level == 255 && run == 2)
758 if (count > expected)
761 coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
762 for (i = 0; i < run; i++)
763 *coeff_data++ = coeff;
766 CLOSE_READER(re, &s->gb);
769 if (count > expected) {
770 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
771 ret = AVERROR(EINVAL);
775 peak_table(coeff_data - count, &s->peak, count);
776 if (s->difference_coding)
777 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
779 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
780 if (bytes > bytestream2_get_bytes_left(&gb)) {
781 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
782 ret = AVERROR(EINVAL);
785 bytestream2_seek(&gb, bytes, SEEK_CUR);
787 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
790 /* Copy last line of coefficients if odd height */
791 if (highpass_height & 1) {
792 memcpy(&coeff_data[highpass_height * highpass_stride],
793 &coeff_data[(highpass_height - 1) * highpass_stride],
794 highpass_stride * sizeof(*coeff_data));
799 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
800 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
801 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
802 ret = AVERROR(EINVAL);
807 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
808 ret = AVERROR(EINVAL);
812 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
813 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
815 return AVERROR_INVALIDDATA;
819 for (plane = 0; plane < planes && !ret; plane++) {
821 int lowpass_height = s->plane[plane].band[0][0].height;
822 int lowpass_width = s->plane[plane].band[0][0].width;
823 int highpass_stride = s->plane[plane].band[0][1].stride;
824 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
825 ptrdiff_t dst_linesize;
826 int16_t *low, *high, *output, *dst;
828 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
830 dst_linesize = pic->linesize[act_plane];
832 dst_linesize = pic->linesize[act_plane] / 2;
835 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
836 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
837 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
838 ret = AVERROR(EINVAL);
842 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
844 low = s->plane[plane].subband[0];
845 high = s->plane[plane].subband[2];
846 output = s->plane[plane].l_h[0];
847 for (i = 0; i < lowpass_width; i++) {
848 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
854 low = s->plane[plane].subband[1];
855 high = s->plane[plane].subband[3];
856 output = s->plane[plane].l_h[1];
858 for (i = 0; i < lowpass_width; i++) {
859 // note the stride of "low" is highpass_stride
860 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
866 low = s->plane[plane].l_h[0];
867 high = s->plane[plane].l_h[1];
868 output = s->plane[plane].subband[0];
869 for (i = 0; i < lowpass_height * 2; i++) {
870 horiz_filter(output, low, high, lowpass_width);
871 low += lowpass_width;
872 high += lowpass_width;
873 output += lowpass_width * 2;
876 output = s->plane[plane].subband[0];
877 for (i = 0; i < lowpass_height * 2; i++) {
878 for (j = 0; j < lowpass_width * 2; j++)
881 output += lowpass_width * 2;
886 lowpass_height = s->plane[plane].band[1][1].height;
887 lowpass_width = s->plane[plane].band[1][1].width;
888 highpass_stride = s->plane[plane].band[1][1].stride;
890 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
891 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
892 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
893 ret = AVERROR(EINVAL);
897 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
899 low = s->plane[plane].subband[0];
900 high = s->plane[plane].subband[5];
901 output = s->plane[plane].l_h[3];
902 for (i = 0; i < lowpass_width; i++) {
903 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
909 low = s->plane[plane].subband[4];
910 high = s->plane[plane].subband[6];
911 output = s->plane[plane].l_h[4];
912 for (i = 0; i < lowpass_width; i++) {
913 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
919 low = s->plane[plane].l_h[3];
920 high = s->plane[plane].l_h[4];
921 output = s->plane[plane].subband[0];
922 for (i = 0; i < lowpass_height * 2; i++) {
923 horiz_filter(output, low, high, lowpass_width);
924 low += lowpass_width;
925 high += lowpass_width;
926 output += lowpass_width * 2;
929 output = s->plane[plane].subband[0];
930 for (i = 0; i < lowpass_height * 2; i++) {
931 for (j = 0; j < lowpass_width * 2; j++)
934 output += lowpass_width * 2;
938 lowpass_height = s->plane[plane].band[2][1].height;
939 lowpass_width = s->plane[plane].band[2][1].width;
940 highpass_stride = s->plane[plane].band[2][1].stride;
942 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
943 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
944 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
945 ret = AVERROR(EINVAL);
949 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
950 if (s->progressive) {
951 low = s->plane[plane].subband[0];
952 high = s->plane[plane].subband[8];
953 output = s->plane[plane].l_h[6];
954 for (i = 0; i < lowpass_width; i++) {
955 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
961 low = s->plane[plane].subband[7];
962 high = s->plane[plane].subband[9];
963 output = s->plane[plane].l_h[7];
964 for (i = 0; i < lowpass_width; i++) {
965 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
971 dst = (int16_t *)pic->data[act_plane];
972 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
976 dst += pic->linesize[act_plane] >> 1;
978 low = s->plane[plane].l_h[6];
979 high = s->plane[plane].l_h[7];
981 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
982 (lowpass_height * 2 > avctx->coded_height / 2 ||
983 lowpass_width * 2 > avctx->coded_width / 2 )
985 ret = AVERROR_INVALIDDATA;
989 for (i = 0; i < lowpass_height * 2; i++) {
990 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
991 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
993 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
994 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
995 process_alpha(dst, lowpass_width * 2);
996 low += lowpass_width;
997 high += lowpass_width;
1001 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
1002 pic->interlaced_frame = 1;
1003 low = s->plane[plane].subband[0];
1004 high = s->plane[plane].subband[7];
1005 output = s->plane[plane].l_h[6];
1006 for (i = 0; i < lowpass_height; i++) {
1007 horiz_filter(output, low, high, lowpass_width);
1008 low += lowpass_width;
1009 high += lowpass_width;
1010 output += lowpass_width * 2;
1013 low = s->plane[plane].subband[8];
1014 high = s->plane[plane].subband[9];
1015 output = s->plane[plane].l_h[7];
1016 for (i = 0; i < lowpass_height; i++) {
1017 horiz_filter(output, low, high, lowpass_width);
1018 low += lowpass_width;
1019 high += lowpass_width;
1020 output += lowpass_width * 2;
1023 dst = (int16_t *)pic->data[act_plane];
1024 low = s->plane[plane].l_h[6];
1025 high = s->plane[plane].l_h[7];
1026 for (i = 0; i < lowpass_height; i++) {
1027 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
1028 low += lowpass_width * 2;
1029 high += lowpass_width * 2;
1030 dst += pic->linesize[act_plane];
1036 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1046 static av_cold int cfhd_close(AVCodecContext *avctx)
1048 CFHDContext *s = avctx->priv_data;
1052 ff_free_vlc(&s->vlc_9);
1053 ff_free_vlc(&s->vlc_18);
1058 AVCodec ff_cfhd_decoder = {
1060 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1061 .type = AVMEDIA_TYPE_VIDEO,
1062 .id = AV_CODEC_ID_CFHD,
1063 .priv_data_size = sizeof(CFHDContext),
1065 .close = cfhd_close,
1066 .decode = cfhd_decode,
1067 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1068 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,