H264 YUV420视频翻转

7 篇文章 0 订阅
4 篇文章 0 订阅

h264解码后视频翻转,基于SDL overlay.

翻转YUV420:

 

void TurnPlanar(const unsigned char *srcp_y, unsigned char *dstp_y,
			  const unsigned char *srcp_u, unsigned char *dstp_u,
			  const unsigned char *srcp_v, unsigned char *dstp_v,
			  const int rowsize, const int height,
			  const int rowsizeUV, const int heightUV,
			  const int src_pitch_y, const int dst_pitch_y,
			  const int src_pitch_u, const int dst_pitch_uv,
			  const int src_pitch_v, const int direction)
{
	/*++++++++++++++++++++++++++++++++++++++++++
	(0,0)************************>x
	*
	*
	*
	*
	*
	*
	>
	y
	++++++++++++++++++++++++++++++++++++++++++++*/
	int y, x, offset;
	if (direction == 1) // Right
	{
		for(y=0; y<height; y++)
		{
			offset = height-1-y;
			for (x=0; x<rowsize; x++)
			{
				dstp_y[offset] = srcp_y[x];
				offset += dst_pitch_y;
			}
			srcp_y += src_pitch_y;
		}
		for(y=0; y<heightUV; y++)
		{
			offset = heightUV-1-y;
			for (x=0; x<rowsizeUV; x++)
			{
				dstp_u[offset] = srcp_u[x];
				dstp_v[offset] = srcp_v[x];
				offset += dst_pitch_uv;
			}
			srcp_u += src_pitch_u;
			srcp_v += src_pitch_v;
		}
	}
	else if (direction == -1) // Left
	{
		srcp_y += rowsize-1;
		for(y=0; y<height; y++)
		{
			offset = y;
			for (x=0; x<rowsize; x++)
			{
				dstp_y[offset] = srcp_y[-x];
				offset += dst_pitch_y;
			}
			srcp_y += src_pitch_y;
		}
		srcp_u += rowsizeUV-1;
		srcp_v += rowsizeUV-1;
		for(y=0; y<heightUV; y++)
		{
			offset = y;
			for (x=0; x<rowsizeUV; x++)
			{
				dstp_u[offset] = srcp_u[-x];
				dstp_v[offset] = srcp_v[-x];
				offset += dst_pitch_uv;
			}
			srcp_u += src_pitch_u;
			srcp_v += src_pitch_v;
		}
	}
	else // 180
	{
		dstp_y += (height-1)*dst_pitch_y + (rowsize-1);
		for (y = 0; y<height; y++) {
			for (x = 0; x<rowsize; x++) {	
				dstp_y[-x] = srcp_y[x];
			}
			dstp_y -= dst_pitch_y;
			srcp_y += src_pitch_y;
		}
		dstp_u += (heightUV-1)*dst_pitch_uv + (rowsizeUV-1);
		dstp_v += (heightUV-1)*dst_pitch_uv + (rowsizeUV-1);
		for (y = 0; y<heightUV; y++) {
			for (x = 0; x<rowsizeUV; x++) {	
				dstp_u[-x] = srcp_u[x];
				dstp_v[-x] = srcp_v[x];
			}
			dstp_u -= dst_pitch_uv;
			dstp_v -= dst_pitch_uv;
			srcp_u += src_pitch_u;
			srcp_v += src_pitch_v;
		}
	}
}


应用部分:

在设置编码参数时。如果侧翻,需要将width 和 height互换

pImg_convert_ctx = sws_getContext(FrameData.width,FrameData.height, FrameData.pix_fmt ,\
			FrameData.height, FrameData.width, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);


 

AVPicture *pict = new AVPicture;
	pict->data[0] = bmp->pixels[0];
	pict->data[1] = bmp->pixels[2];
	pict->data[2] = bmp->pixels[1];
	pict->linesize[0] = bmp->pitches[0];
	pict->linesize[1] = bmp->pitches[2];
	pict->linesize[2] = bmp->pitches[1];

	// Convert the image into YUV format that SDL uses
	int nScale = sws_scale(pImg_convert_ctx, ((AVPicture *)FrameData.pFrame)->data, ((AVPicture *)FrameData.pFrame)->linesize,\
		                   0, FrameData.height, picttmp->data, picttmp->linesize);

	
	int pitch, pitchUV, align;
	align = screen->format->BitsPerPixel;
	pitch = (FrameData.width + align - 1)/align*align;
	pitchUV = (pitch+1)>>1;
	TurnPlanar(picttmp->data[0], pict->data[0], picttmp->data[1], pict->data[1], picttmp->data[2], pict->data[2],\
		FrameData.height, FrameData.width, FrameData.height/2, FrameData.width/2,\
		pitch, pitch, pitchUV, pitchUV, pitchUV, 1);//侧翻


 效果如下:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值