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
23 #include "rtpdec_amr.h"
24 #include "libavutil/avstring.h"
26 static const uint8_t frame_sizes_nb[16] = {
27 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
29 static const uint8_t frame_sizes_wb[16] = {
30 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
33 static int amr_handle_packet(AVFormatContext *ctx,
41 const uint8_t *frame_sizes = NULL;
44 const uint8_t *speech_data;
47 if (st->codec->codec_id == CODEC_ID_AMR_NB) {
48 frame_sizes = frame_sizes_nb;
49 } else if (st->codec->codec_id == CODEC_ID_AMR_WB) {
50 frame_sizes = frame_sizes_wb;
52 av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
53 return AVERROR_INVALIDDATA;
56 if (st->codec->channels != 1) {
57 av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
58 return AVERROR_INVALIDDATA;
61 /* The AMR RTP packet consists of one header byte, followed
62 * by one TOC byte for each AMR frame in the packet, followed
63 * by the speech data for all the AMR frames.
65 * The header byte contains only a codec mode request, for
66 * requesting what kind of AMR data the sender wants to
67 * receive. Not used at the moment.
70 /* Count the number of frames in the packet. The highest bit
71 * is set in a TOC byte if there are more frames following.
73 for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
75 if (1 + frames >= len) {
76 /* We hit the end of the packet while counting frames. */
77 av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
78 return AVERROR_INVALIDDATA;
81 speech_data = buf + 1 + frames;
83 /* Everything except the codec mode request byte should be output. */
84 if (av_new_packet(pkt, len - 1)) {
85 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
86 return AVERROR(ENOMEM);
88 pkt->stream_index = st->index;
91 for (i = 0; i < frames; i++) {
92 uint8_t toc = buf[1 + i];
93 int frame_size = frame_sizes[(toc >> 3) & 0x0f];
95 if (speech_data + frame_size > buf + len) {
96 /* Too little speech data */
97 av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
98 /* Set the unwritten part of the packet to zero. */
99 memset(ptr, 0, pkt->data + pkt->size - ptr);
100 pkt->size = ptr - pkt->data;
104 /* Extract the AMR frame mode from the TOC byte */
107 /* Copy the speech data */
108 memcpy(ptr, speech_data, frame_size);
109 speech_data += frame_size;
113 if (speech_data < buf + len) {
114 av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
115 /* Set the unwritten part of the packet to zero. */
116 memset(ptr, 0, pkt->data + pkt->size - ptr);
117 pkt->size = ptr - pkt->data;
123 static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
124 PayloadContext *data, const char *line)
127 char attr[25], value[25];
129 /* Parse an fmtp line this one:
130 * a=fmtp:97 octet-align=1; interleaving=0
131 * That is, a normal fmtp: line followed by semicolon & space
132 * separated key/value pairs.
134 if (av_strstart(line, "fmtp:", &p)) {
137 int interleaving = 0;
140 while (*p && *p == ' ') p++; /* strip spaces */
141 while (*p && *p != ' ') p++; /* eat protocol identifier */
142 while (*p && *p == ' ') p++; /* strip trailing spaces */
144 while (ff_rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value))) {
145 /* Some AMR SDP configurations contain "octet-align", without
146 * the trailing =1. Therefore, if the value is empty,
147 * interpret it as "1".
149 if (!strcmp(value, "")) {
150 av_log(s, AV_LOG_WARNING, "AMR fmtp attribute %s had "
151 "nonstandard empty value\n", attr);
154 if (!strcmp(attr, "octet-align"))
155 octet_align = atoi(value);
156 else if (!strcmp(attr, "crc"))
158 else if (!strcmp(attr, "interleaving"))
159 interleaving = atoi(value);
160 else if (!strcmp(attr, "channels"))
161 channels = atoi(value);
163 if (!octet_align || crc || interleaving || channels != 1) {
164 av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
171 RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
173 .codec_type = AVMEDIA_TYPE_AUDIO,
174 .codec_id = CODEC_ID_AMR_NB,
175 .parse_sdp_a_line = amr_parse_sdp_line,
176 .parse_packet = amr_handle_packet,
179 RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
180 .enc_name = "AMR-WB",
181 .codec_type = AVMEDIA_TYPE_AUDIO,
182 .codec_id = CODEC_ID_AMR_WB,
183 .parse_sdp_a_line = amr_parse_sdp_line,
184 .parse_packet = amr_handle_packet,