From patchwork Tue Sep 3 01:02:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 14860 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 82E17449F2B for ; Tue, 3 Sep 2019 04:03:17 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 702CC687FB3; Tue, 3 Sep 2019 04:03:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f194.google.com (mail-oi1-f194.google.com [209.85.167.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D7CAE687FB1 for ; Tue, 3 Sep 2019 04:03:15 +0300 (EEST) Received: by mail-oi1-f194.google.com with SMTP id t24so11481809oij.13 for ; Mon, 02 Sep 2019 18:03:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tmm1-net.20150623.gappssmtp.com; s=20150623; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sbNzLTrdxH0cXXYlcTzUczwD82PLaa7JQrHM/r29Gq4=; b=YzFuP6LVy1o64z2BkGhCSy+22xKQOCAZIAYPrl01cmdQ0b7AvVuMfE2U/IJmPJynPx IFqh0JsLLYdSulumbpZrM981VTiU0nwpqDYOKsSYRV4C0F3SvJx2Sa484Snds85zEcRJ lqu5/HfPQjQ/NuaUZuOg9VKpvvlNxASHOny6GGjHntiGnciYQ0mAwByjsnQ8CRfmXCx1 Mu1eeO4g6svLrZPVX9Sv3M5AJUe340W6rHl7EgcVvtMB8HK3vu7CfGV7G6nq2ACqhK1l GNUy8KbT9SHcIozXW4HlKCRM52U5RfBBiV8QiuWUHPUaBLarqawtC/pqWYLTVuRNX3gO JpbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=sbNzLTrdxH0cXXYlcTzUczwD82PLaa7JQrHM/r29Gq4=; b=Fzkr1s3EoBIaX9s4IRuTSqTcGTF5SobpW44kLKFWdulAcLdlgkgDxSSl82Hz/d9cjA 4lUaAONyCpeNz9w49FyzJ8ZN+2YpMDlKidAhzyEKxFHkm9XX3fI3D+ACh80JF0DNV4+B 1fInNzp9Gi/u/yDGsMVDUV0OkJ6auvVNioob1YS9ZhkcCQi+ecGEr4krfHFydOCxzTG6 9SzCMz/rqeoY34775Teis3rncqGh4JIXFriGSXh2xj/kJYAmAzsMYvtlGMHKU7z2RwLX /rEt7jGD0MSa4g/CuMYlCcmV9A1LTeM9NBfdGwrCMq0ZoOpkIMgN48pyU8rbW9CdiSCZ nlWQ== X-Gm-Message-State: APjAAAXiepvCeA/jRizasB1mOS2FcgKN7lb6gZ5+21c6l7KsH1PFV5zW gCPOeH2mFxdXAjgmJnut6+dWNzuoAY0= X-Google-Smtp-Source: APXvYqxWMq7mWkmVRggIRXT/3x7Pvf7ERESOPc2b8LUAnnWPVXvDDVVsFd+phD1r71KDrQuhI9ujxQ== X-Received: by 2002:aca:5c85:: with SMTP id q127mr12741000oib.164.1567472594166; Mon, 02 Sep 2019 18:03:14 -0700 (PDT) Received: from tmm1-imac.lan (ip184-189-221-177.sb.sd.cox.net. [184.189.221.177]) by smtp.gmail.com with ESMTPSA id a21sm4299633oie.9.2019.09.02.18.03.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 02 Sep 2019 18:03:13 -0700 (PDT) From: Aman Gupta To: ffmpeg-devel@ffmpeg.org Date: Mon, 2 Sep 2019 18:02:21 -0700 Message-Id: <20190903010230.96236-17-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190903010230.96236-1-ffmpeg@tmm1.net> References: <20190903010230.96236-1-ffmpeg@tmm1.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 16/25] avcodec/v4l2_m2m_dec: add support for AV_PIX_FMT_DRM_PRIME 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: lorusak@gmail.com, Aman Gupta , jorge.ramirez.ortiz@gmail.com, Aman Gupta Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Aman Gupta Based on patchset submitted to ffmpeg-devel by Lukas Rusak Signed-off-by: Aman Gupta --- libavcodec/v4l2_m2m_dec.c | 85 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c index a3744208f3..6b73d2ffd9 100644 --- a/libavcodec/v4l2_m2m_dec.c +++ b/libavcodec/v4l2_m2m_dec.c @@ -23,11 +23,16 @@ #include #include + +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_drm.h" #include "libavutil/pixfmt.h" #include "libavutil/pixdesc.h" #include "libavutil/opt.h" #include "libavcodec/avcodec.h" #include "libavcodec/decode.h" +#include "libavcodec/hwaccel.h" +#include "libavcodec/internal.h" #include "v4l2_context.h" #include "v4l2_m2m.h" @@ -39,7 +44,7 @@ static int v4l2_try_start(AVCodecContext *avctx) V4L2Context *const capture = &s->capture; V4L2Context *const output = &s->output; struct v4l2_selection selection; - int ret; + int ret, pix_fmt; /* 1. start the output process */ if (!output->streamon) { @@ -62,8 +67,13 @@ static int v4l2_try_start(AVCodecContext *avctx) } /* 2.1 update the AVCodecContext */ - avctx->pix_fmt = ff_v4l2_format_v4l2_to_avfmt(capture->format.fmt.pix_mp.pixelformat, AV_CODEC_ID_RAWVIDEO); + pix_fmt = ff_v4l2_format_v4l2_to_avfmt(capture->format.fmt.pix_mp.pixelformat, AV_CODEC_ID_RAWVIDEO); + if (avctx->pix_fmt != AV_PIX_FMT_DRM_PRIME) + avctx->pix_fmt = pix_fmt; + else + avctx->sw_pix_fmt = pix_fmt; capture->av_pix_fmt = avctx->pix_fmt; + capture->sw_pix_fmt = avctx->sw_pix_fmt; /* 3. set the crop parameters */ selection.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; @@ -189,15 +199,42 @@ static av_cold int v4l2_decode_init(AVCodecContext *avctx) output->av_codec_id = avctx->codec_id; output->av_pix_fmt = AV_PIX_FMT_NONE; + /* negotiate drm vs software pixel formats */ + avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts); + switch (avctx->pix_fmt) { + case AV_PIX_FMT_DRM_PRIME: + avctx->sw_pix_fmt = AV_PIX_FMT_NV12; + break; + + case AV_PIX_FMT_NONE: + return 0; + break; + + default: + break; + } + capture->av_codec_id = AV_CODEC_ID_RAWVIDEO; capture->av_pix_fmt = avctx->pix_fmt; + capture->sw_pix_fmt = avctx->sw_pix_fmt; + + if (avctx->hw_device_ctx) { + s->device_ref = av_buffer_ref(avctx->hw_device_ctx); + } else { + s->device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM); + if (!s->device_ref) + return AVERROR(ENOMEM); + + ret = av_hwdevice_ctx_init(s->device_ref); + if (ret < 0) { + av_buffer_unref(&s->device_ref); + return ret; + } + } ret = ff_v4l2_m2m_codec_init(priv); if (ret) { av_log(avctx, AV_LOG_ERROR, "can't configure decoder\n"); - s->self_ref = NULL; - av_buffer_unref(&priv->context_ref); - return ret; } s->avctx = avctx; @@ -210,6 +247,25 @@ static av_cold int v4l2_decode_close(AVCodecContext *avctx) return ff_v4l2_m2m_codec_end(avctx->priv_data); } +static void v4l2_flush(AVCodecContext *avctx) +{ + V4L2m2mPriv *priv = avctx->priv_data; + V4L2m2mContext* s = priv->context; + int ret; + + /* wait for pending buffer references */ + if (atomic_load(&s->refcount)) + while(sem_wait(&s->refsync) == -1 && errno == EINTR); + + ret = ff_v4l2_context_set_status(&s->output, VIDIOC_STREAMOFF); + if (ret) + av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->output.name); + + ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF); + if (ret) + av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->capture.name); +} + #define OFFSET(x) offsetof(V4L2m2mPriv, x) #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM @@ -220,6 +276,19 @@ static const AVOption options[] = { { NULL}, }; +static const AVCodecHWConfigInternal *v4l2_m2m_hw_configs[] = { + &(const AVCodecHWConfigInternal) { + .public = { + .pix_fmt = AV_PIX_FMT_DRM_PRIME, + .methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | + AV_CODEC_HW_CONFIG_METHOD_INTERNAL, + .device_type = AV_HWDEVICE_TYPE_DRM + }, + .hwaccel = NULL, + }, + NULL +}; + #define M2MDEC_CLASS(NAME) \ static const AVClass v4l2_m2m_ ## NAME ## _dec_class = { \ .class_name = #NAME "_v4l2m2m_decoder", \ @@ -240,7 +309,13 @@ static const AVOption options[] = { .init = v4l2_decode_init, \ .receive_frame = v4l2_receive_frame, \ .close = v4l2_decode_close, \ + .flush = v4l2_flush, \ + .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_DRM_PRIME, \ + AV_PIX_FMT_NV12, \ + AV_PIX_FMT_YUV420P, \ + AV_PIX_FMT_NONE}, \ .bsfs = bsf_name, \ + .hw_configs = v4l2_m2m_hw_configs, \ .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \ .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \ .wrapper_name = "v4l2m2m", \