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 = {
34 "Codec/Depayr/Network",
35 "Extracts AMR audio from RTP packets (RFC 3267)",
36 "Wim Taymans <wim@fluendo.com>"
39 /* RtpAMRDepay signals and args */
52 /* input is an RTP packet
54 * params see RFC 3267, section 8.1
56 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("application/x-rtp, "
61 "media = (string) \"audio\", "
62 "payload = (int) [ 96, 255 ], "
63 "clock-rate = (int) 8000, "
64 "encoding-name = (string) \"AMR\", "
65 "encoding-params = (string) \"1\", "
66 "octet-align = (string) \"1\", "
67 "crc = (string) { \"0\", \"1\" }, "
68 "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
69 /* following options are not needed for a decoder
71 "mode-set = (int) [ 0, 7 ], "
72 "mode-change-period = (int) [ 1, MAX ], "
73 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
74 "maxptime = (int) [ 20, MAX ], "
75 "ptime = (int) [ 20, MAX ]"
80 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
84 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
87 static void gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass);
88 static void gst_rtp_amr_depay_base_init (GstRtpAMRDepayClass * klass);
89 static void gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay);
91 static gboolean gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps);
92 static GstFlowReturn gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buffer);
94 static void gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_rtp_amr_depay_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
99 static GstStateChangeReturn gst_rtp_amr_depay_change_state (GstElement *
100 element, GstStateChange transition);
102 static GstElementClass *parent_class = NULL;
105 gst_rtp_amr_depay_get_type (void)
107 static GType rtpamrdepay_type = 0;
109 if (!rtpamrdepay_type) {
110 static const GTypeInfo rtpamrdepay_info = {
111 sizeof (GstRtpAMRDepayClass),
112 (GBaseInitFunc) gst_rtp_amr_depay_base_init,
114 (GClassInitFunc) gst_rtp_amr_depay_class_init,
117 sizeof (GstRtpAMRDepay),
119 (GInstanceInitFunc) gst_rtp_amr_depay_init,
123 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpAMRDepay",
124 &rtpamrdepay_info, 0);
126 return rtpamrdepay_type;
130 gst_rtp_amr_depay_base_init (GstRtpAMRDepayClass * klass)
132 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
134 gst_element_class_add_pad_template (element_class,
135 gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
136 gst_element_class_add_pad_template (element_class,
137 gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
139 gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
143 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
148 gobject_class = (GObjectClass *) klass;
149 gstelement_class = (GstElementClass *) klass;
151 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
153 gobject_class->set_property = gst_rtp_amr_depay_set_property;
154 gobject_class->get_property = gst_rtp_amr_depay_get_property;
156 gstelement_class->change_state = gst_rtp_amr_depay_change_state;
160 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
162 rtpamrdepay->srcpad =
163 gst_pad_new_from_template (gst_static_pad_template_get
164 (&gst_rtp_amr_depay_src_template), "src");
166 gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->srcpad);
168 rtpamrdepay->sinkpad =
169 gst_pad_new_from_template (gst_static_pad_template_get
170 (&gst_rtp_amr_depay_sink_template), "sink");
171 gst_pad_set_setcaps_function (rtpamrdepay->sinkpad,
172 gst_rtp_amr_depay_sink_setcaps);
173 gst_pad_set_chain_function (rtpamrdepay->sinkpad, gst_rtp_amr_depay_chain);
174 gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->sinkpad);
178 gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps)
180 GstStructure *structure;
182 GstRtpAMRDepay *rtpamrdepay;
186 rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
188 structure = gst_caps_get_structure (caps, 0);
190 if (!(str = gst_structure_get_string (structure, "octet-align")))
191 rtpamrdepay->octet_align = FALSE;
193 rtpamrdepay->octet_align = (atoi (str) == 1);
195 if (!(str = gst_structure_get_string (structure, "crc")))
196 rtpamrdepay->crc = FALSE;
198 rtpamrdepay->crc = (atoi (str) == 1);
200 if (rtpamrdepay->crc) {
201 /* crc mode implies octet aligned mode */
202 rtpamrdepay->octet_align = TRUE;
205 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
206 rtpamrdepay->robust_sorting = FALSE;
208 rtpamrdepay->robust_sorting = (atoi (str) == 1);
210 if (rtpamrdepay->robust_sorting) {
211 /* robust_sorting mode implies octet aligned mode */
212 rtpamrdepay->octet_align = TRUE;
215 if (!(str = gst_structure_get_string (structure, "interleaving")))
216 rtpamrdepay->interleaving = FALSE;
218 rtpamrdepay->interleaving = (atoi (str) == 1);
220 if (rtpamrdepay->interleaving) {
221 /* interleaving mode implies octet aligned mode */
222 rtpamrdepay->octet_align = TRUE;
225 if (!(params = gst_structure_get_string (structure, "encoding-params")))
226 rtpamrdepay->channels = 1;
228 rtpamrdepay->channels = atoi (params);
231 if (!gst_structure_get_int (structure, "clock-rate", &rtpamrdepay->rate))
232 rtpamrdepay->rate = 8000;
234 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
235 * no robust sorting, no interleaving for now */
236 if (rtpamrdepay->channels != 1)
238 if (rtpamrdepay->rate != 8000)
240 if (rtpamrdepay->octet_align != TRUE)
242 if (rtpamrdepay->robust_sorting != FALSE)
244 if (rtpamrdepay->interleaving != FALSE)
247 srccaps = gst_caps_new_simple ("audio/AMR",
248 "channels", G_TYPE_INT, rtpamrdepay->channels,
249 "rate", G_TYPE_INT, rtpamrdepay->rate, NULL);
250 gst_pad_set_caps (rtpamrdepay->srcpad, srccaps);
251 gst_caps_unref (srccaps);
253 rtpamrdepay->negotiated = TRUE;
259 static gint frame_size[16] = {
260 12, 13, 15, 17, 19, 20, 26, 31,
261 5, -1, -1, -1, -1, -1, -1, 0
265 gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buf)
267 GstRtpAMRDepay *rtpamrdepay;
271 rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
273 if (!rtpamrdepay->negotiated)
276 if (!gst_rtp_buffer_validate (buf))
279 /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC,
280 * no robust sorting, no interleaving data is to be depayd */
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 */
296 payload = gst_rtp_buffer_get_payload (buf);
298 /* depay CMR. The CMR is used by the sender to request
299 * a new encoding mode.
306 CMR = (payload[0] & 0xf0) >> 4;
308 /* strip CMR header now, pack FT and the data for the decoder */
312 if (rtpamrdepay->interleaving) {
313 ILL = (payload[0] & 0xf0) >> 4;
314 ILP = (payload[0] & 0x0f);
324 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
325 * +-+-+-+-+-+-+-+-+..
326 * |F| FT |Q|P|P| more FT..
327 * +-+-+-+-+-+-+-+-+..
329 /* count number of packets by counting the FTs. Also
330 * count number of amr data bytes and number of non-empty
331 * packets (this is also the number of CRCs if present). */
333 num_nonempty_packets = 0;
335 for (i = 0; i < payload_len; i++) {
339 FT = (payload[i] & 0x78) >> 3;
341 fr_size = frame_size[FT];
347 num_nonempty_packets++;
351 if ((payload[i] & 0x80) == 0)
355 /* this is impossible */
356 if (num_packets == payload_len)
359 if (rtpamrdepay->crc) {
360 /* data len + CRC len + header bytes should be smaller than payload_len */
361 if (num_packets + num_nonempty_packets + amr_len > payload_len)
364 /* data len + header bytes should be smaller than payload_len */
365 if (num_packets + amr_len > payload_len)
369 timestamp = gst_rtp_buffer_get_timestamp (buf);
371 outbuf = gst_buffer_new_and_alloc (payload_len);
372 GST_BUFFER_TIMESTAMP (outbuf) = timestamp * GST_SECOND / rtpamrdepay->rate;
374 /* point to destination */
375 p = GST_BUFFER_DATA (outbuf);
376 /* point to first data packet */
377 dp = payload + num_packets;
378 if (rtpamrdepay->crc) {
379 /* skip CRC if present */
380 dp += num_nonempty_packets;
383 for (i = 0; i < num_packets; i++) {
386 fr_size = frame_size[(payload[i] & 0x78) >> 3];
390 /* copy data packet, FIXME, calc CRC here. */
391 memcpy (p, dp, fr_size);
397 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (rtpamrdepay->srcpad));
399 GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
400 GST_BUFFER_SIZE (outbuf));
401 ret = gst_pad_push (rtpamrdepay->srcpad, outbuf);
403 gst_buffer_unref (buf);
410 GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
411 ("not negotiated"), (NULL));
412 gst_buffer_unref (buf);
413 return GST_FLOW_NOT_NEGOTIATED;
417 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
418 ("amr packet did not validate"), (NULL));
419 gst_buffer_unref (buf);
425 gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
426 const GValue * value, GParamSpec * pspec)
428 GstRtpAMRDepay *rtpamrdepay;
430 rtpamrdepay = GST_RTP_AMR_DEPAY (object);
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440 gst_rtp_amr_depay_get_property (GObject * object, guint prop_id, GValue * value,
443 GstRtpAMRDepay *rtpamrdepay;
445 rtpamrdepay = GST_RTP_AMR_DEPAY (object);
449 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
454 static GstStateChangeReturn
455 gst_rtp_amr_depay_change_state (GstElement * element, GstStateChange transition)
457 GstRtpAMRDepay *rtpamrdepay;
458 GstStateChangeReturn ret;
460 rtpamrdepay = GST_RTP_AMR_DEPAY (element);
462 switch (transition) {
463 case GST_STATE_CHANGE_NULL_TO_READY:
465 case GST_STATE_CHANGE_READY_TO_PAUSED:
471 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
473 switch (transition) {
474 case GST_STATE_CHANGE_READY_TO_NULL:
483 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
485 return gst_element_register (plugin, "rtpamrdepay",
486 GST_RANK_NONE, GST_TYPE_RTP_AMR_DEPAY);