From patchwork Fri Sep 24 09:04:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Shun Ho X-Patchwork-Id: 30550 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp1291758iob; Fri, 24 Sep 2021 02:06:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzAJiK9wE177TbV8CDaYS/rl6PqEpct9KldjIVVPWlSTJvBQVkiKiJJFZy0pH1484I+zKU2 X-Received: by 2002:a17:906:fc7:: with SMTP id c7mr10211290ejk.333.1632474385578; Fri, 24 Sep 2021 02:06:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632474385; cv=none; d=google.com; s=arc-20160816; b=gicynsZj/0RefUKFP0ftasJgPagavRTSvaPGpfClydel/VGl885CgxMtVS2Ih1ZqUb GNPXlvF79+EA4EUaNH1DZ0Lukc9dIKyEN1TWTPGKVddV+Q45WTvSPO0OzRjkHZa3WswI 3NfFA+UOEbe+EkfVtAPtIGcitQURLPQwE92Vkh6LLlGAOeb6LdkZwP+N/x59/p4PF5q6 dyM1YKDbYtFDNC0LZ4FoFPJdLO6Zt4NPl5NcBvEtt7KwcalUNzrk69aqAs0cFG1XpfeJ CzUW5EggH4b2ih+2GwvQ7cT+W8BwF3+5oYQD6rnChhbe4AKN6g+4ZNYRASjEGyDP9K8t mU8g== 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=3ujlG8j6N620Yd5+3oh68sn2jyLEj7b8iFONPR9NfCE=; b=ZCD63veUWLK/HNEke6kpXN8Lbb+jZdXGB7Q2kIchscVGZmCklVAeKcsdJ/RlusGEEA KV3Jmv0DIN86UxS6pxQd68dUeRKnAqWN1tR9Yy+vllrTUQS/TuPE7Js87sqWyqCGo6xl nyKfMCr34sdD3Nidmv785FGtLl2HNv81knY6qKC6+ZDCGM7wts601wyHJQyW4Magfq3+ sBaVhN61ibCRof323nL57X17NpEjQrb7pEp57AgcK5s53XelZDj/K4A5C02llg9laF6m YDT/Eb3gccUZXTcEuyyyRnYK/gIUbxYixhVxBesUUl2wHkGVfGzydtkHRL5sJ+WITkr4 fnww== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=EhtNEyQV; 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 z18si8182609edq.553.2021.09.24.02.06.25; Fri, 24 Sep 2021 02:06:25 -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=EhtNEyQV; 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 C18736880FC; Fri, 24 Sep 2021 12:06:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 49BF868078A for ; Fri, 24 Sep 2021 12:06:07 +0300 (EEST) Received: by mail-pj1-f54.google.com with SMTP id t20so6504016pju.5 for ; Fri, 24 Sep 2021 02:06:07 -0700 (PDT) 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=JPfkHHWxLqb95JZT5ZNp0PHdq4ucGQxUii+uAZrdghM=; b=EhtNEyQVu1IyqTWcBmFNkFcU3J5TtIwKhAyQc+ko75IeceDZHpUkrxMoVZ4mFfkY+c 7zRGLoPi45tIrdOMxPCUyPJOe4eQKGi9fVIaJhPrGiJasuVG+7hlKk0N2UZb6tD7bsNY 29L96PVCi4PMV12FjhmUSYqFMYpz66alufXGWHT4fK4ZK29KfCWN2Je3v3GMZoHfOBwl +AUO1yh5Rd6QQ9kNJcO81porVI6bTMZyX+qr6UQnlC5T0UYlKNSx1vKOvPt7hna6P+H8 nr/ab+SCnFRYko4uWct/yfOuuYJ27YiCwY01c+Y80Yn7RxFBhCWSu5HR3Q05SKF5Kuf4 mz3A== 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=JPfkHHWxLqb95JZT5ZNp0PHdq4ucGQxUii+uAZrdghM=; b=zOUVXdZGhxmD7EknBvOhaugYmW/8u7+Hz7z+XefIwposYBEhr66v/PFCZ9EjvEqLcs 2xpTBcljtdv0fQdWgMazLSOWVb10xhJkvzTZ48hTy839vuE6qVP7Y0YFfKUxVw+1I23P oTUvXNDvmKS6Vmyk5iLF0zWQVpOKiRG2v/gW/Kpm75Wf24YYNNNZOj/46cNEXpxHoC7M 12Z1TKZM5vGdcdwWYTAYfKEqgghz1psKWh7IffhF10A9PCJvvcFefXwf5znMBSJ4idj7 LybEOAWchC9tIQb17D49SUtdy7yjzSkL+VyUCRpZdq1gsfaYCpZYEEW/dPwrcAy5+3ZD 8MpA== X-Gm-Message-State: AOAM531qnQa5UPWjqlGv4+UQvReUf8Mt7Lp2Khmc6qxNkJ7Xsc8QaOuu nzTGUn6CTn5t6bhWcDcqRjOZ13Zjb1fzPg== X-Received: by 2002:a17:90a:1d0:: with SMTP id 16mr1010836pjd.60.1632474365362; Fri, 24 Sep 2021 02:06:05 -0700 (PDT) Received: from localhost.localdomain ([103.252.202.177]) by smtp.gmail.com with ESMTPSA id s2sm8029279pfe.215.2021.09.24.02.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 02:06:05 -0700 (PDT) From: Ho Ming Shun To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Sep 2021 17:04:35 +0800 Message-Id: <20210924090438.11954-2-cyph1984@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210924090438.11954-1-cyph1984@gmail.com> References: <20210924090438.11954-1-cyph1984@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/4] avcodec/mmaldec: use decoupled dataflow 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: Ho Ming Shun Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: J9FZk6UQNVjm MMAL is an fundamentally an asynchronous decoder, which was a bad fit for the legacy dataflow API. Often multiple packets are enqueued before a flood of frames are returned from MMAL. The previous lockstep dataflow meant that any delay in returning packets from the VPU would cause ctx->queue_decoded_frames to grow with no way of draining the queue. Testing this with mpv streaming from a live RTSP source visibly reduced latency introduced by frames waiting in queue_decoded_frames from roughly 2s to 0. Signed-off-by: Ho Ming Shun --- libavcodec/mmaldec.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/libavcodec/mmaldec.c b/libavcodec/mmaldec.c index 6c3e5d99b6..d4788b33f3 100644 --- a/libavcodec/mmaldec.c +++ b/libavcodec/mmaldec.c @@ -578,6 +578,7 @@ static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt, done: av_buffer_unref(&buf); + av_packet_unref(avpkt); return ret; } @@ -663,6 +664,12 @@ static int ffmal_copy_frame(AVCodecContext *avctx, AVFrame *frame, avctx->pix_fmt, avctx->width, avctx->height); } + frame->sample_aspect_ratio = avctx->sample_aspect_ratio; + frame->width = avctx->width; + frame->width = avctx->width; + frame->height = avctx->height; + frame->format = avctx->pix_fmt; + frame->pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts; frame->pkt_dts = AV_NOPTS_VALUE; @@ -771,12 +778,12 @@ done: return ret; } -static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame, - AVPacket *avpkt) +static int ffmmal_receive_frame(AVCodecContext *avctx, AVFrame *frame) { MMALDecodeContext *ctx = avctx->priv_data; - AVFrame *frame = data; + AVPacket avpkt = {0}; int ret = 0; + int got_frame = 0; if (avctx->extradata_size && !ctx->extradata_sent) { AVPacket pkt = {0}; @@ -788,7 +795,11 @@ static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame, return ret; } - if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0) + ret = ff_decode_get_packet(avctx, &avpkt); + if(ret == 0) { + if ((ret = ffmmal_add_packet(avctx, &avpkt, 0)) < 0) + return ret; + } else if(ret < 0 && !(ret == AVERROR(EAGAIN))) return ret; if ((ret = ffmmal_fill_input_port(avctx)) < 0) @@ -797,7 +808,7 @@ static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame, if ((ret = ffmmal_fill_output_port(avctx)) < 0) return ret; - if ((ret = ffmmal_read_frame(avctx, frame, got_frame)) < 0) + if ((ret = ffmmal_read_frame(avctx, frame, &got_frame)) < 0) return ret; // ffmmal_read_frame() can block for a while. Since the decoder is @@ -809,7 +820,10 @@ static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame, if ((ret = ffmmal_fill_input_port(avctx)) < 0) return ret; - return ret; + if(!got_frame && ret == 0) + return AVERROR(EAGAIN); + else + return ret; } static const AVCodecHWConfigInternal *const mmal_hw_configs[] = { @@ -841,7 +855,7 @@ static const AVOption options[]={ .priv_data_size = sizeof(MMALDecodeContext), \ .init = ffmmal_init_decoder, \ .close = ffmmal_close_decoder, \ - .decode = ffmmal_decode, \ + .receive_frame = ffmmal_receive_frame, \ .flush = ffmmal_flush, \ .priv_class = &ffmmal_##NAME##_dec_class, \ .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE, \