]> git.sesse.net Git - ffmpeg/blob - libavformat/mp3enc.c
Split mp3 demuxer and muxer into separate files.
[ffmpeg] / libavformat / mp3enc.c
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 static void id3v2_put_ttag(AVFormatContext *s, const char *buf, int len,
80                            uint32_t tag)
81 {
82     put_be32(s->pb, tag);
83     id3v2_put_size(s, len + 1);
84     put_be16(s->pb, 0);
85     put_byte(s->pb, 3); /* UTF-8 */
86     put_buffer(s->pb, buf, len);
87 }
88
89
90 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
91 {
92     put_buffer(s->pb, pkt->data, pkt->size);
93     put_flush_packet(s->pb);
94     return 0;
95 }
96
97 static int mp3_write_trailer(struct AVFormatContext *s)
98 {
99     uint8_t buf[ID3v1_TAG_SIZE];
100
101     /* write the id3v1 tag */
102     if (id3v1_create_tag(s, buf) > 0) {
103         put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
104         put_flush_packet(s->pb);
105     }
106     return 0;
107 }
108
109 #if CONFIG_MP2_MUXER
110 AVOutputFormat mp2_muxer = {
111     "mp2",
112     NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
113     "audio/x-mpeg",
114     "mp2,m2a",
115     0,
116     CODEC_ID_MP2,
117     CODEC_ID_NONE,
118     NULL,
119     mp3_write_packet,
120     mp3_write_trailer,
121 };
122 #endif
123
124 #if CONFIG_MP3_MUXER
125 /**
126  * Write an ID3v2.4 header at beginning of stream
127  */
128
129 static int mp3_write_header(struct AVFormatContext *s)
130 {
131     AVMetadataTag *t = NULL;
132     int totlen = 0;
133     int64_t size_pos, cur_pos;
134
135     put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
136     put_byte(s->pb, 0);
137     put_byte(s->pb, 0); /* flags */
138
139     /* reserve space for size */
140     size_pos = url_ftell(s->pb);
141     put_be32(s->pb, 0);
142
143     ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
144     while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
145         uint32_t tag = 0;
146
147         if (t->key[0] == 'T' && strlen(t->key) == 4) {
148             int i;
149             for (i = 0; *ff_id3v2_tags[i]; i++)
150                 if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
151                     int len = strlen(t->value);
152                     tag = AV_RB32(t->key);
153                     totlen += len + ID3v2_HEADER_SIZE + 2;
154                     id3v2_put_ttag(s, t->value, len + 1, tag);
155                     break;
156                 }
157         }
158
159         if (!tag) { /* unknown tag, write as TXXX frame */
160             int   len = strlen(t->key), len1 = strlen(t->value);
161             char *buf = av_malloc(len + len1 + 2);
162             if (!buf)
163                 return AVERROR(ENOMEM);
164             tag = MKBETAG('T', 'X', 'X', 'X');
165             strcpy(buf,           t->key);
166             strcpy(buf + len + 1, t->value);
167             id3v2_put_ttag(s, buf, len + len1 + 2, tag);
168             totlen += len + len1 + ID3v2_HEADER_SIZE + 3;
169             av_free(buf);
170         }
171     }
172
173     cur_pos = url_ftell(s->pb);
174     url_fseek(s->pb, size_pos, SEEK_SET);
175     id3v2_put_size(s, totlen);
176     url_fseek(s->pb, cur_pos, SEEK_SET);
177
178     return 0;
179 }
180
181 AVOutputFormat mp3_muxer = {
182     "mp3",
183     NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
184     "audio/x-mpeg",
185     "mp3",
186     0,
187     CODEC_ID_MP3,
188     CODEC_ID_NONE,
189     mp3_write_header,
190     mp3_write_packet,
191     mp3_write_trailer,
192     AVFMT_NOTIMESTAMPS,
193 };
194 #endif