FFmpeg音頻解碼播放

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


音頻解碼流程.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è)與分析,做完這步操作一些媒體的基本信息都被填入上下文涡上≈憾希看看AVFormatContext 有哪些重要的信息

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ì)信息),通過(guò)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)過(guò)層層的解析媒體文件信息幾乎都可以取到梢杭。有些信息也可能沒(méi)有比如AVCodecParameters 中的width,height信息碼流中可能并不含蓋秸滴,要等具體解碼來(lái)覆蓋該值武契。

創(chuàng)建JNI層創(chuàng)建AudioTrack 上篇文章講到了子線程與主線程調(diào)用Java的區(qū)別,這里把播放放到了子線程荡含,JNIEnv包括對(duì)象都是線程私有的咒唆,主要是通過(guò)JavaVM來(lái)獲取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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市释液,隨后出現(xiàn)的幾起案子全释,更是在濱河造成了極大的恐慌,老刑警劉巖误债,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浸船,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡寝蹈,警方通過(guò)查閱死者的電腦和手機(jī)李命,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)箫老,“玉大人封字,你說(shuō)我怎么就攤上這事∷w蓿” “怎么了阔籽?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)牲蜀。 經(jīng)常有香客問(wèn)我笆制,道長(zhǎng),這世上最難降的妖魔是什么涣达? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任在辆,我火速辦了婚禮,結(jié)果婚禮上峭判,老公的妹妹穿的比我還像新娘开缎。我一直安慰自己,他們只是感情好林螃,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布奕删。 她就那樣靜靜地躺著,像睡著了一般疗认。 火紅的嫁衣襯著肌膚如雪完残。 梳的紋絲不亂的頭發(fā)上伏钠,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音谨设,去河邊找鬼熟掂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛扎拣,可吹牛的內(nèi)容都是我干的赴肚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼二蓝,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼誉券!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起刊愚,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤踊跟,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后鸥诽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體商玫,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年牡借,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拳昌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蓖捶,死狀恐怖地回,靈堂內(nèi)的尸體忽然破棺而出扁远,到底是詐尸還是另有隱情俊鱼,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布畅买,位于F島的核電站并闲,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏谷羞。R本人自食惡果不足惜帝火,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望湃缎。 院中可真熱鬧犀填,春花似錦、人聲如沸嗓违。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蹂季。三九已至冕广,卻和暖如春疏日,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背撒汉。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工沟优, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人睬辐。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓挠阁,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親溯饵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鹃唯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容