Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpamrdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.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 GST_DEBUG_CATEGORY_STATIC (rtpamrdepay_debug);
31 #define GST_CAT_DEFAULT (rtpamrdepay_debug)
32
33 /* references:
34  *
35  * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
36  * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
37  * Wideband (AMR-WB) Audio Codecs.
38  */
39
40 /* RtpAMRDepay signals and args */
41 enum
42 {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 enum
48 {
49   ARG_0
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) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
63         "clock-rate = (int) 8000, "
64         "encoding-name = (string) \"AMR\", "
65         "encoding-params = (string) \"1\", "
66         /* NOTE that all values must be strings in orde to be able to do SDP <->
67          * GstCaps mapping. */
68         "octet-align = (string) \"1\", "
69         "crc = (string) { \"0\", \"1\" }, "
70         "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\";"
71         /* following options are not needed for a decoder
72          *
73          "mode-set = (int) [ 0, 7 ], "
74          "mode-change-period = (int) [ 1, MAX ], "
75          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
76          "maxptime = (int) [ 20, MAX ], "
77          "ptime = (int) [ 20, MAX ]"
78          */
79         "application/x-rtp, "
80         "media = (string) \"audio\", "
81         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
82         "clock-rate = (int) 16000, "
83         "encoding-name = (string) \"AMR-WB\", "
84         "encoding-params = (string) \"1\", "
85         /* NOTE that all values must be strings in orde to be able to do SDP <->
86          * GstCaps mapping. */
87         "octet-align = (string) \"1\", "
88         "crc = (string) { \"0\", \"1\" }, "
89         "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
90         /* following options are not needed for a decoder
91          *
92          "mode-set = (int) [ 0, 7 ], "
93          "mode-change-period = (int) [ 1, MAX ], "
94          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
95          "maxptime = (int) [ 20, MAX ], "
96          "ptime = (int) [ 20, MAX ]"
97          */
98     )
99     );
100
101 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
102     GST_STATIC_PAD_TEMPLATE ("src",
103     GST_PAD_SRC,
104     GST_PAD_ALWAYS,
105     GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000;"
106         "audio/AMR-WB, " "channels = (int) 1," "rate = (int) 16000")
107     );
108
109 static gboolean gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload,
110     GstCaps * caps);
111 static GstBuffer *gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload,
112     GstBuffer * buf);
113
114 #define gst_rtp_amr_depay_parent_class parent_class
115 G_DEFINE_TYPE (GstRtpAMRDepay, gst_rtp_amr_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
116
117 static void
118 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
119 {
120   GstElementClass *gstelement_class;
121   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
122
123   gstelement_class = (GstElementClass *) klass;
124   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
125
126   gst_element_class_add_pad_template (gstelement_class,
127       gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
128   gst_element_class_add_pad_template (gstelement_class,
129       gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
130
131   gst_element_class_set_details_simple (gstelement_class, "RTP AMR depayloader",
132       "Codec/Depayloader/Network/RTP",
133       "Extracts AMR or AMR-WB audio from RTP packets (RFC 3267)",
134       "Wim Taymans <wim.taymans@gmail.com>");
135
136   gstrtpbasedepayload_class->process = gst_rtp_amr_depay_process;
137   gstrtpbasedepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
138
139   GST_DEBUG_CATEGORY_INIT (rtpamrdepay_debug, "rtpamrdepay", 0,
140       "AMR/AMR-WB RTP Depayloader");
141 }
142
143 static void
144 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
145 {
146   GstRTPBaseDepayload *depayload;
147
148   depayload = GST_RTP_BASE_DEPAYLOAD (rtpamrdepay);
149
150   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
151 }
152
153 static gboolean
154 gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
155 {
156   GstStructure *structure;
157   GstCaps *srccaps;
158   GstRtpAMRDepay *rtpamrdepay;
159   const gchar *params;
160   const gchar *str, *type;
161   gint clock_rate, need_clock_rate;
162   gboolean res;
163
164   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
165
166   structure = gst_caps_get_structure (caps, 0);
167
168   /* figure out the mode first and set the clock rates */
169   if ((str = gst_structure_get_string (structure, "encoding-name"))) {
170     if (strcmp (str, "AMR") == 0) {
171       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
172       need_clock_rate = 8000;
173       type = "audio/AMR";
174     } else if (strcmp (str, "AMR-WB") == 0) {
175       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
176       need_clock_rate = 16000;
177       type = "audio/AMR-WB";
178     } else
179       goto invalid_mode;
180   } else
181     goto invalid_mode;
182
183   if (!(str = gst_structure_get_string (structure, "octet-align")))
184     rtpamrdepay->octet_align = FALSE;
185   else
186     rtpamrdepay->octet_align = (atoi (str) == 1);
187
188   if (!(str = gst_structure_get_string (structure, "crc")))
189     rtpamrdepay->crc = FALSE;
190   else
191     rtpamrdepay->crc = (atoi (str) == 1);
192
193   if (rtpamrdepay->crc) {
194     /* crc mode implies octet aligned mode */
195     rtpamrdepay->octet_align = TRUE;
196   }
197
198   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
199     rtpamrdepay->robust_sorting = FALSE;
200   else
201     rtpamrdepay->robust_sorting = (atoi (str) == 1);
202
203   if (rtpamrdepay->robust_sorting) {
204     /* robust_sorting mode implies octet aligned mode */
205     rtpamrdepay->octet_align = TRUE;
206   }
207
208   if (!(str = gst_structure_get_string (structure, "interleaving")))
209     rtpamrdepay->interleaving = FALSE;
210   else
211     rtpamrdepay->interleaving = (atoi (str) == 1);
212
213   if (rtpamrdepay->interleaving) {
214     /* interleaving mode implies octet aligned mode */
215     rtpamrdepay->octet_align = TRUE;
216   }
217
218   if (!(params = gst_structure_get_string (structure, "encoding-params")))
219     rtpamrdepay->channels = 1;
220   else {
221     rtpamrdepay->channels = atoi (params);
222   }
223
224   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
225     clock_rate = need_clock_rate;
226   depayload->clock_rate = clock_rate;
227
228   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
229    * no robust sorting, no interleaving for now */
230   if (rtpamrdepay->channels != 1)
231     return FALSE;
232   if (clock_rate != need_clock_rate)
233     return FALSE;
234   if (rtpamrdepay->octet_align != TRUE)
235     return FALSE;
236   if (rtpamrdepay->robust_sorting != FALSE)
237     return FALSE;
238   if (rtpamrdepay->interleaving != FALSE)
239     return FALSE;
240
241   srccaps = gst_caps_new_simple (type,
242       "channels", G_TYPE_INT, rtpamrdepay->channels,
243       "rate", G_TYPE_INT, clock_rate, NULL);
244   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
245   gst_caps_unref (srccaps);
246
247   return res;
248
249   /* ERRORS */
250 invalid_mode:
251   {
252     GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
253     return FALSE;
254   }
255 }
256
257 /* -1 is invalid */
258 static const gint nb_frame_size[16] = {
259   12, 13, 15, 17, 19, 20, 26, 31,
260   5, -1, -1, -1, -1, -1, -1, 0
261 };
262
263 static const gint wb_frame_size[16] = {
264   17, 23, 32, 36, 40, 46, 50, 58,
265   60, 5, -1, -1, -1, -1, -1, 0
266 };
267
268 static GstBuffer *
269 gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
270 {
271   GstRtpAMRDepay *rtpamrdepay;
272   const gint *frame_size;
273   GstBuffer *outbuf = NULL;
274   gint payload_len;
275   GstRTPBuffer rtp = { NULL };
276   GstMapInfo map;
277
278   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
279
280   /* setup frame size pointer */
281   if (rtpamrdepay->mode == GST_RTP_AMR_DP_MODE_NB)
282     frame_size = nb_frame_size;
283   else
284     frame_size = wb_frame_size;
285
286   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
287
288   /* when we get here, 1 channel, 8000/16000 Hz, octet aligned, no CRC,
289    * no robust sorting, no interleaving data is to be depayloaded */
290   {
291     guint8 *payload, *p, *dp;
292     gint i, num_packets, num_nonempty_packets;
293     gint amr_len;
294     gint ILL, ILP;
295
296     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
297
298     /* need at least 2 bytes for the header */
299     if (payload_len < 2)
300       goto too_small;
301
302     payload = gst_rtp_buffer_get_payload (&rtp);
303
304     /* depay CMR. The CMR is used by the sender to request
305      * a new encoding mode.
306      *
307      *  0 1 2 3 4 5 6 7
308      * +-+-+-+-+-+-+-+-+
309      * | CMR   |R|R|R|R|
310      * +-+-+-+-+-+-+-+-+
311      */
312     /* CMR = (payload[0] & 0xf0) >> 4; */
313
314     /* strip CMR header now, pack FT and the data for the decoder */
315     payload_len -= 1;
316     payload += 1;
317
318     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
319
320     if (rtpamrdepay->interleaving) {
321       ILL = (payload[0] & 0xf0) >> 4;
322       ILP = (payload[0] & 0x0f);
323
324       payload_len -= 1;
325       payload += 1;
326
327       if (ILP > ILL)
328         goto wrong_interleaving;
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         goto wrong_framesize;
353
354       if (fr_size > 0) {
355         amr_len += fr_size;
356         num_nonempty_packets++;
357       }
358       num_packets++;
359
360       if ((payload[i] & 0x80) == 0)
361         break;
362     }
363
364     if (rtpamrdepay->crc) {
365       /* data len + CRC len + header bytes should be smaller than payload_len */
366       if (num_packets + num_nonempty_packets + amr_len > payload_len)
367         goto wrong_length_1;
368     } else {
369       /* data len + header bytes should be smaller than payload_len */
370       if (num_packets + amr_len > payload_len)
371         goto wrong_length_2;
372     }
373
374     outbuf = gst_buffer_new_and_alloc (payload_len);
375
376     /* point to destination */
377     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
378
379     /* point to first data packet */
380     p = map.data;
381     dp = payload + num_packets;
382     if (rtpamrdepay->crc) {
383       /* skip CRC if present */
384       dp += num_nonempty_packets;
385     }
386
387     for (i = 0; i < num_packets; i++) {
388       gint fr_size;
389
390       /* copy FT, clear F bit */
391       *p++ = payload[i] & 0x7f;
392
393       fr_size = frame_size[(payload[i] & 0x78) >> 3];
394       if (fr_size > 0) {
395         /* copy data packet, FIXME, calc CRC here. */
396         memcpy (p, dp, fr_size);
397
398         p += fr_size;
399         dp += fr_size;
400       }
401     }
402     gst_buffer_unmap (outbuf, &map);
403
404     /* we can set the duration because each packet is 20 milliseconds */
405     GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
406
407     if (gst_rtp_buffer_get_marker (&rtp)) {
408       /* marker bit marks a discont buffer after a talkspurt. */
409       GST_DEBUG_OBJECT (depayload, "marker bit was set");
410       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
411     }
412
413     GST_DEBUG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
414         gst_buffer_get_size (outbuf));
415   }
416   return outbuf;
417
418   /* ERRORS */
419 too_small:
420   {
421     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
422         (NULL), ("AMR RTP payload too small (%d)", payload_len));
423     goto bad_packet;
424   }
425 wrong_interleaving:
426   {
427     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
428         (NULL), ("AMR RTP wrong interleaving"));
429     goto bad_packet;
430   }
431 wrong_framesize:
432   {
433     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
434         (NULL), ("AMR RTP frame size == -1"));
435     goto bad_packet;
436   }
437 wrong_length_1:
438   {
439     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
440         (NULL), ("AMR RTP wrong length 1"));
441     goto bad_packet;
442   }
443 wrong_length_2:
444   {
445     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
446         (NULL), ("AMR RTP wrong length 2"));
447     goto bad_packet;
448   }
449 bad_packet:
450   {
451     /* no fatal error */
452     return NULL;
453   }
454 }
455
456 gboolean
457 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
458 {
459   return gst_element_register (plugin, "rtpamrdepay",
460       GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_DEPAY);
461 }