[Libav-user] Resampling = noise

Strahinja Radman dr.strashni at gmail.com
Mon Aug 30 18:18:09 EEST 2021


On Fri, Aug 27, 2021 at 7:19 PM Baumgarten, Julien <
julien.baumgarten at viadialog.com> wrote:

> Thx Bob for your advice.
> I'll try to re-sample then encode.
>
>
> [image: avatar] [image: viadialog]
> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_logo>
> Julien BAUMGARTEN
>
> Chef de Projet Développement
>
> 01 77 45 30 94
> <0177453094>
>
> julien.baumgarten at viadialog.com
>
> www.viadialog.com
>
> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_link>
>
> 152 Boulevard Pereire, 75017 Paris
> [image: facebook] <https://www.facebook.com/viadialog>
> [image: twitter] <https://twitter.com/viadialog>
> [image: linkedin] <https://www.linkedin.com/company/viatelecom>
>
> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_banner>
> This email message (including its attachments) is confidential and may
> contain privileged information and is intended solely for the use of the
> individual and/or entity to whom it is addressed. If you are not the
> intended recipient of this e-mail you may not share, distribute or copy
> this e-mail (including its attachments), or any part thereof. If this
> e-mail is received in error, please notify the sender immediately by return
> e-mail and make sure that this e-mail (including its attachments), and all
> copies thereof, are immediately deleted from your system. Please further
> note that when you communicate with us via email or visit our website we
> process your personal data. See our privacy policy for more information
> about how we process it: https://www.viadialog.com/mentions-legales
>
>
> Le ven. 27 août 2021 à 17:31, Bob Kirnum <bkirnum at gmail.com> a écrit :
>
>> My understanding is that re-sampling is done on linear PCM, not encoded
>> audio (G.711 PCM).  I always encode after re-sampling.
>>
>> On Fri, Aug 27, 2021 at 6:38 AM Baumgarten, Julien <
>> julien.baumgarten at viadialog.com> wrote:
>>
>>> I don't succeed in encode with ffmpeg library.
>>> So I do the encoding before the resampling.
>>>
>>> [image: avatar] [image: viadialog]
>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_logo>
>>> Julien BAUMGARTEN
>>>
>>> Chef de Projet Développement
>>>
>>> 01 77 45 30 94
>>> <0177453094>
>>>
>>> julien.baumgarten at viadialog.com
>>>
>>> www.viadialog.com
>>>
>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_link>
>>>
>>> 152 Boulevard Pereire, 75017 Paris
>>> [image: facebook] <https://www.facebook.com/viadialog>
>>> [image: twitter] <https://twitter.com/viadialog>
>>> [image: linkedin] <https://www.linkedin.com/company/viatelecom>
>>>
>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_banner>
>>> This email message (including its attachments) is confidential and may
>>> contain privileged information and is intended solely for the use of the
>>> individual and/or entity to whom it is addressed. If you are not the
>>> intended recipient of this e-mail you may not share, distribute or copy
>>> this e-mail (including its attachments), or any part thereof. If this
>>> e-mail is received in error, please notify the sender immediately by return
>>> e-mail and make sure that this e-mail (including its attachments), and all
>>> copies thereof, are immediately deleted from your system. Please further
>>> note that when you communicate with us via email or visit our website we
>>> process your personal data. See our privacy policy for more information
>>> about how we process it: https://www.viadialog.com/mentions-legales
>>>
>>>
>>> Le ven. 27 août 2021 à 12:55, Bob Kirnum <bkirnum at gmail.com> a écrit :
>>>
>>>> In addition to re-sampling from 16 kHz to 8 kHz, are you then encoding
>>>> the resulting 8 kHz linear PCM (16 bit?) to G.711 (Alaw?)?
>>>>
>>>> On Fri, Aug 27, 2021 at 2:08 AM Baumgarten, Julien <
>>>> julien.baumgarten at viadialog.com> wrote:
>>>>
>>>>> Hi Polochon,
>>>>>
>>>>> Thx for your answer. I know I'll lose on audio quality by resampling
>>>>> 16kHZ to 8kHZ but I need to play the audio on VOIP calls which requires
>>>>> G711 a-law 8k HZ samples :(
>>>>> If I work with your command line, the sound is faaaaaaaaaaar much
>>>>> better. No noise at all
>>>>>
>>>>>
>>>>> [image: avatar] [image: viadialog]
>>>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_logo>
>>>>> Julien BAUMGARTEN
>>>>>
>>>>> Chef de Projet Développement
>>>>>
>>>>> 01 77 45 30 94
>>>>> <0177453094>
>>>>>
>>>>> julien.baumgarten at viadialog.com
>>>>>
>>>>> www.viadialog.com
>>>>>
>>>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_link>
>>>>>
>>>>> 152 Boulevard Pereire, 75017 Paris
>>>>> [image: facebook] <https://www.facebook.com/viadialog>
>>>>> [image: twitter] <https://twitter.com/viadialog>
>>>>> [image: linkedin] <https://www.linkedin.com/company/viatelecom>
>>>>>
>>>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_banner>
>>>>> This email message (including its attachments) is confidential and may
>>>>> contain privileged information and is intended solely for the use of the
>>>>> individual and/or entity to whom it is addressed. If you are not the
>>>>> intended recipient of this e-mail you may not share, distribute or copy
>>>>> this e-mail (including its attachments), or any part thereof. If this
>>>>> e-mail is received in error, please notify the sender immediately by return
>>>>> e-mail and make sure that this e-mail (including its attachments), and all
>>>>> copies thereof, are immediately deleted from your system. Please further
>>>>> note that when you communicate with us via email or visit our website we
>>>>> process your personal data. See our privacy policy for more information
>>>>> about how we process it: https://www.viadialog.com/mentions-legales
>>>>>
>>>>>
>>>>> Le ven. 27 août 2021 à 00:00, Polochon Street <polochonstreet at gmx.fr>
>>>>> a écrit :
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I'm by no means an expert, but just a remark - 8kHz is somewhat low
>>>>>> quality, so maybe that's why the audio sounds awful?
>>>>>>
>>>>>> Does it sound better when you try resampling it manually via
>>>>>> something like `ffmpeg -i input.wav -ar 8000 output.wav`?
>>>>>>
>>>>>> Best,
>>>>>> Paul
>>>>>>  Le 26/08/2021 à 20:55, Baumgarten, Julien a écrit :
>>>>>>
>>>>>> Hi guys,
>>>>>>
>>>>>> I made a previous post in order to get some help in converting +
>>>>>> resampling 16bit PCM (16k HZ) samples to A-law PCM (8k HZ) samples.
>>>>>> I succeeded in converting with another library than ffmpeg but it
>>>>>> works.
>>>>>> I am focusing now on the resampling.
>>>>>>
>>>>>> I tried the following source code:
>>>>>>
>>>>>> int64_t src_ch_layout = AV_CH_LAYOUT_MONO, dst_ch_layout = AV_CH_LAYOUT_MONO;               int src_rate = 16000, dst_rate = 8000;               uint8_t **src_data = NULL, **dst_data = NULL;               int src_nb_channels = 0, dst_nb_channels = 0;               int src_linesize = 0, dst_linesize = 0;               int src_nb_samples = this->_nbSamplesReceived, dst_nb_samples;               enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_U8, dst_sample_fmt = AV_SAMPLE_FMT_U8;               const char *dst_filename = "/tmp/resample.raw";               FILE *dst_file;               int dst_bufsize;               const char *fmt;               struct SwrContext *swr_ctx;               int ret;               dst_file = fopen(dst_filename, "wb");               if (!dst_file) {
>>>>>>                   fprintf(stderr, "Could not open destination file %s\n", dst_filename);                  exit(1);               }
>>>>>>
>>>>>>                swr_ctx = swr_alloc();               if (!swr_ctx) {
>>>>>>                   fprintf(stderr, "Could not allocate resampler context\n");                  ret = AVERROR(ENOMEM);//                goto end;               }
>>>>>>
>>>>>>                /* set in options */               av_opt_set_int(swr_ctx, "in_channel_layout",    src_ch_layout, 0);               av_opt_set_int(swr_ctx, "in_sample_rate",       src_rate, 0);               av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);               /* set out options */               av_opt_set_int(swr_ctx, "out_channel_layout",    dst_ch_layout, 0);               av_opt_set_int(swr_ctx, "out_sample_rate",       dst_rate, 0);               av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);               /* initialize the resampling context */               if ((ret = swr_init(swr_ctx)) < 0) {
>>>>>>                   fprintf(stderr, "Failed to initialize the resampling context\n");//                goto end;               }
>>>>>>
>>>>>>                /* Define nb channels */               src_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);               dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout);               // Define ouput nb samples               dst_nb_samples = av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);               ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels, src_nb_samples, src_sample_fmt, 0);               if (ret < 0) {
>>>>>>                   fprintf(stderr, "Could not allocate source samples\n");//                goto end;               }
>>>>>>                ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels, dst_nb_samples, dst_sample_fmt, 0);               if (ret < 0) {
>>>>>>                   fprintf(stderr, "Could not allocate destination samples\n");//                goto end;               }
>>>>>>
>>>>>>                // Fill source samples buffer with A-law samples               unsigned int i = 0;               std::for_each(this->_test1.begin(), this->_test1.end(), [this, &src_data, &i](const uint8_t &data) {
>>>>>>                   src_data[0][i++] = data;               });               /* convert to destination format */               ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);               if (ret < 0) {
>>>>>>                   fprintf(stderr, "Error while converting\n");                  // TODO: handle error               }
>>>>>>                dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels, ret, dst_sample_fmt, 1);               if (dst_bufsize < 0) {
>>>>>>                   fprintf(stderr, "Could not get sample buffer size\n");                  // TODO: handle error               }
>>>>>>                // Write resampled data into file               fwrite(dst_data[0], 1, dst_bufsize, dst_file);               if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0) {
>>>>>>                   fprintf(stderr, "Resampling failed.\n");                  // TODO: handle error               }
>>>>>>                // Close out file               fclose(dst_file);               // Release memory               if (src_data) av_freep(&src_data[0]);               av_freep(&src_data);               if (dst_data) av_freep(&dst_data[0]);               av_freep(&dst_data);               swr_free(&swr_ctx);
>>>>>>
>>>>>> When dst_rate is equal to src_rate, the output is OK without any
>>>>>> noise.
>>>>>> However, when dst_rate is lower than src_rate, the audio is
>>>>>> awful with too much noise.
>>>>>>
>>>>>> Did I miss something or am I doing something wrong?
>>>>>>
>>>>>> Yours sincerely,
>>>>>> Julien BAUMGARTEN
>>>>>>
>>>>>>
>>>>>> [image: avatar] [image: viadialog]
>>>>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_logo>
>>>>>>
>>>>>>
>>>>>> Julien BAUMGARTEN
>>>>>>
>>>>>> Chef de Projet Développement
>>>>>>
>>>>>> 01 77 45 30 94
>>>>>> <0177453094>
>>>>>>
>>>>>> julien.baumgarten at viadialog.com
>>>>>>
>>>>>> www.viadialog.com
>>>>>>
>>>>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_link>
>>>>>>
>>>>>> 152 Boulevard Pereire, 75017 Paris
>>>>>> [image: facebook] <https://www.facebook.com/viadialog>
>>>>>> [image: twitter] <https://twitter.com/viadialog>
>>>>>> [image: linkedin] <https://www.linkedin.com/company/viatelecom>
>>>>>>
>>>>>>
>>>>>> <https://www.viadialog.com/?utm_source=signature&utm_medium=email&utm_campaign=email_signature_banner>
>>>>>>
>>>>>> This email message (including its attachments) is confidential and
>>>>>> may contain privileged information and is intended solely for the use of
>>>>>> the individual and/or entity to whom it is addressed. If you are not the
>>>>>> intended recipient of this e-mail you may not share, distribute or copy
>>>>>> this e-mail (including its attachments), or any part thereof. If this
>>>>>> e-mail is received in error, please notify the sender immediately by return
>>>>>> e-mail and make sure that this e-mail (including its attachments), and all
>>>>>> copies thereof, are immediately deleted from your system. Please further
>>>>>> note that when you communicate with us via email or visit our website we
>>>>>> process your personal data. See our privacy policy for more information
>>>>>> about how we process it: https://www.viadialog.com/mentions-legales
>>>>>>
>>>>>> _______________________________________________
>>>>>> Libav-user mailing listLibav-user at ffmpeg.orghttps://ffmpeg.org/mailman/listinfo/libav-user
>>>>>>
>>>>>> To unsubscribe, visit link above, or emaillibav-user-request at ffmpeg.org with subject "unsubscribe".
>>>>>>
>>>>>> _______________________________________________
>>>>>> Libav-user mailing list
>>>>>> Libav-user at ffmpeg.org
>>>>>> https://ffmpeg.org/mailman/listinfo/libav-user
>>>>>>
>>>>>> To unsubscribe, visit link above, or email
>>>>>> libav-user-request at ffmpeg.org with subject "unsubscribe".
>>>>>>
>>>>> _______________________________________________
>>>>> Libav-user mailing list
>>>>> Libav-user at ffmpeg.org
>>>>> https://ffmpeg.org/mailman/listinfo/libav-user
>>>>>
>>>>> To unsubscribe, visit link above, or email
>>>>> libav-user-request at ffmpeg.org with subject "unsubscribe".
>>>>>
>>>> _______________________________________________
>>>> Libav-user mailing list
>>>> Libav-user at ffmpeg.org
>>>> https://ffmpeg.org/mailman/listinfo/libav-user
>>>>
>>>> To unsubscribe, visit link above, or email
>>>> libav-user-request at ffmpeg.org with subject "unsubscribe".
>>>>
>>> _______________________________________________
>>> Libav-user mailing list
>>> Libav-user at ffmpeg.org
>>> https://ffmpeg.org/mailman/listinfo/libav-user
>>>
>>> To unsubscribe, visit link above, or email
>>> libav-user-request at ffmpeg.org with subject "unsubscribe".
>>>
>> _______________________________________________
>> Libav-user mailing list
>> Libav-user at ffmpeg.org
>> https://ffmpeg.org/mailman/listinfo/libav-user
>>
>> To unsubscribe, visit link above, or email
>> libav-user-request at ffmpeg.org with subject "unsubscribe".
>>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/libav-user
>
> To unsubscribe, visit link above, or email
> libav-user-request at ffmpeg.org with subject "unsubscribe".
>


Hello,

Also make sure that the number of samples set while opening the encoder
matches the actual number of samples that you are feeding the encoder and
that number of samples matches what you set in the stream's container. For
me when these numbers were off I always had problems with the sound.

-- 

Regards
Strahinja Radman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20210830/e715266b/attachment.htm>


More information about the Libav-user mailing list