运行环境: Mac OS 10.15
FFMPEG版本: 4.2.2
SDL: 2.0
参考 https://blog.csdn.net/leixiaohua1020/article/details/38868499
//
// main.cpp
// ffmpeg_sample2
//
// Created by gaoguanglei on 2020/1/24.
// Copyright © 2020 gaoguanglei. All rights reserved.
//
#include <iostream>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL2/SDL.h>
#include <libavutil/imgutils.h>
}
#define REFRESH_EVENT (SDL_USEREVENT+1)
#define BREAK_EVENT (SDL_USEREVENT+2)
int g_thread_exit = 0;
int g_thread_pause = 0;
int refresh_thread(void*)
{
while(!g_thread_exit)
{
if(!g_thread_pause)
{
SDL_Event event;
event.type = REFRESH_EVENT;
SDL_PushEvent(&event);
SDL_Delay(40);
}
else
{
continue;
}
}
SDL_Event event;
event.type = BREAK_EVENT;
SDL_PushEvent(&event);
return 0;
}
int main(int argc, const char * argv[]) {
// insert code here...
// avformat_network_init();
AVFormatContext* pAVFormatContext = avformat_alloc_context();
/** Open an input stream and read the header. The codecs are not opened.
* The stream must be closed with avformat_close_input().
*/
if(avformat_open_input(&pAVFormatContext, "/Users/gaoguanglei/work/test.mov", nullptr, nullptr)!=0)
{
printf("could not open input stream\n");
}
/**
* Read packets of a media file to get stream information. This
* is useful for file formats with no headers such as MPEG. This
* function also computes the real framerate in case of MPEG-2 repeat
* frame mode.
* The logical file position is not changed by this function;
* examined packets may be buffered for later processing.
*/
if(avformat_find_stream_info(pAVFormatContext, nullptr) != 0)
{
printf("count not find stream info\n");
}
int videIndex = -1;
for(int i=0; i<pAVFormatContext->nb_streams; i++)
{
if(pAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
videIndex = i;
break;
}
}
if(videIndex == -1)
{
printf("video index == -1, return\n");
return 0;
}
/**
* Find a registered decoder with a matching codec ID.
*
* @param id AVCodecID of the requested decoder
* @return A decoder if one was found, NULL otherwise.
*/
AVCodec* pAVCodec = avcodec_find_decoder(pAVFormatContext->streams[videIndex]->codecpar->codec_id);
if(pAVCodec == nullptr)
{
printf("could not find codec\n");
}
/**
* Allocate an AVCodecContext and set its fields to default values. The
* resulting struct should be freed with avcodec_free_context().
*
* @param codec if non-NULL, allocate private data and initialize defaults
* for the given codec. It is illegal to then call avcodec_open2()
* with a different codec.
* If NULL, then the codec-specific defaults won't be initialized,
* which may result in suboptimal default settings (this is
* important mainly for encoders, e.g. libx264).
*
* @return An AVCodecContext filled with default values or NULL on failure.
*/
AVCodecContext* pAVCodecContext = avcodec_alloc_context3(pAVCodec);
/**
* Fill the codec context based on the values from the supplied codec
* parameters. Any allocated fields in codec that have a corresponding field in
* par are freed and replaced with duplicates of the corresponding field in par.
* Fields in codec that do not have a counterpart in par are not touched.
*
* @return >= 0 o