yading@11
|
1 /*
|
yading@11
|
2 * RTP AMR Depacketizer, RFC 3267
|
yading@11
|
3 * Copyright (c) 2010 Martin Storsjo
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 #include "libavutil/channel_layout.h"
|
yading@11
|
23 #include "avformat.h"
|
yading@11
|
24 #include "rtpdec_formats.h"
|
yading@11
|
25 #include "libavutil/avstring.h"
|
yading@11
|
26
|
yading@11
|
27 static const uint8_t frame_sizes_nb[16] = {
|
yading@11
|
28 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
|
yading@11
|
29 };
|
yading@11
|
30 static const uint8_t frame_sizes_wb[16] = {
|
yading@11
|
31 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
|
yading@11
|
32 };
|
yading@11
|
33
|
yading@11
|
34 struct PayloadContext {
|
yading@11
|
35 int octet_align;
|
yading@11
|
36 int crc;
|
yading@11
|
37 int interleaving;
|
yading@11
|
38 int channels;
|
yading@11
|
39 };
|
yading@11
|
40
|
yading@11
|
41 static PayloadContext *amr_new_context(void)
|
yading@11
|
42 {
|
yading@11
|
43 PayloadContext *data = av_mallocz(sizeof(PayloadContext));
|
yading@11
|
44 if(!data) return data;
|
yading@11
|
45 data->channels = 1;
|
yading@11
|
46 return data;
|
yading@11
|
47 }
|
yading@11
|
48
|
yading@11
|
49 static void amr_free_context(PayloadContext *data)
|
yading@11
|
50 {
|
yading@11
|
51 av_free(data);
|
yading@11
|
52 }
|
yading@11
|
53
|
yading@11
|
54 static int amr_handle_packet(AVFormatContext *ctx, PayloadContext *data,
|
yading@11
|
55 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
yading@11
|
56 const uint8_t *buf, int len, uint16_t seq,
|
yading@11
|
57 int flags)
|
yading@11
|
58 {
|
yading@11
|
59 const uint8_t *frame_sizes = NULL;
|
yading@11
|
60 int frames;
|
yading@11
|
61 int i;
|
yading@11
|
62 const uint8_t *speech_data;
|
yading@11
|
63 uint8_t *ptr;
|
yading@11
|
64
|
yading@11
|
65 if (st->codec->codec_id == AV_CODEC_ID_AMR_NB) {
|
yading@11
|
66 frame_sizes = frame_sizes_nb;
|
yading@11
|
67 } else if (st->codec->codec_id == AV_CODEC_ID_AMR_WB) {
|
yading@11
|
68 frame_sizes = frame_sizes_wb;
|
yading@11
|
69 } else {
|
yading@11
|
70 av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
|
yading@11
|
71 return AVERROR_INVALIDDATA;
|
yading@11
|
72 }
|
yading@11
|
73
|
yading@11
|
74 if (st->codec->channels != 1) {
|
yading@11
|
75 av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
|
yading@11
|
76 return AVERROR_INVALIDDATA;
|
yading@11
|
77 }
|
yading@11
|
78 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
|
yading@11
|
79
|
yading@11
|
80 /* The AMR RTP packet consists of one header byte, followed
|
yading@11
|
81 * by one TOC byte for each AMR frame in the packet, followed
|
yading@11
|
82 * by the speech data for all the AMR frames.
|
yading@11
|
83 *
|
yading@11
|
84 * The header byte contains only a codec mode request, for
|
yading@11
|
85 * requesting what kind of AMR data the sender wants to
|
yading@11
|
86 * receive. Not used at the moment.
|
yading@11
|
87 */
|
yading@11
|
88
|
yading@11
|
89 /* Count the number of frames in the packet. The highest bit
|
yading@11
|
90 * is set in a TOC byte if there are more frames following.
|
yading@11
|
91 */
|
yading@11
|
92 for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
|
yading@11
|
93
|
yading@11
|
94 if (1 + frames >= len) {
|
yading@11
|
95 /* We hit the end of the packet while counting frames. */
|
yading@11
|
96 av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
|
yading@11
|
97 return AVERROR_INVALIDDATA;
|
yading@11
|
98 }
|
yading@11
|
99
|
yading@11
|
100 speech_data = buf + 1 + frames;
|
yading@11
|
101
|
yading@11
|
102 /* Everything except the codec mode request byte should be output. */
|
yading@11
|
103 if (av_new_packet(pkt, len - 1)) {
|
yading@11
|
104 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
|
yading@11
|
105 return AVERROR(ENOMEM);
|
yading@11
|
106 }
|
yading@11
|
107 pkt->stream_index = st->index;
|
yading@11
|
108 ptr = pkt->data;
|
yading@11
|
109
|
yading@11
|
110 for (i = 0; i < frames; i++) {
|
yading@11
|
111 uint8_t toc = buf[1 + i];
|
yading@11
|
112 int frame_size = frame_sizes[(toc >> 3) & 0x0f];
|
yading@11
|
113
|
yading@11
|
114 if (speech_data + frame_size > buf + len) {
|
yading@11
|
115 /* Too little speech data */
|
yading@11
|
116 av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
|
yading@11
|
117 /* Set the unwritten part of the packet to zero. */
|
yading@11
|
118 memset(ptr, 0, pkt->data + pkt->size - ptr);
|
yading@11
|
119 pkt->size = ptr - pkt->data;
|
yading@11
|
120 return 0;
|
yading@11
|
121 }
|
yading@11
|
122
|
yading@11
|
123 /* Extract the AMR frame mode from the TOC byte */
|
yading@11
|
124 *ptr++ = toc & 0x7C;
|
yading@11
|
125
|
yading@11
|
126 /* Copy the speech data */
|
yading@11
|
127 memcpy(ptr, speech_data, frame_size);
|
yading@11
|
128 speech_data += frame_size;
|
yading@11
|
129 ptr += frame_size;
|
yading@11
|
130 }
|
yading@11
|
131
|
yading@11
|
132 if (speech_data < buf + len) {
|
yading@11
|
133 av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
|
yading@11
|
134 /* Set the unwritten part of the packet to zero. */
|
yading@11
|
135 memset(ptr, 0, pkt->data + pkt->size - ptr);
|
yading@11
|
136 pkt->size = ptr - pkt->data;
|
yading@11
|
137 }
|
yading@11
|
138
|
yading@11
|
139 return 0;
|
yading@11
|
140 }
|
yading@11
|
141
|
yading@11
|
142 static int amr_parse_fmtp(AVStream *stream, PayloadContext *data,
|
yading@11
|
143 char *attr, char *value)
|
yading@11
|
144 {
|
yading@11
|
145 /* Some AMR SDP configurations contain "octet-align", without
|
yading@11
|
146 * the trailing =1. Therefore, if the value is empty,
|
yading@11
|
147 * interpret it as "1".
|
yading@11
|
148 */
|
yading@11
|
149 if (!strcmp(value, "")) {
|
yading@11
|
150 av_log(NULL, AV_LOG_WARNING, "AMR fmtp attribute %s had "
|
yading@11
|
151 "nonstandard empty value\n", attr);
|
yading@11
|
152 strcpy(value, "1");
|
yading@11
|
153 }
|
yading@11
|
154 if (!strcmp(attr, "octet-align"))
|
yading@11
|
155 data->octet_align = atoi(value);
|
yading@11
|
156 else if (!strcmp(attr, "crc"))
|
yading@11
|
157 data->crc = atoi(value);
|
yading@11
|
158 else if (!strcmp(attr, "interleaving"))
|
yading@11
|
159 data->interleaving = atoi(value);
|
yading@11
|
160 else if (!strcmp(attr, "channels"))
|
yading@11
|
161 data->channels = atoi(value);
|
yading@11
|
162 return 0;
|
yading@11
|
163 }
|
yading@11
|
164
|
yading@11
|
165 static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
|
yading@11
|
166 PayloadContext *data, const char *line)
|
yading@11
|
167 {
|
yading@11
|
168 const char *p;
|
yading@11
|
169 int ret;
|
yading@11
|
170
|
yading@11
|
171 if (st_index < 0)
|
yading@11
|
172 return 0;
|
yading@11
|
173
|
yading@11
|
174 /* Parse an fmtp line this one:
|
yading@11
|
175 * a=fmtp:97 octet-align=1; interleaving=0
|
yading@11
|
176 * That is, a normal fmtp: line followed by semicolon & space
|
yading@11
|
177 * separated key/value pairs.
|
yading@11
|
178 */
|
yading@11
|
179 if (av_strstart(line, "fmtp:", &p)) {
|
yading@11
|
180 ret = ff_parse_fmtp(s->streams[st_index], data, p, amr_parse_fmtp);
|
yading@11
|
181 if (!data->octet_align || data->crc ||
|
yading@11
|
182 data->interleaving || data->channels != 1) {
|
yading@11
|
183 av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
|
yading@11
|
184 return -1;
|
yading@11
|
185 }
|
yading@11
|
186 return ret;
|
yading@11
|
187 }
|
yading@11
|
188 return 0;
|
yading@11
|
189 }
|
yading@11
|
190
|
yading@11
|
191 RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
|
yading@11
|
192 .enc_name = "AMR",
|
yading@11
|
193 .codec_type = AVMEDIA_TYPE_AUDIO,
|
yading@11
|
194 .codec_id = AV_CODEC_ID_AMR_NB,
|
yading@11
|
195 .parse_sdp_a_line = amr_parse_sdp_line,
|
yading@11
|
196 .alloc = amr_new_context,
|
yading@11
|
197 .free = amr_free_context,
|
yading@11
|
198 .parse_packet = amr_handle_packet,
|
yading@11
|
199 };
|
yading@11
|
200
|
yading@11
|
201 RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
|
yading@11
|
202 .enc_name = "AMR-WB",
|
yading@11
|
203 .codec_type = AVMEDIA_TYPE_AUDIO,
|
yading@11
|
204 .codec_id = AV_CODEC_ID_AMR_WB,
|
yading@11
|
205 .parse_sdp_a_line = amr_parse_sdp_line,
|
yading@11
|
206 .alloc = amr_new_context,
|
yading@11
|
207 .free = amr_free_context,
|
yading@11
|
208 .parse_packet = amr_handle_packet,
|
yading@11
|
209 };
|