Revision ab561df9

View differences:

libavformat/mov.c
151 151

  
152 152
/* map numeric codes from mdhd atom to ISO 639 */
153 153
/* cf. QTFileFormat.pdf p253, qtff.pdf p205 */
154
/* http://developer.apple.com/documentation/mac/Text/Text-368.html */
155
/* deprecated by putting the code as 3*5bit ascii */
154 156
static const char *mov_mdhd_language_map[] = { 
155 157
/* 0-9 */
156 158
"eng", "fra", "ger", "ita", "dut", "sve", "spa", "dan", "por", "nor", 
......
353 355
#define print_atom(a,b)
354 356
#endif
355 357

  
356
const char *ff_mov_lang_to_iso639(int code)
358
static int ff_mov_lang_to_iso639(int code, char *to)
357 359
{
360
    int i;
361
    /* is it the mangled iso code? */
362
    /* see http://www.geocities.com/xhelmboyx/quicktime/formats/mp4-layout.txt */
363
    if (code > 138) {
364
        for (i = 2; i >= 0; i--) {
365
            to[i] = 0x60 + (code & 0x1f);
366
            code >>= 5;
367
        }
368
        return 1;
369
    }
370
    /* old fashion apple lang code */
358 371
    if (code >= (sizeof(mov_mdhd_language_map)/sizeof(char *)))
359
        return NULL;
372
        return 0;
360 373
    if (!mov_mdhd_language_map[code])
361
        return NULL;
362
    return mov_mdhd_language_map[code];
374
        return 0;
375
    strncpy(to, mov_mdhd_language_map[code], 4);
376
    return 1;
363 377
}
364 378

  
365
extern int ff_mov_iso639_to_lang(const char *lang); /* for movenc.c */
366
int ff_mov_iso639_to_lang(const char *lang)
379
extern int ff_mov_iso639_to_lang(const char *lang, int mp4); /* for movenc.c */
380
int ff_mov_iso639_to_lang(const char *lang, int mp4)
367 381
{
368
    int i;
369
    for (i = 0; i < (sizeof(mov_mdhd_language_map)/sizeof(char *)); i++) {
382
    int i, code = 0;
383
    
384
    /* old way, only for QT? */
385
    for (i = 0; !mp4 && (i < (sizeof(mov_mdhd_language_map)/sizeof(char *))); i++) {
370 386
        if (mov_mdhd_language_map[i] && !strcmp(lang, mov_mdhd_language_map[i]))
371 387
            return i;
372 388
    }
373
    return -1;
389
    /* XXX:can we do that in mov too? */
390
    if (!mp4)
391
        return 0;
392
    /* handle undefined as such */
393
    if (lang[0] == '\0')
394
        lang = "und";
395
    /* 5bit ascii */
396
    for (i = 0; i < 3; i++) {
397
        unsigned char c = (unsigned char)lang[i];
398
        if (c < 0x60)
399
            return 0;
400
        if (c > 0x60 + 0x1f)
401
            return 0;
402
        code <<= 5;
403
        code |= (c - 0x60);
404
    }
405
    return code;
374 406
}
375 407

  
376 408
static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
......
675 707
{
676 708
    int version;
677 709
    int lang;
678
    const char *iso639;
679 710
    print_atom("mdhd", atom);
680 711

  
681 712
    version = get_byte(pb); /* version */
......
697 728
    c->fc->streams[c->fc->nb_streams-1]->duration = (version==1)?get_be64(pb):get_be32(pb); /* duration */
698 729

  
699 730
    lang = get_be16(pb); /* language */
700
    iso639 = ff_mov_lang_to_iso639(lang);
701
    if (iso639)
702
        strncpy(c->fc->streams[c->fc->nb_streams-1]->language, iso639, 4);
731
    ff_mov_lang_to_iso639(lang, c->fc->streams[c->fc->nb_streams-1]->language);
703 732
    get_be16(pb); /* quality */
704 733

  
705 734
    return 0;
libavformat/movenc.c
52 52
    long        sampleCount;
53 53
    long        sampleDuration;
54 54
    int         hasKeyframes;
55
    int         language;
55 56
    int         trackID;
56 57
    AVCodecContext *enc;
57 58

  
......
72 73

  
73 74
static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
74 75

  
76
/* output language code from iso639 language name */
77
extern int ff_mov_iso639_to_lang(const char *lang, int mp4);
78

  
75 79
const CodecTag ff_mov_obj_type[] = {
76 80
    { CODEC_ID_MPEG4     ,  32 },
77 81
    { CODEC_ID_AAC       ,  64 },
......
701 705
    put_be32(pb, track->time); /* modification time */
702 706
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
703 707
    put_be32(pb, track->trackDuration); /* duration */
704
    put_be16(pb, 0); /* language, 0 = english */
708
    put_be16(pb, track->language); /* language */
705 709
    put_be16(pb, 0); /* reserved (quality) */
706 710
    return 32;
707 711
}
......
1331 1335
                    av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
1332 1336
            }
1333 1337
        }
1338
        /* don't know yet if mp4 or not */
1339
        mov->tracks[i].language = ff_mov_iso639_to_lang(s->streams[i]->language, 1);
1334 1340
    }
1335 1341

  
1336 1342
    /* Default mode == MP4 */

Also available in: Unified diff