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