ffmpeg新接口之體驗

       許久不使用ffmpeg了,最近一年一直是使用gstreamer在做媒體處理(因爲產品採用開源框架緣故),考慮gstreamter過於龐大,過於晦澀(依賴glib,各種插件,面向對象的C框架等),自研框架還是優先開用ffmpeg來處理。將3年前的ffmpeg解碼器例子用最新ffmpeg(4.2.3)來編譯,問題一大堆。

首先:相關宏定義的頭文件路徑變了(雖然通過頭文件引入相關調整解決了)。
其次:API接口變了。編譯被中止了。

                              

                                                                  ffmpeg老接口編譯中止

咋看,新版本ffmpeg很多接口已經降級不能使用了。主要變動如下:
1.    初始方式變了。
以前ffmpeg初始化相關av_regigser_all,av_register_xxx都廢棄了,新的ffmpeg版本不需要av_regigser這個方式註冊這個流程了。
2.    解碼編碼相關接口改了。
以前ffmpeg需要使用avcodec_encode_video2,avcodec_decode_video2,avcodec_decode_audio4, avcodec_encode_audio2等一系列相關avcodec_encode_xxxx, avcodec_decode_xxxx編解碼器接口全部廢棄,取而代之是avcodec_send_frame,avcodec_receive_packet ,avcodec_send_packe,avcodec_receive_frame 四個新接口統一了。
3.    其次結構體某些參數可能也改了。查看相關說明即可。
雖然新的ffmpeg的API和相關結構體參數發生了改變,好在ffmpeg註釋比較明白,老的工程移植過來總體難度不大。
下面以一個h264解碼生成YUV的例子來展示一下老工程在新的API框架下的使用

typedef struct Scale_Context
{
    struct SwsContext *Sctx;
    int *dstStride;
    uint8_t **DstData;
};
static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
    const char *filename, Scale_Context *pScx)
{
    char buf[1024];
    int ret;
    ret = avcodec_send_packet(dec_ctx, pkt);
    if (ret < 0) {
        fprintf(stderr, "Error sending a packet for decoding\n");
        exit(1);
    }
    int iGot_Frame = 0;
    while (ret >= 0) {
        //ret = avcodec_decode_video2(dec_ctx, frame, &iGot_Frame,pkt); 老的接口已經廢棄
        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr, "Error during decoding\n");
            exit(1);
        }

        if (pScx->Sctx != NULL)
        {
             sws_scale(pScx->Sctx, frame->data,
             frame->linesize, 0, 1080, pScx->DstData, pScx->dstStride);
        }
        //I420ToYUY2() libyuv
        printf("saving frame %3d\n", dec_ctx->frame_number);
        fflush(stdout);
        /* the picture is allocated by the decoder. no need to
        free it */
        int linesize = frame->linesize[0];
        if (pScx->Sctx == NULL)
        {
            _snprintf(buf, sizeof(buf), "%s-%d.yuv420", filename, dec_ctx->frame_number);
            yuv420_save(frame->data, frame->linesize,
                frame->width, frame->height, buf);
        }
        else
        {
            _snprintf(buf, sizeof(buf), "%s-%d.yuyv422", filename, dec_ctx->frame_number);
            yuyv422_save(pScx->DstData[0], pScx->dstStride[0], frame->width, frame->height, buf);
        }

    }
}

Decode 接口採用新的API解碼。avcodec_send_packet將AVpacket送入解碼器,avcodec_receive_frame從解碼器讀出解碼後的原始幀數據YUV。

int _tmain(int argc, _TCHAR* argv[])
{
    const char *filename, *outfilename;
    const AVCodec *codec;
    AVCodecParserContext *parser;
    AVCodecContext *c = NULL;
    FILE *f;
    AVFrame *frame;
    uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    uint8_t *data;
    size_t   data_size;
    int ret;
    AVPacket *pkt;
    Scale_Context SctxObj;
    struct SwsContext *sws_ctx = NULL;
    uint8_t *src_data[4], *dst_data[4];
    int src_linesize[4], dst_linesize[4];
    int type = 0;
    if (argc <= 2) {
        fprintf(stderr, "Usage: %s <input file> <output file> [type]\n", argv[0]);
        exit(0);
    }
    filename = argv[1];
    outfilename = argv[2];
    if (argc>3)
     type = atoi(argv[3]);
    pkt = av_packet_alloc();
    if (!pkt)
        exit(1);
   //av_register_all();新框架註冊接口已經廢棄
    /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
    memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);

    /* find the MPEG-1 video decoder */
    codec = avcodec_find_decoder(AV_CODEC_ID_H264);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }
    parser = av_parser_init(codec->id);
    if (!parser) {
        fprintf(stderr, "parser not found\n");
        exit(1);
    }
    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }
    /* For some codecs, such as msmpeg4 and mpeg4, width and height
    MUST be initialized there because this information is not
    available in the bitstream. */
    /* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }
    if (type > 0)
    {

        int src_w = 1920, src_h = 1080, dst_w = 1920, dst_h = 1080;
        /* create scaling context  1080P的YUV420轉換成YUYV422 */
        sws_ctx = sws_getContext(src_w, src_h, AV_PIX_FMT_YUV420P,
            dst_w, dst_h, AV_PIX_FMT_YUYV422,
            SWS_BILINEAR, NULL, NULL, NULL);
        if (!sws_ctx) {
            fprintf(stderr,
                "Impossible to create scale context for the conversion ");
            ret = AVERROR(EINVAL);
            exit(1);
        }

        /* buffer is going to be written to rawvideo file, no alignment */
        if ((ret = av_image_alloc(dst_data, dst_linesize,
            dst_w, dst_h, AV_PIX_FMT_YUYV422, 1)) < 0) {
            fprintf(stderr, "Could not allocate destination image\n");
            exit(1);
        }
    }
    SctxObj.Sctx = sws_ctx;
    SctxObj.dstStride = dst_linesize;
    SctxObj.DstData = dst_data;
    while (!feof(f)) {
        /* read raw data from the input file */
        data_size = fread(inbuf, 1, INBUF_SIZE, f);
        if (!data_size)
            break;

        /* use the parser to split the data into frames */
        data = inbuf;
        while (data_size > 0) {
            ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
                data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
            if (ret < 0) {
                fprintf(stderr, "Error while parsing\n");
                exit(1);
            }
            data += ret;
            data_size -= ret;

            if (pkt->size)
                decode(c, frame, pkt, outfilename, &SctxObj);
        }
    }
    /* flush the decoder */
    decode(c, frame, NULL, outfilename, &SctxObj);
    fclose(f);
    av_parser_close(parser);
    avcodec_free_context(&c);
    av_frame_free(&frame);
    av_packet_free(&pkt);
    getchar();
    return 0;
}

main函數傳入3個參數,第一個參數是輸入264文件名,第二個參數是輸出YUV的文件名,第3個參數指定YUV的格式。type大於0,輸入爲yuyv422,否則爲YUV420格式。在這裏我們演示了通過ffmpeg自帶的libswscale完成圖像數據的轉換。
libswscale:主要功能:
圖像格式互轉,圖像縮放,前後圖像濾波處理等。如:YUV和RGB各種格式互轉,YUV或RGB圖像的縮放,圖像各種濾波處理等。libswscale不足之處是轉換效率不高,如果對性能有更高追求,建議採用opencv或libyuv來完成圖像轉換,縮放。
編譯,運行該程序,生成的YUV420和YUYV422格式如下:

                                     

YUV420和YUYV422格式顯示如下:

                           

                                                                                   YUV420格式的預覽

                              

                                                                       YUV422的預覽

更多更詳細資源請關注公衆號:AV_Chat

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章