webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-rtpamrdepay
22  * @title: rtpamrdepay
23  * @see_also: rtpamrpay
24  *
25  * Extract AMR audio from RTP packets according to RFC 3267.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3267.txt
27  *
28  * ## Example pipeline
29  * |[
30  * gst-launch-1.0 udpsrc caps='application/x-rtp, media=(string)audio, clock-rate=(int)8000, encoding-name=(string)AMR, encoding-params=(string)1, octet-align=(string)1, payload=(int)96' ! rtpamrdepay ! amrnbdec ! pulsesink
31  * ]| This example pipeline will depayload and decode an RTP AMR stream. Refer to
32  * the rtpamrpay example to create the RTP stream.
33  *
34  */
35
36 /*
37  * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
38  * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
39  * Wideband (AMR-WB) Audio Codecs.
40  *
41  */
42 #ifdef HAVE_CONFIG_H
43 #  include "config.h"
44 #endif
45
46 #include <gst/rtp/gstrtpbuffer.h>
47 #include <gst/audio/audio.h>
48
49 #include <stdlib.h>
50 #include <string.h>
51 #include "gstrtpelements.h"
52 #include "gstrtpamrdepay.h"
53 #include "gstrtputils.h"
54
55 GST_DEBUG_CATEGORY_STATIC (rtpamrdepay_debug);
56 #define GST_CAT_DEFAULT (rtpamrdepay_debug)
57
58 /* RtpAMRDepay signals and args */
59 enum
60 {
61   /* FILL ME */
62   LAST_SIGNAL
63 };
64
65 enum
66 {
67   PROP_0
68 };
69
70 /* input is an RTP packet
71  *
72  * params see RFC 3267, section 8.1
73  */
74 static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
75     GST_STATIC_PAD_TEMPLATE ("sink",
76     GST_PAD_SINK,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("application/x-rtp, "
79         "media = (string) \"audio\", "
80         "clock-rate = (int) 8000, " "encoding-name = (string) \"AMR\", "
81         /* This is the default, so the peer doesn't have to specify it
82          * "encoding-params = (string) \"1\", " */
83         /* NOTE that all values must be strings in orde to be able to do SDP <->
84          * GstCaps mapping. */
85         "octet-align = (string) \"1\";"
86         /* following options are not needed for a decoder
87          *
88          "crc = (string) { \"0\", \"1\" }, "
89          "robust-sorting = (string) \"0\", "
90          "interleaving = (string) \"0\";"
91          "mode-set = (int) [ 0, 7 ], "
92          "mode-change-period = (int) [ 1, MAX ], "
93          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
94          "maxptime = (int) [ 20, MAX ], "
95          "ptime = (int) [ 20, MAX ]"
96          */
97         "application/x-rtp, "
98         "media = (string) \"audio\", "
99         "clock-rate = (int) 16000, " "encoding-name = (string) \"AMR-WB\", "
100         /* This is the default, so the peer doesn't have to specify it
101          * "encoding-params = (string) \"1\", " */
102         /* NOTE that all values must be strings in orde to be able to do SDP <->
103          * GstCaps mapping. */
104         "octet-align = (string) \"1\";"
105         /* following options are not needed for a decoder
106          *
107          "crc = (string) { \"0\", \"1\" }, "
108          "robust-sorting = (string) \"0\", "
109          "interleaving = (string) \"0\""
110          "mode-set = (int) [ 0, 7 ], "
111          "mode-change-period = (int) [ 1, MAX ], "
112          "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
113          "maxptime = (int) [ 20, MAX ], "
114          "ptime = (int) [ 20, MAX ]"
115          */
116     )
117     );
118
119 static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
120     GST_STATIC_PAD_TEMPLATE ("src",
121     GST_PAD_SRC,
122     GST_PAD_ALWAYS,
123     GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000;"
124         "audio/AMR-WB, " "channels = (int) 1," "rate = (int) 16000")
125     );
126
127 static gboolean gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload,
128     GstCaps * caps);
129 static GstBuffer *gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload,
130     GstRTPBuffer * rtp);
131
132 #define gst_rtp_amr_depay_parent_class parent_class
133 G_DEFINE_TYPE (GstRtpAMRDepay, gst_rtp_amr_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
134 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpamrdepay, "rtpamrdepay",
135     GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_DEPAY, rtp_element_init (plugin));
136
137 static void
138 gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
139 {
140   GstElementClass *gstelement_class;
141   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
142
143   gstelement_class = (GstElementClass *) klass;
144   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
145
146   gst_element_class_add_static_pad_template (gstelement_class,
147       &gst_rtp_amr_depay_src_template);
148   gst_element_class_add_static_pad_template (gstelement_class,
149       &gst_rtp_amr_depay_sink_template);
150
151   gst_element_class_set_static_metadata (gstelement_class,
152       "RTP AMR depayloader", "Codec/Depayloader/Network/RTP",
153       "Extracts AMR or AMR-WB audio from RTP packets (RFC 3267)",
154       "Wim Taymans <wim.taymans@gmail.com>");
155
156   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_amr_depay_process;
157   gstrtpbasedepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
158
159   GST_DEBUG_CATEGORY_INIT (rtpamrdepay_debug, "rtpamrdepay", 0,
160       "AMR/AMR-WB RTP Depayloader");
161 }
162
163 static void
164 gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay)
165 {
166   GstRTPBaseDepayload *depayload;
167
168   depayload = GST_RTP_BASE_DEPAYLOAD (rtpamrdepay);
169
170   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
171 }
172
173 static gboolean
174 gst_rtp_amr_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
175 {
176   GstStructure *structure;
177   GstCaps *srccaps;
178   GstRtpAMRDepay *rtpamrdepay;
179   const gchar *params;
180   const gchar *str, *type;
181   gint clock_rate, need_clock_rate;
182   gboolean res;
183
184   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
185
186   structure = gst_caps_get_structure (caps, 0);
187
188   /* figure out the mode first and set the clock rates */
189   if ((str = gst_structure_get_string (structure, "encoding-name"))) {
190     if (strcmp (str, "AMR") == 0) {
191       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_NB;
192       need_clock_rate = 8000;
193       type = "audio/AMR";
194     } else if (strcmp (str, "AMR-WB") == 0) {
195       rtpamrdepay->mode = GST_RTP_AMR_DP_MODE_WB;
196       need_clock_rate = 16000;
197       type = "audio/AMR-WB";
198     } else
199       goto invalid_mode;
200   } else
201     goto invalid_mode;
202
203   if (!(str = gst_structure_get_string (structure, "octet-align")))
204     rtpamrdepay->octet_align = FALSE;
205   else
206     rtpamrdepay->octet_align = (atoi (str) == 1);
207
208   if (!(str = gst_structure_get_string (structure, "crc")))
209     rtpamrdepay->crc = FALSE;
210   else
211     rtpamrdepay->crc = (atoi (str) == 1);
212
213   if (rtpamrdepay->crc) {
214     /* crc mode implies octet aligned mode */
215     rtpamrdepay->octet_align = TRUE;
216   }
217
218   if (!(str = gst_structure_get_string (structure, "robust-sorting")))
219     rtpamrdepay->robust_sorting = FALSE;
220   else
221     rtpamrdepay->robust_sorting = (atoi (str) == 1);
222
223   if (rtpamrdepay->robust_sorting) {
224     /* robust_sorting mode implies octet aligned mode */
225     rtpamrdepay->octet_align = TRUE;
226   }
227
228   if (!(str = gst_structure_get_string (structure, "interleaving")))
229     rtpamrdepay->interleaving = FALSE;
230   else
231     rtpamrdepay->interleaving = (atoi (str) == 1);
232
233   if (rtpamrdepay->interleaving) {
234     /* interleaving mode implies octet aligned mode */
235     rtpamrdepay->octet_align = TRUE;
236   }
237
238   if (!(params = gst_structure_get_string (structure, "encoding-params")))
239     rtpamrdepay->channels = 1;
240   else {
241     rtpamrdepay->channels = atoi (params);
242   }
243
244   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
245     clock_rate = need_clock_rate;
246   depayload->clock_rate = clock_rate;
247
248   /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
249    * no robust sorting, no interleaving for now */
250   if (rtpamrdepay->channels != 1)
251     return FALSE;
252   if (clock_rate != need_clock_rate)
253     return FALSE;
254   if (rtpamrdepay->octet_align != TRUE)
255     return FALSE;
256   if (rtpamrdepay->robust_sorting != FALSE)
257     return FALSE;
258   if (rtpamrdepay->interleaving != FALSE)
259     return FALSE;
260
261   srccaps = gst_caps_new_simple (type,
262       "channels", G_TYPE_INT, rtpamrdepay->channels,
263       "rate", G_TYPE_INT, clock_rate, NULL);
264   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
265   gst_caps_unref (srccaps);
266
267   return res;
268
269   /* ERRORS */
270 invalid_mode:
271   {
272     GST_ERROR_OBJECT (rtpamrdepay, "invalid encoding-name");
273     return FALSE;
274   }
275 }
276
277 /* -1 is invalid */
278 static const gint nb_frame_size[16] = {
279   12, 13, 15, 17, 19, 20, 26, 31,
280   5, -1, -1, -1, -1, -1, -1, 0
281 };
282
283 static const gint wb_frame_size[16] = {
284   17, 23, 32, 36, 40, 46, 50, 58,
285   60, 5, -1, -1, -1, -1, -1, 0
286 };
287
288 static GstBuffer *
289 gst_rtp_amr_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
290 {
291   GstRtpAMRDepay *rtpamrdepay;
292   const gint *frame_size;
293   GstBuffer *outbuf = NULL;
294   gint payload_len;
295   GstMapInfo map;
296
297   rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
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     gint i, num_packets, num_nonempty_packets;
310     gint amr_len;
311     gint ILL, ILP;
312
313     payload_len = gst_rtp_buffer_get_payload_len (rtp);
314
315     /* need at least 2 bytes for the header */
316     if (payload_len < 2)
317       goto too_small;
318
319     payload = gst_rtp_buffer_get_payload (rtp);
320
321     /* depay CMR. The CMR is used by the sender to request
322      * a new encoding mode.
323      *
324      *  0 1 2 3 4 5 6 7
325      * +-+-+-+-+-+-+-+-+
326      * | CMR   |R|R|R|R|
327      * +-+-+-+-+-+-+-+-+
328      */
329     /* CMR = (payload[0] & 0xf0) >> 4; */
330
331     /* strip CMR header now, pack FT and the data for the decoder */
332     payload_len -= 1;
333     payload += 1;
334
335     GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);
336
337     if (rtpamrdepay->interleaving) {
338       ILL = (payload[0] & 0xf0) >> 4;
339       ILP = (payload[0] & 0x0f);
340
341       payload_len -= 1;
342       payload += 1;
343
344       if (ILP > ILL)
345         goto wrong_interleaving;
346     }
347
348     /*
349      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
350      * +-+-+-+-+-+-+-+-+..
351      * |F|  FT   |Q|P|P| more FT..
352      * +-+-+-+-+-+-+-+-+..
353      */
354     /* count number of packets by counting the FTs. Also
355      * count number of amr data bytes and number of non-empty
356      * packets (this is also the number of CRCs if present). */
357     amr_len = 0;
358     num_nonempty_packets = 0;
359     num_packets = 0;
360     for (i = 0; i < payload_len; i++) {
361       gint fr_size;
362       guint8 FT;
363
364       FT = (payload[i] & 0x78) >> 3;
365
366       fr_size = frame_size[FT];
367       GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
368       if (fr_size == -1)
369         goto wrong_framesize;
370
371       if (fr_size > 0) {
372         amr_len += fr_size;
373         num_nonempty_packets++;
374       }
375       num_packets++;
376
377       if ((payload[i] & 0x80) == 0)
378         break;
379     }
380
381     if (rtpamrdepay->crc) {
382       /* data len + CRC len + header bytes should be smaller than payload_len */
383       if (num_packets + num_nonempty_packets + amr_len > payload_len)
384         goto wrong_length_1;
385     } else {
386       /* data len + header bytes should be smaller than payload_len */
387       if (num_packets + amr_len > payload_len)
388         goto wrong_length_2;
389     }
390
391     outbuf = gst_buffer_new_and_alloc (payload_len);
392
393     /* point to destination */
394     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
395
396     /* point to first data packet */
397     p = map.data;
398     dp = payload + num_packets;
399     if (rtpamrdepay->crc) {
400       /* skip CRC if present */
401       dp += num_nonempty_packets;
402     }
403
404     for (i = 0; i < num_packets; i++) {
405       gint fr_size;
406
407       /* copy FT, clear F bit */
408       *p++ = payload[i] & 0x7f;
409
410       fr_size = frame_size[(payload[i] & 0x78) >> 3];
411       if (fr_size > 0) {
412         /* copy data packet, FIXME, calc CRC here. */
413         memcpy (p, dp, fr_size);
414
415         p += fr_size;
416         dp += fr_size;
417       }
418     }
419     gst_buffer_unmap (outbuf, &map);
420
421     /* we can set the duration because each packet is 20 milliseconds */
422     GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
423
424     if (gst_rtp_buffer_get_marker (rtp)) {
425       /* marker bit marks a buffer after a talkspurt. */
426       GST_DEBUG_OBJECT (depayload, "marker bit was set");
427       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
428     }
429
430     GST_DEBUG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
431         gst_buffer_get_size (outbuf));
432
433     gst_rtp_copy_audio_meta (rtpamrdepay, outbuf, rtp->buffer);
434   }
435
436   return outbuf;
437
438   /* ERRORS */
439 too_small:
440   {
441     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
442         (NULL), ("AMR RTP payload too small (%d)", payload_len));
443     goto bad_packet;
444   }
445 wrong_interleaving:
446   {
447     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
448         (NULL), ("AMR RTP wrong interleaving"));
449     goto bad_packet;
450   }
451 wrong_framesize:
452   {
453     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
454         (NULL), ("AMR RTP frame size == -1"));
455     goto bad_packet;
456   }
457 wrong_length_1:
458   {
459     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
460         (NULL), ("AMR RTP wrong length 1"));
461     goto bad_packet;
462   }
463 wrong_length_2:
464   {
465     GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
466         (NULL), ("AMR RTP wrong length 2"));
467     goto bad_packet;
468   }
469 bad_packet:
470   {
471     /* no fatal error */
472     return NULL;
473   }
474 }