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 Only mono files are supported.
26 
27 */
28 
30 #include "avformat.h"
31 #include "internal.h"
32 #include "rawenc.h"
33 
34 typedef struct {
35  uint64_t cumulated_size;
36  uint64_t block_count;
37 } AMRContext;
38 
39 static const char AMR_header[] = "#!AMR\n";
40 static const char AMRWB_header[] = "#!AMR-WB\n";
41 
42 static const uint8_t amrnb_packed_size[16] = {
43  13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
44 };
45 static const uint8_t amrwb_packed_size[16] = {
46  18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
47 };
48 
49 #if CONFIG_AMR_MUXER
50 static int amr_write_header(AVFormatContext *s)
51 {
52  AVIOContext *pb = s->pb;
53  AVCodecParameters *par = s->streams[0]->codecpar;
54 
55  s->priv_data = NULL;
56 
57  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
58  avio_write(pb, AMR_header, sizeof(AMR_header) - 1); /* magic number */
59  } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
60  avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
61  } else {
62  return -1;
63  }
64  return 0;
65 }
66 #endif /* CONFIG_AMR_MUXER */
67 
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" (not supported)
73 
74  if (!memcmp(p->buf, AMR_header, 5))
75  return AVPROBE_SCORE_MAX;
76  else
77  return 0;
78 }
79 
80 /* amr input */
82 {
83  AVIOContext *pb = s->pb;
84  AVStream *st;
85  uint8_t header[9];
86 
87  if (avio_read(pb, header, 6) != 6)
88  return AVERROR_INVALIDDATA;
89 
90  st = avformat_new_stream(s, NULL);
91  if (!st)
92  return AVERROR(ENOMEM);
93  if (memcmp(header, AMR_header, 6)) {
94  if (avio_read(pb, header + 6, 3) != 3)
95  return AVERROR_INVALIDDATA;
96  if (memcmp(header, AMRWB_header, 9)) {
97  return -1;
98  }
99 
100  st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
102  st->codecpar->sample_rate = 16000;
103  } else {
104  st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
106  st->codecpar->sample_rate = 8000;
107  }
108  st->codecpar->channels = 1;
111  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
112 
113  return 0;
114 }
115 
117 {
118  AVCodecParameters *par = s->streams[0]->codecpar;
119  int read, size = 0, toc, mode;
120  int64_t pos = avio_tell(s->pb);
121  AMRContext *amr = s->priv_data;
122 
123  if (avio_feof(s->pb)) {
124  return AVERROR_EOF;
125  }
126 
127  // FIXME this is wrong, this should rather be in an AVParser
128  toc = avio_r8(s->pb);
129  mode = (toc >> 3) & 0x0F;
130 
131  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
133  } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
135  }
136 
137  if (!size || av_new_packet(pkt, size))
138  return AVERROR(EIO);
139 
140  if (amr->cumulated_size < UINT64_MAX - size) {
141  amr->cumulated_size += size;
142  /* Both AMR formats have 50 frames per second */
143  s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
144  }
145 
146  pkt->stream_index = 0;
147  pkt->pos = pos;
148  pkt->data[0] = toc;
149  pkt->duration = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
150  read = avio_read(s->pb, pkt->data + 1, size - 1);
151 
152  if (read != size - 1) {
153  if (read < 0)
154  return read;
155  return AVERROR(EIO);
156  }
157 
158  return 0;
159 }
160 
161 #if CONFIG_AMR_DEMUXER
163  .name = "amr",
164  .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
165  .priv_data_size = sizeof(AMRContext),
170 };
171 #endif
172 
173 #if CONFIG_AMRNB_DEMUXER
174 static int amrnb_probe(const AVProbeData *p)
175 {
176  int mode, i = 0, valid = 0, invalid = 0;
177  const uint8_t *b = p->buf;
178 
179  while (i < p->buf_size) {
180  mode = b[i] >> 3 & 0x0F;
181  if (mode < 9 && (b[i] & 0x4) == 0x4) {
182  int last = b[i];
183  int size = amrnb_packed_size[mode];
184  while (size--) {
185  if (b[++i] != last)
186  break;
187  }
188  if (size > 0) {
189  valid++;
190  i += size;
191  }
192  } else {
193  valid = 0;
194  invalid++;
195  i++;
196  }
197  }
198  if (valid > 100 && valid >> 4 > invalid)
199  return AVPROBE_SCORE_EXTENSION / 2 + 1;
200  return 0;
201 }
202 
203 static int amrnb_read_header(AVFormatContext *s)
204 {
206  if (!st)
207  return AVERROR(ENOMEM);
209  st->codecpar->sample_rate = 8000;
210  st->codecpar->channels = 1;
213  avpriv_set_pts_info(st, 64, 1, 8000);
214 
215  return 0;
216 }
217 
219  .name = "amrnb",
220  .long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
221  .priv_data_size = sizeof(AMRContext),
222  .read_probe = amrnb_probe,
223  .read_header = amrnb_read_header,
226 };
227 #endif
228 
229 #if CONFIG_AMRWB_DEMUXER
230 static int amrwb_probe(const AVProbeData *p)
231 {
232  int mode, i = 0, valid = 0, invalid = 0;
233  const uint8_t *b = p->buf;
234 
235  while (i < p->buf_size) {
236  mode = b[i] >> 3 & 0x0F;
237  if (mode < 10 && (b[i] & 0x4) == 0x4) {
238  int last = b[i];
239  int size = amrwb_packed_size[mode];
240  while (size--) {
241  if (b[++i] != last)
242  break;
243  }
244  if (size > 0) {
245  valid++;
246  i += size;
247  }
248  } else {
249  valid = 0;
250  invalid++;
251  i++;
252  }
253  }
254  if (valid > 100 && valid >> 4 > invalid)
255  return AVPROBE_SCORE_EXTENSION / 2 + 1;
256  return 0;
257 }
258 
259 static int amrwb_read_header(AVFormatContext *s)
260 {
262  if (!st)
263  return AVERROR(ENOMEM);
265  st->codecpar->sample_rate = 16000;
266  st->codecpar->channels = 1;
269  avpriv_set_pts_info(st, 64, 1, 16000);
270 
271  return 0;
272 }
273 
275  .name = "amrwb",
276  .long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
277  .priv_data_size = sizeof(AMRContext),
278  .read_probe = amrwb_probe,
279  .read_header = amrwb_read_header,
282 };
283 #endif
284 
285 #if CONFIG_AMR_MUXER
287  .name = "amr",
288  .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
289  .mime_type = "audio/amr",
290  .extensions = "amr",
291  .audio_codec = AV_CODEC_ID_AMR_NB,
292  .video_codec = AV_CODEC_ID_NONE,
293  .write_header = amr_write_header,
294  .write_packet = ff_raw_write_packet,
295  .flags = AVFMT_NOTIMESTAMPS,
296 };
297 #endif
AVOutputFormat::name
const char * name
Definition: avformat.h:491
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:4509
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
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:478
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:462
AVPacket::data
uint8_t * data
Definition: packet.h:369
b
#define b
Definition: input.c:41
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:406
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
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
amr_read_packet
static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: amr.c:116
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
AMRContext::cumulated_size
uint64_t cumulated_size
Definition: amr.c:35
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
pkt
AVPacket * pkt
Definition: movenc.c:59
AMR_header
static const char AMR_header[]
Definition: amr.c:39
AVInputFormat
Definition: avformat.h:640
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
ff_amrnb_demuxer
AVInputFormat ff_amrnb_demuxer
AMRContext
Definition: amrnbdec.c:100
ff_amr_demuxer
AVInputFormat ff_amr_demuxer
AVFormatContext
Format I/O context.
Definition: avformat.h:1232
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
amrnb_packed_size
static const uint8_t amrnb_packed_size[16]
Definition: amr.c:42
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:441
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
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
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, 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:4945
size
int size
Definition: twinvq_data.h:10344
ff_amrwb_demuxer
AVInputFormat ff_amrwb_demuxer
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:225
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
amrwb_packed_size
static const uint8_t amrwb_packed_size[16]
Definition: amr.c:45
rawenc.h
i
int i
Definition: input.c:407
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
AVOutputFormat
Definition: avformat.h:490
AMRContext::block_count
uint64_t block_count
Definition: amr.c:36
uint8_t
uint8_t
Definition: audio_convert.c:194
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:873
amr_read_header
static int amr_read_header(AVFormatContext *s)
Definition: amr.c:81
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
channel_layout.h
mode
mode
Definition: ebur128.h:83
AMRWB_header
static const char AMRWB_header[]
Definition: amr.c:40
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
AVPacket::stream_index
int stream_index
Definition: packet.h:371
ff_amr_muxer
AVOutputFormat ff_amr_muxer
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:346
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
amr_probe
static int amr_probe(const AVProbeData *p)
Definition: amr.c:68
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:364