From patchwork Sat Feb 29 19:57:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 17976 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 87B9B449FB1 for ; Sat, 29 Feb 2020 22:06:18 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 63AB068B074; Sat, 29 Feb 2020 22:06:18 +0200 (EET) 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 8ABE768AF77 for ; Sat, 29 Feb 2020 22:06:12 +0200 (EET) Received: by mail-qk1-f196.google.com with SMTP id f3so6469614qkh.3 for ; Sat, 29 Feb 2020 12:06:12 -0800 (PST) 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=YrPPO0yUPcLNw5xm3UwO7cHuPDxzjrokTBiJUsUaB6M=; b=WKKjZfdh6JWXbBiquDu4JAvV8BtkPA1sR+BZgz50o2fg/ru9ocE+SBjA/KXoU0QyIm qPki9rgQxefcMazKnM7+DqYGuXU8KCPLyJ2/9JHYMvtnsKQDdFhrOaKwc3fQWjhZ9cxV 68sGY5+Ik4mXnQto35jyNGVM1EJ54stUP9sScZOtzIYhIVIleot5pwyQ1kZ6sjZQNjEp uFQ+gw8XfGG138VUka4M6hSkSZP6cc5MNYnFJDUvXPTHf0JsMAYZpH0fFr3IhB6kHju/ rYhCiZcoHAgobJii3dUL2bC60DksWpEhHIKHRoJ3azSHHrn/uaMznsePxZMLWDgVMwSj r4nw== 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=YrPPO0yUPcLNw5xm3UwO7cHuPDxzjrokTBiJUsUaB6M=; b=ShOb9VQsrw8UMgU1+j/sPq9fYWTTM2ypSVExGjFlHL2Tdx4l8YClra47efQv/d0/5+ rhRC5patwpcxRDxXYYfDRePv7xkvCcG4zhAuPZIjqNmfh1iDvWY7pJ/BBYl9ndMVxlJB QdSrbT06DJHpBgUkI8tF9La8gQpEGmB4yreWKeJ/xWnjmZpGM/SNIAeTEC5WNpikapCy tVjd7A4EGrx1bhH5yYCQFvXmrLYSbPBas1B9cpEaxljtl+GrR9MjQbaS8IFB6WsMsK2V gFxTvwSO4wfGZ2OvrMp3U5wMZHJsp+zsPn4BPbxG5mJZ0y3io5aVbq2h+IPEKdIEOQ98 n9xg== X-Gm-Message-State: APjAAAXKudMRnG3r93k7+yg0hE7SaHCn7hdYfnQtq3ak8YJNCBL2VVpa GNnLsb7dxpyL5wzY947Fw3ah2J2b X-Google-Smtp-Source: APXvYqxKpDH+SLNuK1BQNVKzG4n/BCAkHHp7HcPPiumKfryE5b7WUodQpGLUf/xphwt94qdZddk60A== X-Received: by 2002:ac8:8d6:: with SMTP id y22mr9134864qth.85.1583006306022; Sat, 29 Feb 2020 11:58:26 -0800 (PST) Received: from localhost.localdomain ([191.83.221.116]) by smtp.gmail.com with ESMTPSA id s13sm7406087qke.67.2020.02.29.11.58.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Feb 2020 11:58:25 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 29 Feb 2020 16:57:15 -0300 Message-Id: <20200229195715.2097-1-jamrial@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227180202.30982-5-jamrial@gmail.com> References: <20200227180202.30982-5-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4 v2] avcodec/librav1e: adapt to the new internal encode API 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" Signed-off-by: James Almer --- Fixed retrying to feed librav1e a frame after it returned RA_ENCODER_STATUS_ENOUGH_DATA the previous time (Can't be reproduced right now as librav1e hasn't yet introduced a queue limit). Also, no reindenting stuff this time to show the needed changes are trivial. Must still be squashed into PATCH 2/2 libavcodec/librav1e.c | 51 ++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/libavcodec/librav1e.c b/libavcodec/librav1e.c index b8b1b4f8f1..ba8c2a56f9 100644 --- a/libavcodec/librav1e.c +++ b/libavcodec/librav1e.c @@ -30,12 +30,15 @@ #include "libavutil/opt.h" #include "libavutil/pixdesc.h" #include "avcodec.h" +#include "encode.h" #include "internal.h" typedef struct librav1eContext { const AVClass *class; RaContext *ctx; + AVFrame *frame; + RaFrame *rframe; AVBSFContext *bsf; uint8_t *pass_data; @@ -165,7 +168,12 @@ static av_cold int librav1e_encode_close(AVCodecContext *avctx) rav1e_context_unref(ctx->ctx); ctx->ctx = NULL; } + if (ctx->rframe) { + rav1e_frame_unref(ctx->rframe); + ctx->rframe = NULL; + } + av_frame_free(&ctx->frame); av_bsf_free(&ctx->bsf); av_freep(&ctx->pass_data); @@ -180,6 +188,10 @@ static av_cold int librav1e_encode_init(AVCodecContext *avctx) int rret; int ret = 0; + ctx->frame = av_frame_alloc(); + if (!ctx->frame) + return AVERROR(ENOMEM); + cfg = rav1e_config_default(); if (!cfg) { av_log(avctx, AV_LOG_ERROR, "Could not allocate rav1e config.\n"); @@ -399,18 +411,27 @@ end: return ret; } -static int librav1e_send_frame(AVCodecContext *avctx, const AVFrame *frame) +static int librav1e_receive_packet(AVCodecContext *avctx, AVPacket *pkt) { librav1eContext *ctx = avctx->priv_data; - RaFrame *rframe = NULL; + RaFrame *rframe = ctx->rframe; + RaPacket *rpkt = NULL; int ret; - if (frame) { + if (!rframe) { + AVFrame *frame = ctx->frame; + + ret = ff_encode_get_frame(avctx, frame); + if (ret < 0 && ret != AVERROR_EOF) + return ret; + + if (frame->buf[0]) { const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); rframe = rav1e_frame_new(ctx->ctx); if (!rframe) { av_log(avctx, AV_LOG_ERROR, "Could not allocate new rav1e frame.\n"); + av_frame_unref(frame); return AVERROR(ENOMEM); } @@ -421,17 +442,23 @@ static int librav1e_send_frame(AVCodecContext *avctx, const AVFrame *frame) (frame->height >> shift) * frame->linesize[i], frame->linesize[i], bytes); } + av_frame_unref(frame); + } } ret = rav1e_send_frame(ctx->ctx, rframe); if (rframe) + if (ret == RA_ENCODER_STATUS_ENOUGH_DATA) { + ctx->rframe = rframe; /* Queue is full. Store the RaFrame to retry next call */ + } else { rav1e_frame_unref(rframe); /* No need to unref if flushing. */ + ctx->rframe = NULL; + } switch (ret) { case RA_ENCODER_STATUS_SUCCESS: - break; case RA_ENCODER_STATUS_ENOUGH_DATA: - return AVERROR(EAGAIN); + break; case RA_ENCODER_STATUS_FAILURE: av_log(avctx, AV_LOG_ERROR, "Could not send frame: %s\n", rav1e_status_to_str(ret)); return AVERROR_EXTERNAL; @@ -440,15 +467,6 @@ static int librav1e_send_frame(AVCodecContext *avctx, const AVFrame *frame) return AVERROR_UNKNOWN; } - return 0; -} - -static int librav1e_receive_packet(AVCodecContext *avctx, AVPacket *pkt) -{ - librav1eContext *ctx = avctx->priv_data; - RaPacket *rpkt = NULL; - int ret; - retry: if (avctx->flags & AV_CODEC_FLAG_PASS1) { @@ -473,9 +491,7 @@ retry: } return AVERROR_EOF; case RA_ENCODER_STATUS_ENCODED: - if (avctx->internal->draining) - goto retry; - return AVERROR(EAGAIN); + goto retry; case RA_ENCODER_STATUS_NEED_MORE_DATA: if (avctx->internal->draining) { av_log(avctx, AV_LOG_ERROR, "Unexpected error when receiving packet after EOF.\n"); @@ -575,7 +591,6 @@ AVCodec ff_librav1e_encoder = { .type = AVMEDIA_TYPE_VIDEO, .id = AV_CODEC_ID_AV1, .init = librav1e_encode_init, - .send_frame = librav1e_send_frame, .receive_packet = librav1e_receive_packet, .close = librav1e_encode_close, .priv_data_size = sizeof(librav1eContext),