• Andreas Rheinhardt's avatar
    avformat/aviobuf: Avoid allocating buffer when using dynamic buffer · 163bb9ac
    Andreas Rheinhardt authored
    Up until now, using a dynamic buffer entailed at least three
    allocations: One for the AVIOContext, one for the AVIOContext's opaque
    (which, among other things, contains the small write buffer), and one
    for the big buffer that is independently allocated that is returned when
    calling avio_close_dyn_buf().
    
    It is possible to avoid the third allocation if one doesn't use a
    packetized dynamic buffer, if all the data written so far fit into the
    write buffer and if one does not require the actual (big) buffer to have
    an indefinite lifetime. This is done by making avio_get_dyn_buf() return
    a pointer to the data in the write buffer if nothing has been written to
    the main buffer yet. The dynamic buffer will then be freed using
    ffio_free_dynamic_buffer (which needed to be modified not to call
    avio_close_dyn_buf() internally).
    
    So a typical use-case like:
    
    size = avio_close_dyn_buf(dyn_pb, &buf);
    do something with buf
    av_free(buf);
    
    can be converted to:
    
    size = avio_get_dyn_buf(dyn_pb, &buf);
    do something with buf
    ffio_free_dynamic_buffer(&dyn_pb);
    
    In more complex scenarios this can simplify freeing as well, because it
    is now clear that freeing always has to be performed via
    ffio_free_dynamic_buffer().
    
    Of course, in case this saves an allocation it also saves a memcpy.
    Signed-off-by: 's avatarAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
    163bb9ac
Name
Last commit
Last update
compat Loading commit data...
doc Loading commit data...
ffbuild Loading commit data...
fftools Loading commit data...
libavcodec Loading commit data...
libavdevice Loading commit data...
libavfilter Loading commit data...
libavformat Loading commit data...
libavresample Loading commit data...
libavutil Loading commit data...
libpostproc Loading commit data...
libswresample Loading commit data...
libswscale Loading commit data...
presets Loading commit data...
tests Loading commit data...
tools Loading commit data...
.gitattributes Loading commit data...
.gitignore Loading commit data...
.travis.yml Loading commit data...
CONTRIBUTING.md Loading commit data...
COPYING.GPLv2 Loading commit data...
COPYING.GPLv3 Loading commit data...
COPYING.LGPLv2.1 Loading commit data...
COPYING.LGPLv3 Loading commit data...
CREDITS Loading commit data...
Changelog Loading commit data...
INSTALL.md Loading commit data...
LICENSE.md Loading commit data...
MAINTAINERS Loading commit data...
Makefile Loading commit data...
README.md Loading commit data...
RELEASE Loading commit data...
configure Loading commit data...