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