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;
47 avctx->bits_per_raw_sample = 10;
50 return ff_cfhd_init_vlcs(s);
53 static void init_plane_defaults(CFHDContext *s)
57 s->subband_num_actual = 0;
60 static void init_peak_table_defaults(CFHDContext *s)
64 memset(&s->peak.base, 0, sizeof(s->peak.base));
67 static void init_frame_defaults(CFHDContext *s)
71 s->cropped_height = 0;
74 s->subband_cnt = SUBBAND_COUNT;
76 s->lowpass_precision = 16;
81 s->difference_coding = 0;
83 init_plane_defaults(s);
84 init_peak_table_defaults(s);
87 /* TODO: merge with VLC tables or use LUT */
88 static inline int dequant_and_decompand(int level, int quantisation, int codebook)
90 if (codebook == 0 || codebook == 1) {
91 int64_t abslevel = abs(level);
93 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (256 * 256 * 256))) *
94 FFSIGN(level) * quantisation;
96 return level * quantisation;
98 return level * quantisation;
101 static inline void difference_coding(int16_t *band, int width, int height)
105 for (i = 0; i < height; i++) {
106 for (j = 1; j < width; j++) {
107 band[j] += band[j-1];
113 static inline void peak_table(int16_t *band, Peak *peak, int length)
116 for (i = 0; i < length; i++)
117 if (abs(band[i]) > peak->level)
118 band[i] = bytestream2_get_le16(&peak->base);
121 static inline void process_alpha(int16_t *alpha, int width)
124 for (i = 0; i < width; i++) {
126 channel -= ALPHA_COMPAND_DC_OFFSET;
128 channel *= ALPHA_COMPAND_GAIN;
130 channel = av_clip_uintp2(channel, 12);
135 static inline void process_bayer(AVFrame *frame)
137 const int linesize = frame->linesize[0];
138 uint16_t *r = (uint16_t *)frame->data[0];
139 uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
140 uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
141 uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
142 const int mid = 2048;
144 for (int y = 0; y < frame->height >> 1; y++) {
145 for (int x = 0; x < frame->width; x += 2) {
155 R = (rg - mid) * 2 + g;
158 B = (bg - mid) * 2 + g;
160 R = av_clip_uintp2(R * 16, 16);
161 G1 = av_clip_uintp2(G1 * 16, 16);
162 G2 = av_clip_uintp2(G2 * 16, 16);
163 B = av_clip_uintp2(B * 16, 16);
178 static inline void filter(int16_t *output, ptrdiff_t out_stride,
179 int16_t *low, ptrdiff_t low_stride,
180 int16_t *high, ptrdiff_t high_stride,
186 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
187 output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
189 output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
191 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
192 output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
194 output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
196 for (i = 1; i < len - 1; i++) {
197 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
198 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
200 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
202 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
203 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
205 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
208 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
209 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
211 output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
213 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
214 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
216 output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
219 static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
220 int width, int linesize, int plane)
224 for (i = 0; i < width; i++) {
225 even = (low[i] - high[i])/2;
226 odd = (low[i] + high[i])/2;
227 output[i] = av_clip_uintp2(even, 10);
228 output[i + linesize] = av_clip_uintp2(odd, 10);
231 static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
234 filter(output, 1, low, 1, high, 1, width, 0);
237 static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
240 filter(output, 1, low, 1, high, 1, width, clip);
243 static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
246 filter(output, 2, low, 1, high, 1, width, clip);
249 static void vert_filter(int16_t *output, ptrdiff_t out_stride,
250 int16_t *low, ptrdiff_t low_stride,
251 int16_t *high, ptrdiff_t high_stride, int len)
253 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
256 static void free_buffers(CFHDContext *s)
260 for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
261 av_freep(&s->plane[i].idwt_buf);
262 av_freep(&s->plane[i].idwt_tmp);
264 for (j = 0; j < 9; j++)
265 s->plane[i].subband[j] = NULL;
267 for (j = 0; j < 8; j++)
268 s->plane[i].l_h[j] = NULL;
274 static int alloc_buffers(AVCodecContext *avctx)
276 CFHDContext *s = avctx->priv_data;
277 int i, j, ret, planes;
278 int chroma_x_shift, chroma_y_shift;
281 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
283 s->coded_height *= 2;
286 if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
288 avctx->pix_fmt = s->coded_format;
290 if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
292 &chroma_y_shift)) < 0)
294 planes = av_pix_fmt_count_planes(s->coded_format);
295 if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
301 for (i = 0; i < planes; i++) {
302 int w8, h8, w4, h4, w2, h2;
303 int width = i ? avctx->width >> chroma_x_shift : avctx->width;
304 int height = i ? avctx->height >> chroma_y_shift : avctx->height;
305 ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
307 height = FFALIGN(height / 8, 2) * 8;
308 s->plane[i].width = width;
309 s->plane[i].height = height;
310 s->plane[i].stride = stride;
312 w8 = FFALIGN(s->plane[i].width / 8, 8);
313 h8 = FFALIGN(height, 8) / 8;
319 s->plane[i].idwt_buf =
320 av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
321 s->plane[i].idwt_tmp =
322 av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
323 if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
324 return AVERROR(ENOMEM);
326 s->plane[i].subband[0] = s->plane[i].idwt_buf;
327 s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
328 s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
329 s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
330 s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
331 s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
332 s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
333 s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
334 s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
335 s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
337 for (j = 0; j < DWT_LEVELS; j++) {
338 for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
339 s->plane[i].band[j][k].a_width = w8 << j;
340 s->plane[i].band[j][k].a_height = h8 << j;
344 /* ll2 and ll1 commented out because they are done in-place */
345 s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
346 s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
347 // s->plane[i].l_h[2] = ll2;
348 s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
349 s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
350 // s->plane[i].l_h[5] = ll1;
351 s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
352 s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
355 s->a_height = s->coded_height;
356 s->a_width = s->coded_width;
357 s->a_format = s->coded_format;
362 static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
365 CFHDContext *s = avctx->priv_data;
367 ThreadFrame frame = { .f = data };
369 int ret = 0, i, j, planes, plane, got_buffer = 0;
372 s->coded_format = AV_PIX_FMT_YUV422P10;
373 init_frame_defaults(s);
374 planes = av_pix_fmt_count_planes(s->coded_format);
376 bytestream2_init(&gb, avpkt->data, avpkt->size);
378 while (bytestream2_get_bytes_left(&gb) > 4) {
379 /* Bit weird but implement the tag parsing as the spec says */
380 uint16_t tagu = bytestream2_get_be16(&gb);
381 int16_t tag = (int16_t)tagu;
382 int8_t tag8 = (int8_t)(tagu >> 8);
383 uint16_t abstag = abs(tag);
384 int8_t abs_tag8 = abs(tag8);
385 uint16_t data = bytestream2_get_be16(&gb);
386 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
387 av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
388 } else if (tag == SampleFlags) {
389 av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
390 s->progressive = data & 0x0001;
391 } else if (tag == ImageWidth) {
392 av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
393 s->coded_width = data;
394 } else if (tag == ImageHeight) {
395 av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
396 s->coded_height = data;
397 } else if (tag == 101) {
398 av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
399 if (data < 1 || data > 31) {
400 av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
401 ret = AVERROR(EINVAL);
405 } else if (tag == ChannelCount) {
406 av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
407 s->channel_cnt = data;
409 av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
410 ret = AVERROR_PATCHWELCOME;
413 } else if (tag == SubbandCount) {
414 av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
415 if (data != SUBBAND_COUNT) {
416 av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
417 ret = AVERROR_PATCHWELCOME;
420 } else if (tag == ChannelNumber) {
421 s->channel_num = data;
422 av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
423 if (s->channel_num >= planes) {
424 av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
425 ret = AVERROR(EINVAL);
428 init_plane_defaults(s);
429 } else if (tag == SubbandNumber) {
430 if (s->subband_num != 0 && data == 1) // hack
432 av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
433 s->subband_num = data;
434 if (s->level >= DWT_LEVELS) {
435 av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
436 ret = AVERROR(EINVAL);
439 if (s->subband_num > 3) {
440 av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
441 ret = AVERROR(EINVAL);
444 } else if (tag == 51) {
445 av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
446 s->subband_num_actual = data;
447 if (s->subband_num_actual >= 10) {
448 av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
449 ret = AVERROR(EINVAL);
452 } else if (tag == LowpassPrecision)
453 av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
454 else if (tag == Quantization) {
455 s->quantisation = data;
456 av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
457 } else if (tag == PrescaleShift) {
458 s->prescale_shift[0] = (data >> 0) & 0x7;
459 s->prescale_shift[1] = (data >> 3) & 0x7;
460 s->prescale_shift[2] = (data >> 6) & 0x7;
461 av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
462 } else if (tag == LowpassWidth) {
463 av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
464 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
465 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
466 ret = AVERROR(EINVAL);
469 s->plane[s->channel_num].band[0][0].width = data;
470 s->plane[s->channel_num].band[0][0].stride = data;
471 } else if (tag == LowpassHeight) {
472 av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
473 if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
474 av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
475 ret = AVERROR(EINVAL);
478 s->plane[s->channel_num].band[0][0].height = data;
479 } else if (tag == SampleType)
480 av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
481 else if (tag == TransformType) {
483 avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
484 ret = AVERROR_PATCHWELCOME;
487 av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
488 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
489 if (abstag == 0x4001)
491 av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
492 bytestream2_skipu(&gb, data * 4);
493 } else if (tag == 23) {
494 av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
495 avpriv_report_missing_feature(avctx, "Skip frame");
496 ret = AVERROR_PATCHWELCOME;
498 } else if (tag == SampleIndexTable) {
499 av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
500 if (data > bytestream2_get_bytes_left(&gb) / 4) {
501 av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
502 ret = AVERROR_INVALIDDATA;
505 for (i = 0; i < data; i++) {
506 uint16_t tag2 = bytestream2_get_be16(&gb);
507 uint16_t val2 = bytestream2_get_be16(&gb);
508 av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
510 } else if (tag == HighpassWidth) {
511 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);
513 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
514 ret = AVERROR(EINVAL);
517 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
518 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
519 } else if (tag == HighpassHeight) {
520 av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
522 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
523 ret = AVERROR(EINVAL);
526 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
527 } else if (tag == 49) {
528 av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
530 av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
531 ret = AVERROR(EINVAL);
534 s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
535 s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
536 } else if (tag == 50) {
537 av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
539 av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
540 ret = AVERROR(EINVAL);
543 s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
544 } else if (tag == 71) {
546 av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
547 } else if (tag == 72) {
548 s->codebook = data & 0xf;
549 s->difference_coding = (data >> 4) & 1;
550 av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
551 } else if (tag == 70) {
552 av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
553 if (!(data == 10 || data == 12)) {
554 av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
555 ret = AVERROR(EINVAL);
559 } else if (tag == EncodedFormat) {
560 av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
562 s->coded_format = AV_PIX_FMT_YUV422P10;
563 } else if (data == 2) {
564 s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
565 } else if (data == 3) {
566 s->coded_format = AV_PIX_FMT_GBRP12;
567 } else if (data == 4) {
568 s->coded_format = AV_PIX_FMT_GBRAP12;
570 avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
571 ret = AVERROR_PATCHWELCOME;
574 planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
575 } else if (tag == -85) {
576 av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
577 s->cropped_height = data;
578 } else if (tag == -75) {
579 s->peak.offset &= ~0xffff;
580 s->peak.offset |= (data & 0xffff);
583 } else if (tag == -76) {
584 s->peak.offset &= 0xffff;
585 s->peak.offset |= (data & 0xffffU)<<16;
588 } else if (tag == -74 && s->peak.offset) {
589 s->peak.level = data;
590 bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
592 av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
594 /* Some kind of end of header tag */
595 if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
596 s->coded_format != AV_PIX_FMT_NONE) {
597 if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
598 s->a_format != s->coded_format) {
600 if ((ret = alloc_buffers(avctx)) < 0) {
605 ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
608 if (s->cropped_height) {
609 unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
610 if (avctx->height < height)
611 return AVERROR_INVALIDDATA;
612 avctx->height = height;
617 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
622 s->coded_format = AV_PIX_FMT_NONE;
625 coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
627 /* Lowpass coefficients */
628 if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
629 int lowpass_height = s->plane[s->channel_num].band[0][0].height;
630 int lowpass_width = s->plane[s->channel_num].band[0][0].width;
631 int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
632 int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
635 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
636 ret = AVERROR(EINVAL);
640 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
641 lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
642 av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
643 ret = AVERROR(EINVAL);
647 av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
648 for (i = 0; i < lowpass_height; i++) {
649 for (j = 0; j < lowpass_width; j++)
650 coeff_data[j] = bytestream2_get_be16u(&gb);
652 coeff_data += lowpass_width;
655 /* Align to mod-4 position to continue reading tags */
656 bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
658 /* Copy last line of coefficients if odd height */
659 if (lowpass_height & 1) {
660 memcpy(&coeff_data[lowpass_height * lowpass_width],
661 &coeff_data[(lowpass_height - 1) * lowpass_width],
662 lowpass_width * sizeof(*coeff_data));
665 av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
668 if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
669 int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
670 int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
671 int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
672 int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
673 int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
675 int a_expected = highpass_a_height * highpass_a_width;
676 int level, run, coeff;
677 int count = 0, bytes;
680 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
681 ret = AVERROR(EINVAL);
685 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
686 av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
687 ret = AVERROR(EINVAL);
690 expected = highpass_height * highpass_stride;
692 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);
694 init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
696 OPEN_READER(re, &s->gb);
699 UPDATE_CACHE(re, &s->gb);
700 GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
709 if (count > expected)
712 coeff = dequant_and_decompand(level, s->quantisation, 0);
713 for (i = 0; i < run; i++)
714 *coeff_data++ = coeff;
718 UPDATE_CACHE(re, &s->gb);
719 GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
723 if (level == 255 && run == 2)
728 if (count > expected)
731 coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
732 for (i = 0; i < run; i++)
733 *coeff_data++ = coeff;
736 CLOSE_READER(re, &s->gb);
739 if (count > expected) {
740 av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
741 ret = AVERROR(EINVAL);
745 peak_table(coeff_data - count, &s->peak, count);
746 if (s->difference_coding)
747 difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
749 bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
750 if (bytes > bytestream2_get_bytes_left(&gb)) {
751 av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
752 ret = AVERROR(EINVAL);
755 bytestream2_seek(&gb, bytes, SEEK_CUR);
757 av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
760 /* Copy last line of coefficients if odd height */
761 if (highpass_height & 1) {
762 memcpy(&coeff_data[highpass_height * highpass_stride],
763 &coeff_data[(highpass_height - 1) * highpass_stride],
764 highpass_stride * sizeof(*coeff_data));
769 if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
770 s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
771 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
772 ret = AVERROR(EINVAL);
777 av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
778 ret = AVERROR(EINVAL);
782 planes = av_pix_fmt_count_planes(avctx->pix_fmt);
783 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
785 return AVERROR_INVALIDDATA;
789 for (plane = 0; plane < planes && !ret; plane++) {
791 int lowpass_height = s->plane[plane].band[0][0].height;
792 int lowpass_width = s->plane[plane].band[0][0].width;
793 int highpass_stride = s->plane[plane].band[0][1].stride;
794 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
795 ptrdiff_t dst_linesize;
796 int16_t *low, *high, *output, *dst;
798 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
800 dst_linesize = pic->linesize[act_plane];
802 dst_linesize = pic->linesize[act_plane] / 2;
805 if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
806 !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
807 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
808 ret = AVERROR(EINVAL);
812 av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
814 low = s->plane[plane].subband[0];
815 high = s->plane[plane].subband[2];
816 output = s->plane[plane].l_h[0];
817 for (i = 0; i < lowpass_width; i++) {
818 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
824 low = s->plane[plane].subband[1];
825 high = s->plane[plane].subband[3];
826 output = s->plane[plane].l_h[1];
828 for (i = 0; i < lowpass_width; i++) {
829 // note the stride of "low" is highpass_stride
830 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
836 low = s->plane[plane].l_h[0];
837 high = s->plane[plane].l_h[1];
838 output = s->plane[plane].subband[0];
839 for (i = 0; i < lowpass_height * 2; i++) {
840 horiz_filter(output, low, high, lowpass_width);
841 low += lowpass_width;
842 high += lowpass_width;
843 output += lowpass_width * 2;
846 output = s->plane[plane].subband[0];
847 for (i = 0; i < lowpass_height * 2; i++) {
848 for (j = 0; j < lowpass_width * 2; j++)
851 output += lowpass_width * 2;
856 lowpass_height = s->plane[plane].band[1][1].height;
857 lowpass_width = s->plane[plane].band[1][1].width;
858 highpass_stride = s->plane[plane].band[1][1].stride;
860 if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
861 !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
862 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
863 ret = AVERROR(EINVAL);
867 av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
869 low = s->plane[plane].subband[0];
870 high = s->plane[plane].subband[5];
871 output = s->plane[plane].l_h[3];
872 for (i = 0; i < lowpass_width; i++) {
873 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
879 low = s->plane[plane].subband[4];
880 high = s->plane[plane].subband[6];
881 output = s->plane[plane].l_h[4];
882 for (i = 0; i < lowpass_width; i++) {
883 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
889 low = s->plane[plane].l_h[3];
890 high = s->plane[plane].l_h[4];
891 output = s->plane[plane].subband[0];
892 for (i = 0; i < lowpass_height * 2; i++) {
893 horiz_filter(output, low, high, lowpass_width);
894 low += lowpass_width;
895 high += lowpass_width;
896 output += lowpass_width * 2;
899 output = s->plane[plane].subband[0];
900 for (i = 0; i < lowpass_height * 2; i++) {
901 for (j = 0; j < lowpass_width * 2; j++)
904 output += lowpass_width * 2;
908 lowpass_height = s->plane[plane].band[2][1].height;
909 lowpass_width = s->plane[plane].band[2][1].width;
910 highpass_stride = s->plane[plane].band[2][1].stride;
912 if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
913 !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
914 av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
915 ret = AVERROR(EINVAL);
919 av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
920 if (s->progressive) {
921 low = s->plane[plane].subband[0];
922 high = s->plane[plane].subband[8];
923 output = s->plane[plane].l_h[6];
924 for (i = 0; i < lowpass_width; i++) {
925 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
931 low = s->plane[plane].subband[7];
932 high = s->plane[plane].subband[9];
933 output = s->plane[plane].l_h[7];
934 for (i = 0; i < lowpass_width; i++) {
935 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
941 dst = (int16_t *)pic->data[act_plane];
942 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
946 dst += pic->linesize[act_plane] >> 1;
948 low = s->plane[plane].l_h[6];
949 high = s->plane[plane].l_h[7];
951 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
952 (lowpass_height * 2 > avctx->coded_height / 2 ||
953 lowpass_width * 2 > avctx->coded_width / 2 )
955 ret = AVERROR_INVALIDDATA;
959 for (i = 0; i < lowpass_height * 2; i++) {
960 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
961 horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
963 horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
964 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
965 process_alpha(dst, lowpass_width * 2);
966 low += lowpass_width;
967 high += lowpass_width;
971 av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
972 pic->interlaced_frame = 1;
973 low = s->plane[plane].subband[0];
974 high = s->plane[plane].subband[7];
975 output = s->plane[plane].l_h[6];
976 for (i = 0; i < lowpass_height; i++) {
977 horiz_filter(output, low, high, lowpass_width);
978 low += lowpass_width;
979 high += lowpass_width;
980 output += lowpass_width * 2;
983 low = s->plane[plane].subband[8];
984 high = s->plane[plane].subband[9];
985 output = s->plane[plane].l_h[7];
986 for (i = 0; i < lowpass_height; i++) {
987 horiz_filter(output, low, high, lowpass_width);
988 low += lowpass_width;
989 high += lowpass_width;
990 output += lowpass_width * 2;
993 dst = (int16_t *)pic->data[act_plane];
994 low = s->plane[plane].l_h[6];
995 high = s->plane[plane].l_h[7];
996 for (i = 0; i < lowpass_height; i++) {
997 interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
998 low += lowpass_width * 2;
999 high += lowpass_width * 2;
1000 dst += pic->linesize[act_plane];
1006 if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
1016 static av_cold int cfhd_close(AVCodecContext *avctx)
1018 CFHDContext *s = avctx->priv_data;
1022 ff_free_vlc(&s->vlc_9);
1023 ff_free_vlc(&s->vlc_18);
1028 AVCodec ff_cfhd_decoder = {
1030 .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
1031 .type = AVMEDIA_TYPE_VIDEO,
1032 .id = AV_CODEC_ID_CFHD,
1033 .priv_data_size = sizeof(CFHDContext),
1035 .close = cfhd_close,
1036 .decode = cfhd_decode,
1037 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1038 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,