2 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-rtpamrdepay
22 * @see_also: rtpamrpay
24 * Extract AMR audio from RTP packets according to RFC 3267.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3267.txt
28 * <title>Example pipeline</title>
30 * gst-launch-1.0 udpsrc caps='application/x-rtp, media=(string)audio, clock-rate=(int)8000, encoding-name=(string)AMR, encoding-params=(string)1, octet-align=(string)1, payload=(int)96' ! rtpamrdepay ! amrnbdec ! pulsesink
31 * ]| This example pipeline will depayload and decode an RTP AMR stream. Refer to
32 * the rtpamrpay example to create the RTP stream.
35 * Last reviewed on 2013-04-25 (1.1.0)
39 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
40 * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
41 * Wideband (AMR-WB) Audio Codecs.
48 #include <gst/rtp/gstrtpbuffer.h>
52 #include "gstrtpamrdepay.h"
54 GST_DEBUG_CATEGORY_STATIC (rtpamrdepay_debug);
55 #define GST_CAT_DEFAULT (rtpamrdepay_debug)
57 /* RtpAMRDepay signals and args */
69 /* input is an RTP packet
71 * params see RFC 3267, section 8.1
73 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_STATIC_CAPS ("application/x-rtp, "
78 "media = (string) \"audio\", "
79 "clock-rate = (int) 8000, "
80 "encoding-name = (string) \"AMR\", "
81 "encoding-params = (string) \"1\", "
82 /* NOTE that all values must be strings in orde to be able to do SDP <->
84 "octet-align = (string) \"1\";"
85 /* following options are not needed for a decoder
87 "crc = (string) { \"0\", \"1\" }, "
88 "robust-sorting = (string) \"0\", "
89 "interleaving = (string) \"0\";"
90 "mode-set = (int) [ 0, 7 ], "
91 "mode-change-period = (int) [ 1, MAX ], "
92 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
93 "maxptime = (int) [ 20, MAX ], "
94 "ptime = (int) [ 20, MAX ]"
97 "media = (string) \"audio\", "
98 "clock-rate = (int) 16000, "
99 "encoding-name = (string) \"AMR-WB\", "
100 "encoding-params = (string) \"1\", "
101 /* NOTE that all values must be strings in orde to be able to do SDP <->
102 * GstCaps mapping. */
103 "octet-align = (string) \"1\";"
104 /* following options are not needed for a decoder
106 "crc = (string) { \"0\", \"1\" }, "
107 "robust-sorting = (string) \"0\", "
108 "interleaving = (string) \"0\""
109 "mode-set = (int) [ 0, 7 ], "
110 "mode-change-period = (int) [ 1, MAX ], "
111 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
112 "maxptime = (int) [ 20, MAX ], "
113 "ptime = (int) [ 20, MAX ]"
118 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
119 GST_STATIC_PAD_TEMPLATE ("src",
122 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000;"
123 "audio/AMR-WB, " "channels = (int) 1," "rate = (int) 16000")
126 static gboolean gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload,
128 static GstBuffer *gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload,
131 #define gst_rtp_amr_depay_parent_class parent_class
132 G_DEFINE_TYPE (GstRtpAMRDepay, gst_rtp_amr_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
135 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
137 GstElementClass *gstelement_class;
138 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
140 gstelement_class = (GstElementClass *) klass;
141 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
143 gst_element_class_add_pad_template (gstelement_class,
144 gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
145 gst_element_class_add_pad_template (gstelement_class,
146 gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
148 gst_element_class_set_static_metadata (gstelement_class,
149 "RTP AMR depayloader", "Codec/Depayloader/Network/RTP",
150 "Extracts AMR or AMR-WB audio from RTP packets (RFC 3267)",
151 "Wim Taymans <wim.taymans@gmail.com>");
153 gstrtpbasedepayload_class->process = gst_rtp_amr_depay_process;
154 gstrtpbasedepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
156 GST_DEBUG_CATEGORY_INIT (rtpamrdepay_debug, "rtpamrdepay", 0,
157 "AMR/AMR-WB RTP Depayloader");
161 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
163 GstRTPBaseDepayload *depayload;
165 depayload = GST_RTP_BASE_DEPAYLOAD (rtpamrdepay);
167 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
171 gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
173 GstStructure *structure;
175 GstRtpAMRDepay *rtpamrdepay;
177 const gchar *str, *type;
178 gint clock_rate, need_clock_rate;
181 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
183 structure = gst_caps_get_structure (caps, 0);
185 /* figure out the mode first and set the clock rates */
186 if ((str = gst_structure_get_string (structure, "encoding-name"))) {
187 if (strcmp (str, "AMR") == 0) {
188 rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
189 need_clock_rate = 8000;
191 } else if (strcmp (str, "AMR-WB") == 0) {
192 rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
193 need_clock_rate = 16000;
194 type = "audio/AMR-WB";
200 if (!(str = gst_structure_get_string (structure, "octet-align")))
201 rtpamrdepay->octet_align = FALSE;
203 rtpamrdepay->octet_align = (atoi (str) == 1);
205 if (!(str = gst_structure_get_string (structure, "crc")))
206 rtpamrdepay->crc = FALSE;
208 rtpamrdepay->crc = (atoi (str) == 1);
210 if (rtpamrdepay->crc) {
211 /* crc mode implies octet aligned mode */
212 rtpamrdepay->octet_align = TRUE;
215 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
216 rtpamrdepay->robust_sorting = FALSE;
218 rtpamrdepay->robust_sorting = (atoi (str) == 1);
220 if (rtpamrdepay->robust_sorting) {
221 /* robust_sorting mode implies octet aligned mode */
222 rtpamrdepay->octet_align = TRUE;
225 if (!(str = gst_structure_get_string (structure, "interleaving")))
226 rtpamrdepay->interleaving = FALSE;
228 rtpamrdepay->interleaving = (atoi (str) == 1);
230 if (rtpamrdepay->interleaving) {
231 /* interleaving mode implies octet aligned mode */
232 rtpamrdepay->octet_align = TRUE;
235 if (!(params = gst_structure_get_string (structure, "encoding-params")))
236 rtpamrdepay->channels = 1;
238 rtpamrdepay->channels = atoi (params);
241 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
242 clock_rate = need_clock_rate;
243 depayload->clock_rate = clock_rate;
245 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
246 * no robust sorting, no interleaving for now */
247 if (rtpamrdepay->channels != 1)
249 if (clock_rate != need_clock_rate)
251 if (rtpamrdepay->octet_align != TRUE)
253 if (rtpamrdepay->robust_sorting != FALSE)
255 if (rtpamrdepay->interleaving != FALSE)
258 srccaps = gst_caps_new_simple (type,
259 "channels", G_TYPE_INT, rtpamrdepay->channels,
260 "rate", G_TYPE_INT, clock_rate, NULL);
261 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
262 gst_caps_unref (srccaps);
269 GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
275 static const gint nb_frame_size[16] = {
276 12, 13, 15, 17, 19, 20, 26, 31,
277 5, -1, -1, -1, -1, -1, -1, 0
280 static const gint wb_frame_size[16] = {
281 17, 23, 32, 36, 40, 46, 50, 58,
282 60, 5, -1, -1, -1, -1, -1, 0
286 gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
288 GstRtpAMRDepay *rtpamrdepay;
289 const gint *frame_size;
290 GstBuffer *outbuf = NULL;
292 GstRTPBuffer rtp = { NULL };
295 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
297 /* setup frame size pointer */
298 if (rtpamrdepay->mode == GST_RTP_AMR_DP_MODE_NB)
299 frame_size = nb_frame_size;
301 frame_size = wb_frame_size;
303 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
305 /* when we get here, 1 channel, 8000/16000 Hz, octet aligned, no CRC,
306 * no robust sorting, no interleaving data is to be depayloaded */
308 guint8 *payload, *p, *dp;
309 gint i, num_packets, num_nonempty_packets;
313 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
315 /* need at least 2 bytes for the header */
319 payload = gst_rtp_buffer_get_payload (&rtp);
321 /* depay CMR. The CMR is used by the sender to request
322 * a new encoding mode.
329 /* CMR = (payload[0] & 0xf0) >> 4; */
331 /* strip CMR header now, pack FT and the data for the decoder */
335 GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
337 if (rtpamrdepay->interleaving) {
338 ILL = (payload[0] & 0xf0) >> 4;
339 ILP = (payload[0] & 0x0f);
345 goto wrong_interleaving;
349 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
350 * +-+-+-+-+-+-+-+-+..
351 * |F| FT |Q|P|P| more FT..
352 * +-+-+-+-+-+-+-+-+..
354 /* count number of packets by counting the FTs. Also
355 * count number of amr data bytes and number of non-empty
356 * packets (this is also the number of CRCs if present). */
358 num_nonempty_packets = 0;
360 for (i = 0; i < payload_len; i++) {
364 FT = (payload[i] & 0x78) >> 3;
366 fr_size = frame_size[FT];
367 GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
369 goto wrong_framesize;
373 num_nonempty_packets++;
377 if ((payload[i] & 0x80) == 0)
381 if (rtpamrdepay->crc) {
382 /* data len + CRC len + header bytes should be smaller than payload_len */
383 if (num_packets + num_nonempty_packets + amr_len > payload_len)
386 /* data len + header bytes should be smaller than payload_len */
387 if (num_packets + amr_len > payload_len)
391 outbuf = gst_buffer_new_and_alloc (payload_len);
393 /* point to destination */
394 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
396 /* point to first data packet */
398 dp = payload + num_packets;
399 if (rtpamrdepay->crc) {
400 /* skip CRC if present */
401 dp += num_nonempty_packets;
404 for (i = 0; i < num_packets; i++) {
407 /* copy FT, clear F bit */
408 *p++ = payload[i] & 0x7f;
410 fr_size = frame_size[(payload[i] & 0x78) >> 3];
412 /* copy data packet, FIXME, calc CRC here. */
413 memcpy (p, dp, fr_size);
419 gst_buffer_unmap (outbuf, &map);
421 /* we can set the duration because each packet is 20 milliseconds */
422 GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
424 if (gst_rtp_buffer_get_marker (&rtp)) {
425 /* marker bit marks a buffer after a talkspurt. */
426 GST_DEBUG_OBJECT (depayload, "marker bit was set");
427 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
430 GST_DEBUG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
431 gst_buffer_get_size (outbuf));
434 gst_rtp_buffer_unmap (&rtp);
440 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
441 (NULL), ("AMR RTP payload too small (%d)", payload_len));
446 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
447 (NULL), ("AMR RTP wrong interleaving"));
452 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
453 (NULL), ("AMR RTP frame size == -1"));
458 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
459 (NULL), ("AMR RTP wrong length 1"));
464 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
465 (NULL), ("AMR RTP wrong length 2"));
471 gst_rtp_buffer_unmap (&rtp);
477 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
479 return gst_element_register (plugin, "rtpamrdepay",
480 GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_DEPAY);