Revision b182eeb0

View differences:

libavformat/mp3.c
526 526
}
527 527

  
528 528
#if CONFIG_MP2_MUXER || CONFIG_MP3_MUXER
529
static void id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
529
static int id3v1_set_string(AVFormatContext *s, const char *key,
530
                            uint8_t *buf, int buf_size)
530 531
{
531
    int v, i;
532
    AVMetadataTag *tag;
533
    if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
534
        strncpy(buf, tag->value, buf_size);
535
    return !!tag;
536
}
537

  
538
static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
539
{
540
    AVMetadataTag *tag;
541
    int i, count = 0;
532 542

  
533 543
    memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
534 544
    buf[0] = 'T';
535 545
    buf[1] = 'A';
536 546
    buf[2] = 'G';
537
    strncpy(buf + 3, s->title, 30);
538
    strncpy(buf + 33, s->author, 30);
539
    strncpy(buf + 63, s->album, 30);
540
    v = s->year;
541
    if (v > 0) {
542
        for(i = 0;i < 4; i++) {
543
            buf[96 - i] = '0' + (v % 10);
544
            v = v / 10;
545
        }
546
    }
547
    strncpy(buf + 97, s->comment, 30);
548
    if (s->track != 0) {
547
    count += id3v1_set_string(s, "title",   buf +  3, 30);
548
    count += id3v1_set_string(s, "author",  buf + 33, 30);
549
    count += id3v1_set_string(s, "album",   buf + 63, 30);
550
    count += id3v1_set_string(s, "year",    buf + 93,  4);
551
    count += id3v1_set_string(s, "comment", buf + 97, 30);
552
    if ((tag = av_metadata_get(s->metadata, "track", NULL, 0))) {
549 553
        buf[125] = 0;
550
        buf[126] = s->track;
554
        buf[126] = atoi(tag->value);
555
        count++;
551 556
    }
557
    if ((tag = av_metadata_get(s->metadata, "genre", NULL, 0))) {
552 558
    for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
553
        if (!strcasecmp(s->genre, id3v1_genre_str[i])) {
559
        if (!strcasecmp(tag->value, id3v1_genre_str[i])) {
554 560
            buf[127] = i;
561
            count++;
555 562
            break;
556 563
        }
557 564
    }
565
    }
566
    return count;
558 567
}
559 568

  
560 569
/* simple formats */
......
584 593

  
585 594
static int mp3_write_header(struct AVFormatContext *s)
586 595
{
596
    AVMetadataTag *title, *author, *album, *genre, *copyright, *track, *year;
587 597
    int totlen = 0;
588
    char tracktxt[10];
589
    char yeartxt[10];
590

  
591
    if(s->track)
592
        snprintf(tracktxt, sizeof(tracktxt), "%d", s->track);
593
    if(s->year)
594
        snprintf( yeartxt, sizeof(yeartxt) , "%d", s->year );
595

  
596
    if(s->title[0])     totlen += 11 + strlen(s->title);
597
    if(s->author[0])    totlen += 11 + strlen(s->author);
598
    if(s->album[0])     totlen += 11 + strlen(s->album);
599
    if(s->genre[0])     totlen += 11 + strlen(s->genre);
600
    if(s->copyright[0]) totlen += 11 + strlen(s->copyright);
601
    if(s->track)        totlen += 11 + strlen(tracktxt);
602
    if(s->year)         totlen += 11 + strlen(yeartxt);
598

  
599
    title     = av_metadata_get(s->metadata, "title",     NULL, 0);
600
    author    = av_metadata_get(s->metadata, "author",    NULL, 0);
601
    album     = av_metadata_get(s->metadata, "album",     NULL, 0);
602
    genre     = av_metadata_get(s->metadata, "genre",     NULL, 0);
603
    copyright = av_metadata_get(s->metadata, "copyright", NULL, 0);
604
    track     = av_metadata_get(s->metadata, "track",     NULL, 0);
605
    year      = av_metadata_get(s->metadata, "year",      NULL, 0);
606

  
607
    if(title)     totlen += 11 + strlen(title->value);
608
    if(author)    totlen += 11 + strlen(author->value);
609
    if(album)     totlen += 11 + strlen(album->value);
610
    if(genre)     totlen += 11 + strlen(genre->value);
611
    if(copyright) totlen += 11 + strlen(copyright->value);
612
    if(track)     totlen += 11 + strlen(track->value);
613
    if(year)      totlen += 11 + strlen(year->value);
603 614
    if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
604 615
        totlen += strlen(LIBAVFORMAT_IDENT) + 11;
605 616

  
......
612 623

  
613 624
    id3v2_put_size(s, totlen);
614 625

  
615
    if(s->title[0])     id3v2_put_ttag(s, s->title,     MKBETAG('T', 'I', 'T', '2'));
616
    if(s->author[0])    id3v2_put_ttag(s, s->author,    MKBETAG('T', 'P', 'E', '1'));
617
    if(s->album[0])     id3v2_put_ttag(s, s->album,     MKBETAG('T', 'A', 'L', 'B'));
618
    if(s->genre[0])     id3v2_put_ttag(s, s->genre,     MKBETAG('T', 'C', 'O', 'N'));
619
    if(s->copyright[0]) id3v2_put_ttag(s, s->copyright, MKBETAG('T', 'C', 'O', 'P'));
620
    if(s->track)        id3v2_put_ttag(s, tracktxt,     MKBETAG('T', 'R', 'C', 'K'));
621
    if(s->year)         id3v2_put_ttag(s, yeartxt,      MKBETAG('T', 'Y', 'E', 'R'));
626
    if(title)     id3v2_put_ttag(s, title->value,     MKBETAG('T', 'I', 'T', '2'));
627
    if(author)    id3v2_put_ttag(s, author->value,    MKBETAG('T', 'P', 'E', '1'));
628
    if(album)     id3v2_put_ttag(s, album->value,     MKBETAG('T', 'A', 'L', 'B'));
629
    if(genre)     id3v2_put_ttag(s, genre->value,     MKBETAG('T', 'C', 'O', 'N'));
630
    if(copyright) id3v2_put_ttag(s, copyright->value, MKBETAG('T', 'C', 'O', 'P'));
631
    if(track)     id3v2_put_ttag(s, track->value,     MKBETAG('T', 'R', 'C', 'K'));
632
    if(year)      id3v2_put_ttag(s, year->value,      MKBETAG('T', 'Y', 'E', 'R'));
622 633
    if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
623 634
        id3v2_put_ttag(s, LIBAVFORMAT_IDENT,            MKBETAG('T', 'E', 'N', 'C'));
624 635
    return 0;
......
636 647
    uint8_t buf[ID3v1_TAG_SIZE];
637 648

  
638 649
    /* write the id3v1 tag */
639
    if (s->title[0] != '\0') {
640
        id3v1_create_tag(s, buf);
650
    if (id3v1_create_tag(s, buf) > 0) {
641 651
        put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
642 652
        put_flush_packet(s->pb);
643 653
    }

Also available in: Unified diff