2 * RTP AMR Depacketizer, RFC 3267
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
24 #include "rtpdec_formats.h"
25 #include "libavutil/avstring.h"
27 static const uint8_t frame_sizes_nb[16] = {
28 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
30 static const uint8_t frame_sizes_wb[16] = {
31 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
34 struct PayloadContext {
41 static PayloadContext *amr_new_context(void)
43 PayloadContext *data = av_mallocz(sizeof(PayloadContext));
44 if(!data) return data;
49 static void amr_free_context(PayloadContext *data)
54 static int amr_handle_packet(AVFormatContext *ctx,
62 const uint8_t *frame_sizes = NULL;
65 const uint8_t *speech_data;
68 if (st->codec->codec_id == AV_CODEC_ID_AMR_NB) {
69 frame_sizes = frame_sizes_nb;
70 } else if (st->codec->codec_id == AV_CODEC_ID_AMR_WB) {
71 frame_sizes = frame_sizes_wb;
73 av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
74 return AVERROR_INVALIDDATA;
77 if (st->codec->channels != 1) {
78 av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
79 return AVERROR_INVALIDDATA;
81 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
83 /* The AMR RTP packet consists of one header byte, followed
84 * by one TOC byte for each AMR frame in the packet, followed
85 * by the speech data for all the AMR frames.
87 * The header byte contains only a codec mode request, for
88 * requesting what kind of AMR data the sender wants to
89 * receive. Not used at the moment.
92 /* Count the number of frames in the packet. The highest bit
93 * is set in a TOC byte if there are more frames following.
95 for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
97 if (1 + frames >= len) {
98 /* We hit the end of the packet while counting frames. */
99 av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
100 return AVERROR_INVALIDDATA;
103 speech_data = buf + 1 + frames;
105 /* Everything except the codec mode request byte should be output. */
106 if (av_new_packet(pkt, len - 1)) {
107 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
108 return AVERROR(ENOMEM);
110 pkt->stream_index = st->index;
113 for (i = 0; i < frames; i++) {
114 uint8_t toc = buf[1 + i];
115 int frame_size = frame_sizes[(toc >> 3) & 0x0f];
117 if (speech_data + frame_size > buf + len) {
118 /* Too little speech data */
119 av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
120 /* Set the unwritten part of the packet to zero. */
121 memset(ptr, 0, pkt->data + pkt->size - ptr);
122 pkt->size = ptr - pkt->data;
126 /* Extract the AMR frame mode from the TOC byte */
129 /* Copy the speech data */
130 memcpy(ptr, speech_data, frame_size);
131 speech_data += frame_size;
135 if (speech_data < buf + len) {
136 av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
137 /* Set the unwritten part of the packet to zero. */
138 memset(ptr, 0, pkt->data + pkt->size - ptr);
139 pkt->size = ptr - pkt->data;
145 static int amr_parse_fmtp(AVStream *stream, PayloadContext *data,
146 char *attr, char *value)
148 /* Some AMR SDP configurations contain "octet-align", without
149 * the trailing =1. Therefore, if the value is empty,
150 * interpret it as "1".
152 if (!strcmp(value, "")) {
153 av_log(NULL, AV_LOG_WARNING, "AMR fmtp attribute %s had "
154 "nonstandard empty value\n", attr);
157 if (!strcmp(attr, "octet-align"))
158 data->octet_align = atoi(value);
159 else if (!strcmp(attr, "crc"))
160 data->crc = atoi(value);
161 else if (!strcmp(attr, "interleaving"))
162 data->interleaving = atoi(value);
163 else if (!strcmp(attr, "channels"))
164 data->channels = atoi(value);
168 static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
169 PayloadContext *data, const char *line)
177 /* Parse an fmtp line this one:
178 * a=fmtp:97 octet-align=1; interleaving=0
179 * That is, a normal fmtp: line followed by semicolon & space
180 * separated key/value pairs.
182 if (av_strstart(line, "fmtp:", &p)) {
183 ret = ff_parse_fmtp(s->streams[st_index], data, p, amr_parse_fmtp);
184 if (!data->octet_align || data->crc ||
185 data->interleaving || data->channels != 1) {
186 av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
194 RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
196 .codec_type = AVMEDIA_TYPE_AUDIO,
197 .codec_id = AV_CODEC_ID_AMR_NB,
198 .parse_sdp_a_line = amr_parse_sdp_line,
199 .alloc = amr_new_context,
200 .free = amr_free_context,
201 .parse_packet = amr_handle_packet,
204 RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
205 .enc_name = "AMR-WB",
206 .codec_type = AVMEDIA_TYPE_AUDIO,
207 .codec_id = AV_CODEC_ID_AMR_WB,
208 .parse_sdp_a_line = amr_parse_sdp_line,
209 .alloc = amr_new_context,
210 .free = amr_free_context,
211 .parse_packet = amr_handle_packet,