FFmpeg scale and format convert YUV data

Source: Internet
Author: User
Tags printf
ffmpeg scale and format convert YUV data
The following code is a code that implements a change in the width or YUV format of YUV with FFmpeg.
For example: 352*288->720*576 (with double three or bilinear difference), yuv420p->yuv422p

int videoscaleyuvzoom (int is_flip,int in_width, int in_height,int in_pix_fmt, int out_width, int out_height,int ou t_pix_fmt,uint8_t * buf) {//////////////////////////////////////////////////////////////////////////AVFrame *
	Pinputframe = NULL;
	Avframe * Poutputframe = NULL; static int sws_flags = Sws_bicubic;
	Difference algorithm, dual three-time struct swscontext * img_convert_ctx = NULL;
	uint8_t * m_pvideooutput_zoombuf = NULL;

	int m_pvideooutput_zoomsize = 0; 
	Assign a avframe and set the default value-Enter Pinputframe = Avcodec_alloc_frame ();
		if (Pinputframe = = NULL) {printf ("Assign Pinputframe frame failed \ n");
	return-1; 
	}//Assign a avframe and set the default value-Output Poutputframe = Avcodec_alloc_frame ();
		if (Poutputframe = = NULL) {printf ("Failed to assign picture frame \ n");
	return-1;
	} m_pvideooutput_zoomsize = Avpicture_get_size ((avpixelformat) out_pix_fmt, out_width,out_height); M_pvideooutput_zoombuf = (uint8_t *) calloc (1,m_pvideooutput_zoomsize * 3 * sizeof (char)); The maximum allocated space, can meet the various formats of YUV Avpicture_fill ((avpicture *) Poutputframe, (Unsigned char *) M_pvideooutput_zoombuf, (Avpixelformat) out_pix_fmt,out_width, out_height); Memory Association//Set conversion context if (Img_convert_ctx = = NULL) {img_convert_ctx = Sws_getcontext (In_width, In_height, (AV
		PixelFormat) in_pix_fmt, Out_width, Out_height, (Avpixelformat) out_pix_fmt, sws_flags, NULL, NULL, NULL);
			if (Img_convert_ctx = = NULL) {fprintf (stderr, "Cannot initialize the conversion context\n");
		return-1; }}////////////////////////////////////////////////////////////////////////////Start Conversion Avpicture_fill ((Avpicture *) Pinputframe, (unsigned char *) buf, (Avpixelformat) in_pix_fmt,in_width, in_height);  Memory Association//Camera image inversion problem if (Is_flip) {if ((avpixelformat) in_pix_fmt = = Av_pix_fmt_bgr24 | | RGB Series (avpixelformat) in_pix_fmt = = av_pix_fmt_rgb24) {pinputframe->data[0] + = pinputframe->linesize[0]*
			(in_height-1);
		Pinputframe->linesize[0] *=-1; } else if ((avpixelformat) in_pix_fmt = = av_pix_fmt_yuv420p)//yuv420 Series {PinputFrame->data[0] + = pinputframe->linesize[0]* (in_height-1);
			Pinputframe->linesize[0] *=-1;
			PINPUTFRAME->DATA[1] + = pinputframe->linesize[1]* (IN_HEIGHT/2-1);
			PINPUTFRAME->LINESIZE[1] *=-1;
			PINPUTFRAME->DATA[2] + = pinputframe->linesize[2]* (IN_HEIGHT/2-1);
		PINPUTFRAME->LINESIZE[2] *=-1; } else {//extensible}} Sws_scale (Img_convert_ctx, Pinputframe->data, pinputframe->linesize, 0, I

	N_height, Poutputframe->data, poutputframe->linesize);

	Return the newly converted video to memcpy (buf,m_pvideooutput_zoombuf,m_pvideooutput_zoomsize); if (pinputframe) {Avcodec_free_frame (&
		Amp;pinputframe);
	Pinputframe = NULL;
		} if (Poutputframe) {avcodec_free_frame (&poutputframe);
	Poutputframe = NULL;
	} if (M_pvideooutput_zoombuf) {free (M_PVIDEOOUTPUT_ZOOMBUF);
		} if (Img_convert_ctx) {sws_freecontext (IMG_CONVERT_CTX);
	Img_convert_ctx = NULL;
}	return m_pvideooutput_zoomsize; }

Exchange please add QQ Group: 62054820
qq:379969650

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.