参考文章 1:http://blog.csdn.net/leixiaohua1020/article/details/8652605
2:http://blog.csdn.net/leixiaohua1020/article/details/40525591
#ifdef _WIN32
//Windows
#include
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include
#include
#include
#include
#ifdef __cplusplus }; #endif #endif //Refresh #define SFM_REFRESH_EVENT (SDL_USEREVENT + 1) int thread_exit=0; //Thread int sfp_refresh_thread(void *opaque) { SDL_Event event; while (thread_exit==0) { event.type = SFM_REFRESH_EVENT; SDL_PushEvent(&event); //Wait 40 ms SDL_Delay(40); } return 0; } int SCREENW = 640; int SCREENH = 480; int main(int argc, char* argv[]) { AVFormatContext *pFormatCtx; int i, videoindex; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame,*pFrameYUV; AVPacket *packet; struct SwsContext *img_convert_ctx; //SDL int ret, got_picture; int screen_w=0,screen_h=0; //SDL_Surface *screen; //SDL_Overlay *bmp; std::cout<
nb_streams); i++) if(pFormatCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){ videoindex=i; break; } if(videoindex==-1){ printf("Didn't find a video stream.\n"); return -1; } pCodecCtx = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(pCodecCtx,pFormatCtx->streams[videoindex]->codecpar); 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; } pFrame=av_frame_alloc(); pFrameYUV=av_frame_alloc(); //uint8_t *out_buffer=(uint8_t *)av_malloc(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()); return -1; } SDL_Window *screen; screen = SDL_CreateWindow("palyer",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREENW,SCREENH,SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); if(!screen) { printf("SDL: could not create window - exiting:%s\n",SDL_GetError()); return -1; } SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0); screen_w = pCodecCtx->width; screen_h = pCodecCtx->height; unsigned char *buffer = new unsigned char[screen_w*screen_h*3/2]; //screen = SDL_SetVideoMode(screen_w, screen_h, 0,0); SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer,SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,screen_w,screen_h); rect.x = 0; rect.y = 0; rect.w = screen_w; rect.h = screen_h; packet=(AVPacket *)av_malloc(sizeof(AVPacket)); printf("---------------File Information------------------\n"); av_dump_format(pFormatCtx,0,filepath,0); printf("-------------------------------------------------\n"); // 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); //-------------- video_tid = SDL_CreateThread(sfp_refresh_thread,NULL,NULL); // //Event Loop for (;;) { //Wait SDL_WaitEvent(&myEvent); if(myEvent.type==SFM_REFRESH_EVENT){ //------------------------------ if(av_read_frame(pFormatCtx, packet)>=0){ if(packet->stream_index==videoindex){ //ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); ret = avcodec_send_packet(pCodecCtx,packet); if(ret < 0){ printf("Decode Error.\n"); return -1; } ret = avcodec_receive_frame(pCodecCtx,pFrame); if(ret== AVERROR(EAGAIN) || ret==AVERROR_EOF) continue; else if(ret<0) { exit(-1); } if(true){ for(int i=0;i
data[0] + j + i*pFrame->linesize[0]); } for(int i=0;i
data[1] + j + i*pFrame->linesize[1]); buffer[(5*screen_w*screen_h/4)+j+(i*screen_w/2)] = *(pFrame->data[2] + j + i*pFrame->linesize[2]); } } SDL_UpdateTexture( sdlTexture, NULL, buffer, screen_w); rect.x = 0; rect.y = 0; rect.w = screen_w; rect.h = screen_h; SDL_RenderClear( sdlRenderer ); SDL_RenderCopy( sdlRenderer, sdlTexture, NULL, &rect); SDL_RenderPresent( sdlRenderer ); //Delay 40ms SDL_Delay(40); } } av_packet_unref(packet); }else{ //Exit Thread thread_exit=1; break; } }else if(myEvent.type==SDL_QUIT){ break; } } delete[] buffer; SDL_Quit(); av_free(pFrameYUV); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return 0; }