[FFmpeg-devel] [PATCH v4 1/1] avutils/hwcontext: When deriving a hwdevice, search for existing device in both directions

Soft Works softworkz at hotmail.com
Sun Jan 9 23:15:28 EET 2022



> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces at ffmpeg.org> On Behalf Of Mark
> Thompson
> Sent: Sunday, January 9, 2022 7:39 PM
> To: ffmpeg-devel at ffmpeg.org
> Subject: Re: [FFmpeg-devel] [PATCH v4 1/1] avutils/hwcontext: When deriving a
> hwdevice, search for existing device in both directions
> 
> On 05/01/2022 03:38, Xiang, Haihao wrote:
> > ... this patch really fixed some issues for me and others.
> 
> Can you explain this in more detail?
> 
> I'd like to understand whether the issues you refer to are something which
> would be fixed by the ffmpeg utility allowing selection of devices for
> libavfilter, or whether they are something unrelated.
> 
> (For library users the currently-supported way of getting the same device
> again is to keep a reference to the device and reuse it.  If there is some
> case where you can't do that then it would be useful to hear about it.)

Hi Mark,

they have 3 workaround patches on their staging repo, but I'll let Haihao 
answer in detail.

I have another question. I've been searching high and low, yet I can't
find the message. Do you remember that patch discussion from (quite a 
few) months ago, where it was about another QSV change (something about
device creation from the command line, IIRC). There was a command line
example with QSV and you correctly remarked something like:
"Do you even know that just for this command line, there are 5 device
creations happening in the background, implicit and explicit, and in
one case (or 2), it's not even creating the specified device but
a session for the default device instead"
(just roughly from memory)

Do you remember - or was it Philip?

Anyway, this is something that the patch will improve. There has been one
other commit since that time regarding explicit device creation from 
Haihao (iirc), which already reduced the device creation and fixed the
incorrect default session creation. 

My patch tackles this from another side: at that time, you (or Philip) 
explained that the secondary context that QSV requires (VAAPI, D3Dx)
and that is initially created when setting up the QSV device, does not
even get used when subsequently deriving to a context of that kind. 
Instead, a new device is being created in this case.

That's another scenario which is fixed by this patch.


It's a hybrid device context, that's the reason why QSV is more affected 
than all other hwaccels as it consists of a QSV session already DERIVED 
from a VAAPI or D3Dx device. 

Example (let's assume Windows with D3D9): You go into decoding with a 
QSV decoder in a QSV context and then you want to use an OpenCL filter. 
This requires an OpenCL context, and of course you want to share the 
frames memory. For memory sharing, OpenCL requires the underlying context 
of the QSV session - in this example D3D9.

Before this patch - like you said - deriving devices was usually (except 
reverse hwmap) forward-only. That means - you are stuck in this situation:
you could (forward-)derive to a D3D9 context, but that doesn't help: for
sharing the memory, you need to provide the original hw device to OpenCL,
you can't supply just another newly derived device of the same type.
And there is (was) no way to get the original hw context.


Anyway I'm wondering whether it can even be logically valid to derive
from one device to another and then to another instance of the previous
device type.
From my understanding, "deriving" or "hw mapping" from one device to 
another means to establish a different way or accessor to a common
resource/data, which means that you can access the data in one or the 
other way.

Now let's assume a forward device-derivation chain like this:

D3D_1  >>  OpenCL_1  >>  D3D_2

We have D3D surfaces, then we share them with OpenCL. Both *_1
contexts provide access to the same data.
Then we derive again "forward only" and we get a new D3D_2
context. It is derived from OpenCL_1, so it must provide
access to the same data as OpenCL_1 AND D3D_1.

Now we have two different D3D contexts which are supposed to
provide access to the same data!


1. This doesn't even work technically
   - neither from D3D (iirc)
   - nor from ffmpeg (not cleanly)

2. This doesn't make sense at all. There should always be
   only a single device context of a device type for the same 
   resource

3. Why would somebody even want this - what kind of use case?


Regards,
softworkz





More information about the ffmpeg-devel mailing list