Zoom:
Using FFmpeg for image Data format conversion and image scaling applications, the main use of the Swscale.h file in the three functions, respectively:
struct Swscontext *sws_getcontext (int srcw, int srcH, enum Avpixelformat srcformat,
int dstw, int dsth, enum Avpixelfo Rmat Dstformat,
int flags, Swsfilter *srcfilter,
swsfilter *dstfilter, const double *param);
int Sws_scale (struct swscontext *c, const uint8_t *const srcslice[],
const int srcstride[], int srcslicey, int srcslic EH,
uint8_t *const dst[], const int dststride[]);
void Sws_freecontext (struct swscontext *swscontext);
The sws_getcontext function can be considered an initialization function, and its parameters are defined as:
int Srcw,int SrcH is the height and width of the original image data;
int Dstw,int Dsth is the height and width of the output image data;
Enum Avpixelformat Srcformat is the type of input and output image data; eg:av_pix_fmt_yuv420, pav_pix_fmt_rgb24;
The int flags is the scale algorithm type; eg:sws_bicubic, Sws_bicublin, Sws_point, Sws_sinc;
Swsfilter *srcfilter, Swsfilter *dstfilter,const double *param can be used without tube, all is null;
The sws_scale function is the execution function, and its parameter definitions are:
struct Swscontext *c is the value returned by the Sws_getcontext function;
Const uint8_t *const srcslice[],uint8_t *const dst[] is an array of buffer pointers for each color channel of the input and output image data;
the const int Srcstride[],const int dststride[] is an array of bytes stored per row for each color channel of the input and output image data;
The int srcslicey is a progressive scan starting from the number of columns of the input image data, usually set to 0;
The int SRCSLICEH is the number of rows to be scanned, usually the height of the input image data;
The sws_freecontext function is the End function, and its parameters are the values returned by the Sws_getcontext function;
To do an actual scaling YUV420 function Packaging example is as follows:
int scaleimg (avcodeccontext *pcodecctx,avframe *src_picture,avframe *dst_picture,int nDstH, int nDstW) {int i; int NSRC
STRIDE[3];
int ndststride[3];
int nsrch = pcodecctx->height;
int NSRCW = pcodecctx->width;
struct swscontext* m_pswscontext;
uint8_t *psrcbuff[3] = {Src_picture->data[0],src_picture->data[1], src_picture->data[2]};
Nsrcstride[0] = NSRCW;
NSRCSTRIDE[1] = NSRCW/2;
NSRCSTRIDE[2] = NSRCW/2;
Dst_picture->linesize[0] = NDSTW;
DST_PICTURE->LINESIZE[1] = NDSTW/2;
DST_PICTURE->LINESIZE[2] = NDSTW/2;
printf ("nsrcw%d\n", NSRCW); M_pswscontext = Sws_getcontext (NSRCW, Nsrch, pix_fmt_yuv420p, NDSTW, Ndsth, pix_fmt_yuv420p, SWS_BICUBIC, NULL, NULL, NUL
L);
if (NULL = = M_pswscontext) {printf ("FFmpeg get Context error!\n"); exit (-1);} Sws_scale (M_pswscontext, src_picture->data,src_picture->linesize, 0, pcodecctx->height,dst_picture->
Data,dst_picture->linesize); printf ("line0:%d line1:%d line2:%d\n", Dst_picture->lineSize[0], dst_picture->linesize[1], dst_picture->linesize[2]);
Sws_freecontext (M_pswscontext);
return 1; }
The function is simple, apply the environment initial pointer, after scaling can. Read this article friend, this function can be copied directly using Yo. If in doubt can leave a message or e-mail: leoluopy@gmail.com
The RGB scaling can be referred to below:
int Scaleyuvimgtorgb (int nsrcw,int nsrch, uint8_t* src_data,int *linesize,int ndstw, int ndsth) {int i; INT ret ;
FILE *nrgb_file;
Avframe *ndst_picture;
struct swscontext* m_pswscontext;
Ndst_picture = Avcodec_alloc_frame ();
if (!ndst_picture) {printf ("Ndst_picture avcodec_alloc_frame failed\n");
Exit (1); } if (Avpicture_alloc ((avpicture *) NDST_PICTURE,PIX_FMT_RGB24,NDSTW, ndsth) <0) {printf ("Dst_picture Avpictur
E_alloc failed\n ");
Exit (1); } M_pswscontext = Sws_getcontext (NSRCW, Nsrch, pix_fmt_yuv420p, NDSTW, Ndsth, Pix_fmt_rgb24, SWS_B
Icubic, NULL, NULL, NULL);
if (NULL = = M_pswscontext) {printf ("FFmpeg get Context error!\n");
Exit (-1);
} ret = Sws_scale (M_pswscontext, Src_data,linesize, 0,nsrch,ndst_picture->data,ndst_picture->linesize); Nrgb_file = fopen ("..
\\YUV_STREAM\\RGBFile.rgb "," ab+ "); Fwrite (ndst_pIcture->data[0],ndstw*ndsth*3,1,nrgb_file);
Fclose (Nrgb_file);
Sws_freecontext (M_pswscontext);
Avpicture_free ((avpicture *) ndst_picture);
return 0; }
parameter data and linesize reference YUV plane pointers.
At the same time, if you do not want to use the avpicture structure, you can refer to the following: (note the different image types, linesize must write the right)
char* h264decoder_c::scaleyuvimgtorgb (int nsrcw,int nsrch, uint8_t** src_data,int *linesize,int nDstW, int nDstH)
{
int i; int ret; FILE *nrgb_file;
struct swscontext* m_pswscontext;
char* out_img[3];
int out_linesize[3];
Out_linesize[0] = 2*NDSTW; OUT_LINESIZE[1] = NDSTW; out_linesize[2] = NDSTW;
Out_img[0] = g_rgbimg;
M_pswscontext = Sws_getcontext (NSRCW, Nsrch, pix_fmt_yuv420p, NDSTW, Ndsth, pix_fmt_rgb565, SWS_BICUBIC,
null, NULL, NULL);
if (NULL = = M_pswscontext)
{
printf ("FFmpeg get Context error!\n");
Exit ( -1);
}
ret = Sws_scale (M_pswscontext, Src_data,linesize, 0,nsrch, (uint8_t**) out_img,out_linesize);