FFmpeg音頻解碼播放

音頻的播放Android提供了像MediaPlayer,SoundPool,AudioTrack(需自己解碼音頻)等。這些都只是單純的播放一個(gè)聲音,支持的音頻文件格式也存在有限。比如我們想開發(fā)一款像QQ音樂這樣的音樂播放器,一款好的音樂器并不是簡(jiǎn)單的播放歌曲,里面會(huì)包含有很多設(shè)置,能夠?qū)σ粜У牟僮骶庉?。FFmpeg作為音視頻操作的庫,解碼出音頻文件PCM數(shù)據(jù),PCM作為音頻的原始數(shù)據(jù)我們可以對(duì)其進(jìn)行編輯等等,F(xiàn)Fmpeg可對(duì)音頻添加濾鏡。
不管是視頻圖像解碼還是音頻解碼FFmpeg都有標(biāo)準(zhǔn)的步驟。
看看FFmpeg音頻解碼的過程:


音頻解碼流程.png

下面是ffmpeg解碼音頻流程的代碼

//
// Created by Administrator on 2019/8/21.
//

#include "YBFFmpeg.h"
#include "android_log.h"
#include "PlayerContan.h"
#include <pthread.h>

YBFFmpeg::YBFFmpeg(PlayerJNICall *playerJNICall, const char *url) {
    pPlayerJNICall = playerJNICall;
    char *copyUrl = (char *) malloc(strlen(url) + 1);
    memcpy(copyUrl, url, strlen(url) + 1);
    this->url = url;
}

YBFFmpeg::~YBFFmpeg() {
    relese();
}


void *thread_play(void *arg) {
    YBFFmpeg *ybfFmpeg = (YBFFmpeg *) arg;
    int res;
    int audioStramIndex = -1;
    int index = 0;

    //注冊(cè)所有組件
    av_register_all();

    //打開文件
    res = avformat_open_input(&(ybfFmpeg->pFormatContext), ybfFmpeg->url, NULL, NULL);
    if (res != 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }

    //查找流信息
    res = avformat_find_stream_info(ybfFmpeg->pFormatContext, NULL);
    if (res < 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }

    //查找對(duì)應(yīng)流index 找的是AVMEDIA_TYPE_AUDIO(音頻流)
    audioStramIndex = av_find_best_stream(ybfFmpeg->pFormatContext, AVMEDIA_TYPE_AUDIO,
                                          -1, -1, NULL,
                                          0);
    if (audioStramIndex < 0) {
        LOGE("%s", "no find audio stream");
        ybfFmpeg->callPlayerJniError(PLAYER_FIND_AUDIO_STREAM_ERRO, "no find audio stream");
        return (void*)-1;
    }

    AVCodecParameters *pCodecParameters = ybfFmpeg->pFormatContext->streams[audioStramIndex]->codecpar;
    //查找解碼器
    AVCodec *pCodec = avcodec_find_decoder(
            pCodecParameters->codec_id);
    if (pCodec == NULL) {
        LOGE("%s", "no find Codec fail");
        ybfFmpeg->callPlayerJniError(PLAYER_FIND_CODEC_ERRO, "PLAYER_FIND_CODEC_ERRO");
        return (void*)-1;
    }


    //開辟pCodecContext
    ybfFmpeg->pCodecContext = avcodec_alloc_context3(pCodec);
    if (ybfFmpeg->pCodecContext == NULL) {
        LOGE("%s", "avcodec alloc context fail");
        ybfFmpeg->callPlayerJniError(PLAYER_ALLOC_CODECCONTEXT_ERRO,
                                     "PLAYER_ALLOC_CODECCONTEXT_ERRO");
        return (void*)-1;
    }

    res = avcodec_parameters_to_context(ybfFmpeg->pCodecContext, pCodecParameters);
    if (res < 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }
    //打開解碼器
    res = avcodec_open2(ybfFmpeg->pCodecContext, pCodec, NULL);
    if (res < 0) {
        LOGE("%d,%s", res, av_err2str(res));
        ybfFmpeg->callPlayerJniError(res, av_err2str(res));
        return (void*)res;
    }

    int64_t out_ch_layout = AV_CH_LAYOUT_STEREO;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
    int out_sample_rate = 44100;
    int64_t in_ch_layout = pCodecParameters->channel_layout;
    enum AVSampleFormat in_sample_fmt = ybfFmpeg->pCodecContext->sample_fmt;
    int in_sample_rate = ybfFmpeg->pCodecContext->sample_rate;

    ybfFmpeg->pSwrContext = swr_alloc_set_opts(NULL, out_ch_layout, out_sample_fmt,
                                               out_sample_rate, in_ch_layout,
                                               in_sample_fmt, in_sample_rate, 0, NULL);


    int errocode = swr_init(ybfFmpeg->pSwrContext);

   
    // size 是播放指定的大小,是最終輸出的大小
    int outChannels = av_get_channel_layout_nb_channels(out_ch_layout);


    int dataSize = av_samples_get_buffer_size(NULL, outChannels,
                                              pCodecParameters->frame_size,
                                              out_sample_fmt, 0);

    int out_sample_fmt_track;
    if (out_sample_fmt == AV_SAMPLE_FMT_U8) {
        out_sample_fmt_track = 3;
    } else {
        out_sample_fmt_track = 2;
    }

    uint8_t *resampleOutBuffer = (uint8_t *) malloc(dataSize);

    JNIEnv* tehread_Env;
    ybfFmpeg->pPlayerJNICall->javaVM->AttachCurrentThread(&tehread_Env,NULL);
    ybfFmpeg->pPlayerJNICall->initCrateAudioTrack(tehread_Env,outChannels, out_sample_fmt_track);
    jbyteArray jPcmByteArray =tehread_Env->NewByteArray(dataSize);
    jbyte *jPcmData = tehread_Env->GetByteArrayElements(jPcmByteArray, NULL);

    AVPacket *pkt = av_packet_alloc();
    AVFrame *pFrame = av_frame_alloc();

    //開始解碼流
    while (av_read_frame(ybfFmpeg->pFormatContext, pkt) == 0) {

        if (audioStramIndex == pkt->stream_index) {
            //音頻流
            if (avcodec_send_packet(ybfFmpeg->pCodecContext, pkt) == 0) {

                if (avcodec_receive_frame(ybfFmpeg->pCodecContext, pFrame) == 0) {
                    //解碼數(shù)據(jù)
                    index++;
                    LOGE("解碼音頻%d幀", index);

                    swr_convert(ybfFmpeg->pSwrContext, &resampleOutBuffer, pFrame->nb_samples,
                                (const uint8_t **) (pFrame->data),
                                pFrame->nb_samples);


                    memcpy(jPcmData, resampleOutBuffer, dataSize);
                    // 0 把 c 的數(shù)組的數(shù)據(jù)同步到 jbyteArray , 然后釋放native數(shù)組
                    tehread_Env->ReleaseByteArrayElements(jPcmByteArray,
                                                                               jPcmData,
                                                                               JNI_COMMIT);
                    ybfFmpeg->pPlayerJNICall->callAudioTrackWrite(tehread_Env,jPcmByteArray, 0, dataSize);

                }

            }
        }

        av_packet_unref(pkt);
        av_frame_unref(pFrame);
    }

    av_packet_free(&pkt);
    av_frame_free(&pFrame);
    tehread_Env->DeleteLocalRef(jPcmByteArray);
    ybfFmpeg->pPlayerJNICall->javaVM->DetachCurrentThread()
    free(resampleOutBuffer);

}


void YBFFmpeg::play() {

    pthread_t play_thread;
    pthread_create(&play_thread, NULL, thread_play, this);
    pthread_join(play_thread, NULL);

}


void YBFFmpeg::callPlayerJniError(int code, char *msg) {
    relese();
    callPlayerJniError(code, msg);
}

void YBFFmpeg::relese() {
    if (pSwrContext != NULL) {
        swr_close(pSwrContext);
        swr_free(&pSwrContext);
        pSwrContext = NULL;
    }
    if (pCodecContext != NULL) {
        avcodec_free_context(&pCodecContext);
        pCodecContext = NULL;
    }

    if (pFormatContext != NULL) {
        avformat_close_input(&pFormatContext);
        avformat_free_context(pFormatContext);
        pFormatContext = NULL;
    }
}


上述代碼包含媒體文件的讀取(IO),解碼模塊,音頻渲染。簡(jiǎn)單的展示FFmpeg的解碼音頻的流程。制作播放器,要考慮東西比較多,比如網(wǎng)絡(luò)的抖動(dòng),解碼抖動(dòng),秒開等優(yōu)化。

avformat_open_input()主要是連接網(wǎng)絡(luò)或者本地資源以及碼流頭部信息的拉取,
avformat_find_stream_info()媒體信息的探測(cè)與分析,做完這步操作一些媒體的基本信息都被填入上下文??纯碅VFormatContext 有哪些重要的信息

typedef struct AVFormatContext {
  struct AVInputFormat *iformat;
  struct AVOutputFormat *oformat;
  unsigned int nb_streams;
  AVStream **streams;
  char filename[1024];
  int64_t start_time;
  int64_t duration;
  int64_t bit_rate;
  unsigned int packet_size;
  int max_delay;
  enum AVCodecID video_codec_id;
  AVDictionary *metadata;
  AVCodec *video_codec;
......
}AVFormatContext 

從AVFormatContext 的結(jié)構(gòu)體中可以看出包含的碼流數(shù)量,碼流,文件名,時(shí)長(zhǎng),解碼器等等,如果想要知道碼流的詳細(xì)數(shù)據(jù)(比如音頻流的采樣率,采樣點(diǎn)個(gè)數(shù),通道等詳細(xì)信息),通過av_find_best_stream()可以找到我們關(guān)注的碼流,AVStream結(jié)構(gòu)體中包含了對(duì)應(yīng)碼流的詳細(xì)信息

typedef struct AVStream {
  int index;    /**< stream index in AVFormatContext */
  AVCodecContext *codec;//解碼上下文
   int64_t start_time; 第一幀數(shù)據(jù)顯示時(shí)間
   int64_t nb_frames;  多少幀數(shù)據(jù)
   AVCodecParameters *codecpar;  //解碼參數(shù)
}AVStream 

AVCodecParameters 解碼的一些信息參數(shù)

typedef struct AVCodecParameters {
    enum AVCodecID   codec_id;
    int64_t bit_rate;
    int width;
    int height;
    uint64_t channel_layout;
    int      channels;
    int      sample_rate;
    int      block_align;
    int      frame_size;
}AVCodecParameters 

經(jīng)過層層的解析媒體文件信息幾乎都可以取到。有些信息也可能沒有比如AVCodecParameters 中的width,height信息碼流中可能并不含蓋,要等具體解碼來覆蓋該值。

創(chuàng)建JNI層創(chuàng)建AudioTrack 上篇文章講到了子線程與主線程調(diào)用Java的區(qū)別,這里把播放放到了子線程,JNIEnv包括對(duì)象都是線程私有的,主要是通過JavaVM來獲取JNIEnv,包括創(chuàng)建對(duì)象的全局引用

//
// Created by Administrator on 2019/8/21.
//

#include "PlayerJNICall.h"
#include "android_log.h"

PlayerJNICall::PlayerJNICall(JNIEnv *env, JavaVM *javaVM, jobject jPlayerObj) {
    this->javaVM = javaVM;
    this->JniEnv = env;
    this->jPlayerObj = JniEnv->NewGlobalRef(jPlayerObj);
    this->instance_clazz = (jclass) (JniEnv->NewGlobalRef(
            JniEnv->GetObjectClass(this->jPlayerObj)));


    jErroMid = JniEnv->GetMethodID(instance_clazz, "onErro", "(ILjava/lang/String;)V");
}

PlayerJNICall::~PlayerJNICall() {
    if (audioTrackInstance != NULL) {
        JniEnv->DeleteLocalRef(audioTrackInstance);
        audioTrackInstance = NULL;
    }
    if (jPlayerObj != NULL) {
        JniEnv->DeleteGlobalRef(jPlayerObj);
        jPlayerObj = NULL;
    }
    if (instance_clazz != NULL) {
        JniEnv->DeleteGlobalRef(instance_clazz);
        instance_clazz = NULL;

    }

}

/**
* 創(chuàng)建AudioTrack
* @param pEnv
*
*    public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
           int bufferSizeInBytes, int mode)
*/

//對(duì)象也是線程私有的

void PlayerJNICall::initCrateAudioTrack(JNIEnv *env, int channelConfig, int audioFormat) {
    jmethodID jm_id = env->GetMethodID(instance_clazz, "initAudioTrack",
                                       "(II)Landroid/media/AudioTrack;");


    audioTrackInstance = env->CallObjectMethod(jPlayerObj, jm_id, channelConfig,
                                               audioFormat);


    jclass audioTrackClazz = env->GetObjectClass(audioTrackInstance);


    jmethodID play_mID = env->GetMethodID(audioTrackClazz, "play", "()V");


    jWriteMid = env->GetMethodID(audioTrackClazz, "write", "([BII)I");

    env->CallVoidMethod(audioTrackInstance, play_mID);
}


void PlayerJNICall::callAudioTrackWrite(JNIEnv *env, jbyteArray audioData, int offsetInBytes,
                                        int sizeInBytes) {
    env->CallIntMethod(audioTrackInstance, jWriteMid, audioData, offsetInBytes, sizeInBytes);
}

void PlayerJNICall::callErro(int code, char *msg) {
    jstring jMsg = JniEnv->NewStringUTF(msg);
    JniEnv->CallVoidMethod(audioTrackInstance, jErroMid, code, jMsg);
    JniEnv->ReleaseStringUTFChars(jMsg, msg);
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容