]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/amr.c
give AVInput/OutputFormat structs consistent names
[frescor/ffmpeg.git] / libavformat / amr.c
1 /*
2  * amr file format
3  * Copyright (c) 2001 ffmpeg project
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 /*
21 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
22
23 Only mono files are supported.
24
25 */
26 #include "avformat.h"
27
28 static const unsigned char AMR_header [] = "#!AMR\n";
29 static const unsigned char AMRWB_header [] = "#!AMR-WB\n";
30
31 #ifdef CONFIG_MUXERS
32 static int amr_write_header(AVFormatContext *s)
33 {
34     ByteIOContext *pb = &s->pb;
35     AVCodecContext *enc = s->streams[0]->codec;
36
37     s->priv_data = NULL;
38
39     if (enc->codec_id == CODEC_ID_AMR_NB)
40     {
41         put_tag(pb, AMR_header);       /* magic number */
42     }
43     else if(enc->codec_id == CODEC_ID_AMR_WB)
44     {
45         put_tag(pb, AMRWB_header);       /* magic number */
46     }
47     else
48     {
49         //This is an error!
50     }
51     put_flush_packet(pb);
52     return 0;
53 }
54
55 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
56 {
57     put_buffer(&s->pb, pkt->data, pkt->size);
58     put_flush_packet(&s->pb);
59     return 0;
60 }
61
62 static int amr_write_trailer(AVFormatContext *s)
63 {
64     return 0;
65 }
66 #endif /* CONFIG_MUXERS */
67
68 static int amr_probe(AVProbeData *p)
69 {
70     //Only check for "#!AMR" which could be amr-wb, amr-nb.
71     //This will also trigger multichannel files: "#!AMR_MC1.0\n" and
72     //"#!AMR-WB_MC1.0\n" (not supported)
73
74     if (p->buf_size < 5)
75         return 0;
76     if(memcmp(p->buf,AMR_header,5)==0)
77         return AVPROBE_SCORE_MAX;
78     else
79         return 0;
80 }
81
82 /* amr input */
83 static int amr_read_header(AVFormatContext *s,
84                            AVFormatParameters *ap)
85 {
86     ByteIOContext *pb = &s->pb;
87     AVStream *st;
88     uint8_t header[9];
89
90     get_buffer(pb, header, 6);
91
92     if(memcmp(header,AMR_header,6)!=0)
93     {
94         get_buffer(pb, header+6, 3);
95         if(memcmp(header,AMRWB_header,9)!=0)
96         {
97             return -1;
98         }
99         st = av_new_stream(s, 0);
100         if (!st)
101         {
102             return AVERROR_NOMEM;
103         }
104
105         st->codec->codec_type = CODEC_TYPE_AUDIO;
106         st->codec->codec_tag = MKTAG('s', 'a', 'w', 'b');
107         st->codec->codec_id = CODEC_ID_AMR_WB;
108         st->codec->channels = 1;
109         st->codec->sample_rate = 16000;
110     }
111     else
112     {
113         st = av_new_stream(s, 0);
114         if (!st)
115         {
116             return AVERROR_NOMEM;
117         }
118
119         st->codec->codec_type = CODEC_TYPE_AUDIO;
120         st->codec->codec_tag = MKTAG('s', 'a', 'm', 'r');
121         st->codec->codec_id = CODEC_ID_AMR_NB;
122         st->codec->channels = 1;
123         st->codec->sample_rate = 8000;
124     }
125
126     return 0;
127 }
128
129 #define MAX_SIZE 32
130
131 static int amr_read_packet(AVFormatContext *s,
132                           AVPacket *pkt)
133 {
134     AVCodecContext *enc = s->streams[0]->codec;
135
136     if (enc->codec_id == CODEC_ID_AMR_NB)
137     {
138         const static uint8_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
139         uint8_t toc, q, ft;
140         int read;
141         int size;
142
143         if (url_feof(&s->pb))
144         {
145             return AVERROR_IO;
146         }
147
148         toc=get_byte(&s->pb);
149         q  = (toc >> 2) & 0x01;
150         ft = (toc >> 3) & 0x0F;
151
152         size=packed_size[ft];
153
154         if (av_new_packet(pkt, size+1))
155         {
156             return AVERROR_IO;
157         }
158         pkt->stream_index = 0;
159         pkt->pos= url_ftell(&s->pb);
160         pkt->data[0]=toc;
161
162         read = get_buffer(&s->pb, pkt->data+1, size);
163
164         if (read != size)
165         {
166             av_free_packet(pkt);
167             return AVERROR_IO;
168         }
169
170         return 0;
171     }
172     else if(enc->codec_id == CODEC_ID_AMR_WB)
173     {
174         static uint8_t packed_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
175         uint8_t toc, mode;
176         int read;
177         int size;
178
179         if (url_feof(&s->pb))
180         {
181             return AVERROR_IO;
182         }
183
184         toc=get_byte(&s->pb);
185         mode = (uint8_t)((toc >> 3) & 0x0F);
186         size = packed_size[mode];
187
188         if ( (size==0) || av_new_packet(pkt, size))
189         {
190             return AVERROR_IO;
191         }
192
193         pkt->stream_index = 0;
194         pkt->pos= url_ftell(&s->pb);
195         pkt->data[0]=toc;
196
197         read = get_buffer(&s->pb, pkt->data+1, size-1);
198
199         if (read != (size-1))
200         {
201             av_free_packet(pkt);
202             return AVERROR_IO;
203         }
204
205         return 0;
206     }
207     else
208     {
209         return AVERROR_IO;
210     }
211 }
212
213 static int amr_read_close(AVFormatContext *s)
214 {
215     return 0;
216 }
217
218 static AVInputFormat amr_demuxer = {
219     "amr",
220     "3gpp amr file format",
221     0, /*priv_data_size*/
222     amr_probe,
223     amr_read_header,
224     amr_read_packet,
225     amr_read_close,
226 };
227
228 #ifdef CONFIG_MUXERS
229 static AVOutputFormat amr_muxer = {
230     "amr",
231     "3gpp amr file format",
232     "audio/amr",
233     "amr",
234     0,
235     CODEC_ID_AMR_NB,
236     CODEC_ID_NONE,
237     amr_write_header,
238     amr_write_packet,
239     amr_write_trailer,
240 };
241 #endif
242
243 int amr_init(void)
244 {
245     av_register_input_format(&amr_demuxer);
246 #ifdef CONFIG_MUXERS
247     av_register_output_format(&amr_muxer);
248 #endif
249     return 0;
250 }