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

Justin Ruggles justin.ruggles
Sun Apr 19 06:17:32 CEST 2009


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.

-Justin

-------------- next part --------------
A non-text attachment was scrubbed...
Name: channel_layout_num_channels_2.diff
Type: text/x-diff
Size: 1575 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090419/8a8de55b/attachment.diff>



More information about the ffmpeg-devel mailing list