3 * Copyright (c) 2001 FFmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
23 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
26 #include "config_components.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
31 #include "avio_internal.h"
38 typedef struct AMRContext
{
39 FFRawDemuxerContext rawctx
;
42 static const uint8_t AMR_header
[6] = "#!AMR\x0a";
43 static const uint8_t AMRMC_header
[12] = "#!AMR_MC1.0\x0a";
44 static const uint8_t AMRWB_header
[9] = "#!AMR-WB\x0a";
45 static const uint8_t AMRWBMC_header
[15] = "#!AMR-WB_MC1.0\x0a";
47 static const uint8_t amrnb_packed_size
[16] = {
48 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
50 static const uint8_t amrwb_packed_size
[16] = {
51 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
54 #if CONFIG_AMR_DEMUXER
55 static int amr_probe(const AVProbeData
*p
)
57 // Only check for "#!AMR" which could be amr-wb, amr-nb.
58 // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
61 if (!memcmp(p
->buf
, AMR_header
, 5))
62 return AVPROBE_SCORE_MAX
;
68 static int amr_read_header(AVFormatContext
*s
)
70 AVIOContext
*pb
= s
->pb
;
72 uint8_t header
[19] = { 0 };
73 int read
, back
= 0, ret
;
75 ret
= ffio_ensure_seekback(s
->pb
, sizeof(header
));
79 read
= avio_read(pb
, header
, sizeof(header
));
83 st
= avformat_new_stream(s
, NULL
);
85 return AVERROR(ENOMEM
);
86 if (!memcmp(header
, AMR_header
, sizeof(AMR_header
))) {
87 st
->codecpar
->codec_tag
= MKTAG('s', 'a', 'm', 'r');
88 st
->codecpar
->codec_id
= AV_CODEC_ID_AMR_NB
;
89 st
->codecpar
->sample_rate
= 8000;
90 st
->codecpar
->ch_layout
= (AVChannelLayout
)AV_CHANNEL_LAYOUT_MONO
;
91 back
= read
- sizeof(AMR_header
);
92 } else if (!memcmp(header
, AMRWB_header
, sizeof(AMRWB_header
))) {
93 st
->codecpar
->codec_tag
= MKTAG('s', 'a', 'w', 'b');
94 st
->codecpar
->codec_id
= AV_CODEC_ID_AMR_WB
;
95 st
->codecpar
->sample_rate
= 16000;
96 st
->codecpar
->ch_layout
= (AVChannelLayout
)AV_CHANNEL_LAYOUT_MONO
;
97 back
= read
- sizeof(AMRWB_header
);
98 } else if (!memcmp(header
, AMRMC_header
, sizeof(AMRMC_header
))) {
99 st
->codecpar
->codec_tag
= MKTAG('s', 'a', 'm', 'r');
100 st
->codecpar
->codec_id
= AV_CODEC_ID_AMR_NB
;
101 st
->codecpar
->sample_rate
= 8000;
102 st
->codecpar
->ch_layout
.nb_channels
= AV_RL32(header
+ 12);
103 back
= read
- 4 - sizeof(AMRMC_header
);
104 } else if (!memcmp(header
, AMRWBMC_header
, sizeof(AMRWBMC_header
))) {
105 st
->codecpar
->codec_tag
= MKTAG('s', 'a', 'w', 'b');
106 st
->codecpar
->codec_id
= AV_CODEC_ID_AMR_WB
;
107 st
->codecpar
->sample_rate
= 16000;
108 st
->codecpar
->ch_layout
.nb_channels
= AV_RL32(header
+ 15);
109 back
= read
- 4 - sizeof(AMRWBMC_header
);
111 return AVERROR_INVALIDDATA
;
114 if (st
->codecpar
->ch_layout
.nb_channels
< 1)
115 return AVERROR_INVALIDDATA
;
117 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
118 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_FULL_RAW
;
119 avpriv_set_pts_info(st
, 64, 1, st
->codecpar
->sample_rate
);
122 avio_seek(pb
, -back
, SEEK_CUR
);
127 const FFInputFormat ff_amr_demuxer
= {
129 .p
.long_name
= NULL_IF_CONFIG_SMALL("3GPP AMR"),
130 .p
.flags
= AVFMT_GENERIC_INDEX
,
131 .p
.priv_class
= &ff_raw_demuxer_class
,
132 .priv_data_size
= sizeof(AMRContext
),
133 .read_probe
= amr_probe
,
134 .read_header
= amr_read_header
,
135 .read_packet
= ff_raw_read_partial_packet
,
139 #if CONFIG_AMRNB_DEMUXER
140 static int amrnb_probe(const AVProbeData
*p
)
142 int mode
, i
= 0, valid
= 0, invalid
= 0;
143 const uint8_t *b
= p
->buf
;
145 while (i
< p
->buf_size
) {
146 mode
= b
[i
] >> 3 & 0x0F;
147 if (mode
< 9 && (b
[i
] & 0x4) == 0x4) {
149 int size
= amrnb_packed_size
[mode
];
164 if (valid
> 100 && valid
>> 4 > invalid
)
165 return AVPROBE_SCORE_EXTENSION
/ 2 + 1;
169 static int amrnb_read_header(AVFormatContext
*s
)
171 AVStream
*st
= avformat_new_stream(s
, NULL
);
173 return AVERROR(ENOMEM
);
174 st
->codecpar
->codec_id
= AV_CODEC_ID_AMR_NB
;
175 st
->codecpar
->sample_rate
= 8000;
176 st
->codecpar
->ch_layout
= (AVChannelLayout
)AV_CHANNEL_LAYOUT_MONO
;
177 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
178 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_FULL_RAW
;
179 avpriv_set_pts_info(st
, 64, 1, 8000);
184 const FFInputFormat ff_amrnb_demuxer
= {
186 .p
.long_name
= NULL_IF_CONFIG_SMALL("raw AMR-NB"),
187 .p
.flags
= AVFMT_GENERIC_INDEX
,
188 .p
.priv_class
= &ff_raw_demuxer_class
,
189 .priv_data_size
= sizeof(AMRContext
),
190 .read_probe
= amrnb_probe
,
191 .read_header
= amrnb_read_header
,
192 .read_packet
= ff_raw_read_partial_packet
,
196 #if CONFIG_AMRWB_DEMUXER
197 static int amrwb_probe(const AVProbeData
*p
)
199 int mode
, i
= 0, valid
= 0, invalid
= 0;
200 const uint8_t *b
= p
->buf
;
202 while (i
< p
->buf_size
) {
203 mode
= b
[i
] >> 3 & 0x0F;
204 if (mode
< 10 && (b
[i
] & 0x4) == 0x4) {
206 int size
= amrwb_packed_size
[mode
];
221 if (valid
> 100 && valid
>> 4 > invalid
)
222 return AVPROBE_SCORE_EXTENSION
/ 2 + 1;
226 static int amrwb_read_header(AVFormatContext
*s
)
228 AVStream
*st
= avformat_new_stream(s
, NULL
);
230 return AVERROR(ENOMEM
);
231 st
->codecpar
->codec_id
= AV_CODEC_ID_AMR_WB
;
232 st
->codecpar
->sample_rate
= 16000;
233 st
->codecpar
->ch_layout
= (AVChannelLayout
)AV_CHANNEL_LAYOUT_MONO
;
234 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
235 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_FULL_RAW
;
236 avpriv_set_pts_info(st
, 64, 1, 16000);
241 const FFInputFormat ff_amrwb_demuxer
= {
243 .p
.long_name
= NULL_IF_CONFIG_SMALL("raw AMR-WB"),
244 .p
.flags
= AVFMT_GENERIC_INDEX
,
245 .p
.priv_class
= &ff_raw_demuxer_class
,
246 .priv_data_size
= sizeof(AMRContext
),
247 .read_probe
= amrwb_probe
,
248 .read_header
= amrwb_read_header
,
249 .read_packet
= ff_raw_read_partial_packet
,
254 static int amr_write_header(AVFormatContext
*s
)
256 AVIOContext
*pb
= s
->pb
;
257 AVCodecParameters
*par
= s
->streams
[0]->codecpar
;
259 if (par
->codec_id
== AV_CODEC_ID_AMR_NB
) {
260 avio_write(pb
, AMR_header
, sizeof(AMR_header
)); /* magic number */
261 } else if (par
->codec_id
== AV_CODEC_ID_AMR_WB
) {
262 avio_write(pb
, AMRWB_header
, sizeof(AMRWB_header
)); /* magic number */
269 const FFOutputFormat ff_amr_muxer
= {
271 .p
.long_name
= NULL_IF_CONFIG_SMALL("3GPP AMR"),
272 .p
.mime_type
= "audio/amr",
273 .p
.extensions
= "amr",
274 .p
.audio_codec
= AV_CODEC_ID_AMR_NB
,
275 .p
.video_codec
= AV_CODEC_ID_NONE
,
276 .p
.subtitle_codec
= AV_CODEC_ID_NONE
,
277 .p
.flags
= AVFMT_NOTIMESTAMPS
,
278 .flags_internal
= FF_OFMT_FLAG_MAX_ONE_OF_EACH
,
279 .write_header
= amr_write_header
,
280 .write_packet
= ff_raw_write_packet
,