The transplant ffmpeg process, encountered Swscale usage problem, so found this article. Although the article has been a long time, but there are a lot to learn from. Thank you for the "cuckoo Clock".
Transferred from: http://guguclock.blogspot.com/2009/12/ffmpeg-swscale.html
If you want to convert a pixelformat to another pixelformat, for example, convert yuv420p to YUYV422, or change the size of the map, you can use Swscale.
Among them, the list of PixelFormat is libavutil/pixfmt.h in the definition.
Swscale's usage can be used to refer to LIBSWSCALE/SWSCALE-EXAMPLE.C's sample code. There are three main function
Sws_getcontext ()
Sws_scale ()
Sws_freecontext ()
Among them, we can think of Sws_getcontext () as the initialization function, and Sws_freecontext () as the function of the bundle. These two functions can be executed at the beginning and end of each line.
The real 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 dsth, enum PixelFormat Dstformat, int flags, Swsfilter *srcfilter, Swsfilter *dstfilter, const double *param)
There are 10 parameters, of which the first seven are more important;
The first three parameters represent the width, height, and pixelformat of the source;
Four to six parameters represent the width, height and pixelformat of destination;
The seventh reference represents the method of which scale is to be used, and the available methods can be found in libswscale/swscale.h.
The last three parameters, if not used, can be filled with null.
Sws_getcontext will return a swscontext struct, and we can think of this struct as a handler, after which Sws_scale and Sws_freecontext are used.
Here is a simple example of a 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 proclaimed img_convert_ctx as a pointer to the Swscontext, and then we gave Img_convert_ctx the value of Sws_getcontext's return.
Note the parameters of Sws_getcontext; In_width and in_height represent the width and height of the source, and the Out_width and out_height represent the width and height of the conversion; input and output PixelFormat are yuv420p; Use the Sws_point scale method.
After the initialization is complete, the main scale action is then done; we complete it through Sws_scale. The declaration 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 parameters;
The first is the parameters obtained by Sws_getcontext.
The second SRC and sixth DST points point to the buffer of input and output.
The third srcstride and seventh dststride points to the input and output stride; If you don't know what a stride is, you can think of it as a byte in each column first.
The fourth Srcslicey, in the meaning of the remark, refers to the position of the first column to be dealt with; Here I am from the head, so I fill 0 directly. If you want to know more details, you can refer to the swscale.h.
The fifth SRCSLICEH refers to the height of the source slice.
An example is as follows
Sws_scale (Img_convert_ctx, Inbuf, inlinesize, 0, In_height, Outbuf, outlinesize);
This should be more understood, you can refer to the above parameters to explain.
Finally, after the whole deal, call Sws_freecontext (). The usage is very simple, fill in the parameters that the Sws_getcontext obtains. As follows
Sws_freecontext (IMG_CONVERT_CTX);
Finally, to use the Swscale, as long as the Sws_getcontext () in the initialization, Sws_scale () into the main conversion, Sws_freecontext () end, you can complete the action.
The following is a simple example program that takes the first image out of FOREMAN.YUV and then saves it to another map.
=====================================================================================
/*
* Need to set srcfile and Dstfile, long and wide information
* Need link Libswscale
* There are three main function
* Sws_getcontext () is used by initial, Sws_freecontext () is the end bundle
* Sws_scale () is the function of the main work
* The preset will only be converted to the first YUV, if you want to switch the entire file, you can remove the decoding loop
*/
#include "libswscale/swscale.h"
#define Srcfile "FOREMAN_CIF.YUV"
#define Dstfile "OUT.YUV"
int main ()
{
To set the length and width of the original YUV
const int in_width = 352;
const int in_height = 288;
Setting purpose YUV length and width
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);
The 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);
The function *******
Sws_freecontext *******
Sws_freecontext (IMG_CONVERT_CTX);
Fclose (Fin);
Fclose (Fout);
return 0;
}
=====================================================================================
The following two images perform the results
Input Image
Output Image