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 
26 #include "config_components.h"
27 
29 #include "libavutil/intreadwrite.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "demux.h"
33 #include "internal.h"
34 #include "mux.h"
35 #include "rawdec.h"
36 #include "rawenc.h"
37 
38 typedef struct AMRContext {
40 } AMRContext;
41 
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";
46 
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
49 };
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
52 };
53 
54 #if CONFIG_AMR_DEMUXER
55 static int amr_probe(const AVProbeData *p)
56 {
57  // Only check for "#!AMR" which could be amr-wb, amr-nb.
58  // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
59  // "#!AMR-WB_MC1.0\n"
60 
61  if (!memcmp(p->buf, AMR_header, 5))
62  return AVPROBE_SCORE_MAX;
63  else
64  return 0;
65 }
66 
67 /* amr input */
68 static int amr_read_header(AVFormatContext *s)
69 {
70  AVIOContext *pb = s->pb;
71  AVStream *st;
72  uint8_t header[19] = { 0 };
73  int read, back = 0, ret;
74 
75  ret = ffio_ensure_seekback(s->pb, sizeof(header));
76  if (ret < 0)
77  return ret;
78 
79  read = avio_read(pb, header, sizeof(header));
80  if (read < 0)
81  return read;
82 
83  st = avformat_new_stream(s, NULL);
84  if (!st)
85  return AVERROR(ENOMEM);
86  if (!memcmp(header, AMR_header, sizeof(AMR_header))) {
87  st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
89  st->codecpar->sample_rate = 8000;
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');
95  st->codecpar->sample_rate = 16000;
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');
101  st->codecpar->sample_rate = 8000;
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');
107  st->codecpar->sample_rate = 16000;
109  back = read - 4 - sizeof(AMRWBMC_header);
110  } else {
111  return AVERROR_INVALIDDATA;
112  }
113 
114  if (st->codecpar->ch_layout.nb_channels < 1)
115  return AVERROR_INVALIDDATA;
116 
119  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
120 
121  if (back > 0)
122  avio_seek(pb, -back, SEEK_CUR);
123 
124  return 0;
125 }
126 
128  .p.name = "amr",
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,
136 };
137 #endif
138 
139 #if CONFIG_AMRNB_DEMUXER
140 static int amrnb_probe(const AVProbeData *p)
141 {
142  int mode, i = 0, valid = 0, invalid = 0;
143  const uint8_t *b = p->buf;
144 
145  while (i < p->buf_size) {
146  mode = b[i] >> 3 & 0x0F;
147  if (mode < 9 && (b[i] & 0x4) == 0x4) {
148  int last = b[i];
149  int size = amrnb_packed_size[mode];
150  while (size--) {
151  if (b[++i] != last)
152  break;
153  }
154  if (size > 0) {
155  valid++;
156  i += size;
157  }
158  } else {
159  valid = 0;
160  invalid++;
161  i++;
162  }
163  }
164  if (valid > 100 && valid >> 4 > invalid)
165  return AVPROBE_SCORE_EXTENSION / 2 + 1;
166  return 0;
167 }
168 
169 static int amrnb_read_header(AVFormatContext *s)
170 {
172  if (!st)
173  return AVERROR(ENOMEM);
175  st->codecpar->sample_rate = 8000;
179  avpriv_set_pts_info(st, 64, 1, 8000);
180 
181  return 0;
182 }
183 
185  .p.name = "amrnb",
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,
193 };
194 #endif
195 
196 #if CONFIG_AMRWB_DEMUXER
197 static int amrwb_probe(const AVProbeData *p)
198 {
199  int mode, i = 0, valid = 0, invalid = 0;
200  const uint8_t *b = p->buf;
201 
202  while (i < p->buf_size) {
203  mode = b[i] >> 3 & 0x0F;
204  if (mode < 10 && (b[i] & 0x4) == 0x4) {
205  int last = b[i];
206  int size = amrwb_packed_size[mode];
207  while (size--) {
208  if (b[++i] != last)
209  break;
210  }
211  if (size > 0) {
212  valid++;
213  i += size;
214  }
215  } else {
216  valid = 0;
217  invalid++;
218  i++;
219  }
220  }
221  if (valid > 100 && valid >> 4 > invalid)
222  return AVPROBE_SCORE_EXTENSION / 2 + 1;
223  return 0;
224 }
225 
226 static int amrwb_read_header(AVFormatContext *s)
227 {
229  if (!st)
230  return AVERROR(ENOMEM);
232  st->codecpar->sample_rate = 16000;
236  avpriv_set_pts_info(st, 64, 1, 16000);
237 
238  return 0;
239 }
240 
242  .p.name = "amrwb",
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,
250 };
251 #endif
252 
253 #if CONFIG_AMR_MUXER
254 static int amr_write_header(AVFormatContext *s)
255 {
256  AVIOContext *pb = s->pb;
257  AVCodecParameters *par = s->streams[0]->codecpar;
258 
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 */
263  } else {
264  return -1;
265  }
266  return 0;
267 }
268 
270  .p.name = "amr",
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,
281 };
282 #endif
ff_amr_demuxer
const FFInputFormat ff_amr_demuxer
AVOutputFormat::name
const char * name
Definition: avformat.h:510
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
AMRContext::rawctx
FFRawDemuxerContext rawctx
Definition: amr.c:39
ff_amr_muxer
const FFOutputFormat ff_amr_muxer
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
mode
Definition: swscale.c:52
b
#define b
Definition: input.c:41
ff_amrwb_demuxer
const FFInputFormat ff_amrwb_demuxer
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:427
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
AV_CODEC_ID_AMR_WB
@ AV_CODEC_ID_AMR_WB
Definition: codec_id.h:428
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
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: avformat.c:867
AMR_header
static const uint8_t AMR_header[6]
Definition: amr.c:42
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
FFRawDemuxerContext
Definition: rawdec.h:37
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
ff_raw_read_partial_packet
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:33
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
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:31
AMRContext
Definition: amrnbdec.c:100
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:325
ff_raw_demuxer_class
const AVClass ff_raw_demuxer_class
Definition: rawdec.c:141
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
rawdec.h
amrnb_packed_size
static const uint8_t amrnb_packed_size[16]
Definition: amr.c:47
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
FFOutputFormat
Definition: mux.h:61
ff_amrnb_demuxer
const FFInputFormat ff_amrnb_demuxer
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
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:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
header
static const uint8_t header[24]
Definition: sdr2.c:68
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
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:1023
amrwb_packed_size
static const uint8_t amrwb_packed_size[16]
Definition: amr.c:50
rawenc.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avio_internal.h
AMRWBMC_header
static const uint8_t AMRWBMC_header[15]
Definition: amr.c:45
AMRMC_header
static const uint8_t AMRMC_header[12]
Definition: amr.c:43
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#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
demux.h
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
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:612
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:597
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:392
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
FFInputFormat
Definition: demux.h:42
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
read
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.
Definition: bitstream_template.h:231
AMRWB_header
static const uint8_t AMRWB_header[9]
Definition: amr.c:44
mux.h