From patchwork Thu Feb 27 18:01:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 17941 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 70EC844B25D for ; Thu, 27 Feb 2020 20:03:19 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5310F68B32B; Thu, 27 Feb 2020 20:03:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8D36568B03C for ; Thu, 27 Feb 2020 20:03:13 +0200 (EET) Received: by mail-qt1-f176.google.com with SMTP id v25so2963662qto.7 for ; Thu, 27 Feb 2020 10:03:13 -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=jtFlA2peSPdGkA1qz7ENVLzuDdpGSGnPGYR1zDe5zqw=; b=rRMu+twv3RIKOThb/noCjcJUsl0QUNnZrbeVE3i9A00pd3f+FMjcSNJ4Le7OFWGIEZ 6gnqr1VKKDcvdtMoKITIH+gTEqTQ6FdYlVqW49mtrCgUKaXyVXGP8h21lmJtG1GeUfbV 4QMt00XZY312zH/lIe2nztxht/cTtLU5IH6X5nipeKYfRM0UnSTUE0IpH0jwQYVMBuGE big75yfUGt1HZqQc0gw+iLBdKTi9DnWhCQXBx1cnHjAkOL9ex7vw6lHewR6YIhbZsvXc R4RPfls7hcKSBr9rCHJmJS1zjEFSXVRfuT2SqlG47jyrbwjwT+BEpCycUMQuhi9KelHK NPtA== 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=jtFlA2peSPdGkA1qz7ENVLzuDdpGSGnPGYR1zDe5zqw=; b=ewaBbsoHBsrYag7CDoCi8w9WEXKLls5diXZ9aaOcEwbOjK/OFh/QP2BFZXtTEDIDB3 ZcMMMyctffdukSJR90HKakzb4NmmchloLjZSmUpe6irGVXJ/NBUlsvl6C29CXg3vjlwJ H8Jc/V99EuEeDukiMR3GTa6IH4wo9JMu6ggb3BVUxjgKavbtdGQxLPWLfNYOfSjXAUKd lxsmhM9NSvc/WvL9zksn2Kt0sJ4tcvkfY1qV7ddTvcHBGukft0v2WIK0Ylz/FFI+eJl2 dkL5uG1cTcWq/GGLqr8aBG8USuz3RvP507vTGlKLhBo30GHbEjKBNyjbgQ8GG3P90dZa 32yw== X-Gm-Message-State: APjAAAVuiyHQpvLNx7Cu79yBcmg47InG9QePplObOL7+uceca6B9pYgx eZ5KeE5Ai0KuhQP3PVqVK+0U0XWN X-Google-Smtp-Source: APXvYqz+6QG45oNuRuvp7ip9zNHp3hTu9YfrQtSNoQDQURRqwsqOK2mXnk/PL6V4i5gR9r25EVov3g== X-Received: by 2002:ac8:1b18:: with SMTP id y24mr443811qtj.158.1582826592046; Thu, 27 Feb 2020 10:03:12 -0800 (PST) Received: from localhost.localdomain ([191.83.210.181]) by smtp.gmail.com with ESMTPSA id v80sm3523323qka.15.2020.02.27.10.03.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2020 10:03:11 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 27 Feb 2020 15:01:59 -0300 Message-Id: <20200227180202.30982-2-jamrial@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227180202.30982-1-jamrial@gmail.com> References: <20200227180202.30982-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] avcodec: move avcodec_flush_buffers from decode.c to utils.c 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" It's not a decoding exclusive function. Signed-off-by: James Almer --- libavcodec/decode.c | 36 ------------------------------------ libavcodec/utils.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 03b9da25f9..93b0db7ef8 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -2020,42 +2020,6 @@ int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags) return ret; } -static void bsfs_flush(AVCodecContext *avctx) -{ - DecodeFilterContext *s = &avctx->internal->filter; - - for (int i = 0; i < s->nb_bsfs; i++) - av_bsf_flush(s->bsfs[i]); -} - -void avcodec_flush_buffers(AVCodecContext *avctx) -{ - AVCodecInternal *avci = avctx->internal; - - avci->draining = 0; - avci->draining_done = 0; - avci->nb_draining_errors = 0; - av_frame_unref(avci->buffer_frame); - av_frame_unref(avci->compat_decode_frame); - av_packet_unref(avci->buffer_pkt); - avci->buffer_pkt_valid = 0; - - av_packet_unref(avci->ds.in_pkt); - - if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) - ff_thread_flush(avctx); - else if (avctx->codec->flush) - avctx->codec->flush(avctx); - - avctx->pts_correction_last_pts = - avctx->pts_correction_last_dts = INT64_MIN; - - bsfs_flush(avctx); - - if (!avctx->refcounted_frames) - av_frame_unref(avci->to_free); -} - void ff_decode_bsfs_uninit(AVCodecContext *avctx) { DecodeFilterContext *s = &avctx->internal->filter; diff --git a/libavcodec/utils.c b/libavcodec/utils.c index c4dc136d3c..5257a1c627 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -1084,6 +1084,39 @@ FF_ENABLE_DEPRECATION_WARNINGS goto end; } +void avcodec_flush_buffers(AVCodecContext *avctx) +{ + AVCodecInternal *avci = avctx->internal; + + avci->draining = 0; + avci->draining_done = 0; + avci->nb_draining_errors = 0; + av_frame_unref(avci->buffer_frame); + av_frame_unref(avci->compat_decode_frame); + av_packet_unref(avci->buffer_pkt); + avci->buffer_pkt_valid = 0; + + av_packet_unref(avci->ds.in_pkt); + + if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) + ff_thread_flush(avctx); + else if (avctx->codec->flush) + avctx->codec->flush(avctx); + + avctx->pts_correction_last_pts = + avctx->pts_correction_last_dts = INT64_MIN; + + if (av_codec_is_decoder(avctx->codec)) { + DecodeFilterContext *s = &avctx->internal->filter; + + for (int i = 0; i < s->nb_bsfs; i++) + av_bsf_flush(s->bsfs[i]); + } + + if (!avctx->refcounted_frames) + av_frame_unref(avci->to_free); +} + void avsubtitle_free(AVSubtitle *sub) { int i; From patchwork Thu Feb 27 18:02:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 17942 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 579D044B25D for ; Thu, 27 Feb 2020 20:03:22 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3A23868B2EB; Thu, 27 Feb 2020 20:03:22 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4319168B6E2 for ; Thu, 27 Feb 2020 20:03:15 +0200 (EET) Received: by mail-qt1-f175.google.com with SMTP id d9so2931879qte.12 for ; Thu, 27 Feb 2020 10:03:15 -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=vSAJCYt0umTKRXj78ZxtQH7n6ioMVmjP8GbR/ZgF1wE=; b=jxAlnv76JYiJ7q1H47I3siAAL9TZolvRstdAQuHiYDFqSvt4Udcxu1OVsbEksxQxhX SHmMBa0eedL9RzZtQKZfmI+PBuluM+kfvH7WIeAqrzJQwvjMpidHnZk44yP8yjx7ei2c Q6OQsu8WeNvYeNI394Ssp1Hm2OwYfDezbBwTSSqSwHnKLQDC65O/uQksaF6/6/YU14mr ATTSBHqBz5YTxwjFuTxWSq3u6BZIpyN2QNQbnVAlvYoD2O9Q98nHx7XMolOwijxFtsSQ zDlOKW51uBUbQYbcFck4VUgOibwgv4b5Q7+jNOeubKmJb2A4+VIM8m2N0Wk9q96eWlly x/Xg== 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=vSAJCYt0umTKRXj78ZxtQH7n6ioMVmjP8GbR/ZgF1wE=; b=fbqESWp/helMolUqr26ZqNLP5g/tKWQPjhCMOu0WcpTL5aqSo6GlSPmUg14GguWeps U4glWMfFTtQuKUXJtjKTT8YhJF+xefxYMF2wE+lBYOtKlU1FbZA1m5aIliHWBzDqDhK5 iBPGkhEwbF20hVs5BikvqcAuVUUn1K0pv+pHmZGYqL3iHiMFpdUSzB5LtdhwFinSXO9l INtZuSBRZ90n3boK4R8uYt3CaabQayzG4OXD0NbibzBs0KV5kvqE6sn+WJY4HtcQXH5h RgfKlOrEBnlC74UixuIl2nrMa6QuZM0aeGpzfxQRraQVjxaZw/8mwjfBa8G44Bvv7nJF qjxQ== X-Gm-Message-State: APjAAAU1z3ajqsTt7NGRUk67GxJfqE791VPB2xAqzmpi3Us7SrAXXvQk 2Vf5gbODk9QCP0MAuPHprkxJMlA1 X-Google-Smtp-Source: APXvYqyAQiVF76J2NLFNgyhm3tKpY3PBjPwQQoE2MHPmr4F0Ex2NRMN2pbwfJWkEc70EoQ2VJYoWWQ== X-Received: by 2002:aed:3302:: with SMTP id u2mr440346qtd.156.1582826593395; Thu, 27 Feb 2020 10:03:13 -0800 (PST) Received: from localhost.localdomain ([191.83.210.181]) by smtp.gmail.com with ESMTPSA id v80sm3523323qka.15.2020.02.27.10.03.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2020 10:03:13 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 27 Feb 2020 15:02:00 -0300 Message-Id: <20200227180202.30982-3-jamrial@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227180202.30982-1-jamrial@gmail.com> References: <20200227180202.30982-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avcodec/encode: restructure the core encoding code 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" This commit follows the same logic as 061a0c14bb, but for the encode API: The new public encoding API will no longer be a wrapper around the old deprecated one, and the internal API used by the encoders now consists of a single receive_packet() callback that pulls frames as required. Signed-off-by: James Almer --- libavcodec/avcodec.h | 12 +- libavcodec/encode.c | 268 ++++++++++++++++++++++++++++++++---------- libavcodec/encode.h | 39 ++++++ libavcodec/internal.h | 7 +- libavcodec/utils.c | 12 +- 5 files changed, 268 insertions(+), 70 deletions(-) create mode 100644 libavcodec/encode.h diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 894a9e5565..9d22390dd3 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -3641,14 +3641,10 @@ typedef struct AVCodec { int (*decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt); int (*close)(AVCodecContext *); /** - * Encode API with decoupled packet/frame dataflow. The API is the - * same as the avcodec_ prefixed APIs (avcodec_send_frame() etc.), except - * that: - * - never called if the codec is closed or the wrong type, - * - if AV_CODEC_CAP_DELAY is not set, drain frames are never sent, - * - only one drain frame is ever passed down, - */ - int (*send_frame)(AVCodecContext *avctx, const AVFrame *frame); + * Encode API with decoupled frame/packet dataflow. This function is called + * to get one output packet. It should call ff_encode_get_packet() to obtain + * input data. + */ int (*receive_packet)(AVCodecContext *avctx, AVPacket *avpkt); /** diff --git a/libavcodec/encode.c b/libavcodec/encode.c index 9ed2cf0f59..a887a0ab55 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -26,6 +26,7 @@ #include "libavutil/samplefmt.h" #include "avcodec.h" +#include "encode.h" #include "frame_thread_encoder.h" #include "internal.h" @@ -359,101 +360,250 @@ int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, return ret; } -static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet) +int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame) { + AVCodecInternal *avci = avctx->internal; + + if (avci->draining) + return AVERROR_EOF; + + if (!avci->buffer_frame->buf[0]) + return AVERROR(EAGAIN); + + av_frame_move_ref(frame, avci->buffer_frame); + + return 0; +} + +static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt) +{ + AVCodecInternal *avci = avctx->internal; + EncodeSimpleContext *es = &avci->es; + AVFrame *frame = es->in_frame; + AVFrame *padded_frame = NULL; + int got_packet; int ret; - *got_packet = 0; - av_packet_unref(avctx->internal->buffer_pkt); - avctx->internal->buffer_pkt_valid = 0; + if (!frame->buf[0] && !avci->draining) { + av_frame_unref(frame); + ret = ff_encode_get_frame(avctx, frame); + if (ret < 0 && ret != AVERROR_EOF) + return ret; + } - if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { - ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt, - frame, got_packet); - } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { - ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt, - frame, got_packet); - } else { - ret = AVERROR(EINVAL); + if (avci->draining_done) + return AVERROR_EOF; + + if (!frame->buf[0]) { + if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY || + avctx->active_thread_type & FF_THREAD_FRAME)) + return AVERROR_EOF; + + // Flushing is signaled with a NULL frame + frame = NULL; + } + + got_packet = 0; + + if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { + if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out) + avctx->stats_out[0] = '\0'; + + if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) { + ret = AVERROR(EINVAL); + goto end; + } + if (frame) { + if (frame->format == AV_PIX_FMT_NONE) + av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n"); + if (frame->width == 0 || frame->height == 0) + av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n"); + } + } else if (frame && avctx->codec->type == AVMEDIA_TYPE_AUDIO) { + /* extract audio service type metadata */ + AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE); + if (sd && sd->size >= sizeof(enum AVAudioServiceType)) + avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data; + + /* check for valid frame size */ + if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) { + if (frame->nb_samples > avctx->frame_size) { + av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n"); + ret = AVERROR(EINVAL); + goto end; + } + } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) { + /* if we already got an undersized frame, that must have been the last */ + if (avctx->internal->last_audio_frame) { + av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame (avcodec_encode_audio2)\n", avctx->frame_size); + ret = AVERROR(EINVAL); + goto end; + } + + if (frame->nb_samples < avctx->frame_size) { + ret = pad_last_frame(avctx, &padded_frame, frame); + if (ret < 0) + goto end; + + av_frame_unref(frame); + frame = padded_frame; + avctx->internal->last_audio_frame = 1; + } + + if (frame->nb_samples != avctx->frame_size) { + av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size); + ret = AVERROR(EINVAL); + goto end; + } + } + } + + av_assert0(avctx->codec->encode2); + + if (CONFIG_FRAME_THREAD_ENCODER && + avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME)) + ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet); + else + ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet); + + av_assert0(ret <= 0); + + emms_c(); + + if (!ret && got_packet) { + if (avpkt->data) { + ret = av_packet_make_refcounted(avpkt); + if (ret < 0) + goto end; + } + + if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY || + avctx->active_thread_type & FF_THREAD_FRAME)) { + if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { + avpkt->pts = avpkt->dts = frame->pts; + } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { + if (avpkt->pts == AV_NOPTS_VALUE) + avpkt->pts = frame->pts; + if (!avpkt->duration) + avpkt->duration = ff_samples_to_time_base(avctx, + frame->nb_samples); + } + } + if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { + /* NOTE: if we add any audio encoders which output non-keyframe packets, + * this needs to be moved to the encoders, but for now we can do it + * here to simplify things */ + avpkt->flags |= AV_PKT_FLAG_KEY; + avpkt->dts = avpkt->pts; + } + } + + if (avci->draining && !got_packet) + avci->draining_done = 1; + +end: + if (ret < 0 || !got_packet) + av_packet_unref(avpkt); + + if (frame) { + if (!ret) + avctx->frame_number++; + av_frame_unref(frame); } - if (ret >= 0 && *got_packet) { + av_frame_free(&padded_frame); + + if (got_packet) // Encoders must always return ref-counted buffers. // Side-data only packets have no data and can be not ref-counted. - av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf); - avctx->internal->buffer_pkt_valid = 1; - ret = 0; - } else { - av_packet_unref(avctx->internal->buffer_pkt); + av_assert0(!avpkt->data || avpkt->buf); + + return ret; +} + +static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) +{ + int ret; + + while (!avpkt->data && !avpkt->side_data) { + ret = encode_simple_internal(avctx, avpkt); + if (ret < 0) + return ret; } + return 0; +} + +static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt) +{ + AVCodecInternal *avci = avctx->internal; + int ret; + + av_assert0(!avpkt->data && !avpkt->side_data); + + if (avctx->codec->receive_packet) { + ret = avctx->codec->receive_packet(avctx, avpkt); + if (!ret) + // Encoders must always return ref-counted buffers. + // Side-data only packets have no data and can be not ref-counted. + av_assert0(!avpkt->data || avpkt->buf); + } else + ret = encode_simple_receive_packet(avctx, avpkt); + + if (ret == AVERROR_EOF) + avci->draining_done = 1; + return ret; } int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame) { + AVCodecInternal *avci = avctx->internal; + int ret; + if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec)) return AVERROR(EINVAL); - if (avctx->internal->draining) + if (avci->draining) return AVERROR_EOF; - if (!frame) { - avctx->internal->draining = 1; + if (avci->buffer_frame->data[0]) + return AVERROR(EAGAIN); - if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) - return 0; + if (!frame) { + avci->draining = 1; + } else { + ret = av_frame_ref(avci->buffer_frame, frame); + if (ret < 0) + return ret; } - if (avctx->codec->send_frame) - return avctx->codec->send_frame(avctx, frame); - - // Emulation via old API. Do it here instead of avcodec_receive_packet, because: - // 1. if the AVFrame is not refcounted, the copying will be much more - // expensive than copying the packet data - // 2. assume few users use non-refcounted AVPackets, so usually no copy is - // needed - - if (avctx->internal->buffer_pkt_valid) - return AVERROR(EAGAIN); + if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) { + ret = encode_receive_packet_internal(avctx, avci->buffer_pkt); + if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) + return ret; + } - return do_encode(avctx, frame, &(int){0}); + return 0; } int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) { + AVCodecInternal *avci = avctx->internal; + int ret; + av_packet_unref(avpkt); if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec)) return AVERROR(EINVAL); - if (avctx->codec->receive_packet) { - int ret; - if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) - return AVERROR_EOF; - ret = avctx->codec->receive_packet(avctx, avpkt); - if (!ret) - // Encoders must always return ref-counted buffers. - // Side-data only packets have no data and can be not ref-counted. - av_assert0(!avpkt->data || avpkt->buf); - return ret; - } - - // Emulation via old API. - - if (!avctx->internal->buffer_pkt_valid) { - int got_packet; - int ret; - if (!avctx->internal->draining) - return AVERROR(EAGAIN); - ret = do_encode(avctx, NULL, &got_packet); + if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) { + av_packet_move_ref(avpkt, avci->buffer_pkt); + } else { + ret = encode_receive_packet_internal(avctx, avpkt); if (ret < 0) return ret; - if (ret >= 0 && !got_packet) - return AVERROR_EOF; } - av_packet_move_ref(avpkt, avctx->internal->buffer_pkt); - avctx->internal->buffer_pkt_valid = 0; return 0; } diff --git a/libavcodec/encode.h b/libavcodec/encode.h new file mode 100644 index 0000000000..2eef31251a --- /dev/null +++ b/libavcodec/encode.h @@ -0,0 +1,39 @@ +/* + * generic encoding-related code + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_ENCODE_H +#define AVCODEC_ENCODE_H + +#include "libavutil/frame.h" + +#include "avcodec.h" + +/** + * Called by encoders to get the next frame for encoding. + * + * @param frame An empty frame to be filled with data. + * @return 0 if a new reference has been successfully written to frame + * AVERROR(EAGAIN) if no data is currently available + * AVERROR_EOF if and end of stream has been reached, so no more data + * will be available + */ +int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame); + +#endif /* AVCODEC_ENCODE_H */ diff --git a/libavcodec/internal.h b/libavcodec/internal.h index bccd9222d4..8b97e08e9f 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -132,6 +132,10 @@ typedef struct DecodeFilterContext { int nb_bsfs; } DecodeFilterContext; +typedef struct EncodeSimpleContext { + AVFrame *in_frame; +} EncodeSimpleContext; + typedef struct AVCodecInternal { /** * Whether the parent AVCodecContext is a copy of the context which had @@ -171,6 +175,8 @@ typedef struct AVCodecInternal { DecodeSimpleContext ds; DecodeFilterContext filter; + EncodeSimpleContext es; + /** * Properties (timestamps+side data) extracted from the last packet passed * for decoding. @@ -204,7 +210,6 @@ typedef struct AVCodecInternal { * buffers for using new encode/decode API through legacy API */ AVPacket *buffer_pkt; - int buffer_pkt_valid; // encoding: packet without data can be valid AVFrame *buffer_frame; int draining_done; /* set to 1 when the caller is using the old decoding API */ diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 5257a1c627..ccc08c2dfb 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -93,7 +93,7 @@ void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size) int av_codec_is_encoder(const AVCodec *codec) { - return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame); + return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet); } int av_codec_is_decoder(const AVCodec *codec) @@ -607,6 +607,12 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code goto free_and_end; } + avci->es.in_frame = av_frame_alloc(); + if (!avctx->internal->es.in_frame) { + ret = AVERROR(ENOMEM); + goto free_and_end; + } + avci->buffer_pkt = av_packet_alloc(); if (!avci->buffer_pkt) { ret = AVERROR(ENOMEM); @@ -1074,6 +1080,7 @@ FF_ENABLE_DEPRECATION_WARNINGS av_packet_free(&avci->last_pkt_props); av_packet_free(&avci->ds.in_pkt); + av_frame_free(&avci->es.in_frame); ff_decode_bsfs_uninit(avctx); av_freep(&avci->pool); @@ -1094,9 +1101,9 @@ void avcodec_flush_buffers(AVCodecContext *avctx) av_frame_unref(avci->buffer_frame); av_frame_unref(avci->compat_decode_frame); av_packet_unref(avci->buffer_pkt); - avci->buffer_pkt_valid = 0; av_packet_unref(avci->ds.in_pkt); + av_frame_unref(avci->es.in_frame); if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) ff_thread_flush(avctx); @@ -1162,6 +1169,7 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_packet_free(&avctx->internal->last_pkt_props); av_packet_free(&avctx->internal->ds.in_pkt); + av_frame_free(&avctx->internal->es.in_frame); for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++) av_buffer_pool_uninit(&pool->pools[i]); From patchwork Thu Feb 27 18:02:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 17944 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 736FD44AC1F for ; Thu, 27 Feb 2020 20:10:17 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4E2E168B709; Thu, 27 Feb 2020 20:10:17 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D6E4A68AD48 for ; Thu, 27 Feb 2020 20:10:15 +0200 (EET) Received: by mail-qk1-f176.google.com with SMTP id z12so220749qkg.12 for ; Thu, 27 Feb 2020 10:10:15 -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=BR95UM3DGOOfzARksu4ToE2vhp2WZe9P3zQ9/jd1o+U=; b=TsN4dhEmviEnvxcEgSTg86nHPwuAZ9PopRIr4wrUjtcoB1Ktr7V7rNgV0MxJBH+Qqu PyPXntfzQ0vMls8MnWQRTcFXf9ILeQygsb70K6pZBH+HarFzrpLF54p8SG+EQMXcsCxw Xjy86/H186atmLPBXx68Kr8K1bNFOVQ3hFfJAre61lUdm+gODxndUAaEX4jphD8yAQaR vfX8dQjkSCuU1B+ercR1JLrcXnX9lYB5dk48YsMl86DTgsF+3GQrutuw9UkTcQsNjcYN VJGbRIchRE90K3L0C85X+A0MyD6cYY6+AmJbv83qN2bN8+6xyWE4EAHN44y+8fC7jst7 M+UA== 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=BR95UM3DGOOfzARksu4ToE2vhp2WZe9P3zQ9/jd1o+U=; b=HMWh4D+5XUbppymB6qlRzo0e99ZdgRp3OwaiI+cyF5JMd4Q3T6Vj0xAmjtUDobdn9T 7UvsTS/ycFUte24orT25NjsBIOlt74ma1HgPrseq3aklA7X3tcOhWTh7+CoetMo9l/yR ltviALmtzujXhv0OqZ2IfbMFN+22WywE8w4L9rs3sQKpVoLprgWFsytdnCCg3tMTCwK3 FufejhZw6C0Qn3QdKfa6R1DSceivWQ6t1UrIpETsbx17rptNYBttny6RCwCnTMPEE8DA 8a7lAmam0sCnmwoZBOXbwTwsW8Wn001TrkC/OUsGBYmgK7PFWAuPR6vGW3zQmkAmqmSR MxnA== X-Gm-Message-State: APjAAAWnrBx7JfCI7NueLh7xUh4eLrKsSllKelupaOn8clwtAWYaiU8+ NoZukOxs2d9Z2y4fcipHL19WpSGt X-Google-Smtp-Source: APXvYqz27wOI8/WAXlyI1i014KaYIM+kd2IVFdbOedYGaD19fdVVNfKSFZX0duHaCuVEYyIpwhZ92A== X-Received: by 2002:a37:8e03:: with SMTP id q3mr445147qkd.395.1582826594572; Thu, 27 Feb 2020 10:03:14 -0800 (PST) Received: from localhost.localdomain ([191.83.210.181]) by smtp.gmail.com with ESMTPSA id v80sm3523323qka.15.2020.02.27.10.03.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2020 10:03:14 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 27 Feb 2020 15:02:01 -0300 Message-Id: <20200227180202.30982-4-jamrial@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227180202.30982-1-jamrial@gmail.com> References: <20200227180202.30982-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] avcodec/encode: restructure the old 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" Following the same logic as 061a0c14bb, this commit turns the old encode API into a wrapper for the new one. Signed-off-by: James Almer --- This could be squashed with the previous commit, like it was done in 061a0c14bb, but i figured it would be easier to review this way. libavcodec/encode.c | 353 ++++++++++++------------------------------ libavcodec/internal.h | 1 + libavcodec/utils.c | 9 ++ 3 files changed, 110 insertions(+), 253 deletions(-) diff --git a/libavcodec/encode.c b/libavcodec/encode.c index a887a0ab55..420eeba45f 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -32,43 +32,28 @@ int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size) { - if (avpkt->size < 0) { - av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size); - return AVERROR(EINVAL); - } if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) { av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n", size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE); return AVERROR(EINVAL); } + av_assert0(!avpkt->data); + if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned - av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer); - if (!avpkt->data || avpkt->size < size) { - av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size); - avpkt->data = avctx->internal->byte_buffer; - avpkt->size = avctx->internal->byte_buffer_size; - } + av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size); + avpkt->data = avctx->internal->byte_buffer; + avpkt->size = size; } - if (avpkt->data) { - AVBufferRef *buf = avpkt->buf; - - if (avpkt->size < size) { - av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size); - return AVERROR(EINVAL); - } - - av_init_packet(avpkt); - avpkt->buf = buf; - avpkt->size = size; - return 0; - } else { + if (!avpkt->data) { int ret = av_new_packet(avpkt, size); if (ret < 0) av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size); return ret; } + + return 0; } int ff_alloc_packet(AVPacket *avpkt, int size) @@ -116,236 +101,6 @@ fail: return ret; } -int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, - AVPacket *avpkt, - const AVFrame *frame, - int *got_packet_ptr) -{ - AVFrame *extended_frame = NULL; - AVFrame *padded_frame = NULL; - int ret; - AVPacket user_pkt = *avpkt; - int needs_realloc = !user_pkt.data; - - *got_packet_ptr = 0; - - if (!avctx->codec->encode2) { - av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n"); - return AVERROR(ENOSYS); - } - - if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) { - av_packet_unref(avpkt); - return 0; - } - - /* ensure that extended_data is properly set */ - if (frame && !frame->extended_data) { - if (av_sample_fmt_is_planar(avctx->sample_fmt) && - avctx->channels > AV_NUM_DATA_POINTERS) { - av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, " - "with more than %d channels, but extended_data is not set.\n", - AV_NUM_DATA_POINTERS); - return AVERROR(EINVAL); - } - av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n"); - - extended_frame = av_frame_alloc(); - if (!extended_frame) - return AVERROR(ENOMEM); - - memcpy(extended_frame, frame, sizeof(AVFrame)); - extended_frame->extended_data = extended_frame->data; - frame = extended_frame; - } - - /* extract audio service type metadata */ - if (frame) { - AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE); - if (sd && sd->size >= sizeof(enum AVAudioServiceType)) - avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data; - } - - /* check for valid frame size */ - if (frame) { - if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) { - if (frame->nb_samples > avctx->frame_size) { - av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n"); - ret = AVERROR(EINVAL); - goto end; - } - } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) { - /* if we already got an undersized frame, that must have been the last */ - if (avctx->internal->last_audio_frame) { - av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame (avcodec_encode_audio2)\n", avctx->frame_size); - ret = AVERROR(EINVAL); - goto end; - } - - if (frame->nb_samples < avctx->frame_size) { - ret = pad_last_frame(avctx, &padded_frame, frame); - if (ret < 0) - goto end; - - frame = padded_frame; - avctx->internal->last_audio_frame = 1; - } - - if (frame->nb_samples != avctx->frame_size) { - av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size); - ret = AVERROR(EINVAL); - goto end; - } - } - } - - av_assert0(avctx->codec->encode2); - - ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr); - if (!ret) { - if (*got_packet_ptr) { - if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) { - if (avpkt->pts == AV_NOPTS_VALUE) - avpkt->pts = frame->pts; - if (!avpkt->duration) - avpkt->duration = ff_samples_to_time_base(avctx, - frame->nb_samples); - } - avpkt->dts = avpkt->pts; - } else { - avpkt->size = 0; - } - } - if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) { - needs_realloc = 0; - if (user_pkt.data) { - if (user_pkt.size >= avpkt->size) { - memcpy(user_pkt.data, avpkt->data, avpkt->size); - } else { - av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size); - avpkt->size = user_pkt.size; - ret = -1; - } - avpkt->buf = user_pkt.buf; - avpkt->data = user_pkt.data; - } else if (!avpkt->buf) { - ret = av_packet_make_refcounted(avpkt); - if (ret < 0) - goto end; - } - } - - if (!ret) { - if (needs_realloc && avpkt->data) { - ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE); - if (ret >= 0) - avpkt->data = avpkt->buf->data; - } - if (frame) - avctx->frame_number++; - } - - if (ret < 0 || !*got_packet_ptr) { - av_packet_unref(avpkt); - goto end; - } - - /* NOTE: if we add any audio encoders which output non-keyframe packets, - * this needs to be moved to the encoders, but for now we can do it - * here to simplify things */ - avpkt->flags |= AV_PKT_FLAG_KEY; - -end: - av_frame_free(&padded_frame); - av_free(extended_frame); - - return ret; -} - -int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, - AVPacket *avpkt, - const AVFrame *frame, - int *got_packet_ptr) -{ - int ret; - AVPacket user_pkt = *avpkt; - int needs_realloc = !user_pkt.data; - - *got_packet_ptr = 0; - - if (!avctx->codec->encode2) { - av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n"); - return AVERROR(ENOSYS); - } - - if(CONFIG_FRAME_THREAD_ENCODER && - avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME)) - return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr); - - if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out) - avctx->stats_out[0] = '\0'; - - if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) { - av_packet_unref(avpkt); - return 0; - } - - if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) - return AVERROR(EINVAL); - - if (frame && frame->format == AV_PIX_FMT_NONE) - av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n"); - if (frame && (frame->width == 0 || frame->height == 0)) - av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n"); - - av_assert0(avctx->codec->encode2); - - ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr); - av_assert0(ret <= 0); - - emms_c(); - - if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) { - needs_realloc = 0; - if (user_pkt.data) { - if (user_pkt.size >= avpkt->size) { - memcpy(user_pkt.data, avpkt->data, avpkt->size); - } else { - av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size); - avpkt->size = user_pkt.size; - ret = -1; - } - avpkt->buf = user_pkt.buf; - avpkt->data = user_pkt.data; - } else if (!avpkt->buf) { - ret = av_packet_make_refcounted(avpkt); - if (ret < 0) - return ret; - } - } - - if (!ret) { - if (!*got_packet_ptr) - avpkt->size = 0; - else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) - avpkt->pts = avpkt->dts = frame->pts; - - if (needs_realloc && avpkt->data) { - ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE); - if (ret >= 0) - avpkt->data = avpkt->buf->data; - } - - if (frame) - avctx->frame_number++; - } - - if (ret < 0 || !*got_packet_ptr) - av_packet_unref(avpkt); - - return ret; -} - int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub) { @@ -607,3 +362,95 @@ int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket * return 0; } + +static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt, + int *got_packet, const AVFrame *frame) +{ + AVCodecInternal *avci = avctx->internal; + AVPacket user_pkt; + int ret; + + *got_packet = 0; + + ret = avcodec_send_frame(avctx, frame); + if (ret == AVERROR_EOF) + ret = 0; + else if (ret == AVERROR(EAGAIN)) { + /* we fully drain all the output in each encode call, so this should not + * ever happen */ + return AVERROR_BUG; + } else if (ret < 0) + return ret; + + av_packet_move_ref(&user_pkt, avpkt); + while (ret >= 0) { + ret = avcodec_receive_packet(avctx, avpkt); + if (ret < 0) { + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + ret = 0; + goto finish; + } + + if (avpkt != avci->compat_encode_packet) { + if (avpkt->data && user_pkt.data) { + if (user_pkt.size >= avpkt->size) { + memcpy(user_pkt.data, avpkt->data, avpkt->size); + av_buffer_unref(&avpkt->buf); + avpkt->buf = user_pkt.buf; + avpkt->data = user_pkt.data; + av_init_packet(&user_pkt); + } else { + av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size); + ret = AVERROR(EINVAL); + goto finish; + } + } + + *got_packet = 1; + avpkt = avci->compat_encode_packet; + } else { + if (!avci->compat_decode_warned) { + av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* " + "API cannot return all the packets for this encoder. " + "Some packets will be dropped. Update your code to the " + "new encoding API to fix this.\n"); + avci->compat_decode_warned = 1; + } + } + + if (avci->draining) + break; + } + +finish: + if (ret < 0) + av_packet_unref(&user_pkt); + + return ret; +} + +int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, + AVPacket *avpkt, + const AVFrame *frame, + int *got_packet_ptr) +{ + int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame); + + if (ret < 0) + av_packet_unref(avpkt); + + return ret; +} + +int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, + AVPacket *avpkt, + const AVFrame *frame, + int *got_packet_ptr) +{ + int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame); + + if (ret < 0) + av_packet_unref(avpkt); + + return ret; +} diff --git a/libavcodec/internal.h b/libavcodec/internal.h index 8b97e08e9f..771b9ec32b 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -222,6 +222,7 @@ typedef struct AVCodecInternal { * of the packet (that should be submitted in the next decode call */ size_t compat_decode_partial_size; AVFrame *compat_decode_frame; + AVPacket *compat_encode_packet; int showed_multi_packet_warning; diff --git a/libavcodec/utils.c b/libavcodec/utils.c index ccc08c2dfb..5191f8747b 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -613,6 +613,12 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code goto free_and_end; } + avci->compat_encode_packet = av_packet_alloc(); + if (!avctx->internal->compat_encode_packet) { + ret = AVERROR(ENOMEM); + goto free_and_end; + } + avci->buffer_pkt = av_packet_alloc(); if (!avci->buffer_pkt) { ret = AVERROR(ENOMEM); @@ -1076,6 +1082,7 @@ FF_ENABLE_DEPRECATION_WARNINGS av_frame_free(&avci->to_free); av_frame_free(&avci->compat_decode_frame); av_frame_free(&avci->buffer_frame); + av_packet_free(&avci->compat_encode_packet); av_packet_free(&avci->buffer_pkt); av_packet_free(&avci->last_pkt_props); @@ -1101,6 +1108,7 @@ void avcodec_flush_buffers(AVCodecContext *avctx) av_frame_unref(avci->buffer_frame); av_frame_unref(avci->compat_decode_frame); av_packet_unref(avci->buffer_pkt); + av_packet_unref(avci->compat_encode_packet); av_packet_unref(avci->ds.in_pkt); av_frame_unref(avci->es.in_frame); @@ -1165,6 +1173,7 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_frame_free(&avctx->internal->to_free); av_frame_free(&avctx->internal->compat_decode_frame); av_frame_free(&avctx->internal->buffer_frame); + av_packet_free(&avctx->internal->compat_encode_packet); av_packet_free(&avctx->internal->buffer_pkt); av_packet_free(&avctx->internal->last_pkt_props); From patchwork Thu Feb 27 18:02: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: 17943 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 E3CB644B25D for ; Thu, 27 Feb 2020 20:03:24 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CCF3F68B735; Thu, 27 Feb 2020 20:03:24 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CFD1268B6F9 for ; Thu, 27 Feb 2020 20:03:17 +0200 (EET) Received: by mail-qv1-f52.google.com with SMTP id y8so1978480qvk.6 for ; Thu, 27 Feb 2020 10:03:17 -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=O2BOu+PJytEU7lwXRwQ9br49UxDEwUOYnxWd1Wv31oI=; b=RREYLyzAtlEnvcMtbX6Vg8C/zKpE+w0UesH1qQlzzOHHcf8NDaO4WugUbBtRYfX/aU Ftvz3FNPnuPySsabY9mZYv/2eUIhsoheps/rv1PxRAauiuP+KVNb/DNOTwLYTg7in1zY C15sUdlXRTYw02w894oTiD4l3oJO3QKsBeoB+uN3dPSOOZan21QdLulXfMx8cXX5/gN+ iGeNi1Orklle9KxQJRsaUevOrncd9tJ75qAEvPtsyOUm/QaLa5Yz3+8OlMQ68a/1vosK KGaSLh+SrbEx0AzsdJjXQjdVrTPN2r9Ldk6d2S+Wj0wi3WkOsn5fAM/K5B1H8VgGePZ3 JwRQ== 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=O2BOu+PJytEU7lwXRwQ9br49UxDEwUOYnxWd1Wv31oI=; b=D/sCGFe9rN3/N02vNhWebwMSIawKpPhDYJgVot/Ll2Fl9VfH0O80Zb78bO9t4ytk/j 6SI2UBy99gR6qt0d47p+JbRubFJ7yxgLxzBZxcVh9TdagEcTllKvqxAIGRdfAwu3r3gs vTgaeZO1Qgbp0lrQdXgMwlZHazODVk0UPmSbr6ZlZGqhiVi45qDux7Uk1tqYwlRrTJDi l/kWTZEOvFL/179qBIuWsxWpv2PmhuDWPoPTL2MU11Gc0J+VAeST93FvPEGa2G2pTDhS DYtFY2Nn7D12v64b/XZz3nkKynvCW/H7+pQeSavF6zL0OIOCyXrA3OjHGVtfURaAWQIw p5KQ== X-Gm-Message-State: APjAAAUkkuWS3iwOQJIMjImm3ELeiLn1JypFdkd6MRd0aTtGLDMWzjIB SY3qih/RIk8ZLjIeTrfqGrrEIULY X-Google-Smtp-Source: APXvYqwN3ou/u4aq+bMOvK751uGNcSnUfQoSmVuuQhvy/VGjlOsGz/RkCF7MApziAG8y8ZcAIoGj9A== X-Received: by 2002:a05:6214:6f0:: with SMTP id bk16mr38102qvb.23.1582826595955; Thu, 27 Feb 2020 10:03:15 -0800 (PST) Received: from localhost.localdomain ([191.83.210.181]) by smtp.gmail.com with ESMTPSA id v80sm3523323qka.15.2020.02.27.10.03.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2020 10:03:15 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 27 Feb 2020 15:02:02 -0300 Message-Id: <20200227180202.30982-5-jamrial@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227180202.30982-1-jamrial@gmail.com> References: <20200227180202.30982-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] 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 --- Must be squashed into PATCH 2/2 libavcodec/librav1e.c | 65 ++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/libavcodec/librav1e.c b/libavcodec/librav1e.c index b8b1b4f8f1..02e9c0902f 100644 --- a/libavcodec/librav1e.c +++ b/libavcodec/librav1e.c @@ -30,12 +30,14 @@ #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; AVBSFContext *bsf; uint8_t *pass_data; @@ -166,6 +168,7 @@ static av_cold int librav1e_encode_close(AVCodecContext *avctx) ctx->ctx = NULL; } + av_frame_free(&ctx->frame); av_bsf_free(&ctx->bsf); av_freep(&ctx->pass_data); @@ -180,6 +183,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,27 +406,36 @@ 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; + AVFrame *frame = ctx->frame; RaFrame *rframe = NULL; + RaPacket *rpkt = NULL; int ret; - if (frame) { - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); + if (!frame->buf[0]) { + const AVPixFmtDescriptor *desc; - rframe = rav1e_frame_new(ctx->ctx); - if (!rframe) { - av_log(avctx, AV_LOG_ERROR, "Could not allocate new rav1e frame.\n"); - return AVERROR(ENOMEM); - } + ret = ff_encode_get_frame(avctx, frame); + if (ret < 0 && ret != AVERROR_EOF) + return ret; - for (int i = 0; i < desc->nb_components; i++) { - int shift = i ? desc->log2_chroma_h : 0; - int bytes = desc->comp[0].depth == 8 ? 1 : 2; - rav1e_frame_fill_plane(rframe, i, frame->data[i], - (frame->height >> shift) * frame->linesize[i], - frame->linesize[i], bytes); + if (frame->buf[0]) { + rframe = rav1e_frame_new(ctx->ctx); + if (!rframe) { + av_log(avctx, AV_LOG_ERROR, "Could not allocate new rav1e frame.\n"); + return AVERROR(ENOMEM); + } + + desc = av_pix_fmt_desc_get(frame->format); + for (int i = 0; i < desc->nb_components; i++) { + int shift = i ? desc->log2_chroma_h : 0; + int bytes = desc->comp[0].depth == 8 ? 1 : 2; + rav1e_frame_fill_plane(rframe, i, frame->data[i], + (frame->height >> shift) * frame->linesize[i], + frame->linesize[i], bytes); + } } } @@ -427,28 +443,22 @@ static int librav1e_send_frame(AVCodecContext *avctx, const AVFrame *frame) if (rframe) rav1e_frame_unref(rframe); /* No need to unref if flushing. */ - switch (ret) { + switch(ret) { case RA_ENCODER_STATUS_SUCCESS: + av_frame_unref(frame); 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)); + av_frame_unref(frame); return AVERROR_EXTERNAL; default: av_log(avctx, AV_LOG_ERROR, "Unknown return code %d from rav1e_send_frame: %s\n", ret, rav1e_status_to_str(ret)); + av_frame_unref(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 +483,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 +583,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),