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 
33 typedef struct {
34  uint64_t cumulated_size;
35  uint64_t block_count;
36 } AMRContext;
37 
38 static const char AMR_header[] = "#!AMR\n";
39 static const char AMRWB_header[] = "#!AMR-WB\n";
40 
41 static const uint8_t amrnb_packed_size[16] = {
42  13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
43 };
44 static const uint8_t amrwb_packed_size[16] = {
45  18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
46 };
47 
48 #if CONFIG_AMR_MUXER
49 static int amr_write_header(AVFormatContext *s)
50 {
51  AVIOContext *pb = s->pb;
52  AVCodecParameters *par = s->streams[0]->codecpar;
53 
54  s->priv_data = NULL;
55 
56  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
57  avio_write(pb, AMR_header, sizeof(AMR_header) - 1); /* magic number */
58  } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
59  avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
60  } else {
61  return -1;
62  }
63  avio_flush(pb);
64  return 0;
65 }
66 
67 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
68 {
69  avio_write(s->pb, pkt->data, pkt->size);
70  return 0;
71 }
72 #endif /* CONFIG_AMR_MUXER */
73 
74 static int amr_probe(const AVProbeData *p)
75 {
76  // Only check for "#!AMR" which could be amr-wb, amr-nb.
77  // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
78  // "#!AMR-WB_MC1.0\n" (not supported)
79 
80  if (!memcmp(p->buf, AMR_header, 5))
81  return AVPROBE_SCORE_MAX;
82  else
83  return 0;
84 }
85 
86 /* amr input */
88 {
89  AVIOContext *pb = s->pb;
90  AVStream *st;
91  uint8_t header[9];
92 
93  if (avio_read(pb, header, 6) != 6)
94  return AVERROR_INVALIDDATA;
95 
96  st = avformat_new_stream(s, NULL);
97  if (!st)
98  return AVERROR(ENOMEM);
99  if (memcmp(header, AMR_header, 6)) {
100  if (avio_read(pb, header + 6, 3) != 3)
101  return AVERROR_INVALIDDATA;
102  if (memcmp(header, AMRWB_header, 9)) {
103  return -1;
104  }
105 
106  st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
108  st->codecpar->sample_rate = 16000;
109  } else {
110  st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
112  st->codecpar->sample_rate = 8000;
113  }
114  st->codecpar->channels = 1;
117  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
118 
119  return 0;
120 }
121 
123 {
124  AVCodecParameters *par = s->streams[0]->codecpar;
125  int read, size = 0, toc, mode;
126  int64_t pos = avio_tell(s->pb);
127  AMRContext *amr = s->priv_data;
128 
129  if (avio_feof(s->pb)) {
130  return AVERROR_EOF;
131  }
132 
133  // FIXME this is wrong, this should rather be in an AVParser
134  toc = avio_r8(s->pb);
135  mode = (toc >> 3) & 0x0F;
136 
137  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
139  } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
141  }
142 
143  if (!size || av_new_packet(pkt, size))
144  return AVERROR(EIO);
145 
146  if (amr->cumulated_size < UINT64_MAX - size) {
147  amr->cumulated_size += size;
148  /* Both AMR formats have 50 frames per second */
149  s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
150  }
151 
152  pkt->stream_index = 0;
153  pkt->pos = pos;
154  pkt->data[0] = toc;
155  pkt->duration = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
156  read = avio_read(s->pb, pkt->data + 1, size - 1);
157 
158  if (read != size - 1) {
160  if (read < 0)
161  return read;
162  return AVERROR(EIO);
163  }
164 
165  return 0;
166 }
167 
168 #if CONFIG_AMR_DEMUXER
170  .name = "amr",
171  .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
172  .priv_data_size = sizeof(AMRContext),
177 };
178 #endif
179 
180 #if CONFIG_AMRNB_DEMUXER
181 static int amrnb_probe(const AVProbeData *p)
182 {
183  int mode, i = 0, valid = 0, invalid = 0;
184  const uint8_t *b = p->buf;
185 
186  while (i < p->buf_size) {
187  mode = b[i] >> 3 & 0x0F;
188  if (mode < 9 && (b[i] & 0x4) == 0x4) {
189  int last = b[i];
190  int size = amrnb_packed_size[mode];
191  while (size--) {
192  if (b[++i] != last)
193  break;
194  }
195  if (size > 0) {
196  valid++;
197  i += size;
198  }
199  } else {
200  valid = 0;
201  invalid++;
202  i++;
203  }
204  }
205  if (valid > 100 && valid >> 4 > invalid)
206  return AVPROBE_SCORE_EXTENSION / 2 + 1;
207  return 0;
208 }
209 
210 static int amrnb_read_header(AVFormatContext *s)
211 {
213  if (!st)
214  return AVERROR(ENOMEM);
216  st->codecpar->sample_rate = 8000;
217  st->codecpar->channels = 1;
220  avpriv_set_pts_info(st, 64, 1, 8000);
221 
222  return 0;
223 }
224 
226  .name = "amrnb",
227  .long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
228  .priv_data_size = sizeof(AMRContext),
229  .read_probe = amrnb_probe,
230  .read_header = amrnb_read_header,
233 };
234 #endif
235 
236 #if CONFIG_AMRWB_DEMUXER
237 static int amrwb_probe(const AVProbeData *p)
238 {
239  int mode, i = 0, valid = 0, invalid = 0;
240  const uint8_t *b = p->buf;
241 
242  while (i < p->buf_size) {
243  mode = b[i] >> 3 & 0x0F;
244  if (mode < 10 && (b[i] & 0x4) == 0x4) {
245  int last = b[i];
246  int size = amrwb_packed_size[mode];
247  while (size--) {
248  if (b[++i] != last)
249  break;
250  }
251  if (size > 0) {
252  valid++;
253  i += size;
254  }
255  } else {
256  valid = 0;
257  invalid++;
258  i++;
259  }
260  }
261  if (valid > 100 && valid >> 4 > invalid)
262  return AVPROBE_SCORE_EXTENSION / 2 + 1;
263  return 0;
264 }
265 
266 static int amrwb_read_header(AVFormatContext *s)
267 {
269  if (!st)
270  return AVERROR(ENOMEM);
272  st->codecpar->sample_rate = 16000;
273  st->codecpar->channels = 1;
276  avpriv_set_pts_info(st, 64, 1, 16000);
277 
278  return 0;
279 }
280 
282  .name = "amrwb",
283  .long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
284  .priv_data_size = sizeof(AMRContext),
285  .read_probe = amrwb_probe,
286  .read_header = amrwb_read_header,
289 };
290 #endif
291 
292 #if CONFIG_AMR_MUXER
294  .name = "amr",
295  .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
296  .mime_type = "audio/amr",
297  .extensions = "amr",
298  .audio_codec = AV_CODEC_ID_AMR_NB,
299  .video_codec = AV_CODEC_ID_NONE,
300  .write_header = amr_write_header,
301  .write_packet = amr_write_packet,
302  .flags = AVFMT_NOTIMESTAMPS,
303 };
304 #endif
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AVOutputFormat::name
const char * name
Definition: avformat.h:496
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:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3949
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
b
#define b
Definition: input.c:41
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: avcodec.h:547
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1495
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3961
AV_CODEC_ID_AMR_WB
@ AV_CODEC_ID_AMR_WB
Definition: avcodec.h:548
amr_read_packet
static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: amr.c:122
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
AMRContext::cumulated_size
uint64_t cumulated_size
Definition: amr.c:34
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:468
AMR_header
static const char AMR_header[]
Definition: amr.c:38
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:86
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:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
NULL
#define NULL
Definition: coverity.c:32
amrnb_packed_size
static const uint8_t amrnb_packed_size[16]
Definition: amr.c:41
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:446
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:456
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:4067
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
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:4910
size
int size
Definition: twinvq_data.h:11134
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:218
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
amrwb_packed_size
static const uint8_t amrwb_packed_size[16]
Definition: amr.c:44
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: avcodec.h:216
AVOutputFormat
Definition: avformat.h:495
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AMRContext::block_count
uint64_t block_count
Definition: amr.c:35
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:870
amr_read_header
static int amr_read_header(AVFormatContext *s)
Definition: amr.c:87
avformat.h
channel_layout.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
mode
mode
Definition: ebur128.h:83
AMRWB_header
static const char AMRWB_header[]
Definition: amr.c:39
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
ff_amr_muxer
AVOutputFormat ff_amr_muxer
avio_flush
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:238
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1497
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4059
amr_probe
static int amr_probe(const AVProbeData *p)
Definition: amr.c:74
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
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:358