From patchwork Tue Jun 20 14:16:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42240 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:be15:b0:121:b37c:e101 with SMTP id ge21csp1427259pzb; Tue, 20 Jun 2023 07:17:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4oNBi2lPjyNS9A4HjZxp0ygsJn8rFl3xEs3Gj1dRRG9kN4d3wVAG6Nu8IBY7JHMTgYC0t5 X-Received: by 2002:a17:907:7f20:b0:988:6f63:e7c5 with SMTP id qf32-20020a1709077f2000b009886f63e7c5mr5153205ejc.4.1687270638087; Tue, 20 Jun 2023 07:17:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687270638; cv=none; d=google.com; s=arc-20160816; b=aFD0CuGJBMGVFx6jjugIXZ68dah2itXliH9T7vKWlZwYXoPOfSrKkVWxKqi2t0iZNe Sx+6h9dzDoEqnjnbxwFaxKl+ZeeTdunUnm6+9yT3V0oz76i312DApXVDFbkX7T6JIDG5 YIiJUFTx77UJXqksGjZNHjqV0PsMXD82oCce5OOjm1eLbY27ZYeSnLyAKVfOGLelinbb v19UimWu/MB9zRQFbyK+RBTbMO9QUVBsKCk9oy/brWyUavSE7qEDvsR2diAbdmOSuILH Nlo+PrsiGZL+95qLtTCWUVuJPuQCKgDkBb/Vxrg39g9WhpE4BLUVfOrcg2JAFBCPeFy+ MrEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding: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:delivered-to; bh=mGZJeInUU2ZWhAobK+g1OuC2EKG3K9FJoZkvHdqjYu0=; b=gPo1m5MBYYPhjKo+1O7cZYSolUNmm4sUESOWPSRWbaQAWYCws7EeN73i+Ik5drPUWM XYM+EqBgGyZFee+sblU97UQn947OMn1HPrrYmD9bv1cW/VAX7/BGyNLrNGgfazr3/a/j TKtB1cK36uw3iQLosNw0YRig/wynw3AlLZwPue+/cTBiiLvMlObiGvDwC1llEb668LPa ZG4p/ZEB9Xw7aTrEjyk7LbGfllJW9kWyw4XKoihQpRjP8KybobNHW1LE8L777zoYMrls n4ukq6m/m5+/w3qAaJleOqdrLG9E2ITd3t9oNSse0aeKXiCyWsPtq3YtO+vaAdZWbH84 UVFw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id c1-20020a17090654c100b00988812e1529si1286951ejp.396.2023.06.20.07.17.17; Tue, 20 Jun 2023 07:17:18 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 82ACB68C165; Tue, 20 Jun 2023 17:16:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D923068A892 for ; Tue, 20 Jun 2023 17:16:43 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id A45E32404EE for ; Tue, 20 Jun 2023 16:16:43 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id c-a1m2XmO6Fq for ; Tue, 20 Jun 2023 16:16:43 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 8E9C72404EC for ; Tue, 20 Jun 2023 16:16:42 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id D2F073A10A9 for ; Tue, 20 Jun 2023 16:16:36 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Jun 2023 16:16:03 +0200 Message-Id: <20230620141608.31759-4-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230620141608.31759-1-anton@khirnov.net> References: <20230620141608.31759-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/9] lavc: add generic-decode-layer private data 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: MpY745EcKnJ4 Move AVCodecInternal.nb_draining_errors to it, should should not be visible outside of decode.c. --- libavcodec/avcodec.c | 4 +++- libavcodec/avcodec_internal.h | 2 ++ libavcodec/decode.c | 22 ++++++++++++++++++++-- libavcodec/internal.h | 10 +++++++--- libavcodec/pthread_frame.c | 3 ++- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index a5cb6035b6..ff251d2dae 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -150,7 +150,9 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE) return AVERROR(EINVAL); - avci = av_mallocz(sizeof(*avci)); + avci = av_codec_is_decoder(codec) ? + ff_decode_internal_alloc() : + av_mallocz(sizeof(AVCodecInternal)); if (!avci) { ret = AVERROR(ENOMEM); goto end; diff --git a/libavcodec/avcodec_internal.h b/libavcodec/avcodec_internal.h index 6ffe575c3e..f52f91e07c 100644 --- a/libavcodec/avcodec_internal.h +++ b/libavcodec/avcodec_internal.h @@ -53,4 +53,6 @@ int ff_decode_preinit(struct AVCodecContext *avctx); void ff_decode_flush_buffers(struct AVCodecContext *avctx); void ff_encode_flush_buffers(struct AVCodecContext *avctx); +struct AVCodecInternal *ff_decode_internal_alloc(void); + #endif // AVCODEC_AVCODEC_INTERNAL_H diff --git a/libavcodec/decode.c b/libavcodec/decode.c index b5e5b4a2db..c070148b58 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -50,6 +50,11 @@ #include "internal.h" #include "thread.h" +struct DecodeContext { + /* to prevent infinite loop on errors when draining */ + int nb_draining_errors; +}; + static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt) { int ret; @@ -439,7 +444,7 @@ FF_ENABLE_DEPRECATION_WARNINGS int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ? avctx->thread_count : 1); - if (avci->nb_draining_errors++ >= nb_errors_max) { + if (avci->d->nb_draining_errors++ >= nb_errors_max) { av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. " "Stop draining and force EOF.\n"); avci->draining_done = 1; @@ -1752,5 +1757,18 @@ void ff_decode_flush_buffers(AVCodecContext *avctx) av_bsf_flush(avci->bsf); - avci->nb_draining_errors = 0; + avci->d->nb_draining_errors = 0; +} + +AVCodecInternal *ff_decode_internal_alloc(void) +{ + struct Dummy { + AVCodecInternal i; + DecodeContext d; + } *dummy = av_mallocz(sizeof(*dummy)); + if (!dummy) + return NULL; + dummy->i.d = &dummy->d; + + return &dummy->i; } diff --git a/libavcodec/internal.h b/libavcodec/internal.h index dceae182c0..b672092ac4 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -49,7 +49,14 @@ # define STRIDE_ALIGN 8 #endif +typedef struct DecodeContext DecodeContext; + typedef struct AVCodecInternal { + /** + * Generic decoding private data. + */ + DecodeContext *d; + /** * When using frame-threaded decoding, this field is set for the first * worker thread (e.g. to decode extradata just once). @@ -148,9 +155,6 @@ typedef struct AVCodecInternal { AVFrame *buffer_frame; int draining_done; - /* to prevent infinite loop on errors when draining */ - int nb_draining_errors; - /* used when avctx flag AV_CODEC_FLAG_DROPCHANGED is set */ int changed_frames_dropped; int initial_format; diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index 008f3da43b..bc305f561f 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -28,6 +28,7 @@ #include #include "avcodec.h" +#include "avcodec_internal.h" #include "codec_internal.h" #include "decode.h" #include "hwconfig.h" @@ -815,7 +816,7 @@ static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, p->parent = fctx; p->avctx = copy; - copy->internal = av_mallocz(sizeof(*copy->internal)); + copy->internal = ff_decode_internal_alloc(); if (!copy->internal) return AVERROR(ENOMEM); copy->internal->thread_ctx = p;