[FFmpeg-devel] [PATCH] avformat/pcm: decrease delay when reading PCM streams.

Tomas Härdin tjoppen at acc.umu.se
Tue Feb 13 17:11:45 EET 2018


tis 2018-02-13 klockan 15:11 +0100 skrev Philipp M. Scholl:
>  The blocksize of the PCM decoder is hard-coded. This creates
>  unnecessary delay when reading low-rate (<100Hz) streams. This creates
> issues when multiplexing multiple streams, since other inputs are only
> opened/read after a low-rate input block was completely read.
> 
>  This patch decreases the blocksize for low-rate inputs, so
> approximately a block is read every 10ms. This decreases the startup
> delay when multiplexing inputs with different rates.
> 
> > Signed-off-by: Philipp M. Scholl <pscholl at bawue.de>
> ---
> diff --git a/libavformat/pcm.c b/libavformat/pcm.c
> index 806f91b6b1..f0ea029633 100644
> --- a/libavformat/pcm.c
> +++ b/libavformat/pcm.c
> @@ -24,17 +24,48 @@
>  #include "internal.h"
>  #include "pcm.h"
>  
> -#define RAW_SAMPLES     1024
> +#define RAW_SAMPLES 1024

Please avoid mixing cosmetic and functional changes

> +
> +/*
> + * copied from https://stackoverflow.com/questions/2679815/previous-power-of-2
> + * computes the previous power of two.
> + */

Got any idea what kind of license Hacker's Delight uses?

> +static uint32_t flp2 (uint32_t x)
> +{
> +    x = x | (x >> 1);
> +    x = x | (x >> 2);
> +    x = x | (x >> 4);
> +    x = x | (x >> 8);
> +    x = x | (x >> 16);
> +    x = x - (x >> 1);
> +    return x + (x == 0); /* if x is zero, return 1 */
> +}

What's wrong with 1<<ff_log2(x)?

>  
>  int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
>  {
> -    int ret, size;
> +    int i, ret, strmsize, readsize = INT_MAX;
> +    AVCodecParameters *codec;
> +
> +     /*
> +      * recompute read size based on sample rate of the inputs, make sure
> +      * to complete a read every 10ms by selecting the smallest sample rate
> +      */
> +    for (i=0; i < s->nb_streams; i++) {
> +      codec = s->streams[i]->codecpar;
> +      strmsize = codec->sample_rate / 100 * codec->block_align;

What if sample_rate < 100 Hz?

> +      readsize = FFMIN(readsize, strmsize);
> +    }
> +
> +    /*
> +     * clamp to RAW_SAMPLES if larger, and to previous power of two.
> +     */
> +    readsize = flp2(FFMIN(readsize,
> +               RAW_SAMPLES * s->streams[0]->codecpar->block_align));

Any particular reason for previous power of two?

>  
> -    size= RAW_SAMPLES*s->streams[0]->codecpar->block_align;
> -    if (size <= 0)
> +    if (readsize <= 0)
>          return AVERROR(EINVAL);

Might want to separate this into a check for block_align>0, then
FFMAX(readsize, block_align) so we always get something. That would
take care of the < 100 Hz issue

>  
> -    ret= av_get_packet(s->pb, pkt, size);
> +    ret = av_get_packet(s->pb, pkt, readsize);

Keeping the name as "size" removes the need for this hunk :)

/Tomas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20180213/83a86f2c/attachment.sig>


More information about the ffmpeg-devel mailing list