2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.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"
32 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
33 * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
34 * Wideband (AMR-WB) Audio Codecs.
37 /* elementfactory information */
38 static const GstElementDetails gst_rtp_amrdepay_details =
39 GST_ELEMENT_DETAILS ("RTP packet depayloader",
40 "Codec/Depayloader/Network",
41 "Extracts AMR audio from RTP packets (RFC 3267)",
42 "Wim Taymans <wim@fluendo.com>");
44 /* RtpAMRDepay signals and args */
56 /* input is an RTP packet
58 * params see RFC 3267, section 8.1
60 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("application/x-rtp, "
65 "media = (string) \"audio\", "
66 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
67 "clock-rate = (int) 8000, "
68 "encoding-name = (string) \"AMR\", "
69 "encoding-params = (string) \"1\", "
70 /* NOTE that all values must be strings in orde to be able to do SDP <->
72 "octet-align = (string) \"1\", "
73 "crc = (string) { \"0\", \"1\" }, "
74 "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
75 /* following options are not needed for a decoder
77 "mode-set = (int) [ 0, 7 ], "
78 "mode-change-period = (int) [ 1, MAX ], "
79 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
80 "maxptime = (int) [ 20, MAX ], "
81 "ptime = (int) [ 20, MAX ]"
86 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
87 GST_STATIC_PAD_TEMPLATE ("src",
90 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
93 static gboolean gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload,
95 static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
98 GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
99 GST_TYPE_BASE_RTP_DEPAYLOAD);
102 gst_rtp_amr_depay_base_init (gpointer klass)
104 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
108 gst_element_class_add_pad_template (element_class,
109 gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
111 gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
115 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
119 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
121 gobject_class = (GObjectClass *) klass;
122 gstelement_class = (GstElementClass *) klass;
123 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
125 parent_class = g_type_class_peek_parent (klass);
127 gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
128 gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
132 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay,
133 GstRtpAMRDepayClass * klass)
135 GstBaseRTPDepayload *depayload;
137 depayload = GST_BASE_RTP_DEPAYLOAD (rtpamrdepay);
139 depayload->clock_rate = 8000;
140 gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
144 gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
146 GstStructure *structure;
148 GstRtpAMRDepay *rtpamrdepay;
153 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
155 structure = gst_caps_get_structure (caps, 0);
157 if (!(str = gst_structure_get_string (structure, "octet-align")))
158 rtpamrdepay->octet_align = FALSE;
160 rtpamrdepay->octet_align = (atoi (str) == 1);
162 if (!(str = gst_structure_get_string (structure, "crc")))
163 rtpamrdepay->crc = FALSE;
165 rtpamrdepay->crc = (atoi (str) == 1);
167 if (rtpamrdepay->crc) {
168 /* crc mode implies octet aligned mode */
169 rtpamrdepay->octet_align = TRUE;
172 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
173 rtpamrdepay->robust_sorting = FALSE;
175 rtpamrdepay->robust_sorting = (atoi (str) == 1);
177 if (rtpamrdepay->robust_sorting) {
178 /* robust_sorting mode implies octet aligned mode */
179 rtpamrdepay->octet_align = TRUE;
182 if (!(str = gst_structure_get_string (structure, "interleaving")))
183 rtpamrdepay->interleaving = FALSE;
185 rtpamrdepay->interleaving = (atoi (str) == 1);
187 if (rtpamrdepay->interleaving) {
188 /* interleaving mode implies octet aligned mode */
189 rtpamrdepay->octet_align = TRUE;
192 if (!(params = gst_structure_get_string (structure, "encoding-params")))
193 rtpamrdepay->channels = 1;
195 rtpamrdepay->channels = atoi (params);
198 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
200 depayload->clock_rate = clock_rate;
202 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
203 * no robust sorting, no interleaving for now */
204 if (rtpamrdepay->channels != 1)
206 if (clock_rate != 8000)
208 if (rtpamrdepay->octet_align != TRUE)
210 if (rtpamrdepay->robust_sorting != FALSE)
212 if (rtpamrdepay->interleaving != FALSE)
215 srccaps = gst_caps_new_simple ("audio/AMR",
216 "channels", G_TYPE_INT, rtpamrdepay->channels,
217 "rate", G_TYPE_INT, clock_rate, NULL);
218 gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
219 gst_caps_unref (srccaps);
221 rtpamrdepay->negotiated = TRUE;
227 static gint frame_size[16] = {
228 12, 13, 15, 17, 19, 20, 26, 31,
229 5, -1, -1, -1, -1, -1, -1, 0
233 gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
235 GstRtpAMRDepay *rtpamrdepay;
236 GstBuffer *outbuf = NULL;
239 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
241 if (!rtpamrdepay->negotiated)
244 if (!gst_rtp_buffer_validate (buf))
247 /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC,
248 * no robust sorting, no interleaving data is to be depayloaded */
250 guint8 *payload, *p, *dp;
252 gint i, num_packets, num_nonempty_packets;
256 payload_len = gst_rtp_buffer_get_payload_len (buf);
258 /* need at least 2 bytes for the header */
262 payload = gst_rtp_buffer_get_payload (buf);
264 /* depay CMR. The CMR is used by the sender to request
265 * a new encoding mode.
272 CMR = (payload[0] & 0xf0) >> 4;
274 /* strip CMR header now, pack FT and the data for the decoder */
278 GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
280 if (rtpamrdepay->interleaving) {
281 ILL = (payload[0] & 0xf0) >> 4;
282 ILP = (payload[0] & 0x0f);
288 goto wrong_interleaving;
292 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
293 * +-+-+-+-+-+-+-+-+..
294 * |F| FT |Q|P|P| more FT..
295 * +-+-+-+-+-+-+-+-+..
297 /* count number of packets by counting the FTs. Also
298 * count number of amr data bytes and number of non-empty
299 * packets (this is also the number of CRCs if present). */
301 num_nonempty_packets = 0;
303 for (i = 0; i < payload_len; i++) {
307 FT = (payload[i] & 0x78) >> 3;
309 fr_size = frame_size[FT];
310 GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
312 goto wrong_framesize;
316 num_nonempty_packets++;
320 if ((payload[i] & 0x80) == 0)
324 if (rtpamrdepay->crc) {
325 /* data len + CRC len + header bytes should be smaller than payload_len */
326 if (num_packets + num_nonempty_packets + amr_len > payload_len)
329 /* data len + header bytes should be smaller than payload_len */
330 if (num_packets + amr_len > payload_len)
334 outbuf = gst_buffer_new_and_alloc (payload_len);
336 /* point to destination */
337 p = GST_BUFFER_DATA (outbuf);
338 /* point to first data packet */
339 dp = payload + num_packets;
340 if (rtpamrdepay->crc) {
341 /* skip CRC if present */
342 dp += num_nonempty_packets;
345 for (i = 0; i < num_packets; i++) {
348 /* copy FT, clear F bit */
349 *p++ = payload[i] & 0x7f;
351 fr_size = frame_size[(payload[i] & 0x78) >> 3];
353 /* copy data packet, FIXME, calc CRC here. */
354 memcpy (p, dp, fr_size);
360 gst_buffer_set_caps (outbuf,
361 GST_PAD_CAPS (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload)));
363 GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
364 GST_BUFFER_SIZE (outbuf));
371 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
372 (NULL), ("AMR RTP packet did not validate"));
377 GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
378 (NULL), ("not negotiated"));
383 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
384 (NULL), ("AMR RTP payload too small (%d)", payload_len));
389 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
390 (NULL), ("AMR RTP wrong interleaving"));
395 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
396 (NULL), ("AMR RTP frame size == -1"));
401 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
402 (NULL), ("AMR RTP wrong length 1"));
407 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
408 (NULL), ("AMR RTP wrong length 2"));
419 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
421 return gst_element_register (plugin, "rtpamrdepay",
422 GST_RANK_MARGINAL, GST_TYPE_RTP_AMR_DEPAY);