From patchwork Sun Mar 8 00:23:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andriy Gelman X-Patchwork-Id: 18087 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 8FB9D44B400 for ; Sun, 8 Mar 2020 02:29:34 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 664EF68A938; Sun, 8 Mar 2020 02:29:34 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f194.google.com (mail-qt1-f194.google.com [209.85.160.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B0986689DAF for ; Sun, 8 Mar 2020 02:29:27 +0200 (EET) Received: by mail-qt1-f194.google.com with SMTP id l21so4544166qtr.8 for ; Sat, 07 Mar 2020 16:29:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=le5McliYwesCRBEnxcYWIyXMMu9l3/ZBKr26M2S4FOI=; b=ISadqAaXC9wJm+DYizZl3nJSLqh+9F/IjNk0lAdpXKLb/EW3JRDuyN68BqxQEcs3it 4rRddcSMNKIfjD4fb7hK3XzldlfYXSBmrFXpDCTWRi4P5fjxfRC5D8OyjJ3ps4WIlAAc +kIQpKLTQNfw5X/7902ilFoORXybuFsaTVpXE17yyTmmrbrUBb96trUV3pwpjOEy4hFL 5ZJjsf1CJ2X5JE+WybfhY1vR9oMft20Uyzw2BprgPLv6UWITLDM0DXcazJbm7jqI3PTN jmId9iP2gZmZizk+IB5P++2gE5z6a21utJgO+kYewduxbMQ5RksaNZ+vD+gZrWjcgGCI ihKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=le5McliYwesCRBEnxcYWIyXMMu9l3/ZBKr26M2S4FOI=; b=jHAmJ8rPWCdFF8I8V/dkr2k1Pt26BW5rJsXXujevgJglO7OPCew9L8YWP2N9+nioBS mQCHxvZJjajKW1YHjtoet8/1BwjEEbq8F8SqBOsj5wi5AIOBCabwxZoGSWkOB0yDChCY m0RFIOM67HpDLzdKFy45AwLiC/fAREZCg0zMuLVo/DNJTRCodbhKFr4JbkPh3M7/eUJp +JguJ8DQV3jhth5cWrgRVQEQaDzYOOUbo0znEn0bdTlzzg71rKNZPEWI/CozcZDmNUzW IrpSf3Vw5j//NVuLugcf5onb5DXjkUjc/5EKiviX9/Z0MrV61GVAGzWcbMw//o7nzm2p 1CNA== X-Gm-Message-State: ANhLgQ0e8aN0HaieuhBXY6S+C4wzQomxQimfZCw6WHG2ct/+CHnOKR5J BZy6Mt/VfCvmcQDXDF9UyaQcM4M4 X-Google-Smtp-Source: ADFU+vtm3kFkVeXTm2nqrcujysnUZiflCQuYbDVHJktKLGmH8ONr60aCl1GD7y2bBZEzb+jR5yz4QQ== X-Received: by 2002:ac8:7b5b:: with SMTP id m27mr6914109qtu.317.1583627004136; Sat, 07 Mar 2020 16:23:24 -0800 (PST) Received: from localhost.localdomain (c-71-232-27-28.hsd1.ma.comcast.net. [71.232.27.28]) by smtp.gmail.com with ESMTPSA id s139sm15993884qke.70.2020.03.07.16.23.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2020 16:23:23 -0800 (PST) From: Andriy Gelman X-Google-Original-From: Andriy Gelman To: ffmpeg-devel@ffmpeg.org Date: Sat, 7 Mar 2020 19:23:05 -0500 Message-Id: <20200308002305.10822-1-andriy.gelman@gmail.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/v4l2_m2m_enc: 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 Cc: Andriy Gelman Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Andriy Gelman Should be squashed with: http://ffmpeg.org/pipermail/ffmpeg-devel/2020-February/257735.html Signed-off-by: Andriy Gelman --- libavcodec/v4l2_m2m.c | 9 ++++++++- libavcodec/v4l2_m2m.h | 6 +++++- libavcodec/v4l2_m2m_dec.c | 2 +- libavcodec/v4l2_m2m_enc.c | 17 +++++++++++++++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/libavcodec/v4l2_m2m.c b/libavcodec/v4l2_m2m.c index 2d21f910bcc..1799837fb9f 100644 --- a/libavcodec/v4l2_m2m.c +++ b/libavcodec/v4l2_m2m.c @@ -329,6 +329,7 @@ static void v4l2_m2m_destroy_context(void *opaque, uint8_t *context) sem_destroy(&s->refsync); close(s->fd); + av_frame_free(&s->frame); av_free(s); } @@ -394,7 +395,7 @@ int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv) return v4l2_configure_contexts(s); } -int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s) +int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, int is_encoder, V4L2m2mContext **s) { *s = av_mallocz(sizeof(V4L2m2mContext)); if (!*s) @@ -417,5 +418,11 @@ int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s) priv->context->self_ref = priv->context_ref; priv->context->fd = -1; + if (is_encoder) { + priv->context->frame = av_frame_alloc(); + if (!priv->context->frame) + return AVERROR(ENOMEM); + } + return 0; } diff --git a/libavcodec/v4l2_m2m.h b/libavcodec/v4l2_m2m.h index 456281f48c5..5d6106224dd 100644 --- a/libavcodec/v4l2_m2m.h +++ b/libavcodec/v4l2_m2m.h @@ -58,6 +58,9 @@ typedef struct V4L2m2mContext { int draining; AVPacket buf_pkt; + /* Reference to a frame. Only used during encoding */ + AVFrame *frame; + /* Reference to self; only valid while codec is active. */ AVBufferRef *self_ref; @@ -79,11 +82,12 @@ typedef struct V4L2m2mPriv { * Allocate a new context and references for a V4L2 M2M instance. * * @param[in] ctx The V4L2m2mPriv instantiated by the encoder/decoder. + * @param[in] is_encoder Whether the context is for encoder/decoder * @param[out] ctx The V4L2m2mContext. * * @returns 0 in success, a negative error code otherwise. */ -int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s); +int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, int is_encoder, V4L2m2mContext **s); /** diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c index d666edffe46..7c7ac2de8c5 100644 --- a/libavcodec/v4l2_m2m_dec.c +++ b/libavcodec/v4l2_m2m_dec.c @@ -181,7 +181,7 @@ static av_cold int v4l2_decode_init(AVCodecContext *avctx) V4L2m2mPriv *priv = avctx->priv_data; int ret; - ret = ff_v4l2_m2m_create_context(priv, &s); + ret = ff_v4l2_m2m_create_context(priv, !av_codec_is_decoder(avctx->codec), &s); if (ret < 0) return ret; diff --git a/libavcodec/v4l2_m2m_enc.c b/libavcodec/v4l2_m2m_enc.c index c9f1741bfd0..22b0cbd27ac 100644 --- a/libavcodec/v4l2_m2m_enc.c +++ b/libavcodec/v4l2_m2m_enc.c @@ -24,6 +24,7 @@ #include #include #include +#include "encode.h" #include "libavcodec/avcodec.h" #include "libavutil/pixdesc.h" #include "libavutil/pixfmt.h" @@ -259,11 +260,24 @@ static int v4l2_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context; V4L2Context *const capture = &s->capture; V4L2Context *const output = &s->output; + AVFrame *frame = s->frame; int ret; if (s->draining) goto dequeue; + ret = ff_encode_get_frame(avctx, frame); + if (ret < 0 && ret != AVERROR_EOF) + return ret; + + if (ret == AVERROR_EOF) + frame = NULL; + + ret = v4l2_send_frame(avctx, frame); + av_frame_unref(frame); + if (ret < 0) + return ret; + if (!output->streamon) { ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON); if (ret) { @@ -293,7 +307,7 @@ static av_cold int v4l2_encode_init(AVCodecContext *avctx) uint32_t v4l2_fmt_output; int ret; - ret = ff_v4l2_m2m_create_context(priv, &s); + ret = ff_v4l2_m2m_create_context(priv, !av_codec_is_decoder(avctx->codec), &s); if (ret < 0) return ret; @@ -367,7 +381,6 @@ static const AVOption options[] = { .priv_data_size = sizeof(V4L2m2mPriv), \ .priv_class = &v4l2_m2m_ ## NAME ##_enc_class, \ .init = v4l2_encode_init, \ - .send_frame = v4l2_send_frame, \ .receive_packet = v4l2_receive_packet, \ .close = v4l2_encode_close, \ .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY, \