[FFmpeg-devel] [PATCH 0/3] cbs: Reuse the units array

Andreas Rheinhardt andreas.rheinhardt at googlemail.com
Tue Feb 5 22:08:49 EET 2019

Currently, in a lot of places where cbs is used there is a fragment in
the relevant context that is constantly reused to decompose and
recompose packets. This currently works as follows:

The fragment (which is already zeroed at the very beginning) is
zeroed at the beginning of the process of reading a packet/extradata;
and when the packet has been split into units, each time a unit is added
the whole units array is reallocated. Then the units are decomposed into
their content and the users of cbs (mostly bitstream filters) work with
the content/data before they uninitialize the fragment which in particular
sets every member of the fragment to zero/NULL. Then this process iterates.

This has two flaws: The minor one is that the fragment is effectively
zeroed twice in every iteration; this can be solved by making the
zeroing in ff_cbs_read* optional.

The bigger one is that the units array is constantly reallocated which
can be quite expensive, depending upon the number of units a packet is
split into. This could be solved by adding the distinction between valid
units and allocated units which enables one to keep the units array.

In the end, there was no place left where ff_cbs_read* needed to zero
anything, but I nevertheless included the option to do so so that one can
use uninitialized fragments.

I kept the changes to the VAAPI code to a minimum, because I can't test
VAAPI. But it seems as if one could avoid reallocating the units array
there, too, by changing the ff_cbs_fragment_uninit calls already present
to not free the units array and freeing the units array in the *_close
functions (via ff_cbs_fragment_uninit).

One last thing: I am unsure whether I should have added a flush-function
for the BSFs that don't have one now in order to free the units array;
but I decided against it, because e.g. every time cbs is used e.g. with
H264, the CodedBitstreamH264Context also has internal buffers (the
parameter sets) and nevertheless h264_metadata has no flush mechanism;
and h264_redundant_pps does have a flush function, but these parameter
sets aren't flushed, so this seems to be unnecessary. 

Andreas Rheinhardt (3):
  libavcodec/cbs: Add ability to keep the units array.
  libavcodec/cbs: Don't zero twice
  cbs: Stop reallocating fragment unit arrays

 libavcodec/av1_metadata_bsf.c       | 10 +++--
 libavcodec/av1_parser.c             |  9 ++--
 libavcodec/cbs.c                    | 68 ++++++++++++++++++-----------
 libavcodec/cbs.h                    | 39 ++++++++++++++---
 libavcodec/filter_units_bsf.c       | 15 ++++---
 libavcodec/h264_metadata_bsf.c      | 10 +++--
 libavcodec/h264_redundant_pps_bsf.c | 10 +++--
 libavcodec/h265_metadata_bsf.c      | 10 +++--
 libavcodec/mpeg2_metadata_bsf.c     | 10 +++--
 libavcodec/trace_headers_bsf.c      | 14 +++---
 libavcodec/vaapi_encode_h264.c      |  8 ++--
 libavcodec/vaapi_encode_h265.c      |  8 ++--
 libavcodec/vaapi_encode_mjpeg.c     |  2 +-
 libavcodec/vaapi_encode_mpeg2.c     |  4 +-
 libavcodec/vp9_metadata_bsf.c       |  6 ++-
 15 files changed, 141 insertions(+), 82 deletions(-)


More information about the ffmpeg-devel mailing list