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 "clock-rate = (int) 8000, "
63 "encoding-name = (string) \"AMR\", "
64 "encoding-params = (string) \"1\", "
65 "octet-align = (string) \"1\", "
66 "crc = (string) { \"0\", \"1\" }, "
67 "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
68 /* following options are not needed for a decoder
70 "mode-set = (int) [ 0, 7 ], "
71 "mode-change-period = (int) [ 1, MAX ], "
72 "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
73 "maxptime = (int) [ 20, MAX ], "
74 "ptime = (int) [ 20, MAX ]"
79 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
83 GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
86 static void gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass);
87 static void gst_rtp_amr_depay_base_init (GstRtpAMRDepayClass * klass);
88 static void gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay);
90 static gboolean gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps);
91 static GstFlowReturn gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buffer);
93 static void gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_rtp_amr_depay_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
98 static GstStateChangeReturn gst_rtp_amr_depay_change_state (GstElement *
99 element, GstStateChange transition);
101 static GstElementClass *parent_class = NULL;
104 gst_rtp_amr_depay_get_type (void)
106 static GType rtpamrdepay_type = 0;
108 if (!rtpamrdepay_type) {
109 static const GTypeInfo rtpamrdepay_info = {
110 sizeof (GstRtpAMRDepayClass),
111 (GBaseInitFunc) gst_rtp_amr_depay_base_init,
113 (GClassInitFunc) gst_rtp_amr_depay_class_init,
116 sizeof (GstRtpAMRDepay),
118 (GInstanceInitFunc) gst_rtp_amr_depay_init,
122 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpAMRDepay",
123 &rtpamrdepay_info, 0);
125 return rtpamrdepay_type;
129 gst_rtp_amr_depay_base_init (GstRtpAMRDepayClass * klass)
131 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133 gst_element_class_add_pad_template (element_class,
134 gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
135 gst_element_class_add_pad_template (element_class,
136 gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
138 gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
142 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
144 GObjectClass *gobject_class;
145 GstElementClass *gstelement_class;
147 gobject_class = (GObjectClass *) klass;
148 gstelement_class = (GstElementClass *) klass;
150 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
152 gobject_class->set_property = gst_rtp_amr_depay_set_property;
153 gobject_class->get_property = gst_rtp_amr_depay_get_property;
155 gstelement_class->change_state = gst_rtp_amr_depay_change_state;
159 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
161 rtpamrdepay->srcpad =
162 gst_pad_new_from_template (gst_static_pad_template_get
163 (&gst_rtp_amr_depay_src_template), "src");
165 gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->srcpad);
167 rtpamrdepay->sinkpad =
168 gst_pad_new_from_template (gst_static_pad_template_get
169 (&gst_rtp_amr_depay_sink_template), "sink");
170 gst_pad_set_setcaps_function (rtpamrdepay->sinkpad,
171 gst_rtp_amr_depay_sink_setcaps);
172 gst_pad_set_chain_function (rtpamrdepay->sinkpad, gst_rtp_amr_depay_chain);
173 gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->sinkpad);
177 gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps)
179 GstStructure *structure;
181 GstRtpAMRDepay *rtpamrdepay;
185 rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
187 structure = gst_caps_get_structure (caps, 0);
189 if (!(str = gst_structure_get_string (structure, "octet-align")))
190 rtpamrdepay->octet_align = FALSE;
192 rtpamrdepay->octet_align = (atoi (str) == 1);
194 if (!(str = gst_structure_get_string (structure, "crc")))
195 rtpamrdepay->crc = FALSE;
197 rtpamrdepay->crc = (atoi (str) == 1);
199 if (rtpamrdepay->crc) {
200 /* crc mode implies octet aligned mode */
201 rtpamrdepay->octet_align = TRUE;
204 if (!(str = gst_structure_get_string (structure, "robust-sorting")))
205 rtpamrdepay->robust_sorting = FALSE;
207 rtpamrdepay->robust_sorting = (atoi (str) == 1);
209 if (rtpamrdepay->robust_sorting) {
210 /* robust_sorting mode implies octet aligned mode */
211 rtpamrdepay->octet_align = TRUE;
214 if (!(str = gst_structure_get_string (structure, "interleaving")))
215 rtpamrdepay->interleaving = FALSE;
217 rtpamrdepay->interleaving = (atoi (str) == 1);
219 if (rtpamrdepay->interleaving) {
220 /* interleaving mode implies octet aligned mode */
221 rtpamrdepay->octet_align = TRUE;
224 if (!(params = gst_structure_get_string (structure, "encoding-params")))
225 rtpamrdepay->channels = 1;
227 rtpamrdepay->channels = atoi (params);
230 if (!gst_structure_get_int (structure, "clock-rate", &rtpamrdepay->rate))
231 rtpamrdepay->rate = 8000;
233 /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
234 * no robust sorting, no interleaving for now */
235 if (rtpamrdepay->channels != 1)
237 if (rtpamrdepay->rate != 8000)
239 if (rtpamrdepay->octet_align != TRUE)
241 if (rtpamrdepay->robust_sorting != FALSE)
243 if (rtpamrdepay->interleaving != FALSE)
246 srccaps = gst_caps_new_simple ("audio/AMR",
247 "channels", G_TYPE_INT, rtpamrdepay->channels,
248 "rate", G_TYPE_INT, rtpamrdepay->rate, NULL);
249 gst_pad_set_caps (rtpamrdepay->srcpad, srccaps);
250 gst_caps_unref (srccaps);
252 rtpamrdepay->negotiated = TRUE;
258 static gint frame_size[16] = {
259 12, 13, 15, 17, 19, 20, 26, 31,
260 5, -1, -1, -1, -1, -1, -1, 0
264 gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buf)
266 GstRtpAMRDepay *rtpamrdepay;
270 rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
272 if (!rtpamrdepay->negotiated)
275 if (!gst_rtp_buffer_validate (buf)) {
276 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
277 (NULL), ("AMR RTP packet did not validate"));
281 /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC,
282 * no robust sorting, no interleaving data is to be depayloaded */
285 guint8 *payload, *p, *dp;
288 gint i, num_packets, num_nonempty_packets;
292 payload_len = gst_rtp_buffer_get_payload_len (buf);
294 /* need at least 2 bytes for the header */
295 if (payload_len < 2) {
296 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
297 (NULL), ("AMR RTP payload too small (%d)", payload_len));
301 payload = gst_rtp_buffer_get_payload (buf);
303 /* depay CMR. The CMR is used by the sender to request
304 * a new encoding mode.
311 CMR = (payload[0] & 0xf0) >> 4;
313 /* strip CMR header now, pack FT and the data for the decoder */
317 GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
319 if (rtpamrdepay->interleaving) {
320 ILL = (payload[0] & 0xf0) >> 4;
321 ILP = (payload[0] & 0x0f);
327 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
328 (NULL), ("AMR RTP wrong interleaving"));
334 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
335 * +-+-+-+-+-+-+-+-+..
336 * |F| FT |Q|P|P| more FT..
337 * +-+-+-+-+-+-+-+-+..
339 /* count number of packets by counting the FTs. Also
340 * count number of amr data bytes and number of non-empty
341 * packets (this is also the number of CRCs if present). */
343 num_nonempty_packets = 0;
345 for (i = 0; i < payload_len; i++) {
349 FT = (payload[i] & 0x78) >> 3;
351 fr_size = frame_size[FT];
352 GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
354 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
355 (NULL), ("AMR RTP frame size == -1"));
361 num_nonempty_packets++;
365 if ((payload[i] & 0x80) == 0)
369 /* this is impossible */
370 if (num_packets == payload_len) {
371 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
372 (NULL), ("AMR RTP num_packets == payload_len"));
376 if (rtpamrdepay->crc) {
377 /* data len + CRC len + header bytes should be smaller than payload_len */
378 if (num_packets + num_nonempty_packets + amr_len > payload_len) {
379 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
380 (NULL), ("AMR RTP wrong length 1"));
384 /* data len + header bytes should be smaller than payload_len */
385 if (num_packets + amr_len > payload_len) {
386 GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
387 (NULL), ("AMR RTP wrong length 2"));
392 timestamp = gst_rtp_buffer_get_timestamp (buf);
394 outbuf = gst_buffer_new_and_alloc (payload_len);
395 GST_BUFFER_TIMESTAMP (outbuf) =
396 gst_util_uint64_scale_int (timestamp, GST_SECOND, rtpamrdepay->rate);
398 /* point to destination */
399 p = GST_BUFFER_DATA (outbuf);
400 /* point to first data packet */
401 dp = payload + num_packets;
402 if (rtpamrdepay->crc) {
403 /* skip CRC if present */
404 dp += num_nonempty_packets;
407 for (i = 0; i < num_packets; i++) {
410 fr_size = frame_size[(payload[i] & 0x78) >> 3];
412 /* copy FT, clear F bit */
413 *p++ = payload[i] & 0x7f;
414 /* copy data packet, FIXME, calc CRC here. */
415 memcpy (p, dp, fr_size);
421 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (rtpamrdepay->srcpad));
423 GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
424 GST_BUFFER_SIZE (outbuf));
425 ret = gst_pad_push (rtpamrdepay->srcpad, outbuf);
427 gst_buffer_unref (buf);
435 GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
436 (NULL), ("not negotiated"));
437 gst_buffer_unref (buf);
438 return GST_FLOW_NOT_NEGOTIATED;
442 gst_buffer_unref (buf);
449 gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
450 const GValue * value, GParamSpec * pspec)
452 GstRtpAMRDepay *rtpamrdepay;
454 rtpamrdepay = GST_RTP_AMR_DEPAY (object);
458 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
464 gst_rtp_amr_depay_get_property (GObject * object, guint prop_id, GValue * value,
467 GstRtpAMRDepay *rtpamrdepay;
469 rtpamrdepay = GST_RTP_AMR_DEPAY (object);
473 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
478 static GstStateChangeReturn
479 gst_rtp_amr_depay_change_state (GstElement * element, GstStateChange transition)
481 GstRtpAMRDepay *rtpamrdepay;
482 GstStateChangeReturn ret;
484 rtpamrdepay = GST_RTP_AMR_DEPAY (element);
486 switch (transition) {
487 case GST_STATE_CHANGE_NULL_TO_READY:
489 case GST_STATE_CHANGE_READY_TO_PAUSED:
495 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
497 switch (transition) {
498 case GST_STATE_CHANGE_READY_TO_NULL:
507 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
509 return gst_element_register (plugin, "rtpamrdepay",
510 GST_RANK_NONE, GST_TYPE_RTP_AMR_DEPAY);