diff --git a/libavformat/mp3.c b/libavformat/mp3.c index a9134f180f..857661ba60 100644 --- a/libavformat/mp3.c +++ b/libavformat/mp3.c @@ -526,35 +526,44 @@ static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt) } #if CONFIG_MP2_MUXER || CONFIG_MP3_MUXER -static void id3v1_create_tag(AVFormatContext *s, uint8_t *buf) +static int id3v1_set_string(AVFormatContext *s, const char *key, + uint8_t *buf, int buf_size) { - int v, i; + AVMetadataTag *tag; + if ((tag = av_metadata_get(s->metadata, key, NULL, 0))) + strncpy(buf, tag->value, buf_size); + return !!tag; +} + +static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf) +{ + AVMetadataTag *tag; + int i, count = 0; memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */ buf[0] = 'T'; buf[1] = 'A'; buf[2] = 'G'; - strncpy(buf + 3, s->title, 30); - strncpy(buf + 33, s->author, 30); - strncpy(buf + 63, s->album, 30); - v = s->year; - if (v > 0) { - for(i = 0;i < 4; i++) { - buf[96 - i] = '0' + (v % 10); - v = v / 10; - } - } - strncpy(buf + 97, s->comment, 30); - if (s->track != 0) { + count += id3v1_set_string(s, "title", buf + 3, 30); + count += id3v1_set_string(s, "author", buf + 33, 30); + count += id3v1_set_string(s, "album", buf + 63, 30); + count += id3v1_set_string(s, "year", buf + 93, 4); + count += id3v1_set_string(s, "comment", buf + 97, 30); + if ((tag = av_metadata_get(s->metadata, "track", NULL, 0))) { buf[125] = 0; - buf[126] = s->track; + buf[126] = atoi(tag->value); + count++; } + if ((tag = av_metadata_get(s->metadata, "genre", NULL, 0))) { for(i = 0; i <= ID3v1_GENRE_MAX; i++) { - if (!strcasecmp(s->genre, id3v1_genre_str[i])) { + if (!strcasecmp(tag->value, id3v1_genre_str[i])) { buf[127] = i; + count++; break; } } + } + return count; } /* simple formats */ @@ -584,22 +593,24 @@ static void id3v2_put_ttag(AVFormatContext *s, const char *string, uint32_t tag) static int mp3_write_header(struct AVFormatContext *s) { + AVMetadataTag *title, *author, *album, *genre, *copyright, *track, *year; int totlen = 0; - char tracktxt[10]; - char yeartxt[10]; - if(s->track) - snprintf(tracktxt, sizeof(tracktxt), "%d", s->track); - if(s->year) - snprintf( yeartxt, sizeof(yeartxt) , "%d", s->year ); + title = av_metadata_get(s->metadata, "title", NULL, 0); + author = av_metadata_get(s->metadata, "author", NULL, 0); + album = av_metadata_get(s->metadata, "album", NULL, 0); + genre = av_metadata_get(s->metadata, "genre", NULL, 0); + copyright = av_metadata_get(s->metadata, "copyright", NULL, 0); + track = av_metadata_get(s->metadata, "track", NULL, 0); + year = av_metadata_get(s->metadata, "year", NULL, 0); - if(s->title[0]) totlen += 11 + strlen(s->title); - if(s->author[0]) totlen += 11 + strlen(s->author); - if(s->album[0]) totlen += 11 + strlen(s->album); - if(s->genre[0]) totlen += 11 + strlen(s->genre); - if(s->copyright[0]) totlen += 11 + strlen(s->copyright); - if(s->track) totlen += 11 + strlen(tracktxt); - if(s->year) totlen += 11 + strlen(yeartxt); + if(title) totlen += 11 + strlen(title->value); + if(author) totlen += 11 + strlen(author->value); + if(album) totlen += 11 + strlen(album->value); + if(genre) totlen += 11 + strlen(genre->value); + if(copyright) totlen += 11 + strlen(copyright->value); + if(track) totlen += 11 + strlen(track->value); + if(year) totlen += 11 + strlen(year->value); if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) totlen += strlen(LIBAVFORMAT_IDENT) + 11; @@ -612,13 +623,13 @@ static int mp3_write_header(struct AVFormatContext *s) id3v2_put_size(s, totlen); - if(s->title[0]) id3v2_put_ttag(s, s->title, MKBETAG('T', 'I', 'T', '2')); - if(s->author[0]) id3v2_put_ttag(s, s->author, MKBETAG('T', 'P', 'E', '1')); - if(s->album[0]) id3v2_put_ttag(s, s->album, MKBETAG('T', 'A', 'L', 'B')); - if(s->genre[0]) id3v2_put_ttag(s, s->genre, MKBETAG('T', 'C', 'O', 'N')); - if(s->copyright[0]) id3v2_put_ttag(s, s->copyright, MKBETAG('T', 'C', 'O', 'P')); - if(s->track) id3v2_put_ttag(s, tracktxt, MKBETAG('T', 'R', 'C', 'K')); - if(s->year) id3v2_put_ttag(s, yeartxt, MKBETAG('T', 'Y', 'E', 'R')); + if(title) id3v2_put_ttag(s, title->value, MKBETAG('T', 'I', 'T', '2')); + if(author) id3v2_put_ttag(s, author->value, MKBETAG('T', 'P', 'E', '1')); + if(album) id3v2_put_ttag(s, album->value, MKBETAG('T', 'A', 'L', 'B')); + if(genre) id3v2_put_ttag(s, genre->value, MKBETAG('T', 'C', 'O', 'N')); + if(copyright) id3v2_put_ttag(s, copyright->value, MKBETAG('T', 'C', 'O', 'P')); + if(track) id3v2_put_ttag(s, track->value, MKBETAG('T', 'R', 'C', 'K')); + if(year) id3v2_put_ttag(s, year->value, MKBETAG('T', 'Y', 'E', 'R')); if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C')); return 0; @@ -636,8 +647,7 @@ static int mp3_write_trailer(struct AVFormatContext *s) uint8_t buf[ID3v1_TAG_SIZE]; /* write the id3v1 tag */ - if (s->title[0] != '\0') { - id3v1_create_tag(s, buf); + if (id3v1_create_tag(s, buf) > 0) { put_buffer(s->pb, buf, ID3v1_TAG_SIZE); put_flush_packet(s->pb); }