comparison rtpdec_amr.c @ 5643:457efbf56d15 libavformat

RTP/AMR depacketizer, by Martin Storsj <$firstname at $firstname dot st>.
author rbultje
date Wed, 10 Feb 2010 17:20:50 +0000
parents
children 484fceabfad2
comparison
equal deleted inserted replaced
5642:747753995dee 5643:457efbf56d15
1 /*
2 * RTP AMR Depacketizer, RFC 3267
3 * Copyright (c) 2010 Martin Storsjo
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 #include "avformat.h"
23 #include "rtpdec_amr.h"
24 #include "libavutil/avstring.h"
25
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
28 };
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
31 };
32
33 static int amr_handle_packet(AVFormatContext *ctx,
34 PayloadContext *data,
35 AVStream *st,
36 AVPacket * pkt,
37 uint32_t * timestamp,
38 const uint8_t * buf,
39 int len, int flags)
40 {
41 const uint8_t *frame_sizes = NULL;
42 int frames;
43 int i;
44 const uint8_t *speech_data;
45 uint8_t *ptr;
46
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;
51 } else {
52 av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
53 return AVERROR_INVALIDDATA;
54 }
55
56 if (st->codec->channels != 1) {
57 av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
58 return AVERROR_INVALIDDATA;
59 }
60
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.
64 *
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.
68 */
69
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.
72 */
73 for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
74
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;
79 }
80
81 speech_data = buf + 1 + frames;
82
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_NOMEM;
87 }
88 pkt->stream_index = st->index;
89 ptr = pkt->data;
90
91 for (i = 0; i < frames; i++) {
92 uint8_t toc = buf[1 + i];
93 int frame_size = frame_sizes[(toc >> 3) & 0x0f];
94
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;
101 return 0;
102 }
103
104 /* Extract the AMR frame mode from the TOC byte */
105 *ptr++ = toc & 0x7C;
106
107 /* Copy the speech data */
108 memcpy(ptr, speech_data, frame_size);
109 speech_data += frame_size;
110 ptr += frame_size;
111 }
112
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;
118 }
119
120 return 0;
121 }
122
123 static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
124 PayloadContext *data, const char *line)
125 {
126 const char *p;
127 char attr[25], value[25];
128
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.
133 */
134 if (av_strstart(line, "fmtp:", &p)) {
135 int octet_align = 0;
136 int crc = 0;
137 int interleaving = 0;
138 int channels = 1;
139
140 while (*p && *p == ' ') p++; /* strip spaces */
141 while (*p && *p != ' ') p++; /* eat protocol identifier */
142 while (*p && *p == ' ') p++; /* strip trailing spaces */
143
144 while (rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value))) {
145 if (!strcmp(attr, "octet-align"))
146 octet_align = atoi(value);
147 else if (!strcmp(attr, "crc"))
148 crc = atoi(value);
149 else if (!strcmp(attr, "interleaving"))
150 interleaving = atoi(value);
151 else if (!strcmp(attr, "channels"))
152 channels = atoi(value);
153 }
154 if (!octet_align || crc || interleaving || channels != 1) {
155 av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
156 return -1;
157 }
158 }
159 return 0;
160 }
161
162 RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
163 .enc_name = "AMR",
164 .codec_type = CODEC_TYPE_AUDIO,
165 .codec_id = CODEC_ID_AMR_NB,
166 .parse_sdp_a_line = amr_parse_sdp_line,
167 .parse_packet = amr_handle_packet,
168 };
169
170 RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
171 .enc_name = "AMR-WB",
172 .codec_type = CODEC_TYPE_AUDIO,
173 .codec_id = CODEC_ID_AMR_WB,
174 .parse_sdp_a_line = amr_parse_sdp_line,
175 .parse_packet = amr_handle_packet,
176 };
177