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