[PATCH] Fix af_lavcresample to use the new ch_layout scheme.

Ivan Kalvachev <[email protected]> Sun, 7 Apr 2024 15:14:09 +0300
Newsgroups gmane.comp.video.mplayer.devel
Message-ID <CABA=pqdSme61y2Zk5avBfkuPfkq3xdV-_GUmF1UtopJXzDphUw@mail.gmail.com>
Just like the rest of FFmpeg 7.x swresample library doesn't accept
"number of channels" as separate parameter anymore and requires it to
be part of channel layout structure that also describes the audio
speakers layout.

While Alexander Strasser's (beastd) patch series mostly fix this kind
of change in other parts of the code, here it remained unnoticed
because it doesn't cause compilation failure.

The existing af_lavcresample code uses av_opt_set*() and because of
that the errors are returned at runtime.

Since lavresample is automatically inserted to change the sample rate
(e.g. 44100->48000) it caused some videos to lose sound entirely.

To fix it, I'm just getting the default channel layout for the given
number of channels. It's the simplest code that makes it work.

This together with the change of ad_ffmpeg.c patch let's me play
everything so far.

Best Regards
    Ivan Kalvachev

_______________________________________________
MPlayer-dev-eng mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/mplayer-dev-eng
lavcresample_chlayout.patch (text/x-patch, 1.7 KB)
Index: af_lavcresample.c
===================================================================
--- af_lavcresample.c	(revision 38449)
+++ af_lavcresample.c	(working copy)
@@ -57,6 +57,7 @@
 // Initialization and runtime control
 static int control(struct af_instance_s* af, int cmd, void* arg)
 {
+  AVChannelLayout ch_layout;
   af_resample_t* s   = (af_resample_t*)af->setup;
   af_data_t *data= (af_data_t*)arg;
   int out_rate, test_output_res; // helpers for checking input format
@@ -73,6 +74,8 @@
     af->mul = (double)af->data->rate / data->rate;
     af->delay = af->data->nch * s->filter_length / FFMIN(af->mul, 1); // *bps*.5
 
+    av_channel_layout_default(&ch_layout, af->data->nch);
+
     if (s->ctx_out_rate != af->data->rate || s->ctx_in_rate != data->rate || s->ctx_filter_size != s->filter_length ||
         s->ctx_phase_shift != s->phase_shift || s->ctx_linear != s->linear || s->ctx_cutoff != s->cutoff) {
         swr_free(&s->swrctx);
@@ -85,8 +88,9 @@
         av_opt_set_double(s->swrctx, "cutoff", s->cutoff, 0);
         av_opt_set_sample_fmt(s->swrctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
         av_opt_set_sample_fmt(s->swrctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
-        av_opt_set_int(s->swrctx, "in_channel_count", af->data->nch, 0);
-        av_opt_set_int(s->swrctx, "out_channel_count", af->data->nch, 0);
+        if(av_opt_set_chlayout(s->swrctx, "in_chlayout",  &ch_layout, 0) < 0) return AF_ERROR;
+        if(av_opt_set_chlayout(s->swrctx, "out_chlayout", &ch_layout, 0) < 0) return AF_ERROR;
+
         if(swr_init(s->swrctx) < 0) return AF_ERROR;
         s->ctx_out_rate    = af->data->rate;
         s->ctx_in_rate     = data->rate;