changeset 4355:d8a6a01679be libavformat

use new metadata API in the mp3 muxer
author aurel
date Mon, 02 Feb 2009 22:13:03 +0000
parents 6111dd03464b
children 459c0242194e
files mp3.c
diffstat 1 files changed, 49 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/mp3.c	Mon Feb 02 22:11:56 2009 +0000
+++ b/mp3.c	Mon Feb 02 22:13:03 2009 +0000
@@ -526,35 +526,44 @@
 }
 
 #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;
-        }
+    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] = atoi(tag->value);
+        count++;
     }
-    strncpy(buf + 97, s->comment, 30);
-    if (s->track != 0) {
-        buf[125] = 0;
-        buf[126] = s->track;
-    }
+    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 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 @@
 
     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 @@
     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);
     }