#include "stdafx.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
//新版里的图像转换结构需要引入的头文件
#include "libswscale/swscale.h"
//SDL
#include "sdl/SDL.h"
#include "sdl/SDL_thread.h"
};
//自定义事件
//刷新画面
#define SFM_REFRESH_EVENT (SDL_USEREVENT + 1)
int thread_exit=0;
//Thread
int sfp_refresh_thread(void *opaque)
{
while (thread_exit==0) {
SDL_Event event;
event.type = SFM_REFRESH_EVENT;
//将事件添加到事件队列
SDL_PushEvent(&event);
//时间间隔
SDL_Delay(15);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
//=============================================================
//**************************定义变量***************************
//=============================================================
AVFormatContext *pFormatCtx;//影音格式环境
int i, videoindex;//video标志
AVCodecContext *pCodecCtx;//编解码器环境
AVCodec *pCodec;
//=============================================================
//**************************初始化环境*************************
//=============================================================
av_register_all();//初始化编码解码器
avformat_network_init();//网络组件的全局初始化,不使用网络可以省略
//=============================================================
//*********************分配空间读取文件************************
//=============================================================
char filepath[] = "MP5.mp4";//文件路径
pFormatCtx = avformat_alloc_context();//分配空间
//打开文件
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
printf("Couldn't open input stream.(无法打开输入流)\n");
return -1;
}
//读取媒体文件的包以获取流信息
if(av_find_stream_info(pFormatCtx)<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("Didn't find a video stream.(没有找到视频流)\n");
return -1;
}
//=============================================================
//*********************找到正确的编解码并打开******************
//=============================================================
//获取编解码
pCodecCtx=pFormatCtx->streams[videoindex]->codec;
//找到一个匹配的编解码器ID注册解码器
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;
}
//=============================================================
//*********************************开始解码********************
//=============================================================
//av框架
AVFrame *pFrame,*pFrameYUV;
//开辟内存
pFrame=avcodec_alloc_frame();
//开辟内存
pFrameYUV=avcodec_alloc_frame();
//开辟内存
uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
//填充根据pCodecCtx的基本信息
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
//=============================================================
//*****************************用SDL播放********************
//=============================================================
//此函数加载SDL动态链接库
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
printf( "Could not initialize SDL - %s\n", SDL_GetError());
return -1;
}
//定义屏幕高宽度
int screen_w=0,screen_h=0;
//创建一个播放窗口
SDL_Surface *screen;
screen_w = pCodecCtx->width;
screen_h = pCodecCtx->height;
//设置视频模式——高宽
screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);
if(!screen) {
printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());
return -1;
}
//YUV视频轮廓
SDL_Overlay *bmp;
//此函数创建视频输出轮廓
bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);
//有用的数据类型的名称
SDL_Rect rect;
int ret, got_picture;//定义返回值变量,
//AVPacket这个结构储存压缩的数据
AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));//开辟空间
//输出一下信息-----------------------------
printf("File Information(文件信息)---------------------\n");
//记录日志
av_dump_format(pFormatCtx,0,filepath,0);
printf("-------------------------------------------------\n");
//img转ctx
struct SwsContext *img_convert_ctx;
//分配并返回一个swscontext,你需要使用sws_scale()执行缩放/转换操作
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
for (;;) {
if(av_read_frame(pFormatCtx, packet)>=0){
if(packet->stream_index==videoindex){
//avcodec_decode_video2解码一帧视频数据。输入一个压缩编码的结构体AVPacket,输出一个解码后的结构体AVFrame
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if(ret < 0){
printf("Decode Error.(解码错误)\n");
return -1;
}
if(got_picture){
//sws_scale用于转换像素的函数
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
//加锁
SDL_LockYUVOverlay(bmp);
bmp->pixels[0]=pFrameYUV->data[0];
bmp->pixels[2]=pFrameYUV->data[1];
bmp->pixels[1]=pFrameYUV->data[2];
bmp->pitches[0]=pFrameYUV->linesize[0];
bmp->pitches[2]=pFrameYUV->linesize[1];
bmp->pitches[1]=pFrameYUV->linesize[2];
//解锁
SDL_UnlockYUVOverlay(bmp);
rect.x = 0;
rect.y = 0;
rect.w = screen_w;
rect.h = screen_h;
//测试自己填充数据----------------
SDL_DisplayYUVOverlay(bmp, &rect); //显示
SDL_Delay(40);
}
}
av_free_packet(packet);//释放
}else{
//Exit Thread结束线程
thread_exit=1;
break;
}
}
SDL_Quit();//退出
sws_freeContext(img_convert_ctx);//释放
av_free(out_buffer);//释放
av_free(pFrameYUV);//释放
avcodec_close(pCodecCtx);//释放
avformat_close_input(&pFormatCtx);//释放
return 0;
}