音視頻開發(fā)之旅(34) - 基于FFmpeg實(shí)現(xiàn)簡單的視頻解碼器

目錄

  1. FFmpeg解碼過程流程圖和關(guān)鍵的數(shù)據(jù)結(jié)構(gòu)
  2. mp4通過FFmpeg解碼YUV裸視頻數(shù)據(jù)
  3. 遇到的問題
  4. 資料
  5. 收獲

一、FFmpeg解碼過程流程圖和關(guān)鍵的數(shù)據(jù)結(jié)構(gòu)

FFmpeg解碼涉及的知識點(diǎn)比較多,很容易被函數(shù)和結(jié)構(gòu)體搞定不知所錯,我們先從整體上對解碼流程有個認(rèn)知,畫了張圖把解碼流程圖,如下


1.1 解碼流程如下

  1. avformat_open_input 打開媒體文件
  2. avformat_find_stream_info 初始化AVFormatContext_
  3. 匹配到視頻流的index
  4. avcodec_find_decoder 根據(jù)視頻流信息的codec_id找到對應(yīng)的解碼器_
  5. avcodec_open2 使用給定的AVCodec初始化AVCodecContext_
  6. 初始化輸出文件、解碼AVPacket和AVFrame結(jié)構(gòu)體
  7. av_read_frame 開始一幀一幀讀取
  8. avcodec_send_packet
  9. avcodec_receive_frame
  10. 格式轉(zhuǎn)換 、分別寫入YUV文件
  11. Opengl渲染(本篇不涉及,放到后面單獨(dú)篇學(xué)習(xí)實(shí)踐)
  12. 釋放資源

1.2 關(guān)鍵函數(shù)

下面我們來看下解碼流程中的關(guān)鍵函數(shù)

1. av_register_all
在3.x或者以前的版本在使用ffmpeg的復(fù)用/解復(fù)用器或者編解碼器之前一定要先調(diào)用該函數(shù)。但是4.x之后ffmpeg修了了內(nèi)部實(shí)現(xiàn),該函數(shù)可以省略不寫。

2. avformat_open_input

attribute_deprecated int av_open_input_file(AVFormatContext **ic_ptr,constchar *filename,

                       AVInputFormat *fmt,

                       int buf_size,

                       AVFormatParameters *ap);

以輸入方式打開一個媒體文件,codecs并沒有打開,只讀取了文件的頭信息.

3. avformat_find_stream_info

Read packets of a media file to get stream information

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

獲取多媒體信息

4. avcodec_find_decoder

Find a registered decoder with a matching codec ID

AVCodec *avcodec_find_decoder(enum AVCodecID id);

根據(jù)codecID找到一個注冊過的解碼器

5. avcodec_open2

Initialize the AVCodecContext to use the given AVCodec

int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);

使用給定的AVCodec初始化AVCodecContext_

6. av_read_frame

Return the next frame of a stream.
@return 0 if OK, < 0 on error or end of file

int av_read_frame(AVFormatContext *s, AVPacket *pkt);

讀取一幀數(shù)據(jù),讀到的是AVPacket

7. avcodec_send_packet

Supply raw packet data as input to a decoder.
@return 0 on success, otherwise negative error code

int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);

給解碼器發(fā)送一幀壓縮的AVPacket 數(shù)據(jù)

8. avcodec_receive_frame

Return decoded output data from a decoder.
@return 0 on success

int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);

接收解碼器解碼的一幀AVFrame數(shù)據(jù)

9. sws_scale

int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],
              const int srcStride[], int srcSliceY, int srcSliceH,
              uint8_t *const dst[], const int dstStride[]);

解碼后YUV格式的視頻像素數(shù)據(jù)保存在AVFrame的data 0-3中。但這些像素并不是連續(xù)存儲的,每行有效像素之后存儲了一些無效像素,經(jīng)過該函數(shù)處理,去掉無效數(shù)據(jù)。否則會出現(xiàn)花屏。

10. 資源釋放相關(guān)函數(shù)

    av_packet_unref(packet);

    sws_freeContext(img_convert_ctx);

    fclose(pYUVFile);

    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecContext);
    avformat_close_input(&avFormatContext);

1.3 關(guān)鍵結(jié)構(gòu)體

關(guān)鍵結(jié)構(gòu)體包括AVFormatContext、AVStream、AVCodecContext、AVCodec、AVCodecParameters、AVPacket、AVFrame等
下面4張圖片來自雷神


AVFormatCotext和AVInputFormat是和封裝格式相關(guān)的結(jié)構(gòu)體

AVStream、AVCodecContex、AVCodec是和編解碼相關(guān)的結(jié)構(gòu)體

AVPacket 與 AVFrame

1.4 補(bǔ)充知識

1. 宏定義define里面的##

宏定義define里面的##可能不太常見,它的含義就是拼接兩個字符串,比如
#define Conn(x,y) x##y

那么 int  n = Conn(123,456);  結(jié)果就是n=123456;

2. 文件的打開方式

File * fp = fopen(info.txt,"wb+")

fprintf()
或者fwirte

fclose(fp);

關(guān)于打開方式的說明如下

.  r+ 以可讀寫方式打開文件,該文件必須存在。

  rb+ 讀寫打開一個二進(jìn)制文件,只允許讀寫數(shù)據(jù)。

  rt+ 讀寫打開一個文本文件,允許讀和寫。

  w 打開只寫文件,若文件存在則文件長度清0,若文件不存在則建立該文件。

  w+ 打開可讀寫文件,若文件存在則文件長度清為零,若文件不存在則建立該文件。

  a 以附加的方式打開只寫文件

  a+ 以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾后

  wb 只寫打開或新建一個二進(jìn)制文件;只允許寫數(shù)據(jù)。

  wb+ 讀寫打開或建立一個二進(jìn)制文件,允許讀和寫。

  wt+ 讀寫打開或著建立一個文本文件;允許讀寫。

  at+ 讀寫打開一個文本文件,允許讀或在文本末追加數(shù)據(jù)。

  ab+ 讀寫打開一個二進(jìn)制文件,允許讀或在文件末追加數(shù)據(jù)。

3. YUV數(shù)據(jù)類型

輸出解碼前的h264碼流、輸出解碼后的YUV信息
使用Elecard StreamEye Tools查看輸出的h264數(shù)據(jù)

視頻顯示的流程,就是將像素數(shù)據(jù)“畫”在屏幕上的過程。
例如顯示YUV,就是將YUV“畫”在系統(tǒng)的窗口中。

YUV 4:4:4采樣,每一個Y對應(yīng)一組UV分量。
YUV 4:2:2采樣,每兩個Y共用一組UV分量。 
YUV 4:2:0采樣,每四個Y共用一組UV分量。


 YUV420P,Y,U,V三個分量都是平面格式,分為I420和YV12。I420格式和YV12格式的不同處在U平面和V平面的位置不同。在I420格式中,U平面緊跟在Y平面之后,然后才是V平面(即:YUV);但YV12則是相反(即:YVU)。
YUV420SP, Y分量平面格式,UV打包格式, 即NV12。 NV12與NV21類似,U 和 V 交錯排列,不同在于UV順序。

I420: YYYYYYYY UU VV    =>YUV420P (最常見的)
YV12: YYYYYYYY VV UU    =>YUV420P
NV12: YYYYYYYY UVUV     =>YUV420SP
NV21: YYYYYYYY VUVU     =>YUV420SP

二、mp4通過FFmpeg解碼成YUV裸數(shù)據(jù)

通過上一小節(jié),我們了解了FFmpeg解碼流程和關(guān)鍵的結(jié)構(gòu)體,這一小節(jié)我們來實(shí)踐。

具體步驟說明和代碼實(shí)現(xiàn)如下:

#include <jni.h>
#include <string>


extern "C" {
#include "include/libavcodec/avcodec.h"
#include "include/libavformat/avformat.h"
#include "include/log.h"
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>

}

extern "C"
JNIEXPORT jint JNICALL
Java_android_spport_mylibrary2_Demo_decodeVideo(JNIEnv *env, jobject thiz, jstring inputPath,
                                                jstring outPath) {
    //申請avFormatContext空間,記得要釋放
    AVFormatContext *avFormatContext = avformat_alloc_context();


    const char *url = env->GetStringUTFChars(inputPath, 0);
    //1. 打開媒體文件
    int reuslt = avformat_open_input(&avFormatContext, url, NULL, NULL);
    if (reuslt != 0) {
        LOGE("open input error url=%s, result=%d", url, reuslt);
        return -1;
    }
    //2.讀取媒體文件信息,給avFormatContext賦值
    if (avformat_find_stream_info(avFormatContext, NULL) < 0) {
        LOGE("find stream error");
        return -1;
    }

    //3. 匹配到視頻流的index
    int videoIndex = -1;
    for (int i = 0; i < avFormatContext->nb_streams; i++) {
        AVMediaType codecType = avFormatContext->streams[i]->codecpar->codec_type;
        LOGI("avcodec type %d", codecType);
        if (AVMEDIA_TYPE_VIDEO == codecType) {
            videoIndex = i;
            break;
        }
    }
    if (videoIndex == -1) {
        LOGE("not find a video stream");
        return -1;
    }

    AVCodecParameters *pCodecParameters = avFormatContext->streams[videoIndex]->codecpar;

    //4. 根據(jù)視頻流信息的codec_id找到對應(yīng)的解碼器
    AVCodec *pCodec = avcodec_find_decoder(pCodecParameters->codec_id);

    if (pCodec == NULL) {
        LOGE("Couldn`t find Codec");
        return -1;
    }

    AVCodecContext *pCodecContext = avFormatContext->streams[videoIndex]->codec;

    //5.使用給定的AVCodec初始化AVCodecContext
    int openResult = avcodec_open2(pCodecContext, pCodec, NULL);
    if (openResult < 0) {
        LOGE("avcodec open2 result %d", openResult);
        return -1;
    }

    const char *outPathStr = env->GetStringUTFChars(outPath, NULL);

    //6. 初始化輸出文件、解碼AVPacket和AVFrame結(jié)構(gòu)體

    //新建一個二進(jìn)制文件,已存在的文件將內(nèi)容清空,允許讀寫
    FILE *pYUVFile = fopen(outPathStr, "wb+");
    if (pYUVFile == NULL) {
        LOGE(" fopen outPut file error");
        return -1;
    }


    auto *packet = (AVPacket *) av_malloc(sizeof(AVPacket));

    //avcodec_receive_frame時作為參數(shù),獲取到frame,獲取到的frame有些可能是錯誤的要過濾掉,否則相應(yīng)幀可能出現(xiàn)綠屏
    AVFrame *pFrame = av_frame_alloc();
    //作為yuv輸出的frame承載者,會進(jìn)行縮放和過濾出錯的幀,YUV相應(yīng)的數(shù)據(jù)也是從該對象中讀取
    AVFrame *pFrameYUV = av_frame_alloc();

    //out_buffer中數(shù)據(jù)用于渲染的,且格式為YUV420P
    uint8_t *out_buffer = (unsigned char *) av_malloc(
            av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecContext->width,
                                     pCodecContext->height, 1));

    av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,
                         AV_PIX_FMT_YUV420P, pCodecContext->width, pCodecContext->height, 1);

    // 由于解碼出來的幀格式不一定是YUV420P的,在渲染之前需要進(jìn)行格式轉(zhuǎn)換
    struct SwsContext *img_convert_ctx = sws_getContext(pCodecContext->width, pCodecContext->height,
                                                        pCodecContext->pix_fmt,
                                                        pCodecContext->width, pCodecContext->height,
                                                        AV_PIX_FMT_YUV420P,
                                                        SWS_BICUBIC, NULL, NULL, NULL);


    int readPackCount = -1;
    int frame_cnt = 0;
    clock_t startTime = clock();

    //7. 開始一幀一幀讀取
    while ((readPackCount = av_read_frame(avFormatContext, packet) >= 0)) {
        LOGI(" read fame count is %d", readPackCount);

        if (packet->stream_index == videoIndex) {
            //8. send AVPacket
            int sendPacket = avcodec_send_packet(pCodecContext, packet);
            //return 0 on success, otherwise negative error code:
            if (sendPacket != 0) {
                LOGE("avodec send packet error %d", sendPacket);
                continue;
            }
            //9. receive frame
            // 0:  success, a frame was returned
            int receiveFrame = avcodec_receive_frame(pCodecContext, pFrame);

            if (receiveFrame != 0) {
                //如果接收到的fame不等于0,忽略這次receiver否則會出現(xiàn)綠屏幀
                LOGE("avcodec_receive_frame error %d", receiveFrame);
                continue;
            }
            //10. 格式轉(zhuǎn)換
            sws_scale(img_convert_ctx, (const uint8_t *const *) pFrame->data, pFrame->linesize,
                      0, pCodecContext->height,
                      pFrameYUV->data, pFrameYUV->linesize);

            //11. 分別寫入YUV數(shù)據(jù)
            int y_size = pCodecParameters->width * pCodecParameters->height;
            //YUV420p
            fwrite(pFrameYUV->data[0], 1, y_size, pYUVFile);//Y
            fwrite(pFrameYUV->data[1], 1, y_size / 4, pYUVFile);//U
            fwrite(pFrameYUV->data[2], 1, y_size / 4, pYUVFile);//V

            //輸出I、P、B幀信息
            char pictypeStr[10] = {0};
            switch (pFrame->pict_type) {
                case AV_PICTURE_TYPE_I: {
                    sprintf(pictypeStr, "I");
                    break;
                }
                case AV_PICTURE_TYPE_P: {
                    sprintf(pictypeStr, "P");
                    break;
                }
                case AV_PICTURE_TYPE_B: {
                    sprintf(pictypeStr, "B");
                    break;
                }
            }
            LOGI("Frame index %5d. Tpye %s", frame_cnt, pictypeStr);
            frame_cnt++;
        }
    }

    LOGI("frame count is %d", frame_cnt);
    clock_t endTime = clock();

    //long類型用%ld輸出
    LOGI("decode video use Time %ld", (endTime - startTime));


    //12.釋放相關(guān)資源

    //釋放packet
    av_packet_unref(packet);

    sws_freeContext(img_convert_ctx);

    fclose(pYUVFile);

    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecContext);
    avformat_close_input(&avFormatContext);
    return 0;

}
}

解碼后的數(shù)據(jù)使用ffplayer進(jìn)行播放。注意參數(shù)設(shè)置, 比如格式和分辨率等

eg:
ffplay /Users/yabin/Desktop/tmp/ffmpeg/output8.yuv -pix_fmt yuv420p -s 784x480

代碼已上傳至 github
https://github.com/ayyb1988/ffmpegvideodecodedemo

三、遇到的問題

  1. avformat_open_input -13

原因: 沒有讀寫權(quán)限導(dǎo)致,-13是權(quán)限相關(guān)的錯誤,

在AndroidManifest.xml中加入以下權(quán)限
然后在代碼上添加動態(tài)權(quán)限檢查
  1. 生成的yuv導(dǎo)出來后用ffplay或者yuvplayer播放 出現(xiàn)花屏
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 784x480, 338 kb/s, 25 fps, 25 tbr, 16k tbn, 50 tbc (default)

ffplay /Users/yabin/Desktop/tmp/ffmpeg/output.yuv -pix_fmt yuv420p -s 784x480

原因:沒有對YUV數(shù)據(jù)設(shè)置格式和分辨率等信息

    uint8_t *out_buffer = (unsigned char *) av_malloc(
            av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecContext->width,
                                     pCodecContext->height, 1));

    av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,
                         AV_PIX_FMT_YUV420P, pCodecContext->width, pCodecContext->height, 1);


    struct SwsContext *img_convert_ctx = sws_getContext(pCodecContext->width, pCodecContext->height,
                                                        pCodecContext->pix_fmt,
                                                        pCodecContext->width, pCodecContext->height,
                                                        AV_PIX_FMT_YUV420P,
                                                        SWS_BICUBIC, NULL, NULL, NULL);

...

    sws_scale(img_convert_ctx, (const uint8_t *const *) pFrame->data, pFrame->linesize,
                      0, pCodecContext->height,
                      pFrameYUV->data, pFrameYUV->linesize);
  1. 第一幀出現(xiàn)綠屏
    原因: 如果接收到的fame不等于0,要忽略這次receiver否則會出現(xiàn)綠屏幀
 int sendPacket = avcodec_send_packet(pCodecContext, packet);
            //return 0 on success, otherwise negative error code:
            if (sendPacket != 0) {
                LOGE("avodec send packet error %d", sendPacket);
                continue;
            }
            // 0:  success, a frame was returned
            int receiveFrame = avcodec_receive_frame(pCodecContext, pFrame);

            if (receiveFrame != 0) {
                //如果接收到的fame不等于0,忽略這次receiver否則會出現(xiàn)綠屏幀
                LOGE("avcodec_receive_frame error %d", receiveFrame);
                continue;
            }

四、 資料

  1. 《音視頻開發(fā)進(jìn)階》
  2. FFMPEG中最關(guān)鍵的結(jié)構(gòu)體之間的關(guān)系
  3. ffmpeg函數(shù)介紹
  4. 100行代碼實(shí)現(xiàn)最簡單的基于FFMPEG+SDL的視頻播放器(SDL1.x)
  5. 最簡單的基于FFmpeg的移動端例子:Android 視頻解碼器-單個庫版
  6. 圖文詳解YUV420數(shù)據(jù)格式
  7. ffmpeg flv轉(zhuǎn)MP4 一點(diǎn)心得
  8. FFmpeg編解碼處理1-轉(zhuǎn)碼全流程簡介
  9. FFmpeg源代碼簡單分析:常見結(jié)構(gòu)體的初始化和銷毀(AVFormatContext,AVFrame等)

測試視頻來自:FFmpeg編解碼處理1-轉(zhuǎn)碼全流程簡介
下載測試文件(右鍵另存為):tnmil2.flv

五、收獲

  1. 了解ffmpeg解碼流程
  2. 了解ffmpeg關(guān)鍵的結(jié)構(gòu)以及之間的關(guān)系
  3. 解碼mp4為視頻裸數(shù)據(jù)YUV
  4. 花屏、錄屏問題分析解決

與Mediacodec解碼對比、YUV渲染播放等內(nèi)容,我們后續(xù)章節(jié)來學(xué)習(xí)實(shí)踐。

感謝你的閱讀

下一篇我們學(xué)習(xí)實(shí)踐使用FFmpeg解碼音頻,歡迎關(guān)注公眾號“音視頻開發(fā)之旅”,一起學(xué)習(xí)成長。

歡迎交流

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

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

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