This article mainly describes how to play a video file.
If there is no foundation for YUV, you can look at: http://www.cnblogs.com/nanguabing/archive/2012/04/12/2443485.html
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { fprintf(stderr, "Could not initialize SDL - %s/n", SDL_GetError()); exit(1); }
The sdl_init () function tells the SDL library which features will be used. sdl_init_video | sdl_init_audio | sdl_init_timer is video, audio, and time.
SDL_Surface *screen; screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0); if (!screen) { fprintf(stderr, "SDL: could not set video mode - exiting/n"); exit(1); }
Sdl_surface is the container for displaying images.
Function sdl_setvideomode (pcodecctx-> width, pcodecctx-> height,); create a window with four parameters, the window width, height, the number of digits occupied by a single pixel, and a tag variable are displayed in sequence. The simplest way to set the third parameter is to set it to 0, so that it is the current default display setting ). The last parameter uses sdl_hwsurface and sdl_doublebuf. To be used together, use the OR operator: sdl_hwsurface | sdl_doublebuf.
The sdl_setvideomode () function not only creates a window, but also creates a memory area called "screen buffer" to display images. This area is responsible for displaying the screen to the screen. The variable sdl_hwsurface indicates that the cache is created in the display memory; sdl_doublebuf indicates that I want to create two cache areas and one is used as the front-end cache, the content that we are displaying on the screen is stored here, And the content that we are going to display on the screen is used as the backend cache. When we display the content to be displayed, we only need to swap the front-end cache and the back-end cache, so that the content of the back-end cache will be displayed. (Note: in other words, the front-end cache becomes the back-end cache and can be used to store the next picture to be displayed.) This technology is called dual-cache to accelerate the rendering process of images.
SDL_Overlay *bmp; bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height, SDL_YV12_OVERLAY, screen);
Sdl_overlay is used to store YUV.
The function sdl_createyuvoverlay (pcodecctx-> width, pcodecctx-> height, sdl_yv12_overlay, screen) is used to create YUV. The parameters are width, height, YUV format, and sdl_surface.
Sdl_rect rect; If (framefinished) {sdl_lockyuvoverlay (BMP); avpicture PICT; // assign YUV to avpicture Pict. data [0] = BMP-> pixels [0]; Pict. data [1] = BMP-> pixels [2]; Pict. data [2] = BMP-> pixels [1]; Pict. linesize [0] = BMP-> pitches [0]; Pict. linesize [1] = BMP-> pitches [2]; Pict. linesize [2] = BMP-> pitches [1]; // convert the image into YUV format that SDL uses img_convert (& pict, pix_fmt_yuv420p, (avpicture *) pframe, pcodecctx-> pix_fmt, pcodecctx-> width, pcodecctx-> height); sdl_unlockyuvoverlay (BMP); rect. X = 0; rect. y = 0; rect. W = pcodecctx-> width; rect. H = pcodecctx-> height; sdl_displayyuvoverlay (BMP, & rect );}
Sdl_rect defines a rectangular area on the screen. It is used by video functions such as sdl_blitsurface () to define the texture area.
Sdl_lockyuvoverlay (BMP); locks YUV.
The custom function img_convert (& pict, pix_fmt_yuv420p, (avpicture *) pframe, pcodecctx-> pix_fmt, pcodecctx-> width, pcodecctx-> height) converts YUV to image.
Sdl_unlockyuvoverlay (BMP); unlock YUV.
Function sdl_displayyuvoverlay (BMP, & rect); displays images.
SDL_Event event; av_free_packet(&packet); SDL_PollEvent(&event); switch (event.type) { case SDL_QUIT: SDL_Quit(); exit(0); break; default: break; }
The video logout function.
C File Download
Http://download.csdn.net/detail/wenwei19861106/4220091