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