• Andreas Rheinhardt's avatar
    avcodec/flac_parser: Fix off-by-one error · e5e5be4c
    Andreas Rheinhardt authored
    The flac parser uses a fifo to buffer its data. Consequently, when
    searching for sync codes of flac packets, one needs to take care of
    the possibility of wraparound. This is done by using an optimized start
    code search that works on each of the continuous buffers separately and
    by explicitly checking whether the last pre-wrap byte and the first
    post-wrap byte constitute a valid sync code.
    
    Moreover, the last MAX_FRAME_HEADER_SIZE - 1 bytes ought not to be searched
    for (the start of) a sync code because a header that might be found in this
    region might not be completely available. These bytes ought to be searched
    lateron when more data is available or when flushing.
    
    Unfortunately there was an off-by-one error in the calculation of the
    length to search of the post-wrap buffer: It was too large, because the
    calculation was based on the amount of bytes available in the fifo from
    the last pre-wrap byte onwards. This meant that a header might be
    parsed twice (once prematurely and once regularly when more data is
    available); it could also mean that an invalid header will be treated as
    valid (namely if the length of said invalid header is
    MAX_FRAME_HEADER_SIZE and the invalid byte that will be treated as the
    last byte of this potential header happens to be the right CRC-8).
    
    Should a header be parsed twice, the second instance will be the best child
    of the first instance; the first instance's score will be
    FLAC_HEADER_BASE_SCORE - FLAC_HEADER_CHANGED_PENALTY ( = 3) higher than
    the second instance's score. So the frame belonging to the first
    instance will be output and it will be done as a zero length frame (the
    difference of the header's offset and the child's offset). This has
    serious consequences when flushing, as returning a zero length buffer
    signals to the caller that no more data will be output; consequently the
    last frames not yet output will be dropped.
    
    Furthermore, a "sample/frame number mismatch in adjacent frames" warning
    got output when returning the zero-length frame belonging to the first
    header, because the child's sample/frame number of course didn't match
    the expected sample frame/number given its parent.
    
    filter/hdcd-mix.flac from the FATE-suite was affected by this (the last
    frame was omitted) which is the reason why several FATE-tests needed to
    be updated.
    
    Fixes ticket #5937.
    Signed-off-by: 's avatarAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
    e5e5be4c
Name
Last commit
Last update
..
api Loading commit data...
checkasm Loading commit data...
dnn Loading commit data...
fate Loading commit data...
filtergraphs Loading commit data...
ref Loading commit data...
.gitignore Loading commit data...
Makefile Loading commit data...
audiogen.c Loading commit data...
audiomatch.c Loading commit data...
base64.c Loading commit data...
copycooker.sh Loading commit data...
extended.ffconcat Loading commit data...
fate-run.sh Loading commit data...
fate-valgrind.supp Loading commit data...
fate.sh Loading commit data...
md5.sh Loading commit data...
refcmp-metadata.awk Loading commit data...
reference.pnm Loading commit data...
rotozoom.c Loading commit data...
simple1.ffconcat Loading commit data...
simple2.ffconcat Loading commit data...
test.ffmeta Loading commit data...
tiny_psnr.c Loading commit data...
tiny_ssim.c Loading commit data...
utils.c Loading commit data...
videogen.c Loading commit data...