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>
27 #include "gstrtpamrdepay.h"
31 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
32 * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
33 * Wideband (AMR-WB) Audio Codecs.
36 /* elementfactory information */
37 static const GstElementDetails gst_rtp_amrdepay_details =
38 GST_ELEMENT_DETAILS ("RTP packet parser",
39 "Codec/Depayloader/Network",
40 "Extracts AMR audio from RTP packets (RFC 3267)",
41 "Wim Taymans <wim@fluendo.com>");
43 /* RtpAMRDepay signals and args */
55 /* input is an RTP packet
57 * params see RFC 3267, section 8.1
59 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("application/x-rtp, "
64 "media = (string) \"audio\", "
65 "clock-rate = (int) 8000, "
66 "encoding-name = (string) \"AMR\", "
67 "encoding-params = (string) \"1\", "
68 /* NOTE that all values must be strings in orde to be able to do SDP <->
70 "octet-align = (string) \"1\", "
71 "crc = (string) { \"0\", \"1\" }, "
72 "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
73 /* following options are not needed for a decoder
75 "mode-set = (int) [ 0, 7 ], "
76 "mode-change-period = (int) [ 1, MAX ], "
77 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
78 "maxptime = (int) [ 20, MAX ], "
79 "ptime = (int) [ 20, MAX ]"
84 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
85 GST_STATIC_PAD_TEMPLATE ("src",
88 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
91 static gboolean gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload,
93 static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
96 GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
97 GST_TYPE_BASE_RTP_DEPAYLOAD);
100 gst_rtp_amr_depay_base_init (gpointer klass)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
109 gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
113 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
115 GObjectClass *gobject_class;
116 GstElementClass *gstelement_class;
117 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
119 gobject_class = (GObjectClass *) klass;
120 gstelement_class = (GstElementClass *) klass;
121 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
123 parent_class = g_type_class_peek_parent (klass);
125 gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
126 gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
130 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay,
131 GstRtpAMRDepayClass * klass)
133 GstBaseRTPDepayload *depayload;
135 depayload = GST_BASE_RTP_DEPAYLOAD (rtpamrdepay);
137 depayload->clock_rate = 8000;
138 gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
142 gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
144 GstStructure *structure;
146 GstRtpAMRDepay *rtpamrdepay;
151 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
153 structure = gst_caps_get_structure (caps, 0);
155 if (!(str = gst_structure_get_string (structure, "octet-align")))
156 rtpamrdepay->octet_align = FALSE;
158 rtpamrdepay->octet_align = (atoi (str) == 1);
160 if (!(str = gst_structure_get_string (structure, "crc")))
161 rtpamrdepay->crc = FALSE;
163 rtpamrdepay->crc = (atoi (str) == 1);
165 if (rtpamrdepay->crc) {
166 /* crc mode implies octet aligned mode */
167 rtpamrdepay->octet_align = TRUE;
170 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
171 rtpamrdepay->robust_sorting = FALSE;
173 rtpamrdepay->robust_sorting = (atoi (str) == 1);
175 if (rtpamrdepay->robust_sorting) {
176 /* robust_sorting mode implies octet aligned mode */
177 rtpamrdepay->octet_align = TRUE;
180 if (!(str = gst_structure_get_string (structure, "interleaving")))
181 rtpamrdepay->interleaving = FALSE;
183 rtpamrdepay->interleaving = (atoi (str) == 1);
185 if (rtpamrdepay->interleaving) {
186 /* interleaving mode implies octet aligned mode */
187 rtpamrdepay->octet_align = TRUE;
190 if (!(params = gst_structure_get_string (structure, "encoding-params")))
191 rtpamrdepay->channels = 1;
193 rtpamrdepay->channels = atoi (params);
196 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
199 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
200 * no robust sorting, no interleaving for now */
201 if (rtpamrdepay->channels != 1)
203 if (clock_rate != 8000)
205 if (rtpamrdepay->octet_align != TRUE)
207 if (rtpamrdepay->robust_sorting != FALSE)
209 if (rtpamrdepay->interleaving != FALSE)
212 srccaps = gst_caps_new_simple ("audio/AMR",
213 "channels", G_TYPE_INT, rtpamrdepay->channels,
214 "rate", G_TYPE_INT, clock_rate, NULL);
215 gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
216 gst_caps_unref (srccaps);
218 rtpamrdepay->negotiated = TRUE;
224 static gint frame_size[16] = {
225 12, 13, 15, 17, 19, 20, 26, 31,
226 5, -1, -1, -1, -1, -1, -1, 0
230 gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
232 GstRtpAMRDepay *rtpamrdepay;
233 GstBuffer *outbuf = NULL;
235 rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
237 if (!rtpamrdepay->negotiated)
240 if (!gst_rtp_buffer_validate (buf)) {
241 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
242 (NULL), ("AMR RTP packet did not validate"));
246 /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC,
247 * no robust sorting, no interleaving data is to be depayloaded */
250 guint8 *payload, *p, *dp;
253 gint i, num_packets, num_nonempty_packets;
257 payload_len = gst_rtp_buffer_get_payload_len (buf);
259 /* need at least 2 bytes for the header */
260 if (payload_len < 2) {
261 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
262 (NULL), ("AMR RTP payload too small (%d)", payload_len));
266 payload = gst_rtp_buffer_get_payload (buf);
268 /* depay CMR. The CMR is used by the sender to request
269 * a new encoding mode.
276 CMR = (payload[0] & 0xf0) >> 4;
278 /* strip CMR header now, pack FT and the data for the decoder */
282 GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
284 if (rtpamrdepay->interleaving) {
285 ILL = (payload[0] & 0xf0) >> 4;
286 ILP = (payload[0] & 0x0f);
292 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
293 (NULL), ("AMR RTP wrong interleaving"));
299 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
300 * +-+-+-+-+-+-+-+-+..
301 * |F| FT |Q|P|P| more FT..
302 * +-+-+-+-+-+-+-+-+..
304 /* count number of packets by counting the FTs. Also
305 * count number of amr data bytes and number of non-empty
306 * packets (this is also the number of CRCs if present). */
308 num_nonempty_packets = 0;
310 for (i = 0; i < payload_len; i++) {
314 FT = (payload[i] & 0x78) >> 3;
316 fr_size = frame_size[FT];
317 GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
319 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
320 (NULL), ("AMR RTP frame size == -1"));
326 num_nonempty_packets++;
330 if ((payload[i] & 0x80) == 0)
334 if (rtpamrdepay->crc) {
335 /* data len + CRC len + header bytes should be smaller than payload_len */
336 if (num_packets + num_nonempty_packets + amr_len > payload_len) {
337 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
338 (NULL), ("AMR RTP wrong length 1"));
342 /* data len + header bytes should be smaller than payload_len */
343 if (num_packets + amr_len > payload_len) {
344 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
345 (NULL), ("AMR RTP wrong length 2"));
350 timestamp = gst_rtp_buffer_get_timestamp (buf);
352 outbuf = gst_buffer_new_and_alloc (payload_len);
353 GST_BUFFER_TIMESTAMP (outbuf) =
354 gst_util_uint64_scale_int (timestamp, GST_SECOND,
355 depayload->clock_rate);
357 /* point to destination */
358 p = GST_BUFFER_DATA (outbuf);
359 /* point to first data packet */
360 dp = payload + num_packets;
361 if (rtpamrdepay->crc) {
362 /* skip CRC if present */
363 dp += num_nonempty_packets;
366 for (i = 0; i < num_packets; i++) {
369 /* copy FT, clear F bit */
370 *p++ = payload[i] & 0x7f;
372 fr_size = frame_size[(payload[i] & 0x78) >> 3];
374 /* copy data packet, FIXME, calc CRC here. */
375 memcpy (p, dp, fr_size);
381 gst_buffer_set_caps (outbuf,
382 GST_PAD_CAPS (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload)));
384 GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
385 GST_BUFFER_SIZE (outbuf));
393 GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
394 (NULL), ("not negotiated"));
405 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
407 return gst_element_register (plugin, "rtpamrdepay",
408 GST_RANK_MARGINAL, GST_TYPE_RTP_AMR_DEPAY);