//ffmpeg simple player
//
//媒资检索系统子系统
//
//2013 雷霄骅 leixiaohua1020@126.com
//中国传媒大学/数字电视技术
//
#include "stdafx.h"
#define SFM_REFRESH_EVENT (SDL_USEREVENT + 1)
int thread_exit = 0;
int sfp_refresh_thread(void *opaque)
{
while (thread_exit==0) {
SDL_Event event;
event.type = SFM_REFRESH_EVENT;
SDL_PushEvent(&event);
//Wait 40 ms
SDL_Delay(40);
}
return 0;
}
FILE *fp_open;
//typedef int (* io_callback)(void *opaque, uint8_t *buf, int buf_size);
int fill_iobuffer(void * opaque,uint8_t *buf, int bufsize){
if(!feof(fp_open)){
int true_size=fread(buf,1,bufsize,fp_open);
return true_size;
}else{
printf(" bufsize is %d \n",bufsize);
return -1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
#if 0
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
char filepath[]="2.mp4";
char filepath1[]="2.mpg";
char filepath2[]="2.avi";
if(avformat_open_input(&pFormatCtx,filepath1,NULL,NULL)!=0){
printf("无法打开文件\n");
return -1;
}
#endif
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVInputFormat *avinputformat = NULL;
fp_open = fopen("1.mp4","rb+");
unsigned char * iobuffer = (unsigned char *)av_malloc(327680000);
AVIOContext *avio = avio_alloc_context(iobuffer,327680000,0,NULL,fill_iobuffer,NULL,NULL);
// string filepath3 = "2.mpg";
av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
//pFormatCtx = NULL; //不需要avformat alloc context分配内存 程序也可以奔跑
#if 1
#if 0
//探测流的格式
if(av_probe_input_buffer(avio,&avinputformat,"",NULL,0,0)<0)
{
printf("探测流失败");
return -1;
}
pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;
#endif
pFormatCtx->pb = avio;
//指定了AVIOcotext 即是pb指针
if(avformat_open_input(&pFormatCtx,NULL,NULL,NULL)<0){
printf("无法打开文件\n");
return -1;
}
#endif
#if 0
if(avformat_open_input(&pFormatCtx,"1.mp4",NULL,NULL)<0){
printf("无法打开文件\n");
return -1;
}
#endif
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;
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)];
if(*out_buffer)
{
}
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,"1.mp4",0);
printf("-------------------------------------------------\n");
//-----------------------------------------------
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
//--------------
SDL_Thread *video_tid = SDL_CreateThread(sfp_refresh_thread,NULL);
//
SDL_WM_SetCaption("Simple FFmpeg Player (SDL Update)",NULL);
//Event Loop
SDL_Event event;
for(;;)
{
SDL_WaitEvent(&event);
if(event.type==SFM_REFRESH_EVENT)
{
if(av_read_frame(pFormatCtx, packet)>=0) //while改成if 起一个线程发给
{
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);
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);
}
}
av_free_packet(packet);
}else
{
thread_exit=1;
break;
}
}
}
SDL_Quit();
sws_freeContext(img_convert_ctx);
delete[] out_buffer;
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
ffmpeg+sdl 简单播放器 MP4 AVI文件
共233个文件
h:172个
tlog:18个
lib:10个
4星 · 超过85%的资源 需积分: 20 75 下载量 15 浏览量
2015-01-14
13:22:42
上传
评论 3
收藏 8.97MB RAR 举报
温馨提示
代码是完整工程,在VS下面就可以打开,2010,2005都可以,代码支持两种功能,可以把文件读到内存,再从内存读取数据,最后显示出来,也可以直接在本地播放,用的是sdl显示,sdl采用事件触发播放的,不会在那循环读
资源推荐
资源详情
资源评论
收起资源包目录
ffmpeg+sdl 简单播放器 MP4 AVI文件 (233个子文件)
ffmpeg_simple_player1.cpp 6KB
stdafx.cpp 231B
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
ffmpeg_simple_player1.exe 33KB
ffmpeg_simple_player1.vcxproj.filters 1KB
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
my_dir.h 3KB
postprocess.h 3KB
my_tree.h 3KB
my_getopt.h 3KB
my_net.h 3KB
共 233 条
- 1
- 2
- 3
wcqys
- 粉丝: 2
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页