//simplest_ffmpeg_player
//
//雷霄骅 Lei Xiaohua
//leixiaohua1020@126.com
//中国传媒大学/数字电视技术
//Communication University of China / Digital TV Technology
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
char filepath[]="E:/TestVideos/UMN.avi";
av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
printf("无法打开文件\n");
return -1;
}
if(av_find_stream_info(pFormatCtx)<0)
{
printf("Couldn't find stream information.\n");
return -1;
}
videoindex=-1;
FILE *output=fopen("out.yuv","wb+");
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;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
printf("Codec not found.\n");
return -1;
}
if(avcodec_open(pCodecCtx, pCodec)<0)
{
printf("Could not open codec.\n");
return -1;
}
AVFrame *pFrame,*pFrameYUV;
pFrame=avcodec_alloc_frame();
pFrameYUV=avcodec_alloc_frame();
uint8_t *out_buffer;
out_buffer=new uint8_t[avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)];
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
//------------SDL----------------
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
printf( "Could not initialize SDL - %s\n", SDL_GetError());
exit(1);
}
SDL_Surface *screen;
screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0);
if(!screen) { printf("SDL: could not set video mode - exiting\n");
exit(1);
}
SDL_Overlay *bmp;
bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);
SDL_Rect rect;
//---------------
int ret, got_picture;
static struct SwsContext *img_convert_ctx;
int y_size = pCodecCtx->width * pCodecCtx->height;
AVPacket *packet=(AVPacket *)malloc(sizeof(AVPacket));
av_new_packet(packet, y_size);
//输出一下信息-----------------------------
printf("文件信息-----------------------------------------\n");
av_dump_format(pFormatCtx,0,filepath,0);
printf("-------------------------------------------------\n");
//------------------------------
while(av_read_frame(pFormatCtx, packet)>=0)
{
if(packet->stream_index==videoindex)
{
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if(ret < 0)
{
printf("解码错误\n");
return -1;
}
if(got_picture)
{
//img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
//sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
//sws_freeContext(img_convert_ctx);
//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 = pCodecCtx->width;
//rect.h = pCodecCtx->height;
//SDL_DisplayYUVOverlay(bmp, &rect);
////延时40ms
//SDL_Delay(40);
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
//fwrite(pFrameYUV->data[0],(pCodecCtx->width)*(pCodecCtx->height)*3,1,output);
fwrite(pFrameYUV->data[0],(pCodecCtx->width)*(pCodecCtx->height),1,output);
fwrite(pFrameYUV->data[1],(pCodecCtx->width)*(pCodecCtx->height)/4,1,output);
fwrite(pFrameYUV->data[2],(pCodecCtx->width)*(pCodecCtx->height)/4,1,output);
}
}
av_free_packet(packet);
}
delete[] out_buffer;
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
FFmpeg视频播放器的简单实例 (251个子文件)
825dc642cb6eb9a060e54bf8d69288fbee4904 15B
config 380B
simplest_ffmpeg_player.cpp 5KB
stdafx.cpp 231B
description 73B
avcodec-54.dll 13.26MB
avformat-54.dll 2.43MB
avfilter-2.dll 903KB
avdevice-53.dll 342KB
swscale-2.dll 307KB
SDL.dll 297KB
postproc-52.dll 158KB
avutil-51.dll 141KB
swresample-0.dll 36KB
exclude 240B
simplest_ffmpeg_player.exe 32KB
simplest_ffmpeg_player.vcxproj.filters 1KB
cuc_ieschool.flv 912KB
SDL_opengl.h 329KB
avcodec.h 151KB
my_uctype.h 68KB
avformat.h 66KB
my_global.h 52KB
my_sys.h 43KB
SDL_video.h 37KB
avfilter.h 36KB
mysql.h 29KB
m_ctype.h 27KB
mysqld_error.h 26KB
my_pthread.h 24KB
opt.h 24KB
my_base.h 23KB
mysql_com.h 21KB
SDL_stdinc.h 16KB
avio.h 15KB
pixfmt.h 15KB
intreadwrite.h 14KB
myisampack.h 14KB
SDL_events.h 13KB
m_string.h 12KB
swscale.h 12KB
common.h 11KB
SDL_audio.h 11KB
config-win.h 10KB
lf.h 9KB
my_atomic.h 8KB
violite.h 8KB
_mingw.h 8KB
avutil.h 7KB
SDL_keysym.h 7KB
pixdesc.h 7KB
my_bitmap.h 7KB
keycache.h 7KB
my_config.h 7KB
avstring.h 7KB
avfiltergraph.h 7KB
eval.h 6KB
audioconvert.h 6KB
thr_lock.h 6KB
SDL_syswm.h 6KB
mem.h 6KB
SDL_endian.h 6KB
SDL_cdrom.h 6KB
log.h 6KB
samplefmt.h 6KB
my_dbug.h 6KB
stdint.h 6KB
SDL_mutex.h 6KB
my_time.h 6KB
xvmc.h 6KB
inttypes.h 6KB
swresample.h 6KB
parseutils.h 5KB
t_ctype.h 5KB
SDL_joystick.h 5KB
timecode.h 5KB
begin_code.h 5KB
SDL_rwops.h 5KB
imgutils.h 5KB
bprint.h 5KB
dict.h 5KB
SDL_mouse.h 5KB
SDL_config_win32.h 4KB
waiting_threads.h 4KB
SDL_timer.h 4KB
fifo.h 4KB
SDL_thread.h 4KB
SDL_config_macosx.h 4KB
generic-msvc.h 4KB
SDL_keyboard.h 4KB
vda.h 4KB
decimal.h 4KB
errmsg.h 4KB
vaapi.h 4KB
attributes.h 4KB
mathematics.h 4KB
rational.h 4KB
my_trie.h 4KB
SDL_config_os2.h 4KB
error.h 3KB
共 251 条
- 1
- 2
- 3
资源评论
- zhu_beyond_jun2016-09-28没用啊 差评
- zhouyongku2015-05-29还可以 仿佛是从雷老大那里抄过来的
- WS2010SW2016-09-27不错,非常实用!
- linzebin272015-07-02能用,好好研究下
FlyingIsland
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功