From patchwork Fri Sep 2 14:20:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn van Beurden X-Patchwork-Id: 37612 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:139a:b0:8f:1db5:eae2 with SMTP id w26csp980316pzh; Fri, 2 Sep 2022 07:21:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR4k+YY6EVyU13NL4HMisfbLDVgMe1+4yndVSgs+RJj7T2LFBnuNNkOT8+aWc1Bqg89rgdc1 X-Received: by 2002:a17:906:99c1:b0:6fe:b01d:134 with SMTP id s1-20020a17090699c100b006feb01d0134mr26975688ejn.598.1662128468810; Fri, 02 Sep 2022 07:21:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662128468; cv=none; d=google.com; s=arc-20160816; b=Ne+r702FsL0P80KOPa2wqN+smvcRv2e3aaOu2CtomDUvoZ5AWBrz69MKDWdA4KPJ3m wNq+gsh8QKf0wH1VTiK7kJufzQgwhpr4gV6X4uKQOvQnsnYM/qKN8dgn73zUIwL0sw/Z NEkisUKt0m44d5wMCN58kngQjmCf2K8/AS3G4U83okCuG3S/hHf62edDKPSIXyi49HLd lS0iCo94FO5SWLkcVGpK1vOWxB7eH6WiHm/6GOhfI0qvvB2zqOJWKmwHewNmr6K8tuXm zaPUMUL6HlZVvRjk7lvG/W3ujVWvrofLNRVtofjuWyQjZQYkWHEnXXiJCrULmMJCcaoN eTNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=Rv4ItlyBqWhX9/SA1dlnnYLEJ83zPaEQKS75o6J7zTM=; b=ZMqftOjaMnPYHD0NXCIuwahOg5nBhPZEFr/LbASD8Eo43II5FfYQar78hVlDvldn9J NttpRbLbyIogv8EjPp78lipA+5uR9K92wzBvUFHBKhVrvwRD1H9Rb9l2quE5tQqVSF2X cB7xFmSqG49h3cvTvRavxE/TMLXgG/48aMZp1l1vXRbl+0muQCi7qgbKEhe8ZLNoqzIF C+MoV1TQJYdTNhr/JRpg/D/hnmbM3GAG1CTdyhpgOZVL5Rc94XxtpiNRUo8qFrYnrrzI KBgLKu1YW0pwXoNbM/vrvLdOfkS/eNwt/eSyy9qbd63EvO3qne50iNtN32RC6ph9Bybz KuGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=fg6gaXhs; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id nb15-20020a1709071c8f00b007419cc73107si2049608ejc.757.2022.09.02.07.21.08; Fri, 02 Sep 2022 07:21:08 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=fg6gaXhs; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 48BE368B9A4; Fri, 2 Sep 2022 17:20:59 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CE35368B889 for ; Fri, 2 Sep 2022 17:20:52 +0300 (EEST) Received: by mail-ej1-f44.google.com with SMTP id p16so4165890ejb.9 for ; Fri, 02 Sep 2022 07:20:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=A0kJowUUh1X/w5rhVj+a5kR6sCKC0pfW8lBEV1md2Ew=; b=fg6gaXhsJkUIBCNxLQbYeyw86AfjwyZ0WYaoRqrO+wVxyBp1+NSkCF2GqStFEWcrnq eCjyUhX4JY70TXemHgBhlfCXJeQI4s80E2bpoHQx8mlPADbp9rGzE1J2hMcHSWdSfXam T4aN1LyJYnoYS2S/dsP6t/FmCtAYipxRXjk3c7BnY+1RTcR7sqO3zne69xBgbLMEVZb3 1iRORk5DE7NB4Nqj13FCEdO7wdBMBMhDW4A+m9msoUD1Kh1maxWiA9BGwgHQ9UJcEg17 UEfO65cvTcy2buohl79QysGOfpV/cip+4pul0mfPaRFgCwa+aWs4wruj3SqUDwVg8LWu YJYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=A0kJowUUh1X/w5rhVj+a5kR6sCKC0pfW8lBEV1md2Ew=; b=twA4+F0M2O8UD89NegVslrVQzt5Lp6qA3zKUvAYZtzoZ7lbwpaWV/P4AxTpID2N89d II6Ub4TzNGCImDcGWcsHiwVMRmaq/asaC75bxRQ8QjePZzeTPtduVR0IduZjQFh6/mQs Ji+ap59JNtkvLNWJKiL2uSBYRzhTRcI08KNt9h3ISqOLHoWVnxnZzW9NALdSBT5NS3dV KS3lQ9Eqw+zdVRvw2FKKz38WEqSE5cP2sQrtCKpdh0Iwe6KlnfpAaZu43jXW7De5ivSp 02zWht79Ne4h13fT17O4t1DvKhzchKF5LhEPr4pZY0SKIIiOUf+kDaoaVlKklN/8z59O f/fA== X-Gm-Message-State: ACgBeo0A3WFsFDUBJOZ3TBgnhUeEtnNDOpx2aelRtWLcCYWDn46l2ZrO OzNvbch3BtGZQ2SLdGDxa4ngmyhUTemn9Q== X-Received: by 2002:a17:907:1dd7:b0:73d:d0b6:8b3c with SMTP id og23-20020a1709071dd700b0073dd0b68b3cmr25670811ejc.135.1662128451770; Fri, 02 Sep 2022 07:20:51 -0700 (PDT) Received: from localhost.localdomain (92-64-99-37.biz.kpn.net. [92.64.99.37]) by smtp.googlemail.com with ESMTPSA id d20-20020a17090694d400b007417c5dbfeesm1328814ejy.70.2022.09.02.07.20.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Sep 2022 07:20:51 -0700 (PDT) From: Martijn van Beurden To: ffmpeg-devel@ffmpeg.org Date: Fri, 2 Sep 2022 16:20:27 +0200 Message-Id: <20220902142028.261480-2-mvanb1@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220902142028.261480-1-mvanb1@gmail.com> References: <20220902142028.261480-1-mvanb1@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 1/2] libavcodec/flacdec: Implement decoding of 32 bit-per-sample PCM X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Martijn van Beurden Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: X1bh0Zw8UAJI Add decoding of FLAC files coding for 32 bit-per-sample PCM to libavcodec. --- libavcodec/flac.c | 4 +- libavcodec/flacdec.c | 248 ++++++++++++++++++++++++++++++++++++++---- libavcodec/get_bits.h | 12 ++ libavcodec/mathops.h | 9 ++ 4 files changed, 250 insertions(+), 23 deletions(-) diff --git a/libavcodec/flac.c b/libavcodec/flac.c index 352d663c67..174b4801be 100644 --- a/libavcodec/flac.c +++ b/libavcodec/flac.c @@ -28,7 +28,7 @@ #include "flacdata.h" #include "flac_parse.h" -static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 }; +static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 32 }; static const AVChannelLayout flac_channel_layouts[8] = { AV_CHANNEL_LAYOUT_MONO, @@ -82,7 +82,7 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, /* bits per sample */ bps_code = get_bits(gb, 3); - if (bps_code == 3 || bps_code == 7) { + if (bps_code == 3) { av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sample size code (%d)\n", bps_code); diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c index c5d9e95168..14a9202569 100644 --- a/libavcodec/flacdec.c +++ b/libavcodec/flacdec.c @@ -64,6 +64,9 @@ typedef struct FLACContext { int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples uint8_t *decoded_buffer; unsigned int decoded_buffer_size; + int64_t *decoded_33bps; ///< decoded samples for a 33 bps subframe + uint8_t *decoded_buffer_33bps; + unsigned int decoded_buffer_size_33bps; int buggy_lpc; ///< use workaround for old lavc encoded files FLACDSPContext dsp; @@ -154,6 +157,24 @@ static int allocate_buffers(FLACContext *s) s->stream_info.channels, s->stream_info.max_blocksize, AV_SAMPLE_FMT_S32P, 0); + if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) { + buf_size = av_samples_get_buffer_size(NULL, 1, + s->stream_info.max_blocksize, + AV_SAMPLE_FMT_S64P, 0); + if (buf_size < 0) + return buf_size; + + av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size); + if (!s->decoded_buffer) + return AVERROR(ENOMEM); + + ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL, + s->decoded_buffer_33bps, + 1, + s->stream_info.max_blocksize, + AV_SAMPLE_FMT_S64P, 0); + + } return ret < 0 ? ret : 0; } @@ -331,6 +352,94 @@ static int decode_subframe_fixed(FLACContext *s, int32_t *decoded, return 0; } +static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded, + int pred_order, int bps) +{ + const int blocksize = s->blocksize; + int i; + int ret; + + /* warm up samples */ + for (i = 0; i < pred_order; i++) { + decoded[i] = get_sbits_long(&s->gb, bps); + } + + if ((ret = decode_residuals(s, decoded, pred_order)) < 0) + return ret; + + switch (pred_order) { + case 0: + break; + case 1: + for (i = pred_order; i < blocksize; i++) + decoded[i] += decoded[i-1]; + break; + case 2: + for (i = pred_order; i < blocksize; i++) + decoded[i] = (int64_t)decoded[i] + 2*(int64_t)decoded[i-1] - (int64_t)decoded[i-2]; + break; + case 3: + for (i = pred_order; i < blocksize; i++) + decoded[i] = (int64_t)decoded[i] + 3*(int64_t)decoded[i-1] - 3*(int64_t)decoded[i-2] + (int64_t)decoded[i-3]; + break; + case 4: + for (i = pred_order; i < blocksize; i++) + decoded[i] = (int64_t)decoded[i] + 4*(int64_t)decoded[i-1] - 6*(int64_t)decoded[i-2] + 4*(int64_t)decoded[i-3] - (int64_t)decoded[i-4]; + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); + return AVERROR_INVALIDDATA; + } + + return 0; +} + + +static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded, + int32_t *residual, int pred_order) +{ + const int blocksize = s->blocksize; + int i; + int ret; + + /* warm up samples */ + for (i = 0; i < pred_order; i++) { + decoded[i] = get_sbits64(&s->gb, 33); + av_log(s->avctx, AV_LOG_DEBUG, "warm-up %d = %" PRId64 "\n", i, decoded[i]); + } + + if ((ret = decode_residuals(s, residual, pred_order)) < 0) + return ret; + + switch (pred_order) { + case 0: + for (i = pred_order; i < blocksize; i++) + decoded[i] = residual[i]; + break; + case 1: + for (i = pred_order; i < blocksize; i++) + decoded[i] = residual[i] + decoded[i-1]; + break; + case 2: + for (i = pred_order; i < blocksize; i++) + decoded[i] = residual[i] + 2*decoded[i-1] - decoded[i-2]; + break; + case 3: + for (i = pred_order; i < blocksize; i++) + decoded[i] = residual[i] + 3*decoded[i-1] - 3*decoded[i-2] + decoded[i-3]; + break; + case 4: + for (i = pred_order; i < blocksize; i++) + decoded[i] = residual[i] + 4*decoded[i-1] - 6*decoded[i-2] + 4*decoded[i-3] - decoded[i-4]; + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); + return AVERROR_INVALIDDATA; + } + + return 0; +} + static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32], int order, int qlevel, int len, int bps) { @@ -402,12 +511,53 @@ static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order, return 0; } +static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded, + int32_t *residual, int pred_order) +{ + int i, j, ret; + int coeff_prec, qlevel; + int coeffs[32]; + + /* warm up samples */ + for (i = 0; i < pred_order; i++) { + decoded[i] = get_sbits64(&s->gb, 33); + } + + coeff_prec = get_bits(&s->gb, 4) + 1; + if (coeff_prec == 16) { + av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n"); + return AVERROR_INVALIDDATA; + } + qlevel = get_sbits(&s->gb, 5); + if (qlevel < 0) { + av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n", + qlevel); + return AVERROR_INVALIDDATA; + } + + for (i = 0; i < pred_order; i++) { + coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); + } + + if ((ret = decode_residuals(s, residual, pred_order)) < 0) + return ret; + + for (i = pred_order; i < s->blocksize; i++, decoded++) { + int64_t sum = 0; + for (j = 0; j < pred_order; j++) + sum += (int64_t)coeffs[j] * decoded[j]; + decoded[j] = residual[i] + (sum >> qlevel); + } + + return 0; +} + static inline int decode_subframe(FLACContext *s, int channel) { int32_t *decoded = s->decoded[channel]; int type, wasted = 0; int bps = s->stream_info.bps; - int i, tmp, ret; + int i, ret; if (channel == 0) { if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE) @@ -436,34 +586,63 @@ static inline int decode_subframe(FLACContext *s, int channel) wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb)); bps -= wasted; } - if (bps > 32) { - avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32"); - return AVERROR_PATCHWELCOME; - } //FIXME use av_log2 for types if (type == 0) { - tmp = get_sbits_long(&s->gb, bps); - for (i = 0; i < s->blocksize; i++) - decoded[i] = tmp; + if (bps < 33) { + int32_t tmp = get_sbits_long(&s->gb, bps); + for (i = 0; i < s->blocksize; i++) + decoded[i] = tmp; + } else { + int64_t tmp = get_sbits64(&s->gb, 33); + for (i = 0; i < s->blocksize; i++) + s->decoded_33bps[i] = tmp; + } } else if (type == 1) { - for (i = 0; i < s->blocksize; i++) - decoded[i] = get_sbits_long(&s->gb, bps); + if (bps < 33) { + for (i = 0; i < s->blocksize; i++) + decoded[i] = get_sbits_long(&s->gb, bps); + } else { + for (i = 0; i < s->blocksize; i++) + s->decoded_33bps[i] = get_sbits64(&s->gb, 33); + } } else if ((type >= 8) && (type <= 12)) { - if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0) - return ret; + int order = type & ~0x8; + if (bps < 33) { + if (bps + order <= 32) { + if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0) + return ret; + } else { + if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0) + return ret; + } + } else { + if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0) + return ret; + } } else if (type >= 32) { - if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0) - return ret; + if (bps < 33) { + if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0) + return ret; + } else { + if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0) + return ret; + } } else { av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n"); return AVERROR_INVALIDDATA; } - if (wasted && wasted < 32) { - int i; - for (i = 0; i < s->blocksize; i++) - decoded[i] = (unsigned)decoded[i] << wasted; + if (wasted) { + if (wasted+bps == 33) { + int i; + for (i = 0; i < s->blocksize; i++) + s->decoded_33bps[i] = (uint64_t)decoded[i] << wasted; + } else if (wasted < 32) { + int i; + for (i = 0; i < s->blocksize; i++) + decoded[i] = (unsigned)decoded[i] << wasted; + } } return 0; @@ -554,6 +733,26 @@ static int decode_frame(FLACContext *s) return 0; } +static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len) +{ + int i; + if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) { + for (i = 0; i < len; i++) + decoded[1][i] = decoded[0][i] - decoded_33bps[i]; + } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) { + for (i = 0; i < len; i++) + decoded[0][i] = decoded[1][i] + decoded_33bps[i]; + } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) { + for (i = 0; i < len; i++) { + uint64_t a = decoded[0][i]; + int64_t b = decoded_33bps[i]; + a -= b >> 1; + decoded[0][i] = (a + b); + decoded[1][i] = a; + } + } +} + static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) { @@ -612,9 +811,15 @@ static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame, if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) return ret; - s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, - s->stream_info.channels, - s->blocksize, s->sample_shift); + if (s->stream_info.bps == 32 && s->ch_mode > 0) { + decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize); + s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels, + s->blocksize, s->sample_shift); + } else { + s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, + s->stream_info.channels, + s->blocksize, s->sample_shift); + } if (bytes_read > buf_size) { av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size); @@ -635,6 +840,7 @@ static av_cold int flac_decode_close(AVCodecContext *avctx) FLACContext *s = avctx->priv_data; av_freep(&s->decoded_buffer); + av_freep(&s->decoded_buffer_33bps); return 0; } diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index 992765dc92..52d13b8242 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -596,6 +596,18 @@ static inline int get_sbits_long(GetBitContext *s, int n) return sign_extend(get_bits_long(s, n), n); } +/** + * Read 0-64 bits as a signed integer. + */ +static inline int64_t get_sbits64(GetBitContext *s, int n) +{ + // sign_extend(x, 0) is undefined + if (!n) + return 0; + + return sign_extend64(get_bits64(s, n), n); +} + /** * Show 0-32 bits. */ diff --git a/libavcodec/mathops.h b/libavcodec/mathops.h index f81d21f9c4..8a82d9d086 100644 --- a/libavcodec/mathops.h +++ b/libavcodec/mathops.h @@ -137,6 +137,15 @@ static inline av_const int sign_extend(int val, unsigned bits) } #endif +#ifndef sign_extend64 +static inline av_const int64_t sign_extend64(int64_t val, unsigned bits) +{ + unsigned shift = 8 * sizeof(int64_t) - bits; + union { uint64_t u; int64_t s; } v = { (uint64_t) val << shift }; + return v.s >> shift; +} +#endif + #ifndef zero_extend static inline av_const unsigned zero_extend(unsigned val, unsigned bits) {