[vlc-commits] [Git][videolan/vlc][master] 3 commits: avcodec: use p_enc audio channels instead of context channels in encoder

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Thu Jul 6 12:27:29 UTC 2023



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
29747a8a by Ilkka Ollakka at 2023-07-06T11:54:35+00:00
avcodec: use p_enc audio channels instead of context channels in encoder

Allows to have less conditions in code when adding new ch_layout use

- - - - -
c4302ca5 by Ilkka Ollakka at 2023-07-06T11:54:35+00:00
avcodec: add handling of new ch_layout in audio encoder

conditioned to avcodec version where is it added

- - - - -
b73dc884 by Ilkka Ollakka at 2023-07-06T11:54:35+00:00
avcodec: use ch_layout for channel layout in audio encoder

channels and channel_layout has been deprecated in FFMPEG 5.1 and will be removed eventually

also always create the mapping, as ch_layout is always there

- - - - -


1 changed file:

- modules/codec/avcodec/encoder.c


Changes:

=====================================
modules/codec/avcodec/encoder.c
=====================================
@@ -172,6 +172,7 @@ static const uint64_t pi_channels_map[][2] =
     { AV_CH_STEREO_RIGHT,      0 },
 };
 
+#if !LIBAVCODEC_VERSION_CHECK(59, 24, 100)
 static const uint32_t channel_mask[][2] = {
     {0,0},
     {AOUT_CHAN_CENTER, AV_CH_LAYOUT_MONO},
@@ -184,6 +185,7 @@ static const uint32_t channel_mask[][2] = {
     {AOUT_CHANS_7_1, AV_CH_LAYOUT_7POINT1},
     {AOUT_CHANS_8_1, AV_CH_LAYOUT_OCTAGONAL},
 };
+#endif
 
 static const char *const ppsz_enc_options[] = {
     "keyint", "bframes", "vt", "qmin", "qmax", "codec", "hq",
@@ -757,51 +759,39 @@ int InitVideoEnc( vlc_object_t *p_this )
         date_Init( &p_sys->buffer_date, p_enc->fmt_out.audio.i_rate, 1 );
         p_context->time_base.num = 1;
         p_context->time_base.den = p_context->sample_rate;
-        p_context->channels      = p_enc->fmt_out.audio.i_channels;
-        p_context->channel_layout = channel_mask[p_context->channels][1];
 
-        /* Setup Channel ordering for multichannel audio
+        /* Setup Channel ordering for audio
          * as VLC channel order isn't same as libavcodec expects
          */
 
         p_sys->i_channels_to_reorder = 0;
 
-        /* Specified order
+        /* Create channel layout for avcodec
          * Copied from audio.c
          */
-        const unsigned i_order_max = 8 * sizeof(p_context->channel_layout);
         uint32_t pi_order_dst[AOUT_CHAN_MAX] = { 0 };
         uint32_t order_mask = 0;
         int i_channels_src = 0;
 
-        if( p_context->channel_layout )
-        {
-            msg_Dbg( p_enc, "Creating channel order for reordering");
-            for( unsigned i = 0; i < sizeof(pi_channels_map)/sizeof(*pi_channels_map); i++ )
-            {
-                if( p_context->channel_layout & pi_channels_map[i][0] )
-                {
-                    msg_Dbg( p_enc, "%d %"PRIx64" mapped to %"PRIx64"", i_channels_src, pi_channels_map[i][0], pi_channels_map[i][1]);
-                    pi_order_dst[i_channels_src++] = pi_channels_map[i][1];
-                    order_mask |= pi_channels_map[i][1];
-                }
-            }
-        }
-        else
+        msg_Dbg( p_enc, "Creating channel order for reordering");
+#if LIBAVCODEC_VERSION_CHECK(59, 24, 100)
+        av_channel_layout_default( &p_context->ch_layout, p_enc->fmt_out.audio.i_channels );
+        uint64_t channel_mask = p_context->ch_layout.u.mask;
+#else
+        p_context->channels = p_enc->fmt_out.audio.i_channels;
+        p_context->channel_layout = channel_mask[p_context->channels][1];
+        uint64_t channel_mask = p_context->channel_layout;
+#endif
+        for( unsigned i = 0; i < sizeof(pi_channels_map)/sizeof(*pi_channels_map); i++ )
         {
-            msg_Dbg( p_enc, "Creating default channel order for reordering");
-            /* Create default order  */
-            for( unsigned int i = 0; i < __MIN( i_order_max, (unsigned)p_sys->p_context->channels ); i++ )
+            if( channel_mask & pi_channels_map[i][0] )
             {
-                if( i < sizeof(pi_channels_map)/sizeof(*pi_channels_map) )
-                {
-                    msg_Dbg( p_enc, "%d channel is %"PRIx64"", i_channels_src, pi_channels_map[i][1]);
-                    pi_order_dst[i_channels_src++] = pi_channels_map[i][1];
-                    order_mask |= pi_channels_map[i][1];
-                }
+                msg_Dbg( p_enc, "%d %"PRIx64" mapped to %"PRIx64"", i_channels_src, pi_channels_map[i][0], pi_channels_map[i][1]);
+                pi_order_dst[i_channels_src++] = pi_channels_map[i][1];
+                order_mask |= pi_channels_map[i][1];
             }
         }
-        if( i_channels_src != p_context->channels )
+        if( i_channels_src != p_enc->fmt_out.audio.i_channels )
             msg_Err( p_enc, "Channel layout not understood" );
 
         p_sys->i_channels_to_reorder =
@@ -907,7 +897,7 @@ int InitVideoEnc( vlc_object_t *p_this )
     if( ret )
     {
         if( p_enc->fmt_in.i_cat != AUDIO_ES ||
-                (p_context->channels <= 2 && i_codec_id != AV_CODEC_ID_MP2
+                (p_enc->fmt_out.audio.i_channels <= 2 && i_codec_id != AV_CODEC_ID_MP2
                  && i_codec_id != AV_CODEC_ID_MP3) )
 errmsg:
         {
@@ -932,10 +922,14 @@ errmsg:
             goto error;
         }
 
-        if( p_context->channels > 2 )
+        if( p_enc->fmt_out.audio.i_channels > 2 )
         {
+#if LIBAVCODEC_VERSION_CHECK(59, 24, 100)
+            av_channel_layout_default( &p_context->ch_layout, 2 );
+#else
             p_context->channels = 2;
             p_context->channel_layout = channel_mask[p_context->channels][1];
+#endif
 
             /* Change fmt_in in order to ask for a channels conversion */
             p_enc->fmt_in.audio.i_channels =
@@ -1038,7 +1032,7 @@ errmsg:
                                     p_context->frame_size :
                                     AV_INPUT_BUFFER_MIN_SIZE;
         p_sys->i_buffer_out = av_samples_get_buffer_size(NULL,
-                p_sys->p_context->channels, p_sys->i_frame_size,
+                p_enc->fmt_out.audio.i_channels, p_sys->i_frame_size,
                 p_sys->p_context->sample_fmt, DEFAULT_ALIGN);
         p_sys->p_buffer = av_malloc( p_sys->i_buffer_out );
         if ( unlikely( p_sys->p_buffer == NULL ) )
@@ -1290,13 +1284,17 @@ static block_t *handle_delay_buffer( encoder_t *p_enc, encoder_sys_t *p_sys, uns
 {
     block_t *p_block = NULL;
     //How much we need to copy from new packet
-    const size_t leftover = leftover_samples * p_sys->p_context->channels * p_sys->i_sample_bytes;
+    const size_t leftover = leftover_samples * p_enc->fmt_out.audio.i_channels * p_sys->i_sample_bytes;
 
     av_frame_unref( p_sys->frame );
     p_sys->frame->format     = p_sys->p_context->sample_fmt;
     p_sys->frame->nb_samples = leftover_samples + p_sys->i_samples_delay;
+#if LIBAVCODEC_VERSION_CHECK(59, 24, 100)
+    av_channel_layout_copy(&p_sys->frame->ch_layout, &p_sys->p_context->ch_layout);
+#else
     p_sys->frame->channel_layout = p_sys->p_context->channel_layout;
     p_sys->frame->channels = p_sys->p_context->channels;
+#endif
 
     if( likely( date_Get( &p_sys->buffer_date ) != VLC_TICK_INVALID) )
     {
@@ -1316,7 +1314,7 @@ static block_t *handle_delay_buffer( encoder_t *p_enc, encoder_sys_t *p_sys, uns
         // We need to deinterleave from p_aout_buf to p_buffer the leftover bytes
         if( p_sys->b_planar )
             aout_Deinterleave( p_sys->p_interleave_buf, p_sys->p_buffer,
-                p_sys->i_frame_size, p_sys->p_context->channels, p_enc->fmt_in.i_codec );
+                p_sys->i_frame_size, p_enc->fmt_out.audio.i_channels, p_enc->fmt_in.i_codec );
         else
             memcpy( p_sys->p_buffer + buffer_delay, p_aout_buf->p_buffer, leftover);
 
@@ -1333,7 +1331,7 @@ static block_t *handle_delay_buffer( encoder_t *p_enc, encoder_sys_t *p_sys, uns
         memset( p_sys->p_buffer + (leftover+buffer_delay), 0, padding_size );
         buffer_delay += padding_size;
     }
-    if( avcodec_fill_audio_frame( p_sys->frame, p_sys->p_context->channels,
+    if( avcodec_fill_audio_frame( p_sys->frame, p_enc->fmt_out.audio.i_channels,
             p_sys->p_context->sample_fmt, p_sys->b_planar ? p_sys->p_interleave_buf : p_sys->p_buffer,
             p_sys->i_buffer_out,
             DEFAULT_ALIGN) < 0 )
@@ -1363,7 +1361,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
 
     //i_bytes_left is amount of bytes we get
     i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
-    buffer_delay = p_sys->i_samples_delay * p_sys->i_sample_bytes * p_sys->p_context->channels;
+    buffer_delay = p_sys->i_samples_delay * p_sys->i_sample_bytes * p_enc->fmt_out.audio.i_channels;
 
     //p_sys->i_buffer_out = p_sys->i_frame_size * chan * p_sys->i_sample_bytes
     //Calculate how many bytes we would need from current buffer to fill frame
@@ -1425,8 +1423,12 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
         else
             p_sys->frame->nb_samples = p_sys->i_frame_size;
         p_sys->frame->format = p_sys->p_context->sample_fmt;
+#if LIBAVCODEC_VERSION_CHECK(59, 24, 100)
+        av_channel_layout_copy(&p_sys->frame->ch_layout, &p_sys->p_context->ch_layout);
+#else
         p_sys->frame->channel_layout = p_sys->p_context->channel_layout;
         p_sys->frame->channels = p_sys->p_context->channels;
+#endif
 
         if( likely(date_Get( &p_sys->buffer_date ) != VLC_TICK_INVALID) )
         {
@@ -1437,12 +1439,12 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
         else p_sys->frame->pts = AV_NOPTS_VALUE;
 
         const int in_bytes = p_sys->frame->nb_samples *
-            p_sys->p_context->channels * p_sys->i_sample_bytes;
+            p_enc->fmt_out.audio.i_channels* p_sys->i_sample_bytes;
 
         if( p_sys->b_planar )
         {
             aout_Deinterleave( p_sys->p_buffer, p_aout_buf->p_buffer,
-                               p_sys->frame->nb_samples, p_sys->p_context->channels, p_enc->fmt_in.i_codec );
+                               p_sys->frame->nb_samples, p_enc->fmt_out.audio.i_channels, p_enc->fmt_in.i_codec );
 
         }
         else
@@ -1450,7 +1452,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
             memcpy(p_sys->p_buffer, p_aout_buf->p_buffer, in_bytes);
         }
 
-        if( avcodec_fill_audio_frame( p_sys->frame, p_sys->p_context->channels,
+        if( avcodec_fill_audio_frame( p_sys->frame, p_enc->fmt_out.audio.i_channels,
                                     p_sys->p_context->sample_fmt,
                                     p_sys->p_buffer,
                                     p_sys->i_buffer_out,
@@ -1476,7 +1478,7 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
     if( p_aout_buf->i_nb_samples > 0 )
     {
        memcpy( p_sys->p_buffer + buffer_delay, p_aout_buf->p_buffer,
-               p_aout_buf->i_nb_samples * p_sys->i_sample_bytes * p_sys->p_context->channels);
+               p_aout_buf->i_nb_samples * p_sys->i_sample_bytes * p_enc->fmt_out.audio.i_channels);
        p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
     }
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/bf70d6aa232abcd22f66cc19b6954f001506eb77...b73dc8841d999c6be9de718cd2cd3aeb13279792

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/bf70d6aa232abcd22f66cc19b6954f001506eb77...b73dc8841d999c6be9de718cd2cd3aeb13279792
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list