[vlc-commits] [Git][videolan/vlc][master] audio_output: require 0 terminated output channel order table

Steve Lhomme (@robUx4) gitlab at videolan.org
Sun Nov 29 08:42:55 UTC 2026



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
e0bc4a81 by Steve Lhomme at 2026-11-29T08:21:22+00:00
audio_output: require 0 terminated output channel order table

Just as we require the input to be 0 terminated.
If the output cannot handle all the channels we return 0 as there is no way
to distinguish 0 error from no reordering needed.

And add some comments in the code.

As we need an extra item in addition to the AOUT_CHAN_MAX we may receive,
we no longer need to memset the output in AndroidDevice_GetChanOrder().

- - - - -


8 changed files:

- include/vlc_aout.h
- modules/audio_output/alsa.c
- modules/audio_output/android/aaudio.c
- modules/audio_output/android/audiotrack.c
- modules/audio_output/android/device.h
- modules/audio_output/apple/coreaudio_common.c
- modules/codec/avcodec/encoder.c
- src/audio_output/common.c


Changes:

=====================================
include/vlc_aout.h
=====================================
@@ -387,9 +387,11 @@ static const uint32_t pi_vlc_chan_order_wg4[] =
  * This function computes the reordering needed to go from pi_chan_order_in to
  * pi_chan_order_out.
  * If pi_chan_order_in or pi_chan_order_out is NULL, it will assume that vlc
- * internal (WG4) order is requested.
+ * internal (WG4 = pi_vlc_chan_order_wg4) order is requested.
+ * The tables must be 0 terminated.
  */
-VLC_API unsigned aout_CheckChannelReorder( const uint32_t *, const uint32_t *,
+VLC_API unsigned aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
+                                           const uint32_t *pi_chan_order_out,
                                            uint32_t mask, uint8_t *table );
 
 /**


=====================================
modules/audio_output/alsa.c
=====================================
@@ -656,7 +656,7 @@ static int Map2Mask (vlc_object_t *obj, const snd_pcm_chmap_t *restrict map)
 static unsigned SetupChannelsFixed(const snd_pcm_chmap_t *restrict map,
                                uint16_t *restrict maskp, uint8_t *restrict tab)
 {
-    uint32_t chans_out[AOUT_CHAN_MAX];
+    uint32_t chans_out[AOUT_CHAN_MAX + 1] = {0};
     uint16_t mask = 0;
 
     for (unsigned i = 0; i < map->channels; i++)


=====================================
modules/audio_output/android/aaudio.c
=====================================
@@ -568,7 +568,7 @@ OpenAAudioStream(aout_stream_t *stream)
 
     vt.AAudioStreamBuilder_setFormat(builder, sys->cfg.format);
 
-    uint32_t chans_out[AOUT_CHAN_MAX];
+    uint32_t chans_out[AOUT_CHAN_MAX + 1] = {0};
     AndroidDevice_GetChanOrder(sys->fmt.i_physical_channels, chans_out,
                                AOUT_CHAN_MAX );
     sys->chans_to_reorder =


=====================================
modules/audio_output/android/audiotrack.c
=====================================
@@ -1721,7 +1721,7 @@ StartPCM( JNIEnv *env, aout_stream_t *stream, unsigned i_max_channels )
     if( i_ret != VLC_SUCCESS )
         return i_ret;
 
-    uint32_t p_chans_out[AOUT_CHAN_MAX];
+    uint32_t p_chans_out[AOUT_CHAN_MAX + 1] = {0};
     AndroidDevice_GetChanOrder( p_sys->fmt.i_physical_channels, p_chans_out,
                                 AOUT_CHAN_MAX );
     p_sys->i_chans_to_reorder =


=====================================
modules/audio_output/android/device.h
=====================================
@@ -37,7 +37,6 @@ AndroidDevice_GetChanOrder( uint16_t i_physical_channels, uint32_t p_chans_out[]
                             size_t chan_out_max )
 {
     assert( chan_out_max >= AOUT_CHAN_MAX);
-    memset( p_chans_out, 0, chan_out_max * sizeof(uint32_t) );
 
 #define HAS_CHAN( x ) ( ( i_physical_channels & (x) ) == (x) )
     /* samples will be in the following order: FL FR FC LFE BL BR BC SL SR */


=====================================
modules/audio_output/apple/coreaudio_common.c
=====================================
@@ -611,7 +611,7 @@ MapOutputLayout(audio_output_t *p_aout, audio_sample_format_t *fmt,
 
         msg_Dbg(p_aout, "output layout of AUHAL has %d channels",
                 (int) outlayout->mNumberChannelDescriptions);
-        uint32_t chans_out[AOUT_CHAN_MAX];
+        uint32_t chans_out[AOUT_CHAN_MAX + 1] = {0};
 
         /* For 7.1, AOUT_CHAN_MIDDLELEFT/RIGHT needs to be swapped with
          * AOUT_CHAN_REARLEFT/RIGHT. Auhal


=====================================
modules/codec/avcodec/encoder.c
=====================================
@@ -801,7 +801,7 @@ int InitVideoEnc( vlc_object_t *p_this )
         /* Create channel layout for avcodec
          * Copied from audio.c
          */
-        uint32_t pi_order_dst[AOUT_CHAN_MAX] = { 0 };
+        uint32_t pi_order_dst[AOUT_CHAN_MAX + 1] = { 0 };
         uint32_t order_mask = 0;
         int i_channels_src = 0;
 


=====================================
src/audio_output/common.c
=====================================
@@ -252,9 +252,9 @@ void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
  *****************************************************************************/
 unsigned aout_CheckChannelReorder( const uint32_t *chans_in,
                                    const uint32_t *chans_out,
-                                   uint32_t mask, uint8_t *restrict table )
+                                   uint32_t mask_in_out, uint8_t *restrict table )
 {
-    static_assert(AOUT_CHAN_MAX <= (sizeof (mask) * CHAR_BIT), "Missing bits");
+    static_assert(AOUT_CHAN_MAX <= (sizeof (mask_in_out) * CHAR_BIT), "Missing bits");
 
     unsigned channels = 0;
 
@@ -266,21 +266,26 @@ unsigned aout_CheckChannelReorder( const uint32_t *chans_in,
     for( unsigned i = 0; chans_in[i]; i++ )
     {
         const uint32_t chan = chans_in[i];
-        if( !(mask & chan) )
+        if( !(mask_in_out & chan) )
             continue;
 
-        unsigned index = 0;
+        uint8_t index_out = 0;
         for( unsigned j = 0; chan != chans_out[j]; j++ )
-            if( mask & chans_out[j] )
-                index++;
+        {
+            if (chans_out[j] == 0)
+                // end of output channels reached, the input channel is not found
+                return 0;
+            if( mask_in_out & chans_out[j] )
+                index_out++; // previously handled channels
+        }
 
-        table[channels++] = index;
+        table[channels++] = index_out;
     }
 
     for( unsigned i = 0; i < channels; i++ )
         if( table[i] != i )
             return channels;
-    return 0;
+    return 0; // no reordering needed, all input index match the output index
 }
 
 void aout_ChannelReorder( void *ptr, size_t bytes, uint8_t channels,



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/e0bc4a81fd17bdd4deadbee08dfe6df6034421b7

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/e0bc4a81fd17bdd4deadbee08dfe6df6034421b7
You're receiving this email because of your account on code.videolan.org. Manage all notifications: https://code.videolan.org/-/profile/notifications | Help: https://code.videolan.org/help




More information about the vlc-commits mailing list