From patchwork Mon Mar 16 21:30:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 18252 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 5221444B70B for ; Mon, 16 Mar 2020 23:31:04 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 39C5268B48D; Mon, 16 Mar 2020 23:31:04 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f193.google.com (mail-qk1-f193.google.com [209.85.222.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 49C0468AA9C for ; Mon, 16 Mar 2020 23:30:56 +0200 (EET) Received: by mail-qk1-f193.google.com with SMTP id u25so29013829qkk.3 for ; Mon, 16 Mar 2020 14:30:56 -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=28yYAE6u/RHoed8cSVKiiAXH/aJ4nETMfD4E5Mc7MQk=; b=nlVkcRHSGEQxf6Y0pJzXluBK8Os+5jwKY7hudXZeaIOlNikRCBKv8d0uJOH26WZGPw z8WGHcXz5/Tq+lOvBBwq26HHp25cHT58L10mZWXfb6Rrsp9hNqnrzSm0qIWnPSy6Ss/p jFxLd+ntVfE0UNTzFGESqtViDulms/8kvi07xI7Ry51flgttUFC0GcNL3QM1OYJstPNi VnuxvjlmLnYJ8NoRRmX1q18ElFAi0hpFxnvM1xNK3/12rx5pHt6c8h1Q1fn7EPN9cXT+ uzU5trbWCXF/fAr/ZfYljWwMk608zObdJFeGrvcsAn5a9SSxf2EGziaLzMNRb8MfqQh7 lE3g== 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=28yYAE6u/RHoed8cSVKiiAXH/aJ4nETMfD4E5Mc7MQk=; b=lb+N07mZIB/K9laTLlrSboeN1NuATDAp98NnI8LCYn7FssgGdZsXRsmyxyQI3/mNud Znr6vbsRi3McsUf92zW8AHVyKFG77/q2CeXBJL39JGRP2Voxwg7VMRO0VxdtZAFikzQv BL7q2dTAqzd+0LFqu3Ee2skLMS4yP+59pJnqpCr2c/BA6Au8JA/reoVZLts0IwbJ0y6q nUoe8R2m7v2Fxljunis295Y2x2w2Cco5bB5P77+DuLbIAvSIKoeJhZQigyq8c/6CI+m8 bILnJE421IhFR10kAgGkNpaCc36X+NKx7+fK8EIqyGsdkGK3W1FWnYyAbTlYtZAQfPZH 2vTQ== X-Gm-Message-State: ANhLgQ2hRfaf/OHCNDW5Sd+SH98wiAethZEVwmgxIQ+98q2tbnqb+MHa hHuRcR1dnfNZ3LoXECsAYIMMtw2A X-Google-Smtp-Source: ADFU+vs/peoyAd+tqw50jYwIwksCRcMoAH3IOu/+D0fWHQEoFMg1f1IPK6njx7oiKvg/rt3O7tSfVQ== X-Received: by 2002:a37:7d81:: with SMTP id y123mr1884288qkc.102.1584394254887; Mon, 16 Mar 2020 14:30:54 -0700 (PDT) Received: from localhost.localdomain ([181.23.71.173]) by smtp.gmail.com with ESMTPSA id r10sm601308qkm.23.2020.03.16.14.30.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 14:30:54 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 16 Mar 2020 18:30:02 -0300 Message-Id: <20200316213002.46612-4-jamrial@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200316213002.46612-1-jamrial@gmail.com> References: <20200316213002.46612-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3 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 --- 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),