From patchwork Fri Sep 24 06:28: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: 30546 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp1196701iob; Thu, 23 Sep 2021 23:32:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxb0tZas8LI2H/NY0wW3Rwh60ce3P/QPUvm2OWmnfjDjnIjZ45mDqWAuJ7d6UQeLj4FOIyp X-Received: by 2002:a50:d581:: with SMTP id v1mr3269363edi.210.1632465138306; Thu, 23 Sep 2021 23:32:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632465138; cv=none; d=google.com; s=arc-20160816; b=BUV0hWk7Xx5DBFFR/GbipPj9fq1URvlf3RH/IsliKkAtxiaPoRx64S3iDQ3Z8MVBmP FF1lZT1okWOo++TQr8208oKZx4tlsZv+uB0tkSnAQ2MVKxTH9ESneRw/UGlzIPwXeyrr 8KSLQAIYlVJV1fb3eed4K+ZYCG9xUxZg5No5KoX2CoYoqlTtCs1GG6tVvVAyjZ4r3TNi zr6lR/r+sReRlL72GdfCIM18THKPCbcE5/sLJh5Ot0CJMj5gXVlLzq9ml0y5pOVlrJ/Q IQoGgzQe8fvAdbxzQKYPfzqZf0kzEkUzQhA8hU882FBrt9yKQ8aS++ldZiDSzLxuaV5E Mosw== 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=3AS26pkdCZO9VYJZ7+CzCpEsWn0+hzihK7431nv/Xic=; b=ZU7iZMtjzj9uKxlkj4ubemBwX+EnFXXfIq43BCUwiWAFZG7fh8m+YsaExARX9fh74C QSo4fZwo2tqMYpuGnnNFjekiKlmX1U0lMaZL0BavbXEULBQG/+NAAUU+5GQhY2ue4yVu uYrgx4NfIBDJjy91YZNLgozT6kl50NfI4MEOGIqL53bjAguZehAhCibWcJ4niaYXa9on XiFlLn5ad3haWWj01YrRVTWu9o12M69sgJ89LmNXepGNHNCvTf/XxhjdUxueH82obNC3 O2vnm0RLzqvE7WuWFZPxLcXLG58o5/L0vQabm4BO5R2o10fLSbuvSFiBpAiyhFddv/p7 Ow0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=lQe49vO9; 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 g15si8613546eds.75.2021.09.23.23.32.17; Thu, 23 Sep 2021 23:32: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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=lQe49vO9; 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 8BA4D68A607; Fri, 24 Sep 2021 09:32:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5032668A1EF for ; Fri, 24 Sep 2021 09:32:09 +0300 (EEST) Received: by mail-pg1-f173.google.com with SMTP id t1so8962527pgv.3 for ; Thu, 23 Sep 2021 23:32:09 -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=E6zp5Al298nQG9GvjSdnjU9Y6qkycrOmu+/kg/VSCmA=; b=lQe49vO9r9wtuHVutSSM26+xenJ7upH4OxVwD7VBBH3fW/IF0mDt0Lf3THVdixA7tz ARogggwWiObHCHaI525e7smYLQrtK63+dp80tihu/URTCZ+zr1CBANDVX04849qeUCBM vifwMv+U8rlLuNI70in5Y91VxNaPMVXdJRDhQMd8MZu6/RFE5yr3yclCaEjId6ftNAcZ WLshSezrBzLTkZ6K3nDd5AbDdozOkDIqvcJyxp40dX535chf6dS9sHi8bJKwqrHHXG7U jm5AR+KK3WxxJRLcSI6zh8UiqYReLlk1VCsZ8uMARXMM3cFyU/wESk4Tjg2IBtt9qfpP bgGQ== 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=E6zp5Al298nQG9GvjSdnjU9Y6qkycrOmu+/kg/VSCmA=; b=bhaqnvWS1pHysx/Q0oZg+113fKyQ2p2i14gNOfpJ897DdkAjHHl/8CYe7XO5pzoRAF NgNJG/ppGS2MqzuAPVpR2fNB8aKMJ0vBhp74OjPvtuEPeWVtiLpo2q/DBe40OJ3Sd+6v 76bBcpN+ekj6tm9ZhfXD3NuxzftW0S/lBMClW33N+Gq59NP5P49ei14SI7jX6d0Hqfna Vkqg7KnHf3Lyy7R9SoFS/OZFp3jxBaAZ+h1ME1LjiL+BOYxpOXy8PV8rT70o5PjjP9uv Mjkwi/QZZbdnlt4nZfRR5JhXz2VqZwwDfPtfrl77M+cvXhAxhGqm38/A9+EONiBmapf7 9f6Q== X-Gm-Message-State: AOAM5331japR962SglsSw322QRxlwFU2MMpRC3x/65DvNZ2zfyBI05Lw gigigdoy0DaHqOH6pldXbyuvVM9l0R9DkA== X-Received: by 2002:a63:8f06:: with SMTP id n6mr2424805pgd.315.1632465127297; Thu, 23 Sep 2021 23:32:07 -0700 (PDT) Received: from localhost.localdomain ([103.252.202.177]) by smtp.gmail.com with ESMTPSA id d3sm7427886pfn.156.2021.09.23.23.32.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Sep 2021 23:32:06 -0700 (PDT) From: Ho Ming Shun To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Sep 2021 14:28:35 +0800 Message-Id: <20210924062838.7461-2-cyph1984@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210924062838.7461-1-cyph1984@gmail.com> References: <20210924062838.7461-1-cyph1984@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 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: 8rCIjojzh8bR 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 03e15420ce..b97cc6ff5e 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; #if FF_API_PKT_PTS FF_DISABLE_DEPRECATION_WARNINGS @@ -776,12 +783,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}; @@ -793,7 +800,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) @@ -802,7 +813,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 @@ -814,7 +825,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[] = { @@ -846,7 +860,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, \