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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpamrdepay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpamrdepay_debug);
31 #define GST_CAT_DEFAULT (rtpamrdepay_debug)
35 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
36 * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
37 * Wideband (AMR-WB) Audio Codecs.
40 /* elementfactory information */
41 static const GstElementDetails gst_rtp_amrdepay_details =
42 GST_ELEMENT_DETAILS ("RTP AMR depayloader",
43 "Codec/Depayloader/Network",
44 "Extracts AMR or AMR-WB audio from RTP packets (RFC 3267)",
45 "Wim Taymans <wim.taymans@gmail.com>");
47 /* RtpAMRDepay signals and args */
59 /* input is an RTP packet
61 * params see RFC 3267, section 8.1
63 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_STATIC_CAPS ("application/x-rtp, "
68 "media = (string) \"audio\", "
69 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
70 "clock-rate = (int) 8000, "
71 "encoding-name = (string) \"AMR\", "
72 "encoding-params = (string) \"1\", "
73 /* NOTE that all values must be strings in orde to be able to do SDP <->
75 "octet-align = (string) \"1\", "
76 "crc = (string) { \"0\", \"1\" }, "
77 "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\";"
78 /* following options are not needed for a decoder
80 "mode-set = (int) [ 0, 7 ], "
81 "mode-change-period = (int) [ 1, MAX ], "
82 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
83 "maxptime = (int) [ 20, MAX ], "
84 "ptime = (int) [ 20, MAX ]"
87 "media = (string) \"audio\", "
88 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
89 "clock-rate = (int) 16000, "
90 "encoding-name = (string) \"AMR-WB\", "
91 "encoding-params = (string) \"1\", "
92 /* NOTE that all values must be strings in orde to be able to do SDP <->
94 "octet-align = (string) \"1\", "
95 "crc = (string) { \"0\", \"1\" }, "
96 "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
97 /* following options are not needed for a decoder
99 "mode-set = (int) [ 0, 7 ], "
100 "mode-change-period = (int) [ 1, MAX ], "
101 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
102 "maxptime = (int) [ 20, MAX ], "
103 "ptime = (int) [ 20, MAX ]"
108 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
109 GST_STATIC_PAD_TEMPLATE ("src",
112 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000;"
113 "audio/AMR-WB, " "channels = (int) 1," "rate = (int) 16000")
116 static gboolean gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload,
118 static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
121 GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
122 GST_TYPE_BASE_RTP_DEPAYLOAD);
125 gst_rtp_amr_depay_base_init (gpointer klass)
127 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
129 gst_element_class_add_pad_template (element_class,
130 gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
131 gst_element_class_add_pad_template (element_class,
132 gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
134 gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
138 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
140 GObjectClass *gobject_class;
141 GstElementClass *gstelement_class;
142 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
144 gobject_class = (GObjectClass *) klass;
145 gstelement_class = (GstElementClass *) klass;
146 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
148 parent_class = g_type_class_peek_parent (klass);
150 gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
151 gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
153 GST_DEBUG_CATEGORY_INIT (rtpamrdepay_debug, "rtpamrdepay", 0,
154 "AMR/AMR-WB RTP Depayloader");
158 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay,
159 GstRtpAMRDepayClass * klass)
161 GstBaseRTPDepayload *depayload;
163 depayload = GST_BASE_RTP_DEPAYLOAD (rtpamrdepay);
165 gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
169 gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
171 GstStructure *structure;
173 GstRtpAMRDepay *rtpamrdepay;
175 const gchar *str, *type;
176 gint clock_rate, need_clock_rate;
179 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
181 structure = gst_caps_get_structure (caps, 0);
183 /* figure out the mode first and set the clock rates */
184 if ((str = gst_structure_get_string (structure, "encoding-name"))) {
185 if (strcmp (str, "AMR") == 0) {
186 rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
187 need_clock_rate = 8000;
189 } else if (strcmp (str, "AMR-WB") == 0) {
190 rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
191 need_clock_rate = 16000;
192 type = "audio/AMR-WB";
198 if (!(str = gst_structure_get_string (structure, "octet-align")))
199 rtpamrdepay->octet_align = FALSE;
201 rtpamrdepay->octet_align = (atoi (str) == 1);
203 if (!(str = gst_structure_get_string (structure, "crc")))
204 rtpamrdepay->crc = FALSE;
206 rtpamrdepay->crc = (atoi (str) == 1);
208 if (rtpamrdepay->crc) {
209 /* crc mode implies octet aligned mode */
210 rtpamrdepay->octet_align = TRUE;
213 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
214 rtpamrdepay->robust_sorting = FALSE;
216 rtpamrdepay->robust_sorting = (atoi (str) == 1);
218 if (rtpamrdepay->robust_sorting) {
219 /* robust_sorting mode implies octet aligned mode */
220 rtpamrdepay->octet_align = TRUE;
223 if (!(str = gst_structure_get_string (structure, "interleaving")))
224 rtpamrdepay->interleaving = FALSE;
226 rtpamrdepay->interleaving = (atoi (str) == 1);
228 if (rtpamrdepay->interleaving) {
229 /* interleaving mode implies octet aligned mode */
230 rtpamrdepay->octet_align = TRUE;
233 if (!(params = gst_structure_get_string (structure, "encoding-params")))
234 rtpamrdepay->channels = 1;
236 rtpamrdepay->channels = atoi (params);
239 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
240 clock_rate = need_clock_rate;
241 depayload->clock_rate = clock_rate;
243 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
244 * no robust sorting, no interleaving for now */
245 if (rtpamrdepay->channels != 1)
247 if (clock_rate != need_clock_rate)
249 if (rtpamrdepay->octet_align != TRUE)
251 if (rtpamrdepay->robust_sorting != FALSE)
253 if (rtpamrdepay->interleaving != FALSE)
256 srccaps = gst_caps_new_simple (type,
257 "channels", G_TYPE_INT, rtpamrdepay->channels,
258 "rate", G_TYPE_INT, clock_rate, NULL);
259 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
260 gst_caps_unref (srccaps);
267 GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
273 static gint nb_frame_size[16] = {
274 12, 13, 15, 17, 19, 20, 26, 31,
275 5, -1, -1, -1, -1, -1, -1, 0
277 static gint wb_frame_size[16] = {
278 17, 23, 32, 36, 40, 46, 50, 58,
279 60, -1, -1, -1, -1, -1, -1, 0
283 gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
285 GstRtpAMRDepay *rtpamrdepay;
286 GstBuffer *outbuf = NULL;
290 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
292 /* setup frame size pointer */
293 if (rtpamrdepay->mode == GST_RTP_AMR_DP_MODE_NB)
294 frame_size = nb_frame_size;
296 frame_size = wb_frame_size;
298 /* when we get here, 1 channel, 8000/16000 Hz, octet aligned, no CRC,
299 * no robust sorting, no interleaving data is to be depayloaded */
301 guint8 *payload, *p, *dp;
303 gint i, num_packets, num_nonempty_packets;
308 marker = gst_rtp_buffer_get_marker (buf);
309 payload_len = gst_rtp_buffer_get_payload_len (buf);
311 /* need at least 2 bytes for the header */
315 payload = gst_rtp_buffer_get_payload (buf);
317 /* depay CMR. The CMR is used by the sender to request
318 * a new encoding mode.
325 CMR = (payload[0] & 0xf0) >> 4;
327 /* strip CMR header now, pack FT and the data for the decoder */
331 GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
333 if (rtpamrdepay->interleaving) {
334 ILL = (payload[0] & 0xf0) >> 4;
335 ILP = (payload[0] & 0x0f);
341 goto wrong_interleaving;
345 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
346 * +-+-+-+-+-+-+-+-+..
347 * |F| FT |Q|P|P| more FT..
348 * +-+-+-+-+-+-+-+-+..
350 /* count number of packets by counting the FTs. Also
351 * count number of amr data bytes and number of non-empty
352 * packets (this is also the number of CRCs if present). */
354 num_nonempty_packets = 0;
356 for (i = 0; i < payload_len; i++) {
360 FT = (payload[i] & 0x78) >> 3;
362 fr_size = frame_size[FT];
363 GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
365 goto wrong_framesize;
369 num_nonempty_packets++;
373 if ((payload[i] & 0x80) == 0)
377 if (rtpamrdepay->crc) {
378 /* data len + CRC len + header bytes should be smaller than payload_len */
379 if (num_packets + num_nonempty_packets + amr_len > payload_len)
382 /* data len + header bytes should be smaller than payload_len */
383 if (num_packets + amr_len > payload_len)
387 outbuf = gst_buffer_new_and_alloc (payload_len);
389 /* point to destination */
390 p = GST_BUFFER_DATA (outbuf);
391 /* point to first data packet */
392 dp = payload + num_packets;
393 if (rtpamrdepay->crc) {
394 /* skip CRC if present */
395 dp += num_nonempty_packets;
398 for (i = 0; i < num_packets; i++) {
401 /* copy FT, clear F bit */
402 *p++ = payload[i] & 0x7f;
404 fr_size = frame_size[(payload[i] & 0x78) >> 3];
406 /* copy data packet, FIXME, calc CRC here. */
407 memcpy (p, dp, fr_size);
413 /* we can set the duration because each packet is 20 milliseconds */
414 GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
417 /* marker bit marks a discont buffer after a talkspurt. */
418 GST_DEBUG_OBJECT (depayload, "marker bit was set");
419 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
423 GST_DEBUG_OBJECT (depayload, "pushing buffer of size %d",
424 GST_BUFFER_SIZE (outbuf));
431 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
432 (NULL), ("AMR RTP payload too small (%d)", payload_len));
437 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
438 (NULL), ("AMR RTP wrong interleaving"));
443 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
444 (NULL), ("AMR RTP frame size == -1"));
449 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
450 (NULL), ("AMR RTP wrong length 1"));
455 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
456 (NULL), ("AMR RTP wrong length 2"));
467 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
469 return gst_element_register (plugin, "rtpamrdepay",
470 GST_RANK_MARGINAL, GST_TYPE_RTP_AMR_DEPAY);