gst/rtp/: Caps extra properties must be defined as strings for depayloaders because...
[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 details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
27 #include "gstrtpamrdepay.h"
28
29 /* references:
30  *
31  * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
32  * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
33  * Wideband (AMR-WB) Audio Codecs.
34  */
35
36 /* elementfactory information */
37 static const GstElementDetails gst_rtp_amrdepay_details =
38 GST_ELEMENT_DETAILS ("RTP packet parser",
39     "Codec/Depayr/Network",
40     "Extracts AMR audio from RTP packets (RFC 3267)",
41     "Wim Taymans <wim@fluendo.com>");
42
43 /* RtpAMRDepay signals and args */
44 enum
45 {
46   /* FILL ME */
47   LAST_SIGNAL
48 };
49
50 enum
51 {
52   ARG_0
53 };
54
55 /* input is an RTP packet
56  *
57  * params see RFC 3267, section 8.1
58  */
59 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
61     GST_PAD_SINK,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("application/x-rtp, "
64         "media = (string) \"audio\", "
65         "clock-rate = (int) 8000, "
66         "encoding-name = (string) \"AMR\", "
67         "encoding-params = (string) \"1\", "
68         /* NOTE that all values must be strings in orde to be able to do SDP <->
69          * GstCaps mapping. */
70         "octet-align = (string) \"1\", "
71         "crc = (string) { \"0\", \"1\" }, "
72         "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
73         /* following options are not needed for a decoder
74          *
75          "mode-set = (int) [ 0, 7 ], "
76          "mode-change-period = (int) [ 1, MAX ], "
77          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
78          "maxptime = (int) [ 20, MAX ], "
79          "ptime = (int) [ 20, MAX ]"
80          */
81     )
82     );
83
84 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
85 GST_STATIC_PAD_TEMPLATE ("src",
86     GST_PAD_SRC,
87     GST_PAD_ALWAYS,
88     GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
89     );
90
91 static gboolean gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload,
92     GstCaps * caps);
93 static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
94     GstBuffer * buf);
95
96 GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
97     GST_TYPE_BASE_RTP_DEPAYLOAD);
98
99 static void
100 gst_rtp_amr_depay_base_init (gpointer klass)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
103
104   gst_element_class_add_pad_template (element_class,
105       gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
106   gst_element_class_add_pad_template (element_class,
107       gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
108
109   gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
110 }
111
112 static void
113 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
114 {
115   GObjectClass *gobject_class;
116   GstElementClass *gstelement_class;
117   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
118
119   gobject_class = (GObjectClass *) klass;
120   gstelement_class = (GstElementClass *) klass;
121   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
122
123   parent_class = g_type_class_peek_parent (klass);
124
125   gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
126   gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
127 }
128
129 static void
130 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay,
131     GstRtpAMRDepayClass * klass)
132 {
133   GstBaseRTPDepayload *depayload;
134
135   depayload = GST_BASE_RTP_DEPAYLOAD (rtpamrdepay);
136
137   depayload->clock_rate = 8000;
138   gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
139 }
140
141 static gboolean
142 gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
143 {
144   GstStructure *structure;
145   GstCaps *srccaps;
146   GstRtpAMRDepay *rtpamrdepay;
147   const gchar *params;
148   const gchar *str;
149   gint clock_rate;
150
151   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
152
153   structure = gst_caps_get_structure (caps, 0);
154
155   if (!(str = gst_structure_get_string (structure, "octet-align")))
156     rtpamrdepay->octet_align = FALSE;
157   else
158     rtpamrdepay->octet_align = (atoi (str) == 1);
159
160   if (!(str = gst_structure_get_string (structure, "crc")))
161     rtpamrdepay->crc = FALSE;
162   else
163     rtpamrdepay->crc = (atoi (str) == 1);
164
165   if (rtpamrdepay->crc) {
166     /* crc mode implies octet aligned mode */
167     rtpamrdepay->octet_align = TRUE;
168   }
169
170   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
171     rtpamrdepay->robust_sorting = FALSE;
172   else
173     rtpamrdepay->robust_sorting = (atoi (str) == 1);
174
175   if (rtpamrdepay->robust_sorting) {
176     /* robust_sorting mode implies octet aligned mode */
177     rtpamrdepay->octet_align = TRUE;
178   }
179
180   if (!(str = gst_structure_get_string (structure, "interleaving")))
181     rtpamrdepay->interleaving = FALSE;
182   else
183     rtpamrdepay->interleaving = (atoi (str) == 1);
184
185   if (rtpamrdepay->interleaving) {
186     /* interleaving mode implies octet aligned mode */
187     rtpamrdepay->octet_align = TRUE;
188   }
189
190   if (!(params = gst_structure_get_string (structure, "encoding-params")))
191     rtpamrdepay->channels = 1;
192   else {
193     rtpamrdepay->channels = atoi (params);
194   }
195
196   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
197     clock_rate = 8000;
198
199   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
200    * no robust sorting, no interleaving for now */
201   if (rtpamrdepay->channels != 1)
202     return FALSE;
203   if (clock_rate != 8000)
204     return FALSE;
205   if (rtpamrdepay->octet_align != TRUE)
206     return FALSE;
207   if (rtpamrdepay->robust_sorting != FALSE)
208     return FALSE;
209   if (rtpamrdepay->interleaving != FALSE)
210     return FALSE;
211
212   srccaps = gst_caps_new_simple ("audio/AMR",
213       "channels", G_TYPE_INT, rtpamrdepay->channels,
214       "rate", G_TYPE_INT, clock_rate, NULL);
215   gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
216   gst_caps_unref (srccaps);
217
218   rtpamrdepay->negotiated = TRUE;
219
220   return TRUE;
221 }
222
223 /* -1 is invalid */
224 static gint frame_size[16] = {
225   12, 13, 15, 17, 19, 20, 26, 31,
226   5, -1, -1, -1, -1, -1, -1, 0
227 };
228
229 static GstBuffer *
230 gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
231 {
232   GstRtpAMRDepay *rtpamrdepay;
233   GstBuffer *outbuf = NULL;
234
235   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
236
237   if (!rtpamrdepay->negotiated)
238     goto not_negotiated;
239
240   if (!gst_rtp_buffer_validate (buf)) {
241     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
242         (NULL), ("AMR RTP packet did not validate"));
243     goto bad_packet;
244   }
245
246   /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC, 
247    * no robust sorting, no interleaving data is to be depayloaded */
248   {
249     gint payload_len;
250     guint8 *payload, *p, *dp;
251     guint32 timestamp;
252     guint8 CMR;
253     gint i, num_packets, num_nonempty_packets;
254     gint amr_len;
255     gint ILL, ILP;
256
257     payload_len = gst_rtp_buffer_get_payload_len (buf);
258
259     /* need at least 2 bytes for the header */
260     if (payload_len < 2) {
261       GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
262           (NULL), ("AMR RTP payload too small (%d)", payload_len));
263       goto bad_packet;
264     }
265
266     payload = gst_rtp_buffer_get_payload (buf);
267
268     /* depay CMR. The CMR is used by the sender to request
269      * a new encoding mode.
270      *
271      *  0 1 2 3 4 5 6 7 
272      * +-+-+-+-+-+-+-+-+
273      * | CMR   |R|R|R|R|
274      * +-+-+-+-+-+-+-+-+
275      */
276     CMR = (payload[0] & 0xf0) >> 4;
277
278     /* strip CMR header now, pack FT and the data for the decoder */
279     payload_len -= 1;
280     payload += 1;
281
282     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
283
284     if (rtpamrdepay->interleaving) {
285       ILL = (payload[0] & 0xf0) >> 4;
286       ILP = (payload[0] & 0x0f);
287
288       payload_len -= 1;
289       payload += 1;
290
291       if (ILP > ILL) {
292         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
293             (NULL), ("AMR RTP wrong interleaving"));
294         goto bad_packet;
295       }
296     }
297
298     /* 
299      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 
300      * +-+-+-+-+-+-+-+-+..
301      * |F|  FT   |Q|P|P| more FT..
302      * +-+-+-+-+-+-+-+-+..
303      */
304     /* count number of packets by counting the FTs. Also
305      * count number of amr data bytes and number of non-empty
306      * packets (this is also the number of CRCs if present). */
307     amr_len = 0;
308     num_nonempty_packets = 0;
309     num_packets = 0;
310     for (i = 0; i < payload_len; i++) {
311       gint fr_size;
312       guint8 FT;
313
314       FT = (payload[i] & 0x78) >> 3;
315
316       fr_size = frame_size[FT];
317       GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
318       if (fr_size == -1) {
319         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
320             (NULL), ("AMR RTP frame size == -1"));
321         goto bad_packet;
322       }
323
324       if (fr_size > 0) {
325         amr_len += fr_size;
326         num_nonempty_packets++;
327       }
328       num_packets++;
329
330       if ((payload[i] & 0x80) == 0)
331         break;
332     }
333
334     if (rtpamrdepay->crc) {
335       /* data len + CRC len + header bytes should be smaller than payload_len */
336       if (num_packets + num_nonempty_packets + amr_len > payload_len) {
337         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
338             (NULL), ("AMR RTP wrong length 1"));
339         goto bad_packet;
340       }
341     } else {
342       /* data len + header bytes should be smaller than payload_len */
343       if (num_packets + amr_len > payload_len) {
344         GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
345             (NULL), ("AMR RTP wrong length 2"));
346         goto bad_packet;
347       }
348     }
349
350     timestamp = gst_rtp_buffer_get_timestamp (buf);
351
352     outbuf = gst_buffer_new_and_alloc (payload_len);
353     GST_BUFFER_TIMESTAMP (outbuf) =
354         gst_util_uint64_scale_int (timestamp, GST_SECOND,
355         depayload->clock_rate);
356
357     /* point to destination */
358     p = GST_BUFFER_DATA (outbuf);
359     /* point to first data packet */
360     dp = payload + num_packets;
361     if (rtpamrdepay->crc) {
362       /* skip CRC if present */
363       dp += num_nonempty_packets;
364     }
365
366     for (i = 0; i < num_packets; i++) {
367       gint fr_size;
368
369       /* copy FT, clear F bit */
370       *p++ = payload[i] & 0x7f;
371
372       fr_size = frame_size[(payload[i] & 0x78) >> 3];
373       if (fr_size > 0) {
374         /* copy data packet, FIXME, calc CRC here. */
375         memcpy (p, dp, fr_size);
376
377         p += fr_size;
378         dp += fr_size;
379       }
380     }
381     gst_buffer_set_caps (outbuf,
382         GST_PAD_CAPS (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload)));
383
384     GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
385         GST_BUFFER_SIZE (outbuf));
386   }
387
388   return outbuf;
389
390   /* ERRORS */
391 not_negotiated:
392   {
393     GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
394         (NULL), ("not negotiated"));
395     return NULL;
396   }
397 bad_packet:
398   {
399     /* no fatal error */
400     return NULL;
401   }
402 }
403
404 gboolean
405 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
406 {
407   return gst_element_register (plugin, "rtpamrdepay",
408       GST_RANK_NONE, GST_TYPE_RTP_AMR_DEPAY);
409 }