libavcodec/cbs: Stop needlessly reallocating the units array

Currently, a fragment's unit array is constantly reallocated during
splitting of a packet. This commit changes this: One can keep the units
array by distinguishing between the number of allocated and the number
of valid units in the units array.

The more units a packet is split into, the bigger the benefit.
So MPEG-2 benefits the most; for a video coming from an NTSC-DVD
(usually 32 units per frame) the average cost of cbs_insert_unit (for a
single unit) went down from 6717 decicycles to 450 decicycles (based
upon 10 runs with 4194304 runs each); if each packet consists of only
one unit, it went down from 2425 to 448; for a H.264 video where most
packets contain nine units, it went from 4431 to 450.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
This commit is contained in:
Andreas Rheinhardt
2019-02-11 23:47:43 +01:00
committed by Mark Thompson
parent c5b452ed2f
commit b8c45bbcbc
15 changed files with 113 additions and 59 deletions

View File

@@ -145,10 +145,19 @@ typedef struct CodedBitstreamFragment {
* and has not been decomposed.
*/
int nb_units;
/**
* Pointer to an array of units of length nb_units.
* Number of allocated units.
*
* Must be NULL if nb_units is zero.
* Must always be >= nb_units; designed for internal use by cbs.
*/
int nb_units_allocated;
/**
* Pointer to an array of units of length nb_units_allocated.
* Only the first nb_units are valid.
*
* Must be NULL if nb_units_allocated is zero.
*/
CodedBitstreamUnit *units;
} CodedBitstreamFragment;
@@ -231,6 +240,9 @@ void ff_cbs_close(CodedBitstreamContext **ctx);
* This also updates the internal state, so will need to be called for
* codecs with extradata to read parameter sets necessary for further
* parsing even if the fragment itself is not desired.
*
* The fragment must have been zeroed or reset via ff_cbs_fragment_reset
* before use.
*/
int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
@@ -243,6 +255,9 @@ int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
* This also updates the internal state of the coded bitstream context
* with any persistent data from the fragment which may be required to
* read following fragments (e.g. parameter sets).
*
* The fragment must have been zeroed or reset via ff_cbs_fragment_reset
* before use.
*/
int ff_cbs_read_packet(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
@@ -255,6 +270,9 @@ int ff_cbs_read_packet(CodedBitstreamContext *ctx,
* This also updates the internal state of the coded bitstream context
* with any persistent data from the fragment which may be required to
* read following fragments (e.g. parameter sets).
*
* The fragment must have been zeroed or reset via ff_cbs_fragment_reset
* before use.
*/
int ff_cbs_read(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
@@ -294,11 +312,18 @@ int ff_cbs_write_packet(CodedBitstreamContext *ctx,
/**
* Free all allocated memory in a fragment.
* Free the units contained in a fragment as well as the fragment's
* own data buffer, but not the units array itself.
*/
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag);
/**
* Free the units array of a fragment in addition to what
* ff_cbs_fragment_reset does.
*/
void ff_cbs_fragment_free(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag);
/**
* Allocate a new internal content buffer of the given size in the unit.