webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpqcelpdepay.c
1 /* GStreamer
2  * Copyright (C) <2010> 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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gst/rtp/gstrtpbuffer.h>
25 #include <gst/audio/audio.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gstrtpelements.h"
30 #include "gstrtpqcelpdepay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpqcelpdepay_debug);
34 #define GST_CAT_DEFAULT (rtpqcelpdepay_debug)
35
36 /* references:
37  *
38  * RFC 2658 - RTP Payload Format for PureVoice(tm) Audio
39  */
40 #define FRAME_DURATION (20 * GST_MSECOND)
41
42 /* RtpQCELPDepay signals and args */
43 enum
44 {
45   /* FILL ME */
46   LAST_SIGNAL
47 };
48
49 enum
50 {
51   PROP_0
52 };
53
54 static GstStaticPadTemplate gst_rtp_qcelp_depay_sink_template =
55     GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("application/x-rtp, "
59         "media = (string) \"audio\", "
60         "clock-rate = (int) 8000, "
61         "encoding-name = (string) \"QCELP\"; "
62         "application/x-rtp, "
63         "media = (string) \"audio\", "
64         "payload = (int) " GST_RTP_PAYLOAD_QCELP_STRING ", "
65         "clock-rate = (int) 8000")
66     );
67
68 static GstStaticPadTemplate gst_rtp_qcelp_depay_src_template =
69 GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS ("audio/qcelp, " "channels = (int) 1," "rate = (int) 8000")
73     );
74
75 static void gst_rtp_qcelp_depay_finalize (GObject * object);
76
77 static gboolean gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload,
78     GstCaps * caps);
79 static GstBuffer *gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
80     GstRTPBuffer * rtp);
81
82 #define gst_rtp_qcelp_depay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpQCELPDepay, gst_rtp_qcelp_depay,
84     GST_TYPE_RTP_BASE_DEPAYLOAD);
85 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpqcelpdepay, "rtpqcelpdepay",
86     GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY, rtp_element_init (plugin));
87
88 static void
89 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
90 {
91   GObjectClass *gobject_class;
92   GstElementClass *gstelement_class;
93   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
94
95   gobject_class = (GObjectClass *) klass;
96   gstelement_class = (GstElementClass *) klass;
97   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
98
99   gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
100
101   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qcelp_depay_process;
102   gstrtpbasedepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
103
104   gst_element_class_add_static_pad_template (gstelement_class,
105       &gst_rtp_qcelp_depay_src_template);
106   gst_element_class_add_static_pad_template (gstelement_class,
107       &gst_rtp_qcelp_depay_sink_template);
108
109   gst_element_class_set_static_metadata (gstelement_class,
110       "RTP QCELP depayloader", "Codec/Depayloader/Network/RTP",
111       "Extracts QCELP (PureVoice) audio from RTP packets (RFC 2658)",
112       "Wim Taymans <wim.taymans@gmail.com>");
113
114   GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
115       "QCELP RTP Depayloader");
116 }
117
118 static void
119 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay)
120 {
121 }
122
123 static void
124 gst_rtp_qcelp_depay_finalize (GObject * object)
125 {
126   GstRtpQCELPDepay *depay;
127
128   depay = GST_RTP_QCELP_DEPAY (object);
129
130   if (depay->packets != NULL) {
131     g_ptr_array_foreach (depay->packets, (GFunc) gst_buffer_unref, NULL);
132     g_ptr_array_free (depay->packets, TRUE);
133     depay->packets = NULL;
134   }
135
136   G_OBJECT_CLASS (parent_class)->finalize (object);
137 }
138
139
140 static gboolean
141 gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
142 {
143   GstCaps *srccaps;
144   gboolean res;
145
146   srccaps = gst_caps_new_simple ("audio/qcelp",
147       "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
148   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
149   gst_caps_unref (srccaps);
150
151   return res;
152 }
153
154 static const gint frame_size[16] = {
155   1, 4, 8, 17, 35, -8, 0, 0,
156   0, 0, 0, 0, 0, 0, 1, 0
157 };
158
159 /* get the frame length, 0 is invalid, negative values are invalid but can be
160  * recovered from. */
161 static gint
162 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
163 {
164   if (frame_type >= G_N_ELEMENTS (frame_size))
165     return 0;
166
167   return frame_size[frame_type];
168 }
169
170 static guint
171 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
172 {
173   guint count = 0;
174
175   while (size > 0) {
176     gint frame_len;
177
178     frame_len = get_frame_len (depay, data[0]);
179
180     /* 0 is invalid and we throw away the remainder of the frames */
181     if (frame_len == 0)
182       break;
183
184     if (frame_len < 0)
185       frame_len = -frame_len;
186
187     if (frame_len > size)
188       break;
189
190     size -= frame_len;
191     data += frame_len;
192     count++;
193   }
194   return count;
195 }
196
197 static void
198 flush_packets (GstRtpQCELPDepay * depay)
199 {
200   guint i, size;
201
202   GST_DEBUG_OBJECT (depay, "flushing packets");
203
204   size = depay->packets->len;
205
206   for (i = 0; i < size; i++) {
207     GstBuffer *outbuf;
208
209     outbuf = g_ptr_array_index (depay->packets, i);
210     g_ptr_array_index (depay->packets, i) = NULL;
211
212     gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (depay), outbuf);
213   }
214
215   /* and reset interleaving state */
216   depay->interleaved = FALSE;
217   depay->bundling = 0;
218 }
219
220 static void
221 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
222     GstBuffer * outbuf)
223 {
224   guint idx;
225   GstBuffer *old;
226
227   /* figure out the position in the array, note that index is never 0 because we
228    * push those packets immediately. */
229   idx = NNN + ((LLL + 1) * (index - 1));
230
231   GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
232   /* free old buffer (should not happen) */
233   old = g_ptr_array_index (depay->packets, idx);
234   if (old)
235     gst_buffer_unref (old);
236
237   /* store new buffer */
238   g_ptr_array_index (depay->packets, idx) = outbuf;
239 }
240
241 static GstBuffer *
242 create_erasure_buffer (GstRtpQCELPDepay * depay)
243 {
244   GstBuffer *outbuf;
245   GstMapInfo map;
246
247   outbuf = gst_buffer_new_and_alloc (1);
248   gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
249   map.data[0] = 14;
250   gst_buffer_unmap (outbuf, &map);
251
252   return outbuf;
253 }
254
255 static GstBuffer *
256 gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
257     GstRTPBuffer * rtp)
258 {
259   GstRtpQCELPDepay *depay;
260   GstBuffer *outbuf;
261   GstClockTime timestamp;
262   guint payload_len, offset, index;
263   guint8 *payload;
264   guint LLL, NNN;
265
266   depay = GST_RTP_QCELP_DEPAY (depayload);
267
268   payload_len = gst_rtp_buffer_get_payload_len (rtp);
269
270   if (payload_len < 2)
271     goto too_small;
272
273   timestamp = GST_BUFFER_PTS (rtp->buffer);
274
275   payload = gst_rtp_buffer_get_payload (rtp);
276
277   /*  0 1 2 3 4 5 6 7
278    * +-+-+-+-+-+-+-+-+
279    * |RR | LLL | NNN |
280    * +-+-+-+-+-+-+-+-+
281    */
282   /* RR = payload[0] >> 6; */
283   LLL = (payload[0] & 0x38) >> 3;
284   NNN = (payload[0] & 0x07);
285
286   payload_len--;
287   payload++;
288
289   GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
290
291   if (LLL > 5)
292     goto invalid_lll;
293
294   if (NNN > LLL)
295     goto invalid_nnn;
296
297   if (LLL != 0) {
298     /* we are interleaved */
299     if (!depay->interleaved) {
300       guint size;
301
302       GST_DEBUG_OBJECT (depay, "starting interleaving group");
303       /* bundling is not allowed to change in one interleave group */
304       depay->bundling = count_packets (depay, payload, payload_len);
305       GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
306       /* we have one bundle where NNN goes from 0 to L, we don't store the index
307        * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
308       size = (depay->bundling - 1) * (LLL + 1);
309       /* create the array to hold the packets */
310       if (depay->packets == NULL)
311         depay->packets = g_ptr_array_sized_new (size);
312       GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
313       g_ptr_array_set_size (depay->packets, size);
314       /* we were previously not interleaved, figure out how much space we
315        * need to deinterleave */
316       depay->interleaved = TRUE;
317     }
318   } else {
319     /* we are not interleaved */
320     if (depay->interleaved) {
321       GST_DEBUG_OBJECT (depay, "stopping interleaving");
322       /* flush packets if we were previously interleaved */
323       flush_packets (depay);
324     }
325     depay->bundling = 0;
326   }
327
328   index = 0;
329   offset = 1;
330
331   while (payload_len > 0) {
332     gint frame_len;
333     gboolean do_erasure;
334
335     frame_len = get_frame_len (depay, payload[0]);
336     GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
337
338     if (frame_len == 0)
339       goto invalid_frame;
340
341     if (frame_len < 0) {
342       /* need to add an erasure frame but we can recover */
343       frame_len = -frame_len;
344       do_erasure = TRUE;
345     } else {
346       do_erasure = FALSE;
347     }
348
349     if (frame_len > payload_len)
350       goto invalid_frame;
351
352     if (do_erasure) {
353       /* create erasure frame */
354       outbuf = create_erasure_buffer (depay);
355     } else {
356       /* each frame goes into its buffer */
357       outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, offset, frame_len);
358     }
359
360     GST_BUFFER_PTS (outbuf) = timestamp;
361     GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
362
363     gst_rtp_drop_non_audio_meta (depayload, outbuf);
364
365     if (!depay->interleaved || index == 0) {
366       /* not interleaved or first frame in packet, just push */
367       gst_rtp_base_depayload_push (depayload, outbuf);
368
369       if (timestamp != -1)
370         timestamp += FRAME_DURATION;
371     } else {
372       /* put in interleave buffer */
373       add_packet (depay, LLL, NNN, index, outbuf);
374
375       if (timestamp != -1)
376         timestamp += (FRAME_DURATION * (LLL + 1));
377     }
378
379     payload_len -= frame_len;
380     payload += frame_len;
381     offset += frame_len;
382     index++;
383
384     /* discard excess packets */
385     if (depay->bundling > 0 && depay->bundling <= index)
386       break;
387   }
388   while (index < depay->bundling) {
389     GST_DEBUG_OBJECT (depay, "filling with erasure buffer");
390     /* fill remainder with erasure packets */
391     outbuf = create_erasure_buffer (depay);
392     add_packet (depay, LLL, NNN, index, outbuf);
393     index++;
394   }
395   if (depay->interleaved && LLL == NNN) {
396     GST_DEBUG_OBJECT (depay, "interleave group ended, flushing");
397     /* we have the complete interleave group, flush */
398     flush_packets (depay);
399   }
400
401   return NULL;
402
403   /* ERRORS */
404 too_small:
405   {
406     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
407         (NULL), ("QCELP RTP payload too small (%d)", payload_len));
408     return NULL;
409   }
410 invalid_lll:
411   {
412     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
413         (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
414     return NULL;
415   }
416 invalid_nnn:
417   {
418     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
419         (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
420     return NULL;
421   }
422 invalid_frame:
423   {
424     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
425         (NULL), ("QCELP RTP invalid frame received"));
426     return NULL;
427   }
428 }