gst-plugins-bad: update translations
[platform/upstream/gstreamer.git] / subprojects / gst-editing-services / tools / utils.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2015 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdlib.h>
21 #include <glib/gprintf.h>
22 #include <string.h>
23 #include <gst/gst.h>
24 #include "utils.h"
25 #include "../ges/ges-internal.h"
26
27 #undef GST_CAT_DEFAULT
28
29 /* Copy of GST_ASCII_IS_STRING */
30 #define ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
31     ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
32     ((c) == '.'))
33
34 /* g_free after usage */
35 static gchar *
36 _sanitize_argument (gchar * arg, const gchar * prev_arg)
37 {
38   gboolean expect_equal = !(arg[0] == '+' || g_str_has_prefix (arg, "set-")
39       || prev_arg == NULL || prev_arg[0] == '+'
40       || g_str_has_prefix (prev_arg, "set-"));
41   gboolean need_wrap = FALSE;
42   gchar *first_equal = NULL;
43   gchar *wrap_start;
44   gchar *new_string, *tmp_string;
45   gsize num_escape;
46
47   for (tmp_string = arg; *tmp_string != '\0'; tmp_string++) {
48     if (expect_equal && first_equal == NULL && *tmp_string == '=') {
49       first_equal = tmp_string;
50       /* if this is the first equal, then don't count it as necessarily
51        * needing a wrap */
52     } else if (!ASCII_IS_STRING (*tmp_string)) {
53       need_wrap = TRUE;
54       break;
55     }
56   }
57
58   if (!need_wrap)
59     return g_strdup (arg);
60
61   if (first_equal)
62     wrap_start = first_equal + 1;
63   else
64     wrap_start = arg;
65
66   /* need to escape any '"' or '\\' to correctly parse in as a structure */
67   num_escape = 0;
68   for (tmp_string = wrap_start; *tmp_string != '\0'; tmp_string++) {
69     if (*tmp_string == '"' || *tmp_string == '\\')
70       num_escape++;
71   }
72
73   tmp_string = new_string =
74       g_malloc (sizeof (gchar) * (strlen (arg) + num_escape + 3));
75
76   while (arg != wrap_start)
77     *(tmp_string++) = *(arg++);
78   (*tmp_string++) = '"';
79
80   while (*arg != '\0') {
81     if (*arg == '"' || *arg == '\\')
82       (*tmp_string++) = '\\';
83     *(tmp_string++) = *(arg++);
84   }
85   *(tmp_string++) = '"';
86   *tmp_string = '\0';
87
88   return new_string;
89 }
90
91 gchar *
92 sanitize_timeline_description (gchar ** args, GESLauncherParsedOptions * opts)
93 {
94   gint i;
95   gchar *prev_arg = NULL;
96   GString *track_def;
97   GString *timeline_str;
98
99   gchar *string = g_strdup (" ");
100
101   for (i = 1; args[i]; i++) {
102     gchar *new_string;
103     gchar *sanitized = _sanitize_argument (args[i], prev_arg);
104
105     new_string = g_strconcat (string, " ", sanitized, NULL);
106
107     g_free (sanitized);
108     g_free (string);
109     string = new_string;
110     prev_arg = args[i];
111   }
112
113   if (i == 1) {
114     g_free (string);
115
116     return NULL;
117   }
118
119   if (strstr (string, "+track")) {
120     gchar *res = g_strconcat ("ges:", string, NULL);
121     g_free (string);
122
123     return res;
124   }
125
126   timeline_str = g_string_new (string);
127   g_free (string);
128
129   if (opts->track_types & GES_TRACK_TYPE_VIDEO) {
130     track_def = g_string_new (" +track video ");
131
132     if (opts->video_track_caps)
133       g_string_append_printf (track_def, " restrictions=[%s] ",
134           opts->video_track_caps);
135
136     g_string_prepend (timeline_str, track_def->str);
137     g_string_free (track_def, TRUE);
138   }
139
140   if (opts->track_types & GES_TRACK_TYPE_AUDIO) {
141     track_def = g_string_new (" +track audio ");
142
143     if (opts->audio_track_caps)
144       g_string_append_printf (track_def, " restrictions=[%s] ",
145           opts->audio_track_caps);
146
147     g_string_prepend (timeline_str, track_def->str);
148     g_string_free (track_def, TRUE);
149   }
150
151   g_string_prepend (timeline_str, "ges:");
152
153   return g_string_free (timeline_str, FALSE);
154 }
155
156 gboolean
157 get_flags_from_string (GType type, const gchar * str_flags, guint * flags)
158 {
159   GValue value = G_VALUE_INIT;
160   g_value_init (&value, type);
161
162   if (!gst_value_deserialize (&value, str_flags)) {
163     g_value_unset (&value);
164
165     return FALSE;
166   }
167
168   *flags = g_value_get_flags (&value);
169   g_value_unset (&value);
170
171   return TRUE;
172 }
173
174 gchar *
175 ensure_uri (const gchar * location)
176 {
177   if (gst_uri_is_valid (location))
178     return g_strdup (location);
179   else
180     return gst_filename_to_uri (location, NULL);
181 }
182
183 GstEncodingProfile *
184 parse_encoding_profile (const gchar * format)
185 {
186   GstEncodingProfile *profile;
187   GValue value = G_VALUE_INIT;
188
189   g_value_init (&value, GST_TYPE_ENCODING_PROFILE);
190
191   if (!gst_value_deserialize (&value, format)) {
192     g_value_reset (&value);
193
194     return NULL;
195   }
196
197   profile = g_value_dup_object (&value);
198   g_value_reset (&value);
199
200   return profile;
201 }
202
203 void
204 print_enum (GType enum_type)
205 {
206   GEnumClass *enum_class = G_ENUM_CLASS (g_type_class_ref (enum_type));
207   guint i;
208
209   for (i = 0; i < enum_class->n_values; i++) {
210     gst_print ("%s\n", enum_class->values[i].value_nick);
211   }
212
213   g_type_class_unref (enum_class);
214 }
215
216 void
217 ges_print (GstDebugColorFlags c, gboolean err, gboolean nline,
218     const gchar * format, va_list var_args)
219 {
220   GString *str = g_string_new (NULL);
221   GstDebugColorMode color_mode;
222   gchar *color = NULL;
223   const gchar *clear = NULL;
224
225   color_mode = gst_debug_get_color_mode ();
226 #ifdef G_OS_WIN32
227   if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
228 #else
229   if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
230 #endif
231     clear = "\033[00m";
232     color = gst_debug_construct_term_color (c);
233   }
234
235   if (color) {
236     g_string_append (str, color);
237     g_free (color);
238   }
239
240   g_string_append_vprintf (str, format, var_args);
241
242   if (nline)
243     g_string_append_c (str, '\n');
244
245   if (clear)
246     g_string_append (str, clear);
247
248   if (err)
249     gst_printerr ("%s", str->str);
250   else
251     gst_print ("%s", str->str);
252
253   g_string_free (str, TRUE);
254 }
255
256 void
257 ges_ok (const gchar * format, ...)
258 {
259   va_list var_args;
260
261   va_start (var_args, format);
262   ges_print (GST_DEBUG_FG_GREEN, FALSE, TRUE, format, var_args);
263   va_end (var_args);
264 }
265
266 void
267 ges_warn (const gchar * format, ...)
268 {
269   va_list var_args;
270
271   va_start (var_args, format);
272   ges_print (GST_DEBUG_FG_YELLOW, TRUE, TRUE, format, var_args);
273   va_end (var_args);
274 }
275
276 void
277 ges_printerr (const gchar * format, ...)
278 {
279   va_list var_args;
280
281   va_start (var_args, format);
282   ges_print (GST_DEBUG_FG_RED, TRUE, TRUE, format, var_args);
283   va_end (var_args);
284 }
285
286 gchar *
287 get_file_extension (gchar * uri)
288 {
289   size_t len;
290   gint find;
291
292   len = strlen (uri);
293   find = len - 1;
294
295   while (find >= 0) {
296     if (uri[find] == '.')
297       break;
298     find--;
299   }
300
301   if (find <= 0)
302     return NULL;
303
304   return g_strdup (&uri[find + 1]);
305 }
306
307 static const gchar *
308 get_type_icon (gpointer obj)
309 {
310   if (GST_IS_ENCODING_AUDIO_PROFILE (obj) || GST_IS_DISCOVERER_AUDIO_INFO (obj))
311     return "♫";
312   else if (GST_IS_ENCODING_VIDEO_PROFILE (obj)
313       || GST_IS_DISCOVERER_VIDEO_INFO (obj))
314     return "▶";
315   else if (GST_IS_ENCODING_CONTAINER_PROFILE (obj)
316       || GST_IS_DISCOVERER_CONTAINER_INFO (obj))
317     return "∋";
318   else
319     return "";
320 }
321
322 static void
323 print_profile (GstEncodingProfile * profile, const gchar * prefix)
324 {
325   const gchar *name = gst_encoding_profile_get_name (profile);
326   const gchar *desc = gst_encoding_profile_get_description (profile);
327   GstCaps *format = gst_encoding_profile_get_format (profile);
328   gchar *capsdesc = NULL;
329
330   if (gst_caps_is_fixed (format))
331     capsdesc = gst_pb_utils_get_codec_description (format);
332   if (!capsdesc)
333     capsdesc = gst_caps_to_string (format);
334
335   if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
336     gst_print ("%s> %s %s: %s%s%s%s\n", prefix,
337         get_type_icon (profile),
338         capsdesc, name ? name : "",
339         desc ? " (" : "", desc ? desc : "", desc ? ")" : "");
340
341   } else {
342     gst_print ("%s%s %s%s%s%s%s%s", prefix, get_type_icon (profile),
343         name ? name : capsdesc, desc ? ": " : "", desc ? desc : "",
344         name ? " (" : "", name ? capsdesc : "", name ? ")" : "");
345
346     if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
347       GstCaps *caps = gst_encoding_profile_get_restriction (profile);
348
349       if (!caps && gst_caps_is_fixed (format))
350         caps = gst_caps_ref (format);
351
352       if (caps) {
353         GstVideoInfo info;
354
355         if (gst_video_info_from_caps (&info, caps)) {
356           gst_print (" (%dx%d", info.width, info.height);
357           if (info.fps_n)
358             gst_print ("@%d/%dfps", info.fps_n, info.fps_d);
359           gst_print (")");
360         }
361         gst_caps_unref (caps);
362       }
363     } else if (GST_IS_ENCODING_AUDIO_PROFILE (profile)) {
364       GstCaps *caps = gst_encoding_profile_get_restriction (profile);
365
366       if (!caps && gst_caps_is_fixed (format))
367         caps = gst_caps_ref (format);
368
369       if (caps) {
370         GstAudioInfo info;
371
372         if (gst_caps_is_fixed (caps) && gst_audio_info_from_caps (&info, caps))
373           gst_print (" (%d channels @ %dhz)", info.channels, info.rate);
374         gst_caps_unref (caps);
375       }
376     }
377
378
379     gst_print ("\n");
380   }
381
382   gst_caps_unref (format);
383
384   g_free (capsdesc);
385 }
386
387 void
388 describe_encoding_profile (GstEncodingProfile * profile)
389 {
390   g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
391
392   print_profile (profile, "     ");
393   if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
394     const GList *tmp;
395
396     for (tmp =
397         gst_encoding_container_profile_get_profiles
398         (GST_ENCODING_CONTAINER_PROFILE (profile)); tmp; tmp = tmp->next)
399       print_profile (tmp->data, "       - ");
400   }
401 }
402
403 static void
404 describe_stream_info (GstDiscovererStreamInfo * sinfo, GString * desc)
405 {
406   gchar *capsdesc;
407   GstCaps *caps;
408
409   caps = gst_discoverer_stream_info_get_caps (sinfo);
410   capsdesc = gst_pb_utils_get_codec_description (caps);
411   if (!capsdesc)
412     capsdesc = gst_caps_to_string (caps);
413   gst_caps_unref (caps);
414
415   g_string_append_printf (desc, "%s%s%s", desc->len ? ", " : "",
416       get_type_icon (sinfo), capsdesc);
417
418   g_free (capsdesc);
419
420   if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
421     GList *tmp, *streams;
422
423     streams =
424         gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
425         (sinfo));
426     for (tmp = streams; tmp; tmp = tmp->next)
427       describe_stream_info (tmp->data, desc);
428     gst_discoverer_stream_info_list_free (streams);
429   }
430 }
431
432 static gchar *
433 describe_discoverer (GstDiscovererInfo * info)
434 {
435   GString *desc = g_string_new (NULL);
436   GstDiscovererStreamInfo *sinfo = gst_discoverer_info_get_stream_info (info);
437
438   describe_stream_info (sinfo, desc);
439   gst_discoverer_stream_info_unref (sinfo);
440
441   return g_string_free (desc, FALSE);
442 }
443
444 void
445 print_timeline (GESTimeline * timeline)
446 {
447   gchar *uri;
448   GList *layer, *clip, *clips;
449
450   if (!timeline->layers)
451     return;
452
453   uri = ges_command_line_formatter_get_timeline_uri (timeline);
454   gst_print ("\nTimeline description: `%s`\n", &uri[5]);
455   g_free (uri);
456   gst_print ("====================\n\n");
457   for (layer = timeline->layers; layer; layer = layer->next) {
458     clips = ges_layer_get_clips (layer->data);
459
460     if (!clips)
461       continue;
462
463     gst_printerr ("  layer %d: \n", ges_layer_get_priority (layer->data));
464     gst_printerr ("  --------\n");
465     for (clip = clips; clip; clip = clip->next) {
466       gchar *name;
467
468       if (GES_IS_URI_CLIP (clip->data)) {
469         GESUriClipAsset *asset =
470             GES_URI_CLIP_ASSET (ges_extractable_get_asset (clip->data));
471         gchar *asset_desc =
472             describe_discoverer (ges_uri_clip_asset_get_info (asset));
473
474         name = g_strdup_printf ("Clip from: '%s' [%s]",
475             ges_asset_get_id (GES_ASSET (asset)), asset_desc);
476         g_free (asset_desc);
477       } else {
478         name = g_strdup (GES_TIMELINE_ELEMENT_NAME (clip->data));
479       }
480       gst_print ("    - %s\n        start=%" GST_TIME_FORMAT,
481           name, GST_TIME_ARGS (GES_TIMELINE_ELEMENT_START (clip->data)));
482       g_free (name);
483       if (GES_TIMELINE_ELEMENT_INPOINT (clip->data))
484         gst_print (" inpoint=%" GST_TIME_FORMAT,
485             GST_TIME_ARGS (GES_TIMELINE_ELEMENT_INPOINT (clip->data)));
486       gst_print (" duration=%" GST_TIME_FORMAT "\n",
487           GST_TIME_ARGS (GES_TIMELINE_ELEMENT_END (clip->data)));
488     }
489     if (layer->next)
490       gst_printerr ("\n");
491
492     g_list_free_full (clips, gst_object_unref);
493   }
494
495   gst_print ("\n");
496 }