]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mp3enc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / mp3enc.c
index ade6b0ad1ce044e2d52bd7827e8cd284a0d64eca..50342bb9509daaa83f6efccac44a448bbb981e9f 100644 (file)
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/opt.h"
+#include "libavcodec/mpegaudio.h"
 #include "libavcodec/mpegaudiodata.h"
 #include "libavcodec/mpegaudiodecheader.h"
 #include "libavformat/avio_internal.h"
+#include "libavutil/dict.h"
 
 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)))
+    AVDictionaryEntry *tag;
+    if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
         av_strlcpy(buf, tag->value, buf_size);
     return !!tag;
 }
 
 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
 {
-    AVMetadataTag *tag;
+    AVDictionaryEntry *tag;
     int i, count = 0;
 
     memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
@@ -54,13 +56,13 @@ static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
     count += id3v1_set_string(s, "TALB",    buf + 63, 30);       //album
     count += id3v1_set_string(s, "TDRL",    buf + 93,  4);       //date
     count += id3v1_set_string(s, "comment", buf + 97, 30);
-    if ((tag = av_metadata_get(s->metadata, "TRCK", NULL, 0))) { //track
+    if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
         buf[125] = 0;
         buf[126] = atoi(tag->value);
         count++;
     }
     buf[127] = 0xFF; /* default to unknown genre */
-    if ((tag = av_metadata_get(s->metadata, "TCON", NULL, 0))) { //genre
+    if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre
         for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
             if (!strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
                 buf[127] = i;
@@ -163,18 +165,13 @@ AVOutputFormat ff_mp2_muxer = {
 typedef struct MP3Context {
     const AVClass *class;
     int id3v2_version;
-    struct xing_header {
-        int64_t offset;
-        int32_t frames;
-        int32_t size;
-        /* following lame's "VbrTag.c". */
-        struct xing_toc {
-            uint32_t want;
-            uint32_t seen;
-            uint32_t pos;
-            uint64_t bag[VBR_NUM_BAGS];
-        } toc;
-    } xing_header;
+    int64_t frames_offset;
+    int32_t frames;
+    int32_t size;
+    uint32_t want;
+    uint32_t seen;
+    uint32_t pos;
+    uint64_t bag[VBR_NUM_BAGS];
 } MP3Context;
 
 static const AVOption options[] = {
@@ -190,7 +187,7 @@ static const AVClass mp3_muxer_class = {
     .version        = LIBAVUTIL_VERSION_INT,
 };
 
-static int id3v2_check_write_tag(AVFormatContext *s, AVMetadataTag *t, const char table[][4],
+static int id3v2_check_write_tag(AVFormatContext *s, AVDictionaryEntry *t, const char table[][4],
                                  enum ID3v2Encoding enc)
 {
     uint32_t tag;
@@ -271,11 +268,11 @@ static int mp3_write_xing(AVFormatContext *s)
     avio_wb32(s->pb, MKBETAG('X', 'i', 'n', 'g'));
     avio_wb32(s->pb, 0x01 | 0x02 | 0x04);  // frames/size/toc
 
-    mp3->xing_header.offset = avio_tell(s->pb);
-    mp3->xing_header.size = c.frame_size;
-    mp3->xing_header.toc.want=1;
-    mp3->xing_header.toc.seen=0;
-    mp3->xing_header.toc.pos=0;
+    mp3->frames_offset = avio_tell(s->pb);
+    mp3->size = c.frame_size;
+    mp3->want=1;
+    mp3->seen=0;
+    mp3->pos=0;
 
     avio_wb32(s->pb, 0);  // frames
     avio_wb32(s->pb, 0);  // size
@@ -297,50 +294,45 @@ static int mp3_write_xing(AVFormatContext *s)
 static void mp3_xing_add_frame(AVFormatContext *s, AVPacket *pkt)
 {
     MP3Context  *mp3 = s->priv_data;
-    struct xing_header *xing_header = &mp3->xing_header;
-    struct xing_toc *toc = &xing_header->toc;
     int i;
 
-    ++xing_header->frames;
-    xing_header->size += pkt->size;
+    ++mp3->frames;
+    mp3->size += pkt->size;
 
-    if (toc->want == ++toc->seen) {
-        toc->bag[toc->pos] = xing_header->size;
+    if (mp3->want == ++mp3->seen) {
+        mp3->bag[mp3->pos] = mp3->size;
 
-        if (VBR_NUM_BAGS == ++toc->pos) {
+        if (VBR_NUM_BAGS == ++mp3->pos) {
             /* shrink table to half size by throwing away each second bag. */
             for (i = 1; i < VBR_NUM_BAGS; i += 2)
-                toc->bag[i >> 1] = toc->bag[i];
+                mp3->bag[i >> 1] = mp3->bag[i];
 
             /* double wanted amount per bag. */
-            toc->want <<= 1;
+            mp3->want <<= 1;
             /* adjust current position to half of table size. */
-            toc->pos >>= 1;
+            mp3->pos >>= 1;
         }
 
-        toc->seen = 0;
+        mp3->seen = 0;
     }
 }
 
 static void mp3_fix_xing(AVFormatContext *s)
 {
     MP3Context  *mp3 = s->priv_data;
-    struct xing_header *xing_header = &mp3->xing_header;
-    struct xing_toc *toc = &xing_header->toc;
-    double scale = (double)toc->pos / (double)VBR_TOC_SIZE;
     int i;
 
     avio_flush(s->pb);
-    avio_seek(s->pb, xing_header->offset, SEEK_SET);
-    avio_wb32(s->pb, xing_header->frames);
-    avio_wb32(s->pb, xing_header->size);
+    avio_seek(s->pb, mp3->frames_offset, SEEK_SET);
+    avio_wb32(s->pb, mp3->frames);
+    avio_wb32(s->pb, mp3->size);
 
     avio_w8(s->pb, 0);  // first toc entry has to be zero.
 
     for (i = 1; i < VBR_TOC_SIZE; ++i) {
-        int j = (int)floor(scale * i);
-        int seek_point = (int)floor(256.0 * toc->bag[j] / xing_header->size);
-        avio_w8(s->pb, (uint8_t)(seek_point < 256 ? seek_point : 255));
+        int j = i * mp3->pos / VBR_TOC_SIZE;
+        int seek_point = 256LL * mp3->bag[j] / mp3->size;
+        avio_w8(s->pb, FFMIN(seek_point, 255));
     }
 
     avio_flush(s->pb);
@@ -354,7 +346,7 @@ static void mp3_fix_xing(AVFormatContext *s)
 static int mp3_write_header(struct AVFormatContext *s)
 {
     MP3Context  *mp3 = s->priv_data;
-    AVMetadataTag *t = NULL;
+    AVDictionaryEntry *t = NULL;
     int totlen = 0, enc = mp3->id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
                                                     ID3v2_ENCODING_UTF8;
     int64_t size_pos, cur_pos;
@@ -371,7 +363,7 @@ static int mp3_write_header(struct AVFormatContext *s)
     if (mp3->id3v2_version == 4)
         ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
 
-    while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
+    while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
         int ret;
 
         if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
@@ -407,10 +399,10 @@ static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
         return ff_raw_write_packet(s, pkt);
     else {
         MP3Context  *mp3 = s->priv_data;
+#ifdef FILTER_VBR_HEADERS
         MPADecodeHeader c;
         int base;
 
-#ifdef FILTER_VBR_HEADERS
         ff_mpegaudio_decode_header(&c, AV_RB32(pkt->data));
 
         /* filter out XING and INFO headers. */
@@ -430,7 +422,7 @@ static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
             return 0;
 #endif
 
-        if (0 < mp3->xing_header.offset)
+        if (mp3->frames_offset)
             mp3_xing_add_frame(s, pkt);
 
         return ff_raw_write_packet(s, pkt);
@@ -445,7 +437,7 @@ static int mp3_write_trailer(AVFormatContext *s)
     if (ret < 0)
         return ret;
 
-    if (0 < mp3->xing_header.offset)
+    if (mp3->frames_offset)
         mp3_fix_xing(s);
 
     return 0;