簡單FFmpeg+SDL2的視頻播放器

// compatibility with newer API

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)

#define av_frame_alloc avcodec_alloc_frame#define av_frame_free avcodec_free_frame

#endif

int main(int argc, char *argv[]) {?

?AVFormatContext *pFormatCtx = NULL;?

?int? ? ? ? ? ? i, videoStream; ?

AVCodecContext? *pCodecCtxOrig = NULL;?

?AVCodecContext? *pCodecCtx = NULL;?

?AVCodec? ? ? ? *pCodec = NULL;?

?AVFrame? ? ? ? *pFrame = NULL;?

?unsigned char? *out_buffer;?

?AVFrame? ? ? ? *pFrameYUV = NULL;?

?AVPacket? ? ? ? *packet;?

?int? ? ? ? ? ? frameFinished;? float? ? ? ? ? aspect_ratio;?

?struct SwsContext *sws_ctx = NULL;?

?int screen_w=0,screen_h=0;?

?SDL_Texture? ? *texture;

? SDL_Window? ? ? *screen;? SDL_Renderer *renderer;? SDL_Rect? ? ? ? rect;

? SDL_Event? ? ? event;?

?if(argc < 2) {??

? fprintf(stderr, "Usage: test\n");? ?

?exit(1);?

?}??

// Register all formats and codecs??

av_register_all();? ?

?if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {?

?? fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());?

?? exit(1);

? }?

?// Open video file?

?if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)? ?

?return -1;

?// Couldn't open file? ?

?// Retrieve stream information? if(avformat_find_stream_info(pFormatCtx, NULL)<0)??

? return -1;

?// Couldn't find stream information??

? // Dump information about file onto standard error? av_dump_format(pFormatCtx, 0, argv[1], 0);? ?

?// Find the first video stream?

?videoStream=-1;?

?for(i=0; inb_streams; i++)

? ? ? if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {

videoStream=i;

break;

}

if(videoStream==-1)

return -1; // Didn't find a video stream

// Get a pointer to the codec context for the video stream

pCodecCtxOrig=pFormatCtx->streams[videoStream]->codec;

// Find the decoder for the video stream

pCodec=avcodec_find_decoder(pCodecCtxOrig->codec_id);

if(pCodec==NULL) {

fprintf(stderr, "Unsupported codec!\n");

return -1; // Codec not found

}

// Copy context

pCodecCtx = avcodec_alloc_context3(pCodec);

if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {

fprintf(stderr, "Couldn't copy codec context");

return -1; // Error copying codec context

}

// Open codec

if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)

return -1; // Could not open codec

// Allocate video frame

pFrame=av_frame_alloc();

pFrameYUV = av_frame_alloc();

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);

packet=(AVPacket *)av_malloc(sizeof(AVPacket));

screen_w = pCodecCtx->width;

screen_h = pCodecCtx->height;

//SDL 2.0 Support for multiple windows

screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,

screen_w, screen_h,

SDL_WINDOW_OPENGL);

if(!screen) {

printf("SDL: could not create window - exiting:%s\n",SDL_GetError());

return -1;

}

// Allocate a place to put our YUV image on that screen

renderer = SDL_CreateRenderer(screen, -1, 0);

texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);

rect.x=0;

rect.y=0;

rect.w=screen_w;

rect.h=screen_h;

// initialize SWS context for software scaling

sws_ctx = sws_getContext(pCodecCtx->width,

pCodecCtx->height,

pCodecCtx->pix_fmt,

pCodecCtx->width,

pCodecCtx->height,

AV_PIX_FMT_YUV420P,

SWS_BILINEAR,

NULL,

NULL,

NULL

);

// Read frames and save first five frames to disk

i=0;

while(av_read_frame(pFormatCtx, packet)>=0) {

// Is this a packet from the video stream?

if(packet->stream_index==videoStream) {

// Decode video frame

avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, packet);

// Did we get a video frame?

if(frameFinished) {

// SDL_LockYUVOverlay(bmp);

// Convert the image into YUV format that SDL uses

sws_scale(sws_ctx, (const unsigned char* const*)pFrame->data,

pFrame->linesize,

0,

pCodecCtx->height,

pFrameYUV->data,

pFrameYUV->linesize

);

#if OUTPUT_YUV420P

y_size=pCodecCtx->width*pCodecCtx->height;

fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);? ? //Y

fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);? //U

fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);? //V

#endif

#if 0

SDL_UpdateTexture( bmp, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0] );

#else

SDL_UpdateYUVTexture(texture, &rect,

pFrameYUV->data[0],

pFrameYUV->linesize[0],

pFrameYUV->data[1],

pFrameYUV->linesize[1],

pFrameYUV->data[2],

pFrameYUV->linesize[2]);

#endif

SDL_RenderClear( renderer );

SDL_RenderCopy( renderer, texture,? NULL, &rect);

SDL_RenderPresent( renderer );

SDL_Delay(40);

}

}

// Free the packet that was allocated by av_read_frame

av_free_packet(packet);

SDL_PollEvent(&event);

switch(event.type) {

case SDL_QUIT:

SDL_Quit();

exit(0);

break;

default:

break;

}

}

// Free the YUV frame

av_frame_free(&pFrame);

// Close the codec

avcodec_close(pCodecCtx);

avcodec_close(pCodecCtxOrig);

// Close the video file

avformat_close_input(&pFormatCtx);

return 0;

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末舆声,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茁裙,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機拆宛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來讼撒,“玉大人浑厚,你說我怎么就攤上這事〈患纾” “怎么了瞻颂?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長郑象。 經(jīng)常有香客問我贡这,道長,這世上最難降的妖魔是什么厂榛? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任盖矫,我火速辦了婚禮,結(jié)果婚禮上击奶,老公的妹妹穿的比我還像新娘辈双。我一直安慰自己,他們只是感情好柜砾,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布湃望。 她就那樣靜靜地躺著,像睡著了一般痰驱。 火紅的嫁衣襯著肌膚如雪证芭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天担映,我揣著相機與錄音废士,去河邊找鬼。 笑死蝇完,一個胖子當著我的面吹牛官硝,可吹牛的內(nèi)容都是我干的矗蕊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼氢架,長吁一口氣:“原來是場噩夢啊……” “哼傻咖!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起达箍,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤没龙,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后缎玫,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體硬纤,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年赃磨,在試婚紗的時候發(fā)現(xiàn)自己被綠了筝家。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡邻辉,死狀恐怖溪王,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情值骇,我是刑警寧澤莹菱,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站吱瘩,受9級特大地震影響道伟,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜使碾,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一蜜徽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧票摇,春花似錦拘鞋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至祟剔,卻和暖如春傅事,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背峡扩。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留障本,地道東北人教届。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓响鹃,卻偏偏與公主長得像,于是被迫代替她去往敵國和親案训。 傳聞我的和親對象是個殘疾皇子买置,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容