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