[FFmpeg-devel] [PATCH] check if channel layout matches number of channels

Michael Niedermayer michaelni
Sun Apr 19 04:29:04 CEST 2009


On Sat, Apr 18, 2009 at 07:41:14PM -0400, Justin Ruggles wrote:
> Hi,
> 
> Currently the output channel layout is always set to the input channel
> layout, even when upmixing or downmixing.  This patch checks that the
> channel layout matches the number of channels.  If it does not, the
> output channel layout is set to 0.
> 
> I put the function in audioconvert.c/h so that encoders and decoders can
>  also use it to validate the input.
> 
> -Justin
> 
> 

> diff --git a/ffmpeg.c b/ffmpeg.c
> index 200d57d..c050512 100644
> --- a/ffmpeg.c
> +++ b/ffmpeg.c
> @@ -3212,6 +3212,8 @@ static void new_audio_stream(AVFormatContext *oc)
>          audio_enc->channels = audio_channels;
>          audio_enc->sample_fmt = audio_sample_fmt;
>          audio_enc->channel_layout = channel_layout;
> +        if (avcodec_channel_layout_num_channels(channel_layout) != audio_channels)
> +            audio_enc->channel_layout = 0;
>  
>          if(codec && codec->sample_fmts){
>              const enum SampleFormat *p= codec->sample_fmts;
> diff --git a/libavcodec/audioconvert.c b/libavcodec/audioconvert.c
> index b0a8afa..e4c9890 100644
> --- a/libavcodec/audioconvert.c
> +++ b/libavcodec/audioconvert.c
> @@ -153,6 +153,17 @@ void avcodec_get_channel_layout_string(char *buf, int buf_size, int nb_channels,
>      }
>  }
>  
> +int avcodec_channel_layout_num_channels(int64_t channel_layout)
> +{
> +    /* based on a public domain bit counting algorithm found at
> +       http://graphics.stanford.edu/~seander/bithacks.html */
> +    uint64_t v = channel_layout;
> +    v = v - ((v >> 1) & 0x5555555555555555ULL);
> +    v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
> +    v = (v + (v >> 4)) & 0xF0F0F0F0F0F0F0FULL;
> +    return (v * 0x101010101010101ULL) >> 56;
> +}

this is not speed critical, not even close, so we prefer simple code over
fast code.

for(count=0; x; count++)
    x&= x-1;
return count;

is obfuscated enough

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

While the State exists there can be no freedom; when there is freedom there
will be no State. -- Vladimir Lenin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090419/e80075b8/attachment.pgp>



More information about the ffmpeg-devel mailing list