/* * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com> * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * Cineform HD video decoder */ #include "libavutil/attributes.h"#include "libavutil/common.h"#include "libavutil/intreadwrite.h"#include "libavutil/mem.h"#include "libavutil/pixdesc.h" #include "avcodec.h"#include "bytestream.h"#include "codec_internal.h"#include "decode.h"#include "get_bits.h"#include "internal.h"#include "thread.h"#include "cfhd.h" #define ALPHA_COMPAND_DC_OFFSET 256#define ALPHA_COMPAND_GAIN 9400 static av_cold int cfhd_init(AVCodecContext *avctx){ CFHDContext *s = avctx->priv_data; s->avctx = avctx; for (int i = 0; i < 64; i++) { int val = i; if (val >= 40) { if (val >= 54) { val -= 54; val <<= 2; val += 54; } val -= 40; val <<= 2; val += 40; } s->lut[0][i] = val; } for (int i = 0; i < 256; i++) s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256)); return ff_cfhd_init_vlcs(s);} static void init_plane_defaults(CFHDContext *s){ s->subband_num = 0; s->level = 0; s->subband_num_actual = 0;} static void init_peak_table_defaults(CFHDContext *s){ s->peak.level = 0; s->peak.offset = 0; memset(&s->peak.base, 0, sizeof(s->peak.base));} 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 = 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->frame_type = 0; s->sample_type = 0; if (s->transform_type != 2) s->transform_type = -1; init_plane_defaults(s); init_peak_table_defaults(s);} static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook){ if (codebook == 0 || codebook == 1) { return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation; } else return level * quantisation;} static inline void difference_coding(int16_t *band, int width, int height){ int i,j; for (i = 0; i < height; i++) { for (j = 1; j < width; j++) { band[j] += band[j-1]; } band += width; }} static inline void peak_table(int16_t *band, Peak *peak, int length){ int i; for (i = 0; i < length; i++) if (abs(band[i]) > peak->level) band[i] = bytestream2_get_le16(&peak->base);} static inline void process_alpha(int16_t *alpha, int width){ int i, channel; for (i = 0; i < width; i++) { channel = alpha[i]; channel -= ALPHA_COMPAND_DC_OFFSET; channel <<= 3; channel *= ALPHA_COMPAND_GAIN; channel >>= 16; channel = av_clip_uintp2(channel, 12); alpha[i] = channel; }} 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 = 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) { int R, G1, G2, B; int g, rg, bg, gd; g = r[x]; rg = g1[x]; bg = g2[x]; gd = b[x]; gd -= mid; R = (rg - mid) * 2 + g; G1 = g + gd; G2 = g - gd; B = (bg - mid) * 2 + g; 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; g2[x] = G2; b[x] = B; } r += linesize; g1 += linesize; g2 += linesize; b += linesize; }} static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high, int width, int linesize, int plane){ int i; int16_t even, odd; for (i = 0; i < width; i++) { even = (low[i] - high[i])/2; odd = (low[i] + high[i])/2; output[i] = av_clip_uintp2(even, 10); output[i + linesize] = av_clip_uintp2(odd, 10); }} static inline void inverse_temporal_filter(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 free_buffers(CFHDContext *s){ int i, j; for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) { Plane *p = &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 < SUBBAND_COUNT_3D; j++) s->plane[i].subband[j] = NULL; for (j = 0; j < 10; j++) s->plane[i].l_h[j] = NULL; for (j = 0; j < DWT_LEVELS_3D; j++) p->band[j][0].read_ok = p->band[j][1].read_ok = p->band[j][2].read_ok = p->band[j][3].read_ok = 0; } s->a_height = 0; s->a_width = 0; s->a_transform_type = INT_MIN;} static int alloc_buffers(AVCodecContext *avctx){ CFHDContext *s = avctx->priv_data; int i, j, ret, planes, bayer = 0; int chroma_x_shift, chroma_y_shift; unsigned k; if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0) return ret; avctx->pix_fmt = s->coded_format; ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16); if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format, &chroma_x_shift, &chroma_y_shift)) < 0) return ret; planes = av_pix_fmt_count_planes(s->coded_format); if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) { 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 || 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) + 64) * 8; if (chroma_y_shift && !bayer) height = FFALIGN(height / 8, 2) * 8; s->plane[i].width = width; s->plane[i].height = height; s->plane[i].stride = stride; w8 = FFALIGN(s->plane[i].width / 8, 8) + 64; h8 = FFALIGN(height, 8) / 8; w4 = w8 * 2; h4 = h8 * 2; w2 = w4 * 2; h2 = h4 * 2; if (s->transform_type == 0) { s->plane[i].idwt_size = FFALIGN(height, 8) * stride; s->plane[i].idwt_buf = av_calloc(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_calloc(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); s->plane[i].subband[0] = s->plane[i].idwt_buf; s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8; s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8; s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8; 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; s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8; // s->plane[i].l_h[2] = ll2; s->plane[i].l_h[3] = s->plane[i].idwt_tmp; s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4; // 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_transform_type = s->transform_type; s->a_height = s->coded_height; s->a_width = s->coded_width; s->a_format = s->coded_format; return 0;} static int cfhd_decode(AVCodecContext *avctx, AVFrame *pic, int *got_frame, AVPacket *avpkt){ CFHDContext *s = avctx->priv_data; CFHDDSPContext *dsp = &s->dsp; GetByteContext gb; int ret = 0, i, j, plane, got_buffer = 0; int16_t *coeff_data; init_frame_defaults(s); s->planes = av_pix_fmt_count_planes(s->coded_format); bytestream2_init(&gb, avpkt->data, avpkt->size); 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; int8_t tag8 = (int8_t)(tagu >> 8); uint16_t abstag = abs(tag); int8_t abs_tag8 = abs(tag8); uint16_t data = bytestream2_get_be16(&gb); 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); 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 (abstag == VersionMajor) { av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data); } else if (abstag == VersionMinor) { av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data); } else if (abstag == VersionRevision) { av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data); } else if (abstag == VersionEdit) { av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data); } else if (abstag == Version) { av_log(avctx, AV_LOG_DEBUG, "Version %"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 == ChannelCount) { av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data); s->channel_cnt = data; if (data > 4) { av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data); ret = AVERROR_PATCHWELCOME; goto end; } } else if (tag == SubbandCount) { av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data); 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; goto end; } } else if (tag == ChannelNumber) { s->channel_num = data; av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data); if (s->channel_num >= s->planes) { av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n"); ret = AVERROR(EINVAL); goto end; } init_plane_defaults(s); } else if (tag == SubbandNumber) { if (s->subband_num != 0 && data == 1 && (s->transform_type == 0 || s->transform_type == 2)) // hack s->level++; av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data); s->subband_num = data; 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); goto end; } if (s->subband_num > 3) { av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n"); ret = AVERROR(EINVAL); goto end; } } else if (tag == SubbandBand) { av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data); if ((s->transform_type == 0 && data >= SUBBAND_COUNT) || (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) { av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n"); ret = AVERROR(EINVAL); goto end; } if (s->transform_type == 0 || s->transform_type == 2) s->subband_num_actual = data; else av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data); } else if (tag == LowpassPrecision) av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data); else if (tag == Quantization) { s->quantisation = data; av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data); } else if (tag == PrescaleTable) { for (i = 0; i < 8; i++) s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3; av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data); } else if (tag == BandEncoding) { if (!data || data > 5) { av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n"); ret = AVERROR(EINVAL); goto end; } 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); 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); s->plane[s->channel_num].band[0][0].height = data; } else if (tag == SampleType) { s->sample_type = data; av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);Lines 1–500 of 1467. Load more
/* * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com> * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * Cineform HD video decoder */ #include "libavutil/attributes.h" #include "libavutil/common.h" #include "libavutil/intreadwrite.h" #include "libavutil/mem.h" #include "libavutil/pixdesc.h" #include "avcodec.h" #include "bytestream.h" #include "codec_internal.h" #include "decode.h" #include "get_bits.h" #include "internal.h" #include "thread.h" #include "cfhd.h" #define ALPHA_COMPAND_DC_OFFSET 256 #define ALPHA_COMPAND_GAIN 9400 static av_cold int cfhd_init(AVCodecContext *avctx) { CFHDContext *s = avctx->priv_data; s->avctx = avctx; for (int i = 0; i < 64; i++) { int val = i; if (val >= 40) { if (val >= 54) { val -= 54; val <<= 2; val += 54; } val -= 40; val <<= 2; val += 40; } s->lut[0][i] = val; } for (int i = 0; i < 256; i++) s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256)); return ff_cfhd_init_vlcs(s); } static void init_plane_defaults(CFHDContext *s) { s->subband_num = 0; s->level = 0; s->subband_num_actual = 0; } static void init_peak_table_defaults(CFHDContext *s) { s->peak.level = 0; s->peak.offset = 0; memset(&s->peak.base, 0, sizeof(s->peak.base)); } 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 = 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->frame_type = 0; s->sample_type = 0; if (s->transform_type != 2) s->transform_type = -1; init_plane_defaults(s); init_peak_table_defaults(s); } static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook) { if (codebook == 0 || codebook == 1) { return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation; } else return level * quantisation; } static inline void difference_coding(int16_t *band, int width, int height) { int i,j; for (i = 0; i < height; i++) { for (j = 1; j < width; j++) { band[j] += band[j-1]; } band += width; } } static inline void peak_table(int16_t *band, Peak *peak, int length) { int i; for (i = 0; i < length; i++) if (abs(band[i]) > peak->level) band[i] = bytestream2_get_le16(&peak->base); } static inline void process_alpha(int16_t *alpha, int width) { int i, channel; for (i = 0; i < width; i++) { channel = alpha[i]; channel -= ALPHA_COMPAND_DC_OFFSET; channel <<= 3; channel *= ALPHA_COMPAND_GAIN; channel >>= 16; channel = av_clip_uintp2(channel, 12); alpha[i] = channel; } } 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 = 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) { int R, G1, G2, B; int g, rg, bg, gd; g = r[x]; rg = g1[x]; bg = g2[x]; gd = b[x]; gd -= mid; R = (rg - mid) * 2 + g; G1 = g + gd; G2 = g - gd; B = (bg - mid) * 2 + g; 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; g2[x] = G2; b[x] = B; } r += linesize; g1 += linesize; g2 += linesize; b += linesize; } } static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high, int width, int linesize, int plane) { int i; int16_t even, odd; for (i = 0; i < width; i++) { even = (low[i] - high[i])/2; odd = (low[i] + high[i])/2; output[i] = av_clip_uintp2(even, 10); output[i + linesize] = av_clip_uintp2(odd, 10); } } static inline void inverse_temporal_filter(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 free_buffers(CFHDContext *s) { int i, j; for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) { Plane *p = &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 < SUBBAND_COUNT_3D; j++) s->plane[i].subband[j] = NULL; for (j = 0; j < 10; j++) s->plane[i].l_h[j] = NULL; for (j = 0; j < DWT_LEVELS_3D; j++) p->band[j][0].read_ok = p->band[j][1].read_ok = p->band[j][2].read_ok = p->band[j][3].read_ok = 0; } s->a_height = 0; s->a_width = 0; s->a_transform_type = INT_MIN; } static int alloc_buffers(AVCodecContext *avctx) { CFHDContext *s = avctx->priv_data; int i, j, ret, planes, bayer = 0; int chroma_x_shift, chroma_y_shift; unsigned k; if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0) return ret; avctx->pix_fmt = s->coded_format; ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16); if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format, &chroma_x_shift, &chroma_y_shift)) < 0) return ret; planes = av_pix_fmt_count_planes(s->coded_format); if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) { 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 || 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) + 64) * 8; if (chroma_y_shift && !bayer) height = FFALIGN(height / 8, 2) * 8; s->plane[i].width = width; s->plane[i].height = height; s->plane[i].stride = stride; w8 = FFALIGN(s->plane[i].width / 8, 8) + 64; h8 = FFALIGN(height, 8) / 8; w4 = w8 * 2; h4 = h8 * 2; w2 = w4 * 2; h2 = h4 * 2; if (s->transform_type == 0) { s->plane[i].idwt_size = FFALIGN(height, 8) * stride; s->plane[i].idwt_buf = av_calloc(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_calloc(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); s->plane[i].subband[0] = s->plane[i].idwt_buf; s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8; s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8; s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8; 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; s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8; // s->plane[i].l_h[2] = ll2; s->plane[i].l_h[3] = s->plane[i].idwt_tmp; s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4; // 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_transform_type = s->transform_type; s->a_height = s->coded_height; s->a_width = s->coded_width; s->a_format = s->coded_format; return 0; } static int cfhd_decode(AVCodecContext *avctx, AVFrame *pic, int *got_frame, AVPacket *avpkt) { CFHDContext *s = avctx->priv_data; CFHDDSPContext *dsp = &s->dsp; GetByteContext gb; int ret = 0, i, j, plane, got_buffer = 0; int16_t *coeff_data; init_frame_defaults(s); s->planes = av_pix_fmt_count_planes(s->coded_format); bytestream2_init(&gb, avpkt->data, avpkt->size); 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; int8_t tag8 = (int8_t)(tagu >> 8); uint16_t abstag = abs(tag); int8_t abs_tag8 = abs(tag8); uint16_t data = bytestream2_get_be16(&gb); 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); 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 (abstag == VersionMajor) { av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data); } else if (abstag == VersionMinor) { av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data); } else if (abstag == VersionRevision) { av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data); } else if (abstag == VersionEdit) { av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data); } else if (abstag == Version) { av_log(avctx, AV_LOG_DEBUG, "Version %"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 == ChannelCount) { av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data); s->channel_cnt = data; if (data > 4) { av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data); ret = AVERROR_PATCHWELCOME; goto end; } } else if (tag == SubbandCount) { av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data); 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; goto end; } } else if (tag == ChannelNumber) { s->channel_num = data; av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data); if (s->channel_num >= s->planes) { av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n"); ret = AVERROR(EINVAL); goto end; } init_plane_defaults(s); } else if (tag == SubbandNumber) { if (s->subband_num != 0 && data == 1 && (s->transform_type == 0 || s->transform_type == 2)) // hack s->level++; av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data); s->subband_num = data; 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); goto end; } if (s->subband_num > 3) { av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n"); ret = AVERROR(EINVAL); goto end; } } else if (tag == SubbandBand) { av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data); if ((s->transform_type == 0 && data >= SUBBAND_COUNT) || (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) { av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n"); ret = AVERROR(EINVAL); goto end; } if (s->transform_type == 0 || s->transform_type == 2) s->subband_num_actual = data; else av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data); } else if (tag == LowpassPrecision) av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data); else if (tag == Quantization) { s->quantisation = data; av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data); } else if (tag == PrescaleTable) { for (i = 0; i < 8; i++) s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3; av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data); } else if (tag == BandEncoding) { if (!data || data > 5) { av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n"); ret = AVERROR(EINVAL); goto end; } 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); 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); s->plane[s->channel_num].band[0][0].height = data; } else if (tag == SampleType) { s->sample_type = data; av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);