Make VideoFrame.from_numpy_buffer support buffers with padding #1635
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is the last PR after which Raspberry Pi should be able to revert back to the standard mainline PyAV distribution.
To explain the background for this PR:
The Raspberry Pi camera stack uses lots of hardware accelerators, and these have alignment requirements for the start of every image row. This means that the images it spits out normally have padding at the end of each row.
libav
is perfectly capable of using these buffers directly so long as we set itslinesize
values correctly. That's what most of this PR is about.VideoFrame.from_numpy_buffer
is extended to accept buffers where the pixel rows are contiguous, but the image as a whole (because of the padding on the end) isn't. I'm detecting contiguous pixel rows by looking at the array'sstrides
. The last one or two values here allow us to deduce this. We very much preferfrom_numpy_buffer
overfrom_ndarray
becausefrom_ndarray
copies image buffers - and these operations are really expensive for us.I've also taken over the calculation of the linesizes directly, rather than calling another function. The values can be deduced pretty much directly from the array
strides
in all cases, and this solves the problem where, when there's padding, the array width (timesbytes_per_pixel
) is no longer the correct value - the underlying stride always is.There's one final gotcha, which is
yuv420p
images (which are common for us). Here too we usually have padding. For the Y channel, the padding is at the end (as normal). But for the UV rows (the bottom third of the buffer), half the padding appears in the middle of the buffer row, and the other half at the end. This means applications can't create a "view" on this buffer which omits the padding (as they can with the RGB buffer types).Instead, we have to let applications pass in the buffer with all the padding, and give us a separate
width
parameter which tells us what's usable. Obviously I've madewidth
optional, defaulting to the previous behaviour. Again, so long aslibav
has the correctwidth
andlinesize
values, it handles these buffers correctly.I've checked that
to_ndarray
works correctly on all these buffers (by removing the padding), and added quite a few extra tests to check that these new types of buffer are behaving as expected. I also added support for 32-bit RGBA types, which again are quite common for Raspberry Pi users.I hope that all makes sense! If there's anything to discuss, or which you'd like me to look at again, I'm of course very happy to do so.
Thanks very much for your help in getting our various changes merged into mainline PyAV!