参考 雷霄骅(雷神)最简单的基于FFMPEG+SDL的音频播放器 和 100行代码实现最简单的基于FFMPEG+SDL的视频播放器,合并音频和视频示例代码,完成Windows平台下最简单的播放器。
#include "stdafx.h"
#define __STDC_CONSTANT_MACROS
#define SDL_MAIN_HANDLED
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "SDL2/SDL.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
// 解决Log打印问题,可以删掉
FILE __iob_func[3] = { *stdin,*stdout,*stderr };
}
int gThreadExit = 0;
#define SFM_REFRESH_EVENT (SDL_USEREVENT + 1)
#define SFM_BREAK_EVENT (SDL_USEREVENT + 2)
int SfpRefreshThread(void* opaque)
{
gThreadExit = 0;
while (!gThreadExit)
{
SDL_Event event;
event.type = SFM_REFRESH_EVENT;
SDL_PushEvent(&event);
SDL_Delay(1);
}
gThreadExit = 0;
//Break
SDL_Event event;
event.type = SFM_BREAK_EVENT;
SDL_PushEvent(&event);
return 0;
}
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
//Buffer:
//chunk-------pos---len-----|
static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;
/* The audio function callback takes the following parameters:
* stream: A pointer to the audio buffer to be filled
* len: The length (in bytes) of the audio buffer
*/
void fill_audio(void *udata, Uint8 *stream, int len)
{
//SDL 2.0
SDL_memset(stream, 0, len);
if (audio_len == 0)
{
printf("audio len zero");
}
else
{
len = (len > audio_len ? audio_len : len); /* Mix as much data as possible */
SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
audio_pos += len;
audio_len -= len;
}
}
int main()
{
//资源路径,按需更换
//char path[] = "res/diaosi.mov";
//char path[] = "res/cuc_ieschool.flv";
char path[] = "res/Titanic.ts";
AVFormatContext* av_format_context;
av_register_all();
avformat_network_init();
av_format_context = avformat_alloc_context();
uint8_t *au_out_buffer;
SDL_AudioSpec wanted_spec;
int ret;
uint32_t len = 0;
int64_t in_channel_layout;
struct SwrContext *au_convert_ctx;
if (avformat_open_input(&av_format_context, path, nullptr, nullptr) != 0)
{
printf("Couldn't open input stream.\n");
return -1;
}
if (avformat_find_stream_info(av_format_context, nullptr) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
}
// Dump valid information onto standard error
av_dump_format(av_format_context, 0, path, false);
int videoindex = -1;
int audioindex = -1;
for (int i = 0, size = av_format_context->nb_streams; i < size; i++)
{
if (videoindex == -1 && av_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoindex = i;
}
if (audioindex == -1 && av_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioindex = i;
}
}
if (videoindex == -1 || audioindex == -1)
{
printf("Didn't find stream.\n");
return -1;
}
// 视频编码
AVCodecContext* av_codec_context;
AVCodec* av_codec;
av_codec_context = av_format_context->streams[videoindex]->codec;
av_codec = avcodec_find_decoder(av_codec_context->codec_id);
// 音频编码
AVCodecContext* au_codec_context;
AVCodec* au_codec;
au_codec_context = av_format_context->streams[audioindex]->codec;
au_codec = avcodec_find_decoder(au_codec_context->codec_id);
if (av_codec == nullptr || au_codec == nullptr)
{
printf("Codec not found.\n");
return -1;
}
if (avcodec_open2(av_codec_context, av_codec, nullptr) < 0 ||
avcodec_open2(au_codec_context, au_codec, nullptr))
{
printf("Could not open codec.\n");
return -1;
}
AVFrame* av_frame = av_frame_alloc();
AVFrame* av_frame_yuv = av_frame_alloc();
AVPacket* packet = (AVPacket *)av_malloc(sizeof(AVPacket));
struct SwsContext* img_convert_ctx;
uint8_t* out_buffer = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, av_codec_context->width, av_codec_context->height));
avpicture_fill((AVPicture *)av_frame_yuv, out_buffer, PIX_FMT_YUV420P, av_codec_context->width, av_codec_context->height);
av_dump_format(av_format_context, 0, path, 0);
img_convert_ctx = sws_getContext(av_codec_context->width, av_codec_context->height, av_codec_context->pix_fmt,
av_codec_context->width, av_codec_context->height, PIX_FMT_YUV420P, SWS_BICUBIC, nullptr, nullptr, nullptr);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
{
printf("Could not initialize SDL - %s\n", SDL_GetError());
return -1;
}
//Out Audio Param
uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;
//nb_samples: AAC-1024 MP3-1152 音频帧包含的字节数
int out_nb_samples = au_codec_context->frame_size;
AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
int out_sample_rate = au_codec_context->sample_rate;
int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);
//Out Buffer Size
int out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);
au_out_buffer = (uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE * 2);
//SDL_AudioSpec
wanted_spec.freq = out_sample_rate;
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.channels = out_channels;
wanted_spec.silence = 0;
wanted_spec.samples = out_nb_samples;
wanted_spec.callback = fill_audio;
wanted_spec.userdata = au_codec_context;
if (SDL_OpenAudio(&wanted_spec, NULL) < 0)
{
printf("can't open audio.\n");
return -1;
}
//FIX:Some Codec's Context Information is missing
in_channel_layout = av_get_default_channel_layout(au_codec_context->channels);
au_convert_ctx = swr_alloc();
au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, out_channel_layout, out_sample_fmt, out_sample_rate,
in_channel_layout, au_codec_context->sample_fmt, au_codec_context->sample_rate, 0, NULL);
swr_init(au_convert_ctx);
//Play SDL_OpenAudio打开音频之后调用 参数0 代表播放,非0代表暂停
SDL_PauseAudio(0);
// 绘制界面
SDL_Window* screen;
//SDL 2.0 Support for multiple windows
screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
av_codec_context->width, av_codec_context->height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!screen)
{
printf("SDL: could not create window - exiting:%s\n", SDL_GetError());
return -1;
}
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING, av_codec_context->width, av_codec_context->height);
SDL_Rect sdlRect;
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = av_codec_context->width;
sdlRect.h = av_codec_context->height;
SDL_Event event;
SDL_CreateThread(SfpRefreshThread, nullptr, nullptr);
int got_frame;
for (;;)
{
SDL_WaitEvent(&event);
if (event.type == SFM_REFRESH_EVENT)
{
if (av_read_frame(av_format_context, packet) >= 0)
{
// 判断是否为音频流
if (packet->stream_index == audioindex)
{
ret = avcodec_decode_audio4(au_codec_context, av_frame, &got_frame, packet);
if (ret < 0)
{
printf("Error in decoding audio frame.\n");
return -1;
}
if (got_frame > 0)
{
swr_convert(au_convert_ctx, &au_out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t **)av_frame->data,
av_frame->nb_samples);
}
// 等待音频回调完成,近似以音频时间为基准的音视频同步
while (audio_len > 0)
{
}
//Set audio buffer (PCM data)
audio_chunk = (Uint8 *)au_out_buffer;
//Audio buffer length
audio_len = out_buffer_size;
audio_pos = audio_chunk;
}
// 判断是否为视频流
if (packet->stream_index == videoindex)
{
int ret = avcodec_decode_video2(av_codec_context, av_frame, &got_frame, packet);
if (ret < 0)
{
printf("Decode Error.\n");
return -1;
}
if (got_frame)
{
sws_scale(img_convert_ctx, (const uint8_t* const*)av_frame->data, av_frame->linesize, 0, av_codec_context->height,
av_frame_yuv->data, av_frame_yuv->linesize);
//SDL_UpdateTexture(sdlTexture, nullptr, av_frame_yuv->data[0], av_frame_yuv->linesize[0]);
SDL_UpdateYUVTexture(sdlTexture, // sdl texture
&sdlRect, // sdl rect
av_frame_yuv->data[0], // y plane
av_frame_yuv->linesize[0], // y pitch
av_frame_yuv->data[1], // u plane
av_frame_yuv->linesize[1], // u pitch
av_frame_yuv->data[2], // v plane
av_frame_yuv->linesize[2] // v pitch
);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, nullptr, &sdlRect);
SDL_RenderPresent(sdlRenderer);
}
}
av_free_packet(packet);
}
else
{
gThreadExit = 1;
}
}
else if (event.type == SDL_QUIT)
{
gThreadExit = 1;
}
else if (event.type == SFM_BREAK_EVENT)
{
break;
}
}
av_free(out_buffer);
av_free(au_out_buffer);
sws_freeContext(img_convert_ctx);
swr_free(&au_convert_ctx);
SDL_CloseAudio();
SDL_Quit();
av_frame_free(&av_frame_yuv);
av_frame_free(&av_frame);
avcodec_close(av_codec_context);
avcodec_close(au_codec_context);
avformat_close_input(&av_format_context);
return 0;
}
项目上传地址(码云)
https://gitee.com/canber/YCY-src/blob/master/ffmpeg-sdl-demo.rar