From patchwork Sat Jan 8 14:24:37 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: 33162 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp1250643iog; Sat, 8 Jan 2022 06:24:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJyySwKiQmFjEOjqd/2DW4Rn1yOvFiAr97upr9MQUl5SIDLsqsrxbhZ/L1QkIbN8c0Cw58ME X-Received: by 2002:a17:906:c156:: with SMTP id dp22mr55479410ejc.36.1641651896809; Sat, 08 Jan 2022 06:24:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641651896; cv=none; d=google.com; s=arc-20160816; b=uUx/0mbAZ+72XZZd+8mGw/L8Hbq172Kt9iTfoudEK/dMvt/F7XiomuYB7U/FS8ToNC X2a1UuiA35PKx5PZbgbC5j7gsMAJtq9itBCdwBkAOAoNP6h8/3R4JsXVxfoCjWqrhLf3 k9m/7jxGGIoAEyHapBNq7xpD/x271v5OnBjwTZ3lyAtX4ZCbfYnVELgTGVYkFFUhjujH 3QkzVx0V/GKed4mTE2LbUEmpuZOStkDtVP35DDAjInh6fm7l+hq3hLt/j6bez8GU27ST wSg/pBJ4qqH3Jtj8mDoXXKaB6E79jfOhuzpmoqj1/i/X7PB9/lx8OCvnvH2aMTPBVfdf jj6A== 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=PpQNg1qc0ERFmLg2Bx/zEVNyTOOwg4zckr9lgMVnp10=; b=DMVji8ib5YZiBO3HA6rszAA9evA14hZ2np1lu89eRgAcHwr6ysgaTQqGozhG/BGn4B WqJZcL8LUBCJ3wt3tWPLu7iSqZyp1m/4MV/pHoEpCG038M42w5oqSQ7UHqTt/4cTppM2 9VZlGrJIhRKf0M5g4tS+9PV2cBiOjXEh9H+e7otqFNkw6C6q5r8wtoseWH4RAakjPXTe o6dDOKVvM9CnrUpuDU/BaXaQBn0/1kjxG7jvrubsmrYzbC9gFTbYRkJY/SK8cFxFglig NakidcEWlHeT4k4Jrg9QpkE1LlOf/I9w3fUvXdIqg4YPqtk/xgDlUEGCywleQRouaMiL NwiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=R8T9cG9Y; 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 w20si813353edt.492.2022.01.08.06.24.55; Sat, 08 Jan 2022 06:24:56 -0800 (PST) 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=R8T9cG9Y; 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 D129168A927; Sat, 8 Jan 2022 16:24:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 639DE68A7ED for ; Sat, 8 Jan 2022 16:24:45 +0200 (EET) Received: by mail-ed1-f52.google.com with SMTP id j6so33662836edw.12 for ; Sat, 08 Jan 2022 06:24:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=P5Vv3RuX59NZSReVsE11Ip5Ia9M1SHTSKo3ef6r20mM=; b=R8T9cG9YMcB6yoGLy6liAKxhFfBboVfE7uGEu+7jio4fnlkhRB0SVoTVUdAjLVOYM5 SfEHHm99LYjObDwH/57VqGD0OtN6wn2DQoz5DoEBc/g881V5wpWkShE//+jIjulz8d8i rpwVgFLNM9iw8USa1I/wF90BWqzmI0hcqfQIHF/CFPaii4dMx1tHI9D7wSx7PAkY0Gph OZP+YSGaxQeN3/kX1hGMVDhQJK+JLXXXEzrtB9U2xcQeynK/A9IXsRgZhCZIArILnoWF zI+5zYFSFv6MZM5IcQ5KE+8Mt3NOrMGL/RyiIHgFuwar8JaYIS8IhEjqeBf8/682TItZ Vbmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=P5Vv3RuX59NZSReVsE11Ip5Ia9M1SHTSKo3ef6r20mM=; b=K9F6I8UCfBMEphUoJhlaUfAY3OKp/UIq+nIRDL9xC0BZJW7x+GNZ50OJjUIcA973RZ FAoiCo6Pq3L2dsuqrMTQ6mOpkX9/YOF3MHYrjO94I4PKEZ35B04XuMVoyB79sc4o9TMt y5s6fcxF/OmtD/vYYYfff6Bia2G5ASxsbjZg97BnnUJUJSj9rO3Q3AjXFU8r5gJFFGNK mPVNplMTVbJG/a1uiw3kA6TDeSSE1AhhbktiHQq2HyzY7d5k2wd9i3AUCGhbxGfQU+g+ FJmBKsDldWZA+E7c9toyJ0fi6WSqVjv6GR0dxirgOoR6Oi9+DyVMbOBA55h9WOquPrwQ x96A== X-Gm-Message-State: AOAM530LrWKFc5cZ3YGCuyYf7inAhlKAW0A6K+FW4P5EmvUIzqk8mdPf WHRXxLmuG8ccH0M1VHxu+ro5pD9uv2Y= X-Received: by 2002:a50:ed06:: with SMTP id j6mr14531012eds.21.1641651884177; Sat, 08 Jan 2022 06:24:44 -0800 (PST) Received: from localhost.localdomain (92-64-99-37.biz.kpn.net. [92.64.99.37]) by smtp.googlemail.com with ESMTPSA id la21sm550104ejc.137.2022.01.08.06.24.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jan 2022 06:24:43 -0800 (PST) From: Martijn van Beurden To: ffmpeg-devel@ffmpeg.org Date: Sat, 8 Jan 2022 15:24:37 +0100 Message-Id: <20220108142437.756529-1-mvanb1@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4] libavcodec/flacenc: add backward-compatible 32 bit-per-sample capability 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: 2V4h8gqqv+bT Enables creation of FLAC files with up to 32 bits-per-sample, up from the previous limit of 24 bit. This is a feature requested for RAWcooked, the archiving community has a need for storing files with 32-bit integer audio samples. See https://github.com/MediaArea/RAWcooked/issues/356 Restrictions to the encoder are added so created files are compatible with existing decoders. Stereo decorrelation is disabled on 32 bit-per-sample, because a side channel would need 33 bit-per-sample, causing problems in existing 32 bit datapaths. Also only LPC encoding is enabled, because decoders capable of processing 24-bit files already use 64-bit processing for LPC, but not for fixed subframes. Furthermore, predictions and residuals are checked for causing integer overflow, reverting to a verbatim (store) subframe in case no LPC coeffs can be found that do not cause overflow. ffmpeg's FLAC decoder has been forward-compatible with this change since commit c720b9ce98 (May 2015). libFLAC is forward-compatible since release 1.2.1 (September 2007), the flac command line tool however blocks 32-bit files out of caution, it having been untested until now. To create 32 bit files, -bits_per_raw_sample 32 must be specified because of trac ticket 9563 --- libavcodec/flacenc.c | 138 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 15 deletions(-) diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c index 9f6f449323..bec3bf2ca5 100644 --- a/libavcodec/flacenc.c +++ b/libavcodec/flacenc.c @@ -49,6 +49,9 @@ #define MIN_LPC_SHIFT 0 #define MAX_LPC_SHIFT 15 +#define ZIGZAG_32BIT_MAX 0x3FFFFFFF +#define ZIGZAG_32BIT_MIN -0x3FFFFFFF + enum CodingMode { CODING_MODE_RICE = 4, CODING_MODE_RICE2 = 5, @@ -254,10 +257,30 @@ static av_cold int flac_encode_init(AVCodecContext *avctx) s->bps_code = 4; break; case AV_SAMPLE_FMT_S32: - if (avctx->bits_per_raw_sample != 24) - av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n"); - avctx->bits_per_raw_sample = 24; - s->bps_code = 6; + if (avctx->bits_per_raw_sample <= 24) { + if (avctx->bits_per_raw_sample < 24) + av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n"); + avctx->bits_per_raw_sample = 24; + s->bps_code = 6; + } else { + av_log(avctx, AV_LOG_WARNING, "non-streamable bits-per-sample\n"); + s->bps_code = 0; + if (avctx->bits_per_raw_sample == 0) + avctx->bits_per_raw_sample = 32; + if (s->options.lpc_type != FF_LPC_TYPE_LEVINSON && + s->options.lpc_type != FF_LPC_TYPE_CHOLESKY) { + av_log(avctx, AV_LOG_WARNING, "forcing lpc_type levinson, lpc_type fixed or none not supported with >24 bits-per-sample FLAC\n"); + s->options.lpc_type = FF_LPC_TYPE_LEVINSON; + } + if (avctx->bits_per_raw_sample == 32) { + /* Because stereo decorrelation can raise the bitdepth of + * a subframe to 33 bits, we disable it */ + if (s->options.ch_mode != FLAC_CHMODE_INDEPENDENT) { + av_log(avctx, AV_LOG_WARNING, "disabling stereo decorrelation, not supported with 32 bits-per-sample FLAC\n"); + s->options.ch_mode = FLAC_CHMODE_INDEPENDENT; + } + } + } break; } @@ -686,7 +709,7 @@ static uint64_t calc_rice_params(RiceContext *rc, tmp_rc.coding_mode = rc->coding_mode; - for (i = 0; i < n; i++) + for (i = pred_order; i < n; i++) udata[i] = (2 * data[i]) ^ (data[i] >> 31); calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums); @@ -784,6 +807,47 @@ static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, } } +static int lpc_encode_with_overflow_detect(int32_t *res, const int32_t *smp, int len, + int order, int32_t *coefs, int shift) +{ + /* This function checks for every prediction and every residual + * whether they cause integer overflow in existing decoders. In + * case the prediction exceeds int32_t limits, prediction + * coefficients are lowered accordingly. If the residual exceeds + * ZIGZAG_32BIT_MAX and _MIN or coefficients have been lowered + * twice but the prediction still overflows, give up */ + int lpc_reduction_tries = 0; + int64_t pmax; + for (int i = 0; i < order; i++) + res[i] = smp[i]; + do { + pmax = 0; + for (int i = order; i < len; i++) { + int64_t p = 0, tmp; + for (int j = 0; j < order; j++) + p += (int64_t)coefs[j]*smp[(i-1)-j]; + p >>= shift; + tmp = smp[i] - p; + if (p > INT32_MAX && p > pmax) + pmax = p; + else if (p < INT32_MIN && (p * -1) > pmax) + pmax = p * -1; + if (tmp > ZIGZAG_32BIT_MAX || tmp < ZIGZAG_32BIT_MIN) + return 1; + res[i] = tmp; + } + + if (pmax > 0) { + if (lpc_reduction_tries >= 2) + return 1; + lpc_reduction_tries++; + for (int i = 0; i < order; i++) + coefs[i] = ((int64_t)coefs[i] * INT32_MAX) / pmax; + } + } while (pmax > 0); + return 0; +} + static int encode_residual_ch(FlacEncodeContext *s, int ch) { @@ -868,7 +932,11 @@ static int encode_residual_ch(FlacEncodeContext *s, int ch) order = av_clip(order, min_order - 1, max_order - 1); if (order == last_order) continue; - if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(order) <= 32) { + if (s->avctx->bits_per_raw_sample > 24) { + if (lpc_encode_with_overflow_detect(res, smp, n, order+1, + coefs[order], shift[order])) + continue; + } else if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(order) <= 32) { s->flac_dsp.lpc16_encode(res, smp, n, order+1, coefs[order], shift[order]); } else { @@ -888,7 +956,11 @@ static int encode_residual_ch(FlacEncodeContext *s, int ch) opt_order = 0; bits[0] = UINT32_MAX; for (i = min_order-1; i < max_order; i++) { - if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) { + if (s->avctx->bits_per_raw_sample > 24) { + if (lpc_encode_with_overflow_detect(res, smp, n, i+1, + coefs[i], shift[i])) + continue; + } else if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) { s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]); } else { s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]); @@ -910,7 +982,11 @@ static int encode_residual_ch(FlacEncodeContext *s, int ch) for (i = last-step; i <= last+step; i += step) { if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX) continue; - if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) { + if (s->avctx->bits_per_raw_sample > 24) { + if (lpc_encode_with_overflow_detect(res, smp, n, i+1, + coefs[i], shift[i])) + continue; + } else if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) { s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]); } else { s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]); @@ -951,7 +1027,11 @@ static int encode_residual_ch(FlacEncodeContext *s, int ch) if (diffsum >8) continue; - if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order - 1) <= 32) { + if (s->avctx->bits_per_raw_sample > 24) { + if (lpc_encode_with_overflow_detect(res, smp, n, opt_order, + lpc_try, shift[opt_order-1])) + continue; + } else if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order-1) <= 32) { s->flac_dsp.lpc16_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]); } else { s->flac_dsp.lpc32_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]); @@ -972,7 +1052,16 @@ static int encode_residual_ch(FlacEncodeContext *s, int ch) for (i = 0; i < sub->order; i++) sub->coefs[i] = coefs[sub->order-1][i]; - if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order) <= 32) { + if (s->avctx->bits_per_raw_sample > 24) { + if (lpc_encode_with_overflow_detect(res, smp, n, sub->order, + sub->coefs, sub->shift)) { + /* No coefs found that do not cause integer overflow, + * so return a verbatim subframe instead */ + sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; + memcpy(res, smp, n * sizeof(int32_t)); + return subframe_count_exact(s, sub, 0); + } + } else if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(sub->order) <= 32) { s->flac_dsp.lpc16_encode(res, smp, n, sub->order, sub->coefs, sub->shift); } else { s->flac_dsp.lpc32_encode(res, smp, n, sub->order, sub->coefs, sub->shift); @@ -1227,12 +1316,22 @@ static void write_subframes(FlacEncodeContext *s) if (sub->type == FLAC_SUBFRAME_CONSTANT) { put_sbits(&s->pb, sub->obits, res[0]); } else if (sub->type == FLAC_SUBFRAME_VERBATIM) { - while (res < frame_end) - put_sbits(&s->pb, sub->obits, *res++); + if (sub->obits < 32) { + while (res < frame_end) + put_sbits(&s->pb, sub->obits, *res++); + } else { + while (res < frame_end) + put_bits32(&s->pb, *res++); + } } else { /* warm-up samples */ - for (i = 0; i < sub->order; i++) - put_sbits(&s->pb, sub->obits, *res++); + if (sub->obits < 32) { + for (i = 0; i < sub->order; i++) + put_sbits(&s->pb, sub->obits, *res++); + } else { + for (i = 0; i < sub->order; i++) + put_bits32(&s->pb, *res++); + } /* LPC coefficients */ if (sub->type == FLAC_SUBFRAME_LPC) { @@ -1305,7 +1404,7 @@ static int update_md5_sum(FlacEncodeContext *s, const void *samples) (const uint16_t *) samples, buf_size / 2); buf = s->md5_buffer; #endif - } else { + } else if (s->avctx->bits_per_raw_sample <= 24) { int i; const int32_t *samples0 = samples; uint8_t *tmp = s->md5_buffer; @@ -1315,6 +1414,15 @@ static int update_md5_sum(FlacEncodeContext *s, const void *samples) AV_WL24(tmp + 3*i, v); } buf = s->md5_buffer; + } else { + /* s->avctx->bits_per_raw_sample <= 32 */ + int i; + const int32_t *samples0 = samples; + uint8_t *tmp = s->md5_buffer; + + for (i = 0; i < s->frame.blocksize * s->channels; i++) + AV_WL32(tmp + 4*i, samples0[i]); + buf = s->md5_buffer; } av_md5_update(s->md5ctx, buf, buf_size);