Use FFmpeg for image format conversion and image scaling/sws_scale/linux/c++/c/rgb-yuv420__linux

Source: Internet
Author: User

The use of ffmpeg for image Data format conversion and image scaling applications, mainly used in the Swscale.h file three functions, respectively:

struct Swscontext *sws_getcontext (int srcw, int srcH, enum Avpixelformat Srcformat,
int DSTW, int dsth, enum Avpixelformat Dstformat,
int flags, Swsfilter *srcfilter,
Swsfilter *dstfilter, const double *param);

int Sws_scale (struct swscontext *c, const uint8_t *const),

const int srcstride[], int srcslicey, int srcsliceh,
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 parameter definitions are:

int Srcw,int SrcH is the height and width of the original image data;

int Dstw,int Dsth is high and wide for output image data;

Enum Avpixelformat Srcformat is the type of input and output picture data; eg:av_pix_fmt_yuv420, pav_pix_fmt_rgb24;

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 an execution function, and its parameter definitions are:

struct Swscontext *c is the value returned by the Sws_getcontext function;

The 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;

const int Srcstride[],const int dststride[] An array of bytes stored per line for each color channel of the input and output image data;

int Srcslicey is a line-by-row scan from the first number of input image data, usually set to 0;

int SRCSLICEH is the number of rows that need to be scanned, usually the height of the input image data;

the sws_freecontext function is the End function, and its parameter is the value returned by the Sws_getcontext function;

Sample code: rgb24_2_rgb24/1600*1200-352*288

. h

#ifndef int64_c
#define Int64_c
#define Uint64_c
#endif

extern "C"
{
#include "libswscale/ Swscale.h "
}
struct imginfo
{
    unsigned int height;
    unsigned int width;
    unsigned long buffersize;
    unsigned char *bufferptr;
} Rawdate;

. cpp

    int nsrch = Rawdata.height;   1200 int NSRCW = Rawdata.width;   1600 int ndsth = Resized_hight;   258 int NDSTW = resized_width; 352 uint8_t *psrcbuff[3] = {rawdata.bufferptr, rawdata.bufferptr + NSRCW * nsrch, rawdata.bufferptr + NSrcW * n
    SrcH * 2}; uint8_t *pdstbuff[3] = {presizeddata->bufferptr, presizeddata->bufferptr + NDSTW * NDstH, pResizedData->
       
    Bufferptr + NDSTW * ndsth * 2};
    int nsrcstride[3];
    
    int ndststride[3];
    	for (int i=0; i<3; i++) {Nsrcstride[i] = NSRCW * 3;
    Ndststride[i] = NDSTW * 3;
    
    } swscontext* M_pswscontext; M_pswscontext = Sws_getcontext (NSRCW, Nsrch, Av_pix_fmt_rgb24, NDSTW, Ndsth, Av_pix_fmt_
                                  
    RGB24, sws_sinc, NULL, NULL, NULL);
    	if (NULL = = M_pswscontext) {printf ("FFmpeg get Context error!\n"); RetUrn false;
              
    Sws_scale (M_pswscontext, Psrcbuff, nsrcstride, 0, Nsrch, Pdstbuff, ndststride); Sws_freecontext (M_pswscontext);

Sample code: rgb24_2_yuv420/1600*1200-352*288

. cpp

    int nsrch = rawdata->height;    1200 int NSRCW = rawdata->width;    1600 int ndsth = Resized_hight;    The resized_width int NDSTW =;  352 uint8_t *psrcbuff[3] = {rawdata->bufferptr, rawdata->bufferptr + NSRCW * nsrch, Rawdata->bufferptr
    + NSRCW * Nsrch * 2}; uint8_t *pdstbuff[3] = {resizedyuvdata->bufferptr, resizedyuvdata->bufferptr + NDSTW * NDstH, resizedYuvData->
       
    Bufferptr + NDSTW * ndsth * 5/4};
    int nsrcstride[3];
    
    int ndststride[3];
    for (int i=0; i<3; i++) {Nsrcstride[i] = NSRCW * 3;
    } ndststride[0] = NDSTW;
    NDSTSTRIDE[1] = NDSTW/2;
    
    NDSTSTRIDE[2] = NDSTW/2;
    
    swscontext* M_pswscontext; M_pswscontext = Sws_getcontext (NSRCW, Nsrch, Av_pix_fmt_rgb24, NDSTW, Ndsth, Av_pix_fmt_
                           yuv420p, sws_sinc, NULL, NULL, NULL);       
    if (NULL = = M_pswscontext) {printf ("FFmpeg get Context error!\n");
    return false;
              
    Sws_scale (M_pswscontext, Psrcbuff, nsrcstride, 0, Nsrch, Pdstbuff, ndststride);	 Sws_freecontext (M_pswscontext);




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.