gst/rtp/README: Update README
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpamrdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
3  *
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.
8  *
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 
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #  include "config.h"
17 #endif
18
19 #include <gst/rtp/gstrtpbuffer.h>
20
21 #include <string.h>
22 #include "gstrtpamrdec.h"
23
24 /* references:
25  *
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 
28  *   Codecs.
29  */
30
31 /* elementfactory information */
32 static GstElementDetails gst_rtp_amrdec_details = {
33   "RTP packet parser",
34   "Codec/Parser/Network",
35   "Extracts AMR audio from RTP packets (RFC 3267)",
36   "Wim Taymans <wim@fluendo.com>"
37 };
38
39 /* RtpAMRDec signals and args */
40 enum
41 {
42   /* FILL ME */
43   LAST_SIGNAL
44 };
45
46 enum
47 {
48   ARG_0,
49   ARG_FREQUENCY
50 };
51
52 /* input is an RTP packet 
53  *
54  * params see RFC 3267, section 8.1
55  */
56 static GstStaticPadTemplate gst_rtpamrdec_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
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, "
68         "robust-sorting = (string) 0, " "interleaving = (string) 0"
69         /* following options are not needed for a decoder 
70          *
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 ]"
76          */
77     )
78     );
79
80 static GstStaticPadTemplate gst_rtpamrdec_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
82     GST_PAD_SRC,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
85     );
86
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);
90
91 static gboolean gst_rtpamrdec_sink_setcaps (GstPad * pad, GstCaps * caps);
92 static GstFlowReturn gst_rtpamrdec_chain (GstPad * pad, GstBuffer * buffer);
93
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);
98
99 static GstStateChangeReturn gst_rtpamrdec_change_state (GstElement * element,
100     GstStateChange transition);
101
102 static GstElementClass *parent_class = NULL;
103
104 static GType
105 gst_rtpamrdec_get_type (void)
106 {
107   static GType rtpamrdec_type = 0;
108
109   if (!rtpamrdec_type) {
110     static const GTypeInfo rtpamrdec_info = {
111       sizeof (GstRtpAMRDecClass),
112       (GBaseInitFunc) gst_rtpamrdec_base_init,
113       NULL,
114       (GClassInitFunc) gst_rtpamrdec_class_init,
115       NULL,
116       NULL,
117       sizeof (GstRtpAMRDec),
118       0,
119       (GInstanceInitFunc) gst_rtpamrdec_init,
120     };
121
122     rtpamrdec_type =
123         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpAMRDec",
124         &rtpamrdec_info, 0);
125   }
126   return rtpamrdec_type;
127 }
128
129 static void
130 gst_rtpamrdec_base_init (GstRtpAMRDecClass * klass)
131 {
132   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133
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));
138
139   gst_element_class_set_details (element_class, &gst_rtp_amrdec_details);
140 }
141
142 static void
143 gst_rtpamrdec_class_init (GstRtpAMRDecClass * klass)
144 {
145   GObjectClass *gobject_class;
146   GstElementClass *gstelement_class;
147
148   gobject_class = (GObjectClass *) klass;
149   gstelement_class = (GstElementClass *) klass;
150
151   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
152
153   gobject_class->set_property = gst_rtpamrdec_set_property;
154   gobject_class->get_property = gst_rtpamrdec_get_property;
155
156   gstelement_class->change_state = gst_rtpamrdec_change_state;
157 }
158
159 static void
160 gst_rtpamrdec_init (GstRtpAMRDec * rtpamrdec)
161 {
162   rtpamrdec->srcpad =
163       gst_pad_new_from_template (gst_static_pad_template_get
164       (&gst_rtpamrdec_src_template), "src");
165
166   gst_element_add_pad (GST_ELEMENT (rtpamrdec), rtpamrdec->srcpad);
167
168   rtpamrdec->sinkpad =
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);
174 }
175
176 static gboolean
177 gst_rtpamrdec_sink_setcaps (GstPad * pad, GstCaps * caps)
178 {
179   GstStructure *structure;
180   GstCaps *srccaps;
181   GstRtpAMRDec *rtpamrdec;
182   const gchar *params;
183   const gchar *str;
184
185   rtpamrdec = GST_RTP_AMR_DEC (GST_OBJECT_PARENT (pad));
186
187   structure = gst_caps_get_structure (caps, 0);
188
189   if (!(str = gst_structure_get_string (structure, "octet-align")))
190     rtpamrdec->octet_align = FALSE;
191   else
192     rtpamrdec->octet_align = (atoi (str) == 1);
193
194   if (!(str = gst_structure_get_string (structure, "crc")))
195     rtpamrdec->crc = FALSE;
196   else
197     rtpamrdec->crc = (atoi (str) == 1);
198
199   if (rtpamrdec->crc) {
200     /* crc mode implies octet aligned mode */
201     rtpamrdec->octet_align = TRUE;
202   }
203
204   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
205     rtpamrdec->robust_sorting = FALSE;
206   else
207     rtpamrdec->robust_sorting = (atoi (str) == 1);
208
209   if (rtpamrdec->robust_sorting) {
210     /* robust_sorting mode implies octet aligned mode */
211     rtpamrdec->octet_align = TRUE;
212   }
213
214   if (!(str = gst_structure_get_string (structure, "interleaving")))
215     rtpamrdec->interleaving = FALSE;
216   else
217     rtpamrdec->interleaving = (atoi (str) == 1);
218
219   if (rtpamrdec->interleaving) {
220     /* interleaving mode implies octet aligned mode */
221     rtpamrdec->octet_align = TRUE;
222   }
223
224   if (!(params = gst_structure_get_string (structure, "encoding-params")))
225     rtpamrdec->channels = 1;
226   else {
227     rtpamrdec->channels = atoi (params);
228   }
229
230   if (!gst_structure_get_int (structure, "clock-rate", &rtpamrdec->rate))
231     rtpamrdec->rate = 8000;
232
233   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
234    * no robust sorting, no interleaving for now */
235   if (rtpamrdec->channels != 1)
236     return FALSE;
237   if (rtpamrdec->rate != 8000)
238     return FALSE;
239   if (rtpamrdec->octet_align != TRUE)
240     return FALSE;
241   if (rtpamrdec->crc != FALSE)
242     return FALSE;
243   if (rtpamrdec->robust_sorting != FALSE)
244     return FALSE;
245   if (rtpamrdec->interleaving != FALSE)
246     return FALSE;
247
248   srccaps = gst_caps_new_simple ("audio/AMR",
249       "channels", G_TYPE_INT, rtpamrdec->channels,
250       "rate", G_TYPE_INT, rtpamrdec->rate, NULL);
251   gst_pad_set_caps (rtpamrdec->srcpad, srccaps);
252   gst_caps_unref (srccaps);
253
254   rtpamrdec->negotiated = TRUE;
255
256   return TRUE;
257 }
258
259 static GstFlowReturn
260 gst_rtpamrdec_chain (GstPad * pad, GstBuffer * buf)
261 {
262   GstRtpAMRDec *rtpamrdec;
263   GstBuffer *outbuf;
264   GstFlowReturn ret;
265
266   rtpamrdec = GST_RTP_AMR_DEC (GST_OBJECT_PARENT (pad));
267
268   if (!rtpamrdec->negotiated)
269     goto not_negotiated;
270
271   if (!gst_rtpbuffer_validate (buf))
272     goto bad_packet;
273
274   /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC, 
275    * no robust sorting, no interleaving data is to be parsed */
276   {
277     gint payload_len;
278     guint8 *payload;
279     guint32 timestamp;
280     guint8 CMR, F, FT, Q;
281
282     payload_len = gst_rtpbuffer_get_payload_len (buf);
283
284     /* need at least 2 bytes for the header */
285     if (payload_len < 2)
286       goto bad_packet;
287
288     payload = gst_rtpbuffer_get_payload (buf);
289
290     /* parse header 
291      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 
292      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+..
293      * | CMR   |R|R|R|R|F|  FT   |Q|P|P|
294      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+..
295      */
296     CMR = (payload[0] & 0xf0) >> 4;
297     F = (payload[1] & 0x80) >> 7;
298     /* we only support 1 packet per RTP packet for now */
299     if (F != 0)
300       goto one_packet_only;
301
302     FT = (payload[1] & 0x78) >> 3;
303     Q = (payload[1] & 0x04) >> 2;
304
305     /* skip packet */
306     if (FT > 9 && FT < 15) {
307       ret = GST_FLOW_OK;
308       goto skip;
309     }
310
311     /* strip header now, leave FT in the data for the decoder */
312     payload_len -= 1;
313     payload += 1;
314
315     timestamp = gst_rtpbuffer_get_timestamp (buf);
316
317     outbuf = gst_buffer_new_and_alloc (payload_len);
318
319     GST_BUFFER_TIMESTAMP (outbuf) = timestamp * GST_SECOND / rtpamrdec->rate;
320
321     memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
322
323     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (rtpamrdec->srcpad));
324
325     GST_DEBUG ("gst_rtpamrdec_chain: pushing buffer of size %d",
326         GST_BUFFER_SIZE (outbuf));
327     ret = gst_pad_push (rtpamrdec->srcpad, outbuf);
328
329   skip:
330     gst_buffer_unref (buf);
331   }
332
333   return ret;
334
335 not_negotiated:
336   {
337     GST_DEBUG ("not_negotiated");
338     gst_buffer_unref (buf);
339     return GST_FLOW_NOT_NEGOTIATED;
340   }
341 bad_packet:
342   {
343     GST_DEBUG ("Packet did not validate");
344     gst_buffer_unref (buf);
345     return GST_FLOW_ERROR;
346   }
347 one_packet_only:
348   {
349     GST_DEBUG ("One packet per RTP packet only");
350     gst_buffer_unref (buf);
351     return GST_FLOW_ERROR;
352   }
353 }
354
355 static void
356 gst_rtpamrdec_set_property (GObject * object, guint prop_id,
357     const GValue * value, GParamSpec * pspec)
358 {
359   GstRtpAMRDec *rtpamrdec;
360
361   rtpamrdec = GST_RTP_AMR_DEC (object);
362
363   switch (prop_id) {
364     default:
365       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366       break;
367   }
368 }
369
370 static void
371 gst_rtpamrdec_get_property (GObject * object, guint prop_id, GValue * value,
372     GParamSpec * pspec)
373 {
374   GstRtpAMRDec *rtpamrdec;
375
376   rtpamrdec = GST_RTP_AMR_DEC (object);
377
378   switch (prop_id) {
379     default:
380       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381       break;
382   }
383 }
384
385 static GstStateChangeReturn
386 gst_rtpamrdec_change_state (GstElement * element, GstStateChange transition)
387 {
388   GstRtpAMRDec *rtpamrdec;
389   GstStateChangeReturn ret;
390
391   rtpamrdec = GST_RTP_AMR_DEC (element);
392
393   switch (transition) {
394     case GST_STATE_CHANGE_NULL_TO_READY:
395       break;
396     case GST_STATE_CHANGE_READY_TO_PAUSED:
397       break;
398     default:
399       break;
400   }
401
402   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
403
404   switch (transition) {
405     case GST_STATE_CHANGE_READY_TO_NULL:
406       break;
407     default:
408       break;
409   }
410   return ret;
411 }
412
413 gboolean
414 gst_rtpamrdec_plugin_init (GstPlugin * plugin)
415 {
416   return gst_element_register (plugin, "rtpamrdec",
417       GST_RANK_NONE, GST_TYPE_RTP_AMR_DEC);
418 }