在Visual Studio 2010[VC++]中使用ffmpeg類庫

原創地址:http://www.cnblogs.com/Jerry-Chou/archive/2011/03/31/2000761.html


在Visual Studio 2010[VC++]中使用ffmpeg類庫

1,準備工作

很多播放器都使用了ffmpeg這個類庫來編解碼,使用沒有關係,但總是有些人不守規則。在恥辱榜上我看到了騰訊(QQPlayer),還有另一家深圳的公司。

image

我對GPL協議也不太瞭解,issue tracker中顯示QQPlayer需要提供完整項目代碼。我的疑問是:

如果是QQPlayer。其中集成了QQ的一些登陸模塊,但這些代碼不方便公開。但Player相關的代碼已經公開。這樣違反GPL嗎?

NOTE:下文中DLL或LIB(大寫指文件即avcodec.dll,avcodec.lib.etc.),dll或lib(小寫,指目錄)。

繼上篇在MinGW中編譯ffmpeg之後,我們便可以得到一些LIB和DLL,我們可以使用這些LIB和DLL來使用ffmpeg的相關功能函數。

image

其中頭文件在include目錄下,LIB及DLL在bin目錄下。其實這些LIB並不是傳統的靜態庫文件(真正的靜態庫文件是在lib目錄下的*.a文件),他們是dll的導出文件。

 

另外,C99中添加了幾個新的頭文件,VC++中沒有,所以需要你自己下載。並放至相應目錄。對於VS2010來說通常是:C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include。

 

2,示例代碼

網上的ffmpeg的示例代碼大多過時了,在2009年初img_convert這個函數被sws_scale取代了,所以可能你從網上找到的示例代碼並不可以運行(但代碼運作原理還是一樣的)。

我這裏貼出一份當前可以運行的代碼。

// ffmpeg-example.cpp : Defines the entry point for the console application.
//
 
 
#include "stdafx.h"
 
#define inline _inline
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
    /*Include ffmpeg header file*/
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif
 
#include <stdio.h>
 
 
static void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame);
 
int main (int argc, const char * argv[])
{
    AVFormatContext *pFormatCtx;
    int             i, videoStream;
    AVCodecContext  *pCodecCtx;
    AVCodec         *pCodec;
    AVFrame         *pFrame; 
    AVFrame         *pFrameRGB;
    AVPacket        packet;
    int             frameFinished;
    int             numBytes;
    uint8_t         *buffer;
 
    // Register all formats and codecs
    av_register_all();
 
    // Open video file
    if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
        return -1; // Couldn't open file
 
    // Retrieve stream information
    if(av_find_stream_info(pFormatCtx)<0)
        return -1; // Couldn't find stream information
 
    // Dump information about file onto standard error
    dump_format(pFormatCtx, 0, argv[1], false);
 
    // Find the first video stream
    videoStream=-1;
    for(i=0; i<pFormatCtx->nb_streams; i++)
        if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
        {
            videoStream=i;
            break;
        }
        if(videoStream==-1)
            return -1; // Didn't find a video stream
 
        // Get a pointer to the codec context for the video stream
        pCodecCtx=pFormatCtx->streams[videoStream]->codec;
 
        // Find the decoder for the video stream
        pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
        if(pCodec==NULL)
            return -1; // Codec not found
 
        // Open codec
        if(avcodec_open(pCodecCtx, pCodec)<0)
            return -1; // Could not open codec
 
        // Hack to correct wrong frame rates that seem to be generated by some codecs
        if(pCodecCtx->time_base.num>1000 && pCodecCtx->time_base.den==1)
            pCodecCtx->time_base.den=1000;
 
        // Allocate video frame
        pFrame=avcodec_alloc_frame();
 
        // Allocate an AVFrame structure
        pFrameRGB=avcodec_alloc_frame();
        if(pFrameRGB==NULL)
            return -1;
 
        // Determine required buffer size and allocate buffer
        numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
            pCodecCtx->height);
 
        //buffer=malloc(numBytes);
        buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
 
        // Assign appropriate parts of buffer to image planes in pFrameRGB
        avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
            pCodecCtx->width, pCodecCtx->height);
 
        // Read frames and save first five frames to disk
        i=0;
        while(av_read_frame(pFormatCtx, &packet)>=0)
        {
            // Is this a packet from the video stream?
            if(packet.stream_index==videoStream)
            {
                // Decode video frame
                avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, 
                    packet.data, packet.size);
 
                // Did we get a video frame?
                if(frameFinished)
                {
                    static struct SwsContext *img_convert_ctx;
 
#if 0
                    // Older removed code
                    // Convert the image from its native format to RGB swscale
                    img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, 
                        (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, 
                        pCodecCtx->height);
 
                    // function template, for reference
                    int sws_scale(struct SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
                        int srcSliceH, uint8_t* dst[], int dstStride[]);
#endif
                    // Convert the image into YUV format that SDL uses
                    if(img_convert_ctx == NULL) {
                        int w = pCodecCtx->width;
                        int h = pCodecCtx->height;
 
                        img_convert_ctx = sws_getContext(w, h, 
                            pCodecCtx->pix_fmt, 
                            w, h, PIX_FMT_RGB24, SWS_BICUBIC,
                            NULL, NULL, NULL);
                        if(img_convert_ctx == NULL) {
                            fprintf(stderr, "Cannot initialize the conversion context!\n");
                            exit(1);
                        }
                    }
                    int ret = sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, 
                        pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
#if 0 
                    // this use to be true, as of 1/2009, but apparently it is no longer true in 3/2009
                    if(ret) {
                        fprintf(stderr, "SWS_Scale failed [%d]!\n", ret);
                        exit(-1);
                    }
#endif
                    // Save the frame to disk
                    if(i++<=5)
                        SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);
                }
            }
 
            // Free the packet that was allocated by av_read_frame
            av_free_packet(&packet);
        }
 
        // Free the RGB image
        //free(buffer);
        av_free(buffer);
        av_free(pFrameRGB);
 
        // Free the YUV frame
        av_free(pFrame);
 
        // Close the codec
        avcodec_close(pCodecCtx);
 
        // Close the video file
        av_close_input_file(pFormatCtx);
 
        return 0;
}
 
static void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
    FILE *pFile;
    char szFilename[32];
    int  y;
 
    // Open file
    sprintf(szFilename, "frame%d.ppm", iFrame);
    pFile=fopen(szFilename, "wb");
    if(pFile==NULL)
        return;
 
    // Write header
    fprintf(pFile, "P6\n%d %d\n255\n", width, height);
 
    // Write pixel data
    for(y=0; y<height; y++)
        fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
 
    // Close file
    fclose(pFile);
}

顯示代碼中有幾個地方需要注意一下。就是開頭的宏定義部分。第一個是C99中添加了inline關鍵字,第二個是對ffmpeg頭文件中INT64_C的模擬(可能也是爲了解決與C99的兼容問題)。第三個是使用extern C在C++代碼中使用C的頭文件。

 

3,設置Visual Studio

在你編譯,調用上面的代碼之前你還要在Visual Stuido中做相應的設置,纔可以正確引用ffmpeg的庫。

3.1 設置ffmpeg頭文件位置

右擊項目->屬性,添加Include文件目錄位置:

image

 

3.2 設置LIB文件位置

image

 

3.3 設置所引用的LIB文件

image

 

如果一切正常,這時你便可以編譯成功。

 

4,可能出現的問題

4.1 運行時出錯

雖然你可以成功編譯,但你F5,調試時會出現以下錯誤。

image

原因是,你雖然引用了LIB文件,但這並不是真正的靜態庫文件,而是對DLL的引用,所以當你調用ffmpeg庫函數時,需要DLL文件在場。你可以用dumpbin(VS自帶工具)來查看你生成的exe中引用了哪些DLL文件。你在命令行輸入:

>dumpbin ffmpeg-example.exe /imports

你可以從輸出中看出你實際引用以下幾個的DLL文件。

avcodec-52.dll 
avformat-52.dll 
swscale-0.dll 
avutil-50.dll 

 

還有些朋友可能想將ffmpeg庫進行靜態引用,這樣就不需要這些DLL文件了。這樣做是可行的,但是不推薦的。

 

4.2 av_open_input_file失敗

在VS的Command Argumetns中使用全路徑。

image


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