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
19 #include <gst/rtp/gstrtpbuffer.h>
22 #include "gstrtpamrdepay.h"
26 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File Storage Format
27 * for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate Wideband (AMR-WB) Audio
31 /* elementfactory information */
32 static GstElementDetails gst_rtp_amrdepay_details =
33 GST_ELEMENT_DETAILS ("RTP packet parser",
34 "Codec/Depayr/Network",
35 "Extracts AMR audio from RTP packets (RFC 3267)",
36 "Wim Taymans <wim@fluendo.com>");
38 /* RtpAMRDepay signals and args */
51 /* input is an RTP packet
53 * params see RFC 3267, section 8.1
55 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("application/x-rtp, "
60 "media = (string) \"audio\", "
61 "clock-rate = (int) 8000, "
62 "encoding-name = (string) \"AMR\", "
63 "encoding-params = (string) \"1\", "
64 "octet-align = (string) \"1\", "
65 "crc = (string) { \"0\", \"1\" }, "
66 "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
67 /* following options are not needed for a decoder
69 "mode-set = (int) [ 0, 7 ], "
70 "mode-change-period = (int) [ 1, MAX ], "
71 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
72 "maxptime = (int) [ 20, MAX ], "
73 "ptime = (int) [ 20, MAX ]"
78 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
79 GST_STATIC_PAD_TEMPLATE ("src",
82 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
85 static void gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass);
86 static void gst_rtp_amr_depay_base_init (GstRtpAMRDepayClass * klass);
87 static void gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay);
89 static gboolean gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps);
90 static GstFlowReturn gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buffer);
92 static void gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void gst_rtp_amr_depay_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
97 static GstStateChangeReturn gst_rtp_amr_depay_change_state (GstElement *
98 element, GstStateChange transition);
100 static GstElementClass *parent_class = NULL;
103 gst_rtp_amr_depay_get_type (void)
105 static GType rtpamrdepay_type = 0;
107 if (!rtpamrdepay_type) {
108 static const GTypeInfo rtpamrdepay_info = {
109 sizeof (GstRtpAMRDepayClass),
110 (GBaseInitFunc) gst_rtp_amr_depay_base_init,
112 (GClassInitFunc) gst_rtp_amr_depay_class_init,
115 sizeof (GstRtpAMRDepay),
117 (GInstanceInitFunc) gst_rtp_amr_depay_init,
121 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpAMRDepay",
122 &rtpamrdepay_info, 0);
124 return rtpamrdepay_type;
128 gst_rtp_amr_depay_base_init (GstRtpAMRDepayClass * klass)
130 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
132 gst_element_class_add_pad_template (element_class,
133 gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
134 gst_element_class_add_pad_template (element_class,
135 gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
137 gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
141 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
143 GObjectClass *gobject_class;
144 GstElementClass *gstelement_class;
146 gobject_class = (GObjectClass *) klass;
147 gstelement_class = (GstElementClass *) klass;
149 parent_class = g_type_class_peek_parent (klass);
151 gobject_class->set_property = gst_rtp_amr_depay_set_property;
152 gobject_class->get_property = gst_rtp_amr_depay_get_property;
154 gstelement_class->change_state = gst_rtp_amr_depay_change_state;
158 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
160 rtpamrdepay->srcpad =
161 gst_pad_new_from_static_template (&gst_rtp_amr_depay_src_template, "src");
163 gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->srcpad);
165 rtpamrdepay->sinkpad =
166 gst_pad_new_from_static_template (&gst_rtp_amr_depay_sink_template,
168 gst_pad_set_setcaps_function (rtpamrdepay->sinkpad,
169 gst_rtp_amr_depay_sink_setcaps);
170 gst_pad_set_chain_function (rtpamrdepay->sinkpad, gst_rtp_amr_depay_chain);
171 gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->sinkpad);
175 gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps)
177 GstStructure *structure;
179 GstRtpAMRDepay *rtpamrdepay;
183 rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
185 structure = gst_caps_get_structure (caps, 0);
187 if (!(str = gst_structure_get_string (structure, "octet-align")))
188 rtpamrdepay->octet_align = FALSE;
190 rtpamrdepay->octet_align = (atoi (str) == 1);
192 if (!(str = gst_structure_get_string (structure, "crc")))
193 rtpamrdepay->crc = FALSE;
195 rtpamrdepay->crc = (atoi (str) == 1);
197 if (rtpamrdepay->crc) {
198 /* crc mode implies octet aligned mode */
199 rtpamrdepay->octet_align = TRUE;
202 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
203 rtpamrdepay->robust_sorting = FALSE;
205 rtpamrdepay->robust_sorting = (atoi (str) == 1);
207 if (rtpamrdepay->robust_sorting) {
208 /* robust_sorting mode implies octet aligned mode */
209 rtpamrdepay->octet_align = TRUE;
212 if (!(str = gst_structure_get_string (structure, "interleaving")))
213 rtpamrdepay->interleaving = FALSE;
215 rtpamrdepay->interleaving = (atoi (str) == 1);
217 if (rtpamrdepay->interleaving) {
218 /* interleaving mode implies octet aligned mode */
219 rtpamrdepay->octet_align = TRUE;
222 if (!(params = gst_structure_get_string (structure, "encoding-params")))
223 rtpamrdepay->channels = 1;
225 rtpamrdepay->channels = atoi (params);
228 if (!gst_structure_get_int (structure, "clock-rate", &rtpamrdepay->rate))
229 rtpamrdepay->rate = 8000;
231 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
232 * no robust sorting, no interleaving for now */
233 if (rtpamrdepay->channels != 1)
235 if (rtpamrdepay->rate != 8000)
237 if (rtpamrdepay->octet_align != TRUE)
239 if (rtpamrdepay->robust_sorting != FALSE)
241 if (rtpamrdepay->interleaving != FALSE)
244 srccaps = gst_caps_new_simple ("audio/AMR",
245 "channels", G_TYPE_INT, rtpamrdepay->channels,
246 "rate", G_TYPE_INT, rtpamrdepay->rate, NULL);
247 gst_pad_set_caps (rtpamrdepay->srcpad, srccaps);
248 gst_caps_unref (srccaps);
250 rtpamrdepay->negotiated = TRUE;
256 static gint frame_size[16] = {
257 12, 13, 15, 17, 19, 20, 26, 31,
258 5, -1, -1, -1, -1, -1, -1, 0
262 gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buf)
264 GstRtpAMRDepay *rtpamrdepay;
268 rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
270 if (!rtpamrdepay->negotiated)
273 if (!gst_rtp_buffer_validate (buf)) {
274 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
275 (NULL), ("AMR RTP packet did not validate"));
279 /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC,
280 * no robust sorting, no interleaving data is to be depayloaded */
283 guint8 *payload, *p, *dp;
286 gint i, num_packets, num_nonempty_packets;
290 payload_len = gst_rtp_buffer_get_payload_len (buf);
292 /* need at least 2 bytes for the header */
293 if (payload_len < 2) {
294 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
295 (NULL), ("AMR RTP payload too small (%d)", payload_len));
299 payload = gst_rtp_buffer_get_payload (buf);
301 /* depay CMR. The CMR is used by the sender to request
302 * a new encoding mode.
309 CMR = (payload[0] & 0xf0) >> 4;
311 /* strip CMR header now, pack FT and the data for the decoder */
315 GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
317 if (rtpamrdepay->interleaving) {
318 ILL = (payload[0] & 0xf0) >> 4;
319 ILP = (payload[0] & 0x0f);
325 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
326 (NULL), ("AMR RTP wrong interleaving"));
332 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
333 * +-+-+-+-+-+-+-+-+..
334 * |F| FT |Q|P|P| more FT..
335 * +-+-+-+-+-+-+-+-+..
337 /* count number of packets by counting the FTs. Also
338 * count number of amr data bytes and number of non-empty
339 * packets (this is also the number of CRCs if present). */
341 num_nonempty_packets = 0;
343 for (i = 0; i < payload_len; i++) {
347 FT = (payload[i] & 0x78) >> 3;
349 fr_size = frame_size[FT];
350 GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
352 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
353 (NULL), ("AMR RTP frame size == -1"));
359 num_nonempty_packets++;
363 if ((payload[i] & 0x80) == 0)
367 if (rtpamrdepay->crc) {
368 /* data len + CRC len + header bytes should be smaller than payload_len */
369 if (num_packets + num_nonempty_packets + amr_len > payload_len) {
370 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
371 (NULL), ("AMR RTP wrong length 1"));
375 /* data len + header bytes should be smaller than payload_len */
376 if (num_packets + amr_len > payload_len) {
377 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
378 (NULL), ("AMR RTP wrong length 2"));
383 timestamp = gst_rtp_buffer_get_timestamp (buf);
385 outbuf = gst_buffer_new_and_alloc (payload_len);
386 GST_BUFFER_TIMESTAMP (outbuf) =
387 gst_util_uint64_scale_int (timestamp, GST_SECOND, rtpamrdepay->rate);
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 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (rtpamrdepay->srcpad));
415 GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
416 GST_BUFFER_SIZE (outbuf));
417 ret = gst_pad_push (rtpamrdepay->srcpad, outbuf);
419 gst_buffer_unref (buf);
427 GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
428 (NULL), ("not negotiated"));
429 gst_buffer_unref (buf);
430 return GST_FLOW_NOT_NEGOTIATED;
434 gst_buffer_unref (buf);
441 gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
442 const GValue * value, GParamSpec * pspec)
444 GstRtpAMRDepay *rtpamrdepay;
446 rtpamrdepay = GST_RTP_AMR_DEPAY (object);
450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
456 gst_rtp_amr_depay_get_property (GObject * object, guint prop_id, GValue * value,
459 GstRtpAMRDepay *rtpamrdepay;
461 rtpamrdepay = GST_RTP_AMR_DEPAY (object);
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
470 static GstStateChangeReturn
471 gst_rtp_amr_depay_change_state (GstElement * element, GstStateChange transition)
473 GstRtpAMRDepay *rtpamrdepay;
474 GstStateChangeReturn ret;
476 rtpamrdepay = GST_RTP_AMR_DEPAY (element);
478 switch (transition) {
479 case GST_STATE_CHANGE_NULL_TO_READY:
481 case GST_STATE_CHANGE_READY_TO_PAUSED:
487 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
489 switch (transition) {
490 case GST_STATE_CHANGE_READY_TO_NULL:
499 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
501 return gst_element_register (plugin, "rtpamrdepay",
502 GST_RANK_NONE, GST_TYPE_RTP_AMR_DEPAY);