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

Justin Ruggles justin.ruggles
Sun Apr 19 16:06:51 CEST 2009


Michael Niedermayer wrote:
> On Sun, Apr 19, 2009 at 12:17:32AM -0400, Justin Ruggles wrote:
>> Michael Niedermayer wrote:
>>> 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
>> new patch attached with the simpler code.
> 
> ok

Applied, with Reimar's suggested comment.




More information about the ffmpeg-devel mailing list