Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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_static_pad_template (element_class,
89       &gst_rtp_qcelp_depay_src_template);
90   gst_element_class_add_static_pad_template (element_class,
91       &gst_rtp_qcelp_depay_sink_template);
92
93   gst_element_class_set_details_simple (element_class, "RTP QCELP depayloader",
94       "Codec/Depayloader/Network/RTP",
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 G_GNUC_UNUSED *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   gboolean res;
148
149   srccaps = gst_caps_new_simple ("audio/qcelp",
150       "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
151   res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
152   gst_caps_unref (srccaps);
153
154   return res;
155 }
156
157 static const gint frame_size[16] = {
158   1, 4, 8, 17, 35, -8, 0, 0,
159   0, 0, 0, 0, 0, 0, 1, 0
160 };
161
162 /* get the frame length, 0 is invalid, negative values are invalid but can be
163  * recovered from. */
164 static gint
165 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
166 {
167   if (frame_type >= G_N_ELEMENTS (frame_size))
168     return 0;
169
170   return frame_size[frame_type];
171 }
172
173 static guint
174 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
175 {
176   guint count = 0;
177
178   while (size > 0) {
179     gint frame_len;
180
181     frame_len = get_frame_len (depay, data[0]);
182
183     /* 0 is invalid and we throw away the remainder of the frames */
184     if (frame_len == 0)
185       break;
186
187     if (frame_len < 0)
188       frame_len = -frame_len;
189
190     if (frame_len > size)
191       break;
192
193     size -= frame_len;
194     data += frame_len;
195     count++;
196   }
197   return count;
198 }
199
200 static void
201 flush_packets (GstRtpQCELPDepay * depay)
202 {
203   guint i, size;
204
205   GST_DEBUG_OBJECT (depay, "flushing packets");
206
207   size = depay->packets->len;
208
209   for (i = 0; i < size; i++) {
210     GstBuffer *outbuf;
211
212     outbuf = g_ptr_array_index (depay->packets, i);
213     g_ptr_array_index (depay->packets, i) = NULL;
214
215     gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (depay), outbuf);
216   }
217
218   /* and reset interleaving state */
219   depay->interleaved = FALSE;
220   depay->bundling = 0;
221 }
222
223 static void
224 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
225     GstBuffer * outbuf)
226 {
227   guint idx;
228   GstBuffer *old;
229
230   /* figure out the position in the array, note that index is never 0 because we
231    * push those packets immediately. */
232   idx = NNN + ((LLL + 1) * (index - 1));
233
234   GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
235   /* free old buffer (should not happen) */
236   old = g_ptr_array_index (depay->packets, idx);
237   if (old)
238     gst_buffer_unref (old);
239
240   /* store new buffer */
241   g_ptr_array_index (depay->packets, idx) = outbuf;
242 }
243
244 static GstBuffer *
245 create_erasure_buffer (GstRtpQCELPDepay * depay)
246 {
247   GstBuffer *outbuf;
248
249   outbuf = gst_buffer_new_and_alloc (1);
250   GST_BUFFER_DATA (outbuf)[0] = 14;
251
252   return outbuf;
253 }
254
255 static GstBuffer *
256 gst_rtp_qcelp_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
257 {
258   GstRtpQCELPDepay *depay;
259   GstBuffer *outbuf;
260   GstClockTime timestamp;
261   guint payload_len, offset, index;
262   guint8 *payload;
263   guint LLL, NNN;
264
265   depay = GST_RTP_QCELP_DEPAY (depayload);
266
267   payload_len = gst_rtp_buffer_get_payload_len (buf);
268
269   if (payload_len < 2)
270     goto too_small;
271
272   timestamp = GST_BUFFER_TIMESTAMP (buf);
273
274   payload = gst_rtp_buffer_get_payload (buf);
275
276   /*  0 1 2 3 4 5 6 7
277    * +-+-+-+-+-+-+-+-+
278    * |RR | LLL | NNN |
279    * +-+-+-+-+-+-+-+-+
280    */
281   /* RR = payload[0] >> 6; */
282   LLL = (payload[0] & 0x38) >> 3;
283   NNN = (payload[0] & 0x07);
284
285   payload_len--;
286   payload++;
287
288   GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
289
290   if (LLL > 5)
291     goto invalid_lll;
292
293   if (NNN > LLL)
294     goto invalid_nnn;
295
296   if (LLL != 0) {
297     /* we are interleaved */
298     if (!depay->interleaved) {
299       guint size;
300
301       GST_DEBUG_OBJECT (depay, "starting interleaving group");
302       /* bundling is not allowed to change in one interleave group */
303       depay->bundling = count_packets (depay, payload, payload_len);
304       GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
305       /* we have one bundle where NNN goes from 0 to L, we don't store the index
306        * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
307       size = (depay->bundling - 1) * (LLL + 1);
308       /* create the array to hold the packets */
309       if (depay->packets == NULL)
310         depay->packets = g_ptr_array_sized_new (size);
311       GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
312       g_ptr_array_set_size (depay->packets, size);
313       /* we were previously not interleaved, figure out how much space we
314        * need to deinterleave */
315       depay->interleaved = TRUE;
316     }
317   } else {
318     /* we are not interleaved */
319     if (depay->interleaved) {
320       GST_DEBUG_OBJECT (depay, "stopping interleaving");
321       /* flush packets if we were previously interleaved */
322       flush_packets (depay);
323     }
324     depay->bundling = 0;
325   }
326
327   index = 0;
328   offset = 1;
329
330   while (payload_len > 0) {
331     gint frame_len;
332     gboolean do_erasure;
333
334     frame_len = get_frame_len (depay, payload[0]);
335     GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
336
337     if (frame_len == 0)
338       goto invalid_frame;
339
340     if (frame_len < 0) {
341       /* need to add an erasure frame but we can recover */
342       frame_len = -frame_len;
343       do_erasure = TRUE;
344     } else {
345       do_erasure = FALSE;
346     }
347
348     if (frame_len > payload_len)
349       goto invalid_frame;
350
351     if (do_erasure) {
352       /* create erasure frame */
353       outbuf = create_erasure_buffer (depay);
354     } else {
355       /* each frame goes into its buffer */
356       outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, offset, frame_len);
357     }
358
359     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
360     GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
361
362     if (!depay->interleaved || index == 0) {
363       /* not interleaved or first frame in packet, just push */
364       gst_base_rtp_depayload_push (depayload, outbuf);
365
366       if (timestamp != -1)
367         timestamp += FRAME_DURATION;
368     } else {
369       /* put in interleave buffer */
370       add_packet (depay, LLL, NNN, index, outbuf);
371
372       if (timestamp != -1)
373         timestamp += (FRAME_DURATION * (LLL + 1));
374     }
375
376     payload_len -= frame_len;
377     payload += frame_len;
378     offset += frame_len;
379     index++;
380
381     /* discard excess packets */
382     if (depay->bundling > 0 && depay->bundling <= index)
383       break;
384   }
385   while (index < depay->bundling) {
386     GST_DEBUG_OBJECT (depay, "filling with erasure buffer");
387     /* fill remainder with erasure packets */
388     outbuf = create_erasure_buffer (depay);
389     add_packet (depay, LLL, NNN, index, outbuf);
390     index++;
391   }
392   if (depay->interleaved && LLL == NNN) {
393     GST_DEBUG_OBJECT (depay, "interleave group ended, flushing");
394     /* we have the complete interleave group, flush */
395     flush_packets (depay);
396   }
397
398   return NULL;
399
400   /* ERRORS */
401 too_small:
402   {
403     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
404         (NULL), ("QCELP RTP payload too small (%d)", payload_len));
405     return NULL;
406   }
407 invalid_lll:
408   {
409     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
410         (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
411     return NULL;
412   }
413 invalid_nnn:
414   {
415     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
416         (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
417     return NULL;
418   }
419 invalid_frame:
420   {
421     GST_ELEMENT_WARNING (depay, STREAM, DECODE,
422         (NULL), ("QCELP RTP invalid frame received"));
423     return NULL;
424   }
425 }
426
427 gboolean
428 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
429 {
430   return gst_element_register (plugin, "rtpqcelpdepay",
431       GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY);
432 }