Swscale usage in FFMPEG

Source: Internet
Author: User

From http://blog.csdn.net/bweaglegao/article/details/8540860

This article was found due to swscale usage problems during FFMPEG porting. Although the article has been around for a long time, there are still many references. Thank you ".

From: http://guguclock.blogspot.com/2009/12/ffmpeg-swscale.html

If you want to merge a pixelformat into another pixelformat, for example, convert yuv420p to yuyv422, or change the size of the parameter, you can use swscale.

The list of pixelformats is defined in libavutil/pixfmt. h.

The use of swscale can refer to the libswscale/swscale-example.c sample code. There are three main functions
Sws_getcontext ()
Sws_scale ()
Sws_freecontext ()

We can regard sws_getcontext () as the initialization function and sws_freecontext () as the end function. These two functions can be split at the beginning and end of each line.

The main function is sws_scale ().

The declaration of sws_getcontext () is as follows:

Swscontext * sws_getcontext (INT srcw, int srch, Enum pixelformat srcformat, int dstw, int dsomething, Enum pixelformat dstformat, int flags, swsfilter * handle, swsfilter * dstfilter, const double * PARAM)

There are a total of 10 records, of which the first seven are important;
The first three regions represent the region, height, and pixelformat of the source;
The four to six regions represent the region, height, and pixelformat of destination;
The seventh metric represents the scale method to be used. The available methods can be found in libswscale/swscale. h.

The last three values can be null if they are not used.

Sws_getcontext returns a swscontext struct. we can regard this struct as a handler, which will be used in both sws_scale and sws_freecontext.

The following is a simple example of sws_getcontext:

Struct swscontext * img_convert_ctx;
Img_convert_ctx = sws_getcontext (in_width, in_height, pix_fmt_yuv420p,
Out_width, out_height, pix_fmt_yuv420p, sws_point,
Null, null, null );

At the beginning, we declared img_convert_ctx as a pointer pointing to swscontext. Then, we sent the return value of sws_getcontext to img_convert_ctx.

Note the number of records in sws_getcontext. The in_width and in_height values indicate the values and heights of the source. The out_width and out_height values indicate the values and heights of the created values; the pixelformat of input and output is yuv420p; The sws_point scale method is used.

After Initialization is complete, major scale operations will be performed. We have done this through sws_scale. The announcement of sws_scale () is as follows:

Int sws_scale (swscontext * C, uint8_t * SRC [], int srcstride [], int srcslicey, int srcsliceh, uint8_t * DST [], int dststride [])

There are a total of seven shards;
The first vertex number is the vertex number obtained by sws_getcontext.
The second SRC and sixth DST point to the buffer of the input and output respectively.
The third srcstride and the seventh dststride point to the stride of the input and output separately. If you do not know what the stride is, you can first regard it as the byte number of each column.
The fourth srcslicey refers to the location where the first column is to be processed. Here I am from the beginning, so enter 0 directly. If you want to know more about swscale. H, you can explain it.
The fifth srcsliceh refers to the source slice height.

An example is as follows:

Sws_scale (img_convert_ctx, inbuf, inlinesize, 0, in_height, outbuf, outlinesize );

This should be better understood than others. You can refer to the data description above.

Finally, call sws_freecontext () to end the process. The usage is very simple. Enter the number of records obtained by sws_getcontext. As follows:

Sws_freecontext (img_convert_ctx );

Finally, sort it out again. To use swscale, you only need to use sws_getcontext () for initialization, and sws_scale () for major vertex encryption and sws_freecontext () to complete all the operations.

The following is a simple sort routine, which can be used to retrieve the first batch from the foreman. YUV and save it as another batch.

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

/*
* You need to set srcfile, dstfile, and persistent storage.
* Link libswscale is required.
* There are mainly three functions.
* Sws_getcontext () is used for initial, and sws_freecontext () is used for end
* Sws_scale () is the main function
* The setting only changes the first batch of YUV. If you want to adjust the entire batch, you can remove the decoding of the decoding loop.
*/

# Include "libswscale/swscale. H"

# Define srcfile "foreman_cif.yuv"
# Define dstfile "Out. YUV"

Int main ()
{
// Set the length of the original YUV
Const int in_width = 352;
Const int in_height = 288;
// Set the length of the target YUV
Const int out_width = 640;
Const int out_height = 480;

Const int read_size = in_width * in_height * 3/2;
Const int write_size = out_width * out_height * 3/2;
Struct swscontext * img_convert_ctx;
Uint8_t * inbuf [4];
Uint8_t * outbuf [4];
Int inlinesize [4] = {in_width, in_width/2, in_width/2, 0 };
Int outlinesize [4] = {out_width, out_width/2, out_width/2, 0 };

Uint8_t in [352*288*3> 1];
Uint8_t out [640*480*3> 1];

File * fin = fopen (srcfile, "rb ");
File * fout = fopen (dstfile, "WB ");

If (fin = NULL ){
Printf ("Open input file % s error. \ n", srcfile );
Return-1;
}

If (fout = NULL ){
Printf ("Open output file % s error. \ n", dstfile );
Return-1;
}

Inbuf [0] = malloc (in_width * in_height );
Inbuf [1] = malloc (in_width * in_height> 2 );
Inbuf [2] = malloc (in_width * in_height> 2 );
Inbuf [3] = NULL;

Outbuf [0] = malloc (out_width * out_height );
Outbuf [1] = malloc (out_width * out_height> 2 );
Outbuf [2] = malloc (out_width * out_height> 2 );
Outbuf [3] = NULL;

// ********* Initialize software scaling *********
// ********** Sws_getcontext **********************
Img_convert_ctx = sws_getcontext (in_width, in_height, pix_fmt_yuv420p,
Out_width, out_height, pix_fmt_yuv420p, sws_point,
Null, null, null );
If (img_convert_ctx = NULL ){
Fprintf (stderr, "Cannot initialize the conversion context! \ N ");
Return-1;
}

Fread (in, 1, read_size, fin );

Memcpy (inbuf [0], in, in_width * in_height );
Memcpy (inbuf [1], in + in_width * in_height, in_width * in_height> 2 );
Memcpy (inbuf [2], in + (in_width * in_height * 5> 2), in_width * in_height> 2 );

// ********** Main function ******
// ********** Sws_scale ************
Sws_scale (img_convert_ctx, inbuf, inlinesize,
0, in_height, outbuf, outlinesize );

Memcpy (Out, outbuf [0], out_width * out_height );
Memcpy (out + out_width * out_height, outbuf [1], out_width * out_height> 2 );
Memcpy (out + (out_width * out_height * 5> 2), outbuf [2], out_width * out_height> 2 );

Fwrite (Out, 1, write_size, fout );

// ********* End function *******
// ********** Sws_freecontext *******
Sws_freecontext (img_convert_ctx );

Fclose (FIN );
Fclose (fout );

Return 0;
}

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

The result of the following two rows

Input Image

Output Image

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.