]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mp3enc.c
mpegts: remove unused variable
[ffmpeg] / libavformat / mp3enc.c
index 793c9c62af12a721303a84157604b9aefeed2e49..42fc39db961f88e873fd04f8b734490d7bb2cb63 100644 (file)
@@ -19,7 +19,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <strings.h>
 #include "avformat.h"
 #include "avio_internal.h"
 #include "id3v1.h"
@@ -55,11 +54,12 @@ static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
     buf[0] = 'T';
     buf[1] = 'A';
     buf[2] = 'G';
-    count += id3v1_set_string(s, "TIT2",    buf +  3, 30);       //title
-    count += id3v1_set_string(s, "TPE1",    buf + 33, 30);       //author|artist
-    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);
+    /* we knowingly overspecify each tag length by one byte to compensate for the mandatory null byte added by av_strlcpy */
+    count += id3v1_set_string(s, "TIT2",    buf +  3, 30 + 1);       //title
+    count += id3v1_set_string(s, "TPE1",    buf + 33, 30 + 1);       //author|artist
+    count += id3v1_set_string(s, "TALB",    buf + 63, 30 + 1);       //album
+    count += id3v1_set_string(s, "TDRL",    buf + 93,  4 + 1);       //date
+    count += id3v1_set_string(s, "comment", buf + 97, 30 + 1);
     if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
         buf[125] = 0;
         buf[126] = atoi(tag->value);
@@ -68,7 +68,7 @@ static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
     buf[127] = 0xFF; /* default to unknown 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])) {
+            if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
                 buf[127] = i;
                 count++;
                 break;
@@ -156,9 +156,11 @@ static int mp3_write_xing(AVFormatContext *s)
 {
     AVCodecContext   *codec = s->streams[0]->codec;
     MP3Context       *mp3 = s->priv_data;
-    int              bitrate_idx = 3;
+    int              bitrate_idx;
+    int              best_bitrate_idx;
+    int              best_bitrate_error= INT_MAX;
     int64_t          xing_offset;
-    int32_t          mask, header;
+    int32_t          header;
     MPADecodeHeader  c;
     int              srate_idx, i, channels;
     int              needed;
@@ -185,13 +187,21 @@ static int mp3_write_xing(AVFormatContext *s)
     header |= (srate_idx << 2) <<  8;
     header |= channels << 6;
 
-    for (;;) {
+    for (bitrate_idx=1; bitrate_idx<15; bitrate_idx++) {
+        int error;
+        avpriv_mpegaudio_decode_header(&c, header | (bitrate_idx << (4+8)));
+        error= FFABS(c.bit_rate - codec->bit_rate);
+        if(error < best_bitrate_error){
+            best_bitrate_error= error;
+            best_bitrate_idx  = bitrate_idx;
+        }
+    }
+
+    for (bitrate_idx= best_bitrate_idx;; bitrate_idx++) {
         if (15 == bitrate_idx)
             return -1;
 
-        mask = (bitrate_idx << 4) <<  8;
-        header |= mask;
-        avpriv_mpegaudio_decode_header(&c, header);
+        avpriv_mpegaudio_decode_header(&c, header | (bitrate_idx << (4+8)));
         xing_offset=xing_offtbl[c.lsf == 1][c.nb_channels == 1];
         needed = 4              // header
                + xing_offset
@@ -203,9 +213,6 @@ static int mp3_write_xing(AVFormatContext *s)
 
         if (needed <= c.frame_size)
             break;
-
-        header &= ~mask;
-        ++bitrate_idx;
     }
 
     avio_wb32(s->pb, header);