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