FFmpeg
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 /*
23 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
24 */
25 
27 #include "libavutil/intreadwrite.h"
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "internal.h"
31 #include "rawdec.h"
32 #include "rawenc.h"
33 
34 typedef struct AMRContext {
36 } AMRContext;
37 
38 static const uint8_t AMR_header[6] = "#!AMR\x0a";
39 static const uint8_t AMRMC_header[12] = "#!AMR_MC1.0\x0a";
40 static const uint8_t AMRWB_header[9] = "#!AMR-WB\x0a";
41 static const uint8_t AMRWBMC_header[15] = "#!AMR-WB_MC1.0\x0a";
42 
43 static const uint8_t amrnb_packed_size[16] = {
44  13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
45 };
46 static const uint8_t amrwb_packed_size[16] = {
47  18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
48 };
49 
50 #if CONFIG_AMR_MUXER
51 static int amr_write_header(AVFormatContext *s)
52 {
53  AVIOContext *pb = s->pb;
54  AVCodecParameters *par = s->streams[0]->codecpar;
55 
56  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
57  avio_write(pb, AMR_header, sizeof(AMR_header)); /* magic number */
58  } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
59  avio_write(pb, AMRWB_header, sizeof(AMRWB_header)); /* magic number */
60  } else {
61  return -1;
62  }
63  return 0;
64 }
65 #endif /* CONFIG_AMR_MUXER */
66 
67 #if CONFIG_AMR_DEMUXER
68 static int amr_probe(const 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"
73 
74  if (!memcmp(p->buf, AMR_header, 5))
75  return AVPROBE_SCORE_MAX;
76  else
77  return 0;
78 }
79 
80 /* amr input */
81 static int amr_read_header(AVFormatContext *s)
82 {
83  AVIOContext *pb = s->pb;
84  AVStream *st;
85  uint8_t header[19] = { 0 };
86  int read, back = 0, ret;
87 
88  ret = ffio_ensure_seekback(s->pb, sizeof(header));
89  if (ret < 0)
90  return ret;
91 
92  read = avio_read(pb, header, sizeof(header));
93  if (read < 0)
94  return read;
95 
96  st = avformat_new_stream(s, NULL);
97  if (!st)
98  return AVERROR(ENOMEM);
99  if (!memcmp(header, AMR_header, sizeof(AMR_header))) {
100  st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
102  st->codecpar->sample_rate = 8000;
103  st->codecpar->channels = 1;
105  back = read - sizeof(AMR_header);
106  } else if (!memcmp(header, AMRWB_header, sizeof(AMRWB_header))) {
107  st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
109  st->codecpar->sample_rate = 16000;
110  st->codecpar->channels = 1;
112  back = read - sizeof(AMRWB_header);
113  } else if (!memcmp(header, AMRMC_header, sizeof(AMRMC_header))) {
114  st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
116  st->codecpar->sample_rate = 8000;
117  st->codecpar->channels = AV_RL32(header + 12);
118  back = read - 4 - sizeof(AMRMC_header);
119  } else if (!memcmp(header, AMRWBMC_header, sizeof(AMRWBMC_header))) {
120  st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
122  st->codecpar->sample_rate = 16000;
123  st->codecpar->channels = AV_RL32(header + 15);
124  back = read - 4 - sizeof(AMRWBMC_header);
125  } else {
126  return AVERROR_INVALIDDATA;
127  }
128 
129  if (st->codecpar->channels < 1)
130  return AVERROR_INVALIDDATA;
131 
134  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
135 
136  if (back > 0)
137  avio_seek(pb, -back, SEEK_CUR);
138 
139  return 0;
140 }
141 
143  .name = "amr",
144  .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
145  .priv_data_size = sizeof(AMRContext),
146  .read_probe = amr_probe,
147  .read_header = amr_read_header,
150  .priv_class = &ff_raw_demuxer_class,
151 };
152 #endif
153 
154 #if CONFIG_AMRNB_DEMUXER
155 static int amrnb_probe(const AVProbeData *p)
156 {
157  int mode, i = 0, valid = 0, invalid = 0;
158  const uint8_t *b = p->buf;
159 
160  while (i < p->buf_size) {
161  mode = b[i] >> 3 & 0x0F;
162  if (mode < 9 && (b[i] & 0x4) == 0x4) {
163  int last = b[i];
164  int size = amrnb_packed_size[mode];
165  while (size--) {
166  if (b[++i] != last)
167  break;
168  }
169  if (size > 0) {
170  valid++;
171  i += size;
172  }
173  } else {
174  valid = 0;
175  invalid++;
176  i++;
177  }
178  }
179  if (valid > 100 && valid >> 4 > invalid)
180  return AVPROBE_SCORE_EXTENSION / 2 + 1;
181  return 0;
182 }
183 
184 static int amrnb_read_header(AVFormatContext *s)
185 {
187  if (!st)
188  return AVERROR(ENOMEM);
190  st->codecpar->sample_rate = 8000;
191  st->codecpar->channels = 1;
195  avpriv_set_pts_info(st, 64, 1, 8000);
196 
197  return 0;
198 }
199 
201  .name = "amrnb",
202  .long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
203  .priv_data_size = sizeof(AMRContext),
204  .read_probe = amrnb_probe,
205  .read_header = amrnb_read_header,
208  .priv_class = &ff_raw_demuxer_class,
209 };
210 #endif
211 
212 #if CONFIG_AMRWB_DEMUXER
213 static int amrwb_probe(const AVProbeData *p)
214 {
215  int mode, i = 0, valid = 0, invalid = 0;
216  const uint8_t *b = p->buf;
217 
218  while (i < p->buf_size) {
219  mode = b[i] >> 3 & 0x0F;
220  if (mode < 10 && (b[i] & 0x4) == 0x4) {
221  int last = b[i];
222  int size = amrwb_packed_size[mode];
223  while (size--) {
224  if (b[++i] != last)
225  break;
226  }
227  if (size > 0) {
228  valid++;
229  i += size;
230  }
231  } else {
232  valid = 0;
233  invalid++;
234  i++;
235  }
236  }
237  if (valid > 100 && valid >> 4 > invalid)
238  return AVPROBE_SCORE_EXTENSION / 2 + 1;
239  return 0;
240 }
241 
242 static int amrwb_read_header(AVFormatContext *s)
243 {
245  if (!st)
246  return AVERROR(ENOMEM);
248  st->codecpar->sample_rate = 16000;
249  st->codecpar->channels = 1;
253  avpriv_set_pts_info(st, 64, 1, 16000);
254 
255  return 0;
256 }
257 
259  .name = "amrwb",
260  .long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
261  .priv_data_size = sizeof(AMRContext),
262  .read_probe = amrwb_probe,
263  .read_header = amrwb_read_header,
266  .priv_class = &ff_raw_demuxer_class,
267 };
268 #endif
269 
270 #if CONFIG_AMR_MUXER
272  .name = "amr",
273  .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
274  .mime_type = "audio/amr",
275  .extensions = "amr",
276  .audio_codec = AV_CODEC_ID_AMR_NB,
277  .video_codec = AV_CODEC_ID_NONE,
278  .write_header = amr_write_header,
279  .write_packet = ff_raw_write_packet,
280  .flags = AVFMT_NOTIMESTAMPS,
281 };
282 #endif
AVOutputFormat::name
const char * name
Definition: avformat.h:504
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AMRContext::rawctx
FFRawDemuxerContext rawctx
Definition: amr.c:35
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:475
b
#define b
Definition: input.c:40
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:406
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
AV_CODEC_ID_AMR_WB
@ AV_CODEC_ID_AMR_WB
Definition: codec_id.h:407
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
AMR_header
static const uint8_t AMR_header[6]
Definition: amr.c:38
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:432
FFRawDemuxerContext
Definition: rawdec.h:36
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:476
AVInputFormat
Definition: avformat.h:650
ff_raw_read_partial_packet
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:34
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_amrwb_demuxer
const AVInputFormat ff_amrwb_demuxer
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
ff_amr_demuxer
const AVInputFormat ff_amr_demuxer
AMRContext
Definition: amrnbdec.c:100
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:405
ff_raw_demuxer_class
const AVClass ff_raw_demuxer_class
Definition: rawdec.c:142
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
rawdec.h
amrnb_packed_size
static const uint8_t amrnb_packed_size[16]
Definition: amr.c:43
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:457
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
header
static const uint8_t header[24]
Definition: sdr2.c:67
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1055
amrwb_packed_size
static const uint8_t amrwb_packed_size[16]
Definition: amr.c:46
ff_amr_muxer
const AVOutputFormat ff_amr_muxer
ff_amrnb_demuxer
const AVInputFormat ff_amrnb_demuxer
rawenc.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:503
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
avio_internal.h
AMRWBMC_header
static const uint8_t AMRWBMC_header[15]
Definition: amr.c:41
AMRMC_header
static const uint8_t AMRMC_header[12]
Definition: amr.c:39
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:935
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
channel_layout.h
mode
mode
Definition: ebur128.h:83
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:641
AVSTREAM_PARSE_FULL_RAW
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:796
avpriv_set_pts_info
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: utils.c:1195
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AMRWB_header
static const uint8_t AMRWB_header[9]
Definition: amr.c:40