rtp: use boilerplate
[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 /* elementfactory information */
41 static const GstElementDetails gst_rtp_amrdepay_details =
42 GST_ELEMENT_DETAILS ("RTP AMR depayloader",
43     "Codec/Depayloader/Network",
44     "Extracts AMR or AMR-WB audio from RTP packets (RFC 3267)",
45     "Wim Taymans <wim.taymans@gmail.com>");
46
47 /* RtpAMRDepay signals and args */
48 enum
49 {
50   /* FILL ME */
51   LAST_SIGNAL
52 };
53
54 enum
55 {
56   ARG_0
57 };
58
59 /* input is an RTP packet
60  *
61  * params see RFC 3267, section 8.1
62  */
63 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
64     GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("application/x-rtp, "
68         "media = (string) \"audio\", "
69         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
70         "clock-rate = (int) 8000, "
71         "encoding-name = (string) \"AMR\", "
72         "encoding-params = (string) \"1\", "
73         /* NOTE that all values must be strings in orde to be able to do SDP <->
74          * GstCaps mapping. */
75         "octet-align = (string) \"1\", "
76         "crc = (string) { \"0\", \"1\" }, "
77         "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\";"
78         /* following options are not needed for a decoder
79          *
80          "mode-set = (int) [ 0, 7 ], "
81          "mode-change-period = (int) [ 1, MAX ], "
82          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
83          "maxptime = (int) [ 20, MAX ], "
84          "ptime = (int) [ 20, MAX ]"
85          */
86         "application/x-rtp, "
87         "media = (string) \"audio\", "
88         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
89         "clock-rate = (int) 16000, "
90         "encoding-name = (string) \"AMR-WB\", "
91         "encoding-params = (string) \"1\", "
92         /* NOTE that all values must be strings in orde to be able to do SDP <->
93          * GstCaps mapping. */
94         "octet-align = (string) \"1\", "
95         "crc = (string) { \"0\", \"1\" }, "
96         "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
97         /* following options are not needed for a decoder
98          *
99          "mode-set = (int) [ 0, 7 ], "
100          "mode-change-period = (int) [ 1, MAX ], "
101          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
102          "maxptime = (int) [ 20, MAX ], "
103          "ptime = (int) [ 20, MAX ]"
104          */
105     )
106     );
107
108 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
109     GST_STATIC_PAD_TEMPLATE ("src",
110     GST_PAD_SRC,
111     GST_PAD_ALWAYS,
112     GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000;"
113         "audio/AMR-WB, " "channels = (int) 1," "rate = (int) 16000")
114     );
115
116 static gboolean gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload,
117     GstCaps * caps);
118 static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
119     GstBuffer * buf);
120
121 GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
122     GST_TYPE_BASE_RTP_DEPAYLOAD);
123
124 static void
125 gst_rtp_amr_depay_base_init (gpointer klass)
126 {
127   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
128
129   gst_element_class_add_pad_template (element_class,
130       gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
131   gst_element_class_add_pad_template (element_class,
132       gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
133
134   gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
135 }
136
137 static void
138 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
139 {
140   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
141
142   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
143
144   gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
145   gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
146
147   GST_DEBUG_CATEGORY_INIT (rtpamrdepay_debug, "rtpamrdepay", 0,
148       "AMR/AMR-WB RTP Depayloader");
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   gboolean res;
172
173   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
174
175   structure = gst_caps_get_structure (caps, 0);
176
177   /* figure out the mode first and set the clock rates */
178   if ((str = gst_structure_get_string (structure, "encoding-name"))) {
179     if (strcmp (str, "AMR") == 0) {
180       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
181       need_clock_rate = 8000;
182       type = "audio/AMR";
183     } else if (strcmp (str, "AMR-WB") == 0) {
184       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
185       need_clock_rate = 16000;
186       type = "audio/AMR-WB";
187     } else
188       goto invalid_mode;
189   } else
190     goto invalid_mode;
191
192   if (!(str = gst_structure_get_string (structure, "octet-align")))
193     rtpamrdepay->octet_align = FALSE;
194   else
195     rtpamrdepay->octet_align = (atoi (str) == 1);
196
197   if (!(str = gst_structure_get_string (structure, "crc")))
198     rtpamrdepay->crc = FALSE;
199   else
200     rtpamrdepay->crc = (atoi (str) == 1);
201
202   if (rtpamrdepay->crc) {
203     /* crc mode implies octet aligned mode */
204     rtpamrdepay->octet_align = TRUE;
205   }
206
207   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
208     rtpamrdepay->robust_sorting = FALSE;
209   else
210     rtpamrdepay->robust_sorting = (atoi (str) == 1);
211
212   if (rtpamrdepay->robust_sorting) {
213     /* robust_sorting mode implies octet aligned mode */
214     rtpamrdepay->octet_align = TRUE;
215   }
216
217   if (!(str = gst_structure_get_string (structure, "interleaving")))
218     rtpamrdepay->interleaving = FALSE;
219   else
220     rtpamrdepay->interleaving = (atoi (str) == 1);
221
222   if (rtpamrdepay->interleaving) {
223     /* interleaving mode implies octet aligned mode */
224     rtpamrdepay->octet_align = TRUE;
225   }
226
227   if (!(params = gst_structure_get_string (structure, "encoding-params")))
228     rtpamrdepay->channels = 1;
229   else {
230     rtpamrdepay->channels = atoi (params);
231   }
232
233   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
234     clock_rate = need_clock_rate;
235   depayload->clock_rate = clock_rate;
236
237   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
238    * no robust sorting, no interleaving for now */
239   if (rtpamrdepay->channels != 1)
240     return FALSE;
241   if (clock_rate != need_clock_rate)
242     return FALSE;
243   if (rtpamrdepay->octet_align != TRUE)
244     return FALSE;
245   if (rtpamrdepay->robust_sorting != FALSE)
246     return FALSE;
247   if (rtpamrdepay->interleaving != FALSE)
248     return FALSE;
249
250   srccaps = gst_caps_new_simple (type,
251       "channels", G_TYPE_INT, rtpamrdepay->channels,
252       "rate", G_TYPE_INT, clock_rate, NULL);
253   res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
254   gst_caps_unref (srccaps);
255
256   return res;
257
258   /* ERRORS */
259 invalid_mode:
260   {
261     GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
262     return FALSE;
263   }
264 }
265
266 /* -1 is invalid */
267 static gint nb_frame_size[16] = {
268   12, 13, 15, 17, 19, 20, 26, 31,
269   5, -1, -1, -1, -1, -1, -1, 0
270 };
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   /* setup frame size pointer */
288   if (rtpamrdepay->mode == GST_RTP_AMR_DP_MODE_NB)
289     frame_size = nb_frame_size;
290   else
291     frame_size = wb_frame_size;
292
293   /* when we get here, 1 channel, 8000/16000 Hz, octet aligned, no CRC,
294    * no robust sorting, no interleaving data is to be depayloaded */
295   {
296     guint8 *payload, *p, *dp;
297     guint8 CMR;
298     gint i, num_packets, num_nonempty_packets;
299     gint amr_len;
300     gint ILL, ILP;
301
302     payload_len = gst_rtp_buffer_get_payload_len (buf);
303
304     /* need at least 2 bytes for the header */
305     if (payload_len < 2)
306       goto too_small;
307
308     payload = gst_rtp_buffer_get_payload (buf);
309
310     /* depay CMR. The CMR is used by the sender to request
311      * a new encoding mode.
312      *
313      *  0 1 2 3 4 5 6 7
314      * +-+-+-+-+-+-+-+-+
315      * | CMR   |R|R|R|R|
316      * +-+-+-+-+-+-+-+-+
317      */
318     CMR = (payload[0] & 0xf0) >> 4;
319
320     /* strip CMR header now, pack FT and the data for the decoder */
321     payload_len -= 1;
322     payload += 1;
323
324     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
325
326     if (rtpamrdepay->interleaving) {
327       ILL = (payload[0] & 0xf0) >> 4;
328       ILP = (payload[0] & 0x0f);
329
330       payload_len -= 1;
331       payload += 1;
332
333       if (ILP > ILL)
334         goto wrong_interleaving;
335     }
336
337     /*
338      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
339      * +-+-+-+-+-+-+-+-+..
340      * |F|  FT   |Q|P|P| more FT..
341      * +-+-+-+-+-+-+-+-+..
342      */
343     /* count number of packets by counting the FTs. Also
344      * count number of amr data bytes and number of non-empty
345      * packets (this is also the number of CRCs if present). */
346     amr_len = 0;
347     num_nonempty_packets = 0;
348     num_packets = 0;
349     for (i = 0; i < payload_len; i++) {
350       gint fr_size;
351       guint8 FT;
352
353       FT = (payload[i] & 0x78) >> 3;
354
355       fr_size = frame_size[FT];
356       GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
357       if (fr_size == -1)
358         goto wrong_framesize;
359
360       if (fr_size > 0) {
361         amr_len += fr_size;
362         num_nonempty_packets++;
363       }
364       num_packets++;
365
366       if ((payload[i] & 0x80) == 0)
367         break;
368     }
369
370     if (rtpamrdepay->crc) {
371       /* data len + CRC len + header bytes should be smaller than payload_len */
372       if (num_packets + num_nonempty_packets + amr_len > payload_len)
373         goto wrong_length_1;
374     } else {
375       /* data len + header bytes should be smaller than payload_len */
376       if (num_packets + amr_len > payload_len)
377         goto wrong_length_2;
378     }
379
380     outbuf = gst_buffer_new_and_alloc (payload_len);
381
382     /* point to destination */
383     p = GST_BUFFER_DATA (outbuf);
384     /* point to first data packet */
385     dp = payload + num_packets;
386     if (rtpamrdepay->crc) {
387       /* skip CRC if present */
388       dp += num_nonempty_packets;
389     }
390
391     for (i = 0; i < num_packets; i++) {
392       gint fr_size;
393
394       /* copy FT, clear F bit */
395       *p++ = payload[i] & 0x7f;
396
397       fr_size = frame_size[(payload[i] & 0x78) >> 3];
398       if (fr_size > 0) {
399         /* copy data packet, FIXME, calc CRC here. */
400         memcpy (p, dp, fr_size);
401
402         p += fr_size;
403         dp += fr_size;
404       }
405     }
406     /* we can set the duration because each packet is 20 milliseconds */
407     GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
408
409     if (gst_rtp_buffer_get_marker (buf)) {
410       /* marker bit marks a discont buffer after a talkspurt. */
411       GST_DEBUG_OBJECT (depayload, "marker bit was set");
412       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
413     }
414
415     GST_DEBUG_OBJECT (depayload, "pushing buffer of size %d",
416         GST_BUFFER_SIZE (outbuf));
417   }
418   return outbuf;
419
420   /* ERRORS */
421 too_small:
422   {
423     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
424         (NULL), ("AMR RTP payload too small (%d)", payload_len));
425     goto bad_packet;
426   }
427 wrong_interleaving:
428   {
429     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
430         (NULL), ("AMR RTP wrong interleaving"));
431     goto bad_packet;
432   }
433 wrong_framesize:
434   {
435     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
436         (NULL), ("AMR RTP frame size == -1"));
437     goto bad_packet;
438   }
439 wrong_length_1:
440   {
441     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
442         (NULL), ("AMR RTP wrong length 1"));
443     goto bad_packet;
444   }
445 wrong_length_2:
446   {
447     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
448         (NULL), ("AMR RTP wrong length 2"));
449     goto bad_packet;
450   }
451 bad_packet:
452   {
453     /* no fatal error */
454     return NULL;
455   }
456 }
457
458 gboolean
459 gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
460 {
461   return gst_element_register (plugin, "rtpamrdepay",
462       GST_RANK_MARGINAL, GST_TYPE_RTP_AMR_DEPAY);
463 }