FFmpeg(11)-基於FFmpeg進行音頻重采樣(swr_init(), swr_convert())


一.包含頭文件和庫文件

修改CMakeLists

# swresample
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${FF}/libswresample.so)

更新target_link_libraries

target_link_libraries( # Specifies the target library.
                       native-lib
                       avcodec
                       avformat
                       avutil
                       swscale
                       swresample

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

在代碼中包含頭文件 #include <libswresample/swresample.h> 

二.相關函數說明

a)

SwrContext *swr_alloc(void);  // 分配重采樣的上下文。

SwrContext *swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, AVSampleFormat out_sample_fmt, int out_sample_rate

, int64_t in_ch_layout, AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx

);

參數1:重采樣上下文

參數2:輸出的layout, 如:5.1聲道…

參數3:輸出的樣本格式。Float, S16, S24

參數4:輸出的樣本率。可以不變。

參數5:輸入的layout。

參數6:輸入的樣本格式。

參數7:輸入的樣本率。

參數8,參數9,日志,不用管,可直接傳0

針對音頻的播放速度,可以通過樣本率的改變而改變。

 

int swr_init(struct SwrContext *s);                       // 初始化上下文

void swr_free(struct SwrContext **s);                  // 釋放上下文空間。

 

b) 

swr_convert()

針對每一幀音頻的處理。把一幀幀的音頻作相應的重采樣

int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count);

參數1:音頻重采樣的上下文

參數2:輸出的指針。傳遞的輸出的數組

參數3:輸出的樣本數量,不是字節數。單通道的樣本數量。

參數4:輸入的數組,AVFrame解碼出來的DATA

參數5:輸入的單通道的樣本數量。

三.示例代碼

// 初始化像素格式轉換上下文
    SwsContext *vctx = NULL;
    int outWidth = 1280;
    int outHeight = 720;
    char *rgb = new char[1920*1080*4];
    char *pcm = new char[48000*4*2];

    // 初始化音頻重采樣上下文
    SwrContext *actx = swr_alloc();
    actx = swr_alloc_set_opts(
            actx,
            av_get_default_channel_layout(2),
            AV_SAMPLE_FMT_S16,
            ac->sample_rate,
            av_get_default_channel_layout(ac->channels),
            ac->sample_fmt,
            ac->sample_rate,
            0, 0
    );

    ret = swr_init(actx);
    if (ret != 0) {
        LOGE("swr_init failed");
    } else {
        LOGI("swr_init success");
    }

    for (;;) {

        if (getNowMs() - start >= 3000) {
            LOGI("now decoder fps is: %d", frameCount / 3);
            start = getNowMs();
            frameCount = 0;
        }
        int ret = av_read_frame(ic, packet);
        if (ret != 0) {
            LOGE("讀取到結尾處");
            int pos = 20 * r2d(ic->streams[videoStream]->time_base);
            // 改變播放進度
            av_seek_frame(ic, videoStream, pos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
            continue;
        }

//        LOGI("Read a Packet. streamIndex=%d, size=%d, pts=%lld, flag=%d",
//             packet->stream_index,
//             packet->size,
//             packet->pts,
//             packet->flags
//        );

        AVCodecContext *cc = vc;
        if (packet->stream_index == audioStream) cc = ac;
        // 發送到線程中去解碼(將packet寫入到解碼隊列當中去)
        ret = avcodec_send_packet(cc, packet);
        // 清理
        int p = packet->pts;
        av_packet_unref(packet);
        if (ret != 0) {
            // LOGE("avcodec_send_packet failed.");
            continue;
        }

        for(;;) {
            // 從已解碼成功的數據當中取出一個frame, 要注意send一次,receive不一定是一次
            ret = avcodec_receive_frame(cc, frame);
            if (ret != 0) {
                break;
            }
            if (cc == vc) {
                frameCount++;
                vctx = sws_getCachedContext(
                        vctx,
                        frame->width,
                        frame->height,
                        (AVPixelFormat)frame->format,
                        outWidth,
                        outHeight,
                        AV_PIX_FMT_RGBA,
                        SWS_FAST_BILINEAR,
                        0, 0, 0
                );
                if (!vctx) {
                    LOGE("sws_getCachedContext failed!");
                } else {
                    // 開始像素格式轉換
                    uint8_t  *data[AV_NUM_DATA_POINTERS] = {0};
                    data[0] = (uint8_t *)rgb;
                    int lines[AV_NUM_DATA_POINTERS] = {0};
                    lines[0] = outWidth * 4;
                    int h = sws_scale(
                            vctx,
                            (const uint8_t **)frame->data,
                            frame->linesize,
                            0,
                            frame->height,
                            data, lines
                    );
                    LOGI("sws_scale = %d", h);
                }
            } else { // 音頻部分
                uint8_t *out[2] = {0};
                out[0] = (uint8_t *)pcm;
                // 音頻重采樣
                int len = swr_convert(
                        actx,
                        out,
                        frame->nb_samples,
                        (const uint8_t **)frame->data,
                        frame->nb_samples
                );
                LOGI("swr_convert = %d", len);
            }
            // LOGI("Receive a frame.........");
        }
    }

    delete rgb;
    delete pcm;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM