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 * @to_caps: the #GstCaps to convert to
237 * @timeout: the maximum amount of time allowed for the processing.
238 * @err: pointer to a #GError. Can be %NULL.
240 * Converts a raw video buffer into the specified output caps.
242 * The output caps can be any raw video formats or any image formats (jpeg, png, ...).
244 * The width, height and pixel-aspect-ratio can also be specified in the output caps.
246 * Returns: The converted #GstBuffer, or %NULL if an error happened (in which case @err
247 * will point to the #GError).
253 gst_video_convert_frame (GstBuffer * buf, const GstCaps * to_caps,
254 GstClockTime timeout, GError ** err)
257 GstBuffer *result = NULL;
258 GError *error = NULL;
260 GstCaps *from_caps, *to_caps_copy = NULL;
262 GstElement *pipeline, *src, *sink;
265 g_return_val_if_fail (buf != NULL, NULL);
266 g_return_val_if_fail (to_caps != NULL, NULL);
267 g_return_val_if_fail (GST_BUFFER_CAPS (buf) != NULL, NULL);
269 from_caps = GST_BUFFER_CAPS (buf);
271 to_caps_copy = gst_caps_new_empty ();
272 n = gst_caps_get_size (to_caps);
273 for (i = 0; i < n; i++) {
274 GstStructure *s = gst_caps_get_structure (to_caps, i);
276 s = gst_structure_copy (s);
277 gst_structure_remove_field (s, "framerate");
278 gst_caps_append_structure (to_caps_copy, s);
282 build_convert_frame_pipeline (&src, &sink, from_caps, to_caps_copy,
287 /* now set the pipeline to the paused state, after we push the buffer into
288 * appsrc, this should preroll the converted buffer in appsink */
289 GST_DEBUG ("running conversion pipeline to caps %" GST_PTR_FORMAT,
291 gst_element_set_state (pipeline, GST_STATE_PAUSED);
293 /* feed buffer in appsrc */
294 GST_DEBUG ("feeding buffer %p, size %u, caps %" GST_PTR_FORMAT,
295 buf, GST_BUFFER_SIZE (buf), from_caps);
296 g_signal_emit_by_name (src, "push-buffer", buf, &ret);
298 /* now see what happens. We either got an error somewhere or the pipeline
300 bus = gst_element_get_bus (pipeline);
301 msg = gst_bus_timed_pop_filtered (bus,
302 timeout, GST_MESSAGE_ERROR | GST_MESSAGE_ASYNC_DONE);
305 switch (GST_MESSAGE_TYPE (msg)) {
306 case GST_MESSAGE_ASYNC_DONE:
308 /* we're prerolled, get the frame from appsink */
309 g_signal_emit_by_name (sink, "pull-preroll", &result);
312 GST_DEBUG ("conversion successful: result = %p", result);
314 GST_ERROR ("prerolled but no result frame?!");
318 case GST_MESSAGE_ERROR:{
321 gst_message_parse_error (msg, &error, &dbg);
323 GST_ERROR ("Could not convert video frame: %s", error->message);
324 GST_DEBUG ("%s [debug: %s]", error->message, GST_STR_NULL (dbg));
328 g_error_free (error);
334 g_return_val_if_reached (NULL);
337 gst_message_unref (msg);
339 GST_ERROR ("Could not convert video frame: timeout during conversion");
341 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
342 "Could not convert video frame: timeout during conversion");
345 gst_element_set_state (pipeline, GST_STATE_NULL);
346 gst_object_unref (bus);
347 gst_object_unref (pipeline);
348 gst_caps_unref (to_caps_copy);
355 gst_caps_unref (to_caps_copy);
360 g_error_free (error);
369 GstElement *pipeline;
370 GstVideoConvertFrameCallback callback;
372 GDestroyNotify destroy_notify;
373 GMainContext *context;
377 } GstVideoConvertFrameContext;
381 GstVideoConvertFrameCallback callback;
385 GDestroyNotify destroy_notify;
387 GstVideoConvertFrameContext *context;
388 } GstVideoConvertFrameCallbackContext;
391 gst_video_convert_frame_context_free (GstVideoConvertFrameContext * ctx)
393 /* Wait until all users of the mutex are done */
394 g_mutex_lock (ctx->mutex);
395 g_mutex_unlock (ctx->mutex);
396 g_mutex_free (ctx->mutex);
398 g_source_remove (ctx->timeout_id);
400 gst_buffer_unref (ctx->buffer);
401 g_main_context_unref (ctx->context);
403 gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
404 gst_object_unref (ctx->pipeline);
406 g_slice_free (GstVideoConvertFrameContext, ctx);
410 gst_video_convert_frame_callback_context_free
411 (GstVideoConvertFrameCallbackContext * ctx)
414 gst_video_convert_frame_context_free (ctx->context);
415 g_slice_free (GstVideoConvertFrameCallbackContext, ctx);
419 convert_frame_dispatch_callback (GstVideoConvertFrameCallbackContext * ctx)
421 ctx->callback (ctx->buffer, ctx->error, ctx->user_data);
423 if (ctx->destroy_notify)
424 ctx->destroy_notify (ctx->user_data);
430 convert_frame_finish (GstVideoConvertFrameContext * context, GstBuffer * buffer,
434 GstVideoConvertFrameCallbackContext *ctx;
436 if (context->timeout_id)
437 g_source_remove (context->timeout_id);
438 context->timeout_id = 0;
440 ctx = g_slice_new (GstVideoConvertFrameCallbackContext);
441 ctx->callback = context->callback;
442 ctx->user_data = context->user_data;
443 ctx->destroy_notify = context->destroy_notify;
444 ctx->buffer = buffer;
446 ctx->context = context;
448 source = g_timeout_source_new (0);
449 g_source_set_callback (source,
450 (GSourceFunc) convert_frame_dispatch_callback, ctx,
451 (GDestroyNotify) gst_video_convert_frame_callback_context_free);
452 g_source_attach (source, context->context);
453 g_source_unref (source);
455 context->finished = TRUE;
459 convert_frame_timeout_callback (GstVideoConvertFrameContext * context)
463 g_mutex_lock (context->mutex);
465 if (context->finished)
468 GST_ERROR ("Could not convert video frame: timeout");
470 error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
471 "Could not convert video frame: timeout");
473 convert_frame_finish (context, NULL, error);
476 g_mutex_unlock (context->mutex);
481 convert_frame_bus_callback (GstBus * bus, GstMessage * message,
482 GstVideoConvertFrameContext * context)
484 g_mutex_lock (context->mutex);
486 if (context->finished)
489 switch (GST_MESSAGE_TYPE (message)) {
490 case GST_MESSAGE_ERROR:{
494 gst_message_parse_error (message, &error, &dbg);
496 GST_ERROR ("Could not convert video frame: %s", error->message);
497 GST_DEBUG ("%s [debug: %s]", error->message, GST_STR_NULL (dbg));
499 convert_frame_finish (context, NULL, error);
509 g_mutex_unlock (context->mutex);
515 convert_frame_need_data_callback (GstElement * src, guint size,
516 GstVideoConvertFrameContext * context)
518 GstFlowReturn ret = GST_FLOW_ERROR;
521 g_mutex_lock (context->mutex);
523 if (context->finished)
526 g_signal_emit_by_name (src, "push-buffer", context->buffer, &ret);
527 gst_buffer_unref (context->buffer);
528 context->buffer = NULL;
530 if (ret != GST_FLOW_OK) {
531 GST_ERROR ("Could not push video frame: %s", gst_flow_get_name (ret));
533 error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
534 "Could not push video frame: %s", gst_flow_get_name (ret));
536 convert_frame_finish (context, NULL, error);
539 g_signal_handlers_disconnect_by_func (src, convert_frame_need_data_callback,
543 g_mutex_unlock (context->mutex);
547 convert_frame_new_buffer_callback (GstElement * sink,
548 GstVideoConvertFrameContext * context)
550 GstBuffer *buf = NULL;
551 GError *error = NULL;
553 g_mutex_lock (context->mutex);
555 if (context->finished)
558 g_signal_emit_by_name (sink, "pull-preroll", &buf);
561 error = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
562 "Could not get converted video frame");
565 convert_frame_finish (context, buf, error);
567 g_signal_handlers_disconnect_by_func (sink, convert_frame_need_data_callback,
571 g_mutex_unlock (context->mutex);
575 * gst_video_convert_frame_async:
577 * @to_caps: the #GstCaps to convert to
578 * @timeout: the maximum amount of time allowed for the processing.
579 * @callback: %GstVideoConvertFrameCallback that will be called after conversion.
580 * @destroy_notify: %GDestroyNotify to be called after @user_data is not needed anymore
582 * Converts a raw video buffer into the specified output caps.
584 * The output caps can be any raw video formats or any image formats (jpeg, png, ...).
586 * The width, height and pixel-aspect-ratio can also be specified in the output caps.
588 * @callback will be called after conversion, when an error occured or if conversion didn't
589 * finish after @timeout. @callback will always be called from the thread default
590 * %GMainContext, see g_main_context_get_thread_default(). If GLib before 2.22 is used,
591 * this will always be the global default main context.
593 * @destroy_notify will be called after the callback was called and @user_data is not needed
600 gst_video_convert_frame_async (GstBuffer * buf, const GstCaps * to_caps,
601 GstClockTime timeout, GstVideoConvertFrameCallback callback,
602 gpointer user_data, GDestroyNotify destroy_notify)
604 GMainContext *context = NULL;
605 GError *error = NULL;
607 GstCaps *from_caps, *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 (GST_BUFFER_CAPS (buf) != NULL);
616 g_return_if_fail (callback != NULL);
618 context = g_main_context_get_thread_default ();
621 context = g_main_context_default ();
623 from_caps = GST_BUFFER_CAPS (buf);
625 to_caps_copy = gst_caps_new_empty ();
626 n = gst_caps_get_size (to_caps);
627 for (i = 0; i < n; i++) {
628 GstStructure *s = gst_caps_get_structure (to_caps, i);
630 s = gst_structure_copy (s);
631 gst_structure_remove_field (s, "framerate");
632 gst_caps_append_structure (to_caps_copy, s);
636 build_convert_frame_pipeline (&src, &sink, from_caps, to_caps_copy,
641 bus = gst_element_get_bus (pipeline);
643 ctx = g_slice_new0 (GstVideoConvertFrameContext);
644 ctx->mutex = g_mutex_new ();
645 ctx->buffer = gst_buffer_ref (buf);
646 ctx->callback = callback;
647 ctx->user_data = user_data;
648 ctx->destroy_notify = destroy_notify;
649 ctx->context = g_main_context_ref (context);
650 ctx->finished = FALSE;
651 ctx->pipeline = pipeline;
653 if (timeout != GST_CLOCK_TIME_NONE) {
654 source = g_timeout_source_new (timeout / GST_MSECOND);
655 g_source_set_callback (source,
656 (GSourceFunc) convert_frame_timeout_callback, ctx, NULL);
657 ctx->timeout_id = g_source_attach (source, context);
658 g_source_unref (source);
661 g_signal_connect (src, "need-data",
662 G_CALLBACK (convert_frame_need_data_callback), ctx);
663 g_signal_connect (sink, "new-preroll",
664 G_CALLBACK (convert_frame_new_buffer_callback), ctx);
666 source = gst_bus_create_watch (bus);
667 g_source_set_callback (source, (GSourceFunc) convert_frame_bus_callback,
669 g_source_attach (source, context);
670 g_source_unref (source);
672 gst_element_set_state (pipeline, GST_STATE_PLAYING);
674 gst_object_unref (bus);
675 gst_caps_unref (to_caps_copy);
681 GstVideoConvertFrameCallbackContext *ctx;
684 gst_caps_unref (to_caps_copy);
686 ctx = g_slice_new0 (GstVideoConvertFrameCallbackContext);
687 ctx->callback = callback;
688 ctx->user_data = user_data;
689 ctx->destroy_notify = destroy_notify;
693 source = g_timeout_source_new (0);
694 g_source_set_callback (source,
695 (GSourceFunc) convert_frame_dispatch_callback, ctx,
696 (GDestroyNotify) gst_video_convert_frame_callback_context_free);
697 g_source_attach (source, context);
698 g_source_unref (source);