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