#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。就可以正常显示了。

最新文章

  1. maven settings.xml 阿里云
  2. Quartus signal tapii 的使用
  3. Linux系统下ssh的相关配置详细解析
  4. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (二) 图片验证码的识别
  5. NOI2010超级钢琴
  6. 【转】C++的面象对象总结
  7. extjs6整合到web项目中
  8. 设置Cookie,登录记住用户登录信息,获取用户登录过得信息
  9. 过渡与动画 - steps调速函数&amp;CSS值与单位之ch
  10. xBIM 格式之间转换
  11. python之路——初识函数
  12. spark计算两个DataFrame的差集、交集、合集
  13. JNI编程实现(Windows)
  14. xubuntu无法进图形界面问题
  15. 【java】之常用四大线程池用法以及ThreadPoolExecutor详解
  16. 子数整数(P1151&NOIP水题测试(2017082301))
  17. SprintBoot 1.2.8 入门
  18. FTP命令字和响应码解释
  19. opencv 摄像头 线程
  20. SV中的数据类型

热门文章

  1. 菜鸟系列Fabric源码学习 — MVCC验证
  2. delphi7 如何描述窗体上的全部控件
  3. 「洛谷P1231」教辅的组成 解题报告
  4. PythonI/O进阶学习笔记_9.python的生成器
  5. spring boot 中AOP的使用
  6. 《吊打面试官》系列-ArrayList
  7. 从零开始Go语言-GoLand(编译器)-Windows(平台)
  8. 传统远程注入线程,加载DLL
  9. 关于redis分布式锁
  10. 一个简易的 LED 数字时钟实现方法