diff mbox

[FFmpeg-devel] hwcontext_cuda: implement frames_get_constraints

Message ID 20170116154416.19116-1-nfxjfg@googlemail.com
State Accepted
Commit c16fe1432d88f87a96be9e943e0f1229543ad61d
Headers show

Commit Message

wm4 Jan. 16, 2017, 3:44 p.m. UTC
Copied and modified from hwcontext_qsv.c.
---
 libavutil/hwcontext_cuda.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

Comments

Philip Langdale Jan. 16, 2017, 5:31 p.m. UTC | #1
On Mon, 16 Jan 2017 16:44:16 +0100
wm4 <nfxjfg@googlemail.com> wrote:

> Copied and modified from hwcontext_qsv.c.
> ---
>  libavutil/hwcontext_cuda.c | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
> 
> diff --git a/libavutil/hwcontext_cuda.c b/libavutil/hwcontext_cuda.c
> index 5dd0d99272..ed595c3e0f 100644
> --- a/libavutil/hwcontext_cuda.c
> +++ b/libavutil/hwcontext_cuda.c
> @@ -39,6 +39,31 @@ static const enum AVPixelFormat
> supported_formats[] = { AV_PIX_FMT_P016,
>  };
>  
> +static int cuda_frames_get_constraints(AVHWDeviceContext *ctx,
> +                                       const void *hwconfig,
> +                                       AVHWFramesConstraints
> *constraints) +{
> +    int i;
> +
> +    constraints->valid_sw_formats =
> av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
> +
> sizeof(*constraints->valid_sw_formats));
> +    if (!constraints->valid_sw_formats)
> +        return AVERROR(ENOMEM);
> +
> +    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
> +        constraints->valid_sw_formats[i] = supported_formats[i];
> +    constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_formats)]
> = AV_PIX_FMT_NONE; +
> +    constraints->valid_hw_formats = av_malloc_array(2,
> sizeof(*constraints->valid_hw_formats));
> +    if (!constraints->valid_hw_formats)
> +        return AVERROR(ENOMEM);

Is it weird if this fails and valid_sw_formats succeeds? I guess that's
up to how the caller handles the error.

> +
> +    constraints->valid_hw_formats[0] = AV_PIX_FMT_CUDA;
> +    constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
> +
> +    return 0;
> +}
> +
>  static void cuda_buffer_free(void *opaque, uint8_t *data)
>  {
>      AVHWFramesContext *ctx = opaque;
> @@ -376,6 +401,7 @@ const HWContextType ff_hwcontext_type_cuda = {
>      .device_create        = cuda_device_create,
>      .device_init          = cuda_device_init,
>      .device_uninit        = cuda_device_uninit,
> +    .frames_get_constraints = cuda_frames_get_constraints,
>      .frames_init          = cuda_frames_init,
>      .frames_get_buffer    = cuda_get_buffer,
>      .transfer_get_formats = cuda_transfer_get_formats,

--phil
wm4 Jan. 16, 2017, 5:56 p.m. UTC | #2
On Mon, 16 Jan 2017 09:31:42 -0800
Philip Langdale <philipl@overt.org> wrote:

> On Mon, 16 Jan 2017 16:44:16 +0100
> wm4 <nfxjfg@googlemail.com> wrote:
> 
> > Copied and modified from hwcontext_qsv.c.
> > ---
> >  libavutil/hwcontext_cuda.c | 26 ++++++++++++++++++++++++++
> >  1 file changed, 26 insertions(+)
> > 
> > diff --git a/libavutil/hwcontext_cuda.c b/libavutil/hwcontext_cuda.c
> > index 5dd0d99272..ed595c3e0f 100644
> > --- a/libavutil/hwcontext_cuda.c
> > +++ b/libavutil/hwcontext_cuda.c
> > @@ -39,6 +39,31 @@ static const enum AVPixelFormat
> > supported_formats[] = { AV_PIX_FMT_P016,
> >  };
> >  
> > +static int cuda_frames_get_constraints(AVHWDeviceContext *ctx,
> > +                                       const void *hwconfig,
> > +                                       AVHWFramesConstraints
> > *constraints) +{
> > +    int i;
> > +
> > +    constraints->valid_sw_formats =
> > av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
> > +
> > sizeof(*constraints->valid_sw_formats));
> > +    if (!constraints->valid_sw_formats)
> > +        return AVERROR(ENOMEM);
> > +
> > +    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
> > +        constraints->valid_sw_formats[i] = supported_formats[i];
> > +    constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_formats)]
> > = AV_PIX_FMT_NONE; +
> > +    constraints->valid_hw_formats = av_malloc_array(2,
> > sizeof(*constraints->valid_hw_formats));
> > +    if (!constraints->valid_hw_formats)
> > +        return AVERROR(ENOMEM);  
> 
> Is it weird if this fails and valid_sw_formats succeeds? I guess that's
> up to how the caller handles the error.

It is, but the QSV code also did it this way.

And if we're completely honest, nobody really cares about tiny leaks
when tiny allocations fail (which in practice never ever happens
anyway).
Philip Langdale Jan. 16, 2017, 6:01 p.m. UTC | #3
On Mon, 16 Jan 2017 18:56:09 +0100
wm4 <nfxjfg@googlemail.com> wrote:

> On Mon, 16 Jan 2017 09:31:42 -0800
> Philip Langdale <philipl@overt.org> wrote:
> 
> > On Mon, 16 Jan 2017 16:44:16 +0100
> > wm4 <nfxjfg@googlemail.com> wrote:
> >   
> > > Copied and modified from hwcontext_qsv.c.
> > > ---
> > >  libavutil/hwcontext_cuda.c | 26 ++++++++++++++++++++++++++
> > >  1 file changed, 26 insertions(+)
> > > 
> > > diff --git a/libavutil/hwcontext_cuda.c
> > > b/libavutil/hwcontext_cuda.c index 5dd0d99272..ed595c3e0f 100644
> > > --- a/libavutil/hwcontext_cuda.c
> > > +++ b/libavutil/hwcontext_cuda.c
> > > @@ -39,6 +39,31 @@ static const enum AVPixelFormat
> > > supported_formats[] = { AV_PIX_FMT_P016,
> > >  };
> > >  
> > > +static int cuda_frames_get_constraints(AVHWDeviceContext *ctx,
> > > +                                       const void *hwconfig,
> > > +                                       AVHWFramesConstraints
> > > *constraints) +{
> > > +    int i;
> > > +
> > > +    constraints->valid_sw_formats =
> > > av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
> > > +
> > > sizeof(*constraints->valid_sw_formats));
> > > +    if (!constraints->valid_sw_formats)
> > > +        return AVERROR(ENOMEM);
> > > +
> > > +    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
> > > +        constraints->valid_sw_formats[i] = supported_formats[i];
> > > +
> > > constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_formats)]
> > > = AV_PIX_FMT_NONE; +
> > > +    constraints->valid_hw_formats = av_malloc_array(2,
> > > sizeof(*constraints->valid_hw_formats));
> > > +    if (!constraints->valid_hw_formats)
> > > +        return AVERROR(ENOMEM);    
> > 
> > Is it weird if this fails and valid_sw_formats succeeds? I guess
> > that's up to how the caller handles the error.  
> 
> It is, but the QSV code also did it this way.
> 
> And if we're completely honest, nobody really cares about tiny leaks
> when tiny allocations fail (which in practice never ever happens
> anyway).

Yeah. The world is going to end if it ever happens.

Ship it.

--phil
wm4 Jan. 23, 2017, 3:23 p.m. UTC | #4
On Mon, 16 Jan 2017 10:01:41 -0800
Philip Langdale <philipl@overt.org> wrote:

> On Mon, 16 Jan 2017 18:56:09 +0100
> wm4 <nfxjfg@googlemail.com> wrote:
> 
> > On Mon, 16 Jan 2017 09:31:42 -0800
> > Philip Langdale <philipl@overt.org> wrote:
> >   
> > > On Mon, 16 Jan 2017 16:44:16 +0100
> > > wm4 <nfxjfg@googlemail.com> wrote:
> > >     
> > > > Copied and modified from hwcontext_qsv.c.
> > > > ---
> > > >  libavutil/hwcontext_cuda.c | 26 ++++++++++++++++++++++++++
> > > >  1 file changed, 26 insertions(+)
> > > > 
> > > > diff --git a/libavutil/hwcontext_cuda.c
> > > > b/libavutil/hwcontext_cuda.c index 5dd0d99272..ed595c3e0f 100644
> > > > --- a/libavutil/hwcontext_cuda.c
> > > > +++ b/libavutil/hwcontext_cuda.c
> > > > @@ -39,6 +39,31 @@ static const enum AVPixelFormat
> > > > supported_formats[] = { AV_PIX_FMT_P016,
> > > >  };
> > > >  
> > > > +static int cuda_frames_get_constraints(AVHWDeviceContext *ctx,
> > > > +                                       const void *hwconfig,
> > > > +                                       AVHWFramesConstraints
> > > > *constraints) +{
> > > > +    int i;
> > > > +
> > > > +    constraints->valid_sw_formats =
> > > > av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
> > > > +
> > > > sizeof(*constraints->valid_sw_formats));
> > > > +    if (!constraints->valid_sw_formats)
> > > > +        return AVERROR(ENOMEM);
> > > > +
> > > > +    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
> > > > +        constraints->valid_sw_formats[i] = supported_formats[i];
> > > > +
> > > > constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_formats)]
> > > > = AV_PIX_FMT_NONE; +
> > > > +    constraints->valid_hw_formats = av_malloc_array(2,
> > > > sizeof(*constraints->valid_hw_formats));
> > > > +    if (!constraints->valid_hw_formats)
> > > > +        return AVERROR(ENOMEM);      
> > > 
> > > Is it weird if this fails and valid_sw_formats succeeds? I guess
> > > that's up to how the caller handles the error.    
> > 
> > It is, but the QSV code also did it this way.
> > 
> > And if we're completely honest, nobody really cares about tiny leaks
> > when tiny allocations fail (which in practice never ever happens
> > anyway).  
> 
> Yeah. The world is going to end if it ever happens.
> 
> Ship it.

Pushed, got tired of waiting for a response on the Libav ML.
diff mbox

Patch

diff --git a/libavutil/hwcontext_cuda.c b/libavutil/hwcontext_cuda.c
index 5dd0d99272..ed595c3e0f 100644
--- a/libavutil/hwcontext_cuda.c
+++ b/libavutil/hwcontext_cuda.c
@@ -39,6 +39,31 @@  static const enum AVPixelFormat supported_formats[] = {
     AV_PIX_FMT_P016,
 };
 
+static int cuda_frames_get_constraints(AVHWDeviceContext *ctx,
+                                       const void *hwconfig,
+                                       AVHWFramesConstraints *constraints)
+{
+    int i;
+
+    constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
+                                                    sizeof(*constraints->valid_sw_formats));
+    if (!constraints->valid_sw_formats)
+        return AVERROR(ENOMEM);
+
+    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
+        constraints->valid_sw_formats[i] = supported_formats[i];
+    constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_formats)] = AV_PIX_FMT_NONE;
+
+    constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
+    if (!constraints->valid_hw_formats)
+        return AVERROR(ENOMEM);
+
+    constraints->valid_hw_formats[0] = AV_PIX_FMT_CUDA;
+    constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
+
+    return 0;
+}
+
 static void cuda_buffer_free(void *opaque, uint8_t *data)
 {
     AVHWFramesContext *ctx = opaque;
@@ -376,6 +401,7 @@  const HWContextType ff_hwcontext_type_cuda = {
     .device_create        = cuda_device_create,
     .device_init          = cuda_device_init,
     .device_uninit        = cuda_device_uninit,
+    .frames_get_constraints = cuda_frames_get_constraints,
     .frames_init          = cuda_frames_init,
     .frames_get_buffer    = cuda_get_buffer,
     .transfer_get_formats = cuda_transfer_get_formats,