+static void
+gst_video_overlay_rectangle_convert (GstVideoInfo * src, GstBuffer * src_buffer,
+ GstVideoFormat dest_format, GstVideoInfo * dest, GstBuffer ** dest_buffer)
+{
+ gint width, height, stride;
+ GstVideoFrame src_frame, dest_frame;
+ GstVideoFormat format;
+ gint k, l;
+ guint8 *sdata, *ddata;
+
+ format = GST_VIDEO_INFO_FORMAT (src);
+
+ width = GST_VIDEO_INFO_WIDTH (src);
+ height = GST_VIDEO_INFO_HEIGHT (src);
+
+ gst_video_info_init (dest);
+ gst_video_info_set_format (dest, dest_format, width, height);
+
+ *dest_buffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (dest));
+
+ gst_video_frame_map (&src_frame, src, src_buffer, GST_MAP_READ);
+ gst_video_frame_map (&dest_frame, dest, *dest_buffer, GST_MAP_WRITE);
+
+ sdata = GST_VIDEO_FRAME_PLANE_DATA (&src_frame, 0);
+ ddata = GST_VIDEO_FRAME_PLANE_DATA (&dest_frame, 0);
+ stride = GST_VIDEO_FRAME_PLANE_STRIDE (&src_frame, 0);
+
+ if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV &&
+ dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB) {
+ gint ayuv;
+ gint a, y, u, v, r, g, b;
+
+ for (k = 0; k < height; k++) {
+ for (l = 0; l < width; l++) {
+ ayuv = GST_READ_UINT32_BE (sdata);
+ a = ayuv >> 24;
+ y = (ayuv >> 16) & 0xff;
+ u = (ayuv >> 8) & 0xff;
+ v = (ayuv & 0xff);
+
+ r = (298 * y + 459 * v - 63514) >> 8;
+ g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
+ b = (298 * y + 541 * u - 73988) >> 8;
+
+ r = CLAMP (r, 0, 255);
+ g = CLAMP (g, 0, 255);
+ b = CLAMP (b, 0, 255);
+
+ /* native endian ARGB */
+ *ddata = ((a << 24) | (r << 16) | (g << 8) | b);
+
+ sdata += 4;
+ ddata += 4;
+ }
+ sdata += stride - 4 * width;
+ }
+ } else if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB &&
+ dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV) {
+ gint argb;
+ gint a, y, u, v, r, g, b;
+
+ for (k = 0; k < height; k++) {
+ for (l = 0; l < width; l++) {
+ /* native endian ARGB */
+ argb = *sdata;
+ a = argb >> 24;
+ r = (argb >> 16) & 0xff;
+ g = (argb >> 8) & 0xff;
+ b = (argb & 0xff);
+
+ y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
+ u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
+ v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
+
+ y = CLAMP (y, 0, 255);
+ u = CLAMP (u, 0, 255);
+ v = CLAMP (v, 0, 255);
+
+ GST_WRITE_UINT32_BE (ddata, ((a << 24) | (y << 16) | (u << 8) | v));
+
+ sdata += 4;
+ ddata += 4;
+ }
+ sdata += stride - 4 * width;
+ }
+ } else {
+ GST_ERROR ("unsupported conversion");
+ g_assert_not_reached ();
+ }
+
+ gst_video_frame_unmap (&src_frame);
+ gst_video_frame_unmap (&dest_frame);
+}
+