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 return ff_cfhd_init_vlcs(s);
52 static void init_plane_defaults(CFHDContext *s)
56 s->subband_num_actual = 0;
59 static void init_peak_table_defaults(CFHDContext *s)
63 memset(&s->peak.base, 0, sizeof(s->peak.base));
66 static void init_frame_defaults(CFHDContext *s)
70 s->cropped_height = 0;
73 s->subband_cnt = SUBBAND_COUNT;
75 s->lowpass_precision = 16;
80 s->difference_coding = 0;
82 init_plane_defaults(s);
83 init_peak_table_defaults(s);
86 /* TODO: merge with VLC tables or use LUT */
87 static inline int dequant_and_decompand(int level, int quantisation, int codebook)
89 if (codebook == 0 || codebook == 1) {
90 int64_t abslevel = abs(level);
92 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (256 * 256 * 256))) *
93 FFSIGN(level) * quantisation;
95 return level * quantisation;
97 return level * quantisation;
100 static inline void difference_coding(int16_t *band, int width, int height)
104 for (i = 0; i < height; i++) {
105 for (j = 1; j < width; j++) {
106 band[j] += band[j-1];
112 static inline void peak_table(int16_t *band, Peak *peak, int length)
115 for (i = 0; i < length; i++)
116 if (abs(band[i]) > peak->level)
117 band[i] = bytestream2_get_le16(&peak->base);
120 static inline void process_alpha(int16_t *alpha, int width)
123 for (i = 0; i < width; i++) {
125 channel -= ALPHA_COMPAND_DC_OFFSET;
127 channel *= ALPHA_COMPAND_GAIN;
129 channel = av_clip_uintp2(channel, 12);
134 static inline void process_bayer(AVFrame *frame)
136 const int linesize = frame->linesize[0];
137 uint16_t *r = (uint16_t *)frame->data[0];
138 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
139 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
140 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
141 const int mid = 2048;
143 for (int y = 0; y < frame->height >> 1; y++) {
144 for (int x = 0; x < frame->width; x += 2) {
154 R = (rg - mid) * 2 + g;
157 B = (bg - mid) * 2 + g;
159 R = av_clip_uintp2(R * 16, 16);
160 G1 = av_clip_uintp2(G1 * 16, 16);
161 G2 = av_clip_uintp2(G2 * 16, 16);
162 B = av_clip_uintp2(B * 16, 16);
177 static inline void filter(int16_t *output, ptrdiff_t out_stride,
178 int16_t *low, ptrdiff_t low_stride,
179 int16_t *high, ptrdiff_t high_stride,
185 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
186 output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
188 output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
190 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
191 output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
193 output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
195 for (i = 1; i < len - 1; i++) {
196 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
197 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
199 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
201 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
202 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
204 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
207 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
208 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
210 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
212 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
213 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
215 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
218 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
219 int width, int linesize, int plane)
223 for (i = 0; i < width; i++) {
224 even = (low[i] - high[i])/2;
225 odd = (low[i] + high[i])/2;
226 output[i] = av_clip_uintp2(even, 10);
227 output[i + linesize] = av_clip_uintp2(odd, 10);
230 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
233 filter(output, 1, low, 1, high, 1, width, 0);
236 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
239 filter(output, 1, low, 1, high, 1, width, clip);
242 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
245 filter(output, 2, low, 1, high, 1, width, clip);
248 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
249 int16_t *low, ptrdiff_t low_stride,
250 int16_t *high, ptrdiff_t high_stride, int len)
252 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
255 static void free_buffers(CFHDContext *s)
259 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
260 av_freep(&s->plane[i].idwt_buf);
261 av_freep(&s->plane[i].idwt_tmp);
263 for (j = 0; j < 9; j++)
264 s->plane[i].subband[j] = NULL;
266 for (j = 0; j < 8; j++)
267 s->plane[i].l_h[j] = NULL;
273 static int alloc_buffers(AVCodecContext *avctx)
275 CFHDContext *s = avctx->priv_data;
276 int i, j, ret, planes;
277 int chroma_x_shift, chroma_y_shift;
280 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
282 s->coded_height *= 2;
285 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
287 avctx->pix_fmt = s->coded_format;
289 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
291 &chroma_y_shift)) < 0)
293 planes = av_pix_fmt_count_planes(s->coded_format);
294 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
300 for (i = 0; i < planes; i++) {
301 int w8, h8, w4, h4, w2, h2;
302 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
303 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
304 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
306 height = FFALIGN(height / 8, 2) * 8;
307 s->plane[i].width = width;
308 s->plane[i].height = height;
309 s->plane[i].stride = stride;
311 w8 = FFALIGN(s->plane[i].width / 8, 8);
312 h8 = FFALIGN(height, 8) / 8;
318 s->plane[i].idwt_buf =
319 av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
320 s->plane[i].idwt_tmp =
321 av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
322 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
323 return AVERROR(ENOMEM);
325 s->plane[i].subband[0] = s->plane[i].idwt_buf;
326 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
327 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
328 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
329 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
330 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
331 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
332 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
333 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
334 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
336 for (j = 0; j < DWT_LEVELS; j++) {
337 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
338 s->plane[i].band[j][k].a_width = w8 << j;
339 s->plane[i].band[j][k].a_height = h8 << j;
343 /* ll2 and ll1 commented out because they are done in-place */
344 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
345 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
346 // s->plane[i].l_h[2] = ll2;
347 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
348 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
349 // s->plane[i].l_h[5] = ll1;
350 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
351 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
354 s->a_height = s->coded_height;
355 s->a_width = s->coded_width;
356 s->a_format = s->coded_format;
361 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
364 CFHDContext *s = avctx->priv_data;
366 ThreadFrame frame = { .f = data };
368 int ret = 0, i, j, planes, plane, got_buffer = 0;
371 s->coded_format = AV_PIX_FMT_YUV422P10;
372 init_frame_defaults(s);
373 planes = av_pix_fmt_count_planes(s->coded_format);
375 bytestream2_init(&gb, avpkt->data, avpkt->size);
377 while (bytestream2_get_bytes_left(&gb) > 4) {
378 /* Bit weird but implement the tag parsing as the spec says */
379 uint16_t tagu = bytestream2_get_be16(&gb);
380 int16_t tag = (int16_t)tagu;
381 int8_t tag8 = (int8_t)(tagu >> 8);
382 uint16_t abstag = abs(tag);
383 int8_t abs_tag8 = abs(tag8);
384 uint16_t data = bytestream2_get_be16(&gb);
385 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
386 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
387 } else if (tag == SampleFlags) {
388 av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
389 s->progressive = data & 0x0001;
390 } else if (tag == ImageWidth) {
391 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
392 s->coded_width = data;
393 } else if (tag == ImageHeight) {
394 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
395 s->coded_height = data;
396 } else if (tag == BitsPerComponent) {
397 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
398 if (data < 1 || data > 31) {
399 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
400 ret = AVERROR(EINVAL);
404 } else if (tag == ChannelCount) {
405 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
406 s->channel_cnt = data;
408 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
409 ret = AVERROR_PATCHWELCOME;
412 } else if (tag == SubbandCount) {
413 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
414 if (data != SUBBAND_COUNT) {
415 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
416 ret = AVERROR_PATCHWELCOME;
419 } else if (tag == ChannelNumber) {
420 s->channel_num = data;
421 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
422 if (s->channel_num >= planes) {
423 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
424 ret = AVERROR(EINVAL);
427 init_plane_defaults(s);
428 } else if (tag == SubbandNumber) {
429 if (s->subband_num != 0 && data == 1) // hack
431 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
432 s->subband_num = data;
433 if (s->level >= DWT_LEVELS) {
434 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
435 ret = AVERROR(EINVAL);
438 if (s->subband_num > 3) {
439 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
440 ret = AVERROR(EINVAL);
443 } else if (tag == SubbandBand) {
444 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
445 s->subband_num_actual = data;
446 if (s->subband_num_actual >= 10) {
447 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
448 ret = AVERROR(EINVAL);
451 } else if (tag == LowpassPrecision)
452 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
453 else if (tag == Quantization) {
454 s->quantisation = data;
455 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
456 } else if (tag == PrescaleShift) {
457 s->prescale_shift[0] = (data >> 0) & 0x7;
458 s->prescale_shift[1] = (data >> 3) & 0x7;
459 s->prescale_shift[2] = (data >> 6) & 0x7;
460 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
461 } else if (tag == LowpassWidth) {
462 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
463 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
464 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
465 ret = AVERROR(EINVAL);
468 s->plane[s->channel_num].band[0][0].width = data;
469 s->plane[s->channel_num].band[0][0].stride = data;
470 } else if (tag == LowpassHeight) {
471 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
472 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
473 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
474 ret = AVERROR(EINVAL);
477 s->plane[s->channel_num].band[0][0].height = data;
478 } else if (tag == SampleType)
479 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
480 else if (tag == TransformType) {
482 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
483 ret = AVERROR_PATCHWELCOME;
486 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
487 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
488 if (abstag == 0x4001)
490 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
491 bytestream2_skipu(&gb, data * 4);
492 } else if (tag == 23) {
493 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
494 avpriv_report_missing_feature(avctx, "Skip frame");
495 ret = AVERROR_PATCHWELCOME;
497 } else if (tag == SampleIndexTable) {
498 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
499 if (data > bytestream2_get_bytes_left(&gb) / 4) {
500 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
501 ret = AVERROR_INVALIDDATA;
504 for (i = 0; i < data; i++) {
505 uint16_t tag2 = bytestream2_get_be16(&gb);
506 uint16_t val2 = bytestream2_get_be16(&gb);
507 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
509 } else if (tag == HighpassWidth) {
510 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);
512 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
513 ret = AVERROR(EINVAL);
516 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
517 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
518 } else if (tag == HighpassHeight) {
519 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
521 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
522 ret = AVERROR(EINVAL);
525 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
526 } else if (tag == BandWidth) {
527 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
529 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
530 ret = AVERROR(EINVAL);
533 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
534 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
535 } else if (tag == BandHeight) {
536 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
538 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
539 ret = AVERROR(EINVAL);
542 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
543 } else if (tag == 71) {
545 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
546 } else if (tag == BandCodingFlags) {
547 s->codebook = data & 0xf;
548 s->difference_coding = (data >> 4) & 1;
549 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
550 } else if (tag == Precision) {
551 av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
552 if (!(data == 10 || data == 12)) {
553 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
554 ret = AVERROR(EINVAL);
557 avctx->bits_per_raw_sample = s->bpc = data;
558 } else if (tag == EncodedFormat) {
559 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
561 s->coded_format = AV_PIX_FMT_YUV422P10;
562 } else if (data == 2) {
563 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
564 } else if (data == 3) {
565 s->coded_format = AV_PIX_FMT_GBRP12;
566 } else if (data == 4) {
567 s->coded_format = AV_PIX_FMT_GBRAP12;
569 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
570 ret = AVERROR_PATCHWELCOME;
573 planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
574 } else if (tag == -85) {
575 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
576 s->cropped_height = data;
577 } else if (tag == -75) {
578 s->peak.offset &= ~0xffff;
579 s->peak.offset |= (data & 0xffff);
582 } else if (tag == -76) {
583 s->peak.offset &= 0xffff;
584 s->peak.offset |= (data & 0xffffU)<<16;
587 } else if (tag == -74 && s->peak.offset) {
588 s->peak.level = data;
589 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
591 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
593 /* Some kind of end of header tag */
594 if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
595 s->coded_format != AV_PIX_FMT_NONE) {
596 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
597 s->a_format != s->coded_format) {
599 if ((ret = alloc_buffers(avctx)) < 0) {
604 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
607 if (s->cropped_height) {
608 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
609 if (avctx->height < height)
610 return AVERROR_INVALIDDATA;
611 avctx->height = height;
616 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
621 s->coded_format = AV_PIX_FMT_NONE;
624 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
626 /* Lowpass coefficients */
627 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
628 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
629 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
630 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
631 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
634 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
635 ret = AVERROR(EINVAL);
639 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
640 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
641 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
642 ret = AVERROR(EINVAL);
646 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
647 for (i = 0; i < lowpass_height; i++) {
648 for (j = 0; j < lowpass_width; j++)
649 coeff_data[j] = bytestream2_get_be16u(&gb);
651 coeff_data += lowpass_width;
654 /* Align to mod-4 position to continue reading tags */
655 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
657 /* Copy last line of coefficients if odd height */
658 if (lowpass_height & 1) {
659 memcpy(&coeff_data[lowpass_height * lowpass_width],
660 &coeff_data[(lowpass_height - 1) * lowpass_width],
661 lowpass_width * sizeof(*coeff_data));
664 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
667 if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
668 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
669 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
670 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
671 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
672 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
674 int a_expected = highpass_a_height * highpass_a_width;
675 int level, run, coeff;
676 int count = 0, bytes;
679 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
680 ret = AVERROR(EINVAL);
684 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
685 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
686 ret = AVERROR(EINVAL);
689 expected = highpass_height * highpass_stride;
691 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);
693 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
695 OPEN_READER(re, &s->gb);
698 UPDATE_CACHE(re, &s->gb);
699 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
708 if (count > expected)
711 coeff = dequant_and_decompand(level, s->quantisation, 0);
712 for (i = 0; i < run; i++)
713 *coeff_data++ = coeff;
717 UPDATE_CACHE(re, &s->gb);
718 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
722 if (level == 255 && run == 2)
727 if (count > expected)
730 coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
731 for (i = 0; i < run; i++)
732 *coeff_data++ = coeff;
735 CLOSE_READER(re, &s->gb);
738 if (count > expected) {
739 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
740 ret = AVERROR(EINVAL);
744 peak_table(coeff_data - count, &s->peak, count);
745 if (s->difference_coding)
746 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
748 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
749 if (bytes > bytestream2_get_bytes_left(&gb)) {
750 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
751 ret = AVERROR(EINVAL);
754 bytestream2_seek(&gb, bytes, SEEK_CUR);
756 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
759 /* Copy last line of coefficients if odd height */
760 if (highpass_height & 1) {
761 memcpy(&coeff_data[highpass_height * highpass_stride],
762 &coeff_data[(highpass_height - 1) * highpass_stride],
763 highpass_stride * sizeof(*coeff_data));
768 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
769 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
770 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
771 ret = AVERROR(EINVAL);
776 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
777 ret = AVERROR(EINVAL);
781 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
782 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
784 return AVERROR_INVALIDDATA;
788 for (plane = 0; plane < planes && !ret; plane++) {
790 int lowpass_height = s->plane[plane].band[0][0].height;
791 int lowpass_width = s->plane[plane].band[0][0].width;
792 int highpass_stride = s->plane[plane].band[0][1].stride;
793 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
794 ptrdiff_t dst_linesize;
795 int16_t *low, *high, *output, *dst;
797 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
799 dst_linesize = pic->linesize[act_plane];
801 dst_linesize = pic->linesize[act_plane] / 2;
804 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
805 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
806 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
807 ret = AVERROR(EINVAL);
811 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
813 low = s->plane[plane].subband[0];
814 high = s->plane[plane].subband[2];
815 output = s->plane[plane].l_h[0];
816 for (i = 0; i < lowpass_width; i++) {
817 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
823 low = s->plane[plane].subband[1];
824 high = s->plane[plane].subband[3];
825 output = s->plane[plane].l_h[1];
827 for (i = 0; i < lowpass_width; i++) {
828 // note the stride of "low" is highpass_stride
829 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
835 low = s->plane[plane].l_h[0];
836 high = s->plane[plane].l_h[1];
837 output = s->plane[plane].subband[0];
838 for (i = 0; i < lowpass_height * 2; i++) {
839 horiz_filter(output, low, high, lowpass_width);
840 low += lowpass_width;
841 high += lowpass_width;
842 output += lowpass_width * 2;
845 output = s->plane[plane].subband[0];
846 for (i = 0; i < lowpass_height * 2; i++) {
847 for (j = 0; j < lowpass_width * 2; j++)
850 output += lowpass_width * 2;
855 lowpass_height = s->plane[plane].band[1][1].height;
856 lowpass_width = s->plane[plane].band[1][1].width;
857 highpass_stride = s->plane[plane].band[1][1].stride;
859 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
860 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
861 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
862 ret = AVERROR(EINVAL);
866 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
868 low = s->plane[plane].subband[0];
869 high = s->plane[plane].subband[5];
870 output = s->plane[plane].l_h[3];
871 for (i = 0; i < lowpass_width; i++) {
872 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
878 low = s->plane[plane].subband[4];
879 high = s->plane[plane].subband[6];
880 output = s->plane[plane].l_h[4];
881 for (i = 0; i < lowpass_width; i++) {
882 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
888 low = s->plane[plane].l_h[3];
889 high = s->plane[plane].l_h[4];
890 output = s->plane[plane].subband[0];
891 for (i = 0; i < lowpass_height * 2; i++) {
892 horiz_filter(output, low, high, lowpass_width);
893 low += lowpass_width;
894 high += lowpass_width;
895 output += lowpass_width * 2;
898 output = s->plane[plane].subband[0];
899 for (i = 0; i < lowpass_height * 2; i++) {
900 for (j = 0; j < lowpass_width * 2; j++)
903 output += lowpass_width * 2;
907 lowpass_height = s->plane[plane].band[2][1].height;
908 lowpass_width = s->plane[plane].band[2][1].width;
909 highpass_stride = s->plane[plane].band[2][1].stride;
911 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
912 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
913 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
914 ret = AVERROR(EINVAL);
918 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
919 if (s->progressive) {
920 low = s->plane[plane].subband[0];
921 high = s->plane[plane].subband[8];
922 output = s->plane[plane].l_h[6];
923 for (i = 0; i < lowpass_width; i++) {
924 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
930 low = s->plane[plane].subband[7];
931 high = s->plane[plane].subband[9];
932 output = s->plane[plane].l_h[7];
933 for (i = 0; i < lowpass_width; i++) {
934 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
940 dst = (int16_t *)pic->data[act_plane];
941 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
945 dst += pic->linesize[act_plane] >> 1;
947 low = s->plane[plane].l_h[6];
948 high = s->plane[plane].l_h[7];
950 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
951 (lowpass_height * 2 > avctx->coded_height / 2 ||
952 lowpass_width * 2 > avctx->coded_width / 2 )
954 ret = AVERROR_INVALIDDATA;
958 for (i = 0; i < lowpass_height * 2; i++) {
959 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
960 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
962 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
963 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
964 process_alpha(dst, lowpass_width * 2);
965 low += lowpass_width;
966 high += lowpass_width;
970 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
971 pic->interlaced_frame = 1;
972 low = s->plane[plane].subband[0];
973 high = s->plane[plane].subband[7];
974 output = s->plane[plane].l_h[6];
975 for (i = 0; i < lowpass_height; i++) {
976 horiz_filter(output, low, high, lowpass_width);
977 low += lowpass_width;
978 high += lowpass_width;
979 output += lowpass_width * 2;
982 low = s->plane[plane].subband[8];
983 high = s->plane[plane].subband[9];
984 output = s->plane[plane].l_h[7];
985 for (i = 0; i < lowpass_height; i++) {
986 horiz_filter(output, low, high, lowpass_width);
987 low += lowpass_width;
988 high += lowpass_width;
989 output += lowpass_width * 2;
992 dst = (int16_t *)pic->data[act_plane];
993 low = s->plane[plane].l_h[6];
994 high = s->plane[plane].l_h[7];
995 for (i = 0; i < lowpass_height; i++) {
996 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
997 low += lowpass_width * 2;
998 high += lowpass_width * 2;
999 dst += pic->linesize[act_plane];
1005 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1015 static av_cold int cfhd_close(AVCodecContext *avctx)
1017 CFHDContext *s = avctx->priv_data;
1021 ff_free_vlc(&s->vlc_9);
1022 ff_free_vlc(&s->vlc_18);
1027 AVCodec ff_cfhd_decoder = {
1029 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1030 .type = AVMEDIA_TYPE_VIDEO,
1031 .id = AV_CODEC_ID_CFHD,
1032 .priv_data_size = sizeof(CFHDContext),
1034 .close = cfhd_close,
1035 .decode = cfhd_decode,
1036 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1037 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,