From patchwork Thu Sep 3 04:03:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 22072 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 0960044B0D2 for ; Thu, 3 Sep 2020 07:04:17 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E62E668AAFD; Thu, 3 Sep 2020 07:04:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f196.google.com (mail-qk1-f196.google.com [209.85.222.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CACEF68AA90 for ; Thu, 3 Sep 2020 07:04:10 +0300 (EEST) Received: by mail-qk1-f196.google.com with SMTP id f2so1950290qkh.3 for ; Wed, 02 Sep 2020 21:04:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=McKFGqkEdwhqW6K7/f+lxrIceT8SzDVU4+W9SxZyYIU=; b=JuekLIbHOoeZ0i5xI76L52IOz3LbNRnDxVWG9eSB/2WfBB5w7B2jnR8nEh6m9AFbiw 0W3wIj/pM5XEKLIClPhhJgatyrkwjRAgsO9+i5jOQBsc17X3fS1WSAhOThUuzOJjsLTH DM01PXV83MHLyYuFc2rxnoSfB17aebok+prPOp4SNzVAV2p1DIkdsKEfh31WdxR8gPFG uvx+b9XTRWfq3XuT4aSvTZQhTEe5986adaId+x4TBbaXwooNnvzlbf+qqD+g0L9LMJ4n fd8KeLpTSViMok+NbysIBlYkPCbz4In8pBeRvc2Y02n6VnGqd/1d7pWpH4OkB1FxfVNo xISw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=McKFGqkEdwhqW6K7/f+lxrIceT8SzDVU4+W9SxZyYIU=; b=PeGxeGQWpii/eTvD81scX4UY1LJwUkSBOVEbHiYY0Ok9tAx1yx5laL0jz4+6DHh1Ru 4U7Q2z5PL4SuK8bmwSA3Fg0lMe3bIVV6KP1jF/oXu+KalsbPwAO/rqrlZKq/HW5I1H9b tLxjigbKs2OwXKOmSdIIdjuPPmbRZdlT5XOW8vJPdEfrkkOmeZUITodpT6DRHjtkyknR 4+Ryl4Opz2ixmFHZfh2VoP2zBY1ApTs/2QAfbwC5YBnsu8MBEnVKwZEvyYkxjkllisLK 06nCEpUh3XIiGhltrkN2BmQ5O6yr2kRKCvHhGm3YnFDJnXdBCsnXDBVL4u+6L3MMyPfj 5d4w== X-Gm-Message-State: AOAM530XlL2F7+zqb41/6SRjbPXsGkntuhPlvSm+x9uweLQ09cYFOqvN GfzJzq4CzEmf/IUdZ3HA7sGx82+jKWM= X-Google-Smtp-Source: ABdhPJw1I/N0mZ7n/6fic+eo3EH3Kbcz9Ig25YZxEfTwfFdahjJShr8IvMa6qkcsSTnVObYU1Tpk0w== X-Received: by 2002:a37:ba06:: with SMTP id k6mr1413904qkf.64.1599105846113; Wed, 02 Sep 2020 21:04:06 -0700 (PDT) Received: from localhost.localdomain ([181.23.70.161]) by smtp.gmail.com with ESMTPSA id z14sm1092268qtn.92.2020.09.02.21.04.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Sep 2020 21:04:05 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 3 Sep 2020 01:03:11 -0300 Message-Id: <20200903040311.53886-3-jamrial@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200903040311.53886-1-jamrial@gmail.com> References: <20200903040311.53886-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3 v2] avcodec/decode: use a packet list to store packet properties 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Keeping only the latest packet fed to the decoder works only for decoders that return a frame immediately after every consumed packet. Decoders that consume several packets before they return a frame will fill said frame with properties taken from the last consumed packet instead of the earliest. Signed-off-by: James Almer --- libavcodec/decode.c | 36 +++++++++++++++++++++++++++--------- libavcodec/internal.h | 2 ++ libavcodec/utils.c | 3 +++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index da587ac1f6..0b6930b9c2 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -43,6 +43,7 @@ #include "decode.h" #include "hwconfig.h" #include "internal.h" +#include "packet_internal.h" #include "thread.h" typedef struct FramePool { @@ -142,15 +143,24 @@ fail2: return 0; } -static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt) +#define IS_EMPTY(pkt) (!(pkt)->data) + +static int extract_packet_props(AVCodecInternal *avci, AVPacket *pkt) { int ret = 0; - av_packet_unref(avci->last_pkt_props); - if (pkt) { - ret = av_packet_copy_props(avci->last_pkt_props, pkt); - if (!ret) - avci->last_pkt_props->size = pkt->size; // HACK: Needed for ff_decode_frame_props(). + ret = avpriv_packet_list_put(&avci->pkt_props, &avci->pkt_props_tail, pkt, + FF_PACKETLIST_FLAG_COPY_PROPS); + if (ret < 0) + return ret; + avci->pkt_props_tail->pkt.size = pkt->size; // HACK: Needed for ff_decode_frame_props(). + avci->pkt_props_tail->pkt.data = (void*)1; // HACK: Needed for IS_EMPTY(). + + if (IS_EMPTY(avci->last_pkt_props)) { + ret = avpriv_packet_list_get(&avci->pkt_props, + &avci->pkt_props_tail, + avci->last_pkt_props); + av_assert0(ret != AVERROR(EAGAIN)); } return ret; } @@ -512,6 +522,7 @@ FF_ENABLE_DEPRECATION_WARNINGS if (ret >= pkt->size || ret < 0) { av_packet_unref(pkt); + av_packet_unref(avci->last_pkt_props); } else { int consumed = ret; @@ -550,9 +561,11 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) av_assert0(!frame->buf[0]); - if (avctx->codec->receive_frame) + if (avctx->codec->receive_frame) { ret = avctx->codec->receive_frame(avctx, frame); - else + if (ret != AVERROR(EAGAIN)) + av_packet_unref(avci->last_pkt_props); + } else ret = decode_simple_receive_frame(avctx, frame); if (ret == AVERROR_EOF) @@ -1683,7 +1696,7 @@ static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame) int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame) { - const AVPacket *pkt = avctx->internal->last_pkt_props; + AVPacket *pkt = avctx->internal->last_pkt_props; int i; static const struct { enum AVPacketSideDataType packet; @@ -1701,6 +1714,11 @@ int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame) { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE }, }; + if (IS_EMPTY(pkt)) + avpriv_packet_list_get(&avctx->internal->pkt_props, + &avctx->internal->pkt_props_tail, + pkt); + if (pkt) { frame->pts = pkt->pts; #if FF_API_PKT_PTS diff --git a/libavcodec/internal.h b/libavcodec/internal.h index 5d0e6e7831..ce4dbbc2b9 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -145,6 +145,8 @@ typedef struct AVCodecInternal { * for decoding. */ AVPacket *last_pkt_props; + AVPacketList *pkt_props; + AVPacketList *pkt_props_tail; /** * temporary buffer used for encoders to store their bitstream diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 14cb5cf1aa..4a180a1500 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -50,6 +50,7 @@ #include "thread.h" #include "frame_thread_encoder.h" #include "internal.h" +#include "packet_internal.h" #include "put_bits.h" #include "raw.h" #include "bytestream.h" @@ -1149,6 +1150,8 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_packet_free(&avctx->internal->compat_encode_packet); av_packet_free(&avctx->internal->buffer_pkt); av_packet_free(&avctx->internal->last_pkt_props); + avpriv_packet_list_free(&avctx->internal->pkt_props, + &avctx->internal->pkt_props_tail); av_packet_free(&avctx->internal->ds.in_pkt); av_frame_free(&avctx->internal->es.in_frame);