On 25/11/2021 02:41, Soft Works wrote:
The test /libavutil/tests/hwdevice checks that when deriving a device
from a source device and then deriving back to the type of the source
device, the result is matching the original source device, i.e. the
derivation mechanism doesn't create a new device in this case.

Previously, this test was usually passed, but only due to two different
kind of flaws:

1. The test covers only a single level of derivation (and back)

It derives device Y from device X and then Y back to the type of X and
checks whether the result matches X.

What it doesn't check for, are longer chains of derivation like:

CUDA1 > OpenCL2 > CUDA3 and then back to OpenCL4

In that case, the second derivation returns the first device (CUDA3 ==
CUDA1), but when deriving OpenCL4, hwcontext.c was creating a new
OpenCL4 context instead of returning OpenCL2, because there was no link
from CUDA1 to OpenCL2 (only backwards from OpenCL2 to CUDA1)

Yes, this is exactly what I expect.

Because of how these APIs work, device derivation is always one-way - you can 
make an OpenCL device from a D3D11 one, but not the other direction.  I don't 
think there is any case which allows both directions (nor should there be, in 
general), so the possibilities form an acyclic graph.

I do expect the user to know here what the possible-derivation graph looks like 
for the cases they want to use (since only a few cases are meaningful), though 
maybe it would be worth explicitly documenting it somewhere.

There are then two non-overlapping operations combined in the device derivation 
function (when A -> B is the meaningful direction):
* Given a context of type A, create a context of type B using the same physical 
device.
* Given a context of type B, retrieve the original context of type A it was 
created from (if it exists).

(Maybe they should have been separate functions to avoid this confusion, but it 
did fit rather nicely in how hwmap works.

Saying that derivation from A should always return the same B is not intended, 
nor do I think it should be.

The problem with doing that is that components can no longer tell whether the 
device they have is shared with someone else, whether they are in ffmpeg, in 
unrelated libraries, or in user code.

What that actually means depends on the API, but in general it would ban doing 
anything which might affect another user of the same device, such as setting 
any global options or calling anything which might not be thread-safe.

(Note that the global options argument in av_hwdevice_create_derived_opts() 
already violates this constraint, making the patch wrong as-is even if you do 
think it is reasonable to impose this constraint on all users.)

If the test would check for two levels of derivation, it would have
failed.

This patch fixes those (yet untested) cases by introducing forward
references (derived_device) in addition to the existing back references
(source_device).

2. hwcontext_qsv didn't properly set the source_device

In case of QSV, hwcontext_qsv creates a source context internally
(vaapi, dxva2 or d3d11va) without calling av_hwdevice_ctx_create_derived
and without setting source_device.

What would go wrong if it did?

This way, the hwcontext test ran successful, but what practically
happened, was that - for example - deriving vaapi from qsv didn't return
the original underlying vaapi device and a new one was created instead:

Er... how would it create a new device?  The only type VAAPI can derive from is 
DRM.

Exactly what the test is intended to detect and prevent. It just
couldn't do so, because the original device was hidden (= not set as the
source_device of the QSV device).

This patch properly makes these setting and fixes all derivation
scenarios.

(at a later stage, /libavutil/tests/hwdevice should be extended to check
longer derivation chains as well)

Signed-off-by: softworkz <softwo...@hotmail.com>
---
v4: Fixed braces code style

  libavutil/hwcontext.c          | 38 ++++++++++++++++++++++++++++++++++
  libavutil/hwcontext.h          |  1 +
  libavutil/hwcontext_internal.h |  6 ++++++
  libavutil/hwcontext_qsv.c      | 13 +++++++++---
  4 files changed, 55 insertions(+), 3 deletions(-)

- Mark
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to