在GStreamer上更改视频格式

时间:2013-10-02 14:18:29

标签: c++ c video gstreamer image-resizing

我正在尝试在gstreamer-1.0上覆盖一个.png图像(带有alpha通道),用于我正在编写的应用程序,但是,经过大量搜索网络并阅读文档后,我仍然有点困惑使用的方法。

我使用的方法是:

filesrc - > pngdec - > imagefreeze - >视频转换 - >视频电视 - >视频混合器 - > output_of_bin input_of_bin - >视频转换 - >视频游戏 - > |

这是较大应用程序的一小部分。

我的问题是在videoscale和videoconvert元素上设置属性,我认为你应该使用GSTCaps,但是这不起作用。我尝试了很多方法。这是提供叠加层的类中的成员函数。我为代码的混乱而道歉,但它是实验性的。

bool ImageOverlay::CreateElement()
{
  GstPad *source_pad, *sink_pad;
  GstElement *file_source, *png_decoder, *freeze, *colorspace,
      *video_mixer, *identity, * colorspace2, *video_scale_1, *video_scale_2;

  /* Create gstreamer elements */
  _image_overlay_element = gst_bin_new("image-player");

  if(!_image_overlay_element)
  {
    g_printerr ("Pipeline could not be created. Exiting.\n");
    return false;
  }

  // Create file source
  file_source = gst_element_factory_make ("multifilesrc",  "file-source");
  g_object_set (G_OBJECT (file_source), "location", "../Template1.png", NULL);

  if(!file_source)
  {
    g_printerr ("File could not be created. Exiting.\n");
    return false;
  }

  // Decode file
  png_decoder  = gst_element_factory_make ("pngdec", "png-decoder");
  if(!png_decoder)
  {
    g_printerr ("png-decoder could not be created. Exiting.\n");
    return false;
  }

  // Create colour space convertor
  colorspace = gst_element_factory_make("videoconvert", "colorspace");
  if(!colorspace)
  {
    g_printerr ("Colorspace could not be created. Exiting.\n");
    return false;
  }

  // Create colour space convertor
  colorspace2 = gst_element_factory_make("videoconvert", "colorspace2");
  if(!colorspace)
  {
    g_printerr ("Colorspace could not be created. Exiting.\n");
    return false;
  }

  // Create idenentiy component, does nothing but is useful for debug
  identity = gst_element_factory_make("identity", "identity");
  if(!identity)
  {
    g_printerr ("Colorspace could not be created. Exiting.\n");
    return false;
  }

  // Create a video mixer
  video_mixer = gst_element_factory_make("videomixer", "mixer");
  if(!video_mixer)
  {
    g_printerr ("videomixer could not be created. Exiting.\n");
    return false;
  }

  // Create a freeze element
  freeze = gst_element_factory_make("imagefreeze", "freeze");
  if(!freeze)
  {
    g_printerr ("ImageFreeze could not be created. Exiting.\n");
    return false;
  }

  video_scale_1 = gst_element_factory_make("videoscale", NULL);
  if(!video_scale_1)
  {
    g_printerr ("video_scale_1 could not be created. Exiting.\n");
    return false;
  }

  video_scale_2 = gst_element_factory_make("videoscale", NULL);
  if(!video_scale_2)
  {
    g_printerr ("video_scale_2 could not be created. Exiting.\n");
    return false;
  }

  // Add to the bin
  gst_bin_add_many (GST_BIN (_image_overlay_element),
                    file_source,
                    png_decoder,
                    colorspace,
                    colorspace2,
                    freeze,
                    video_mixer,
                    identity,
                    video_scale_1,
                    video_scale_2,
                    NULL);


  if (!gst_element_link_many (file_source, png_decoder, video_scale_1, freeze, NULL))
  {
    std::cout << "Could not link" << std::endl;
  }

  GstCaps * caps = gst_caps_new_simple ("video/x-raw",
                                        "format", G_TYPE_STRING, "ARGB",
                                        NULL);


  GstCaps * caps2 = gst_caps_new_simple("width", G_TYPE_INT, 640,
                                        "height", G_TYPE_INT, 480,
                                        NULL);

  gst_element_link_filtered(video_scale_1, freeze, caps2);
  gst_element_link_filtered(video_scale_2, freeze, caps2);

  if (!gst_element_link_many(freeze, video_scale_2, colorspace, NULL))
  {
    std::cout << "Could not link" << std::endl;
  }

  // Link pads to video mixer
  GstPad * sink_pad_1 = gst_element_get_request_pad (video_mixer, "sink_%u");
  GstPad * sink_pad_2 = gst_element_get_request_pad (video_mixer, "sink_%u");
  GstPad * source_2 = gst_element_get_static_pad (identity, "src");
  GstPad * source_1 = gst_element_get_static_pad (colorspace, "src");

  gst_pad_set_caps(source_2, caps);
  gst_pad_set_caps(source_1, caps);
  gst_caps_unref(caps);

  if (!gst_pad_link ( source_1, sink_pad_1) == GST_PAD_LINK_OK)
  {
    std::cout << "Could not link pads" << std::endl;
    return false;
  }

  if (!gst_pad_link ( source_2, sink_pad_2) == GST_PAD_LINK_OK)
  {
    std::cout << "Could not link pads" << std::endl;
    return false;
  }

  gst_pad_set_active(source_1, true);
  gst_pad_set_active(sink_pad_1, true);
  gst_pad_set_active(source_2, true);
  gst_pad_set_active(sink_pad_2, true);

  // Add pad to this element
  source_pad = gst_element_get_static_pad (video_mixer, "src");
  sink_pad = gst_element_get_static_pad(identity, "sink");
  gst_element_add_pad (_image_overlay_element, gst_ghost_pad_new ("src", source_pad));
  gst_element_add_pad (_image_overlay_element, gst_ghost_pad_new ("sink", sink_pad));

  return true;
}

我想让两个来源将它们调整为相同的大小,并在视频上叠加png图像。该块主要起作用,但我无法获得调整大小或alpha通道的工作。

谢谢,

修改

我现在已经修改了我的代码并稍微理解了一下,但似乎问题是,如果使用RGBA,封顶过滤器将不会链接,其他一些格式可以正常工作但没有用。我不明白为什么。

bool ImageOverlay::MakeElements()
{
  try
  {
    // Creates the bin at this level
    CreatePipeline();
    GstElement *file_source, *png_decoder, *freeze, *colorspace,
        *video_mixer, *identity, * colorspace2, *video_scale_1, *video_scale_2, *caps_filter1, *caps_filter2;

    // Create elements
    file_source = CreateElementAndAddToBin("multifilesrc",  "file-source");
    png_decoder   = CreateElementAndAddToBin("pngdec", "png-decoder");
    colorspace    = CreateElementAndAddToBin("videoconvert", "colorspace");
    colorspace2   = CreateElementAndAddToBin("videoconvert", "colorspace2");
    identity      = CreateElementAndAddToBin("identity", "identity");
    video_mixer   = CreateElementAndAddToBin("videomixer", "mixer");
    freeze        = CreateElementAndAddToBin("imagefreeze", "freeze");
    video_scale_1 = CreateElementAndAddToBin("videoscale", "scale1");
    video_scale_2 = CreateElementAndAddToBin("videoscale", "scale2");
    caps_filter1  = CreateElementAndAddToBin("capsfilter", "caps_filter1");
    caps_filter2  = CreateElementAndAddToBin("capsfilter", "caps_filter2");

    // Set input file
    g_object_set (G_OBJECT (file_source), "location", "../Template1.png", NULL);

    // Create caps
    GstCaps *caps = gst_caps_new_simple ("video/x-raw",
                                         "format", G_TYPE_STRING, "YV12", //<<< IF THIS IS SET TO ARGB (THE FORMAT I WANT IT FAILS ON LINKING)
                                         "framerate", GST_TYPE_FRACTION, 25, 1,
                                         "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
                                         "width", G_TYPE_INT, 640,
                                         "height", G_TYPE_INT, 480,
                                         NULL);


    // Set caps on cap filter
    g_object_set(G_OBJECT(caps_filter2), "caps", caps, NULL);
    g_object_set(G_OBJECT(caps_filter1), "caps", caps, NULL);


    // Link up elements
    if (!gst_element_link_many (identity, video_scale_2, colorspace2, caps_filter2, video_mixer, NULL))
    {
      std::cout << "Could not link input branch" << std::endl;  //<<<< IF ARGB OR OTHER FORMATS SUCH AS RGBA ARE USED
    }                                                           // IN CAP FILTER, THIS FAILS, SAME WITH BELOW. IT WORKS WITH YV12!

    if (!gst_element_link_many (file_source, png_decoder, freeze, video_scale_1, colorspace, caps_filter1, video_mixer, NULL))
    {
      std::cout << "Could not link image source branch" << std::endl;
    }

    // Add ghost pads to this bin, allowing it to be used as an element
    SetInputElement(identity);
    SetOutputElement(video_mixer);
  }
  catch(...)
  {
    std::cout << "Overlay generator failed to build, exception thrown" << std::endl;

    // TODO - Clear up in here
    return false;
  }

  return true;
}

1 个答案:

答案 0 :(得分:2)

从gstreamer 1.2开始,混音器会假设视频转换,如果其中一个接收器具有alpha值,它将自动选择alpha格式。

如果您可以更新到1.2,那么您需要使用的管道将具有以下形式:

gst-launch-1.0 uridecodebin uri=file:///home/meh/Pictures/pitivi/nicetext.png ! imagefreeze ! videomixer name=m sink_0::zorder=0 ! videoconvert ! autovideosink videotestsrc ! videoscale ! video/x-raw, width=640, height=400 ! m.

其中nicetext.png是一个(png)640 x 400图像文件,带有透明背景和一些随机文本。

视频游戏之后的部分是一个封顶过滤器,您可以使用:

创建它

gst_element_factory_make("capsfilter", "random_name")

然后使用您需要的宽度和高度设置其caps属性。如果事先不知道这一点,则需要在垫上设置垫探针,从中需要复制宽度和高度,拦截大写事件并在此时设置大写过滤器上限。

如果由于某种原因你不能使用1.2,那么你需要在两个分支上都有一个视频转换和一个封顶过滤器,并指定你需要的格式。