diff options
author | Paul B Mahol <onemda@gmail.com> | 2020-08-04 14:45:48 +0200 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2020-08-08 12:03:43 +0200 |
commit | 1006a2151236f9235bf02822f263b3fb0532111e (patch) | |
tree | 030a3b656d6b8c28dd1bbdc5c702e3af6d8875af | |
parent | 1c6a010e3bc5bf4c08455d9323f3fdc3b0b3c255 (diff) | |
download | ffmpeg-1006a2151236f9235bf02822f263b3fb0532111e.tar.gz |
avcodec/cfhd: add 3d transform support
Based on Gagandeep Singh patch.
-rw-r--r-- | libavcodec/cfhd.c | 682 | ||||
-rw-r--r-- | libavcodec/cfhd.h | 19 |
2 files changed, 622 insertions, 79 deletions
diff --git a/libavcodec/cfhd.c b/libavcodec/cfhd.c index a2cf339858..088ab42355 100644 --- a/libavcodec/cfhd.c +++ b/libavcodec/cfhd.c @@ -88,16 +88,18 @@ static void init_frame_defaults(CFHDContext *s) { s->coded_width = 0; s->coded_height = 0; + s->coded_format = AV_PIX_FMT_YUV422P10; s->cropped_height = 0; s->bpc = 10; - s->channel_cnt = 4; + s->channel_cnt = 3; s->subband_cnt = SUBBAND_COUNT; s->channel_num = 0; s->lowpass_precision = 16; s->quantisation = 1; s->codebook = 0; s->difference_coding = 0; - s->progressive = 0; + s->frame_type = 0; + s->sample_type = 0; init_plane_defaults(s); init_peak_table_defaults(s); } @@ -144,14 +146,15 @@ static inline void process_alpha(int16_t *alpha, int width) } } -static inline void process_bayer(AVFrame *frame) +static inline void process_bayer(AVFrame *frame, int bpc) { const int linesize = frame->linesize[0]; uint16_t *r = (uint16_t *)frame->data[0]; uint16_t *g1 = (uint16_t *)(frame->data[0] + 2); uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]); uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2); - const int mid = 2048; + const int mid = 1 << (bpc - 1); + const int factor = 1 << (16 - bpc); for (int y = 0; y < frame->height >> 1; y++) { for (int x = 0; x < frame->width; x += 2) { @@ -169,10 +172,10 @@ static inline void process_bayer(AVFrame *frame) G2 = g - gd; B = (bg - mid) * 2 + g; - R = av_clip_uintp2(R * 16, 16); - G1 = av_clip_uintp2(G1 * 16, 16); - G2 = av_clip_uintp2(G2 * 16, 16); - B = av_clip_uintp2(B * 16, 16); + R = av_clip_uintp2(R * factor, 16); + G1 = av_clip_uintp2(G1 * factor, 16); + G2 = av_clip_uintp2(G2 * factor, 16); + B = av_clip_uintp2(B * factor, 16); r[x] = R; g1[x] = G1; @@ -240,6 +243,19 @@ static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int output[i + linesize] = av_clip_uintp2(odd, 10); } } + +static inline void inverse_temporal_filter(int16_t *output, int16_t *low, int16_t *high, + int width) +{ + for (int i = 0; i < width; i++) { + int even = (low[i] - high[i]) / 2; + int odd = (low[i] + high[i]) / 2; + + low[i] = even; + high[i] = odd; + } +} + static void horiz_filter(int16_t *output, int16_t *low, int16_t *high, int width) { @@ -272,11 +288,12 @@ static void free_buffers(CFHDContext *s) for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) { av_freep(&s->plane[i].idwt_buf); av_freep(&s->plane[i].idwt_tmp); + s->plane[i].idwt_size = 0; - for (j = 0; j < 9; j++) + for (j = 0; j < SUBBAND_COUNT_3D; j++) s->plane[i].subband[j] = NULL; - for (j = 0; j < 8; j++) + for (j = 0; j < 10; j++) s->plane[i].l_h[j] = NULL; } s->a_height = 0; @@ -286,15 +303,10 @@ static void free_buffers(CFHDContext *s) static int alloc_buffers(AVCodecContext *avctx) { CFHDContext *s = avctx->priv_data; - int i, j, ret, planes; + int i, j, ret, planes, bayer = 0; int chroma_x_shift, chroma_y_shift; unsigned k; - if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) { - s->coded_width *= 2; - s->coded_height *= 2; - } - if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0) return ret; avctx->pix_fmt = s->coded_format; @@ -308,14 +320,16 @@ static int alloc_buffers(AVCodecContext *avctx) planes = 4; chroma_x_shift = 1; chroma_y_shift = 1; + bayer = 1; } for (i = 0; i < planes; i++) { int w8, h8, w4, h4, w2, h2; - int width = i ? avctx->width >> chroma_x_shift : avctx->width; - int height = i ? avctx->height >> chroma_y_shift : avctx->height; + int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width; + int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height; ptrdiff_t stride = FFALIGN(width / 8, 8) * 8; - if (chroma_y_shift) + + if (chroma_y_shift && !bayer) height = FFALIGN(height / 8, 2) * 8; s->plane[i].width = width; s->plane[i].height = height; @@ -328,10 +342,20 @@ static int alloc_buffers(AVCodecContext *avctx) w2 = w4 * 2; h2 = h4 * 2; - s->plane[i].idwt_buf = - av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf)); - s->plane[i].idwt_tmp = - av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp)); + if (s->transform_type == 0) { + s->plane[i].idwt_size = FFALIGN(height, 8) * stride; + s->plane[i].idwt_buf = + av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf)); + s->plane[i].idwt_tmp = + av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp)); + } else { + s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2; + s->plane[i].idwt_buf = + av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf)); + s->plane[i].idwt_tmp = + av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp)); + } + if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp) return AVERROR(ENOMEM); @@ -342,16 +366,41 @@ static int alloc_buffers(AVCodecContext *avctx) s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4; s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4; s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4; + if (s->transform_type == 0) { s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2; s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2; s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2; + } else { + int16_t *frame2 = + s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2; + s->plane[i].subband[8] = frame2 + 2 * w4 * h4; + s->plane[i].subband[9] = frame2 + 1 * w4 * h4; + s->plane[i].subband[10] = frame2 + 3 * w4 * h4; + s->plane[i].subband[11] = frame2 + 2 * w2 * h2; + s->plane[i].subband[12] = frame2 + 1 * w2 * h2; + s->plane[i].subband[13] = frame2 + 3 * w2 * h2; + s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2; + s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2; + s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2; + } + if (s->transform_type == 0) { for (j = 0; j < DWT_LEVELS; j++) { for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) { s->plane[i].band[j][k].a_width = w8 << j; s->plane[i].band[j][k].a_height = h8 << j; } } + } else { + for (j = 0; j < DWT_LEVELS_3D; j++) { + int t = j < 1 ? 0 : (j < 3 ? 1 : 2); + + for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) { + s->plane[i].band[j][k].a_width = w8 << t; + s->plane[i].band[j][k].a_height = h8 << t; + } + } + } /* ll2 and ll1 commented out because they are done in-place */ s->plane[i].l_h[0] = s->plane[i].idwt_tmp; @@ -362,6 +411,12 @@ static int alloc_buffers(AVCodecContext *avctx) // s->plane[i].l_h[5] = ll1; s->plane[i].l_h[6] = s->plane[i].idwt_tmp; s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2; + if (s->transform_type != 0) { + int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2; + + s->plane[i].l_h[8] = frame2; + s->plane[i].l_h[9] = frame2 + 2 * w2 * h2; + } } s->a_height = s->coded_height; @@ -378,16 +433,15 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, GetByteContext gb; ThreadFrame frame = { .f = data }; AVFrame *pic = data; - int ret = 0, i, j, planes, plane, got_buffer = 0; + int ret = 0, i, j, plane, got_buffer = 0; int16_t *coeff_data; - s->coded_format = AV_PIX_FMT_YUV422P10; init_frame_defaults(s); - planes = av_pix_fmt_count_planes(s->coded_format); + s->planes = av_pix_fmt_count_planes(s->coded_format); bytestream2_init(&gb, avpkt->data, avpkt->size); - while (bytestream2_get_bytes_left(&gb) > 4) { + while (bytestream2_get_bytes_left(&gb) >= 4) { /* Bit weird but implement the tag parsing as the spec says */ uint16_t tagu = bytestream2_get_be16(&gb); int16_t tag = (int16_t)tagu; @@ -398,22 +452,17 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) { av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data); } else if (tag == SampleFlags) { - av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data); + av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data); s->progressive = data & 0x0001; + } else if (tag == FrameType) { + s->frame_type = data; + av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data); } else if (tag == ImageWidth) { av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data); s->coded_width = data; } else if (tag == ImageHeight) { av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data); s->coded_height = data; - } else if (tag == BitsPerComponent) { - av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data); - if (data < 1 || data > 31) { - av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data); - ret = AVERROR(EINVAL); - break; - } - s->bpc = data; } else if (tag == ChannelCount) { av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data); s->channel_cnt = data; @@ -424,7 +473,7 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, } } else if (tag == SubbandCount) { av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data); - if (data != SUBBAND_COUNT) { + if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) { av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data); ret = AVERROR_PATCHWELCOME; break; @@ -432,7 +481,7 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, } else if (tag == ChannelNumber) { s->channel_num = data; av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data); - if (s->channel_num >= planes) { + if (s->channel_num >= s->planes) { av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n"); ret = AVERROR(EINVAL); break; @@ -443,7 +492,8 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, s->level++; av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data); s->subband_num = data; - if (s->level >= DWT_LEVELS) { + if ((s->transform_type == 0 && s->level >= DWT_LEVELS) || + (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) { av_log(avctx, AV_LOG_ERROR, "Invalid level\n"); ret = AVERROR(EINVAL); break; @@ -456,7 +506,8 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, } else if (tag == SubbandBand) { av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data); s->subband_num_actual = data; - if (s->subband_num_actual >= 10) { + if ((s->transform_type == 0 && s->subband_num_actual >= SUBBAND_COUNT) || + (s->transform_type == 2 && s->subband_num_actual >= SUBBAND_COUNT_3D && s->subband_num_actual != 255)) { av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n"); ret = AVERROR(EINVAL); break; @@ -471,42 +522,35 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, s->prescale_shift[1] = (data >> 3) & 0x7; s->prescale_shift[2] = (data >> 6) & 0x7; av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data); + } else if (tag == BandEncoding) { + s->band_encoding = data; + av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data); } else if (tag == LowpassWidth) { av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data); - if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) { - av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n"); - ret = AVERROR(EINVAL); - break; - } s->plane[s->channel_num].band[0][0].width = data; s->plane[s->channel_num].band[0][0].stride = data; } else if (tag == LowpassHeight) { av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data); - if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) { - av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n"); - ret = AVERROR(EINVAL); - break; - } s->plane[s->channel_num].band[0][0].height = data; - } else if (tag == SampleType) + } else if (tag == SampleType) { + s->sample_type = data; av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data); - else if (tag == TransformType) { - if (data != 0) { - avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data); - ret = AVERROR_PATCHWELCOME; + } else if (tag == TransformType) { + if (data > 2) { + av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n"); + ret = AVERROR(EINVAL); break; } - av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data); + s->transform_type = data; + av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data); } else if (abstag >= 0x4000 && abstag <= 0x40ff) { if (abstag == 0x4001) s->peak.level = 0; av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required"); bytestream2_skipu(&gb, data * 4); - } else if (tag == 23) { - av_log(avctx, AV_LOG_DEBUG, "Skip frame\n"); - avpriv_report_missing_feature(avctx, "Skip frame"); - ret = AVERROR_PATCHWELCOME; - break; + } else if (tag == FrameIndex) { + av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data); + s->frame_index = data; } else if (tag == SampleIndexTable) { av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data); if (data > bytestream2_get_bytes_left(&gb) / 4) { @@ -555,6 +599,19 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, s->plane[s->channel_num].band[s->level][s->subband_num].height = data; } else if (tag == InputFormat) { av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data); + if (s->coded_format == AV_PIX_FMT_NONE || + s->coded_format == AV_PIX_FMT_YUV422P10) { + if (data >= 100 && data <= 105) { + s->coded_format = AV_PIX_FMT_BAYER_RGGB16; + } else if (data >= 122 && data <= 128) { + s->coded_format = AV_PIX_FMT_GBRP12; + } else if (data == 30) { + s->coded_format = AV_PIX_FMT_GBRAP12; + } else { + s->coded_format = AV_PIX_FMT_YUV422P10; + } + s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format); + } } else if (tag == BandCodingFlags) { s->codebook = data & 0xf; s->difference_coding = (data >> 4) & 1; @@ -582,7 +639,7 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, ret = AVERROR_PATCHWELCOME; break; } - planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format); + s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format); } else if (tag == -85) { av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data); s->cropped_height = data; @@ -602,9 +659,33 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, } else av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data); - /* Some kind of end of header tag */ - if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height && + if (tag == BitstreamMarker && data == 0xf0f && s->coded_format != AV_PIX_FMT_NONE) { + int lowpass_height = s->plane[s->channel_num].band[0][0].height; + int lowpass_width = s->plane[s->channel_num].band[0][0].width; + int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1; + + if (s->coded_width) { + s->coded_width *= factor; + } + + if (s->coded_height) { + s->coded_height *= factor; + } + + if (!s->a_width && !s->coded_width) { + s->coded_width = lowpass_width * factor * 8; + } + + if (!s->a_height && !s->coded_height) { + s->coded_height = lowpass_height * factor * 8; + } + + if (s->a_width && !s->coded_width) + s->coded_width = s->a_width; + if (s->a_height && !s->coded_height) + s->coded_height = s->a_height; + if (s->a_width != s->coded_width || s->a_height != s->coded_height || s->a_format != s->coded_format) { free_buffers(s); @@ -632,7 +713,20 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, s->coded_height = 0; s->coded_format = AV_PIX_FMT_NONE; got_buffer = 1; + } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) { + frame.f->width = + frame.f->height = 0; + + if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) + return ret; + s->coded_width = 0; + s->coded_height = 0; + s->coded_format = AV_PIX_FMT_NONE; + got_buffer = 1; } + + if (s->subband_num_actual == 255) + goto finish; coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual]; /* Lowpass coefficients */ @@ -642,6 +736,20 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height; int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width; + if (lowpass_width < 3 || + lowpass_width > lowpass_a_width) { + av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n"); + ret = AVERROR(EINVAL); + goto end; + } + + if (lowpass_height < 3 || + lowpass_height > lowpass_a_height) { + av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n"); + ret = AVERROR(EINVAL); + goto end; + } + if (!got_buffer) { av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n"); ret = AVERROR(EINVAL); @@ -676,7 +784,7 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height); } - if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) { + if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) { int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height; int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width; int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width; @@ -705,6 +813,12 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8); { OPEN_READER(re, &s->gb); + + const int lossless = (s->sample_type == 2 || s->sample_type == 3 || s->frame_type) && + s->subband_num_actual == 7 && s->band_encoding == 5; + + if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7) + s->codebook = 1; if (!s->codebook) { while (1) { UPDATE_CACHE(re, &s->gb); @@ -720,9 +834,21 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, if (count > expected) break; + if (!lossless) coeff = dequant_and_decompand(s, level, s->quantisation, 0); + else + coeff = level; + if (tag == BandSecondPass) { + const uint16_t q = s->quantisation; + + for (i = 0; i < run; i++) { + *coeff_data |= coeff << 8; + *coeff_data++ *= q; + } + } else { for (i = 0; i < run; i++) *coeff_data++ = coeff; + } } } else { while (1) { @@ -739,9 +865,21 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, if (count > expected) break; + if (!lossless) coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook); + else + coeff = level; + if (tag == BandSecondPass) { + const uint16_t q = s->quantisation; + + for (i = 0; i < run; i++) { + *coeff_data |= coeff << 8; + *coeff_data++ *= q; + } + } else { for (i = 0; i < run; i++) *coeff_data++ = coeff; + } } } CLOSE_READER(re, &s->gb); @@ -766,7 +904,9 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, bytestream2_seek(&gb, bytes, SEEK_CUR); av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected); - s->codebook = 0; +finish: + if (s->subband_num_actual != 255) + s->codebook = 0; /* Copy last line of coefficients if odd height */ if (highpass_height & 1) { @@ -777,6 +917,14 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, } } + s->planes = av_pix_fmt_count_planes(avctx->pix_fmt); + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) { + s->progressive = 1; + s->planes = 4; + } + + ff_thread_finish_setup(avctx); + if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE || s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) { av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n"); @@ -790,14 +938,8 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, goto end; } - planes = av_pix_fmt_count_planes(avctx->pix_fmt); - if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) { - if (!s->progressive) - return AVERROR_INVALIDDATA; - planes = 4; - } - - for (plane = 0; plane < planes && !ret; plane++) { + if (s->transform_type == 0 && s->sample_type != 1) { + for (plane = 0; plane < s->planes && !ret; plane++) { /* level 1 */ int lowpass_height = s->plane[plane].band[0][0].height; int lowpass_width = s->plane[plane].band[0][0].width; @@ -1012,10 +1154,363 @@ static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, } } } + } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) { + for (plane = 0; plane < s->planes && !ret; plane++) { + int lowpass_height = s->plane[plane].band[0][0].height; + int lowpass_width = s->plane[plane].band[0][0].width; + int highpass_stride = s->plane[plane].band[0][1].stride; + int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane; + int16_t *low, *high, *output, *dst; + ptrdiff_t dst_linesize; + + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) { + act_plane = 0; + dst_linesize = pic->linesize[act_plane]; + } else { + dst_linesize = pic->linesize[act_plane] / 2; + } + + if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width || + !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) { + av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n"); + ret = AVERROR(EINVAL); + goto end; + } + + av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride); + + low = s->plane[plane].subband[0]; + high = s->plane[plane].subband[2]; + output = s->plane[plane].l_h[0]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].subband[1]; + high = s->plane[plane].subband[3]; + output = s->plane[plane].l_h[1]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].l_h[0]; + high = s->plane[plane].l_h[1]; + output = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height * 2; i++) { + horiz_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + output += lowpass_width * 2; + } + if (s->bpc == 12) { + output = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height * 2; i++) { + for (j = 0; j < lowpass_width * 2; j++) + output[j] *= 4; + + output += lowpass_width * 2; + } + } + + lowpass_height = s->plane[plane].band[1][1].height; + lowpass_width = s->plane[plane].band[1][1].width; + highpass_stride = s->plane[plane].band[1][1].stride; + + if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width || + !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) { + av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n"); + ret = AVERROR(EINVAL); + goto end; + } + + av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride); + + low = s->plane[plane].l_h[7]; + high = s->plane[plane].subband[5]; + output = s->plane[plane].l_h[3]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].subband[4]; + high = s->plane[plane].subband[6]; + output = s->plane[plane].l_h[4]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].l_h[3]; + high = s->plane[plane].l_h[4]; + output = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height * 2; i++) { + horiz_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + output += lowpass_width * 2; + } + + output = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height * 2; i++) { + for (j = 0; j < lowpass_width * 2; j++) + output[j] *= 4; + output += lowpass_width * 2; + } + + low = s->plane[plane].subband[7]; + high = s->plane[plane].subband[9]; + output = s->plane[plane].l_h[3]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].subband[8]; + high = s->plane[plane].subband[10]; + output = s->plane[plane].l_h[4]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].l_h[3]; + high = s->plane[plane].l_h[4]; + output = s->plane[plane].l_h[9]; + for (i = 0; i < lowpass_height * 2; i++) { + horiz_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + output += lowpass_width * 2; + } + + lowpass_height = s->plane[plane].band[4][1].height; + lowpass_width = s->plane[plane].band[4][1].width; + highpass_stride = s->plane[plane].band[4][1].stride; + av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride); + + if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width || + !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) { + av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n"); + ret = AVERROR(EINVAL); + goto end; + } + + low = s->plane[plane].l_h[7]; + high = s->plane[plane].l_h[9]; + output = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height; i++) { + inverse_temporal_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + } + if (s->progressive) { + low = s->plane[plane].l_h[7]; + high = s->plane[plane].subband[15]; + output = s->plane[plane].l_h[6]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].subband[14]; + high = s->plane[plane].subband[16]; + output = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].l_h[9]; + high = s->plane[plane].subband[12]; + output = s->plane[plane].l_h[8]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + low = s->plane[plane].subband[11]; + high = s->plane[plane].subband[13]; + output = s->plane[plane].l_h[9]; + for (i = 0; i < lowpass_width; i++) { + vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height); + low++; + high++; + output++; + } + + if (s->sample_type == 1) + continue; + + dst = (int16_t *)pic->data[act_plane]; + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) { + if (plane & 1) + dst++; + if (plane > 1) + dst += pic->linesize[act_plane] >> 1; + } + + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 && + (lowpass_height * 2 > avctx->coded_height / 2 || + lowpass_width * 2 > avctx->coded_width / 2 ) + ) { + ret = AVERROR_INVALIDDATA; + goto end; + } + + low = s->plane[plane].l_h[6]; + high = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height * 2; i++) { + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) + horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc); + else + horiz_filter_clip(dst, low, high, lowpass_width, s->bpc); + low += lowpass_width; + high += lowpass_width; + dst += dst_linesize; + } + } else { + pic->interlaced_frame = 1; + low = s->plane[plane].l_h[7]; + high = s->plane[plane].subband[14]; + output = s->plane[plane].l_h[6]; + for (i = 0; i < lowpass_height; i++) { + horiz_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + output += lowpass_width * 2; + } + + low = s->plane[plane].subband[15]; + high = s->plane[plane].subband[16]; + output = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height; i++) { + horiz_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + output += lowpass_width * 2; + } + + low = s->plane[plane].l_h[9]; + high = s->plane[plane].subband[11]; + output = s->plane[plane].l_h[8]; + for (i = 0; i < lowpass_height; i++) { + horiz_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + output += lowpass_width * 2; + } + + low = s->plane[plane].subband[12]; + high = s->plane[plane].subband[13]; + output = s->plane[plane].l_h[9]; + for (i = 0; i < lowpass_height; i++) { + horiz_filter(output, low, high, lowpass_width); + low += lowpass_width; + high += lowpass_width; + output += lowpass_width * 2; + } + + if (s->sample_type == 1) + continue; + + dst = (int16_t *)pic->data[act_plane]; + low = s->plane[plane].l_h[6]; + high = s->plane[plane].l_h[7]; + for (i = 0; i < lowpass_height; i++) { + interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane); + low += lowpass_width * 2; + high += lowpass_width * 2; + dst += pic->linesize[act_plane]; + } + } + } + } + + if (s->transform_type == 2 && s->sample_type == 1) { + int16_t *low, *high, *dst; + int lowpass_height, lowpass_width, highpass_stride; + ptrdiff_t dst_linesize; + + for (plane = 0; plane < s->planes; plane++) { + int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane; + + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) { + act_plane = 0; + dst_linesize = pic->linesize[act_plane]; + } else { + dst_linesize = pic->linesize[act_plane] / 2; + } + + lowpass_height = s->plane[plane].band[4][1].height; + lowpass_width = s->plane[plane].band[4][1].width; + highpass_stride = s->plane[plane].band[4][1].stride; + + if (s->progressive) { + dst = (int16_t *)pic->data[act_plane]; + low = s->plane[plane].l_h[8]; + high = s->plane[plane].l_h[9]; + + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) { + if (plane & 1) + dst++; + if (plane > 1) + dst += pic->linesize[act_plane] >> 1; + } + + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 && + (lowpass_height * 2 > avctx->coded_height / 2 || + lowpass_width * 2 > avctx->coded_width / 2 ) + ) { + ret = AVERROR_INVALIDDATA; + goto end; + } + for (i = 0; i < lowpass_height * 2; i++) { + if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) + horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc); + else + horiz_filter_clip(dst, low, high, lowpass_width, s->bpc); + low += lowpass_width; + high += lowpass_width; + dst += dst_linesize; + } + } else { + dst = (int16_t *)pic->data[act_plane]; + low = s->plane[plane].l_h[8]; + high = s->plane[plane].l_h[9]; + for (i = 0; i < lowpass_height; i++) { + interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane); + low += lowpass_width * 2; + high += lowpass_width * 2; + dst += pic->linesize[act_plane]; + } + } + } + } if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) - process_bayer(pic); + process_bayer(pic, s->bpc); end: if (ret < 0) return ret; @@ -1036,6 +1531,42 @@ static av_cold int cfhd_close(AVCodecContext *avctx) return 0; } +#if HAVE_THREADS +static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) +{ + CFHDContext *psrc = src->priv_data; + CFHDContext *pdst = dst->priv_data; + int ret; + + if (dst == src || psrc->transform_type == 0) + return 0; + + pdst->a_format = psrc->a_format; + pdst->a_width = psrc->a_width; + pdst->a_height = psrc->a_height; + pdst->transform_type = psrc->transform_type; + pdst->progressive = psrc->progressive; + pdst->planes = psrc->planes; + + if (!pdst->plane[0].idwt_buf) { + pdst->coded_width = pdst->a_width; + pdst->coded_height = pdst->a_height; + pdst->coded_format = pdst->a_format; + ret = alloc_buffers(dst); + if (ret < 0) + return ret; + } + + for (int plane = 0; plane < pdst->planes; plane++) { + memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band)); + memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf, + pdst->plane[plane].idwt_size * sizeof(int16_t)); + } + + return 0; +} +#endif + AVCodec ff_cfhd_decoder = { .name = "cfhd", .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"), @@ -1045,6 +1576,7 @@ AVCodec ff_cfhd_decoder = { .init = cfhd_init, .close = cfhd_close, .decode = cfhd_decode, + .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context), .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, }; diff --git a/libavcodec/cfhd.h b/libavcodec/cfhd.h index 52ce94f066..09c570c06b 100644 --- a/libavcodec/cfhd.h +++ b/libavcodec/cfhd.h @@ -42,8 +42,10 @@ enum CFHDParam { NumSpatial = 15, FirstWavelet = 16, GroupTrailer = 18, + FrameType = 19, ImageWidth = 20, ImageHeight = 21, + FrameIndex = 23, LowpassSubband = 25, NumLevels = 26, LowpassWidth = 27, @@ -76,9 +78,9 @@ enum CFHDParam { Precision = 70, InputFormat = 71, BandCodingFlags = 72, + BandSecondPass = 82, PrescaleTable = 83, EncodedFormat = 84, - BitsPerComponent = 101, ChannelWidth = 104, ChannelHeight = 105, PrescaleShift = 109, @@ -86,6 +88,7 @@ enum CFHDParam { #define VLC_BITS 9 #define SUBBAND_COUNT 10 +#define SUBBAND_COUNT_3D 17 typedef struct CFHD_RL_VLC_ELEM { int16_t level; @@ -94,6 +97,7 @@ typedef struct CFHD_RL_VLC_ELEM { } CFHD_RL_VLC_ELEM; #define DWT_LEVELS 3 +#define DWT_LEVELS_3D 6 typedef struct SubBand { ptrdiff_t stride; @@ -110,12 +114,13 @@ typedef struct Plane { int16_t *idwt_buf; int16_t *idwt_tmp; + int idwt_size; /* TODO: merge this into SubBand structure */ - int16_t *subband[SUBBAND_COUNT]; - int16_t *l_h[8]; + int16_t *subband[SUBBAND_COUNT_3D]; + int16_t *l_h[10]; - SubBand band[DWT_LEVELS][4]; + SubBand band[DWT_LEVELS_3D][4]; } Plane; typedef struct Peak { @@ -137,6 +142,11 @@ typedef struct CFHDContext { GetBitContext gb; + int planes; + int frame_type; + int frame_index; + int sample_type; + int transform_type; int coded_width; int coded_height; int cropped_height; @@ -150,6 +160,7 @@ typedef struct CFHDContext { int bpc; // bits per channel/component int channel_cnt; int subband_cnt; + int band_encoding; int channel_num; uint8_t lowpass_precision; uint16_t quantisation; |