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 "gstrtpamrdec.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_amrdec_details = {
34 "Codec/Parser/Network",
35 "Extracts AMR audio from RTP packets (RFC 3267)",
36 "Wim Taymans <wim@fluendo.com>"
39 /* RtpAMRDec signals and args */
52 /* input is an RTP packet
54 * params see RFC 3267, section 8.1
56 static GstStaticPadTemplate gst_rtpamrdec_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 = (boolean) TRUE, "
67 "crc = (boolean) FALSE, "
68 "robust-sorting = (boolean) FALSE, " "interleaving = (boolean) FALSE"
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_rtpamrdec_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
84 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
87 static void gst_rtpamrdec_class_init (GstRtpAMRDecClass * klass);
88 static void gst_rtpamrdec_base_init (GstRtpAMRDecClass * klass);
89 static void gst_rtpamrdec_init (GstRtpAMRDec * rtpamrdec);
91 static gboolean gst_rtpamrdec_sink_setcaps (GstPad * pad, GstCaps * caps);
92 static GstFlowReturn gst_rtpamrdec_chain (GstPad * pad, GstBuffer * buffer);
94 static void gst_rtpamrdec_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_rtpamrdec_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
99 static GstStateChangeReturn gst_rtpamrdec_change_state (GstElement * element,
100 GstStateChange transition);
102 static GstElementClass *parent_class = NULL;
105 gst_rtpamrdec_get_type (void)
107 static GType rtpamrdec_type = 0;
109 if (!rtpamrdec_type) {
110 static const GTypeInfo rtpamrdec_info = {
111 sizeof (GstRtpAMRDecClass),
112 (GBaseInitFunc) gst_rtpamrdec_base_init,
114 (GClassInitFunc) gst_rtpamrdec_class_init,
117 sizeof (GstRtpAMRDec),
119 (GInstanceInitFunc) gst_rtpamrdec_init,
123 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpAMRDec",
126 return rtpamrdec_type;
130 gst_rtpamrdec_base_init (GstRtpAMRDecClass * 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_rtpamrdec_src_template));
136 gst_element_class_add_pad_template (element_class,
137 gst_static_pad_template_get (&gst_rtpamrdec_sink_template));
139 gst_element_class_set_details (element_class, &gst_rtp_amrdec_details);
143 gst_rtpamrdec_class_init (GstRtpAMRDecClass * 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_rtpamrdec_set_property;
154 gobject_class->get_property = gst_rtpamrdec_get_property;
156 gstelement_class->change_state = gst_rtpamrdec_change_state;
160 gst_rtpamrdec_init (GstRtpAMRDec * rtpamrdec)
163 gst_pad_new_from_template (gst_static_pad_template_get
164 (&gst_rtpamrdec_src_template), "src");
166 gst_element_add_pad (GST_ELEMENT (rtpamrdec), rtpamrdec->srcpad);
169 gst_pad_new_from_template (gst_static_pad_template_get
170 (&gst_rtpamrdec_sink_template), "sink");
171 gst_pad_set_setcaps_function (rtpamrdec->sinkpad, gst_rtpamrdec_sink_setcaps);
172 gst_pad_set_chain_function (rtpamrdec->sinkpad, gst_rtpamrdec_chain);
173 gst_element_add_pad (GST_ELEMENT (rtpamrdec), rtpamrdec->sinkpad);
177 gst_rtpamrdec_sink_setcaps (GstPad * pad, GstCaps * caps)
179 GstStructure *structure;
181 GstRtpAMRDec *rtpamrdec;
184 rtpamrdec = GST_RTP_AMR_DEC (GST_OBJECT_PARENT (pad));
186 structure = gst_caps_get_structure (caps, 0);
188 if (!gst_structure_get_boolean (structure, "octet-align",
189 &rtpamrdec->octet_align))
190 rtpamrdec->octet_align = FALSE;
192 if (!gst_structure_get_boolean (structure, "crc", &rtpamrdec->crc))
193 rtpamrdec->crc = FALSE;
195 if (rtpamrdec->crc) {
196 /* crc mode implies octet aligned mode */
197 rtpamrdec->octet_align = TRUE;
200 if (!gst_structure_get_boolean (structure, "robust-sorting",
201 &rtpamrdec->robust_sorting))
202 rtpamrdec->robust_sorting = FALSE;
204 if (rtpamrdec->robust_sorting) {
205 /* robust_sorting mode implies octet aligned mode */
206 rtpamrdec->octet_align = TRUE;
209 if (!gst_structure_get_boolean (structure, "interleaving",
210 &rtpamrdec->interleaving))
211 rtpamrdec->interleaving = FALSE;
213 if (rtpamrdec->interleaving) {
214 /* interleaving mode implies octet aligned mode */
215 rtpamrdec->octet_align = TRUE;
218 if (!(params = gst_structure_get_string (structure, "encoding_params")))
219 rtpamrdec->channels = 1;
221 rtpamrdec->channels = atoi (params);
224 if (!gst_structure_get_int (structure, "clock_rate", &rtpamrdec->rate))
225 rtpamrdec->rate = 8000;
227 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
228 * no robust sorting, no interleaving for now */
229 if (rtpamrdec->channels != 1)
231 if (rtpamrdec->rate != 8000)
233 if (rtpamrdec->octet_align != TRUE)
235 if (rtpamrdec->crc != FALSE)
237 if (rtpamrdec->robust_sorting != FALSE)
239 if (rtpamrdec->interleaving != FALSE)
242 srccaps = gst_caps_new_simple ("audio/AMR",
243 "channels", G_TYPE_INT, rtpamrdec->channels,
244 "rate", G_TYPE_INT, rtpamrdec->rate, NULL);
245 gst_pad_set_caps (rtpamrdec->srcpad, srccaps);
246 gst_caps_unref (srccaps);
248 rtpamrdec->negotiated = TRUE;
254 gst_rtpamrdec_chain (GstPad * pad, GstBuffer * buf)
256 GstRtpAMRDec *rtpamrdec;
260 rtpamrdec = GST_RTP_AMR_DEC (GST_OBJECT_PARENT (pad));
262 if (!rtpamrdec->negotiated)
265 if (!gst_rtpbuffer_validate (buf))
268 /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC,
269 * no robust sorting, no interleaving data is to be parsed */
274 guint8 CMR, F, FT, Q;
276 payload_len = gst_rtpbuffer_get_payload_len (buf);
278 /* need at least 2 bytes for the header */
282 payload = gst_rtpbuffer_get_payload (buf);
285 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
286 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+..
287 * | CMR |R|R|R|R|F| FT |Q|P|P|
288 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+..
290 CMR = (payload[0] & 0xf0) >> 4;
291 F = (payload[1] & 0x80) >> 7;
292 /* we only support 1 packet per RTP packet for now */
294 goto one_packet_only;
296 FT = (payload[1] & 0x78) >> 3;
297 Q = (payload[1] & 0x04) >> 2;
300 if (FT > 9 && FT < 15) {
305 /* strip header now, leave FT in the data for the decoder */
309 timestamp = gst_rtpbuffer_get_timestamp (buf);
311 outbuf = gst_buffer_new_and_alloc (payload_len);
313 GST_BUFFER_TIMESTAMP (outbuf) = timestamp * GST_SECOND / rtpamrdec->rate;
315 memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
317 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (rtpamrdec->srcpad));
319 GST_DEBUG ("gst_rtpamrdec_chain: pushing buffer of size %d",
320 GST_BUFFER_SIZE (outbuf));
321 ret = gst_pad_push (rtpamrdec->srcpad, outbuf);
324 gst_buffer_unref (buf);
331 GST_DEBUG ("not_negotiated");
332 gst_buffer_unref (buf);
333 return GST_FLOW_NOT_NEGOTIATED;
337 GST_DEBUG ("Packet did not validate");
338 gst_buffer_unref (buf);
339 return GST_FLOW_ERROR;
343 GST_DEBUG ("One packet per RTP packet only");
344 gst_buffer_unref (buf);
345 return GST_FLOW_ERROR;
350 gst_rtpamrdec_set_property (GObject * object, guint prop_id,
351 const GValue * value, GParamSpec * pspec)
353 GstRtpAMRDec *rtpamrdec;
355 rtpamrdec = GST_RTP_AMR_DEC (object);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gst_rtpamrdec_get_property (GObject * object, guint prop_id, GValue * value,
368 GstRtpAMRDec *rtpamrdec;
370 rtpamrdec = GST_RTP_AMR_DEC (object);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
379 static GstStateChangeReturn
380 gst_rtpamrdec_change_state (GstElement * element, GstStateChange transition)
382 GstRtpAMRDec *rtpamrdec;
383 GstStateChangeReturn ret;
385 rtpamrdec = GST_RTP_AMR_DEC (element);
387 switch (transition) {
388 case GST_STATE_CHANGE_NULL_TO_READY:
390 case GST_STATE_CHANGE_READY_TO_PAUSED:
396 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
398 switch (transition) {
399 case GST_STATE_CHANGE_READY_TO_NULL:
408 gst_rtpamrdec_plugin_init (GstPlugin * plugin)
410 return gst_element_register (plugin, "rtpamrdec",
411 GST_RANK_NONE, GST_TYPE_RTP_AMR_DEC);