From patchwork Thu Dec 3 00:36:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 24329 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id EC4C744B751 for ; Thu, 3 Dec 2020 02:37:05 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DAB13689E8A; Thu, 3 Dec 2020 02:37:05 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6A827689DFA for ; Thu, 3 Dec 2020 02:36:57 +0200 (EET) Received: by mail-ed1-f67.google.com with SMTP id r5so161153eda.12 for ; Wed, 02 Dec 2020 16:36:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=jquhM3U5VdwiPtaLNc9qSwF4kzAPkzn1nl5YA2kBbGQ=; b=iK4B3IjIvDrEwSQNs1cDAuVk2U78OEZQqAnz74cUAgUL0iPwqN/CoAy0MxlAW0vH4M SJ0H9ZcDLao2ka2StjF9dxP4CLjpyXwfu3JNsy/bqe3tFP0OpRmTSNgrirZhRBcQwywu UJFQBmc2O1zpoF/UOKJQnZE9AIEkBtLSD7OKWiOpcOTx6ja65XukUnWSyv5HzcKm00sl 45Obtcme+YTlDXp8C3ZP6u48MItNZeKk02W6PwqRTQggbkatP6ITW67YvmRo81r/08Mw zzo0UO75ocXDfDzLRNa5MQbYePvRuaRzm1R2wYoBQ1WDjPK3NWRSdPxizbCULbs4yryU UInA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=jquhM3U5VdwiPtaLNc9qSwF4kzAPkzn1nl5YA2kBbGQ=; b=qPE/aTFb4CoyRasFIFeCw1y2C/y7ykW41AsyN157kf2z7qZja7EldYgoeCHQncwu0N lUZA1+CudPc6Jxa4k3Jv3iVBsojyNSkBxAUomRetcIgLZ5aed7ewj9aaHTG29Ncm3Vsj hYvcdCkzlrCywiVfVa0Hm7ohdkkTq9YkNiLrbGybiEuk/oAPY6syqroQ3AYd7UAk2RDQ EWjET4howhoj+R31W/8n2O+fD8m5Ua0I+MYQY2o4Bh1wYFabtLazUo5G2hk8MBUo5L4I srHRgCNSxX+3/+qrlbMKN0mLUnxreAB+eWuvscRASKHzc7963ai0s2Q5HyRV4oBTj9rg 6Beg== X-Gm-Message-State: AOAM532KFjJi/59eJeFTvnqsD0V5Y8uYtMjANXoApMYpR2GKIX/REati LoD+sc8mfpKZwD3HyOpk/XKs5VkjgfG4XA== X-Google-Smtp-Source: ABdhPJwyo2n5It0rjdmpWSTFXyWhdWTAVYRUctcosu5s3eap8yxxuXz/qUfUgGTsCEFJSQbiAv688g== X-Received: by 2002:a50:e68a:: with SMTP id z10mr601587edm.28.1606955816586; Wed, 02 Dec 2020 16:36:56 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id c12sm230777edw.55.2020.12.02.16.36.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2020 16:36:56 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 3 Dec 2020 01:36:28 +0100 Message-Id: <20201203003628.778278-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201127010249.2724610-1-andreas.rheinhardt@gmail.com> References: <20201127010249.2724610-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 223/223] avcodec/mlpenc: Make encoders init-threadsafe X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Andreas Rheinhardt --- libavcodec/mlpenc.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/libavcodec/mlpenc.c b/libavcodec/mlpenc.c index dfd1cc8d60..b61f69d4a5 100644 --- a/libavcodec/mlpenc.c +++ b/libavcodec/mlpenc.c @@ -27,6 +27,7 @@ #include "libavutil/crc.h" #include "libavutil/avstring.h" #include "libavutil/samplefmt.h" +#include "libavutil/thread.h" #include "mlp.h" #include "lpc.h" @@ -403,11 +404,11 @@ static void copy_restart_frame_params(MLPEncodeContext *ctx, } /** Clears a DecodingParams struct the way it should be after a restart header. */ -static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS]) +static void clear_decoding_params(DecodingParams decoding_params[MAX_SUBSTREAMS], int num_substreams) { unsigned int substr; - for (substr = 0; substr < ctx->num_substreams; substr++) { + for (substr = 0; substr < num_substreams; substr++) { DecodingParams *dp = &decoding_params[substr]; dp->param_presence_flags = 0xff; @@ -419,11 +420,11 @@ static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding } /** Clears a ChannelParams struct the way it should be after a restart header. */ -static void clear_channel_params(MLPEncodeContext *ctx, ChannelParams channel_params[MAX_CHANNELS]) +static void clear_channel_params(ChannelParams channel_params[MAX_CHANNELS], int nb_channels) { unsigned int channel; - for (channel = 0; channel < ctx->avctx->channels; channel++) { + for (channel = 0; channel < nb_channels; channel++) { ChannelParams *cp = &channel_params[channel]; memset(&cp->filter_params, 0, sizeof(cp->filter_params)); @@ -441,7 +442,7 @@ static void default_decoding_params(MLPEncodeContext *ctx, { unsigned int substr; - clear_decoding_params(ctx, decoding_params); + clear_decoding_params(decoding_params, ctx->num_substreams); for (substr = 0; substr < ctx->num_substreams; substr++) { DecodingParams *dp = &decoding_params[substr]; @@ -484,8 +485,16 @@ static int mlp_peak_bitrate(int peak_bitrate, int sample_rate) return ((peak_bitrate << 4) - 8) / sample_rate; } +static av_cold void mlp_encode_init_static(void) +{ + clear_channel_params (restart_channel_params, MAX_CHANNELS); + clear_decoding_params(restart_decoding_params, MAX_SUBSTREAMS); + ff_mlp_init_crc(); +} + static av_cold int mlp_encode_init(AVCodecContext *avctx) { + static AVOnce init_static_once = AV_ONCE_INIT; MLPEncodeContext *ctx = avctx->priv_data; unsigned int substr, index; unsigned int sum = 0; @@ -607,8 +616,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } - ff_mlp_init_crc(); - ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) { @@ -726,9 +733,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) rh->max_matrix_channel = rh->max_channel; } - clear_channel_params(ctx, restart_channel_params); - clear_decoding_params(ctx, restart_decoding_params); - if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->number_of_samples, MLP_MAX_LPC_ORDER, FF_LPC_TYPE_LEVINSON)) < 0) { av_log(avctx, AV_LOG_ERROR, @@ -738,6 +742,8 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) ff_af_queue_init(avctx, &ctx->afq); + ff_thread_once(&init_static_once, mlp_encode_init_static); + return 0; } @@ -2331,7 +2337,7 @@ input_and_return: ctx->number_of_samples = number_of_samples; for (index = 0; index < ctx->seq_size[seq_index]; index++) { - clear_channel_params(ctx, ctx->seq_channel_params + index*(ctx->avctx->channels)); + clear_channel_params(ctx->seq_channel_params + index * ctx->avctx->channels, ctx->avctx->channels); default_decoding_params(ctx, ctx->seq_decoding_params + index*(ctx->num_substreams)); } @@ -2393,7 +2399,7 @@ AVCodec ff_mlp_encoder = { .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE}, .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0}, .channel_layouts = ff_mlp_channel_layouts, - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, }; #endif #if CONFIG_TRUEHD_ENCODER @@ -2410,6 +2416,6 @@ AVCodec ff_truehd_encoder = { .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE}, .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0}, .channel_layouts = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0}, - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, }; #endif