]> git.sesse.net Git - ffmpeg/blob - libavformat/amr.c
avformat/spdifenc: fix handling of large TrueHD frames
[ffmpeg] / libavformat / amr.c
1 /*
2  * amr file format
3  * Copyright (c) 2001 FFmpeg project
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 /*
23 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
24
25 Only mono files are supported.
26
27 */
28
29 #include "libavutil/channel_layout.h"
30 #include "avformat.h"
31 #include "internal.h"
32
33 typedef struct {
34     uint64_t cumulated_size;
35     uint64_t block_count;
36 } AMRContext;
37
38 static const char AMR_header[]   = "#!AMR\n";
39 static const char AMRWB_header[] = "#!AMR-WB\n";
40
41 static const uint8_t amrnb_packed_size[16] = {
42     13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
43 };
44 static const uint8_t amrwb_packed_size[16] = {
45     18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
46 };
47
48 #if CONFIG_AMR_MUXER
49 static int amr_write_header(AVFormatContext *s)
50 {
51     AVIOContext    *pb  = s->pb;
52     AVCodecParameters *par = s->streams[0]->codecpar;
53
54     s->priv_data = NULL;
55
56     if (par->codec_id == AV_CODEC_ID_AMR_NB) {
57         avio_write(pb, AMR_header,   sizeof(AMR_header)   - 1); /* magic number */
58     } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
59         avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
60     } else {
61         return -1;
62     }
63     return 0;
64 }
65
66 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
67 {
68     avio_write(s->pb, pkt->data, pkt->size);
69     return 0;
70 }
71 #endif /* CONFIG_AMR_MUXER */
72
73 static int amr_probe(const AVProbeData *p)
74 {
75     // Only check for "#!AMR" which could be amr-wb, amr-nb.
76     // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
77     // "#!AMR-WB_MC1.0\n" (not supported)
78
79     if (!memcmp(p->buf, AMR_header, 5))
80         return AVPROBE_SCORE_MAX;
81     else
82         return 0;
83 }
84
85 /* amr input */
86 static int amr_read_header(AVFormatContext *s)
87 {
88     AVIOContext *pb = s->pb;
89     AVStream *st;
90     uint8_t header[9];
91
92     avio_read(pb, header, 6);
93
94     st = avformat_new_stream(s, NULL);
95     if (!st)
96         return AVERROR(ENOMEM);
97     if (memcmp(header, AMR_header, 6)) {
98         avio_read(pb, header + 6, 3);
99         if (memcmp(header, AMRWB_header, 9)) {
100             return -1;
101         }
102
103         st->codecpar->codec_tag   = MKTAG('s', 'a', 'w', 'b');
104         st->codecpar->codec_id    = AV_CODEC_ID_AMR_WB;
105         st->codecpar->sample_rate = 16000;
106     } else {
107         st->codecpar->codec_tag   = MKTAG('s', 'a', 'm', 'r');
108         st->codecpar->codec_id    = AV_CODEC_ID_AMR_NB;
109         st->codecpar->sample_rate = 8000;
110     }
111     st->codecpar->channels   = 1;
112     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
113     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
114     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
115
116     return 0;
117 }
118
119 static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
120 {
121     AVCodecParameters *par = s->streams[0]->codecpar;
122     int read, size = 0, toc, mode;
123     int64_t pos = avio_tell(s->pb);
124     AMRContext *amr = s->priv_data;
125
126     if (avio_feof(s->pb)) {
127         return AVERROR_EOF;
128     }
129
130     // FIXME this is wrong, this should rather be in an AVParser
131     toc  = avio_r8(s->pb);
132     mode = (toc >> 3) & 0x0F;
133
134     if (par->codec_id == AV_CODEC_ID_AMR_NB) {
135         size = amrnb_packed_size[mode];
136     } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
137         size = amrwb_packed_size[mode];
138     }
139
140     if (!size || av_new_packet(pkt, size))
141         return AVERROR(EIO);
142
143     if (amr->cumulated_size < UINT64_MAX - size) {
144         amr->cumulated_size += size;
145         /* Both AMR formats have 50 frames per second */
146         s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
147     }
148
149     pkt->stream_index = 0;
150     pkt->pos          = pos;
151     pkt->data[0]      = toc;
152     pkt->duration     = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
153     read              = avio_read(s->pb, pkt->data + 1, size - 1);
154
155     if (read != size - 1) {
156         if (read < 0)
157             return read;
158         return AVERROR(EIO);
159     }
160
161     return 0;
162 }
163
164 #if CONFIG_AMR_DEMUXER
165 AVInputFormat ff_amr_demuxer = {
166     .name           = "amr",
167     .long_name      = NULL_IF_CONFIG_SMALL("3GPP AMR"),
168     .priv_data_size = sizeof(AMRContext),
169     .read_probe     = amr_probe,
170     .read_header    = amr_read_header,
171     .read_packet    = amr_read_packet,
172     .flags          = AVFMT_GENERIC_INDEX,
173 };
174 #endif
175
176 #if CONFIG_AMRNB_DEMUXER
177 static int amrnb_probe(const AVProbeData *p)
178 {
179     int mode, i = 0, valid = 0, invalid = 0;
180     const uint8_t *b = p->buf;
181
182     while (i < p->buf_size) {
183         mode = b[i] >> 3 & 0x0F;
184         if (mode < 9 && (b[i] & 0x4) == 0x4) {
185             int last = b[i];
186             int size = amrnb_packed_size[mode];
187             while (size--) {
188                 if (b[++i] != last)
189                     break;
190             }
191             if (size > 0) {
192                 valid++;
193                 i += size;
194             }
195         } else {
196             valid = 0;
197             invalid++;
198             i++;
199         }
200     }
201     if (valid > 100 && valid >> 4 > invalid)
202         return AVPROBE_SCORE_EXTENSION / 2 + 1;
203     return 0;
204 }
205
206 static int amrnb_read_header(AVFormatContext *s)
207 {
208     AVStream *st = avformat_new_stream(s, NULL);
209     if (!st)
210         return AVERROR(ENOMEM);
211     st->codecpar->codec_id       = AV_CODEC_ID_AMR_NB;
212     st->codecpar->sample_rate    = 8000;
213     st->codecpar->channels       = 1;
214     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
215     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
216     avpriv_set_pts_info(st, 64, 1, 8000);
217
218     return 0;
219 }
220
221 AVInputFormat ff_amrnb_demuxer = {
222     .name           = "amrnb",
223     .long_name      = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
224     .priv_data_size = sizeof(AMRContext),
225     .read_probe     = amrnb_probe,
226     .read_header    = amrnb_read_header,
227     .read_packet    = amr_read_packet,
228     .flags          = AVFMT_GENERIC_INDEX,
229 };
230 #endif
231
232 #if CONFIG_AMRWB_DEMUXER
233 static int amrwb_probe(const AVProbeData *p)
234 {
235     int mode, i = 0, valid = 0, invalid = 0;
236     const uint8_t *b = p->buf;
237
238     while (i < p->buf_size) {
239         mode = b[i] >> 3 & 0x0F;
240         if (mode < 10 && (b[i] & 0x4) == 0x4) {
241             int last = b[i];
242             int size = amrwb_packed_size[mode];
243             while (size--) {
244                 if (b[++i] != last)
245                     break;
246             }
247             if (size > 0) {
248                 valid++;
249                 i += size;
250             }
251         } else {
252             valid = 0;
253             invalid++;
254             i++;
255         }
256     }
257     if (valid > 100 && valid >> 4 > invalid)
258         return AVPROBE_SCORE_EXTENSION / 2 + 1;
259     return 0;
260 }
261
262 static int amrwb_read_header(AVFormatContext *s)
263 {
264     AVStream *st = avformat_new_stream(s, NULL);
265     if (!st)
266         return AVERROR(ENOMEM);
267     st->codecpar->codec_id       = AV_CODEC_ID_AMR_WB;
268     st->codecpar->sample_rate    = 16000;
269     st->codecpar->channels       = 1;
270     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
271     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
272     avpriv_set_pts_info(st, 64, 1, 16000);
273
274     return 0;
275 }
276
277 AVInputFormat ff_amrwb_demuxer = {
278     .name           = "amrwb",
279     .long_name      = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
280     .priv_data_size = sizeof(AMRContext),
281     .read_probe     = amrwb_probe,
282     .read_header    = amrwb_read_header,
283     .read_packet    = amr_read_packet,
284     .flags          = AVFMT_GENERIC_INDEX,
285 };
286 #endif
287
288 #if CONFIG_AMR_MUXER
289 AVOutputFormat ff_amr_muxer = {
290     .name              = "amr",
291     .long_name         = NULL_IF_CONFIG_SMALL("3GPP AMR"),
292     .mime_type         = "audio/amr",
293     .extensions        = "amr",
294     .audio_codec       = AV_CODEC_ID_AMR_NB,
295     .video_codec       = AV_CODEC_ID_NONE,
296     .write_header      = amr_write_header,
297     .write_packet      = amr_write_packet,
298     .flags             = AVFMT_NOTIMESTAMPS,
299 };
300 #endif