SDL and video
To display on the screen, we will use SDL. SDL, short for simple direct layer. It is an excellent multi-media library, applicable to multiple platforms, and is used in many projects. You can log on to this database from its official website http://www.libsdl.org/Source codeOr, if possible, you can directly download the SDK to your operating system. Follow this instruction and you will need to compile this library. (The same is true for the remaining guidance)
The SDL library has many ways to draw images on the screen, and it has a special way to display images on the screen-this method is called YUV coverage. YUV (technically not called YUV but YCbCr) is a format similar to RGB for storing original images. Roughly speaking, Y is the brightness component, and U and V are the color components. (This format is much more complex than RGB, because a lot of color information is discarded, and you can have 1 U and 1 V for every 2 Y ). Sdl yuv overwrites a set of raw YUV data and displays them on the screen. It supports four different YUV formats, but yv12 is the fastest. There is also a YUV format called yuv420p, which is the same as yv12, except that the positions of U and V components are changed. 420 means that it performs Secondary Sampling at, which basically means that one color component corresponds to four brightness components. Therefore, its color information is only 1/4 of the original. This is a good way to save bandwidth, because the human eyes do not feel this change. In the name, P indicates that the format is flat-simply put, Y, U, and V are in different arrays. FFmpeg can convert the image format to yuv420p, but now many video streams are in yuv420p format or can be easily converted to yuv420p format.
Therefore, we plan to replace the saveframe () function in tutorial 1 so that it can directly output our frames to the screen. However, we must first look at how to use the SDL library. First, we must first include the header file of the SDL library and initialize it.
# Include <SDL. h>
# Include <sdl_thread.h>
If (sdl_init (sdl_init_video | sdl_init_audio | sdl_init_timer )){
Fprintf (stderr, "cocould not initialize SDL-% s \ n", sdl_geterror ());
Exit (1 );
}
The sdl_init () function tells the SDL library which features will be used. Of course, sdl_geterror () is a function used for manual debugging.
Create a display
Now we need to put something in a place on the screen. The basic area of the image displayed in SDL is the surface area.
Sdl_surface * screen;
Screen = sdl_setvideomode (pcodecctx-> width, pcodecctx-> height, 0, 0 );
If (! Screen ){
Fprintf (stderr, "SDL: cocould not set video mode-exiting \ n ");
Exit (1 );
}
This creates a screen with a given height and width. The next option is the color depth of the screen-0 indicates that the same depth is used as the current one. (This is not working properly on OS X. For the reason, see the source.Code)
Now we create a YUV overwrite on the screen so that we can input the video:
Sdl_overlay * BMP;
BMP = sdl_createyuvoverlay (pcodecctx-> width, pcodecctx-> height,
Sdl_yv12_overlay, screen );
As we mentioned earlier, we use yv12 to display images.
Show images
Those above are simple. Now we need to display the image. Let's take a look at how to process completed frames. We replace the original RGB processing method with saveframe () as the code displayed on the screen. To display the data on the screen, we will first create an avpicture structure and set its Data Pointer and row size to serve our YUV coverage:
If (framefinished ){
Sdl_lockyuvoverlay (BMP );
Avpicture PICT;
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 );
}
First, we lock this overwrite because we will rewrite it. This is a good habit of avoiding problems in the future. As shown above, this avpicture struct has a Data Pointer pointing to a pointer with four elements. Because we are processing yuv420p, we only need three channels, that is, three groups of data. Other formats may require the fourth pointer to represent the alpha channel or other parameters. The row size is the same as its name. In YUV coverage, the structures with the same function are pixel and degree pitch. (To a certain extent, pitch is used in SDL to indicate the value of the specified row data width ). So what we are doing now is to make the three pointers in Pict. Data in our coverage have an address pointing to the necessary space. Similarly, we can directly obtain the row size information from overwrite. As before, we use img_convert to convert the format to pix_fmt_yuv420p.
Draw Images
However, we still need to tell SDL how to actually display the data we provide. We will also pass a rectangle parameter indicating the position, width, height, and zoom of the movie to the SDL function. In this way, SDL scales for us and it can be scaled quickly with the help of a video card.
Sdl_rect rect;
If (framefinished ){
// 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 );
}
Now our video is displayed!
Let's take a moment to look at the feature of SDL: its event-driven system. SDL is set to an event-driven method when you click or move the mouse in SDL or send a signal to it. If yourProgramTo process user input, it detects these events. Your program can also generate events and pass them to the SDL event system. This is quite useful when using SDL for multi-threaded programming. This code can be seen in guide 4. In this program, we will immediately poll the event after processing the package. Now, we will handle the sdl_quit event so that we can exit:
Sdl_event event;
Av_free_packet (& Packet );
Sdl_pollevent (& event );
Switch (event. Type ){
Case sdl_quit:
Sdl_quit ();
Exit (0 );
Break;
Default:
Break;
}
Let's remove the old redundant code and start compilation. If you are using Linux or its variants, the best way to compile with the SDL library is:
Gcc-O tutorial02 tutorial02.c-lavutil-lavformat-lavcodec-LZ-LM \
'Sdl-config-cflags-libs'
The SDL-config command prints the appropriate parameters for GCC compilation containing the correct SDL library. For compilation, you may need to do something different on your own platform: Please refer to the part about your system in the SDL document. Once compiled, run it immediately.
What will happen when you run this program? The movie is crazy! In fact, we only display the frames of all movies as quickly as we can decode the frames from the files. Now we don't have any code to calculate when we need to display the frame of a movie. Finally (in tutorial 5), we will spend enough time exploring synchronization issues. But in the beginning, we will ignore this because we have more important things to deal with: audio!