rtp: mark constant tables as const
[platform/upstream/gst-plugins-good.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 (GstBaseRTPDepayload * depayload,
76     GstCaps * caps);
77 static GstBuffer *gst_rtp_qcelp_depay_process (GstBaseRTPDepayload * depayload,
78     GstBuffer * buf);
79
80 GST_BOILERPLATE (GstRtpQCELPDepay, gst_rtp_qcelp_depay, GstBaseRTPDepayload,
81     GST_TYPE_BASE_RTP_DEPAYLOAD);
82
83 static void
84 gst_rtp_qcelp_depay_base_init (gpointer klass)
85 {
86   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
87
88   gst_element_class_add_pad_template (element_class,
89       gst_static_pad_template_get (&gst_rtp_qcelp_depay_src_template));
90   gst_element_class_add_pad_template (element_class,
91       gst_static_pad_template_get (&gst_rtp_qcelp_depay_sink_template));
92
93   gst_element_class_set_details_simple (element_class, "RTP QCELP depayloader",
94       "Codec/Depayloader/Network",
95       "Extracts QCELP (PureVoice) audio from RTP packets (RFC 2658)",
96       "Wim Taymans <wim.taymans@gmail.com>");
97 }
98
99 static void
100 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
101 {
102   GObjectClass *gobject_class;
103   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
104
105   gobject_class = (GObjectClass *) klass;
106   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
107
108   gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
109
110   gstbasertpdepayload_class->process = gst_rtp_qcelp_depay_process;
111   gstbasertpdepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
112
113   GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
114       "QCELP RTP Depayloader");
115 }
116
117 static void
118 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay,
119     GstRtpQCELPDepayClass * klass)
120 {
121   GstBaseRTPDepayload *depayload;
122
123   depayload = GST_BASE_RTP_DEPAYLOAD (rtpqcelpdepay);
124 }
125
126 static void
127 gst_rtp_qcelp_depay_finalize (GObject * object)
128 {
129   GstRtpQCELPDepay *depay;
130
131   depay = GST_RTP_QCELP_DEPAY (object);
132
133   if (depay->packets != NULL) {
134     g_ptr_array_foreach (depay->packets, (GFunc) gst_buffer_unref, NULL);
135     g_ptr_array_free (depay->packets, TRUE);
136     depay->packets = NULL;
137   }
138
139   G_OBJECT_CLASS (parent_class)->finalize (object);
140 }
141
142
143 static gboolean
144 gst_rtp_qcelp_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
145 {
146   GstCaps *srccaps;
147   GstRtpQCELPDepay *rtpqcelpdepay;
148   gboolean res;
149
150   rtpqcelpdepay = GST_RTP_QCELP_DEPAY (depayload);
151
152   srccaps = gst_caps_new_simple ("audio/qcelp",
153       "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
154   res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
155   gst_caps_unref (srccaps);
156
157   return res;
158 }
159
160 static const gint frame_size[16] = {
161   1, 4, 8, 17, 35, -8, 0, 0,
162   0, 0, 0, 0, 0, 0, 1, 0
163 };
164
165 /* get the frame length, 0 is invalid, negative values are invalid but can be
166  * recovered from. */
167 static gint
168 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
169 {
170   if (frame_type > 16)
171     return 0;
172
173   return frame_size[frame_type];
174 }
175
176 static guint
177 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
178 {
179   guint count = 0;
180
181   while (size > 0) {
182     gint frame_len;
183
184     frame_len = get_frame_len (depay, data[0]);
185
186     /* 0 is invalid and we throw away the remainder of the frames */
187     if (frame_len == 0)
188       break;
189
190     if (frame_len < 0)
191       frame_len = -frame_len;
192
193     if (frame_len > size)
194       break;
195
196     size -= frame_len;
197     data += frame_len;
198     count++;
199   }
200   return count;
201 }
202
203 static void
204 flush_packets (GstRtpQCELPDepay * depay)
205 {
206   guint i, size;
207
208   GST_DEBUG_OBJECT (depay, "flushing packets");
209
210   size = depay->packets->len;
211
212   for (i = 0; i < size; i++) {
213     GstBuffer *outbuf;
214
215     outbuf = g_ptr_array_index (depay->packets, i);
216     g_ptr_array_index (depay->packets, i) = NULL;
217
218     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (depay), outbuf);
219   }
220
221   /* and reset interleaving state */
222   depay->interleaved = FALSE;
223   depay->bundling = 0;
224 }
225
226 static void
227 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
228     GstBuffer * outbuf)
229 {
230   guint idx;
231   GstBuffer *old;
232
233   /* figure out the position in the array, note that index is never 0 because we
234    * push those packets immediately. */
235   idx = NNN + ((LLL + 1) * (index - 1));
236
237   GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
238   /* free old buffer (should not happen) */
239   old = g_ptr_array_index (depay->packets, idx);
240   if (old)
241     gst_buffer_unref (old);
242
243   /* store new buffer */
244   g_ptr_array_index (depay->packets, idx) = outbuf;
245 }
246
247 static GstBuffer *
248 create_erasure_buffer (GstRtpQCELPDepay * depay)
249 {
250   GstBuffer *outbuf;
251
252   outbuf = gst_buffer_new_and_alloc (1);
253   GST_BUFFER_DATA (outbuf)[0] = 14;
254
255   return outbuf;
256 }
257
258 static GstBuffer *
259 gst_rtp_qcelp_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
260 {
261   GstRtpQCELPDepay *depay;
262   GstBuffer *outbuf;
263   GstClockTime timestamp;
264   guint payload_len, offset, index;
265   guint8 *payload;
266   guint RR, LLL, NNN;
267
268   depay = GST_RTP_QCELP_DEPAY (depayload);
269
270   payload_len = gst_rtp_buffer_get_payload_len (buf);
271
272   if (payload_len < 2)
273     goto too_small;
274
275   timestamp = GST_BUFFER_TIMESTAMP (buf);
276
277   payload = gst_rtp_buffer_get_payload (buf);
278
279   /*  0 1 2 3 4 5 6 7
280    * +-+-+-+-+-+-+-+-+
281    * |RR | LLL | NNN |
282    * +-+-+-+-+-+-+-+-+
283    */
284   RR = payload[0] >> 6;
285   LLL = (payload[0] & 0x38) >> 3;
286   NNN = (payload[0] & 0x07);
287
288   payload_len--;
289   payload++;
290
291   GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
292
293   if (LLL > 5)
294     goto invalid_lll;
295
296   if (NNN > LLL)
297     goto invalid_nnn;
298
299   if (LLL != 0) {
300     /* we are interleaved */
301     if (!depay->interleaved) {
302       guint size;
303
304       GST_DEBUG_OBJECT (depay, "starting interleaving group");
305       /* bundling is not allowed to change in one interleave group */
306       depay->bundling = count_packets (depay, payload, payload_len);
307       GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
308       /* we have one bundle where NNN goes from 0 to L, we don't store the index
309        * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
310       size = (depay->bundling - 1) * (LLL + 1);
311       /* create the array to hold the packets */
312       if (depay->packets == NULL)
313         depay->packets = g_ptr_array_sized_new (size);
314       GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
315       g_ptr_array_set_size (depay->packets, size);
316       /* we were previously not interleaved, figure out how much space we
317        * need to deinterleave */
318       depay->interleaved = TRUE;
319     }
320   } else {
321     /* we are not interleaved */
322     if (depay->interleaved) {
323       GST_DEBUG_OBJECT (depay, "stopping interleaving");
324       /* flush packets if we were previously interleaved */
325       flush_packets (depay);
326     }
327     depay->bundling = 0;
328   }
329
330   index = 0;
331   offset = 1;
332
333   while (payload_len > 0) {
334     gint frame_len;
335     gboolean do_erasure;
336
337     frame_len = get_frame_len (depay, payload[0]);
338     GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
339
340     if (frame_len == 0)
341       goto invalid_frame;
342
343     if (frame_len < 0) {
344       /* need to add an erasure frame but we can recover */
345       frame_len = -frame_len;
346       do_erasure = TRUE;
347     } else {
348       do_erasure = FALSE;
349     }
350
351     if (frame_len > payload_len)
352       goto invalid_frame;
353
354     if (do_erasure) {
355       /* create erasure frame */
356       outbuf = create_erasure_buffer (depay);
357     } else {
358       /* each frame goes into its buffer */
359       outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, offset, frame_len);
360     }
361
362     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
363     GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
364
365     if (!depay->interleaved || index == 0) {
366       /* not interleaved or first frame in packet, just push */
367       gst_base_rtp_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 }
429
430 gboolean
431 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
432 {
433   return gst_element_register (plugin, "rtpqcelpdepay",
434       GST_RANK_MARGINAL, GST_TYPE_RTP_QCELP_DEPAY);
435 }