FFmpeg programming study NOTE 2: audio re-sampling, ffmpeg Study Notes

Source: Internet
Author: User

FFmpeg programming study NOTE 2: audio re-sampling, ffmpeg Study Notes


The core function swr_convert of ffmpeg for Implementing Audio re-sampling is very powerful. However, the comment of ffmpeg is too simple and such problems often occur in applications, in fact, swr_convert is also applied after the first execution of swr_convert in the loop of reading data> resampling> encoding data to check whether there is any data, if there is a need to write it into the FIFO, it will be used again next time, this is particularly important in transcoding and high conversion from low sampling rate.

The following simple code is taken from my own test program.


const int frame_size = FFMIN(fifo_size, m_Opt->encode_pCodecCtx->frame_size);if ((ret = av_audio_fifo_read(m_fifo, (void **)m_fifo_samples_array, frame_size)) < frame_size) {fprintf(stderr, "Could not read data from FIFO\n");return AVERROR_EXIT;}int out_samples = av_rescale_rnd(swr_get_delay(m_Opt->out_resample_context, 48000) + 1536, 44100, 48000, AV_ROUND_UP);int conver_samples= swr_convert(m_Opt->out_resample_context, m_fifo_conver_samples_array, frame_size,(const uint8_t **)m_fifo_samples_array, frame_size);ret = av_audio_fifo_size(m_conver_fifo);if ((ret = av_audio_fifo_realloc(m_conver_fifo, av_audio_fifo_size(m_conver_fifo) + frame_size)) < 0) {fprintf(stderr, "Could not reallocate FIFO\n");return ret;}ret = av_audio_fifo_write(m_conver_fifo, (void **)m_fifo_conver_samples_array, conver_samples);fifo_size = av_audio_fifo_size(m_conver_fifo);if ((ret = av_audio_fifo_read(m_conver_fifo, (void **)m_Opt->out_samples_array, frame_size)) < frame_size) {fprintf(stderr, "Could not read data from FIFO\n");return AVERROR_EXIT;}ret = Encode_audio(ret);int ret1 = 0;while ((ret1 = swr_convert(m_Opt->out_resample_context, m_fifo_conver_samples_array, frame_size, NULL, 0)) > 0){if ((ret = av_audio_fifo_realloc(m_conver_fifo, av_audio_fifo_size(m_conver_fifo) + ret1)) < 0) {fprintf(stderr, "Could not reallocate FIFO\n");return ret;}ret = av_audio_fifo_write(m_conver_fifo, (void **)m_fifo_conver_samples_array, ret1);fifo_size = av_audio_fifo_size(m_conver_fifo);if (fifo_size > m_Opt->encode_pFrame->nb_samples){if ((ret = av_audio_fifo_read(m_conver_fifo, (void **)m_Opt->out_samples_array, frame_size)) < frame_size) {fprintf(stderr, "Could not read data from FIFO\n");return AVERROR_EXIT;}ret = Encode_audio(ret);}

Because it is a test code, there are some unnecessary steps in the middle, mainly for debugging.

Focus:

    if ((r = swr_convert(swr_ctx, output, output_nb_samples,(const uint8_t**)input, nb_samples)) < 0)        return -1;    while ((r = swr_convert(swr_ctx, output, output_nb_samples, NULL, 0)) > 0) {     }



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.