1 /* Small helper element for format conversion
2 * Copyright (C) 2005 Tim-Philipp Müller <tim centricular net>
3 * Copyright (C) 2010 Brandon Lewis <brandon.lewis@collabora.co.uk>
4 * Copyright (C) 2010 Edward Hervey <edward.hervey@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 caps_are_raw (const GstCaps * caps)
30 len = gst_caps_get_size (caps);
32 for (i = 0; i < len; i++) {
33 GstStructure *st = gst_caps_get_structure (caps, i);
34 if (gst_structure_has_name (st, "video/x-raw-yuv") ||
35 gst_structure_has_name (st, "video/x-raw-rgb"))
43 create_element (const gchar * factory_name, GstElement ** element,
46 *element = gst_element_factory_make (factory_name, NULL);
50 if (err && *err == NULL) {
51 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
52 "cannot create element '%s' - please check your GStreamer installation",
60 get_encoder (const GstCaps * caps, GError ** err)
62 GList *encoders = NULL;
63 GList *filtered = NULL;
64 GstElementFactory *factory = NULL;
65 GstElement *encoder = NULL;
68 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER |
69 GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE, GST_RANK_NONE);
71 if (encoders == NULL) {
72 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
73 "Cannot find any image encoder");
77 GST_INFO ("got factory list %p", encoders);
78 gst_plugin_feature_list_debug (encoders);
81 gst_element_factory_list_filter (encoders, caps, GST_PAD_SRC, FALSE);
82 GST_INFO ("got filtered list %p", filtered);
84 if (filtered == NULL) {
85 gchar *tmp = gst_caps_to_string (caps);
86 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
87 "Cannot find any image encoder for caps %s", tmp);
92 gst_plugin_feature_list_debug (filtered);
94 factory = (GstElementFactory *) filtered->data;
96 GST_INFO ("got factory %p", factory);
97 encoder = gst_element_factory_create (factory, NULL);
99 GST_INFO ("created encoder element %p, %s", encoder,
100 gst_element_get_name (encoder));
104 gst_plugin_feature_list_free (encoders);
106 gst_plugin_feature_list_free (filtered);
112 build_convert_frame_pipeline (GstElement ** src_element,
113 GstElement ** sink_element, const GstCaps * from_caps,
114 const GstCaps * to_caps, GError ** err)
116 GstElement *src = NULL, *csp = NULL, *vscale = NULL;
117 GstElement *sink = NULL, *encoder = NULL, *pipeline;
118 GError *error = NULL;
120 /* videoscale is here to correct for the pixel-aspect-ratio for us */
121 GST_DEBUG ("creating elements");
122 if (!create_element ("appsrc", &src, &error) ||
123 !create_element ("ffmpegcolorspace", &csp, &error) ||
124 !create_element ("videoscale", &vscale, &error) ||
125 !create_element ("appsink", &sink, &error))
128 pipeline = gst_pipeline_new ("videoconvert-pipeline");
129 if (pipeline == NULL)
132 /* Add black borders if necessary to keep the DAR */
133 g_object_set (vscale, "add-borders", TRUE, NULL);
135 GST_DEBUG ("adding elements");
136 gst_bin_add_many (GST_BIN (pipeline), src, csp, vscale, sink, NULL);
139 g_object_set (src, "caps", from_caps, NULL);
140 g_object_set (sink, "caps", to_caps, NULL);
142 /* FIXME: linking is still way too expensive, profile this properly */
143 GST_DEBUG ("linking src->csp");
144 if (!gst_element_link_pads (src, "src", csp, "sink"))
147 GST_DEBUG ("linking csp->vscale");
148 if (!gst_element_link_pads (csp, "src", vscale, "sink"))
151 if (caps_are_raw (to_caps)) {
152 GST_DEBUG ("linking vscale->sink");
154 if (!gst_element_link_pads (vscale, "src", sink, "sink"))
157 encoder = get_encoder (to_caps, &error);
160 gst_bin_add (GST_BIN (pipeline), encoder);
162 GST_DEBUG ("linking vscale->encoder");
163 if (!gst_element_link (vscale, encoder))
166 GST_DEBUG ("linking encoder->sink");
167 if (!gst_element_link_pads (encoder, "src", sink, "sink"))
171 g_object_set (src, "emit-signals", TRUE, NULL);
172 g_object_set (sink, "emit-signals", TRUE, NULL);
175 *sink_element = sink;
181 gst_object_unref (pipeline);
183 GST_ERROR ("could not find an encoder for provided caps");
187 g_error_free (error);
194 gst_object_unref (src);
196 gst_object_unref (csp);
198 gst_object_unref (vscale);
200 gst_object_unref (sink);
201 GST_ERROR ("Could not convert video frame: %s", error->message);
205 g_error_free (error);
210 gst_object_unref (src);
211 gst_object_unref (csp);
212 gst_object_unref (vscale);
213 gst_object_unref (sink);
215 GST_ERROR ("Could not convert video frame: no pipeline (unknown error)");
217 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
218 "Could not convert video frame: no pipeline (unknown error)");
223 gst_object_unref (pipeline);
225 GST_ERROR ("Could not convert video frame: failed to link elements");
227 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
228 "Could not convert video frame: failed to link elements");
234 * gst_video_convert_frame:
236 * @from_caps: the #GstCaps to convert from
237 * @to_caps: the #GstCaps to convert to
238 * @timeout: the maximum amount of time allowed for the processing.
239 * @err: pointer to a #GError. Can be %NULL.
241 * Converts a raw video buffer into the specified output caps.
243 * The output caps can be any raw video formats or any image formats (jpeg, png, ...).
245 * The width, height and pixel-aspect-ratio can also be specified in the output caps.
247 * Returns: The converted #GstBuffer, or %NULL if an error happened (in which case @err
248 * will point to the #GError).
254 gst_video_convert_frame (GstBuffer * buf, GstCaps * from_caps,
255 const GstCaps * to_caps, GstClockTime timeout, GError ** err)
258 GstBuffer *result = NULL;
259 GError *error = NULL;
261 GstCaps *to_caps_copy = NULL;
263 GstElement *pipeline, *src, *sink;
266 g_return_val_if_fail (buf != NULL, NULL);
267 g_return_val_if_fail (to_caps != NULL, NULL);
268 g_return_val_if_fail (from_caps != NULL, NULL);
270 to_caps_copy = gst_caps_new_empty ();
271 n = gst_caps_get_size (to_caps);
272 for (i = 0; i < n; i++) {
273 GstStructure *s = gst_caps_get_structure (to_caps, i);
275 s = gst_structure_copy (s);
276 gst_structure_remove_field (s, "framerate");
277 gst_caps_append_structure (to_caps_copy, s);
281 build_convert_frame_pipeline (&src, &sink, from_caps, to_caps_copy,
286 /* now set the pipeline to the paused state, after we push the buffer into
287 * appsrc, this should preroll the converted buffer in appsink */
288 GST_DEBUG ("running conversion pipeline to caps %" GST_PTR_FORMAT,
290 gst_element_set_state (pipeline, GST_STATE_PAUSED);
292 /* feed buffer in appsrc */
293 GST_DEBUG ("feeding buffer %p, size %u, caps %" GST_PTR_FORMAT,
294 buf, gst_buffer_get_size (buf), from_caps);
295 g_signal_emit_by_name (src, "push-buffer", buf, &ret);
297 /* now see what happens. We either got an error somewhere or the pipeline
299 bus = gst_element_get_bus (pipeline);
300 msg = gst_bus_timed_pop_filtered (bus,
301 timeout, GST_MESSAGE_ERROR | GST_MESSAGE_ASYNC_DONE);
304 switch (GST_MESSAGE_TYPE (msg)) {
305 case GST_MESSAGE_ASYNC_DONE:
307 /* we're prerolled, get the frame from appsink */
308 g_signal_emit_by_name (sink, "pull-preroll", &result);
311 GST_DEBUG ("conversion successful: result = %p", result);
313 GST_ERROR ("prerolled but no result frame?!");
317 case GST_MESSAGE_ERROR:{
320 gst_message_parse_error (msg, &error, &dbg);
322 GST_ERROR ("Could not convert video frame: %s", error->message);
323 GST_DEBUG ("%s [debug: %s]", error->message, GST_STR_NULL (dbg));
327 g_error_free (error);
333 g_return_val_if_reached (NULL);
336 gst_message_unref (msg);
338 GST_ERROR ("Could not convert video frame: timeout during conversion");
340 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
341 "Could not convert video frame: timeout during conversion");
344 gst_element_set_state (pipeline, GST_STATE_NULL);
345 gst_object_unref (bus);
346 gst_object_unref (pipeline);
347 gst_caps_unref (to_caps_copy);
354 gst_caps_unref (to_caps_copy);
359 g_error_free (error);
368 GstElement *pipeline;
369 GstVideoConvertFrameCallback callback;
371 GDestroyNotify destroy_notify;
372 GMainContext *context;
376 } GstVideoConvertFrameContext;
380 GstVideoConvertFrameCallback callback;
384 GDestroyNotify destroy_notify;
386 GstVideoConvertFrameContext *context;
387 } GstVideoConvertFrameCallbackContext;
390 gst_video_convert_frame_context_free (GstVideoConvertFrameContext * ctx)
392 /* Wait until all users of the mutex are done */
393 g_mutex_lock (ctx->mutex);
394 g_mutex_unlock (ctx->mutex);
395 g_mutex_free (ctx->mutex);
397 g_source_remove (ctx->timeout_id);
399 gst_buffer_unref (ctx->buffer);
400 g_main_context_unref (ctx->context);
402 gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
403 gst_object_unref (ctx->pipeline);
405 g_slice_free (GstVideoConvertFrameContext, ctx);
409 gst_video_convert_frame_callback_context_free
410 (GstVideoConvertFrameCallbackContext * ctx)
413 gst_video_convert_frame_context_free (ctx->context);
414 g_slice_free (GstVideoConvertFrameCallbackContext, ctx);
418 convert_frame_dispatch_callback (GstVideoConvertFrameCallbackContext * ctx)
420 ctx->callback (ctx->buffer, ctx->error, ctx->user_data);
422 if (ctx->destroy_notify)
423 ctx->destroy_notify (ctx->user_data);
429 convert_frame_finish (GstVideoConvertFrameContext * context, GstBuffer * buffer,
433 GstVideoConvertFrameCallbackContext *ctx;
435 if (context->timeout_id)
436 g_source_remove (context->timeout_id);
437 context->timeout_id = 0;
439 ctx = g_slice_new (GstVideoConvertFrameCallbackContext);
440 ctx->callback = context->callback;
441 ctx->user_data = context->user_data;
442 ctx->destroy_notify = context->destroy_notify;
443 ctx->buffer = buffer;
445 ctx->context = context;
447 source = g_timeout_source_new (0);
448 g_source_set_callback (source,
449 (GSourceFunc) convert_frame_dispatch_callback, ctx,
450 (GDestroyNotify) gst_video_convert_frame_callback_context_free);
451 g_source_attach (source, context->context);
452 g_source_unref (source);
454 context->finished = TRUE;
458 convert_frame_timeout_callback (GstVideoConvertFrameContext * context)
462 g_mutex_lock (context->mutex);
464 if (context->finished)
467 GST_ERROR ("Could not convert video frame: timeout");
469 error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
470 "Could not convert video frame: timeout");
472 convert_frame_finish (context, NULL, error);
475 g_mutex_unlock (context->mutex);
480 convert_frame_bus_callback (GstBus * bus, GstMessage * message,
481 GstVideoConvertFrameContext * context)
483 g_mutex_lock (context->mutex);
485 if (context->finished)
488 switch (GST_MESSAGE_TYPE (message)) {
489 case GST_MESSAGE_ERROR:{
493 gst_message_parse_error (message, &error, &dbg);
495 GST_ERROR ("Could not convert video frame: %s", error->message);
496 GST_DEBUG ("%s [debug: %s]", error->message, GST_STR_NULL (dbg));
498 convert_frame_finish (context, NULL, error);
508 g_mutex_unlock (context->mutex);
514 convert_frame_need_data_callback (GstElement * src, guint size,
515 GstVideoConvertFrameContext * context)
517 GstFlowReturn ret = GST_FLOW_ERROR;
520 g_mutex_lock (context->mutex);
522 if (context->finished)
525 g_signal_emit_by_name (src, "push-buffer", context->buffer, &ret);
526 gst_buffer_unref (context->buffer);
527 context->buffer = NULL;
529 if (ret != GST_FLOW_OK) {
530 GST_ERROR ("Could not push video frame: %s", gst_flow_get_name (ret));
532 error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
533 "Could not push video frame: %s", gst_flow_get_name (ret));
535 convert_frame_finish (context, NULL, error);
538 g_signal_handlers_disconnect_by_func (src, convert_frame_need_data_callback,
542 g_mutex_unlock (context->mutex);
546 convert_frame_new_buffer_callback (GstElement * sink,
547 GstVideoConvertFrameContext * context)
549 GstBuffer *buf = NULL;
550 GError *error = NULL;
552 g_mutex_lock (context->mutex);
554 if (context->finished)
557 g_signal_emit_by_name (sink, "pull-preroll", &buf);
560 error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
561 "Could not get converted video frame");
564 convert_frame_finish (context, buf, error);
566 g_signal_handlers_disconnect_by_func (sink, convert_frame_need_data_callback,
570 g_mutex_unlock (context->mutex);
574 * gst_video_convert_frame_async:
576 * @to_caps: the #GstCaps to convert to
577 * @timeout: the maximum amount of time allowed for the processing.
578 * @callback: %GstVideoConvertFrameCallback that will be called after conversion.
579 * @destroy_notify: %GDestroyNotify to be called after @user_data is not needed anymore
581 * Converts a raw video buffer into the specified output caps.
583 * The output caps can be any raw video formats or any image formats (jpeg, png, ...).
585 * The width, height and pixel-aspect-ratio can also be specified in the output caps.
587 * @callback will be called after conversion, when an error occured or if conversion didn't
588 * finish after @timeout. @callback will always be called from the thread default
589 * %GMainContext, see g_main_context_get_thread_default(). If GLib before 2.22 is used,
590 * this will always be the global default main context.
592 * @destroy_notify will be called after the callback was called and @user_data is not needed
599 gst_video_convert_frame_async (GstBuffer * buf, GstCaps * from_caps,
600 const GstCaps * to_caps, GstClockTime timeout,
601 GstVideoConvertFrameCallback callback, gpointer user_data,
602 GDestroyNotify destroy_notify)
604 GMainContext *context = NULL;
605 GError *error = NULL;
607 GstCaps *to_caps_copy = NULL;
608 GstElement *pipeline, *src, *sink;
611 GstVideoConvertFrameContext *ctx;
613 g_return_if_fail (buf != NULL);
614 g_return_if_fail (to_caps != NULL);
615 g_return_if_fail (from_caps != NULL);
616 g_return_if_fail (callback != NULL);
618 context = g_main_context_get_thread_default ();
621 context = g_main_context_default ();
623 to_caps_copy = gst_caps_new_empty ();
624 n = gst_caps_get_size (to_caps);
625 for (i = 0; i < n; i++) {
626 GstStructure *s = gst_caps_get_structure (to_caps, i);
628 s = gst_structure_copy (s);
629 gst_structure_remove_field (s, "framerate");
630 gst_caps_append_structure (to_caps_copy, s);
634 build_convert_frame_pipeline (&src, &sink, from_caps, to_caps_copy,
639 bus = gst_element_get_bus (pipeline);
641 ctx = g_slice_new0 (GstVideoConvertFrameContext);
642 ctx->mutex = g_mutex_new ();
643 ctx->buffer = gst_buffer_ref (buf);
644 ctx->callback = callback;
645 ctx->user_data = user_data;
646 ctx->destroy_notify = destroy_notify;
647 ctx->context = g_main_context_ref (context);
648 ctx->finished = FALSE;
649 ctx->pipeline = pipeline;
651 if (timeout != GST_CLOCK_TIME_NONE) {
652 source = g_timeout_source_new (timeout / GST_MSECOND);
653 g_source_set_callback (source,
654 (GSourceFunc) convert_frame_timeout_callback, ctx, NULL);
655 ctx->timeout_id = g_source_attach (source, context);
656 g_source_unref (source);
659 g_signal_connect (src, "need-data",
660 G_CALLBACK (convert_frame_need_data_callback), ctx);
661 g_signal_connect (sink, "new-preroll",
662 G_CALLBACK (convert_frame_new_buffer_callback), ctx);
664 source = gst_bus_create_watch (bus);
665 g_source_set_callback (source, (GSourceFunc) convert_frame_bus_callback,
667 g_source_attach (source, context);
668 g_source_unref (source);
670 gst_element_set_state (pipeline, GST_STATE_PLAYING);
672 gst_object_unref (bus);
673 gst_caps_unref (to_caps_copy);
679 GstVideoConvertFrameCallbackContext *ctx;
682 gst_caps_unref (to_caps_copy);
684 ctx = g_slice_new0 (GstVideoConvertFrameCallbackContext);
685 ctx->callback = callback;
686 ctx->user_data = user_data;
687 ctx->destroy_notify = destroy_notify;
691 source = g_timeout_source_new (0);
692 g_source_set_callback (source,
693 (GSourceFunc) convert_frame_dispatch_callback, ctx,
694 (GDestroyNotify) gst_video_convert_frame_callback_context_free);
695 g_source_attach (source, context);
696 g_source_unref (source);