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
23 * @see_also: rtpamrpay
25 * Extract AMR audio from RTP packets according to RFC 3267.
26 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3267.txt
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.
37 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
38 * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
39 * Wideband (AMR-WB) Audio Codecs.
46 #include <gst/rtp/gstrtpbuffer.h>
47 #include <gst/audio/audio.h>
51 #include "gstrtpelements.h"
52 #include "gstrtpamrdepay.h"
53 #include "gstrtputils.h"
55 GST_DEBUG_CATEGORY_STATIC (rtpamrdepay_debug);
56 #define GST_CAT_DEFAULT (rtpamrdepay_debug)
58 /* RtpAMRDepay signals and args */
70 /* input is an RTP packet
72 * params see RFC 3267, section 8.1
74 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
75 GST_STATIC_PAD_TEMPLATE ("sink",
78 GST_STATIC_CAPS ("application/x-rtp, "
79 "media = (string) \"audio\", "
80 "clock-rate = (int) 8000, " "encoding-name = (string) \"AMR\", "
81 /* This is the default, so the peer doesn't have to specify it
82 * "encoding-params = (string) \"1\", " */
83 /* NOTE that all values must be strings in orde to be able to do SDP <->
85 "octet-align = (string) \"1\";"
86 /* following options are not needed for a decoder
88 "crc = (string) { \"0\", \"1\" }, "
89 "robust-sorting = (string) \"0\", "
90 "interleaving = (string) \"0\";"
91 "mode-set = (int) [ 0, 7 ], "
92 "mode-change-period = (int) [ 1, MAX ], "
93 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
94 "maxptime = (int) [ 20, MAX ], "
95 "ptime = (int) [ 20, MAX ]"
98 "media = (string) \"audio\", "
99 "clock-rate = (int) 16000, " "encoding-name = (string) \"AMR-WB\", "
100 /* This is the default, so the peer doesn't have to specify it
101 * "encoding-params = (string) \"1\", " */
102 /* NOTE that all values must be strings in orde to be able to do SDP <->
103 * GstCaps mapping. */
104 "octet-align = (string) \"1\";"
105 /* following options are not needed for a decoder
107 "crc = (string) { \"0\", \"1\" }, "
108 "robust-sorting = (string) \"0\", "
109 "interleaving = (string) \"0\""
110 "mode-set = (int) [ 0, 7 ], "
111 "mode-change-period = (int) [ 1, MAX ], "
112 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
113 "maxptime = (int) [ 20, MAX ], "
114 "ptime = (int) [ 20, MAX ]"
119 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
120 GST_STATIC_PAD_TEMPLATE ("src",
123 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000;"
124 "audio/AMR-WB, " "channels = (int) 1," "rate = (int) 16000")
127 static gboolean gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload,
129 static GstBuffer *gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload,
132 #define gst_rtp_amr_depay_parent_class parent_class
133 G_DEFINE_TYPE (GstRtpAMRDepay, gst_rtp_amr_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
134 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpamrdepay, "rtpamrdepay",
135 GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_DEPAY, rtp_element_init (plugin));
138 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
140 GstElementClass *gstelement_class;
141 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
143 gstelement_class = (GstElementClass *) klass;
144 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
146 gst_element_class_add_static_pad_template (gstelement_class,
147 &gst_rtp_amr_depay_src_template);
148 gst_element_class_add_static_pad_template (gstelement_class,
149 &gst_rtp_amr_depay_sink_template);
151 gst_element_class_set_static_metadata (gstelement_class,
152 "RTP AMR depayloader", "Codec/Depayloader/Network/RTP",
153 "Extracts AMR or AMR-WB audio from RTP packets (RFC 3267)",
154 "Wim Taymans <wim.taymans@gmail.com>");
156 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_amr_depay_process;
157 gstrtpbasedepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
159 GST_DEBUG_CATEGORY_INIT (rtpamrdepay_debug, "rtpamrdepay", 0,
160 "AMR/AMR-WB RTP Depayloader");
164 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
166 GstRTPBaseDepayload *depayload;
168 depayload = GST_RTP_BASE_DEPAYLOAD (rtpamrdepay);
170 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
174 gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
176 GstStructure *structure;
178 GstRtpAMRDepay *rtpamrdepay;
180 const gchar *str, *type;
181 gint clock_rate, need_clock_rate;
184 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
186 structure = gst_caps_get_structure (caps, 0);
188 /* figure out the mode first and set the clock rates */
189 if ((str = gst_structure_get_string (structure, "encoding-name"))) {
190 if (strcmp (str, "AMR") == 0) {
191 rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
192 need_clock_rate = 8000;
194 } else if (strcmp (str, "AMR-WB") == 0) {
195 rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
196 need_clock_rate = 16000;
197 type = "audio/AMR-WB";
203 if (!(str = gst_structure_get_string (structure, "octet-align")))
204 rtpamrdepay->octet_align = FALSE;
206 rtpamrdepay->octet_align = (atoi (str) == 1);
208 if (!(str = gst_structure_get_string (structure, "crc")))
209 rtpamrdepay->crc = FALSE;
211 rtpamrdepay->crc = (atoi (str) == 1);
213 if (rtpamrdepay->crc) {
214 /* crc mode implies octet aligned mode */
215 rtpamrdepay->octet_align = TRUE;
218 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
219 rtpamrdepay->robust_sorting = FALSE;
221 rtpamrdepay->robust_sorting = (atoi (str) == 1);
223 if (rtpamrdepay->robust_sorting) {
224 /* robust_sorting mode implies octet aligned mode */
225 rtpamrdepay->octet_align = TRUE;
228 if (!(str = gst_structure_get_string (structure, "interleaving")))
229 rtpamrdepay->interleaving = FALSE;
231 rtpamrdepay->interleaving = (atoi (str) == 1);
233 if (rtpamrdepay->interleaving) {
234 /* interleaving mode implies octet aligned mode */
235 rtpamrdepay->octet_align = TRUE;
238 if (!(params = gst_structure_get_string (structure, "encoding-params")))
239 rtpamrdepay->channels = 1;
241 rtpamrdepay->channels = atoi (params);
244 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
245 clock_rate = need_clock_rate;
246 depayload->clock_rate = clock_rate;
248 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
249 * no robust sorting, no interleaving for now */
250 if (rtpamrdepay->channels != 1)
252 if (clock_rate != need_clock_rate)
254 if (rtpamrdepay->octet_align != TRUE)
256 if (rtpamrdepay->robust_sorting != FALSE)
258 if (rtpamrdepay->interleaving != FALSE)
261 srccaps = gst_caps_new_simple (type,
262 "channels", G_TYPE_INT, rtpamrdepay->channels,
263 "rate", G_TYPE_INT, clock_rate, NULL);
264 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
265 gst_caps_unref (srccaps);
272 GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
278 static const gint nb_frame_size[16] = {
279 12, 13, 15, 17, 19, 20, 26, 31,
280 5, -1, -1, -1, -1, -1, -1, 0
283 static const gint wb_frame_size[16] = {
284 17, 23, 32, 36, 40, 46, 50, 58,
285 60, 5, -1, -1, -1, -1, -1, 0
289 gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
291 GstRtpAMRDepay *rtpamrdepay;
292 const gint *frame_size;
293 GstBuffer *outbuf = NULL;
297 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
299 /* setup frame size pointer */
300 if (rtpamrdepay->mode == GST_RTP_AMR_DP_MODE_NB)
301 frame_size = nb_frame_size;
303 frame_size = wb_frame_size;
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));
433 gst_rtp_copy_audio_meta (rtpamrdepay, outbuf, rtp->buffer);
441 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
442 (NULL), ("AMR RTP payload too small (%d)", payload_len));
447 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
448 (NULL), ("AMR RTP wrong interleaving"));
453 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
454 (NULL), ("AMR RTP frame size == -1"));
459 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
460 (NULL), ("AMR RTP wrong length 1"));
465 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
466 (NULL), ("AMR RTP wrong length 2"));