[Libav-user] Resampling = noise

Baumgarten, Julien julien.baumgarten at viadialog.com
Thu Aug 26 21:55:39 EEST 2021


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20210826/20153345/attachment.htm>


More information about the Libav-user mailing list