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