The simplest example of ffmpeg based Libswscale (YUV to RGB)

Source: Internet
Author: User
Tags color gamut

Turn from: http://blog.csdn.net/leixiaohua1020/article/details/42134965

=====================================================

The simplest list of ffmpeg-based Libswscale examples series:

The simplest example of ffmpeg based Libswscale (YUV to RGB)

The simplest example of an ffmpeg-based Libswscale attachment: Testing the picture generation tool

=====================================================

This article records an example of a libswscale based on FFmpeg. The Libswscale implements a variety of image Pixel format transformations, such as the conversion between YUV and RGB, and image size scaling (for example, 640x360 stretching to 1280x720) function. And Libswscale also made the corresponding instruction set optimization, so its conversion efficiency than the written C language conversion efficiency is much higher.

The program documented in this article converts the pixel format to yuv420p, 480x272 video to pixel format RGB24, and 1280x720 resolution.


Process Simple initialization method Libswscale easy to use, the main function is only 3:
(1)       Sws_g Etcontext (): Initializes the SWSCONTEXT structure with a parameter.
(2)       Sws_scale (): Converts a frame of image.
(3)       Sws_freecontext (): Release swscontext structure.
where Sws_getcontext () can also be replaced with another interface function Sws_getcachedcontext ().
 
Complex but more flexible initialization methods initialization Swscontext There is another method, more flexible, than calling Sws_getcontext () to configure more parameters. The function called by the method is shown below.
(1)       Sws_alloc_context (): Allocates memory for swscontext structure.
(2)       AV_OPT_SET_XXX (): Through Av_opt_set_int (), Av_opt_set () ... And so on. A series of methods set the value of the SWSCONTEXT structure. It is important to note here that the definition of the SWSCONTEXT structure is not visible, so the member variables cannot be assigned directly to them, and they must be assigned through APIs such as Av_opt_set ().
(3)       Sws_init_context (): Initializes the SWSCONTEXT structure.
This complex method can be configured with some sws_getcontext () parameters that cannot be configured. For example, set the image of the YUV pixel range is the JPEG standard (Y, U, v range is 0-255) or MPEG standard (Y range is 16-235,u, v range is 16-240).
 
several knowledge points

Several knowledge points in image pixel data processing are recorded below: pixel format, image stretching, YUV pixel range, color gamut. The knowledge of pixel format pixel format has previously been recorded and will not be repeated. Here's a note of the ffmpeg supported pixel format. There are a few things to note:
(1) All pixel formats are named "Av_pix_fmt_"

(2) after the pixel format name has "P", the representative is the planar format, otherwise it is the packed format. The different components of the planar format are stored in different arrays, for example, the av_pix_fmt_yuv420p storage method is as follows: Data[0]: Y1, Y2, Y3, Y4, Y5, Y6, Y7 ...
DATA[1]: U1, U2, U3, U4 ...

DATA[2]: V1, V2, V3, V4 ...

The data in the packed format is stored in the same array, for example, the Av_pix_fmt_rgb24 storage method is as follows: Data[0]: R1, G1, B1, R2, G2, B2, R3, G3, B3, R4, G4, B4 ...

(3) After the pixel format name has "be", the representative is the big endian format, the name has "LE", the representative is the little endian format.

The ffmpeg-supported pixel format is defined in Libavutil\pixfmt.h and is an enumerated type called Avpixelformat, as shown below.
[CPP]  View Plain  copy  /**   * Pixel format.   *   *  @note & nbsp  * av_pix_fmt_rgb32 is handled in an endian-specific manner. an  RGBA   * color is put together as:   *   (a  << 24)  |  (r << 16)  |  (g << 8)  | b    * This is stored as BGRA on little-endian CPU  architectures and argb on   * big-endian CPUs.   *   *   @par    * When the pixel format is palettized RGB  (av_ PIX_FMT_PAL8), the palettized   * image data is stored in  avframe.data[0]. the palette is transported in   * AVFrame.data[1],  is 1024 bytes long  (256 4-byte entries)  and is   * formatted  the same as in av_pix_fmt_rgb32 described above  (I.e., it is    * also endian-specific).  note also that the individual rgb  palette   * components stored in AVFrame.data[1] should be  in the range 0..255.   * This is important as many  custom pal8 video codecs that were designed   * to run  On the ibm vga graphics adapter use 6-bit palette components.    *   *  @par    * for all the 8bit per pixel  formats, an RGB32 palette is in data[1] like   * for  pal8. this palette is filled in automatically by the function   *  allocating the picture.   *   *  @note    * Make sure  that all newly added big-endian formats have  (pix_fmt & 1)  == 1   * and that all newly added little-endian formats  have  (pix_fmt & 1)  == 0.   * this allows simpler  detection of big vs little-endian.   */   Enum avpixelformat  {       AV_PIX_FMT_NONE = -1,        av_pix_fmt_yuv420p,   ///< planar yuv 4:2:0, 12bpp,  (1 Cr  & cb sample per 2x2 y samples)        av_ pix_fmt_yuyv422,   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.