]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blobdiff - libavformat/mp3.c
frsh: Export information about the last RTP contract and VRES
[frescor/ffmpeg.git] / libavformat / mp3.c
index 346c7ea1585faf4bf25e39905391dac042d21c04..e835a3442420061f0ea5c11325b93ccefaf3cdf5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * MP3 muxer and demuxer
- * Copyright (c) 2003 Fabrice Bellard.
+ * Copyright (c) 2003 Fabrice Bellard
  *
  * This file is part of FFmpeg.
  *
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <strings.h>
 #include "libavutil/avstring.h"
 #include "libavcodec/mpegaudio.h"
 #include "libavcodec/mpegaudiodecheader.h"
 #include "avformat.h"
+#include "id3v2.h"
 
-#define ID3v2_HEADER_SIZE 10
 #define ID3v1_TAG_SIZE 128
 
 #define ID3v1_GENRE_MAX 125
 
-static const char *id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
+static const char * const id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
     [0] = "Blues",
     [1] = "Classic Rock",
     [2] = "Country",
@@ -158,20 +159,6 @@ static const char *id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
     [125] = "Dance Hall",
 };
 
-/* buf must be ID3v2_HEADER_SIZE byte long */
-static int id3v2_match(const uint8_t *buf)
-{
-    return  buf[0] == 'I' &&
-            buf[1] == 'D' &&
-            buf[2] == '3' &&
-            buf[3] != 0xff &&
-            buf[4] != 0xff &&
-            (buf[6] & 0x80) == 0 &&
-            (buf[7] & 0x80) == 0 &&
-            (buf[8] & 0x80) == 0 &&
-            (buf[9] & 0x80) == 0;
-}
-
 static unsigned int id3v2_get_size(ByteIOContext *s, int len)
 {
     int v=0;
@@ -180,18 +167,17 @@ static unsigned int id3v2_get_size(ByteIOContext *s, int len)
     return v;
 }
 
-static void id3v2_read_ttag(AVFormatContext *s, int taglen, char *dst, int dstlen)
+static void id3v2_read_ttag(AVFormatContext *s, int taglen, const char *key)
 {
-    char *q;
-    int len;
+    char *q, dst[512];
+    int len, dstlen = sizeof(dst) - 1;
+    unsigned genre;
 
-    if(dstlen > 0)
-        dst[0]= 0;
+    dst[0]= 0;
     if(taglen < 1)
         return;
 
     taglen--; /* account for encoding type byte */
-    dstlen--; /* Leave space for zero terminator */
 
     switch(get_byte(s->pb)) { /* encoding type */
 
@@ -210,6 +196,14 @@ static void id3v2_read_ttag(AVFormatContext *s, int taglen, char *dst, int dstle
         dst[len] = 0;
         break;
     }
+
+    if (!strcmp(key, "genre")
+        && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
+        && genre <= ID3v1_GENRE_MAX)
+        av_strlcpy(dst, id3v1_genre_str[genre], sizeof(dst));
+
+    if (*dst)
+        av_metadata_set(&s->metadata, key, dst);
 }
 
 /**
@@ -223,8 +217,7 @@ static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t fl
 {
     int isv34, tlen;
     uint32_t tag;
-    offset_t next;
-    char tmp[16];
+    int64_t next;
     int taghdrlen;
     const char *reason;
 
@@ -276,28 +269,27 @@ static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t fl
         switch(tag) {
         case MKBETAG('T', 'I', 'T', '2'):
         case MKBETAG(0,   'T', 'T', '2'):
-            id3v2_read_ttag(s, tlen, s->title, sizeof(s->title));
+            id3v2_read_ttag(s, tlen, "title");
             break;
         case MKBETAG('T', 'P', 'E', '1'):
         case MKBETAG(0,   'T', 'P', '1'):
-            id3v2_read_ttag(s, tlen, s->author, sizeof(s->author));
+            id3v2_read_ttag(s, tlen, "author");
             break;
         case MKBETAG('T', 'A', 'L', 'B'):
         case MKBETAG(0,   'T', 'A', 'L'):
-            id3v2_read_ttag(s, tlen, s->album, sizeof(s->album));
+            id3v2_read_ttag(s, tlen, "album");
             break;
         case MKBETAG('T', 'C', 'O', 'N'):
         case MKBETAG(0,   'T', 'C', 'O'):
-            id3v2_read_ttag(s, tlen, s->genre, sizeof(s->genre));
+            id3v2_read_ttag(s, tlen, "genre");
             break;
         case MKBETAG('T', 'C', 'O', 'P'):
         case MKBETAG(0,   'T', 'C', 'R'):
-            id3v2_read_ttag(s, tlen, s->copyright, sizeof(s->copyright));
+            id3v2_read_ttag(s, tlen, "copyright");
             break;
         case MKBETAG('T', 'R', 'C', 'K'):
         case MKBETAG(0,   'T', 'R', 'K'):
-            id3v2_read_ttag(s, tlen, tmp, sizeof(tmp));
-            s->track = atoi(tmp);
+            id3v2_read_ttag(s, tlen, "track");
             break;
         case 0:
             /* padding, skip to end */
@@ -318,22 +310,25 @@ static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t fl
     url_fskip(s->pb, len);
 }
 
-static void id3v1_get_string(char *str, int str_size,
+static void id3v1_get_string(AVFormatContext *s, const char *key,
                              const uint8_t *buf, int buf_size)
 {
     int i, c;
-    char *q;
+    char *q, str[512];
 
     q = str;
     for(i = 0; i < buf_size; i++) {
         c = buf[i];
         if (c == '\0')
             break;
-        if ((q - str) >= str_size - 1)
+        if ((q - str) >= sizeof(str) - 1)
             break;
         *q++ = c;
     }
     *q = '\0';
+
+    if (*str)
+        av_metadata_set(&s->metadata, key, str);
 }
 
 /* 'buf' must be ID3v1_TAG_SIZE byte long */
@@ -346,17 +341,18 @@ static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf)
           buf[1] == 'A' &&
           buf[2] == 'G'))
         return -1;
-    id3v1_get_string(s->title, sizeof(s->title), buf + 3, 30);
-    id3v1_get_string(s->author, sizeof(s->author), buf + 33, 30);
-    id3v1_get_string(s->album, sizeof(s->album), buf + 63, 30);
-    id3v1_get_string(str, sizeof(str), buf + 93, 4);
-    s->year = atoi(str);
-    id3v1_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
-    if (buf[125] == 0 && buf[126] != 0)
-        s->track = buf[126];
+    id3v1_get_string(s, "title",   buf +  3, 30);
+    id3v1_get_string(s, "author",  buf + 33, 30);
+    id3v1_get_string(s, "album",   buf + 63, 30);
+    id3v1_get_string(s, "year",    buf + 93,  4);
+    id3v1_get_string(s, "comment", buf + 97, 30);
+    if (buf[125] == 0 && buf[126] != 0) {
+        snprintf(str, sizeof(str), "%d", buf[126]);
+        av_metadata_set(&s->metadata, "track", str);
+    }
     genre = buf[127];
     if (genre <= ID3v1_GENRE_MAX)
-        av_strlcpy(s->genre, id3v1_genre_str[genre], sizeof(s->genre));
+        av_metadata_set(&s->metadata, "genre", id3v1_genre_str[genre]);
     return 0;
 }
 
@@ -367,54 +363,60 @@ static int mp3_read_probe(AVProbeData *p)
     int max_frames, first_frames = 0;
     int fsize, frames, sample_rate;
     uint32_t header;
-    uint8_t *buf, *buf2, *end;
+    uint8_t *buf, *buf0, *buf2, *end;
     AVCodecContext avctx;
 
-    if(id3v2_match(p->buf))
-        return AVPROBE_SCORE_MAX/2+1; // this must be less than mpeg-ps because some retards put id3v2 tags before mpeg-ps files
+    buf0 = p->buf;
+    if(ff_id3v2_match(buf0)) {
+        buf0 += ff_id3v2_tag_len(buf0);
+    }
 
     max_frames = 0;
-    buf = p->buf;
-    end = buf + p->buf_size - sizeof(uint32_t);
+    buf = buf0;
+    end = p->buf + p->buf_size - sizeof(uint32_t);
 
     for(; buf < end; buf= buf2+1) {
         buf2 = buf;
 
         for(frames = 0; buf2 < end; frames++) {
             header = AV_RB32(buf2);
-            fsize = ff_mpa_decode_header(&avctx, header, &sample_rate);
+            fsize = ff_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
             if(fsize < 0)
                 break;
             buf2 += fsize;
         }
         max_frames = FFMAX(max_frames, frames);
-        if(buf == p->buf)
+        if(buf == buf0)
             first_frames= frames;
     }
     if   (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
     else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
-    else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
+    else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
+    else if(buf0!=p->buf)  return AVPROBE_SCORE_MAX/4-1;
     else if(max_frames>=1) return 1;
     else                   return 0;
+//mpegps_mp3_unrecognized_format.mpg has max_frames=3
 }
 
 /**
  * Try to find Xing/Info/VBRI tags and compute duration from info therein
  */
-static void mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, offset_t base)
+static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
 {
     uint32_t v, spf;
     int frames = -1; /* Total number of frames in file */
-    const offset_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
-    MPADecodeContext c;
+    const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
+    MPADecodeHeader c;
+    int vbrtag_size = 0;
 
     v = get_be32(s->pb);
     if(ff_mpa_check_header(v) < 0)
-      return;
+      return -1;
 
-    ff_mpegaudio_decode_header(&c, v);
+    if (ff_mpegaudio_decode_header(&c, v) == 0)
+        vbrtag_size = c.frame_size;
     if(c.layer != 3)
-        return;
+        return -1;
 
     /* Check for Xing / Info tag */
     url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
@@ -438,11 +440,15 @@ static void mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, offset_t base)
     }
 
     if(frames < 0)
-        return;
+        return -1;
+
+    /* Skip the vbr tag frame */
+    url_fseek(s->pb, base + vbrtag_size, SEEK_SET);
 
     spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
     st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
                                 st->time_base);
+    return 0;
 }
 
 static int mp3_read_header(AVFormatContext *s,
@@ -451,7 +457,7 @@ static int mp3_read_header(AVFormatContext *s,
     AVStream *st;
     uint8_t buf[ID3v1_TAG_SIZE];
     int len, ret, filesize;
-    offset_t off;
+    int64_t off;
 
     st = av_new_stream(s, 0);
     if (!st)
@@ -480,7 +486,7 @@ static int mp3_read_header(AVFormatContext *s,
     ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
     if (ret != ID3v2_HEADER_SIZE)
         return -1;
-    if (id3v2_match(buf)) {
+    if (ff_id3v2_match(buf)) {
         /* parse ID3v2 header */
         len = ((buf[6] & 0x7f) << 21) |
             ((buf[7] & 0x7f) << 14) |
@@ -492,8 +498,8 @@ static int mp3_read_header(AVFormatContext *s,
     }
 
     off = url_ftell(s->pb);
-    mp3_parse_vbr_tags(s, st, off);
-    url_fseek(s->pb, off, SEEK_SET);
+    if (mp3_parse_vbr_tags(s, st, off) < 0)
+        url_fseek(s->pb, off, SEEK_SET);
 
     /* the parameters will be extracted from the compressed bitstream */
     return 0;
@@ -520,36 +526,45 @@ static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
     return ret;
 }
 
-#ifdef CONFIG_MUXERS
-static void id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
+#if CONFIG_MP2_MUXER || CONFIG_MP3_MUXER
+static int id3v1_set_string(AVFormatContext *s, const char *key,
+                            uint8_t *buf, int buf_size)
+{
+    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)
 {
-    int v, i;
+    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++;
     }
-    for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
-        if (!strcasecmp(s->genre, id3v1_genre_str[i])) {
-            buf[127] = i;
-            break;
+    if ((tag = av_metadata_get(s->metadata, "genre", NULL, 0))) {
+        for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
+            if (!strcasecmp(tag->value, id3v1_genre_str[i])) {
+                buf[127] = i;
+                count++;
+                break;
+            }
         }
     }
+    return count;
 }
 
 /* simple formats */
@@ -579,22 +594,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 );
-
-    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);
+
+    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(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;
 
@@ -607,13 +624,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;
@@ -631,19 +648,18 @@ 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);
     }
     return 0;
 }
-#endif //CONFIG_MUXERS
+#endif /* CONFIG_MP2_MUXER || CONFIG_MP3_MUXER */
 
-#ifdef CONFIG_MP3_DEMUXER
+#if CONFIG_MP3_DEMUXER
 AVInputFormat mp3_demuxer = {
     "mp3",
-    NULL_IF_CONFIG_SMALL("MPEG audio"),
+    NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
     0,
     mp3_read_probe,
     mp3_read_header,
@@ -652,16 +668,12 @@ AVInputFormat mp3_demuxer = {
     .extensions = "mp2,mp3,m2a", /* XXX: use probe */
 };
 #endif
-#ifdef CONFIG_MP2_MUXER
+#if CONFIG_MP2_MUXER
 AVOutputFormat mp2_muxer = {
     "mp2",
     NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
     "audio/x-mpeg",
-#ifdef CONFIG_LIBMP3LAME
     "mp2,m2a",
-#else
-    "mp2,mp3,m2a",
-#endif
     0,
     CODEC_ID_MP2,
     CODEC_ID_NONE,
@@ -670,7 +682,7 @@ AVOutputFormat mp2_muxer = {
     mp3_write_trailer,
 };
 #endif
-#ifdef CONFIG_MP3_MUXER
+#if CONFIG_MP3_MUXER
 AVOutputFormat mp3_muxer = {
     "mp3",
     NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),