之前在https://blog.csdn.net/fengbingchun/article/details/90114411 介紹過如何在Windows7/10上通過MinGW方式編譯FFmpeg 4.1.3源碼生成庫的步驟,那時只能生成最終的庫,卻不能產生vs工程,無法進行單步調試。GitHub上有個項目ShiftMediaProject/FFmpeg提供了vs工程,下面對編譯過程進行說明:
1. 從https://github.com/ShiftMediaProject/FFmpeg clone到E:\GitCode\FFmpeg_VS2013\ShiftMediaProject目錄下,最新commit id爲3371d97d5951e882b3a57c323b3e1f21cfabe900,提交日期爲2019.12.14;
2. 按照SMP/readme.txt的說明,要求vs版本需爲2013及以上,初始打算使用vs2013,不過提示需要下載最新的Service Pack,因此改爲使用vs2017;
3. 下載依賴項,執行FFmpeg/SMP/project_get_dependencies.bat,執行結果如下,可能需要等待較長時間,而且可能需要執行多次,直到所有的依賴項全部clone下來,個別項目(如我多次嘗試下載gnulib始終失敗)可能會需要自己手動clone:git clone https://gitlab.com/libidn/gnulib-mirror.git ,然後將其拷貝到ShiftMediaProject/gnutls/gnulib目錄下;
4. 按照SMP/readme.txt中的說明,下載額外依賴文件,手動創建新目錄E:\GitCode\FFmpeg_VS2013\msvc,下載完後的內容如下:
(1). 從https://www.khronos.org/registry/OpenGL/index_gl.php 下載glext.h和wglext.h,將其保存到新創建的目錄msvc/include/gl下;
(2). 從https://www.khronos.org/registry/OpenGL/index_gl.php 下載khrplatform.h,將其保存到新創建的目錄msvc/include/KHR下;
(3). 從https://github.com/FFmpeg/nv-codec-headers 下載include/ffnvcodec,將ffnvcodec整個目錄拷貝到目錄msvc/include下;
(4). 從https://github.com/GPUOpen-LibrariesAndSDKs/AMF 下載amf/public/include,將include下的內容保存到新創建的目錄msvc/include/AMF下;
5. 有些項目有彙編代碼需要ASM編譯,如libavutil需要NASM,依賴項libvpx需要YASM:
(1). 從https://github.com/ShiftMediaProject/VSNASM 下載VSNASM,以管理員身份運行install_script.bat,可多執行幾次,如下圖所示:vs2017安裝成功,vs2013安裝失敗,因爲使用vs2017編譯先不管vs2013;如果install_script.bat不能成功安裝,可參照VSNASM下的README.md進行安裝:
(2). 從https://github.com/ShiftMediaProject/VSYASM 下載VSYASM,以管理員身份運行install_script.bat,可多執行幾次,如下圖所示 :vs2017安裝成功,vs2013安裝失敗,因爲使用vs2017編譯先不管vs2013;如果install_script.bat不能成功安裝,可參照VSYASM下的README.md進行安裝:
6. 使用vs2017打開FFmpeg/SMP/ffmpeg_deps.sln,發現有些項目不可用,如libass,如下圖所示,點擊安裝並關閉ffmpeg_deps.sln:
7. 再次打開ffmpeg_deps.sln,47個項目均加載成功,如下圖所示:
8. 可選擇編譯靜態庫/動態庫,Debug/Release,x86/x64,這裏選擇x64 靜態庫 Debug,選中解決方案”ffmpeg_deps”,點擊重新生成解決方案,第一次可能會產生一些error,可再次點擊生成解決方案,執行結果如下圖所示:
(1). 在E:\GitCode\FFmpeg_VS2013\msvc\bin\x64目錄下會存放可執行文件,包括:ffmpegd.exe、ffplayd.exe、ffprobed.exe;
(2). 在E:\GitCode\FFmpeg_VS2013\msvc\lib\x64目錄下會存放所有的靜態庫文件包括ffmpeg自身的和依賴項的;
(3). 在E:\GitCode\FFmpeg_VS2013\msvc\include目錄下會存放所需要的頭文件;
(4). 驗證ffmpegd.exe的正確性:獲取視頻設備名,執行如下命令,執行結果如下圖所示:顯示ffmpeg版本爲4.2,檢測到的視頻設備名爲” Integrated Webcam”
ffmpegd.exe -list_devices true -f dshow -i dummy
(5). 驗證ffplayed.exe的正確性:播放一個編碼格式爲rawvideo,像素格式爲bgr24,幀大小爲1280*720的文件,執行如下命令,執行結果如下圖所示:可正常播放
ffplayd.exe -i bgr.bin -f rawvideo -pixel_format bgr -video_size 1280x720
(6). 驗證ffprobed.exe的正確性:查看1.mp4視頻文件信息,執行如下命令,執行結果如下圖所示:可正常顯示視頻文件信息
ffprobed.exe 1.mp4
9. 新建一個test項目,測試代碼test.cpp如下,執行結果如下:可正常打開視頻設備並顯示
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <memory>
#include <fstream>
#include <thread>
#ifdef __cplusplus
extern "C" {
#endif
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/mem.h>
#include <libavutil/imgutils.h>
#ifdef __cplusplus
}
#endif
#include <opencv2/opencv.hpp>
int main()
{
avdevice_register_all();
AVCodecID id = AV_CODEC_ID_MJPEG;
AVCodec* encoder_id = avcodec_find_encoder(id);
AVCodec* decoder_id = avcodec_find_decoder(id);
if (!encoder_id || !decoder_id) {
fprintf(stderr, "codec not found: %d\n", id);
return -1;
}
AVFormatContext* format_context = avformat_alloc_context();
format_context->video_codec_id = id; // 指定編解碼格式
AVInputFormat* input_format = av_find_input_format("dshow");
AVDictionary* dict = nullptr;
//if (av_dict_set(&dict, "vcodec"/*"input_format"*/, "mjpeg", 0) < 0) fprintf(stderr, "fail to av_dict_set: line: %d\n", __LINE__); // 通過av_dict_set設置編解碼格式好像不起作用
if (av_dict_set(&dict, "video_size", "960x540", 0) < 0) fprintf(stderr, "fail to av_dict_set: line: %d\n", __LINE__);
//if (av_dict_set(&dict, "r", "25", 0) < 0) fprintf(stderr, "fail to av_dict_set: line: %d\n", __LINE__); // 通過av_dict_set設置幀率好像不起作用
int ret = avformat_open_input(&format_context, "video=Integrated Webcam", input_format, &dict);
if (ret != 0) {
fprintf(stderr, "fail to avformat_open_input: %d\n", ret);
return -1;
}
ret = avformat_find_stream_info(format_context, nullptr);
if (ret < 0) {
fprintf(stderr, "fail to get stream information: %d\n", ret);
return -1;
}
int video_stream_index = -1;
for (unsigned int i = 0; i < format_context->nb_streams; ++i) {
const AVStream* stream = format_context->streams[i];
if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
fprintf(stdout, "type of the encoded data: %d, dimensions of the video frame in pixels: width: %d, height: %d, pixel format: %d\n",
stream->codecpar->codec_id, stream->codecpar->width, stream->codecpar->height, stream->codecpar->format);
}
}
if (video_stream_index == -1) {
fprintf(stderr, "no video stream\n");
return -1;
}
fprintf(stdout, "frame rate: %f\n", av_q2d(format_context->streams[video_stream_index]->r_frame_rate));
AVCodecParameters* codecpar = format_context->streams[video_stream_index]->codecpar;
const AVCodec* codec = avcodec_find_decoder(codecpar->codec_id);
if (!codec) {
fprintf(stderr, "fail to avcodec_find_decoder\n");
return -1;
}
if (codecpar->codec_id != id) {
fprintf(stderr, "this test code only support mjpeg encode: %d\n", codecpar->codec_id);
return -1;
}
AVCodecContext* codec_context = avcodec_alloc_context3(codec);
if (!codec_context) {
fprintf(stderr, "fail to avcodec_alloc_context3\n");
return -1;
}
codec_context->pix_fmt = AVPixelFormat(codecpar->format);
codec_context->height = codecpar->height;
codec_context->width = codecpar->width;
codec_context->thread_count = 16;
ret = avcodec_open2(codec_context, codec, nullptr);
if (ret != 0) {
fprintf(stderr, "fail to avcodec_open2: %d\n", ret);
return -1;
}
AVPixelFormat dst_pixel_format = AV_PIX_FMT_BGR24;
AVFrame* frame = av_frame_alloc();
AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));
SwsContext* sws_context = sws_getContext(codec_context->width, codec_context->height, codec_context->pix_fmt, codec_context->width, codec_context->height, dst_pixel_format, 0, nullptr, nullptr, nullptr);
if (!frame || !packet || !sws_context) {
fprintf(stderr, "fail to alloc\n");
return -1;
}
uint8_t *bgr_data[4];
int bgr_linesize[4];
av_image_alloc(bgr_data, bgr_linesize, codec_context->width, codec_context->height, dst_pixel_format, 1);
cv::Mat mat(codec_context->height, codec_context->width, CV_8UC3);
const char* winname = "dshow mjpeg video";
cv::namedWindow(winname);
while (1) {
ret = av_read_frame(format_context, packet);
if (ret >= 0 && packet->stream_index == video_stream_index && packet->size > 0) {
ret = avcodec_send_packet(codec_context, packet);
if (ret < 0) {
fprintf(stderr, "##### fail to avcodec_send_packet: %d\n", ret);
av_packet_unref(packet);
continue;
}
ret = avcodec_receive_frame(codec_context, frame);
if (ret < 0) {
fprintf(stderr, "##### fail to avcodec_receive_frame: %d\n", ret);
av_packet_unref(packet);
continue;
}
sws_scale(sws_context, frame->data, frame->linesize, 0, codec_context->height, bgr_data, bgr_linesize);
mat.data = bgr_data[0];
cv::imshow(winname, mat);
}
else if (ret < 0 || packet->size <= 0) {
fprintf(stderr, "##### fail to av_read_frame: %d, packet size: %d\n", ret, packet->size);
continue;
}
av_packet_unref(packet);
int key = cv::waitKey(30);
if (key == 27) break;
}
cv::destroyWindow(winname);
sws_freeContext(sws_context);
av_frame_free(&frame);
av_freep(packet);
av_freep(&bgr_data[0]);
avformat_close_input(&format_context);
av_dict_free(&dict);
fprintf(stdout, "test finish\n");
return 0;
}
在av_read_frame處設置斷點,單步調試,可進入av_read_frame函數內部,如下圖所示: