gst/rtp/gstrtpamrdepay.c: Patch from Sebastien Cote, fixes #319884
[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_template (gst_static_pad_template_get
163       (&gst_rtp_amr_depay_src_template), "src");
164
165   gst_element_add_pad (GST_ELEMENT (rtpamrdepay), rtpamrdepay->srcpad);
166
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);
174 }
175
176 static gboolean
177 gst_rtp_amr_depay_sink_setcaps (GstPad * pad, GstCaps * caps)
178 {
179   GstStructure *structure;
180   GstCaps *srccaps;
181   GstRtpAMRDepay *rtpamrdepay;
182   const gchar *params;
183   const gchar *str;
184
185   rtpamrdepay = GST_RTP_AMR_DEPAY (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     rtpamrdepay->octet_align = FALSE;
191   else
192     rtpamrdepay->octet_align = (atoi (str) == 1);
193
194   if (!(str = gst_structure_get_string (structure, "crc")))
195     rtpamrdepay->crc = FALSE;
196   else
197     rtpamrdepay->crc = (atoi (str) == 1);
198
199   if (rtpamrdepay->crc) {
200     /* crc mode implies octet aligned mode */
201     rtpamrdepay->octet_align = TRUE;
202   }
203
204   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
205     rtpamrdepay->robust_sorting = FALSE;
206   else
207     rtpamrdepay->robust_sorting = (atoi (str) == 1);
208
209   if (rtpamrdepay->robust_sorting) {
210     /* robust_sorting mode implies octet aligned mode */
211     rtpamrdepay->octet_align = TRUE;
212   }
213
214   if (!(str = gst_structure_get_string (structure, "interleaving")))
215     rtpamrdepay->interleaving = FALSE;
216   else
217     rtpamrdepay->interleaving = (atoi (str) == 1);
218
219   if (rtpamrdepay->interleaving) {
220     /* interleaving mode implies octet aligned mode */
221     rtpamrdepay->octet_align = TRUE;
222   }
223
224   if (!(params = gst_structure_get_string (structure, "encoding-params")))
225     rtpamrdepay->channels = 1;
226   else {
227     rtpamrdepay->channels = atoi (params);
228   }
229
230   if (!gst_structure_get_int (structure, "clock-rate", &rtpamrdepay->rate))
231     rtpamrdepay->rate = 8000;
232
233   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
234    * no robust sorting, no interleaving for now */
235   if (rtpamrdepay->channels != 1)
236     return FALSE;
237   if (rtpamrdepay->rate != 8000)
238     return FALSE;
239   if (rtpamrdepay->octet_align != TRUE)
240     return FALSE;
241   if (rtpamrdepay->robust_sorting != FALSE)
242     return FALSE;
243   if (rtpamrdepay->interleaving != FALSE)
244     return FALSE;
245
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);
251
252   rtpamrdepay->negotiated = TRUE;
253
254   return TRUE;
255 }
256
257 /* -1 is invalid */
258 static gint frame_size[16] = {
259   12, 13, 15, 17, 19, 20, 26, 31,
260   5, -1, -1, -1, -1, -1, -1, 0
261 };
262
263 static GstFlowReturn
264 gst_rtp_amr_depay_chain (GstPad * pad, GstBuffer * buf)
265 {
266   GstRtpAMRDepay *rtpamrdepay;
267   GstBuffer *outbuf;
268   GstFlowReturn ret;
269
270   rtpamrdepay = GST_RTP_AMR_DEPAY (GST_OBJECT_PARENT (pad));
271
272   if (!rtpamrdepay->negotiated)
273     goto not_negotiated;
274
275   if (!gst_rtp_buffer_validate (buf)) {
276     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
277         (NULL), ("AMR RTP packet did not validate"));
278     goto bad_packet;
279   }
280
281   /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC, 
282    * no robust sorting, no interleaving data is to be depayloaded */
283   {
284     gint payload_len;
285     guint8 *payload, *p, *dp;
286     guint32 timestamp;
287     guint8 CMR;
288     gint i, num_packets, num_nonempty_packets;
289     gint amr_len;
290     gint ILL, ILP;
291
292     payload_len = gst_rtp_buffer_get_payload_len (buf);
293
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));
298       goto bad_packet;
299     }
300
301     payload = gst_rtp_buffer_get_payload (buf);
302
303     /* depay CMR. The CMR is used by the sender to request
304      * a new encoding mode.
305      *
306      *  0 1 2 3 4 5 6 7 
307      * +-+-+-+-+-+-+-+-+
308      * | CMR   |R|R|R|R|
309      * +-+-+-+-+-+-+-+-+
310      */
311     CMR = (payload[0] & 0xf0) >> 4;
312
313     /* strip CMR header now, pack FT and the data for the decoder */
314     payload_len -= 1;
315     payload += 1;
316
317     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
318
319     if (rtpamrdepay->interleaving) {
320       ILL = (payload[0] & 0xf0) >> 4;
321       ILP = (payload[0] & 0x0f);
322
323       payload_len -= 1;
324       payload += 1;
325
326       if (ILP > ILL) {
327         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
328             (NULL), ("AMR RTP wrong interleaving"));
329         goto bad_packet;
330       }
331     }
332
333     /* 
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      * +-+-+-+-+-+-+-+-+..
338      */
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). */
342     amr_len = 0;
343     num_nonempty_packets = 0;
344     num_packets = 0;
345     for (i = 0; i < payload_len; i++) {
346       gint fr_size;
347       guint8 FT;
348
349       FT = (payload[i] & 0x78) >> 3;
350
351       fr_size = frame_size[FT];
352       GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
353       if (fr_size == -1) {
354         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
355             (NULL), ("AMR RTP frame size == -1"));
356         goto bad_packet;
357       }
358
359       if (fr_size > 0) {
360         amr_len += fr_size;
361         num_nonempty_packets++;
362       }
363       num_packets++;
364
365       if ((payload[i] & 0x80) == 0)
366         break;
367     }
368
369     if (rtpamrdepay->crc) {
370       /* data len + CRC len + header bytes should be smaller than payload_len */
371       if (num_packets + num_nonempty_packets + amr_len > payload_len) {
372         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
373             (NULL), ("AMR RTP wrong length 1"));
374         goto bad_packet;
375       }
376     } else {
377       /* data len + header bytes should be smaller than payload_len */
378       if (num_packets + amr_len > payload_len) {
379         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
380             (NULL), ("AMR RTP wrong length 2"));
381         goto bad_packet;
382       }
383     }
384
385     timestamp = gst_rtp_buffer_get_timestamp (buf);
386
387     outbuf = gst_buffer_new_and_alloc (payload_len);
388     GST_BUFFER_TIMESTAMP (outbuf) =
389         gst_util_uint64_scale_int (timestamp, GST_SECOND, rtpamrdepay->rate);
390
391     /* point to destination */
392     p = GST_BUFFER_DATA (outbuf);
393     /* point to first data packet */
394     dp = payload + num_packets;
395     if (rtpamrdepay->crc) {
396       /* skip CRC if present */
397       dp += num_nonempty_packets;
398     }
399
400     for (i = 0; i < num_packets; i++) {
401       gint fr_size;
402
403       /* copy FT, clear F bit */
404       *p++ = payload[i] & 0x7f;
405
406       fr_size = frame_size[(payload[i] & 0x78) >> 3];
407       if (fr_size > 0) {
408         /* copy data packet, FIXME, calc CRC here. */
409         memcpy (p, dp, fr_size);
410
411         p += fr_size;
412         dp += fr_size;
413       }
414     }
415     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (rtpamrdepay->srcpad));
416
417     GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
418         GST_BUFFER_SIZE (outbuf));
419     ret = gst_pad_push (rtpamrdepay->srcpad, outbuf);
420
421     gst_buffer_unref (buf);
422   }
423
424   return ret;
425
426   /* ERRORS */
427 not_negotiated:
428   {
429     GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
430         (NULL), ("not negotiated"));
431     gst_buffer_unref (buf);
432     return GST_FLOW_NOT_NEGOTIATED;
433   }
434 bad_packet:
435   {
436     gst_buffer_unref (buf);
437     /* no fatal error */
438     return GST_FLOW_OK;
439   }
440 }
441
442 static void
443 gst_rtp_amr_depay_set_property (GObject * object, guint prop_id,
444     const GValue * value, GParamSpec * pspec)
445 {
446   GstRtpAMRDepay *rtpamrdepay;
447
448   rtpamrdepay = GST_RTP_AMR_DEPAY (object);
449
450   switch (prop_id) {
451     default:
452       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453       break;
454   }
455 }
456
457 static void
458 gst_rtp_amr_depay_get_property (GObject * object, guint prop_id, GValue * value,
459     GParamSpec * pspec)
460 {
461   GstRtpAMRDepay *rtpamrdepay;
462
463   rtpamrdepay = GST_RTP_AMR_DEPAY (object);
464
465   switch (prop_id) {
466     default:
467       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
468       break;
469   }
470 }
471
472 static GstStateChangeReturn
473 gst_rtp_amr_depay_change_state (GstElement * element, GstStateChange transition)
474 {
475   GstRtpAMRDepay *rtpamrdepay;
476   GstStateChangeReturn ret;
477
478   rtpamrdepay = GST_RTP_AMR_DEPAY (element);
479
480   switch (transition) {
481     case GST_STATE_CHANGE_NULL_TO_READY:
482       break;
483     case GST_STATE_CHANGE_READY_TO_PAUSED:
484       break;
485     default:
486       break;
487   }
488
489   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
490
491   switch (transition) {
492     case GST_STATE_CHANGE_READY_TO_NULL:
493       break;
494     default:
495       break;
496   }
497   return ret;
498 }
499
500 gboolean
501 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
502 {
503   return gst_element_register (plugin, "rtpamrdepay",
504       GST_RANK_NONE, GST_TYPE_RTP_AMR_DEPAY);
505 }