Browse code

RTP/AMR depacketizer, by Martin Storsjö <$firstname at $firstname dot st>.

Originally committed as revision 21740 to svn://svn.ffmpeg.org/ffmpeg/trunk

Ronald S. Bultje authored on 2010/02/11 02:20:50
Showing 6 changed files
... ...
@@ -56,6 +56,7 @@ version <next>:
56 56
 - IFF PBM/ILBM bitmap decoder
57 57
 - concat protocol
58 58
 - Indeo 5 decoder
59
+- RTP depacketization of AMR
59 60
 
60 61
 
61 62
 
... ...
@@ -214,6 +214,7 @@ OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o        \
214 214
                                             rdt.o         \
215 215
                                             rtp.o         \
216 216
                                             rtpdec.o      \
217
+                                            rtpdec_amr.o  \
217 218
                                             rtpdec_h263.o \
218 219
                                             rtp_asf.o     \
219 220
                                             rtp_h264.o    \
... ...
@@ -22,7 +22,7 @@
22 22
 #define AVFORMAT_AVFORMAT_H
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25
-#define LIBAVFORMAT_VERSION_MINOR 51
25
+#define LIBAVFORMAT_VERSION_MINOR 52
26 26
 #define LIBAVFORMAT_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
... ...
@@ -33,6 +33,7 @@
33 33
 #include "rtp_asf.h"
34 34
 #include "rtp_h264.h"
35 35
 #include "rtp_vorbis.h"
36
+#include "rtpdec_amr.h"
36 37
 #include "rtpdec_h263.h"
37 38
 
38 39
 //#define DEBUG
... ...
@@ -62,6 +63,8 @@ void av_register_rtp_dynamic_payload_handlers(void)
62 62
 {
63 63
     ff_register_dynamic_payload_handler(&mp4v_es_handler);
64 64
     ff_register_dynamic_payload_handler(&mpeg4_generic_handler);
65
+    ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
66
+    ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
65 67
     ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
66 68
     ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
67 69
     ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
68 70
new file mode 100644
... ...
@@ -0,0 +1,177 @@
0
+/*
1
+ * RTP AMR Depacketizer, RFC 3267
2
+ * Copyright (c) 2010 Martin Storsjo
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "rtpdec_amr.h"
23
+#include "libavutil/avstring.h"
24
+
25
+static const uint8_t frame_sizes_nb[16] = {
26
+    12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
27
+};
28
+static const uint8_t frame_sizes_wb[16] = {
29
+    17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
30
+};
31
+
32
+static int amr_handle_packet(AVFormatContext *ctx,
33
+                             PayloadContext *data,
34
+                             AVStream *st,
35
+                             AVPacket * pkt,
36
+                             uint32_t * timestamp,
37
+                             const uint8_t * buf,
38
+                             int len, int flags)
39
+{
40
+    const uint8_t *frame_sizes = NULL;
41
+    int frames;
42
+    int i;
43
+    const uint8_t *speech_data;
44
+    uint8_t *ptr;
45
+
46
+    if (st->codec->codec_id == CODEC_ID_AMR_NB) {
47
+        frame_sizes = frame_sizes_nb;
48
+    } else if (st->codec->codec_id == CODEC_ID_AMR_WB) {
49
+        frame_sizes = frame_sizes_wb;
50
+    } else {
51
+        av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
52
+        return AVERROR_INVALIDDATA;
53
+    }
54
+
55
+    if (st->codec->channels != 1) {
56
+        av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
57
+        return AVERROR_INVALIDDATA;
58
+    }
59
+
60
+    /* The AMR RTP packet consists of one header byte, followed
61
+     * by one TOC byte for each AMR frame in the packet, followed
62
+     * by the speech data for all the AMR frames.
63
+     *
64
+     * The header byte contains only a codec mode request, for
65
+     * requesting what kind of AMR data the sender wants to
66
+     * receive. Not used at the moment.
67
+     */
68
+
69
+    /* Count the number of frames in the packet. The highest bit
70
+     * is set in a TOC byte if there are more frames following.
71
+     */
72
+    for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
73
+
74
+    if (1 + frames >= len) {
75
+        /* We hit the end of the packet while counting frames. */
76
+        av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
77
+        return AVERROR_INVALIDDATA;
78
+    }
79
+
80
+    speech_data = buf + 1 + frames;
81
+
82
+    /* Everything except the codec mode request byte should be output. */
83
+    if (av_new_packet(pkt, len - 1)) {
84
+        av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
85
+        return AVERROR_NOMEM;
86
+    }
87
+    pkt->stream_index = st->index;
88
+    ptr = pkt->data;
89
+
90
+    for (i = 0; i < frames; i++) {
91
+        uint8_t toc = buf[1 + i];
92
+        int frame_size = frame_sizes[(toc >> 3) & 0x0f];
93
+
94
+        if (speech_data + frame_size > buf + len) {
95
+            /* Too little speech data */
96
+            av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
97
+            /* Set the unwritten part of the packet to zero. */
98
+            memset(ptr, 0, pkt->data + pkt->size - ptr);
99
+            pkt->size = ptr - pkt->data;
100
+            return 0;
101
+        }
102
+
103
+        /* Extract the AMR frame mode from the TOC byte */
104
+        *ptr++ = toc & 0x7C;
105
+
106
+        /* Copy the speech data */
107
+        memcpy(ptr, speech_data, frame_size);
108
+        speech_data += frame_size;
109
+        ptr += frame_size;
110
+    }
111
+
112
+    if (speech_data < buf + len) {
113
+        av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
114
+        /* Set the unwritten part of the packet to zero. */
115
+        memset(ptr, 0, pkt->data + pkt->size - ptr);
116
+        pkt->size = ptr - pkt->data;
117
+    }
118
+
119
+    return 0;
120
+}
121
+
122
+static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
123
+                              PayloadContext *data, const char *line)
124
+{
125
+    const char *p;
126
+    char attr[25], value[25];
127
+
128
+    /* Parse an fmtp line this one:
129
+     * a=fmtp:97 octet-align=1; interleaving=0
130
+     * That is, a normal fmtp: line followed by semicolon & space
131
+     * separated key/value pairs.
132
+     */
133
+    if (av_strstart(line, "fmtp:", &p)) {
134
+        int octet_align = 0;
135
+        int crc = 0;
136
+        int interleaving = 0;
137
+        int channels = 1;
138
+
139
+        while (*p && *p == ' ') p++; /* strip spaces */
140
+        while (*p && *p != ' ') p++; /* eat protocol identifier */
141
+        while (*p && *p == ' ') p++; /* strip trailing spaces */
142
+
143
+        while (rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value))) {
144
+            if (!strcmp(attr, "octet-align"))
145
+                octet_align = atoi(value);
146
+            else if (!strcmp(attr, "crc"))
147
+                crc = atoi(value);
148
+            else if (!strcmp(attr, "interleaving"))
149
+                interleaving = atoi(value);
150
+            else if (!strcmp(attr, "channels"))
151
+                channels = atoi(value);
152
+        }
153
+        if (!octet_align || crc || interleaving || channels != 1) {
154
+            av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
155
+            return -1;
156
+        }
157
+    }
158
+    return 0;
159
+}
160
+
161
+RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
162
+    .enc_name         = "AMR",
163
+    .codec_type       = CODEC_TYPE_AUDIO,
164
+    .codec_id         = CODEC_ID_AMR_NB,
165
+    .parse_sdp_a_line = amr_parse_sdp_line,
166
+    .parse_packet     = amr_handle_packet,
167
+};
168
+
169
+RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
170
+    .enc_name         = "AMR-WB",
171
+    .codec_type       = CODEC_TYPE_AUDIO,
172
+    .codec_id         = CODEC_ID_AMR_WB,
173
+    .parse_sdp_a_line = amr_parse_sdp_line,
174
+    .parse_packet     = amr_handle_packet,
175
+};
176
+
0 177
new file mode 100644
... ...
@@ -0,0 +1,30 @@
0
+/*
1
+ * RTP AMR Depacketizer, RFC 3267
2
+ * Copyright (c) 2010 Martin Storsjo
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVFORMAT_RTPDEC_AMR_H
22
+#define AVFORMAT_RTPDEC_AMR_H
23
+
24
+#include "rtpdec.h"
25
+
26
+extern RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler;
27
+extern RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler;
28
+
29
+#endif /* AVFORMAT_RTPDEC_AMR_H */