diff mbox series

[FFmpeg-devel,v1,2/2] avcodec/v4l2_buffer: export v4l2 buffer dma-buf

Message ID 20211112083601.6804-2-ming.qian@nxp.com
State New
Headers show
Series [FFmpeg-devel,v1,1/2] avutil/buffer: support represents dma-buf
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Ming Qian Nov. 12, 2021, 8:36 a.m. UTC
Signed-off-by: Ming Qian <ming.qian@nxp.com>
---
 libavcodec/v4l2_buffers.c | 14 ++++++++++++++
 libavcodec/v4l2_buffers.h |  1 +
 libavcodec/v4l2_context.c |  4 ++++
 3 files changed, 19 insertions(+)

Comments

Lynne Nov. 12, 2021, 8:50 a.m. UTC | #1
12 Nov 2021, 09:36 by ming.qian@nxp.com:

> Signed-off-by: Ming Qian <ming.qian@nxp.com>
> ---
>  libavcodec/v4l2_buffers.c | 14 ++++++++++++++
>  libavcodec/v4l2_buffers.h |  1 +
>  libavcodec/v4l2_context.c |  4 ++++
>  3 files changed, 19 insertions(+)
>
> diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
> index 4b2679eb3814..efa42f4c58ea 100644
> --- a/libavcodec/v4l2_buffers.c
> +++ b/libavcodec/v4l2_buffers.c
> @@ -21,6 +21,9 @@
>  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>  */
>  
> +#ifndef _GNU_SOURCE
> +#define _GNU_SOURCE
> +#endif
>  #include <linux/videodev2.h>
>  #include <sys/ioctl.h>
>  #include <sys/mman.h>
> @@ -267,6 +270,8 @@ static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
>  if (!*buf)
>  return AVERROR(ENOMEM);
>  
> +    if (in->plane_info[plane].dmafd >= 0)
> +        av_buffer_set_dma_buf(*buf, in->plane_info[plane].dmafd);
>  ret = v4l2_buf_increase_ref(in);
>  if (ret)
>  av_buffer_unref(buf);
> @@ -494,6 +499,7 @@ int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out)
>  int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
>  {
>  V4L2Context *ctx = avbuf->context;
> +    struct v4l2_exportbuffer exp;
>  int ret, i;
>  
>  avbuf->buf.memory = V4L2_MEMORY_MMAP;
> @@ -539,6 +545,14 @@ int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
>  
>  if (avbuf->plane_info[i].mm_addr == MAP_FAILED)
>  return AVERROR(ENOMEM);
> +
> +        exp.type = ctx->type;
> +        exp.index = avbuf->buf.index;
> +        exp.plane = i;
> +        exp.fd = -1;
> +        exp.flags = O_CLOEXEC | O_RDWR;
> +        ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_EXPBUF, &exp);
> +        avbuf->plane_info[i].dmafd = exp.fd;
>  }
>  
>  avbuf->status = V4L2BUF_AVAILABLE;
> diff --git a/libavcodec/v4l2_buffers.h b/libavcodec/v4l2_buffers.h
> index 3d2ff1b9a5d7..74550544e0eb 100644
> --- a/libavcodec/v4l2_buffers.h
> +++ b/libavcodec/v4l2_buffers.h
> @@ -55,6 +55,7 @@ typedef struct V4L2Buffer {
>  int bytesperline;
>  void * mm_addr;
>  size_t length;
> +        int dmafd;
>  } plane_info[VIDEO_MAX_PLANES];
>  
>  int num_planes;
> diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
> index b08f0015c2e5..f0406194293d 100644
> --- a/libavcodec/v4l2_context.c
> +++ b/libavcodec/v4l2_context.c
> @@ -453,6 +453,10 @@ static int v4l2_release_buffers(V4L2Context* ctx)
>  if (p->mm_addr && p->length)
>  if (munmap(p->mm_addr, p->length) < 0)
>  av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno)));
> +            if (p->dmafd >= 0) {
> +                close(p->dmafd);
> +                p->dmafd = -1;
> +            }
>  }
>  } 
>

We already have a standard structure for representing DRM frames,
it's AV_PIX_FMT_DRM_PRIME, which encapsulates a AVDRMFrameDescriptor,
which contains DRM fds for every plane, and layer description.

I think there may have been an old patch which did this, but I don't remember.
Ming Qian Nov. 15, 2021, 2:49 a.m. UTC | #2
> -----Original Message-----
> From: ffmpeg-devel [mailto:ffmpeg-devel-bounces@ffmpeg.org] On Behalf Of
> Lynne
> Sent: Friday, November 12, 2021 4:50 PM
> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
> Subject: [EXT] Re: [FFmpeg-devel] [PATCH v1 2/2] avcodec/v4l2_buffer: export
> v4l2 buffer dma-buf
> 
> Caution: EXT Email
> 
> 12 Nov 2021, 09:36 by ming.qian@nxp.com:
> 
> > Signed-off-by: Ming Qian <ming.qian@nxp.com>
> > ---
> >  libavcodec/v4l2_buffers.c | 14 ++++++++++++++
> > libavcodec/v4l2_buffers.h |  1 +  libavcodec/v4l2_context.c |  4 ++++
> >  3 files changed, 19 insertions(+)
> >
> > diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
> > index 4b2679eb3814..efa42f4c58ea 100644
> > --- a/libavcodec/v4l2_buffers.c
> > +++ b/libavcodec/v4l2_buffers.c
> > @@ -21,6 +21,9 @@
> >  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> > 02110-1301 USA  */
> >
> > +#ifndef _GNU_SOURCE
> > +#define _GNU_SOURCE
> > +#endif
> >  #include <linux/videodev2.h>
> >  #include <sys/ioctl.h>
> >  #include <sys/mman.h>
> > @@ -267,6 +270,8 @@ static int v4l2_buf_to_bufref(V4L2Buffer *in, int
> > plane, AVBufferRef **buf)  if (!*buf)  return AVERROR(ENOMEM);
> >
> > +    if (in->plane_info[plane].dmafd >= 0)
> > +        av_buffer_set_dma_buf(*buf, in->plane_info[plane].dmafd);
> >  ret = v4l2_buf_increase_ref(in);
> >  if (ret)
> >  av_buffer_unref(buf);
> > @@ -494,6 +499,7 @@ int ff_v4l2_buffer_avpkt_to_buf(const AVPacket
> > *pkt, V4L2Buffer *out)  int ff_v4l2_buffer_initialize(V4L2Buffer*
> > avbuf, int index)  {  V4L2Context *ctx = avbuf->context;
> > +    struct v4l2_exportbuffer exp;
> >  int ret, i;
> >
> >  avbuf->buf.memory = V4L2_MEMORY_MMAP; @@ -539,6 +545,14 @@
> int
> > ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
> >
> >  if (avbuf->plane_info[i].mm_addr == MAP_FAILED)  return
> > AVERROR(ENOMEM);
> > +
> > +        exp.type = ctx->type;
> > +        exp.index = avbuf->buf.index;
> > +        exp.plane = i;
> > +        exp.fd = -1;
> > +        exp.flags = O_CLOEXEC | O_RDWR;
> > +        ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_EXPBUF, &exp);
> > +        avbuf->plane_info[i].dmafd = exp.fd;
> >  }
> >
> >  avbuf->status = V4L2BUF_AVAILABLE;
> > diff --git a/libavcodec/v4l2_buffers.h b/libavcodec/v4l2_buffers.h
> > index 3d2ff1b9a5d7..74550544e0eb 100644
> > --- a/libavcodec/v4l2_buffers.h
> > +++ b/libavcodec/v4l2_buffers.h
> > @@ -55,6 +55,7 @@ typedef struct V4L2Buffer {  int bytesperline;  void
> > * mm_addr;  size_t length;
> > +        int dmafd;
> >  } plane_info[VIDEO_MAX_PLANES];
> >
> >  int num_planes;
> > diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
> > index b08f0015c2e5..f0406194293d 100644
> > --- a/libavcodec/v4l2_context.c
> > +++ b/libavcodec/v4l2_context.c
> > @@ -453,6 +453,10 @@ static int v4l2_release_buffers(V4L2Context* ctx)
> > if (p->mm_addr && p->length)  if (munmap(p->mm_addr, p->length) < 0)
> > av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name,
> > av_err2str(AVERROR(errno)));
> > +            if (p->dmafd >= 0) {
> > +                close(p->dmafd);
> > +                p->dmafd = -1;
> > +            }
> >  }
> >  }
> >
> 
> We already have a standard structure for representing DRM frames, it's
> AV_PIX_FMT_DRM_PRIME, which encapsulates a AVDRMFrameDescriptor,
> which contains DRM fds for every plane, and layer description.
> 
> I think there may have been an old patch which did this, but I don't remember.

Thanks for your information, I'll check it

> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fffmpeg.
> org%2Fmailman%2Flistinfo%2Fffmpeg-devel&amp;data=04%7C01%7Cming.qi
> an%40nxp.com%7Caf21432e4cff441f393f08d9a5b97e57%7C686ea1d3bc2b4
> c6fa92cd99c5c301635%7C0%7C1%7C637723038369699316%7CUnknown%7
> CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiL
> CJXVCI6Mn0%3D%7C3000&amp;sdata=%2ByZOR2mWn1oTwn5xwkWFkbpb3
> bovbbEpXmH0T1D36%2FY%3D&amp;reserved=0
> 
> To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org
> with subject "unsubscribe".
Ming Qian Nov. 15, 2021, 5:43 a.m. UTC | #3
> -----Original Message-----
> From: ffmpeg-devel [mailto:ffmpeg-devel-bounces@ffmpeg.org] On Behalf Of
> Ming Qian
> Sent: Monday, November 15, 2021 10:49 AM
> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
> Subject: Re: [FFmpeg-devel] [EXT] Re: [PATCH v1 2/2] avcodec/v4l2_buffer:
> export v4l2 buffer dma-buf
> 
> Caution: EXT Email
> 
> > -----Original Message-----
> > From: ffmpeg-devel [mailto:ffmpeg-devel-bounces@ffmpeg.org] On Behalf
> > Of Lynne
> > Sent: Friday, November 12, 2021 4:50 PM
> > To: FFmpeg development discussions and patches
> > <ffmpeg-devel@ffmpeg.org>
> > Subject: [EXT] Re: [FFmpeg-devel] [PATCH v1 2/2] avcodec/v4l2_buffer:
> > export
> > v4l2 buffer dma-buf
> >
> > Caution: EXT Email
> >
> > 12 Nov 2021, 09:36 by ming.qian@nxp.com:
> >
> > > Signed-off-by: Ming Qian <ming.qian@nxp.com>
> > > ---
> > >  libavcodec/v4l2_buffers.c | 14 ++++++++++++++
> > > libavcodec/v4l2_buffers.h |  1 +  libavcodec/v4l2_context.c |  4
> > > ++++
> > >  3 files changed, 19 insertions(+)
> > >
> > > diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
> > > index 4b2679eb3814..efa42f4c58ea 100644
> > > --- a/libavcodec/v4l2_buffers.c
> > > +++ b/libavcodec/v4l2_buffers.c
> > > @@ -21,6 +21,9 @@
> > >  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> > > 02110-1301 USA  */
> > >
> > > +#ifndef _GNU_SOURCE
> > > +#define _GNU_SOURCE
> > > +#endif
> > >  #include <linux/videodev2.h>
> > >  #include <sys/ioctl.h>
> > >  #include <sys/mman.h>
> > > @@ -267,6 +270,8 @@ static int v4l2_buf_to_bufref(V4L2Buffer *in,
> > > int plane, AVBufferRef **buf)  if (!*buf)  return AVERROR(ENOMEM);
> > >
> > > +    if (in->plane_info[plane].dmafd >= 0)
> > > +        av_buffer_set_dma_buf(*buf, in->plane_info[plane].dmafd);
> > >  ret = v4l2_buf_increase_ref(in);
> > >  if (ret)
> > >  av_buffer_unref(buf);
> > > @@ -494,6 +499,7 @@ int ff_v4l2_buffer_avpkt_to_buf(const AVPacket
> > > *pkt, V4L2Buffer *out)  int ff_v4l2_buffer_initialize(V4L2Buffer*
> > > avbuf, int index)  {  V4L2Context *ctx = avbuf->context;
> > > +    struct v4l2_exportbuffer exp;
> > >  int ret, i;
> > >
> > >  avbuf->buf.memory = V4L2_MEMORY_MMAP; @@ -539,6 +545,14 @@
> > int
> > > ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
> > >
> > >  if (avbuf->plane_info[i].mm_addr == MAP_FAILED)  return
> > > AVERROR(ENOMEM);
> > > +
> > > +        exp.type = ctx->type;
> > > +        exp.index = avbuf->buf.index;
> > > +        exp.plane = i;
> > > +        exp.fd = -1;
> > > +        exp.flags = O_CLOEXEC | O_RDWR;
> > > +        ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_EXPBUF, &exp);
> > > +        avbuf->plane_info[i].dmafd = exp.fd;
> > >  }
> > >
> > >  avbuf->status = V4L2BUF_AVAILABLE;
> > > diff --git a/libavcodec/v4l2_buffers.h b/libavcodec/v4l2_buffers.h
> > > index 3d2ff1b9a5d7..74550544e0eb 100644
> > > --- a/libavcodec/v4l2_buffers.h
> > > +++ b/libavcodec/v4l2_buffers.h
> > > @@ -55,6 +55,7 @@ typedef struct V4L2Buffer {  int bytesperline;
> > > void
> > > * mm_addr;  size_t length;
> > > +        int dmafd;
> > >  } plane_info[VIDEO_MAX_PLANES];
> > >
> > >  int num_planes;
> > > diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
> > > index b08f0015c2e5..f0406194293d 100644
> > > --- a/libavcodec/v4l2_context.c
> > > +++ b/libavcodec/v4l2_context.c
> > > @@ -453,6 +453,10 @@ static int v4l2_release_buffers(V4L2Context*
> > > ctx) if (p->mm_addr && p->length)  if (munmap(p->mm_addr, p->length)
> > > < 0) av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n",
> > > ctx->name, av_err2str(AVERROR(errno)));
> > > +            if (p->dmafd >= 0) {
> > > +                close(p->dmafd);
> > > +                p->dmafd = -1;
> > > +            }
> > >  }
> > >  }
> > >
> >
> > We already have a standard structure for representing DRM frames, it's
> > AV_PIX_FMT_DRM_PRIME, which encapsulates a AVDRMFrameDescriptor,
> which
> > contains DRM fds for every plane, and layer description.
> >
> > I think there may have been an old patch which did this, but I don't
> remember.
> 
> Thanks for your information, I'll check it
> 

Hi Lynne,
    Thank you again, I got the point, and then this patch can be dropped now.
    I'll try to export to AV_PIX_FMT_DRM_PRIME in v4l2 codec

> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> >
> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fffmpeg.
> >
> org%2Fmailman%2Flistinfo%2Fffmpeg-devel&amp;data=04%7C01%7Cming.qi
> >
> an%40nxp.com%7Caf21432e4cff441f393f08d9a5b97e57%7C686ea1d3bc2b4
> >
> c6fa92cd99c5c301635%7C0%7C1%7C637723038369699316%7CUnknown%7
> >
> CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiL
> >
> CJXVCI6Mn0%3D%7C3000&amp;sdata=%2ByZOR2mWn1oTwn5xwkWFkbpb3
> > bovbbEpXmH0T1D36%2FY%3D&amp;reserved=0
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fffmpeg.
> org%2Fmailman%2Flistinfo%2Fffmpeg-devel&amp;data=04%7C01%7Cming.qi
> an%40nxp.com%7C10d87aef6ebe4038b01508d9a7e2890f%7C686ea1d3bc2b
> 4c6fa92cd99c5c301635%7C0%7C1%7C637725413673582009%7CUnknown%
> 7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwi
> LCJXVCI6Mn0%3D%7C3000&amp;sdata=pNwzW%2BVZnNNL8vNXzhvJhCrol2F
> SbWHw7FdqSVFh88Q%3D&amp;reserved=0
> 
> To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org
> with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
index 4b2679eb3814..efa42f4c58ea 100644
--- a/libavcodec/v4l2_buffers.c
+++ b/libavcodec/v4l2_buffers.c
@@ -21,6 +21,9 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
 #include <linux/videodev2.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
@@ -267,6 +270,8 @@  static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
     if (!*buf)
         return AVERROR(ENOMEM);
 
+    if (in->plane_info[plane].dmafd >= 0)
+        av_buffer_set_dma_buf(*buf, in->plane_info[plane].dmafd);
     ret = v4l2_buf_increase_ref(in);
     if (ret)
         av_buffer_unref(buf);
@@ -494,6 +499,7 @@  int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out)
 int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
 {
     V4L2Context *ctx = avbuf->context;
+    struct v4l2_exportbuffer exp;
     int ret, i;
 
     avbuf->buf.memory = V4L2_MEMORY_MMAP;
@@ -539,6 +545,14 @@  int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
 
         if (avbuf->plane_info[i].mm_addr == MAP_FAILED)
             return AVERROR(ENOMEM);
+
+        exp.type = ctx->type;
+        exp.index = avbuf->buf.index;
+        exp.plane = i;
+        exp.fd = -1;
+        exp.flags = O_CLOEXEC | O_RDWR;
+        ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_EXPBUF, &exp);
+        avbuf->plane_info[i].dmafd = exp.fd;
     }
 
     avbuf->status = V4L2BUF_AVAILABLE;
diff --git a/libavcodec/v4l2_buffers.h b/libavcodec/v4l2_buffers.h
index 3d2ff1b9a5d7..74550544e0eb 100644
--- a/libavcodec/v4l2_buffers.h
+++ b/libavcodec/v4l2_buffers.h
@@ -55,6 +55,7 @@  typedef struct V4L2Buffer {
         int bytesperline;
         void * mm_addr;
         size_t length;
+        int dmafd;
     } plane_info[VIDEO_MAX_PLANES];
 
     int num_planes;
diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
index b08f0015c2e5..f0406194293d 100644
--- a/libavcodec/v4l2_context.c
+++ b/libavcodec/v4l2_context.c
@@ -453,6 +453,10 @@  static int v4l2_release_buffers(V4L2Context* ctx)
             if (p->mm_addr && p->length)
                 if (munmap(p->mm_addr, p->length) < 0)
                     av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno)));
+            if (p->dmafd >= 0) {
+                close(p->dmafd);
+                p->dmafd = -1;
+            }
         }
     }