接上一篇blog: 同一路RTSP|RTMP流如何同时回调YUV和RGB数据实现渲染和算法分析

我们知道,由于解码后的YUV或RGB数据size比较大,如果想把转换后的RGB数据传给比如python算法的话,数据量还是挺大,为此,开发者提出这样的技术诉求,是不是可以按照指定的坐标和宽高,给出RGB数据,毕竟他们知道哪块区域是需要做算法分析的。

针对此种情况,我们的设计如下:

/*
 * SmartPlayer.cs
 * Author: https://daniusdk.com
 * WeChat: xinsheng120
 */
 
int x = 100;
int y = 100;
int clip_width = 960;
int clip_height = 540;

if (x < 0 || y < 0)
	return;

clip_width = (int)ByteAlign((UInt32)clip_width, 2);
clip_height = (int)ByteAlign((UInt32)clip_height, 2);

if (x + clip_width > video_frame.width_ || y + clip_height > video_frame.height_)
	return;

rgb_frame.format_ = (int)NT.NTSmartPlayerDefine.NT_SP_E_VIDEO_FRAME_FORMAT.NT_SP_E_VIDEO_FRAME_FORMAT_ARGB;
rgb_frame.width_ = clip_width;
rgb_frame.height_ = clip_height;

rgb_frame.timestamp_ = video_frame.timestamp_;
rgb_frame.stride0_ = clip_width * 4;
rgb_frame.stride1_ = 0;
rgb_frame.stride2_ = 0;
rgb_frame.stride3_ = 0;

Int32 argb_size = rgb_frame.stride0_ * rgb_frame.height_;

rgb_frame.plane0_ = Marshal.AllocHGlobal(argb_size);

IntPtr in_plane0 = video_frame.plane0_ + video_frame.stride0_ * y + x;
IntPtr in_plane1 = video_frame.plane1_ + video_frame.stride1_ * (y / 2) + (x / 2);
IntPtr in_plane2 = video_frame.plane2_ + video_frame.stride2_ * (y / 2) + (x / 2);

NTSmartPlayerSDK.NT_SP_I420ToARGB(in_plane0, video_frame.stride0_, in_plane1, video_frame.stride1_, in_plane2, video_frame.stride2_,
	rgb_frame.plane0_, rgb_frame.stride0_, clip_width, clip_height);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

比如我们需要截取的坐标x 100, y 100, 宽 960 高 540,那么处理后的数据,展示出来效果如右图:

RTSP|RTMP流如何指定坐标位置和分辨率获取RGB数据实时渲染和算法分析_rtsp回调yuv

左侧是原始的2560*1440的,右侧是按设定坐标,截取960*540分辨率的数据播放的,右侧的数据,投递给python或其他视觉算法,处理效率会显著提升。

以上是回调YUV数据的同时,按照用户使用场景,截取指定坐标和分辨率的RGB数据的设计参考。感兴趣的开发者,可以单独跟我沟通探讨。