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