annotate src/ffmpeg/libavformat/amr.c @ 828:c294c4700937 trunk

[svn] - more stuff to kill
author nenolod
date Mon, 12 Mar 2007 15:05:25 -0700
parents e8776388b02a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
808
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
1 /*
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
2 * amr file format
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
3 * Copyright (c) 2001 ffmpeg project
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
4 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
5 * This file is part of FFmpeg.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
6 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
11 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
15 * Lesser General Public License for more details.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
16 *
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
20 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
21
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
22 /*
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
23 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
24
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
25 Only mono files are supported.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
26
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
27 */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
28 #include "avformat.h"
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
29
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
30 static const char AMR_header [] = "#!AMR\n";
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
31 static const char AMRWB_header [] = "#!AMR-WB\n";
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
32
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
33 #ifdef CONFIG_MUXERS
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
34 static int amr_write_header(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
35 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
36 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
37 AVCodecContext *enc = s->streams[0]->codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
38
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
39 s->priv_data = NULL;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
40
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
41 if (enc->codec_id == CODEC_ID_AMR_NB)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
42 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
43 put_tag(pb, AMR_header); /* magic number */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
44 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
45 else if(enc->codec_id == CODEC_ID_AMR_WB)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
46 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
47 put_tag(pb, AMRWB_header); /* magic number */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
48 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
49 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
50 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
51 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
52 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
53 put_flush_packet(pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
54 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
55 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
56
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
57 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
58 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
59 put_buffer(&s->pb, pkt->data, pkt->size);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
60 put_flush_packet(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
61 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
62 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
63
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
64 static int amr_write_trailer(AVFormatContext *s)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
65 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
66 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
67 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
68 #endif /* CONFIG_MUXERS */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
69
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
70 static int amr_probe(AVProbeData *p)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
71 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
72 //Only check for "#!AMR" which could be amr-wb, amr-nb.
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
73 //This will also trigger multichannel files: "#!AMR_MC1.0\n" and
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
74 //"#!AMR-WB_MC1.0\n" (not supported)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
75
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
76 if (p->buf_size < 5)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
77 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
78 if(memcmp(p->buf,AMR_header,5)==0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
79 return AVPROBE_SCORE_MAX;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
80 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
81 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
82 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
83
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
84 /* amr input */
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
85 static int amr_read_header(AVFormatContext *s,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
86 AVFormatParameters *ap)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
87 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
88 ByteIOContext *pb = &s->pb;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
89 AVStream *st;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
90 uint8_t header[9];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
91
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
92 get_buffer(pb, header, 6);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
93
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
94 st = av_new_stream(s, 0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
95 if (!st)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
96 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
97 return AVERROR_NOMEM;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
98 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
99 if(memcmp(header,AMR_header,6)!=0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
100 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
101 get_buffer(pb, header+6, 3);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
102 if(memcmp(header,AMRWB_header,9)!=0)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
103 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
104 return -1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
105 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
106
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
107 st->codec->codec_tag = MKTAG('s', 'a', 'w', 'b');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
108 st->codec->codec_id = CODEC_ID_AMR_WB;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
109 st->codec->sample_rate = 16000;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
110 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
111 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
112 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
113 st->codec->codec_tag = MKTAG('s', 'a', 'm', 'r');
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
114 st->codec->codec_id = CODEC_ID_AMR_NB;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
115 st->codec->sample_rate = 8000;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
116 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
117 st->codec->channels = 1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
118 st->codec->codec_type = CODEC_TYPE_AUDIO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
119 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
120
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
121 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
122 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
123
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
124 static int amr_read_packet(AVFormatContext *s,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
125 AVPacket *pkt)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
126 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
127 AVCodecContext *enc = s->streams[0]->codec;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
128 int read, size, toc, mode;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
129
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
130 if (url_feof(&s->pb))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
131 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
132 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
133 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
134
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
135 //FIXME this is wrong, this should rather be in a AVParset
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
136 toc=get_byte(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
137 mode = (toc >> 3) & 0x0F;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
138
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
139 if (enc->codec_id == CODEC_ID_AMR_NB)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
140 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
141 static const uint8_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
142
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
143 size=packed_size[mode]+1;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
144 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
145 else if(enc->codec_id == CODEC_ID_AMR_WB)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
146 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
147 static uint8_t packed_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
148
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
149 size=packed_size[mode];
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
150 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
151 else
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
152 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
153 assert(0);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
154 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
155
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
156 if ( (size==0) || av_new_packet(pkt, size))
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
157 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
158 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
159 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
160
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
161 pkt->stream_index = 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
162 pkt->pos= url_ftell(&s->pb);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
163 pkt->data[0]=toc;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
164 pkt->duration= enc->codec_id == CODEC_ID_AMR_NB ? 160 : 320;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
165 read = get_buffer(&s->pb, pkt->data+1, size-1);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
166
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
167 if (read != size-1)
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
168 {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
169 av_free_packet(pkt);
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
170 return AVERROR_IO;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
171 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
172
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
173 return 0;
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
174 }
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
175
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
176 #ifdef CONFIG_AMR_DEMUXER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
177 AVInputFormat amr_demuxer = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
178 "amr",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
179 "3gpp amr file format",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
180 0, /*priv_data_size*/
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
181 amr_probe,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
182 amr_read_header,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
183 amr_read_packet,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
184 NULL,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
185 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
186 #endif
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
187
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
188 #ifdef CONFIG_AMR_MUXER
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
189 AVOutputFormat amr_muxer = {
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
190 "amr",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
191 "3gpp amr file format",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
192 "audio/amr",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
193 "amr",
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
194 0,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
195 CODEC_ID_AMR_NB,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
196 CODEC_ID_NONE,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
197 amr_write_header,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
198 amr_write_packet,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
199 amr_write_trailer,
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
200 };
e8776388b02a [svn] - add ffmpeg
nenolod
parents:
diff changeset
201 #endif