Add -Wmissing-declarations -Wmissing-prototypes to warning flags
[platform/upstream/gstreamer.git] / gst-libs / gst / tag / lang.c
1 /* GStreamer language codes and names utility functions
2  * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:gsttaglanguagecodes
22  * @short_description: mappings for ISO-639 language codes and names
23  * @see_also: #GstTagList
24  *
25  * <refsect2>
26  * <para>
27  * Provides helper functions to convert between the various ISO-639 language
28  * codes, and to map language codes to language names.
29  * </para>
30  * </refsect2>
31  */
32
33 /* FIXME 0.11: maybe switch to ISO-639-2 everywhere incl. GST_TAG_LANGUAGE? */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #undef GETTEXT_PACKAGE
40 #define GETTEXT_PACKAGE "iso_639"
41
42 #define ISO_639_XML_PATH ISO_CODES_PREFIX "/share/xml/iso-codes/iso_639.xml"
43 #define ISO_CODES_LOCALEDIR ISO_CODES_PREFIX "/share/locale"
44
45 #include <gst/gst-i18n-plugin.h>
46 #include <gst/gst.h>
47
48 #include <string.h>
49 #include <stdlib.h>
50
51 #include "tag.h"
52 #include "lang-tables.dat"
53
54 /* FIXME: remove once we depend on GLib >= 2.22 */
55 #if !GLIB_CHECK_VERSION (2, 22, 0)
56 #define g_mapped_file_unref g_mapped_file_free
57 #endif
58
59 #ifndef GST_DISABLE_GST_DEBUG
60
61 #define GST_CAT_DEFAULT ensure_debug_category()
62
63 static GstDebugCategory *
64 ensure_debug_category (void)
65 {
66   static gsize cat_gonce = 0;
67
68   if (g_once_init_enter (&cat_gonce)) {
69     gsize cat_done;
70
71     cat_done = (gsize) _gst_debug_category_new ("tag-langcodes", 0,
72         "GstTag language codes and names");
73
74     g_once_init_leave (&cat_gonce, cat_done);
75   }
76
77   return (GstDebugCategory *) cat_gonce;
78 }
79
80 #else
81
82 #define ensure_debug_category() /* NOOP */
83
84 #endif /* GST_DISABLE_GST_DEBUG */
85
86 /* ------------------------------------------------------------------------- */
87
88 /* Loading and initing */
89
90 #if defined(HAVE_ISO_CODES)
91 static const gchar *
92 get_val (const gchar ** names, const gchar ** vals, const gchar * name)
93 {
94   while (names != NULL && *names != NULL) {
95     if (strcmp (*names, name) == 0)
96       return *vals;
97     ++names;
98     ++vals;
99   }
100   return NULL;
101 }
102
103 static void
104 parse_start_element (GMarkupParseContext * ctx, const gchar * element_name,
105     const gchar ** attr_names, const gchar ** attr_vals,
106     gpointer user_data, GError ** error)
107 {
108   GHashTable *ht = (GHashTable *) user_data;
109   const gchar *c1, *c2t, *c2b, *name, *tname;
110
111   if (strcmp (element_name, "iso_639_entry") != 0)
112     return;
113
114   c1 = get_val (attr_names, attr_vals, "iso_639_1_code");
115
116   /* only interested in languages with an ISO 639-1 code for now */
117   if (c1 == NULL)
118     return;
119
120   c2t = get_val (attr_names, attr_vals, "iso_639_2T_code");
121   c2b = get_val (attr_names, attr_vals, "iso_639_2B_code");
122   name = get_val (attr_names, attr_vals, "name");
123
124   if (c2t == NULL || c2b == NULL || name == NULL) {
125     GST_WARNING ("broken iso_639.xml entry: c2t=%p, c2b=%p, name=%p", c2t,
126         c2b, name);
127     return;
128   }
129
130   /* translate language name */
131   tname = _(name);
132
133   /* if no translation was found, it will return the input string, which we
134    * we don't want to put into the hash table because it will be freed again */
135   if (G_UNLIKELY (tname == name))
136     tname = g_intern_string (name);
137
138   /* now overwrite default/fallback mappings with names in locale language */
139   g_hash_table_replace (ht, (gpointer) g_intern_string (c1), (gpointer) tname);
140   g_hash_table_replace (ht, (gpointer) g_intern_string (c2b), (gpointer) tname);
141   if (strcmp (c2t, c2b) != 0) {
142     g_hash_table_replace (ht, (gpointer) g_intern_string (c2t),
143         (gpointer) tname);
144   }
145
146   GST_LOG ("%s %s %s : %s - %s", c1, c2t, c2b, name, tname);
147 }
148
149 static void
150 gst_tag_load_iso_639_xml (GHashTable * ht)
151 {
152   GMappedFile *f;
153   GError *err = NULL;
154   gchar *xml_data;
155   gsize xml_len;
156
157 #ifdef ENABLE_NLS
158   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
159       ISO_CODES_LOCALEDIR);
160   bindtextdomain (GETTEXT_PACKAGE, ISO_CODES_LOCALEDIR);
161   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
162 #endif
163
164   f = g_mapped_file_new (ISO_639_XML_PATH, FALSE, NULL);
165   if (f != NULL) {
166     xml_data = (gchar *) g_mapped_file_get_contents (f);
167     xml_len = g_mapped_file_get_length (f);
168   } else {
169     if (!g_file_get_contents (ISO_639_XML_PATH, &xml_data, &xml_len, &err)) {
170       GST_WARNING ("Could not read %s: %s", ISO_639_XML_PATH, err->message);
171       g_error_free (err);
172       return;
173     }
174   }
175
176   if (g_utf8_validate (xml_data, xml_len, NULL)) {
177     GMarkupParser xml_parser = { parse_start_element, NULL, NULL, NULL, NULL };
178     GMarkupParseContext *ctx;
179
180     ctx = g_markup_parse_context_new (&xml_parser, 0, ht, NULL);
181     if (!g_markup_parse_context_parse (ctx, xml_data, xml_len, &err)) {
182       GST_WARNING ("Parsing iso_639.xml failed: %s", err->message);
183       g_error_free (err);
184     }
185     g_markup_parse_context_free (ctx);
186   } else {
187     GST_WARNING ("iso_639.xml file is not valid UTF-8");
188     GST_MEMDUMP ("iso_639.xml file", (guint8 *) xml_data, xml_len);
189   }
190
191   /* ... and clean up */
192   if (f != NULL)
193     g_mapped_file_unref (f);
194   else
195     g_free (xml_data);
196 }
197 #endif /* HAVE_ISO_CODES */
198
199 static GHashTable *
200 gst_tag_get_iso_639_ht (void)
201 {
202   static gsize once_val = 0;
203   int i;
204
205   if (g_once_init_enter (&once_val)) {
206     GHashTable *ht;
207     gsize done_val;
208
209     GST_MEMDUMP ("iso 639 language names (internal default/fallback)",
210         (guint8 *) iso_639_names, sizeof (iso_639_names));
211
212     /* maps code -> language name; all strings are either interned strings
213      * or const static strings from lang-table.c */
214     ht = g_hash_table_new (g_str_hash, g_str_equal);
215
216     /* set up default/fallback mappings */
217     for (i = 0; i < G_N_ELEMENTS (iso_639_codes); ++i) {
218       GST_LOG ("%3d %s %s %c%c 0x%04x  %s", i, iso_639_codes[i].iso_639_1,
219           iso_639_codes[i].iso_639_2,
220           ((iso_639_codes[i].flags & ISO_639_FLAG_2B)) ? 'B' : '.',
221           ((iso_639_codes[i].flags & ISO_639_FLAG_2T)) ? 'T' : '.',
222           iso_639_codes[i].name_offset,
223           iso_639_names + iso_639_codes[i].name_offset);
224
225 #ifdef HAVE_ISO_CODES
226       /* intern these in order to minimise allocations when interning strings
227        * read from the xml file later */
228       g_intern_static_string (iso_639_codes[i].iso_639_1);
229       g_intern_static_string (iso_639_codes[i].iso_639_2);
230       g_intern_static_string (iso_639_names + iso_639_codes[i].name_offset);
231 #endif
232
233       /* and add default mapping (these strings are always valid) */
234       g_hash_table_insert (ht, (gpointer) iso_639_codes[i].iso_639_1,
235           (gpointer) (iso_639_names + iso_639_codes[i].name_offset));
236       g_hash_table_insert (ht, (gpointer) iso_639_codes[i].iso_639_2,
237           (gpointer) (iso_639_names + iso_639_codes[i].name_offset));
238     }
239
240 #ifdef HAVE_ISO_CODES
241     {
242       GstClockTime ts = gst_util_get_timestamp ();
243
244       gst_tag_load_iso_639_xml (ht);
245
246       ts = gst_util_get_timestamp () - ts;
247       GST_INFO ("iso_639.xml loading took %.2gms", (double) ts / GST_MSECOND);
248     }
249 #else
250     GST_INFO ("iso-codes disabled or not available");
251 #endif
252
253     done_val = (gsize) ht;
254     g_once_init_leave (&once_val, done_val);
255   }
256
257   return (GHashTable *) once_val;
258 }
259
260 /* ------------------------------------------------------------------------- */
261
262 static int
263 qsort_strcmp_func (const void *p1, const void *p2)
264 {
265   return strcmp (*(char *const *) p1, *(char *const *) p2);
266 }
267
268 /**
269  * gst_tag_get_language_codes:
270  *
271  * Returns a list of known language codes (in form of two-letter ISO-639-1
272  * codes). This is useful for UIs to build a list of available languages for
273  * tagging purposes (e.g. to tag an audio track appropriately in a video or
274  * audio editor).
275  *
276  * Returns: NULL-terminated string array with two-letter language codes. Free
277  *     with g_strfreev() when no longer needed.
278  *
279  * Since: 0.10.26
280  */
281 gchar **
282 gst_tag_get_language_codes (void)
283 {
284   GHashTableIter iter;
285   GHashTable *ht;
286   gpointer key;
287   gchar **codes;
288   int i;
289
290   ensure_debug_category ();
291
292   ht = gst_tag_get_iso_639_ht ();
293
294   /* we have at least two keys for each language (-1 code and -2 code) */
295   codes = g_new (gchar *, (g_hash_table_size (ht) / 2) + 1);
296
297   i = 0;
298   g_hash_table_iter_init (&iter, ht);
299   while (g_hash_table_iter_next (&iter, &key, NULL)) {
300     const gchar *lang_code = key;
301
302     if (strlen (lang_code) == 2) {
303       codes[i] = g_strdup (lang_code);
304       ++i;
305     }
306   }
307   codes[i] = NULL;
308
309   /* be nice and sort the list */
310   qsort (&codes[0], i, sizeof (gchar *), qsort_strcmp_func);
311
312   return codes;
313 }
314
315 /**
316  * gst_tag_get_language_name:
317  * @language_code: two or three-letter ISO-639 language code
318  *
319  * Returns the name of the language given an ISO-639 language code, such
320  * as often found in a GST_TAG_LANGUAGE tag. The name will be translated
321  * according to the current locale (if the library was built against the
322  * iso-codes package, otherwise the English name will be returned).
323  *
324  * Language codes are case-sensitive and expected to be lower case.
325  *
326  * Returns: language name in UTF-8 format, or NULL if @language_code could
327  *     not be mapped to a language name. The returned string must not be
328  *     modified and does not need to freed; it will stay valid until the
329  *     application is terminated.
330  *
331  * Since: 0.10.26
332  */
333 const gchar *
334 gst_tag_get_language_name (const gchar * language_code)
335 {
336   const gchar *lang_name;
337   GHashTable *ht;
338
339   g_return_val_if_fail (language_code != NULL, NULL);
340
341   ensure_debug_category ();
342
343   ht = gst_tag_get_iso_639_ht ();
344
345   lang_name = g_hash_table_lookup (ht, (gpointer) language_code);
346   GST_LOG ("%s -> %s", language_code, GST_STR_NULL (lang_name));
347
348   return lang_name;
349 }
350
351 /**
352  * gst_tag_get_language_code_iso_639_1:
353  * @lang_code: ISO-639 language code (e.g. "deu" or "ger" or "de")
354  *
355  * Returns two-letter ISO-639-1 language code given a three-letter ISO-639-2
356  * language code or two-letter ISO-639-1 language code (both are accepted for
357  * convenience).
358  *
359  * Language codes are case-sensitive and expected to be lower case.
360  *
361  * Returns: two-letter ISO-639-1 language code string that maps to @lang_code,
362  *     or NULL if no mapping is known. The returned string must not be
363  *     modified or freed.
364  *
365  * Since: 0.10.26
366  */
367 const gchar *
368 gst_tag_get_language_code_iso_639_1 (const gchar * lang_code)
369 {
370   const gchar *c = NULL;
371   int i;
372
373   g_return_val_if_fail (lang_code != NULL, NULL);
374
375   ensure_debug_category ();
376
377   /* FIXME: we are being a bit inconsistent here in the sense that will only
378    * map the language codes from our static table. Theoretically the iso-codes
379    * XML file might have had additional codes that are now in the hash table.
380    * We keep it simple for now and don't waste memory on additional tables. */
381   for (i = 0; i < G_N_ELEMENTS (iso_639_codes); ++i) {
382     /* we check both codes here, so function can be used in a more versatile
383      * way, to convert a language tag to a two-letter language code and/or
384      * verify an existing code */
385     if (strcmp (lang_code, iso_639_codes[i].iso_639_1) == 0 ||
386         strcmp (lang_code, iso_639_codes[i].iso_639_2) == 0) {
387       c = iso_639_codes[i].iso_639_1;
388       break;
389     }
390   }
391
392   GST_LOG ("%s -> %s", lang_code, GST_STR_NULL (c));
393
394   return c;
395 }
396
397 static const gchar *
398 gst_tag_get_language_code_iso_639_2X (const gchar * lang_code, guint8 flags)
399 {
400   int i;
401
402   /* FIXME: we are being a bit inconsistent here in the sense that we will only
403    * map the language codes from our static table. Theoretically the iso-codes
404    * XML file might have had additional codes that are now in the hash table.
405    * We keep it simple for now and don't waste memory on additional tables.
406    * Also, we currently only parse the iso_639.xml file if language names or
407    * a list of all codes is requested, and it'd be nice to keep it like that. */
408   for (i = 0; i < G_N_ELEMENTS (iso_639_codes); ++i) {
409     /* we check both codes here, so function can be used in a more versatile
410      * way, to convert a language tag to a three-letter language code and/or
411      * verify an existing code */
412     if (strcmp (lang_code, iso_639_codes[i].iso_639_1) == 0 ||
413         strcmp (lang_code, iso_639_codes[i].iso_639_2) == 0) {
414       if ((iso_639_codes[i].flags & flags) == flags) {
415         return iso_639_codes[i].iso_639_2;
416       } else if (i > 0 && (iso_639_codes[i - 1].flags & flags) == flags &&
417           iso_639_codes[i].name_offset == iso_639_codes[i - 1].name_offset) {
418         return iso_639_codes[i - 1].iso_639_2;
419       } else if (i < G_N_ELEMENTS (iso_639_codes) &&
420           (iso_639_codes[i + 1].flags & flags) == flags &&
421           iso_639_codes[i].name_offset == iso_639_codes[i + 1].name_offset) {
422         return iso_639_codes[i + 1].iso_639_2;
423       }
424     }
425   }
426   return NULL;
427 }
428
429 /**
430  * gst_tag_get_language_code_iso_639_2T:
431  * @lang_code: ISO-639 language code (e.g. "deu" or "ger" or "de")
432  *
433  * Returns three-letter ISO-639-2 "terminological" language code given a
434  * two-letter ISO-639-1 language code or a three-letter ISO-639-2 language
435  * code (both are accepted for convenience).
436  *
437  * The "terminological" code is derived from the local name of the language
438  * (e.g. "deu" for German instead of "ger"). In most scenarios, the
439  * "terminological" codes are prefered over the "bibliographic" ones.
440  *
441  * Language codes are case-sensitive and expected to be lower case.
442  *
443  * Returns: three-letter ISO-639-2 language code string that maps to @lang_code,
444  *     or NULL if no mapping is known. The returned string must not be
445  *     modified or freed.
446  *
447  * Since: 0.10.26
448  */
449 const gchar *
450 gst_tag_get_language_code_iso_639_2T (const gchar * lang_code)
451 {
452   const gchar *c;
453
454   g_return_val_if_fail (lang_code != NULL, NULL);
455
456   ensure_debug_category ();
457
458   c = gst_tag_get_language_code_iso_639_2X (lang_code, ISO_639_FLAG_2T);
459
460   GST_LOG ("%s -> %s", lang_code, GST_STR_NULL (c));
461
462   return c;
463 }
464
465 /**
466  * gst_tag_get_language_code_iso_639_2B:
467  * @lang_code: ISO-639 language code (e.g. "deu" or "ger" or "de")
468  *
469  * Returns three-letter ISO-639-2 "bibliographic" language code given a
470  * two-letter ISO-639-1 language code or a three-letter ISO-639-2 language
471  * code (both are accepted for convenience).
472  *
473  * The "bibliographic" code is derived from the English name of the language
474  * (e.g. "ger" for German instead of "de" or "deu"). In most scenarios, the
475  * "terminological" codes are prefered.
476  *
477  * Language codes are case-sensitive and expected to be lower case.
478  *
479  * Returns: three-letter ISO-639-2 language code string that maps to @lang_code,
480  *     or NULL if no mapping is known. The returned string must not be
481  *     modified or freed.
482  *
483  * Since: 0.10.26
484  */
485 const gchar *
486 gst_tag_get_language_code_iso_639_2B (const gchar * lang_code)
487 {
488   const gchar *c;
489
490   g_return_val_if_fail (lang_code != NULL, NULL);
491
492   ensure_debug_category ();
493
494   c = gst_tag_get_language_code_iso_639_2X (lang_code, ISO_639_FLAG_2B);
495
496   GST_LOG ("%s -> %s", lang_code, GST_STR_NULL (c));
497
498   return c;
499 }