gst/rtp/: Add support for AMR-WB.
[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 or AMR-WB 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         "application/x-rtp, "
84         "media = (string) \"audio\", "
85         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
86         "clock-rate = (int) 16000, "
87         "encoding-name = (string) \"AMR-WB\", "
88         "encoding-params = (string) \"1\", "
89         /* NOTE that all values must be strings in orde to be able to do SDP <->
90          * GstCaps mapping. */
91         "octet-align = (string) \"1\", "
92         "crc = (string) { \"0\", \"1\" }, "
93         "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
94         /* following options are not needed for a decoder
95          *
96          "mode-set = (int) [ 0, 7 ], "
97          "mode-change-period = (int) [ 1, MAX ], "
98          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
99          "maxptime = (int) [ 20, MAX ], "
100          "ptime = (int) [ 20, MAX ]"
101          */
102     )
103     );
104
105 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
106     GST_STATIC_PAD_TEMPLATE ("src",
107     GST_PAD_SRC,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000;"
110         "audio/AMR-WB, " "channels = (int) 1," "rate = (int) 16000")
111     );
112
113 static gboolean gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload,
114     GstCaps * caps);
115 static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
116     GstBuffer * buf);
117
118 GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
119     GST_TYPE_BASE_RTP_DEPAYLOAD);
120
121 static void
122 gst_rtp_amr_depay_base_init (gpointer klass)
123 {
124   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
125
126   gst_element_class_add_pad_template (element_class,
127       gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
128   gst_element_class_add_pad_template (element_class,
129       gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
130
131   gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
132 }
133
134 static void
135 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
136 {
137   GObjectClass *gobject_class;
138   GstElementClass *gstelement_class;
139   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
140
141   gobject_class = (GObjectClass *) klass;
142   gstelement_class = (GstElementClass *) klass;
143   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
144
145   parent_class = g_type_class_peek_parent (klass);
146
147   gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
148   gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
149 }
150
151 static void
152 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay,
153     GstRtpAMRDepayClass * klass)
154 {
155   GstBaseRTPDepayload *depayload;
156
157   depayload = GST_BASE_RTP_DEPAYLOAD (rtpamrdepay);
158
159   gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
160 }
161
162 static gboolean
163 gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
164 {
165   GstStructure *structure;
166   GstCaps *srccaps;
167   GstRtpAMRDepay *rtpamrdepay;
168   const gchar *params;
169   const gchar *str, *type;
170   gint clock_rate, need_clock_rate;
171
172   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
173
174   structure = gst_caps_get_structure (caps, 0);
175
176   /* figure out the mode first and set the clock rates */
177   if ((str = gst_structure_get_string (structure, "encoding-name"))) {
178     if (strcmp (str, "AMR") == 0) {
179       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
180       clock_rate = need_clock_rate = 8000;
181       type = "audio/AMR";
182     } else if (strcmp (str, "AMR-WB") == 0) {
183       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
184       clock_rate = need_clock_rate = 16000;
185       type = "audio/AMR-WB";
186     } else
187       goto invalid_mode;
188   } else
189     goto invalid_mode;
190
191   if (!(str = gst_structure_get_string (structure, "octet-align")))
192     rtpamrdepay->octet_align = FALSE;
193   else
194     rtpamrdepay->octet_align = (atoi (str) == 1);
195
196   if (!(str = gst_structure_get_string (structure, "crc")))
197     rtpamrdepay->crc = FALSE;
198   else
199     rtpamrdepay->crc = (atoi (str) == 1);
200
201   if (rtpamrdepay->crc) {
202     /* crc mode implies octet aligned mode */
203     rtpamrdepay->octet_align = TRUE;
204   }
205
206   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
207     rtpamrdepay->robust_sorting = FALSE;
208   else
209     rtpamrdepay->robust_sorting = (atoi (str) == 1);
210
211   if (rtpamrdepay->robust_sorting) {
212     /* robust_sorting mode implies octet aligned mode */
213     rtpamrdepay->octet_align = TRUE;
214   }
215
216   if (!(str = gst_structure_get_string (structure, "interleaving")))
217     rtpamrdepay->interleaving = FALSE;
218   else
219     rtpamrdepay->interleaving = (atoi (str) == 1);
220
221   if (rtpamrdepay->interleaving) {
222     /* interleaving mode implies octet aligned mode */
223     rtpamrdepay->octet_align = TRUE;
224   }
225
226   if (!(params = gst_structure_get_string (structure, "encoding-params")))
227     rtpamrdepay->channels = 1;
228   else {
229     rtpamrdepay->channels = atoi (params);
230   }
231
232   gst_structure_get_int (structure, "clock-rate", &clock_rate);
233   depayload->clock_rate = clock_rate;
234
235   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
236    * no robust sorting, no interleaving for now */
237   if (rtpamrdepay->channels != 1)
238     return FALSE;
239   if (clock_rate != need_clock_rate)
240     return FALSE;
241   if (rtpamrdepay->octet_align != TRUE)
242     return FALSE;
243   if (rtpamrdepay->robust_sorting != FALSE)
244     return FALSE;
245   if (rtpamrdepay->interleaving != FALSE)
246     return FALSE;
247
248   srccaps = gst_caps_new_simple (type,
249       "channels", G_TYPE_INT, rtpamrdepay->channels,
250       "rate", G_TYPE_INT, clock_rate, NULL);
251
252   gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
253   gst_caps_unref (srccaps);
254
255   rtpamrdepay->negotiated = TRUE;
256
257   return TRUE;
258
259   /* ERRORS */
260 invalid_mode:
261   {
262     GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
263     return FALSE;
264   }
265 }
266
267 /* -1 is invalid */
268 static gint nb_frame_size[16] = {
269   12, 13, 15, 17, 19, 20, 26, 31,
270   5, -1, -1, -1, -1, -1, -1, 0
271 };
272 static gint wb_frame_size[16] = {
273   17, 23, 32, 36, 40, 46, 50, 58,
274   60, -1, -1, -1, -1, -1, -1, 0
275 };
276
277 static GstBuffer *
278 gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
279 {
280   GstRtpAMRDepay *rtpamrdepay;
281   GstBuffer *outbuf = NULL;
282   gint payload_len;
283   gint *frame_size;
284
285   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
286
287   if (!rtpamrdepay->negotiated)
288     goto not_negotiated;
289
290   if (!gst_rtp_buffer_validate (buf))
291     goto invalid_packet;
292
293   /* setup frame size pointer */
294   if (rtpamrdepay->mode == GST_RTP_AMR_DP_MODE_NB)
295     frame_size = nb_frame_size;
296   else
297     frame_size = wb_frame_size;
298
299   /* when we get here, 1 channel, 8000/16000 Hz, octet aligned, no CRC, 
300    * no robust sorting, no interleaving data is to be depayloaded */
301   {
302     guint8 *payload, *p, *dp;
303     guint8 CMR;
304     gint i, num_packets, num_nonempty_packets;
305     gint amr_len;
306     gint ILL, ILP;
307
308     payload_len = gst_rtp_buffer_get_payload_len (buf);
309
310     /* need at least 2 bytes for the header */
311     if (payload_len < 2)
312       goto too_small;
313
314     payload = gst_rtp_buffer_get_payload (buf);
315
316     /* depay CMR. The CMR is used by the sender to request
317      * a new encoding mode.
318      *
319      *  0 1 2 3 4 5 6 7 
320      * +-+-+-+-+-+-+-+-+
321      * | CMR   |R|R|R|R|
322      * +-+-+-+-+-+-+-+-+
323      */
324     CMR = (payload[0] & 0xf0) >> 4;
325
326     /* strip CMR header now, pack FT and the data for the decoder */
327     payload_len -= 1;
328     payload += 1;
329
330     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
331
332     if (rtpamrdepay->interleaving) {
333       ILL = (payload[0] & 0xf0) >> 4;
334       ILP = (payload[0] & 0x0f);
335
336       payload_len -= 1;
337       payload += 1;
338
339       if (ILP > ILL)
340         goto wrong_interleaving;
341     }
342
343     /* 
344      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 
345      * +-+-+-+-+-+-+-+-+..
346      * |F|  FT   |Q|P|P| more FT..
347      * +-+-+-+-+-+-+-+-+..
348      */
349     /* count number of packets by counting the FTs. Also
350      * count number of amr data bytes and number of non-empty
351      * packets (this is also the number of CRCs if present). */
352     amr_len = 0;
353     num_nonempty_packets = 0;
354     num_packets = 0;
355     for (i = 0; i < payload_len; i++) {
356       gint fr_size;
357       guint8 FT;
358
359       FT = (payload[i] & 0x78) >> 3;
360
361       fr_size = frame_size[FT];
362       GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
363       if (fr_size == -1)
364         goto wrong_framesize;
365
366       if (fr_size > 0) {
367         amr_len += fr_size;
368         num_nonempty_packets++;
369       }
370       num_packets++;
371
372       if ((payload[i] & 0x80) == 0)
373         break;
374     }
375
376     if (rtpamrdepay->crc) {
377       /* data len + CRC len + header bytes should be smaller than payload_len */
378       if (num_packets + num_nonempty_packets + amr_len > payload_len)
379         goto wrong_length_1;
380     } else {
381       /* data len + header bytes should be smaller than payload_len */
382       if (num_packets + amr_len > payload_len)
383         goto wrong_length_2;
384     }
385
386     outbuf = gst_buffer_new_and_alloc (payload_len);
387
388     /* point to destination */
389     p = GST_BUFFER_DATA (outbuf);
390     /* point to first data packet */
391     dp = payload + num_packets;
392     if (rtpamrdepay->crc) {
393       /* skip CRC if present */
394       dp += num_nonempty_packets;
395     }
396
397     for (i = 0; i < num_packets; i++) {
398       gint fr_size;
399
400       /* copy FT, clear F bit */
401       *p++ = payload[i] & 0x7f;
402
403       fr_size = frame_size[(payload[i] & 0x78) >> 3];
404       if (fr_size > 0) {
405         /* copy data packet, FIXME, calc CRC here. */
406         memcpy (p, dp, fr_size);
407
408         p += fr_size;
409         dp += fr_size;
410       }
411     }
412     gst_buffer_set_caps (outbuf,
413         GST_PAD_CAPS (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload)));
414
415     GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
416         GST_BUFFER_SIZE (outbuf));
417   }
418   return outbuf;
419
420   /* ERRORS */
421 invalid_packet:
422   {
423     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
424         (NULL), ("AMR RTP packet did not validate"));
425     goto bad_packet;
426   }
427 not_negotiated:
428   {
429     GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
430         (NULL), ("not negotiated"));
431     return NULL;
432   }
433 too_small:
434   {
435     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
436         (NULL), ("AMR RTP payload too small (%d)", payload_len));
437     goto bad_packet;
438   }
439 wrong_interleaving:
440   {
441     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
442         (NULL), ("AMR RTP wrong interleaving"));
443     goto bad_packet;
444   }
445 wrong_framesize:
446   {
447     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
448         (NULL), ("AMR RTP frame size == -1"));
449     goto bad_packet;
450   }
451 wrong_length_1:
452   {
453     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
454         (NULL), ("AMR RTP wrong length 1"));
455     goto bad_packet;
456   }
457 wrong_length_2:
458   {
459     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
460         (NULL), ("AMR RTP wrong length 2"));
461     goto bad_packet;
462   }
463 bad_packet:
464   {
465     /* no fatal error */
466     return NULL;
467   }
468 }
469
470 gboolean
471 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
472 {
473   return gst_element_register (plugin, "rtpamrdepay",
474       GST_RANK_MARGINAL, GST_TYPE_RTP_AMR_DEPAY);
475 }