diff mov.c @ 890:1e49bfea0ffb libavformat

finally found what those >138 codes were... crappy compressed 5bit ascii. this gets them correctly, and adds setting track lang in movenc too.
author mmu_man
date Fri, 30 Dec 2005 20:34:08 +0000
parents 22b044eeeaed
children edbe5c3717f9
line wrap: on
line diff
--- a/mov.c	Fri Dec 30 18:25:46 2005 +0000
+++ b/mov.c	Fri Dec 30 20:34:08 2005 +0000
@@ -151,6 +151,8 @@
 
 /* map numeric codes from mdhd atom to ISO 639 */
 /* cf. QTFileFormat.pdf p253, qtff.pdf p205 */
+/* http://developer.apple.com/documentation/mac/Text/Text-368.html */
+/* deprecated by putting the code as 3*5bit ascii */
 static const char *mov_mdhd_language_map[] = { 
 /* 0-9 */
 "eng", "fra", "ger", "ita", "dut", "sve", "spa", "dan", "por", "nor", 
@@ -353,24 +355,54 @@
 #define print_atom(a,b)
 #endif
 
-const char *ff_mov_lang_to_iso639(int code)
+static int ff_mov_lang_to_iso639(int code, char *to)
 {
+    int i;
+    /* is it the mangled iso code? */
+    /* see http://www.geocities.com/xhelmboyx/quicktime/formats/mp4-layout.txt */
+    if (code > 138) {
+        for (i = 2; i >= 0; i--) {
+            to[i] = 0x60 + (code & 0x1f);
+            code >>= 5;
+        }
+        return 1;
+    }
+    /* old fashion apple lang code */
     if (code >= (sizeof(mov_mdhd_language_map)/sizeof(char *)))
-        return NULL;
+        return 0;
     if (!mov_mdhd_language_map[code])
-        return NULL;
-    return mov_mdhd_language_map[code];
+        return 0;
+    strncpy(to, mov_mdhd_language_map[code], 4);
+    return 1;
 }
 
-extern int ff_mov_iso639_to_lang(const char *lang); /* for movenc.c */
-int ff_mov_iso639_to_lang(const char *lang)
+extern int ff_mov_iso639_to_lang(const char *lang, int mp4); /* for movenc.c */
+int ff_mov_iso639_to_lang(const char *lang, int mp4)
 {
-    int i;
-    for (i = 0; i < (sizeof(mov_mdhd_language_map)/sizeof(char *)); i++) {
+    int i, code = 0;
+    
+    /* old way, only for QT? */
+    for (i = 0; !mp4 && (i < (sizeof(mov_mdhd_language_map)/sizeof(char *))); i++) {
         if (mov_mdhd_language_map[i] && !strcmp(lang, mov_mdhd_language_map[i]))
             return i;
     }
-    return -1;
+    /* XXX:can we do that in mov too? */
+    if (!mp4)
+        return 0;
+    /* handle undefined as such */
+    if (lang[0] == '\0')
+        lang = "und";
+    /* 5bit ascii */
+    for (i = 0; i < 3; i++) {
+        unsigned char c = (unsigned char)lang[i];
+        if (c < 0x60)
+            return 0;
+        if (c > 0x60 + 0x1f)
+            return 0;
+        code <<= 5;
+        code |= (c - 0x60);
+    }
+    return code;
 }
 
 static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
@@ -675,7 +707,6 @@
 {
     int version;
     int lang;
-    const char *iso639;
     print_atom("mdhd", atom);
 
     version = get_byte(pb); /* version */
@@ -697,9 +728,7 @@
     c->fc->streams[c->fc->nb_streams-1]->duration = (version==1)?get_be64(pb):get_be32(pb); /* duration */
 
     lang = get_be16(pb); /* language */
-    iso639 = ff_mov_lang_to_iso639(lang);
-    if (iso639)
-        strncpy(c->fc->streams[c->fc->nb_streams-1]->language, iso639, 4);
+    ff_mov_lang_to_iso639(lang, c->fc->streams[c->fc->nb_streams-1]->language);
     get_be16(pb); /* quality */
 
     return 0;