FFMPEG学习----使用SDL构建视频播放器
#include <stdio.h>
#include <string.h> extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "SDL.h"
};
//依赖库
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2main.lib") //Refresh Event
#define REFRESH_EVENT (SDL_USEREVENT + 1)
//Break Event
#define BREAK_EVENT (SDL_USEREVENT + 2) bool thread_exit = false;
bool thread_pause = false; int RefreshVideo(void *opaque)
{
thread_exit = false;
thread_pause = false;
while (!thread_exit)
{
//没按 space
if (!thread_pause)
{
SDL_Event event;
event.type = REFRESH_EVENT;
SDL_PushEvent(&event);
}
SDL_Delay(40);
}
thread_exit = false;
thread_pause = false;
//Break 退出main函数循环
SDL_Event event;
event.type = BREAK_EVENT;
SDL_PushEvent(&event);
return 0;
} int main(int argc, char* argv[])
{
//------------FFmpeg----------------
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL, *pFrameYUV = NULL;
unsigned char *out_buffer = NULL;
AVPacket packet;
struct SwsContext *img_convert_ctx = NULL;
int y_size;
int got_picture;
int i, videoIndex;
int frame_cnt = 1; //------------SDL----------------
SDL_Window *sdlScreen = NULL;
SDL_Renderer *sdlRenderer = NULL;
SDL_Texture *sdlTexture = NULL;
SDL_Rect sdlRect;
SDL_Thread *sdlThread = NULL;
SDL_Event event; char filepath[1024] = "";
printf("Usage: program.exe Titanic.ts\n");
if (argc == 2)
{
strcpy(filepath, argv[1]);
}
else
{
printf("Could not find a file\n");
return -1;
} av_register_all(); if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0)
{
printf("Couldn't open an input stream.\n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
}
videoIndex = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoIndex = i;
break;
} if (videoIndex == -1)
{
printf("Couldn't find a video stream.\n");
return -1;
} pCodecCtx = pFormatCtx->streams[videoIndex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
{
printf("Codec not found.\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.\n");
return -1;
} //Output Info-----------------------------
printf("--------------- File Information ----------------\n");
av_dump_format(pFormatCtx, 0, filepath, 0);
printf("-------------------------------------------------\n"); pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
if (pFrame == NULL || pFrameYUV == NULL)
{
printf("memory allocation error\n");
return -1;
}
out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,
AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); if (SDL_Init(SDL_INIT_VIDEO))
{
printf("Could not initialize SDL - %s\n", SDL_GetError());
return -1;
} sdlScreen = SDL_CreateWindow("FFmpeg Player",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
pCodecCtx->width, pCodecCtx->height, SDL_WINDOW_OPENGL);
if (sdlScreen == 0)
{
printf("SDL: could not create SDL_Window - exiting:%s\n", SDL_GetError());
return -1;
} sdlRenderer = SDL_CreateRenderer(sdlScreen, -1, SDL_RENDERER_ACCELERATED);
if (sdlRenderer == NULL)
{
printf("SDL: could not create SDL_Renderer - exiting:%s\n", SDL_GetError());
return -1;
} sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);
if (sdlTexture == NULL)
{
printf("SDL: could not create SDL_Texture - exiting:%s\n", SDL_GetError());
return -1;
} //设置图像显示位置
sdlRect.x = 10;
sdlRect.y = 10;
sdlRect.w = pCodecCtx->width - 20;
sdlRect.h = pCodecCtx->height - 20; sdlThread = SDL_CreateThread(RefreshVideo, NULL, NULL); while (true)
{
SDL_WaitEvent(&event);
if (event.type == REFRESH_EVENT)
{
while (true)
{
if (av_read_frame(pFormatCtx, &packet) < 0)
{
thread_exit = true;
}
if (packet.stream_index == videoIndex)
{
break;
}
}
if (avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet) < 0)
{
printf("Decode Error.\n");
return -1;
}
if (got_picture)
{
sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pFrameYUV->data, pFrameYUV->linesize); SDL_UpdateYUVTexture(sdlTexture, NULL,
pFrameYUV->data[0], pFrameYUV->linesize[0],
pFrameYUV->data[1], pFrameYUV->linesize[1],
pFrameYUV->data[2], pFrameYUV->linesize[2]
);
//SDL_UpdateTexture(sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
SDL_RenderPresent(sdlRenderer); printf("Succeed to decode %d frame!\n", frame_cnt);
frame_cnt++;
}
av_free_packet(&packet);
}
else if (event.type == SDL_KEYDOWN)
{
//pause
if (event.key.keysym.sym == SDLK_SPACE)
{
thread_pause = !thread_pause;
}
}
else if (event.type == SDL_QUIT)
{
thread_exit = true;
}
else if (event.type == BREAK_EVENT)
{
break;
}
}//while SDL_Quit(); sws_freeContext(img_convert_ctx);
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx); return 0;
}
有人会疑惑,为什么解码后的pFrame不直接用于显示,而是调用swscale()转换之后进行显示?
如果不进行转换,而是直接调用SDL进行显示的话,会发现显示出来的图像是混乱的。关键问题在于解码后的pFrame的linesize里存储的不是图像的宽度,而是比宽度大一些的一个值。其原因目前还没有仔细调查(大概是出于性能的考虑)。例如分辨率为480x272的图像,解码后的视频的linesize[0]为512,而不是480。以第1行亮度像素(pFrame->data[0])为例,从0-480存储的是亮度数据,而从480-512则存储的是无效的数据。因此需要使用swscale()进行转换。转换后去除了无效数据,linesize[0]变为480。就可以正常显示了。
FFMPEG学习----使用SDL构建视频播放器的更多相关文章
- FFMPEG学习----使用SDL构建音频播放器
ffmpeg版本:ffmpeg-20160413-git-0efafc5 #include <stdio.h> #include <stdlib.h> #include < ...
- 最简单的基于FFMPEG+SDL的视频播放器 ver2 (採用SDL2.0)
===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...
- 最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)
===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...
- 100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)【转】
转自:http://blog.csdn.net/leixiaohua1020/article/details/8652605 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] ...
- 最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器
===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...
- 音视频处理之FFmpeg+SDL+MFC视频播放器20180411
一.FFmpeg+SDL+MFC视频播放器 1.MFC知识 1).创建MFC工程的方法 打开VC++ 文件->新建->项目->MFC应用程序 应用程序类型->基于对话框 取消勾 ...
- 基于<最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)>的一些个人总结
最近因为项目接近收尾阶段,所以变的没有之前那么忙了,所以最近重新拿起了之前的一些FFMPEG和SDL的相关流媒体播放器的例子在看. 同时自己也用FFMPEG2.01,SDL2.01结合MFC以及网上罗 ...
- 用JavaCV改写“100行代码实现最简单的基于FFMPEG+SDL的视频播放器 ”
FFMPEG的文档少,JavaCV的文档就更少了.从网上找到这篇100行代码实现最简单的基于FFMPEG+SDL的视频播放器.地址是http://blog.csdn.net/leixiaohua102 ...
- 【转】100行代码实现最简单的基于FFMPEG+SDL的视频播放器
FFMPEG工程浩大,可以参考的书籍又不是很多,因此很多刚学习FFMPEG的人常常感觉到无从下手.我刚接触FFMPEG的时候也感觉不知从何学起. 因此我把自己做项目过程中实现的一个非常简单的视频播放器 ...
随机推荐
- StatePattern(状态模式)-----Java/.Net
在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式. 在状态模式中,我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context ...
- 《美国纽约摄影学院摄影教材》PDF教材
下载地址: 美国纽约摄影学院摄影教材(上册).pdf 美国纽约摄影学院摄影教材(下册).pdf 欢迎你到纽约摄影学院来,我们急切地等待着开课,你们也在 跃跃欲试了.那就让我们马上开始吧! 你已 ...
- 听说你的 IDEA 昨天掉链子了?松哥给你准备了大招
昨天一直在忙,中午抽空瞅了一眼技术群,天呐,竟然都在切磋 IDEA 激活码的事情,瞬间明白可能 jetbrains 又在搞事情了. 我大概了解了下,这次出事的主要是 2019 版,之前的 2018 版 ...
- 手把手带你了解消息中间件(2)——RabbitMQ
RabbitMQ官网: http://www.rabbitmq.com/ Erlang官网:https://www.erlang.org 友情提示:RabbitMQ基于Erlang语言开发的,要想使用 ...
- Flutter中Expanded组件用法
Flutter中Expanded组件用法 Expanded组件可以使Row.Column.Flex等子组件在其主轴方向上展开并填充可用空间(例如,Row在水平方向,Column在垂直方向).如果多个子 ...
- 动态规划之抢劫问题-LT213
找到大问题和小问题之间共有的特性,列出一定的状态转移规律,然后设计满足条件的小问题解决方案,最后凭借记忆中的中间值快速求出最终解 动态规划问题的复杂性在于你永远不知道下一个题目中的状态是什么,有什么样 ...
- MongoDB高级用法
MongoDB高级查询用法大全 转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册:http://ww ...
- JS 通过url地址栏获取html页面名称
有的时候需要获取页面名称,为此我在这里封装了一个方. 一.分别根据传递不同的参数,获取到html页面的名称. 通过传递参数,获取到html页面的名称:参数params 以下是参数解释说明 (1)par ...
- 8086汇编语言学习(二) 8086汇编开发环境搭建和Debug模式介绍
1. 8086汇编开发环境搭建 在上篇博客中简单的介绍了8086汇编语言.工欲善其事,必先利其器,在8086汇编语言正式开始学习之前,先介绍一下如何搭建8086汇编的开发环境. 汇编语言设计之初是用于 ...
- 通过例子进阶学习C++(七)CMake项目通过模板库实现约瑟夫环
本文是通过例子学习C++的第七篇,通过这个例子可以快速入门c++相关的语法. 1.问题描述 回顾一下约瑟夫环问题:n 个人围坐在一个圆桌周围,现在从第 s 个人开始报数,数到第 m 个人,让他出局:然 ...