Fix memleak with gst_static_pad_template_get().
[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 "gstrtpamrdepay.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_amrdepay_details = {
33   "RTP packet parser",
34   "Codec/Depayr/Network",
35   "Extracts AMR audio from RTP packets (RFC 3267)",
36   "Wim Taymans <wim@fluendo.com>"
37 };
38
39 /* RtpAMRDepay 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_rtp_amr_depay_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         "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 
69          *
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 ]"
75          */
76     )
77     );
78
79 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
81     GST_PAD_SRC,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
84     );
85
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);
89
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);
92
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);
97
98 static GstStateChangeReturn gst_rtp_amr_depay_change_state (GstElement *
99     element, GstStateChange transition);
100
101 static GstElementClass *parent_class = NULL;
102
103 static GType
104 gst_rtp_amr_depay_get_type (void)
105 {
106   static GType rtpamrdepay_type = 0;
107
108   if (!rtpamrdepay_type) {
109     static const GTypeInfo rtpamrdepay_info = {
110       sizeof (GstRtpAMRDepayClass),
111       (GBaseInitFunc) gst_rtp_amr_depay_base_init,
112       NULL,
113       (GClassInitFunc) gst_rtp_amr_depay_class_init,
114       NULL,
115       NULL,
116       sizeof (GstRtpAMRDepay),
117       0,
118       (GInstanceInitFunc) gst_rtp_amr_depay_init,
119     };
120
121     rtpamrdepay_type =
122         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpAMRDepay",
123         &rtpamrdepay_info, 0);
124   }
125   return rtpamrdepay_type;
126 }
127
128 static void
129 gst_rtp_amr_depay_base_init (GstRtpAMRDepayClass * klass)
130 {
131   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
132
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));
137
138   gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
139 }
140
141 static void
142 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
143 {
144   GObjectClass *gobject_class;
145   GstElementClass *gstelement_class;
146
147   gobject_class = (GObjectClass *) klass;
148   gstelement_class = (GstElementClass *) klass;
149
150   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
151
152   gobject_class->set_property = gst_rtp_amr_depay_set_property;
153   gobject_class->get_property = gst_rtp_amr_depay_get_property;
154
155   gstelement_class->change_state = gst_rtp_amr_depay_change_state;
156 }
157
158 static void
159 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
160 {
161   rtpamrdepay->srcpad =
162       gst_pad_new_from_static_template (&gst_rtp_amr_depay_src_template, "src");
163
164   gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->srcpad);
165
166   rtpamrdepay->sinkpad =
167       gst_pad_new_from_static_template (&gst_rtp_amr_depay_sink_template,
168       "sink");
169   gst_pad_set_setcaps_function (rtpamrdepay->sinkpad,
170       gst_rtp_amr_depay_sink_setcaps);
171   gst_pad_set_chain_function (rtpamrdepay->sinkpad, gst_rtp_amr_depay_chain);
172   gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->sinkpad);
173 }
174
175 static gboolean
176 gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps)
177 {
178   GstStructure *structure;
179   GstCaps *srccaps;
180   GstRtpAMRDepay *rtpamrdepay;
181   const gchar *params;
182   const gchar *str;
183
184   rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
185
186   structure = gst_caps_get_structure (caps, 0);
187
188   if (!(str = gst_structure_get_string (structure, "octet-align")))
189     rtpamrdepay->octet_align = FALSE;
190   else
191     rtpamrdepay->octet_align = (atoi (str) == 1);
192
193   if (!(str = gst_structure_get_string (structure, "crc")))
194     rtpamrdepay->crc = FALSE;
195   else
196     rtpamrdepay->crc = (atoi (str) == 1);
197
198   if (rtpamrdepay->crc) {
199     /* crc mode implies octet aligned mode */
200     rtpamrdepay->octet_align = TRUE;
201   }
202
203   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
204     rtpamrdepay->robust_sorting = FALSE;
205   else
206     rtpamrdepay->robust_sorting = (atoi (str) == 1);
207
208   if (rtpamrdepay->robust_sorting) {
209     /* robust_sorting mode implies octet aligned mode */
210     rtpamrdepay->octet_align = TRUE;
211   }
212
213   if (!(str = gst_structure_get_string (structure, "interleaving")))
214     rtpamrdepay->interleaving = FALSE;
215   else
216     rtpamrdepay->interleaving = (atoi (str) == 1);
217
218   if (rtpamrdepay->interleaving) {
219     /* interleaving mode implies octet aligned mode */
220     rtpamrdepay->octet_align = TRUE;
221   }
222
223   if (!(params = gst_structure_get_string (structure, "encoding-params")))
224     rtpamrdepay->channels = 1;
225   else {
226     rtpamrdepay->channels = atoi (params);
227   }
228
229   if (!gst_structure_get_int (structure, "clock-rate", &rtpamrdepay->rate))
230     rtpamrdepay->rate = 8000;
231
232   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
233    * no robust sorting, no interleaving for now */
234   if (rtpamrdepay->channels != 1)
235     return FALSE;
236   if (rtpamrdepay->rate != 8000)
237     return FALSE;
238   if (rtpamrdepay->octet_align != TRUE)
239     return FALSE;
240   if (rtpamrdepay->robust_sorting != FALSE)
241     return FALSE;
242   if (rtpamrdepay->interleaving != FALSE)
243     return FALSE;
244
245   srccaps = gst_caps_new_simple ("audio/AMR",
246       "channels", G_TYPE_INT, rtpamrdepay->channels,
247       "rate", G_TYPE_INT, rtpamrdepay->rate, NULL);
248   gst_pad_set_caps (rtpamrdepay->srcpad, srccaps);
249   gst_caps_unref (srccaps);
250
251   rtpamrdepay->negotiated = TRUE;
252
253   return TRUE;
254 }
255
256 /* -1 is invalid */
257 static gint frame_size[16] = {
258   12, 13, 15, 17, 19, 20, 26, 31,
259   5, -1, -1, -1, -1, -1, -1, 0
260 };
261
262 static GstFlowReturn
263 gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buf)
264 {
265   GstRtpAMRDepay *rtpamrdepay;
266   GstBuffer *outbuf;
267   GstFlowReturn ret;
268
269   rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
270
271   if (!rtpamrdepay->negotiated)
272     goto not_negotiated;
273
274   if (!gst_rtp_buffer_validate (buf)) {
275     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
276         (NULL), ("AMR RTP packet did not validate"));
277     goto bad_packet;
278   }
279
280   /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC, 
281    * no robust sorting, no interleaving data is to be depayloaded */
282   {
283     gint payload_len;
284     guint8 *payload, *p, *dp;
285     guint32 timestamp;
286     guint8 CMR;
287     gint i, num_packets, num_nonempty_packets;
288     gint amr_len;
289     gint ILL, ILP;
290
291     payload_len = gst_rtp_buffer_get_payload_len (buf);
292
293     /* need at least 2 bytes for the header */
294     if (payload_len < 2) {
295       GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
296           (NULL), ("AMR RTP payload too small (%d)", payload_len));
297       goto bad_packet;
298     }
299
300     payload = gst_rtp_buffer_get_payload (buf);
301
302     /* depay CMR. The CMR is used by the sender to request
303      * a new encoding mode.
304      *
305      *  0 1 2 3 4 5 6 7 
306      * +-+-+-+-+-+-+-+-+
307      * | CMR   |R|R|R|R|
308      * +-+-+-+-+-+-+-+-+
309      */
310     CMR = (payload[0] & 0xf0) >> 4;
311
312     /* strip CMR header now, pack FT and the data for the decoder */
313     payload_len -= 1;
314     payload += 1;
315
316     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
317
318     if (rtpamrdepay->interleaving) {
319       ILL = (payload[0] & 0xf0) >> 4;
320       ILP = (payload[0] & 0x0f);
321
322       payload_len -= 1;
323       payload += 1;
324
325       if (ILP > ILL) {
326         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
327             (NULL), ("AMR RTP wrong interleaving"));
328         goto bad_packet;
329       }
330     }
331
332     /* 
333      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 
334      * +-+-+-+-+-+-+-+-+..
335      * |F|  FT   |Q|P|P| more FT..
336      * +-+-+-+-+-+-+-+-+..
337      */
338     /* count number of packets by counting the FTs. Also
339      * count number of amr data bytes and number of non-empty
340      * packets (this is also the number of CRCs if present). */
341     amr_len = 0;
342     num_nonempty_packets = 0;
343     num_packets = 0;
344     for (i = 0; i < payload_len; i++) {
345       gint fr_size;
346       guint8 FT;
347
348       FT = (payload[i] & 0x78) >> 3;
349
350       fr_size = frame_size[FT];
351       GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
352       if (fr_size == -1) {
353         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
354             (NULL), ("AMR RTP frame size == -1"));
355         goto bad_packet;
356       }
357
358       if (fr_size > 0) {
359         amr_len += fr_size;
360         num_nonempty_packets++;
361       }
362       num_packets++;
363
364       if ((payload[i] & 0x80) == 0)
365         break;
366     }
367
368     if (rtpamrdepay->crc) {
369       /* data len + CRC len + header bytes should be smaller than payload_len */
370       if (num_packets + num_nonempty_packets + amr_len > payload_len) {
371         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
372             (NULL), ("AMR RTP wrong length 1"));
373         goto bad_packet;
374       }
375     } else {
376       /* data len + header bytes should be smaller than payload_len */
377       if (num_packets + amr_len > payload_len) {
378         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
379             (NULL), ("AMR RTP wrong length 2"));
380         goto bad_packet;
381       }
382     }
383
384     timestamp = gst_rtp_buffer_get_timestamp (buf);
385
386     outbuf = gst_buffer_new_and_alloc (payload_len);
387     GST_BUFFER_TIMESTAMP (outbuf) =
388         gst_util_uint64_scale_int (timestamp, GST_SECOND, rtpamrdepay->rate);
389
390     /* point to destination */
391     p = GST_BUFFER_DATA (outbuf);
392     /* point to first data packet */
393     dp = payload + num_packets;
394     if (rtpamrdepay->crc) {
395       /* skip CRC if present */
396       dp += num_nonempty_packets;
397     }
398
399     for (i = 0; i < num_packets; i++) {
400       gint fr_size;
401
402       /* copy FT, clear F bit */
403       *p++ = payload[i] & 0x7f;
404
405       fr_size = frame_size[(payload[i] & 0x78) >> 3];
406       if (fr_size > 0) {
407         /* copy data packet, FIXME, calc CRC here. */
408         memcpy (p, dp, fr_size);
409
410         p += fr_size;
411         dp += fr_size;
412       }
413     }
414     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (rtpamrdepay->srcpad));
415
416     GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
417         GST_BUFFER_SIZE (outbuf));
418     ret = gst_pad_push (rtpamrdepay->srcpad, outbuf);
419
420     gst_buffer_unref (buf);
421   }
422
423   return ret;
424
425   /* ERRORS */
426 not_negotiated:
427   {
428     GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
429         (NULL), ("not negotiated"));
430     gst_buffer_unref (buf);
431     return GST_FLOW_NOT_NEGOTIATED;
432   }
433 bad_packet:
434   {
435     gst_buffer_unref (buf);
436     /* no fatal error */
437     return GST_FLOW_OK;
438   }
439 }
440
441 static void
442 gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
443     const GValue * value, GParamSpec * pspec)
444 {
445   GstRtpAMRDepay *rtpamrdepay;
446
447   rtpamrdepay = GST_RTP_AMR_DEPAY (object);
448
449   switch (prop_id) {
450     default:
451       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452       break;
453   }
454 }
455
456 static void
457 gst_rtp_amr_depay_get_property (GObject * object, guint prop_id, GValue * value,
458     GParamSpec * pspec)
459 {
460   GstRtpAMRDepay *rtpamrdepay;
461
462   rtpamrdepay = GST_RTP_AMR_DEPAY (object);
463
464   switch (prop_id) {
465     default:
466       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
467       break;
468   }
469 }
470
471 static GstStateChangeReturn
472 gst_rtp_amr_depay_change_state (GstElement * element, GstStateChange transition)
473 {
474   GstRtpAMRDepay *rtpamrdepay;
475   GstStateChangeReturn ret;
476
477   rtpamrdepay = GST_RTP_AMR_DEPAY (element);
478
479   switch (transition) {
480     case GST_STATE_CHANGE_NULL_TO_READY:
481       break;
482     case GST_STATE_CHANGE_READY_TO_PAUSED:
483       break;
484     default:
485       break;
486   }
487
488   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
489
490   switch (transition) {
491     case GST_STATE_CHANGE_READY_TO_NULL:
492       break;
493     default:
494       break;
495   }
496   return ret;
497 }
498
499 gboolean
500 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
501 {
502   return gst_element_register (plugin, "rtpamrdepay",
503       GST_RANK_NONE, GST_TYPE_RTP_AMR_DEPAY);
504 }