FFmpeg
Loading...
Searching...
No Matches
amr.c
Go to the documentation of this file.
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/*
23Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
24*/
25
26#include "config_components.h"
27
31#include "avformat.h"
32#include "avio_internal.h"
33#include "demux.h"
34#include "internal.h"
35#include "mux.h"
36#include "rawdec.h"
37#include "rawenc.h"
38
39typedef struct AMRContext {
42
43static attribute_nonstring const uint8_t AMR_header[6] = "#!AMR\x0a";
44static attribute_nonstring const uint8_t AMRMC_header[12] = "#!AMR_MC1.0\x0a";
45static attribute_nonstring const uint8_t AMRWB_header[9] = "#!AMR-WB\x0a";
46static attribute_nonstring const uint8_t AMRWBMC_header[15] = "#!AMR-WB_MC1.0\x0a";
47
48static const uint8_t amrnb_packed_size[16] = {
49 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
50};
51static const uint8_t amrwb_packed_size[16] = {
52 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
53};
54
55#if CONFIG_AMR_DEMUXER
56static int amr_probe(const AVProbeData *p)
57{
58 // Only check for "#!AMR" which could be amr-wb, amr-nb.
59 // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
60 // "#!AMR-WB_MC1.0\n"
61
62 if (!memcmp(p->buf, AMR_header, 5))
63 return AVPROBE_SCORE_MAX;
64 else
65 return 0;
66}
67
68/* amr input */
69static int amr_read_header(AVFormatContext *s)
70{
71 AVIOContext *pb = s->pb;
72 AVStream *st;
73 uint8_t header[19] = { 0 };
74 int read, back = 0, ret;
75
76 ret = ffio_ensure_seekback(s->pb, sizeof(header));
77 if (ret < 0)
78 return ret;
79
80 read = avio_read(pb, header, sizeof(header));
81 if (read < 0)
82 return read;
83
85 if (!st)
86 return AVERROR(ENOMEM);
87 if (!memcmp(header, AMR_header, sizeof(AMR_header))) {
88 st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
90 st->codecpar->sample_rate = 8000;
92 back = read - sizeof(AMR_header);
93 } else if (!memcmp(header, AMRWB_header, sizeof(AMRWB_header))) {
94 st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
96 st->codecpar->sample_rate = 16000;
98 back = read - sizeof(AMRWB_header);
99 } else if (!memcmp(header, AMRMC_header, sizeof(AMRMC_header))) {
100 st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
102 st->codecpar->sample_rate = 8000;
104 back = read - 4 - sizeof(AMRMC_header);
105 } else if (!memcmp(header, AMRWBMC_header, sizeof(AMRWBMC_header))) {
106 st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
108 st->codecpar->sample_rate = 16000;
110 back = read - 4 - sizeof(AMRWBMC_header);
111 } else {
112 return AVERROR_INVALIDDATA;
113 }
114
115 if (st->codecpar->ch_layout.nb_channels < 1)
116 return AVERROR_INVALIDDATA;
117
121
122 if (back > 0)
123 avio_seek(pb, -back, SEEK_CUR);
124
125 return 0;
126}
127
129 .p.name = "amr",
130 .p.long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
131 .p.flags = AVFMT_GENERIC_INDEX,
132 .p.priv_class = &ff_raw_demuxer_class,
133 .priv_data_size = sizeof(AMRContext),
134 .read_probe = amr_probe,
135 .read_header = amr_read_header,
137};
138#endif
139
140#if CONFIG_AMRNB_DEMUXER
141static int amrnb_probe(const AVProbeData *p)
142{
143 int mode, i = 0, valid = 0, invalid = 0;
144 const uint8_t *b = p->buf;
145
146 while (i < p->buf_size) {
147 mode = b[i] >> 3 & 0x0F;
148 if (mode < 9 && (b[i] & 0x4) == 0x4 && (b[i] & 0x03) == 0) {
149 int last = b[i];
151 while (size--) {
152 if (b[++i] != last)
153 break;
154 }
155 if (size > 0) {
156 valid++;
157 i += size;
158 }
159 } else {
160 valid = 0;
161 invalid++;
162 i++;
163 }
164 }
165 if (valid > 100 && valid >> 4 > invalid)
166 return AVPROBE_SCORE_EXTENSION / 2 + 1;
167 return 0;
168}
169
170static int amrnb_read_header(AVFormatContext *s)
171{
173 if (!st)
174 return AVERROR(ENOMEM);
176 st->codecpar->sample_rate = 8000;
180 avpriv_set_pts_info(st, 64, 1, 8000);
181
182 return 0;
183}
184
186 .p.name = "amrnb",
187 .p.long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
188 .p.flags = AVFMT_GENERIC_INDEX,
189 .p.priv_class = &ff_raw_demuxer_class,
190 .priv_data_size = sizeof(AMRContext),
191 .read_probe = amrnb_probe,
192 .read_header = amrnb_read_header,
194};
195#endif
196
197#if CONFIG_AMRWB_DEMUXER
198static int amrwb_probe(const AVProbeData *p)
199{
200 int mode, i = 0, valid = 0, invalid = 0;
201 const uint8_t *b = p->buf;
202
203 while (i < p->buf_size) {
204 mode = b[i] >> 3 & 0x0F;
205 if (mode < 10 && (b[i] & 0x4) == 0x4 && (b[i] & 0x03) == 0) {
206 int last = b[i];
208 while (size--) {
209 if (b[++i] != last)
210 break;
211 }
212 if (size > 0) {
213 valid++;
214 i += size;
215 }
216 } else {
217 valid = 0;
218 invalid++;
219 i++;
220 }
221 }
222 if (valid > 100 && valid >> 4 > invalid)
223 return AVPROBE_SCORE_EXTENSION / 2 + 1;
224 return 0;
225}
226
227static int amrwb_read_header(AVFormatContext *s)
228{
230 if (!st)
231 return AVERROR(ENOMEM);
233 st->codecpar->sample_rate = 16000;
237 avpriv_set_pts_info(st, 64, 1, 16000);
238
239 return 0;
240}
241
243 .p.name = "amrwb",
244 .p.long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
245 .p.flags = AVFMT_GENERIC_INDEX,
246 .p.priv_class = &ff_raw_demuxer_class,
247 .priv_data_size = sizeof(AMRContext),
248 .read_probe = amrwb_probe,
249 .read_header = amrwb_read_header,
251};
252#endif
253
254#if CONFIG_AMR_MUXER
255static int amr_write_header(AVFormatContext *s)
256{
257 AVIOContext *pb = s->pb;
258 AVCodecParameters *par = s->streams[0]->codecpar;
259
260 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
261 avio_write(pb, AMR_header, sizeof(AMR_header)); /* magic number */
262 } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
263 avio_write(pb, AMRWB_header, sizeof(AMRWB_header)); /* magic number */
264 } else {
265 return -1;
266 }
267 return 0;
268}
269
271 .p.name = "amr",
272 .p.long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
273 .p.mime_type = "audio/amr",
274 .p.extensions = "amr",
275 .p.audio_codec = AV_CODEC_ID_AMR_NB,
276 .p.video_codec = AV_CODEC_ID_NONE,
277 .p.subtitle_codec = AV_CODEC_ID_NONE,
278 .p.flags = AVFMT_NOTIMESTAMPS,
279 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
280 .write_header = amr_write_header,
281 .write_packet = ff_raw_write_packet,
282};
283#endif
const FFInputFormat ff_amrwb_demuxer
const FFInputFormat ff_amr_demuxer
const FFOutputFormat ff_amr_muxer
const FFInputFormat ff_amrnb_demuxer
static attribute_nonstring const uint8_t AMR_header[6]
Definition amr.c:43
static attribute_nonstring const uint8_t AMRWBMC_header[15]
Definition amr.c:46
static attribute_nonstring const uint8_t AMRMC_header[12]
Definition amr.c:44
static attribute_nonstring const uint8_t AMRWB_header[9]
Definition amr.c:45
static const uint8_t amrnb_packed_size[16]
Definition amr_parser.c:33
static const uint8_t amrwb_packed_size[16]
Definition amr_parser.c:36
#define attribute_nonstring
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition avformat.h:615
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition aviobuf.c:1026
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AMR_NB
Definition codec_id.h:434
@ AV_CODEC_ID_AMR_WB
Definition codec_id.h:435
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define b
Definition input.c:43
#define AV_RL32(p)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
const AVClass ff_raw_demuxer_class
Definition rawdec.c:141
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawdec.c:33
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawenc.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
static const uint8_t header[24]
Definition sdr2.c:68
FFRawDemuxerContext rawctx
Definition amr.c:40
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
enum AVStreamParseType need_parsing
Definition internal.h:321
Definition swscale.c:71
int size