5a3c7f5f9f09d87d0b81bb0e99d89f8e799976d7
[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 (GstBaseRTPDepayload * depayload,
110     GstCaps * caps);
111 static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
112     GstBuffer * buf);
113
114 GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
115     GST_TYPE_BASE_RTP_DEPAYLOAD);
116
117 static void
118 gst_rtp_amr_depay_base_init (gpointer klass)
119 {
120   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
121
122   gst_element_class_add_pad_template (element_class,
123       gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
124   gst_element_class_add_pad_template (element_class,
125       gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
126
127   gst_element_class_set_details_simple (element_class, "RTP AMR depayloader",
128       "Codec/Depayloader/Network",
129       "Extracts AMR or AMR-WB audio from RTP packets (RFC 3267)",
130       "Wim Taymans <wim.taymans@gmail.com>");
131 }
132
133 static void
134 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
135 {
136   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
137
138   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
139
140   gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
141   gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
142
143   GST_DEBUG_CATEGORY_INIT (rtpamrdepay_debug, "rtpamrdepay", 0,
144       "AMR/AMR-WB RTP Depayloader");
145 }
146
147 static void
148 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay,
149     GstRtpAMRDepayClass * klass)
150 {
151   GstBaseRTPDepayload *depayload;
152
153   depayload = GST_BASE_RTP_DEPAYLOAD (rtpamrdepay);
154
155   gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
156 }
157
158 static gboolean
159 gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
160 {
161   GstStructure *structure;
162   GstCaps *srccaps;
163   GstRtpAMRDepay *rtpamrdepay;
164   const gchar *params;
165   const gchar *str, *type;
166   gint clock_rate, need_clock_rate;
167   gboolean res;
168
169   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
170
171   structure = gst_caps_get_structure (caps, 0);
172
173   /* figure out the mode first and set the clock rates */
174   if ((str = gst_structure_get_string (structure, "encoding-name"))) {
175     if (strcmp (str, "AMR") == 0) {
176       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
177       need_clock_rate = 8000;
178       type = "audio/AMR";
179     } else if (strcmp (str, "AMR-WB") == 0) {
180       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
181       need_clock_rate = 16000;
182       type = "audio/AMR-WB";
183     } else
184       goto invalid_mode;
185   } else
186     goto invalid_mode;
187
188   if (!(str = gst_structure_get_string (structure, "octet-align")))
189     rtpamrdepay->octet_align = FALSE;
190   else
191     rtpamrdepay->octet_align = (atoi (str) == 1);
192
193   if (!(str = gst_structure_get_string (structure, "crc")))
194     rtpamrdepay->crc = FALSE;
195   else
196     rtpamrdepay->crc = (atoi (str) == 1);
197
198   if (rtpamrdepay->crc) {
199     /* crc mode implies octet aligned mode */
200     rtpamrdepay->octet_align = TRUE;
201   }
202
203   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
204     rtpamrdepay->robust_sorting = FALSE;
205   else
206     rtpamrdepay->robust_sorting = (atoi (str) == 1);
207
208   if (rtpamrdepay->robust_sorting) {
209     /* robust_sorting mode implies octet aligned mode */
210     rtpamrdepay->octet_align = TRUE;
211   }
212
213   if (!(str = gst_structure_get_string (structure, "interleaving")))
214     rtpamrdepay->interleaving = FALSE;
215   else
216     rtpamrdepay->interleaving = (atoi (str) == 1);
217
218   if (rtpamrdepay->interleaving) {
219     /* interleaving mode implies octet aligned mode */
220     rtpamrdepay->octet_align = TRUE;
221   }
222
223   if (!(params = gst_structure_get_string (structure, "encoding-params")))
224     rtpamrdepay->channels = 1;
225   else {
226     rtpamrdepay->channels = atoi (params);
227   }
228
229   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
230     clock_rate = need_clock_rate;
231   depayload->clock_rate = clock_rate;
232
233   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
234    * no robust sorting, no interleaving for now */
235   if (rtpamrdepay->channels != 1)
236     return FALSE;
237   if (clock_rate != need_clock_rate)
238     return FALSE;
239   if (rtpamrdepay->octet_align != TRUE)
240     return FALSE;
241   if (rtpamrdepay->robust_sorting != FALSE)
242     return FALSE;
243   if (rtpamrdepay->interleaving != FALSE)
244     return FALSE;
245
246   srccaps = gst_caps_new_simple (type,
247       "channels", G_TYPE_INT, rtpamrdepay->channels,
248       "rate", G_TYPE_INT, clock_rate, NULL);
249   res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
250   gst_caps_unref (srccaps);
251
252   return res;
253
254   /* ERRORS */
255 invalid_mode:
256   {
257     GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
258     return FALSE;
259   }
260 }
261
262 /* -1 is invalid */
263 static gint nb_frame_size[16] = {
264   12, 13, 15, 17, 19, 20, 26, 31,
265   5, -1, -1, -1, -1, -1, -1, 0
266 };
267
268 static gint wb_frame_size[16] = {
269   17, 23, 32, 36, 40, 46, 50, 58,
270   60, 5, -1, -1, -1, -1, -1, 0
271 };
272
273 static GstBuffer *
274 gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
275 {
276   GstRtpAMRDepay *rtpamrdepay;
277   GstBuffer *outbuf = NULL;
278   gint payload_len;
279   gint *frame_size;
280
281   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
282
283   /* setup frame size pointer */
284   if (rtpamrdepay->mode == GST_RTP_AMR_DP_MODE_NB)
285     frame_size = nb_frame_size;
286   else
287     frame_size = wb_frame_size;
288
289   /* when we get here, 1 channel, 8000/16000 Hz, octet aligned, no CRC,
290    * no robust sorting, no interleaving data is to be depayloaded */
291   {
292     guint8 *payload, *p, *dp;
293     guint8 CMR;
294     gint i, num_packets, num_nonempty_packets;
295     gint amr_len;
296     gint ILL, ILP;
297
298     payload_len = gst_rtp_buffer_get_payload_len (buf);
299
300     /* need at least 2 bytes for the header */
301     if (payload_len < 2)
302       goto too_small;
303
304     payload = gst_rtp_buffer_get_payload (buf);
305
306     /* depay CMR. The CMR is used by the sender to request
307      * a new encoding mode.
308      *
309      *  0 1 2 3 4 5 6 7
310      * +-+-+-+-+-+-+-+-+
311      * | CMR   |R|R|R|R|
312      * +-+-+-+-+-+-+-+-+
313      */
314     CMR = (payload[0] & 0xf0) >> 4;
315
316     /* strip CMR header now, pack FT and the data for the decoder */
317     payload_len -= 1;
318     payload += 1;
319
320     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
321
322     if (rtpamrdepay->interleaving) {
323       ILL = (payload[0] & 0xf0) >> 4;
324       ILP = (payload[0] & 0x0f);
325
326       payload_len -= 1;
327       payload += 1;
328
329       if (ILP > ILL)
330         goto wrong_interleaving;
331     }
332
333     /*
334      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
335      * +-+-+-+-+-+-+-+-+..
336      * |F|  FT   |Q|P|P| more FT..
337      * +-+-+-+-+-+-+-+-+..
338      */
339     /* count number of packets by counting the FTs. Also
340      * count number of amr data bytes and number of non-empty
341      * packets (this is also the number of CRCs if present). */
342     amr_len = 0;
343     num_nonempty_packets = 0;
344     num_packets = 0;
345     for (i = 0; i < payload_len; i++) {
346       gint fr_size;
347       guint8 FT;
348
349       FT = (payload[i] & 0x78) >> 3;
350
351       fr_size = frame_size[FT];
352       GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
353       if (fr_size == -1)
354         goto wrong_framesize;
355
356       if (fr_size > 0) {
357         amr_len += fr_size;
358         num_nonempty_packets++;
359       }
360       num_packets++;
361
362       if ((payload[i] & 0x80) == 0)
363         break;
364     }
365
366     if (rtpamrdepay->crc) {
367       /* data len + CRC len + header bytes should be smaller than payload_len */
368       if (num_packets + num_nonempty_packets + amr_len > payload_len)
369         goto wrong_length_1;
370     } else {
371       /* data len + header bytes should be smaller than payload_len */
372       if (num_packets + amr_len > payload_len)
373         goto wrong_length_2;
374     }
375
376     outbuf = gst_buffer_new_and_alloc (payload_len);
377
378     /* point to destination */
379     p = GST_BUFFER_DATA (outbuf);
380     /* point to first data packet */
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     /* we can set the duration because each packet is 20 milliseconds */
403     GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
404
405     if (gst_rtp_buffer_get_marker (buf)) {
406       /* marker bit marks a discont buffer after a talkspurt. */
407       GST_DEBUG_OBJECT (depayload, "marker bit was set");
408       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
409     }
410
411     GST_DEBUG_OBJECT (depayload, "pushing buffer of size %d",
412         GST_BUFFER_SIZE (outbuf));
413   }
414   return outbuf;
415
416   /* ERRORS */
417 too_small:
418   {
419     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
420         (NULL), ("AMR RTP payload too small (%d)", payload_len));
421     goto bad_packet;
422   }
423 wrong_interleaving:
424   {
425     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
426         (NULL), ("AMR RTP wrong interleaving"));
427     goto bad_packet;
428   }
429 wrong_framesize:
430   {
431     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
432         (NULL), ("AMR RTP frame size == -1"));
433     goto bad_packet;
434   }
435 wrong_length_1:
436   {
437     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
438         (NULL), ("AMR RTP wrong length 1"));
439     goto bad_packet;
440   }
441 wrong_length_2:
442   {
443     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
444         (NULL), ("AMR RTP wrong length 2"));
445     goto bad_packet;
446   }
447 bad_packet:
448   {
449     /* no fatal error */
450     return NULL;
451   }
452 }
453
454 gboolean
455 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
456 {
457   return gst_element_register (plugin, "rtpamrdepay",
458       GST_RANK_MARGINAL, GST_TYPE_RTP_AMR_DEPAY);
459 }