Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread wm4
On Mon, 26 Oct 2015 15:01:50 +0100
Gwenole Beauchesne  wrote:

> 2015-10-26 12:44 GMT+01:00 wm4 :
> > On Mon, 26 Oct 2015 11:22:38 +0100
> > Gwenole Beauchesne  wrote:
> >
> >  
> >> >> /**
> >> >>  * Hardware Accelerator identifier.
> >> >>  *
> >> >>  * @note
> >> >>  * A hardware accelerator can be device-less. This means that only the
> >> >>  * underlying hardware resource, e.g. a Linux dma_buf handle, is being
> >> >>  * transported in the AVFrame. That hardware resource could be mapped
> >> >>  * through standard OS-dependent calls, e.g. mmap() on Linux.
> >> >>  */
> >> >> enum AVHWAccelId {
> >> >> AV_HWACCEL_ID_NONE = -1,
> >> >> AV_HWACCEL_ID_VAAPI,
> >> >> AV_HWACCEL_ID_NB,   ///< Not part of ABI
> >> >> };
> >> >>
> >> >> Name to be improved if people have better suggestions, as this really
> >> >> is to be seen as HW resource, not necessarily attached to a particular
> >> >> HW device. i.e. this could be a dma_buf handle from a V4L2 buffer or
> >> >> VA surface.  
> >> >
> >> > OK. (Minor nit: if ID_NONE is valid and means HW API without context,
> >> > maybe it should be 0, not -1. Also, if it was meant this way, maybe
> >> > these should still have their own ID for other purposes.)  
> >>
> >> In my current model, ID_NONE is not meant to be valid because the
> >> hwaccel side data shall only exist for hwaccel purposes. Besides,
> >> having ID_NONE set to -1 is consistent with other liavu enums and
> >> convenient to have ID_NB express directly the exact number of
> >> hwaccels.  
> >
> > OK, this makes sense to me.
> >  
> >> >> I am reworking the patch series as I changed my mind again: current
> >> >> map strategy was overly complex (and required to be). There were at
> >> >> least 4 map flags: AV_FRAME_MAP_{READ,WRITE,READ_FIRST,USWC_MEMORY}. I
> >> >> am now preferring a unique av_hwaccel_frame_get_pixels() defined as
> >> >> follow:
> >> >>
> >> >> /**
> >> >>  * Returns AVFrame pixels into linear memory
> >> >>  *
> >> >>  * This function takes a snapshot of the underlying HW surface and
> >> >>  * exposes it to SW backed memory. This may involve a copy from GPU
> >> >>  * memory to CPU memory.
> >> >>  *
> >> >>  * @note
> >> >>  * There is no effort underway to commit the modified pixels back to
> >> >>  * GPU memory when the \ref dst AVFrame is released.
> >> >>  *
> >> >>  * @param[in] src   the source frame to read
> >> >>  * @param[inout] dstthe target frame to update, or create if NULL
> >> >>  * @param[in] flags an optional combination of AV_FRAME_FLAG_xxx 
> >> >> flags
> >> >>  * @return 0 on success, an AVERROR code on failure.
> >> >>  */
> >> >> int
> >> >> av_hwaccel_frame_get_pixels(AVFrame *src, AVFrame **dst, unsigned 
> >> >> flags);
> >> >>
> >> >> i.e. the cost of allocating and copying AVFrame metadata should be
> >> >> less than the actual work needed behind the scene. So, it looks like a
> >> >> better interim solution for starters.  
> >> >
> >> > So this is for read-access only, right? If it can map data, there
> >> > also needs to be an unmap function, and the user would have to know
> >> > about when to use it.  
> >>
> >> Well, put can be implementing by reversing src/dst in this function. :)
> >> Actually, this can be av_hwaccel_frame_copy(), but the benefit of
> >> having get_pixels() is to leave out the allocation business to lavu
> >> and just having the user to bother about _unref().  
> >
> > Also makes sense to me.
> >
> > What is a problem is that mapped frames and CPU frames (let's call pure
> > CPU-allocated surfaces that) are not exactly the same thing. If the API
> > user assumes the frame is a CPU frame, it might reference it for a long
> > time, which would cause various problems. On the other hand, you don't
> > want the user to force copying a frame if it's really a CPU frame.
> >
> > Maybe this is not really a problem. I'm just mentioning it as another
> > detail.
> >  
> >> >> For compatibility, that's also the idea behind another generic
> >> >> AV_PIX_FMT_HWACCEL that would enforce data[i] to be clear of any
> >> >> user-supplied pointers, and buf[i] shall be filled in by appropriate
> >> >> accessors, or while creating the side-data, e.g.
> >> >> av_vaapi_frame_create_side_data(). i.e. when lavc swallows up an
> >> >> AVFrame with new-style hwaccel, this is where the AV_PIX_FMT_VAAPI
> >> >> would be replaced with AV_PIX_FMT_HWACCEL. Replace "swallows up" with
> >> >> e.g. av_vaapi_frame_convert_in_place() if you prefer. Otherwise, IMHO,
> >> >> the old-style fields should live untouched, hence the need to keep the
> >> >> hwaccel side-data around.  
> >> >
> >> > Isn't the problem more about output?
> >> >
> >> > Again, there's the problem with the current hwaccel API selecting the
> >> > hwaccel with get_format(), just using the hwaccel-specific pixfmt.  
> >>
> >> I also envision a need for AVCodecContext.hwaccel_id field + possibly
> >> .get_hwaccel(). Just so that to depart from that pixfmt tie.  
> 

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread Gwenole Beauchesne
2015-10-26 12:44 GMT+01:00 wm4 :
> On Mon, 26 Oct 2015 11:22:38 +0100
> Gwenole Beauchesne  wrote:
>
>
>> >> /**
>> >>  * Hardware Accelerator identifier.
>> >>  *
>> >>  * @note
>> >>  * A hardware accelerator can be device-less. This means that only the
>> >>  * underlying hardware resource, e.g. a Linux dma_buf handle, is being
>> >>  * transported in the AVFrame. That hardware resource could be mapped
>> >>  * through standard OS-dependent calls, e.g. mmap() on Linux.
>> >>  */
>> >> enum AVHWAccelId {
>> >> AV_HWACCEL_ID_NONE = -1,
>> >> AV_HWACCEL_ID_VAAPI,
>> >> AV_HWACCEL_ID_NB,   ///< Not part of ABI
>> >> };
>> >>
>> >> Name to be improved if people have better suggestions, as this really
>> >> is to be seen as HW resource, not necessarily attached to a particular
>> >> HW device. i.e. this could be a dma_buf handle from a V4L2 buffer or
>> >> VA surface.
>> >
>> > OK. (Minor nit: if ID_NONE is valid and means HW API without context,
>> > maybe it should be 0, not -1. Also, if it was meant this way, maybe
>> > these should still have their own ID for other purposes.)
>>
>> In my current model, ID_NONE is not meant to be valid because the
>> hwaccel side data shall only exist for hwaccel purposes. Besides,
>> having ID_NONE set to -1 is consistent with other liavu enums and
>> convenient to have ID_NB express directly the exact number of
>> hwaccels.
>
> OK, this makes sense to me.
>
>> >> I am reworking the patch series as I changed my mind again: current
>> >> map strategy was overly complex (and required to be). There were at
>> >> least 4 map flags: AV_FRAME_MAP_{READ,WRITE,READ_FIRST,USWC_MEMORY}. I
>> >> am now preferring a unique av_hwaccel_frame_get_pixels() defined as
>> >> follow:
>> >>
>> >> /**
>> >>  * Returns AVFrame pixels into linear memory
>> >>  *
>> >>  * This function takes a snapshot of the underlying HW surface and
>> >>  * exposes it to SW backed memory. This may involve a copy from GPU
>> >>  * memory to CPU memory.
>> >>  *
>> >>  * @note
>> >>  * There is no effort underway to commit the modified pixels back to
>> >>  * GPU memory when the \ref dst AVFrame is released.
>> >>  *
>> >>  * @param[in] src   the source frame to read
>> >>  * @param[inout] dstthe target frame to update, or create if NULL
>> >>  * @param[in] flags an optional combination of AV_FRAME_FLAG_xxx flags
>> >>  * @return 0 on success, an AVERROR code on failure.
>> >>  */
>> >> int
>> >> av_hwaccel_frame_get_pixels(AVFrame *src, AVFrame **dst, unsigned flags);
>> >>
>> >> i.e. the cost of allocating and copying AVFrame metadata should be
>> >> less than the actual work needed behind the scene. So, it looks like a
>> >> better interim solution for starters.
>> >
>> > So this is for read-access only, right? If it can map data, there
>> > also needs to be an unmap function, and the user would have to know
>> > about when to use it.
>>
>> Well, put can be implementing by reversing src/dst in this function. :)
>> Actually, this can be av_hwaccel_frame_copy(), but the benefit of
>> having get_pixels() is to leave out the allocation business to lavu
>> and just having the user to bother about _unref().
>
> Also makes sense to me.
>
> What is a problem is that mapped frames and CPU frames (let's call pure
> CPU-allocated surfaces that) are not exactly the same thing. If the API
> user assumes the frame is a CPU frame, it might reference it for a long
> time, which would cause various problems. On the other hand, you don't
> want the user to force copying a frame if it's really a CPU frame.
>
> Maybe this is not really a problem. I'm just mentioning it as another
> detail.
>
>> >> For compatibility, that's also the idea behind another generic
>> >> AV_PIX_FMT_HWACCEL that would enforce data[i] to be clear of any
>> >> user-supplied pointers, and buf[i] shall be filled in by appropriate
>> >> accessors, or while creating the side-data, e.g.
>> >> av_vaapi_frame_create_side_data(). i.e. when lavc swallows up an
>> >> AVFrame with new-style hwaccel, this is where the AV_PIX_FMT_VAAPI
>> >> would be replaced with AV_PIX_FMT_HWACCEL. Replace "swallows up" with
>> >> e.g. av_vaapi_frame_convert_in_place() if you prefer. Otherwise, IMHO,
>> >> the old-style fields should live untouched, hence the need to keep the
>> >> hwaccel side-data around.
>> >
>> > Isn't the problem more about output?
>> >
>> > Again, there's the problem with the current hwaccel API selecting the
>> > hwaccel with get_format(), just using the hwaccel-specific pixfmt.
>>
>> I also envision a need for AVCodecContext.hwaccel_id field + possibly
>> .get_hwaccel(). Just so that to depart from that pixfmt tie.
>
> There are some of us who would like this. Of course it makes the API
> change larger.

I don't think it would be too larger. I rather think the move would be
less intrusive, thus smoother.

My vision is:
- You want hwaccel: you report those you are interested in/support
through get_hwaccel() ;
  + should you n

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread Sven Dueking


> -Ursprüngliche Nachricht-
> Von: ffmpeg-devel [mailto:ffmpeg-devel-boun...@ffmpeg.org] Im Auftrag
> von Hendrik Leppkes
> Gesendet: Donnerstag, 22. Oktober 2015 17:56
> An: FFmpeg development discussions and patches
> Betreff: Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session
> control and internal allocation
> 
> On Wed, Oct 14, 2015 at 11:47 AM, Hendrik Leppkes 
> wrote:
> > On Wed, Oct 7, 2015 at 7:07 PM, Ivan Uskov 
> wrote:
> >> Hello wm4,
> >>
> >> Wednesday, October 7, 2015, 7:40:45 PM, you wrote:
> >>
> >> w> There's no automagic way to get this done.
> >>
> >> w> Hardware accelerators like vaapi and vdpau need the same thing.
> >> w> These have special APIs to set an API context on the decoder.
> Some
> >> w> APIs use hwaccel_context, vdpau uses av_vdpau_bind_context().
> >>
> >> w> The hwaccel_context pointer is untyped (just a void*), and what
> it
> >> w> means is implicit to the hwaccel or the decoder that is used. It
> >> w> could point to some sort of qsv state, which will have to be
> >> w> explicitly allocated and set by the API user (which might be
> ffmpeg.c).
> >> So  if  I will implement something like ffmpeg_qsv.c (using
> ffmpeg_vdpau.c as
> >> example)   and   extend  the  hwaccels[]  into  ffmpeg_opt.c  by
> corresponded
> >> qsv entries it  would  be the acceptable solution, correct?
> >>
> >> w> For filters there is no such thing yet. New API would have to be
> >> w> created. For filters in particular, we will have to make sure
> that
> >> w> the API isn't overly qsv-specific, and that it is extendable to
> >> w> other APIs (like for example vaapi or vdpau).
> >> Ok,   if   VPP  could be the  issue  I  would  like  to  get
> working  direct
> >> link qsv_decoder-qsv_encoder first.
> >>
> >
> > Libav has a patch that does exactly this, allow direct QSV->QSV
> > transcoding with help of some utility code in ffmpeg.c/avconv.c You
> > might want to look at that instead of re-inventing it. That'll help
> > make everyones live easier, as I'll just merge it when the time
> comes,
> > and the codebases don't diverge too drastically.
> >
> 
> This functionality has been merged now. It works for some samples.
> You can try to use it with a command line like this:
> 
> ffmpeg -hwaccel qsv -c:v h264_qsv -i h264.ts -c:v h264_qsv output.mkv
> 
> This will transcode using a QSV->QSV pipeline, no copying to system
> memory, and about 2.5x faster on my IVB laptop.
> 
> However, its broken on a lot of more complex H264 files, you'll get
> errors like get_buffer() failed - this is because our qsvdec behaves
> rather strangely, and instead of buffering input data when needed, it
> buffers output surfaces, which is not ideal, since it bloats up the
> memory usage an the number of surfaces required explodes into infinity.
> I know how to fix it - just restore the decoder to the same buffering
> model as it originally used, buffer input data instead of output
> surfaces. Unless someone else wants to fix it, I'll simply do it in the
> next week or so.

Hi Henrik,

Let me know if you need help to fix that, sounds like a good chance to learn 
more about the workflow. 
But, this could also result in extra work to double check my code. So, what do 
you think ?

Best,
Sven

> 
> - Hendrik
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread Hendrik Leppkes
On Mon, Oct 26, 2015 at 1:12 PM, wm4  wrote:
> On Mon, 26 Oct 2015 12:56:31 +0100
> Hendrik Leppkes  wrote:
>
>> >> > I think doing cropping as metadata would also simplify code a lot. For
>> >> > example, nobody has to worry about non-mod-2 yuv420 anymore, and how it
>> >> > should be handled. It's less tricky, more correct, more efficient.
>> >>
>> >> OK. A crop side-data is desired then. I somehow was convinced that it
>> >> wouldn't matter for SW. Though, it would actually be really need that
>> >
>> > At least this is my opinion. I would like to have such cropping side
>> > data, instead of half-broken ad-hoc cropping in the decoder and things
>> > like coded_width.
>> >
>> > I don't know what most others think about this. I suspect most would
>> > find such a change too intrusive. At least for hwaccel it's mandatory
>> > though. (What we currently do just barely works, and I need hacks in my
>> > own code to reconstruct the real surface size.)
>>
>> 99% of all cropping use-cases are extremely simple (only bottom/right)
>> and don't require any secret magic in any code.
>> I don't mind adding extra cropping metadata, but if you just don't
>> care about top/left cropping, simply adjusting width/height is as
>> trivial as it gets.
>>
>> Adding mandatory new metadata that every user app has to support to
>> avoid getting 1920x1088 in the future seems a bit ill-advised.
>
> Well, I've seen you complaining multiple times that non-mod-2 yuv420
> does not make any sense...

Obviously it doesn't, but I blame the users creating that, not the decoder.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread wm4
On Mon, 26 Oct 2015 12:56:31 +0100
Hendrik Leppkes  wrote:

> >> > I think doing cropping as metadata would also simplify code a lot. For
> >> > example, nobody has to worry about non-mod-2 yuv420 anymore, and how it
> >> > should be handled. It's less tricky, more correct, more efficient.  
> >>
> >> OK. A crop side-data is desired then. I somehow was convinced that it
> >> wouldn't matter for SW. Though, it would actually be really need that  
> >
> > At least this is my opinion. I would like to have such cropping side
> > data, instead of half-broken ad-hoc cropping in the decoder and things
> > like coded_width.
> >
> > I don't know what most others think about this. I suspect most would
> > find such a change too intrusive. At least for hwaccel it's mandatory
> > though. (What we currently do just barely works, and I need hacks in my
> > own code to reconstruct the real surface size.)  
> 
> 99% of all cropping use-cases are extremely simple (only bottom/right)
> and don't require any secret magic in any code.
> I don't mind adding extra cropping metadata, but if you just don't
> care about top/left cropping, simply adjusting width/height is as
> trivial as it gets.
> 
> Adding mandatory new metadata that every user app has to support to
> avoid getting 1920x1088 in the future seems a bit ill-advised.

Well, I've seen you complaining multiple times that non-mod-2 yuv420
does not make any sense...
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread Hendrik Leppkes
On Mon, Oct 26, 2015 at 12:44 PM, wm4  wrote:
> On Mon, 26 Oct 2015 11:22:38 +0100
> Gwenole Beauchesne  wrote:
>
>
>> >> /**
>> >>  * Hardware Accelerator identifier.
>> >>  *
>> >>  * @note
>> >>  * A hardware accelerator can be device-less. This means that only the
>> >>  * underlying hardware resource, e.g. a Linux dma_buf handle, is being
>> >>  * transported in the AVFrame. That hardware resource could be mapped
>> >>  * through standard OS-dependent calls, e.g. mmap() on Linux.
>> >>  */
>> >> enum AVHWAccelId {
>> >> AV_HWACCEL_ID_NONE = -1,
>> >> AV_HWACCEL_ID_VAAPI,
>> >> AV_HWACCEL_ID_NB,   ///< Not part of ABI
>> >> };
>> >>
>> >> Name to be improved if people have better suggestions, as this really
>> >> is to be seen as HW resource, not necessarily attached to a particular
>> >> HW device. i.e. this could be a dma_buf handle from a V4L2 buffer or
>> >> VA surface.
>> >
>> > OK. (Minor nit: if ID_NONE is valid and means HW API without context,
>> > maybe it should be 0, not -1. Also, if it was meant this way, maybe
>> > these should still have their own ID for other purposes.)
>>
>> In my current model, ID_NONE is not meant to be valid because the
>> hwaccel side data shall only exist for hwaccel purposes. Besides,
>> having ID_NONE set to -1 is consistent with other liavu enums and
>> convenient to have ID_NB express directly the exact number of
>> hwaccels.
>
> OK, this makes sense to me.
>
>> >> I am reworking the patch series as I changed my mind again: current
>> >> map strategy was overly complex (and required to be). There were at
>> >> least 4 map flags: AV_FRAME_MAP_{READ,WRITE,READ_FIRST,USWC_MEMORY}. I
>> >> am now preferring a unique av_hwaccel_frame_get_pixels() defined as
>> >> follow:
>> >>
>> >> /**
>> >>  * Returns AVFrame pixels into linear memory
>> >>  *
>> >>  * This function takes a snapshot of the underlying HW surface and
>> >>  * exposes it to SW backed memory. This may involve a copy from GPU
>> >>  * memory to CPU memory.
>> >>  *
>> >>  * @note
>> >>  * There is no effort underway to commit the modified pixels back to
>> >>  * GPU memory when the \ref dst AVFrame is released.
>> >>  *
>> >>  * @param[in] src   the source frame to read
>> >>  * @param[inout] dstthe target frame to update, or create if NULL
>> >>  * @param[in] flags an optional combination of AV_FRAME_FLAG_xxx flags
>> >>  * @return 0 on success, an AVERROR code on failure.
>> >>  */
>> >> int
>> >> av_hwaccel_frame_get_pixels(AVFrame *src, AVFrame **dst, unsigned flags);
>> >>
>> >> i.e. the cost of allocating and copying AVFrame metadata should be
>> >> less than the actual work needed behind the scene. So, it looks like a
>> >> better interim solution for starters.
>> >
>> > So this is for read-access only, right? If it can map data, there
>> > also needs to be an unmap function, and the user would have to know
>> > about when to use it.
>>
>> Well, put can be implementing by reversing src/dst in this function. :)
>> Actually, this can be av_hwaccel_frame_copy(), but the benefit of
>> having get_pixels() is to leave out the allocation business to lavu
>> and just having the user to bother about _unref().
>
> Also makes sense to me.
>
> What is a problem is that mapped frames and CPU frames (let's call pure
> CPU-allocated surfaces that) are not exactly the same thing. If the API
> user assumes the frame is a CPU frame, it might reference it for a long
> time, which would cause various problems. On the other hand, you don't
> want the user to force copying a frame if it's really a CPU frame.
>
> Maybe this is not really a problem. I'm just mentioning it as another
> detail.
>
>> >> For compatibility, that's also the idea behind another generic
>> >> AV_PIX_FMT_HWACCEL that would enforce data[i] to be clear of any
>> >> user-supplied pointers, and buf[i] shall be filled in by appropriate
>> >> accessors, or while creating the side-data, e.g.
>> >> av_vaapi_frame_create_side_data(). i.e. when lavc swallows up an
>> >> AVFrame with new-style hwaccel, this is where the AV_PIX_FMT_VAAPI
>> >> would be replaced with AV_PIX_FMT_HWACCEL. Replace "swallows up" with
>> >> e.g. av_vaapi_frame_convert_in_place() if you prefer. Otherwise, IMHO,
>> >> the old-style fields should live untouched, hence the need to keep the
>> >> hwaccel side-data around.
>> >
>> > Isn't the problem more about output?
>> >
>> > Again, there's the problem with the current hwaccel API selecting the
>> > hwaccel with get_format(), just using the hwaccel-specific pixfmt.
>>
>> I also envision a need for AVCodecContext.hwaccel_id field + possibly
>> .get_hwaccel(). Just so that to depart from that pixfmt tie.
>
> There are some of us who would like this. Of course it makes the API
> change larger. Also, I do find it useful to have pixfmt distinguish
> between underlying surface types (i.e. the hwaccel API). For example,
> if we add support for hw filters to libavfilter, how would you prevent
> that a vdpau

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread wm4
On Mon, 26 Oct 2015 11:22:38 +0100
Gwenole Beauchesne  wrote:


> >> /**
> >>  * Hardware Accelerator identifier.
> >>  *
> >>  * @note
> >>  * A hardware accelerator can be device-less. This means that only the
> >>  * underlying hardware resource, e.g. a Linux dma_buf handle, is being
> >>  * transported in the AVFrame. That hardware resource could be mapped
> >>  * through standard OS-dependent calls, e.g. mmap() on Linux.
> >>  */
> >> enum AVHWAccelId {
> >> AV_HWACCEL_ID_NONE = -1,
> >> AV_HWACCEL_ID_VAAPI,
> >> AV_HWACCEL_ID_NB,   ///< Not part of ABI
> >> };
> >>
> >> Name to be improved if people have better suggestions, as this really
> >> is to be seen as HW resource, not necessarily attached to a particular
> >> HW device. i.e. this could be a dma_buf handle from a V4L2 buffer or
> >> VA surface.  
> >
> > OK. (Minor nit: if ID_NONE is valid and means HW API without context,
> > maybe it should be 0, not -1. Also, if it was meant this way, maybe
> > these should still have their own ID for other purposes.)  
> 
> In my current model, ID_NONE is not meant to be valid because the
> hwaccel side data shall only exist for hwaccel purposes. Besides,
> having ID_NONE set to -1 is consistent with other liavu enums and
> convenient to have ID_NB express directly the exact number of
> hwaccels.

OK, this makes sense to me.

> >> I am reworking the patch series as I changed my mind again: current
> >> map strategy was overly complex (and required to be). There were at
> >> least 4 map flags: AV_FRAME_MAP_{READ,WRITE,READ_FIRST,USWC_MEMORY}. I
> >> am now preferring a unique av_hwaccel_frame_get_pixels() defined as
> >> follow:
> >>
> >> /**
> >>  * Returns AVFrame pixels into linear memory
> >>  *
> >>  * This function takes a snapshot of the underlying HW surface and
> >>  * exposes it to SW backed memory. This may involve a copy from GPU
> >>  * memory to CPU memory.
> >>  *
> >>  * @note
> >>  * There is no effort underway to commit the modified pixels back to
> >>  * GPU memory when the \ref dst AVFrame is released.
> >>  *
> >>  * @param[in] src   the source frame to read
> >>  * @param[inout] dstthe target frame to update, or create if NULL
> >>  * @param[in] flags an optional combination of AV_FRAME_FLAG_xxx flags
> >>  * @return 0 on success, an AVERROR code on failure.
> >>  */
> >> int
> >> av_hwaccel_frame_get_pixels(AVFrame *src, AVFrame **dst, unsigned flags);
> >>
> >> i.e. the cost of allocating and copying AVFrame metadata should be
> >> less than the actual work needed behind the scene. So, it looks like a
> >> better interim solution for starters.  
> >
> > So this is for read-access only, right? If it can map data, there
> > also needs to be an unmap function, and the user would have to know
> > about when to use it.  
> 
> Well, put can be implementing by reversing src/dst in this function. :)
> Actually, this can be av_hwaccel_frame_copy(), but the benefit of
> having get_pixels() is to leave out the allocation business to lavu
> and just having the user to bother about _unref().

Also makes sense to me.

What is a problem is that mapped frames and CPU frames (let's call pure
CPU-allocated surfaces that) are not exactly the same thing. If the API
user assumes the frame is a CPU frame, it might reference it for a long
time, which would cause various problems. On the other hand, you don't
want the user to force copying a frame if it's really a CPU frame.

Maybe this is not really a problem. I'm just mentioning it as another
detail.

> >> For compatibility, that's also the idea behind another generic
> >> AV_PIX_FMT_HWACCEL that would enforce data[i] to be clear of any
> >> user-supplied pointers, and buf[i] shall be filled in by appropriate
> >> accessors, or while creating the side-data, e.g.
> >> av_vaapi_frame_create_side_data(). i.e. when lavc swallows up an
> >> AVFrame with new-style hwaccel, this is where the AV_PIX_FMT_VAAPI
> >> would be replaced with AV_PIX_FMT_HWACCEL. Replace "swallows up" with
> >> e.g. av_vaapi_frame_convert_in_place() if you prefer. Otherwise, IMHO,
> >> the old-style fields should live untouched, hence the need to keep the
> >> hwaccel side-data around.  
> >
> > Isn't the problem more about output?
> >
> > Again, there's the problem with the current hwaccel API selecting the
> > hwaccel with get_format(), just using the hwaccel-specific pixfmt.  
> 
> I also envision a need for AVCodecContext.hwaccel_id field + possibly
> .get_hwaccel(). Just so that to depart from that pixfmt tie.

There are some of us who would like this. Of course it makes the API
change larger. Also, I do find it useful to have pixfmt distinguish
between underlying surface types (i.e. the hwaccel API). For example,
if we add support for hw filters to libavfilter, how would you prevent
that a vdpau filter takes vaapi surfaces as input?

So I'm not sure if a single AV_PIX_FMT_HWACCEL is the way to go, even
if we make access to hwaccel AVFrames somewhat

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-26 Thread Gwenole Beauchesne
Hi,

2015-10-23 16:52 GMT+02:00 wm4 :
> On Fri, 23 Oct 2015 16:12:07 +0200
> Gwenole Beauchesne  wrote:
>
>> Hi,
>>
>> 2015-10-23 15:41 GMT+02:00 wm4 :
>> > On Fri, 23 Oct 2015 14:54:56 +0200
>> > Gwenole Beauchesne  wrote:
>> >
>> >> Hi,
>> >>
>> >> 2015-10-07 18:40 GMT+02:00 wm4 :
>> >> > On Wed, 7 Oct 2015 19:20:56 +0300
>> >> > Ivan Uskov  wrote:
>> >> >
>> >> >> Hello Hendrik,
>> >> >>
>> >> >> Wednesday, October 7, 2015, 5:58:25 PM, you wrote:
>> >> >>
>> >> >> HL> On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  
>> >> >> wrote:
>> >> >>
>> >> >> HL> Global static variables are not acceptable, sorry.
>> >> >> HL> You'll have to find another way to solve your problem, but global
>> >> >> HL> state is not the way to go.
>> >> >> Unfortunately   I   do   not   have   ideas  how to provide single and 
>> >> >> common
>> >> >> memory  block  for  separatemodules   by  another  way.   Memory  
>> >> >> mapping
>> >> >> files  looks not too portable and much more bulky solution then one 
>> >> >> global variable.
>> >> >> I  do  not  see  the  way to use appropriate field of AVCodecContext 
>> >> >> to share
>> >> >> global data.
>> >> >> Has anybody any ideas?
>> >> >> Is  me  missed  something?  There is really the necessary to have a 
>> >> >> global common
>> >> >> structure shared between decoder, vpp and decoder.
>> >> >>
>> >> >
>> >> > There's no automagic way to get this done.
>> >> >
>> >> > Hardware accelerators like vaapi and vdpau need the same thing. These
>> >> > have special APIs to set an API context on the decoder. Some APIs use
>> >> > hwaccel_context, vdpau uses av_vdpau_bind_context().
>> >> >
>> >> > The hwaccel_context pointer is untyped (just a void*), and what it means
>> >> > is implicit to the hwaccel or the decoder that is used. It could point
>> >> > to some sort of qsv state, which will have to be explicitly allocated
>> >> > and set by the API user (which might be ffmpeg.c).
>> >> >
>> >> > For filters there is no such thing yet. New API would have to be
>> >> > created. For filters in particular, we will have to make sure that the
>> >> > API isn't overly qsv-specific, and that it is extendable to other APIs
>> >> > (like for example vaapi or vdpau).
>> >>
>> >> I have been looking into a slightly different way: the common object
>> >> being transported is an AVFrame. So, my initial approach is to create
>> >> an AV_FRAME_DATA_HWACCEL metadata. Lookups could be optimized by
>> >> keeping around an AVFrameInternal struct that resides in the same
>> >> allocation unit as the AVFrame. But, this is a detail.
>> >>
>> >> From there, there are at least two usage models, when it comes to filters 
>> >> too:
>> >>
>> >> 1. Make the AVHWAccelFrame struct hold multiple hwaccel-specific info,
>> >> with a master one, and slave ones for various different APIs.
>> >> Advantage: a single AVFrame can be used and impersonified whenever
>> >> necessary. e.g. a VA surface master could be exported/re-used with an
>> >> mfxSurface, a dma_buf (for OpenCL), or userptr buffer. Drawback:
>> >> terribly tedious to manage.
>> >>
>> >> 2. Simpler approach: the AVHWAccelFrame holds a unique struct to
>> >> hwaccel specific data. Should we need to export that for use with
>> >> another API, it's not too complicated to av_frame_ref() + add new
>> >> hwaccel-specific metadata.
>> >
>> > It could be something like an API identifier (an enum or so) + API
>> > specific pointer to a struct.
>>
>> That's exactly that:
>>
>> /**
>>  * Hardware Accelerator identifier.
>>  *
>>  * @note
>>  * A hardware accelerator can be device-less. This means that only the
>>  * underlying hardware resource, e.g. a Linux dma_buf handle, is being
>>  * transported in the AVFrame. That hardware resource could be mapped
>>  * through standard OS-dependent calls, e.g. mmap() on Linux.
>>  */
>> enum AVHWAccelId {
>> AV_HWACCEL_ID_NONE = -1,
>> AV_HWACCEL_ID_VAAPI,
>> AV_HWACCEL_ID_NB,   ///< Not part of ABI
>> };
>>
>> Name to be improved if people have better suggestions, as this really
>> is to be seen as HW resource, not necessarily attached to a particular
>> HW device. i.e. this could be a dma_buf handle from a V4L2 buffer or
>> VA surface.
>
> OK. (Minor nit: if ID_NONE is valid and means HW API without context,
> maybe it should be 0, not -1. Also, if it was meant this way, maybe
> these should still have their own ID for other purposes.)

In my current model, ID_NONE is not meant to be valid because the
hwaccel side data shall only exist for hwaccel purposes. Besides,
having ID_NONE set to -1 is consistent with other liavu enums and
convenient to have ID_NB express directly the exact number of
hwaccels.

>> I am reworking the patch series as I changed my mind again: current
>> map strategy was overly complex (and required to be). There were at
>> least 4 map flags: AV_FRAME_MAP_{READ,WRITE,READ_FIRST,USWC_MEMORY}. I
>> am now preferring a unique av_hwaccel_frame_get_pixels() defined as
>> follow:
>>
>> /

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-23 Thread wm4
On Fri, 23 Oct 2015 16:12:07 +0200
Gwenole Beauchesne  wrote:

> Hi,
> 
> 2015-10-23 15:41 GMT+02:00 wm4 :
> > On Fri, 23 Oct 2015 14:54:56 +0200
> > Gwenole Beauchesne  wrote:
> >  
> >> Hi,
> >>
> >> 2015-10-07 18:40 GMT+02:00 wm4 :  
> >> > On Wed, 7 Oct 2015 19:20:56 +0300
> >> > Ivan Uskov  wrote:
> >> >  
> >> >> Hello Hendrik,
> >> >>
> >> >> Wednesday, October 7, 2015, 5:58:25 PM, you wrote:
> >> >>  
> >> >> HL> On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  
> >> >> wrote:  
> >> >>  
> >> >> HL> Global static variables are not acceptable, sorry.
> >> >> HL> You'll have to find another way to solve your problem, but global
> >> >> HL> state is not the way to go.  
> >> >> Unfortunately   I   do   not   have   ideas  how to provide single and 
> >> >> common
> >> >> memory  block  for  separatemodules   by  another  way.   Memory  
> >> >> mapping
> >> >> files  looks not too portable and much more bulky solution then one 
> >> >> global variable.
> >> >> I  do  not  see  the  way to use appropriate field of AVCodecContext to 
> >> >> share
> >> >> global data.
> >> >> Has anybody any ideas?
> >> >> Is  me  missed  something?  There is really the necessary to have a 
> >> >> global common
> >> >> structure shared between decoder, vpp and decoder.
> >> >>  
> >> >
> >> > There's no automagic way to get this done.
> >> >
> >> > Hardware accelerators like vaapi and vdpau need the same thing. These
> >> > have special APIs to set an API context on the decoder. Some APIs use
> >> > hwaccel_context, vdpau uses av_vdpau_bind_context().
> >> >
> >> > The hwaccel_context pointer is untyped (just a void*), and what it means
> >> > is implicit to the hwaccel or the decoder that is used. It could point
> >> > to some sort of qsv state, which will have to be explicitly allocated
> >> > and set by the API user (which might be ffmpeg.c).
> >> >
> >> > For filters there is no such thing yet. New API would have to be
> >> > created. For filters in particular, we will have to make sure that the
> >> > API isn't overly qsv-specific, and that it is extendable to other APIs
> >> > (like for example vaapi or vdpau).  
> >>
> >> I have been looking into a slightly different way: the common object
> >> being transported is an AVFrame. So, my initial approach is to create
> >> an AV_FRAME_DATA_HWACCEL metadata. Lookups could be optimized by
> >> keeping around an AVFrameInternal struct that resides in the same
> >> allocation unit as the AVFrame. But, this is a detail.
> >>
> >> From there, there are at least two usage models, when it comes to filters 
> >> too:
> >>
> >> 1. Make the AVHWAccelFrame struct hold multiple hwaccel-specific info,
> >> with a master one, and slave ones for various different APIs.
> >> Advantage: a single AVFrame can be used and impersonified whenever
> >> necessary. e.g. a VA surface master could be exported/re-used with an
> >> mfxSurface, a dma_buf (for OpenCL), or userptr buffer. Drawback:
> >> terribly tedious to manage.
> >>
> >> 2. Simpler approach: the AVHWAccelFrame holds a unique struct to
> >> hwaccel specific data. Should we need to export that for use with
> >> another API, it's not too complicated to av_frame_ref() + add new
> >> hwaccel-specific metadata.  
> >
> > It could be something like an API identifier (an enum or so) + API
> > specific pointer to a struct.  
> 
> That's exactly that:
> 
> /**
>  * Hardware Accelerator identifier.
>  *
>  * @note
>  * A hardware accelerator can be device-less. This means that only the
>  * underlying hardware resource, e.g. a Linux dma_buf handle, is being
>  * transported in the AVFrame. That hardware resource could be mapped
>  * through standard OS-dependent calls, e.g. mmap() on Linux.
>  */
> enum AVHWAccelId {
> AV_HWACCEL_ID_NONE = -1,
> AV_HWACCEL_ID_VAAPI,
> AV_HWACCEL_ID_NB,   ///< Not part of ABI
> };
> 
> Name to be improved if people have better suggestions, as this really
> is to be seen as HW resource, not necessarily attached to a particular
> HW device. i.e. this could be a dma_buf handle from a V4L2 buffer or
> VA surface.

OK. (Minor nit: if ID_NONE is valid and means HW API without context,
maybe it should be 0, not -1. Also, if it was meant this way, maybe
these should still have their own ID for other purposes.)

> I am reworking the patch series as I changed my mind again: current
> map strategy was overly complex (and required to be). There were at
> least 4 map flags: AV_FRAME_MAP_{READ,WRITE,READ_FIRST,USWC_MEMORY}. I
> am now preferring a unique av_hwaccel_frame_get_pixels() defined as
> follow:
> 
> /**
>  * Returns AVFrame pixels into linear memory
>  *
>  * This function takes a snapshot of the underlying HW surface and
>  * exposes it to SW backed memory. This may involve a copy from GPU
>  * memory to CPU memory.
>  *
>  * @note
>  * There is no effort underway to commit the modified pixels back to
>  * GPU memory when the \ref dst AVFrame is released.
>  *
>  * @param[in] src   the s

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-23 Thread Gwenole Beauchesne
Hi,

2015-10-23 15:41 GMT+02:00 wm4 :
> On Fri, 23 Oct 2015 14:54:56 +0200
> Gwenole Beauchesne  wrote:
>
>> Hi,
>>
>> 2015-10-07 18:40 GMT+02:00 wm4 :
>> > On Wed, 7 Oct 2015 19:20:56 +0300
>> > Ivan Uskov  wrote:
>> >
>> >> Hello Hendrik,
>> >>
>> >> Wednesday, October 7, 2015, 5:58:25 PM, you wrote:
>> >>
>> >> HL> On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  
>> >> wrote:
>> >>
>> >> HL> Global static variables are not acceptable, sorry.
>> >> HL> You'll have to find another way to solve your problem, but global
>> >> HL> state is not the way to go.
>> >> Unfortunately   I   do   not   have   ideas  how to provide single and 
>> >> common
>> >> memory  block  for  separatemodules   by  another  way.   Memory  
>> >> mapping
>> >> files  looks not too portable and much more bulky solution then one 
>> >> global variable.
>> >> I  do  not  see  the  way to use appropriate field of AVCodecContext to 
>> >> share
>> >> global data.
>> >> Has anybody any ideas?
>> >> Is  me  missed  something?  There is really the necessary to have a 
>> >> global common
>> >> structure shared between decoder, vpp and decoder.
>> >>
>> >
>> > There's no automagic way to get this done.
>> >
>> > Hardware accelerators like vaapi and vdpau need the same thing. These
>> > have special APIs to set an API context on the decoder. Some APIs use
>> > hwaccel_context, vdpau uses av_vdpau_bind_context().
>> >
>> > The hwaccel_context pointer is untyped (just a void*), and what it means
>> > is implicit to the hwaccel or the decoder that is used. It could point
>> > to some sort of qsv state, which will have to be explicitly allocated
>> > and set by the API user (which might be ffmpeg.c).
>> >
>> > For filters there is no such thing yet. New API would have to be
>> > created. For filters in particular, we will have to make sure that the
>> > API isn't overly qsv-specific, and that it is extendable to other APIs
>> > (like for example vaapi or vdpau).
>>
>> I have been looking into a slightly different way: the common object
>> being transported is an AVFrame. So, my initial approach is to create
>> an AV_FRAME_DATA_HWACCEL metadata. Lookups could be optimized by
>> keeping around an AVFrameInternal struct that resides in the same
>> allocation unit as the AVFrame. But, this is a detail.
>>
>> From there, there are at least two usage models, when it comes to filters 
>> too:
>>
>> 1. Make the AVHWAccelFrame struct hold multiple hwaccel-specific info,
>> with a master one, and slave ones for various different APIs.
>> Advantage: a single AVFrame can be used and impersonified whenever
>> necessary. e.g. a VA surface master could be exported/re-used with an
>> mfxSurface, a dma_buf (for OpenCL), or userptr buffer. Drawback:
>> terribly tedious to manage.
>>
>> 2. Simpler approach: the AVHWAccelFrame holds a unique struct to
>> hwaccel specific data. Should we need to export that for use with
>> another API, it's not too complicated to av_frame_ref() + add new
>> hwaccel-specific metadata.
>
> It could be something like an API identifier (an enum or so) + API
> specific pointer to a struct.

That's exactly that:

/**
 * Hardware Accelerator identifier.
 *
 * @note
 * A hardware accelerator can be device-less. This means that only the
 * underlying hardware resource, e.g. a Linux dma_buf handle, is being
 * transported in the AVFrame. That hardware resource could be mapped
 * through standard OS-dependent calls, e.g. mmap() on Linux.
 */
enum AVHWAccelId {
AV_HWACCEL_ID_NONE = -1,
AV_HWACCEL_ID_VAAPI,
AV_HWACCEL_ID_NB,   ///< Not part of ABI
};

Name to be improved if people have better suggestions, as this really
is to be seen as HW resource, not necessarily attached to a particular
HW device. i.e. this could be a dma_buf handle from a V4L2 buffer or
VA surface.

I am reworking the patch series as I changed my mind again: current
map strategy was overly complex (and required to be). There were at
least 4 map flags: AV_FRAME_MAP_{READ,WRITE,READ_FIRST,USWC_MEMORY}. I
am now preferring a unique av_hwaccel_frame_get_pixels() defined as
follow:

/**
 * Returns AVFrame pixels into linear memory
 *
 * This function takes a snapshot of the underlying HW surface and
 * exposes it to SW backed memory. This may involve a copy from GPU
 * memory to CPU memory.
 *
 * @note
 * There is no effort underway to commit the modified pixels back to
 * GPU memory when the \ref dst AVFrame is released.
 *
 * @param[in] src   the source frame to read
 * @param[inout] dstthe target frame to update, or create if NULL
 * @param[in] flags an optional combination of AV_FRAME_FLAG_xxx flags
 * @return 0 on success, an AVERROR code on failure.
 */
int
av_hwaccel_frame_get_pixels(AVFrame *src, AVFrame **dst, unsigned flags);

i.e. the cost of allocating and copying AVFrame metadata should be
less than the actual work needed behind the scene. So, it looks like a
better interim solution for starters.

>
>> For VA-API specific purposes, I

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-23 Thread wm4
On Fri, 23 Oct 2015 14:54:56 +0200
Gwenole Beauchesne  wrote:

> Hi,
> 
> 2015-10-07 18:40 GMT+02:00 wm4 :
> > On Wed, 7 Oct 2015 19:20:56 +0300
> > Ivan Uskov  wrote:
> >  
> >> Hello Hendrik,
> >>
> >> Wednesday, October 7, 2015, 5:58:25 PM, you wrote:
> >>  
> >> HL> On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  
> >> wrote:  
> >>  
> >> HL> Global static variables are not acceptable, sorry.
> >> HL> You'll have to find another way to solve your problem, but global
> >> HL> state is not the way to go.  
> >> Unfortunately   I   do   not   have   ideas  how to provide single and 
> >> common
> >> memory  block  for  separatemodules   by  another  way.   Memory  
> >> mapping
> >> files  looks not too portable and much more bulky solution then one global 
> >> variable.
> >> I  do  not  see  the  way to use appropriate field of AVCodecContext to 
> >> share
> >> global data.
> >> Has anybody any ideas?
> >> Is  me  missed  something?  There is really the necessary to have a global 
> >> common
> >> structure shared between decoder, vpp and decoder.
> >>  
> >
> > There's no automagic way to get this done.
> >
> > Hardware accelerators like vaapi and vdpau need the same thing. These
> > have special APIs to set an API context on the decoder. Some APIs use
> > hwaccel_context, vdpau uses av_vdpau_bind_context().
> >
> > The hwaccel_context pointer is untyped (just a void*), and what it means
> > is implicit to the hwaccel or the decoder that is used. It could point
> > to some sort of qsv state, which will have to be explicitly allocated
> > and set by the API user (which might be ffmpeg.c).
> >
> > For filters there is no such thing yet. New API would have to be
> > created. For filters in particular, we will have to make sure that the
> > API isn't overly qsv-specific, and that it is extendable to other APIs
> > (like for example vaapi or vdpau).  
> 
> I have been looking into a slightly different way: the common object
> being transported is an AVFrame. So, my initial approach is to create
> an AV_FRAME_DATA_HWACCEL metadata. Lookups could be optimized by
> keeping around an AVFrameInternal struct that resides in the same
> allocation unit as the AVFrame. But, this is a detail.
> 
> From there, there are at least two usage models, when it comes to filters too:
> 
> 1. Make the AVHWAccelFrame struct hold multiple hwaccel-specific info,
> with a master one, and slave ones for various different APIs.
> Advantage: a single AVFrame can be used and impersonified whenever
> necessary. e.g. a VA surface master could be exported/re-used with an
> mfxSurface, a dma_buf (for OpenCL), or userptr buffer. Drawback:
> terribly tedious to manage.
> 
> 2. Simpler approach: the AVHWAccelFrame holds a unique struct to
> hwaccel specific data. Should we need to export that for use with
> another API, it's not too complicated to av_frame_ref() + add new
> hwaccel-specific metadata.

It could be something like an API identifier (an enum or so) + API
specific pointer to a struct.

> For VA-API specific purposes, I then have:
> - AVVADisplay, which is refcounted, and that can handle automatic
> initialize/terminate calls ;
> - AVVAFrameBuffer that holds an { AVVADisplay, VASurfaceID, and
> possibly VAImageID } tuple (for mappings in non-USWC memory), and that
> populates AVFrame.buf[0].

Sounds good.

> I am undecided yet on whether I'd create an AV_PIX_FMT_HWACCEL format
> and allow hwaccel specific AVFrame to absorb an existing AVFrame, as a
> transitional method from existing vaapi hwaccel to "new" vaapi
> hwaccel. In that new generic hwaccel format model, buf[0] et al. would
> be clearly defined, and data[i] not supposed to be touched by user
> application. For now, the trend towards that option is low in my mind.

I'd still have different pixfmts for each hwaccel, even if they behave
the same. (The main reason being that hw accel init via get_format()
requires it.)

IMHO, one AVFrame plane pointer should point to your suggested
AVHWAccelFrame. This would make more sense with how AVFrame
refcounting works in the general case.

AVFrame specifically demands that AVFrame.buf[] covers all memory
pointed to by AVFrame.data. This doesn't make much sense with the
current API, where AVFrame.data[3] is an API specific surface ID
(usually an integer casted to a pointer), and the AVBufferRef doesn't
really make any sense.

With the new suggestion, the AVBufferRef would cover the memory used by
AVHWAccelFrame. While not particularly useful, this is consistent, and
also frees API users from worrying about what the AVBufferRef data/size
fields should be set to.

As for compatiblity with old code: maybe AVFrame.data[1] could
point to something like this. But I think it's better to make a clean
break.

> PS: other benefit of the AVHWAccelFrame side-data is that I can stuff
> crop information into there. Since this is only useful to hwaccel, no
> need to populate AVFrame with additional fields IMHO.

IMHO, crop information should be gen

Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-23 Thread Gwenole Beauchesne
Hi,

2015-10-07 18:40 GMT+02:00 wm4 :
> On Wed, 7 Oct 2015 19:20:56 +0300
> Ivan Uskov  wrote:
>
>> Hello Hendrik,
>>
>> Wednesday, October 7, 2015, 5:58:25 PM, you wrote:
>>
>> HL> On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  wrote:
>>
>> HL> Global static variables are not acceptable, sorry.
>> HL> You'll have to find another way to solve your problem, but global
>> HL> state is not the way to go.
>> Unfortunately   I   do   not   have   ideas  how to provide single and common
>> memory  block  for  separatemodules   by  another  way.   Memory  mapping
>> files  looks not too portable and much more bulky solution then one global 
>> variable.
>> I  do  not  see  the  way to use appropriate field of AVCodecContext to share
>> global data.
>> Has anybody any ideas?
>> Is  me  missed  something?  There is really the necessary to have a global 
>> common
>> structure shared between decoder, vpp and decoder.
>>
>
> There's no automagic way to get this done.
>
> Hardware accelerators like vaapi and vdpau need the same thing. These
> have special APIs to set an API context on the decoder. Some APIs use
> hwaccel_context, vdpau uses av_vdpau_bind_context().
>
> The hwaccel_context pointer is untyped (just a void*), and what it means
> is implicit to the hwaccel or the decoder that is used. It could point
> to some sort of qsv state, which will have to be explicitly allocated
> and set by the API user (which might be ffmpeg.c).
>
> For filters there is no such thing yet. New API would have to be
> created. For filters in particular, we will have to make sure that the
> API isn't overly qsv-specific, and that it is extendable to other APIs
> (like for example vaapi or vdpau).

I have been looking into a slightly different way: the common object
being transported is an AVFrame. So, my initial approach is to create
an AV_FRAME_DATA_HWACCEL metadata. Lookups could be optimized by
keeping around an AVFrameInternal struct that resides in the same
allocation unit as the AVFrame. But, this is a detail.

From there, there are at least two usage models, when it comes to filters too:

1. Make the AVHWAccelFrame struct hold multiple hwaccel-specific info,
with a master one, and slave ones for various different APIs.
Advantage: a single AVFrame can be used and impersonified whenever
necessary. e.g. a VA surface master could be exported/re-used with an
mfxSurface, a dma_buf (for OpenCL), or userptr buffer. Drawback:
terribly tedious to manage.

2. Simpler approach: the AVHWAccelFrame holds a unique struct to
hwaccel specific data. Should we need to export that for use with
another API, it's not too complicated to av_frame_ref() + add new
hwaccel-specific metadata.

For VA-API specific purposes, I then have:
- AVVADisplay, which is refcounted, and that can handle automatic
initialize/terminate calls ;
- AVVAFrameBuffer that holds an { AVVADisplay, VASurfaceID, and
possibly VAImageID } tuple (for mappings in non-USWC memory), and that
populates AVFrame.buf[0].

I am undecided yet on whether I'd create an AV_PIX_FMT_HWACCEL format
and allow hwaccel specific AVFrame to absorb an existing AVFrame, as a
transitional method from existing vaapi hwaccel to "new" vaapi
hwaccel. In that new generic hwaccel format model, buf[0] et al. would
be clearly defined, and data[i] not supposed to be touched by user
application. For now, the trend towards that option is low in my mind.

PS: other benefit of the AVHWAccelFrame side-data is that I can stuff
crop information into there. Since this is only useful to hwaccel, no
need to populate AVFrame with additional fields IMHO.

>
> Unfortunately, you can be sure that we won't accept your current patch,
> though. Global mutable variables are a no in new code.
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Regards,
-- 
Gwenole Beauchesne
Intel Corporation SAS / 2 rue de Paris, 92196 Meudon Cedex, France
Registration Number (RCS): Nanterre B 302 456 199
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-22 Thread Hendrik Leppkes
On Wed, Oct 14, 2015 at 11:47 AM, Hendrik Leppkes  wrote:
> On Wed, Oct 7, 2015 at 7:07 PM, Ivan Uskov  wrote:
>> Hello wm4,
>>
>> Wednesday, October 7, 2015, 7:40:45 PM, you wrote:
>>
>> w> There's no automagic way to get this done.
>>
>> w> Hardware accelerators like vaapi and vdpau need the same thing. These
>> w> have special APIs to set an API context on the decoder. Some APIs use
>> w> hwaccel_context, vdpau uses av_vdpau_bind_context().
>>
>> w> The hwaccel_context pointer is untyped (just a void*), and what it means
>> w> is implicit to the hwaccel or the decoder that is used. It could point
>> w> to some sort of qsv state, which will have to be explicitly allocated
>> w> and set by the API user (which might be ffmpeg.c).
>> So  if  I will implement something like ffmpeg_qsv.c (using ffmpeg_vdpau.c as
>> example)   and   extend  the  hwaccels[]  into  ffmpeg_opt.c  by corresponded
>> qsv entries it  would  be the acceptable solution, correct?
>>
>> w> For filters there is no such thing yet. New API would have to be
>> w> created. For filters in particular, we will have to make sure that the
>> w> API isn't overly qsv-specific, and that it is extendable to other APIs
>> w> (like for example vaapi or vdpau).
>> Ok,   if   VPP  could be the  issue  I  would  like  to  get  working  direct
>> link qsv_decoder-qsv_encoder first.
>>
>
> Libav has a patch that does exactly this, allow direct QSV->QSV
> transcoding with help of some utility code in ffmpeg.c/avconv.c
> You might want to look at that instead of re-inventing it. That'll
> help make everyones live easier, as I'll just merge it when the time
> comes, and the codebases don't diverge too drastically.
>

This functionality has been merged now. It works for some samples.
You can try to use it with a command line like this:

ffmpeg -hwaccel qsv -c:v h264_qsv -i h264.ts -c:v h264_qsv output.mkv

This will transcode using a QSV->QSV pipeline, no copying to system
memory, and about 2.5x faster on my IVB laptop.

However, its broken on a lot of more complex H264 files, you'll get
errors like get_buffer() failed - this is because our qsvdec behaves
rather strangely, and instead of buffering input data when needed, it
buffers output surfaces, which is not ideal, since it bloats up the
memory usage an the number of surfaces required explodes into
infinity.
I know how to fix it - just restore the decoder to the same buffering
model as it originally used, buffer input data instead of output
surfaces. Unless someone else wants to fix it, I'll simply do it in
the next week or so.

- Hendrik
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-14 Thread Hendrik Leppkes
On Wed, Oct 7, 2015 at 7:07 PM, Ivan Uskov  wrote:
> Hello wm4,
>
> Wednesday, October 7, 2015, 7:40:45 PM, you wrote:
>
> w> There's no automagic way to get this done.
>
> w> Hardware accelerators like vaapi and vdpau need the same thing. These
> w> have special APIs to set an API context on the decoder. Some APIs use
> w> hwaccel_context, vdpau uses av_vdpau_bind_context().
>
> w> The hwaccel_context pointer is untyped (just a void*), and what it means
> w> is implicit to the hwaccel or the decoder that is used. It could point
> w> to some sort of qsv state, which will have to be explicitly allocated
> w> and set by the API user (which might be ffmpeg.c).
> So  if  I will implement something like ffmpeg_qsv.c (using ffmpeg_vdpau.c as
> example)   and   extend  the  hwaccels[]  into  ffmpeg_opt.c  by corresponded
> qsv entries it  would  be the acceptable solution, correct?
>
> w> For filters there is no such thing yet. New API would have to be
> w> created. For filters in particular, we will have to make sure that the
> w> API isn't overly qsv-specific, and that it is extendable to other APIs
> w> (like for example vaapi or vdpau).
> Ok,   if   VPP  could be the  issue  I  would  like  to  get  working  direct
> link qsv_decoder-qsv_encoder first.
>

Libav has a patch that does exactly this, allow direct QSV->QSV
transcoding with help of some utility code in ffmpeg.c/avconv.c
You might want to look at that instead of re-inventing it. That'll
help make everyones live easier, as I'll just merge it when the time
comes, and the codebases don't diverge too drastically.

Adding VPP on top of that would be a future step then.

- Hendrik
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-07 Thread Ivan Uskov
Hello wm4,

Wednesday, October 7, 2015, 7:40:45 PM, you wrote:

w> There's no automagic way to get this done.

w> Hardware accelerators like vaapi and vdpau need the same thing. These
w> have special APIs to set an API context on the decoder. Some APIs use
w> hwaccel_context, vdpau uses av_vdpau_bind_context().

w> The hwaccel_context pointer is untyped (just a void*), and what it means
w> is implicit to the hwaccel or the decoder that is used. It could point
w> to some sort of qsv state, which will have to be explicitly allocated
w> and set by the API user (which might be ffmpeg.c).
So  if  I will implement something like ffmpeg_qsv.c (using ffmpeg_vdpau.c as
example)   and   extend  the  hwaccels[]  into  ffmpeg_opt.c  by corresponded
qsv entries it  would  be the acceptable solution, correct?

w> For filters there is no such thing yet. New API would have to be
w> created. For filters in particular, we will have to make sure that the
w> API isn't overly qsv-specific, and that it is extendable to other APIs
w> (like for example vaapi or vdpau).
Ok,   if   VPP  could be the  issue  I  would  like  to  get  working  direct
link qsv_decoder-qsv_encoder first.

-- 
Best regards,
 Ivanmailto:ivan.us...@nablet.com

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-07 Thread wm4
On Wed, 7 Oct 2015 19:20:56 +0300
Ivan Uskov  wrote:

> Hello Hendrik,
> 
> Wednesday, October 7, 2015, 5:58:25 PM, you wrote:
> 
> HL> On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  wrote:
> 
> HL> Global static variables are not acceptable, sorry.
> HL> You'll have to find another way to solve your problem, but global
> HL> state is not the way to go.
> Unfortunately   I   do   not   have   ideas  how to provide single and common
> memory  block  for  separatemodules   by  another  way.   Memory  mapping
> files  looks not too portable and much more bulky solution then one global 
> variable.
> I  do  not  see  the  way to use appropriate field of AVCodecContext to share
> global data.
> Has anybody any ideas?
> Is  me  missed  something?  There is really the necessary to have a global 
> common
> structure shared between decoder, vpp and decoder. 
> 

There's no automagic way to get this done.

Hardware accelerators like vaapi and vdpau need the same thing. These
have special APIs to set an API context on the decoder. Some APIs use
hwaccel_context, vdpau uses av_vdpau_bind_context().

The hwaccel_context pointer is untyped (just a void*), and what it means
is implicit to the hwaccel or the decoder that is used. It could point
to some sort of qsv state, which will have to be explicitly allocated
and set by the API user (which might be ffmpeg.c).

For filters there is no such thing yet. New API would have to be
created. For filters in particular, we will have to make sure that the
API isn't overly qsv-specific, and that it is extendable to other APIs
(like for example vaapi or vdpau).

Unfortunately, you can be sure that we won't accept your current patch,
though. Global mutable variables are a no in new code.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-07 Thread Ivan Uskov
Hello Hendrik,

Wednesday, October 7, 2015, 5:58:25 PM, you wrote:

HL> On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  wrote:

HL> Global static variables are not acceptable, sorry.
HL> You'll have to find another way to solve your problem, but global
HL> state is not the way to go.
Unfortunately   I   do   not   have   ideas  how to provide single and common
memory  block  for  separatemodules   by  another  way.   Memory  mapping
files  looks not too portable and much more bulky solution then one global 
variable.
I  do  not  see  the  way to use appropriate field of AVCodecContext to share
global data.
Has anybody any ideas?
Is  me  missed  something?  There is really the necessary to have a global 
common
structure shared between decoder, vpp and decoder. 

-- 
Best regards,
 Ivanmailto:ivan.us...@nablet.com

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] libavcodec/qsv.c: Re-design session control and internal allocation

2015-10-07 Thread Hendrik Leppkes
On Wed, Oct 7, 2015 at 4:41 PM, Ivan Uskov  wrote:
> Hello All,
>
> The attached patch represents new design for qsv session control and internal
> allocation.  All qsv modules now uses instance of AVQSVContext so now session
> allocates by external application and session allocates internally by ffmpeg
> itself handles by unified way.
> For  the  case  of  internal  session  allocation  now one global instance of
> AVQSVContext   creates,   I.e. one common session uses for all qsv components
> in  processing  chain   (decoder,   vpp,  encoder).   This   opens  a  way to
> implement a complex video processing into the GPU without system memory using.
>
> index 4c8e6b0..6ced294 100644
>--- a/libavcodec/qsv.c
>+++ b/libavcodec/qsv.c
>@@ -30,6 +30,8 @@
> #include "avcodec.h"
> #include "qsv_internal.h"
>
>+static AVQSVContext* g_av_qsv = NULL;
>+
> int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
> {

Global static variables are not acceptable, sorry.
You'll have to find another way to solve your problem, but global
state is not the way to go.

- Hendrik
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel