Statistics
| Branch: | Revision:

ffmpeg / libavformat / mp3enc.c @ 5052830f

History | View | Annotate | Download (5.88 KB)

1
/*
2
 * MP3 muxer
3
 * Copyright (c) 2003 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include <strings.h>
23
#include "avformat.h"
24
#include "id3v1.h"
25
#include "id3v2.h"
26
#include "libavutil/intreadwrite.h"
27

    
28
static int id3v1_set_string(AVFormatContext *s, const char *key,
29
                            uint8_t *buf, int buf_size)
30
{
31
    AVMetadataTag *tag;
32
    if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
33
        strncpy(buf, tag->value, buf_size);
34
    return !!tag;
35
}
36

    
37
static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
38
{
39
    AVMetadataTag *tag;
40
    int i, count = 0;
41

    
42
    memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
43
    buf[0] = 'T';
44
    buf[1] = 'A';
45
    buf[2] = 'G';
46
    count += id3v1_set_string(s, "TIT2",    buf +  3, 30);       //title
47
    count += id3v1_set_string(s, "TPE1",    buf + 33, 30);       //author|artist
48
    count += id3v1_set_string(s, "TALB",    buf + 63, 30);       //album
49
    count += id3v1_set_string(s, "TDRL",    buf + 93,  4);       //date
50
    count += id3v1_set_string(s, "comment", buf + 97, 30);
51
    if ((tag = av_metadata_get(s->metadata, "TRCK", NULL, 0))) { //track
52
        buf[125] = 0;
53
        buf[126] = atoi(tag->value);
54
        count++;
55
    }
56
    buf[127] = 0xFF; /* default to unknown genre */
57
    if ((tag = av_metadata_get(s->metadata, "TCON", NULL, 0))) { //genre
58
        for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
59
            if (!strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
60
                buf[127] = i;
61
                count++;
62
                break;
63
            }
64
        }
65
    }
66
    return count;
67
}
68

    
69
/* simple formats */
70

    
71
static void id3v2_put_size(AVFormatContext *s, int size)
72
{
73
    put_byte(s->pb, size >> 21 & 0x7f);
74
    put_byte(s->pb, size >> 14 & 0x7f);
75
    put_byte(s->pb, size >> 7  & 0x7f);
76
    put_byte(s->pb, size       & 0x7f);
77
}
78

    
79
/**
80
 * Write a text frame with one (normal frames) or two (TXXX frames) strings
81
 * according to encoding (only UTF-8 or UTF-16+BOM supported).
82
 * @return number of bytes written or a negative error code.
83
 */
84
static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
85
                          uint32_t tag, enum ID3v2Encoding enc)
86
{
87
    int len;
88
    uint8_t *pb;
89
    int (*put)(ByteIOContext*, const char*);
90
    ByteIOContext *dyn_buf;
91
    if (url_open_dyn_buf(&dyn_buf) < 0)
92
        return AVERROR(ENOMEM);
93

    
94
    put_byte(dyn_buf, enc);
95
    if (enc == ID3v2_ENCODING_UTF16BOM) {
96
        put_le16(dyn_buf, 0xFEFF);      /* BOM */
97
        put = avio_put_str16le;
98
    } else
99
        put = avio_put_str;
100

    
101
    put(dyn_buf, str1);
102
    if (str2)
103
        put(dyn_buf, str2);
104
    len = url_close_dyn_buf(dyn_buf, &pb);
105

    
106
    put_be32(s->pb, tag);
107
    id3v2_put_size(s, len);
108
    put_be16(s->pb, 0);
109
    put_buffer(s->pb, pb, len);
110

    
111
    av_freep(&pb);
112
    return len + ID3v2_HEADER_SIZE;
113
}
114

    
115

    
116
static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
117
{
118
    put_buffer(s->pb, pkt->data, pkt->size);
119
    put_flush_packet(s->pb);
120
    return 0;
121
}
122

    
123
static int mp3_write_trailer(struct AVFormatContext *s)
124
{
125
    uint8_t buf[ID3v1_TAG_SIZE];
126

    
127
    /* write the id3v1 tag */
128
    if (id3v1_create_tag(s, buf) > 0) {
129
        put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
130
        put_flush_packet(s->pb);
131
    }
132
    return 0;
133
}
134

    
135
#if CONFIG_MP2_MUXER
136
AVOutputFormat mp2_muxer = {
137
    "mp2",
138
    NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
139
    "audio/x-mpeg",
140
    "mp2,m2a",
141
    0,
142
    CODEC_ID_MP2,
143
    CODEC_ID_NONE,
144
    NULL,
145
    mp3_write_packet,
146
    mp3_write_trailer,
147
};
148
#endif
149

    
150
#if CONFIG_MP3_MUXER
151
/**
152
 * Write an ID3v2.4 header at beginning of stream
153
 */
154

    
155
static int mp3_write_header(struct AVFormatContext *s)
156
{
157
    AVMetadataTag *t = NULL;
158
    int totlen = 0;
159
    int64_t size_pos, cur_pos;
160

    
161
    put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
162
    put_byte(s->pb, 0);
163
    put_byte(s->pb, 0); /* flags */
164

    
165
    /* reserve space for size */
166
    size_pos = url_ftell(s->pb);
167
    put_be32(s->pb, 0);
168

    
169
    ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
170
    while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
171
        uint32_t tag = 0;
172
        int ret;
173

    
174
        if (t->key[0] == 'T' && strlen(t->key) == 4) {
175
            int i;
176
            for (i = 0; *ff_id3v2_tags[i]; i++)
177
                if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
178
                    tag = AV_RB32(t->key);
179
                    if ((ret = id3v2_put_ttag(s, t->value, NULL, tag, ID3v2_ENCODING_UTF8)) < 0)
180
                        return ret;
181
                    totlen += ret;
182
                    break;
183
                }
184
        }
185

    
186
        if (!tag) { /* unknown tag, write as TXXX frame */
187
            tag = MKBETAG('T', 'X', 'X', 'X');
188
            if ((ret = id3v2_put_ttag(s, t->key, t->value, tag, ID3v2_ENCODING_UTF8)) < 0)
189
                return ret;
190
            totlen += ret;
191
        }
192
    }
193

    
194
    cur_pos = url_ftell(s->pb);
195
    url_fseek(s->pb, size_pos, SEEK_SET);
196
    id3v2_put_size(s, totlen);
197
    url_fseek(s->pb, cur_pos, SEEK_SET);
198

    
199
    return 0;
200
}
201

    
202
AVOutputFormat mp3_muxer = {
203
    "mp3",
204
    NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
205
    "audio/x-mpeg",
206
    "mp3",
207
    0,
208
    CODEC_ID_MP3,
209
    CODEC_ID_NONE,
210
    mp3_write_header,
211
    mp3_write_packet,
212
    mp3_write_trailer,
213
    AVFMT_NOTIMESTAMPS,
214
};
215
#endif