rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpqdmdepay.c
1 /* GStreamer
2  * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.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 <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpqdmdepay.h"
28
29 GST_DEBUG_CATEGORY (rtpqdm2depay_debug);
30 #define GST_CAT_DEFAULT rtpqdm2depay_debug
31
32 static GstStaticPadTemplate gst_rtp_qdm2_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("audio/x-qdm2")
37     );
38
39 static GstStaticPadTemplate gst_rtp_qdm2_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("application/x-rtp, "
44         "media = (string) \"audio\", " "encoding-name = (string)\"X-QDM\"")
45     );
46
47 #define gst_rtp_qdm2_depay_parent_class parent_class
48 G_DEFINE_TYPE (GstRtpQDM2Depay, gst_rtp_qdm2_depay,
49     GST_TYPE_RTP_BASE_DEPAYLOAD);
50
51 static const guint8 headheader[20] = {
52   0x0, 0x0, 0x0, 0xc, 0x66, 0x72, 0x6d, 0x61,
53   0x51, 0x44, 0x4d, 0x32, 0x0, 0x0, 0x0, 0x24,
54   0x51, 0x44, 0x43, 0x41
55 };
56
57 static void gst_rtp_qdm2_depay_finalize (GObject * object);
58
59 static GstStateChangeReturn gst_rtp_qdm2_depay_change_state (GstElement *
60     element, GstStateChange transition);
61
62 static GstBuffer *gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload,
63     GstBuffer * buf);
64 gboolean gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter,
65     GstCaps * caps);
66
67 static void
68 gst_rtp_qdm2_depay_class_init (GstRtpQDM2DepayClass * klass)
69 {
70   GObjectClass *gobject_class;
71   GstElementClass *gstelement_class;
72   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
73
74   gobject_class = (GObjectClass *) klass;
75   gstelement_class = (GstElementClass *) klass;
76   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
77
78   gstrtpbasedepayload_class->process = gst_rtp_qdm2_depay_process;
79   gstrtpbasedepayload_class->set_caps = gst_rtp_qdm2_depay_setcaps;
80
81   gobject_class->finalize = gst_rtp_qdm2_depay_finalize;
82
83   gstelement_class->change_state = gst_rtp_qdm2_depay_change_state;
84
85   gst_element_class_add_pad_template (gstelement_class,
86       gst_static_pad_template_get (&gst_rtp_qdm2_depay_src_template));
87   gst_element_class_add_pad_template (gstelement_class,
88       gst_static_pad_template_get (&gst_rtp_qdm2_depay_sink_template));
89
90   gst_element_class_set_static_metadata (gstelement_class,
91       "RTP QDM2 depayloader",
92       "Codec/Depayloader/Network/RTP",
93       "Extracts QDM2 audio from RTP packets (no RFC)",
94       "Edward Hervey <bilboed@bilboed.com>");
95 }
96
97 static void
98 gst_rtp_qdm2_depay_init (GstRtpQDM2Depay * rtpqdm2depay)
99 {
100   rtpqdm2depay->adapter = gst_adapter_new ();
101 }
102
103 static void
104 gst_rtp_qdm2_depay_finalize (GObject * object)
105 {
106   GstRtpQDM2Depay *rtpqdm2depay;
107
108   rtpqdm2depay = GST_RTP_QDM2_DEPAY (object);
109
110   g_object_unref (rtpqdm2depay->adapter);
111   rtpqdm2depay->adapter = NULL;
112
113   G_OBJECT_CLASS (parent_class)->finalize (object);
114 }
115
116 /* only on the sink */
117 gboolean
118 gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
119 {
120   GstStructure *structure = gst_caps_get_structure (caps, 0);
121   gint clock_rate;
122
123   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
124     clock_rate = 44100;         /* default */
125   filter->clock_rate = clock_rate;
126
127   /* will set caps later */
128
129   return TRUE;
130 }
131
132 static void
133 flush_data (GstRtpQDM2Depay * depay)
134 {
135   guint i;
136   guint avail;
137
138   if ((avail = gst_adapter_available (depay->adapter)))
139     gst_adapter_flush (depay->adapter, avail);
140
141   GST_DEBUG ("Flushing %d packets", depay->nbpackets);
142
143   for (i = 0; depay->packets[i]; i++) {
144     QDM2Packet *pack = depay->packets[i];
145     guint32 crc = 0;
146     int i = 0;
147     GstBuffer *buf;
148     guint8 *data;
149
150     /* CRC is the sum of everything (including first bytes) */
151
152     data = pack->data;
153
154     if (G_UNLIKELY (data == NULL))
155       continue;
156
157     /* If the packet size is bigger than 0xff, we need 2 bytes to store the size */
158     if (depay->packetsize > 0xff) {
159       /* Expanded size 0x02 | 0x80 */
160       data[0] = 0x82;
161       GST_WRITE_UINT16_BE (data + 1, depay->packetsize - 3);
162     } else {
163       data[0] = 0x2;
164       data[1] = depay->packetsize - 2;
165     }
166
167     /* Calculate CRC */
168     for (; i < depay->packetsize; i++)
169       crc += data[i];
170
171     GST_DEBUG ("CRC is 0x%x", crc);
172
173     /* Write CRC */
174     if (depay->packetsize > 0xff)
175       GST_WRITE_UINT16_BE (data + 3, crc);
176     else
177       GST_WRITE_UINT16_BE (data + 2, crc);
178
179     GST_MEMDUMP ("Extracted packet", data, depay->packetsize);
180
181     buf = gst_buffer_new ();
182     gst_buffer_append_memory (buf,
183         gst_memory_new_wrapped (0, data, depay->packetsize, 0,
184             depay->packetsize, data, g_free));
185
186     gst_adapter_push (depay->adapter, buf);
187
188     if (pack->data) {
189       pack->data = NULL;
190     }
191   }
192 }
193
194 static void
195 add_packet (GstRtpQDM2Depay * depay, guint32 pid, guint32 len, guint8 * data)
196 {
197   QDM2Packet *packet;
198
199   if (G_UNLIKELY (!depay->configured))
200     return;
201
202   GST_DEBUG ("pid:%d, len:%d, data:%p", pid, len, data);
203
204   if (G_UNLIKELY (depay->packets[pid] == NULL)) {
205     depay->packets[pid] = g_malloc0 (sizeof (QDM2Packet));
206     depay->nbpackets = MAX (depay->nbpackets, pid + 1);
207   }
208   packet = depay->packets[pid];
209
210   GST_DEBUG ("packet:%p", packet);
211   GST_DEBUG ("packet->data:%p", packet->data);
212
213   if (G_UNLIKELY (packet->data == NULL)) {
214     packet->data = g_malloc0 (depay->packetsize);
215     /* We leave space for the header/crc */
216     if (depay->packetsize > 0xff)
217       packet->offs = 5;
218     else
219       packet->offs = 4;
220   }
221
222   /* Finally copy the data over */
223   memcpy (packet->data + packet->offs, data, len);
224   packet->offs += len;
225 }
226
227 static GstBuffer *
228 gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
229 {
230   GstRtpQDM2Depay *rtpqdm2depay;
231   GstBuffer *outbuf = NULL;
232   guint16 seq;
233   GstRTPBuffer rtp = { NULL };
234
235   rtpqdm2depay = GST_RTP_QDM2_DEPAY (depayload);
236
237   {
238     gint payload_len;
239     guint8 *payload;
240     guint avail;
241     guint pos = 0;
242
243     gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
244     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
245     if (payload_len < 3)
246       goto bad_packet;
247
248     payload = gst_rtp_buffer_get_payload (&rtp);
249     seq = gst_rtp_buffer_get_seq (&rtp);
250     if (G_UNLIKELY (seq != rtpqdm2depay->nextseq)) {
251       GST_DEBUG ("GAP in sequence number, Resetting data !");
252       /* Flush previous data */
253       flush_data (rtpqdm2depay);
254       /* And store new timestamp */
255       rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
256       rtpqdm2depay->timestamp = GST_BUFFER_TIMESTAMP (buf);
257       /* And that previous data will be pushed at the bottom */
258     }
259     rtpqdm2depay->nextseq = seq + 1;
260
261     GST_DEBUG ("Payload size %d 0x%x sequence:%d", payload_len, payload_len,
262         seq);
263
264     GST_MEMDUMP ("Incoming payload", payload, payload_len);
265
266     while (pos < payload_len) {
267       switch (payload[pos]) {
268         case 0x80:{
269           GST_DEBUG ("Unrecognized 0x80 marker, skipping 12 bytes");
270           pos += 12;
271         }
272           break;
273         case 0xff:
274           /* HEADERS */
275           GST_DEBUG ("Headers");
276           /* Store the incoming timestamp */
277           rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
278           rtpqdm2depay->timestamp = GST_BUFFER_TIMESTAMP (buf);
279           /* flush the internal data if needed */
280           flush_data (rtpqdm2depay);
281           if (G_UNLIKELY (!rtpqdm2depay->configured)) {
282             guint8 *ourdata;
283             GstBuffer *codecdata;
284             GstMapInfo cmap;
285             GstCaps *caps;
286
287             /* First bytes are unknown */
288             GST_MEMDUMP ("Header", payload + pos, 32);
289             ourdata = payload + pos + 10;
290             pos += 10;
291             rtpqdm2depay->channs = GST_READ_UINT32_BE (payload + pos + 4);
292             rtpqdm2depay->samplerate = GST_READ_UINT32_BE (payload + pos + 8);
293             rtpqdm2depay->bitrate = GST_READ_UINT32_BE (payload + pos + 12);
294             rtpqdm2depay->blocksize = GST_READ_UINT32_BE (payload + pos + 16);
295             rtpqdm2depay->framesize = GST_READ_UINT32_BE (payload + pos + 20);
296             rtpqdm2depay->packetsize = GST_READ_UINT32_BE (payload + pos + 24);
297             /* 16 bit empty block (0x02 0x00) */
298             pos += 30;
299             GST_DEBUG
300                 ("channs:%d, samplerate:%d, bitrate:%d, blocksize:%d, framesize:%d, packetsize:%d",
301                 rtpqdm2depay->channs, rtpqdm2depay->samplerate,
302                 rtpqdm2depay->bitrate, rtpqdm2depay->blocksize,
303                 rtpqdm2depay->framesize, rtpqdm2depay->packetsize);
304
305             /* Caps */
306             codecdata = gst_buffer_new_and_alloc (48);
307             gst_buffer_map (codecdata, &cmap, GST_MAP_WRITE);
308             memcpy (cmap.data, headheader, 20);
309             memcpy (cmap.data + 20, ourdata, 28);
310             gst_buffer_unmap (codecdata, &cmap);
311
312             caps = gst_caps_new_simple ("audio/x-qdm2",
313                 "samplesize", G_TYPE_INT, 16,
314                 "rate", G_TYPE_INT, rtpqdm2depay->samplerate,
315                 "channels", G_TYPE_INT, rtpqdm2depay->channs,
316                 "codec_data", GST_TYPE_BUFFER, codecdata, NULL);
317             gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), caps);
318             gst_caps_unref (caps);
319             rtpqdm2depay->configured = TRUE;
320           } else {
321             GST_DEBUG ("Already configured, skipping headers");
322             pos += 40;
323           }
324           break;
325         default:{
326           /* Shuffled packet contents */
327           guint packetid = payload[pos++];
328           guint packettype = payload[pos++];
329           guint packlen = payload[pos++];
330           guint hsize = 2;
331
332           GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
333               packetid, packettype, packlen);
334
335           /* Packets bigger than 0xff bytes have a type with the high bit set */
336           if (G_UNLIKELY (packettype & 0x80)) {
337             packettype &= 0x7f;
338             packlen <<= 8;
339             packlen |= payload[pos++];
340             hsize = 3;
341             GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
342                 packetid, packettype, packlen);
343           }
344
345           if (packettype > 0x7f) {
346             GST_ERROR ("HOUSTON WE HAVE A PROBLEM !!!!");
347           }
348           add_packet (rtpqdm2depay, packetid, packlen + hsize,
349               payload + pos - hsize);
350           pos += packlen;
351         }
352       }
353     }
354
355     GST_DEBUG ("final pos %d", pos);
356
357     avail = gst_adapter_available (rtpqdm2depay->adapter);
358     if (G_UNLIKELY (avail)) {
359       GST_DEBUG ("Pushing out %d bytes of collected data", avail);
360       outbuf = gst_adapter_take_buffer (rtpqdm2depay->adapter, avail);
361       GST_BUFFER_TIMESTAMP (outbuf) = rtpqdm2depay->ptimestamp;
362       GST_DEBUG ("Outgoing buffer timestamp %" GST_TIME_FORMAT,
363           GST_TIME_ARGS (rtpqdm2depay->ptimestamp));
364     }
365   }
366
367   gst_rtp_buffer_unmap (&rtp);
368   return outbuf;
369
370   /* ERRORS */
371 bad_packet:
372   {
373     GST_ELEMENT_WARNING (rtpqdm2depay, STREAM, DECODE,
374         (NULL), ("Packet was too short"));
375     gst_rtp_buffer_unmap (&rtp);
376     return NULL;
377   }
378 }
379
380 static GstStateChangeReturn
381 gst_rtp_qdm2_depay_change_state (GstElement * element,
382     GstStateChange transition)
383 {
384   GstRtpQDM2Depay *rtpqdm2depay;
385   GstStateChangeReturn ret;
386
387   rtpqdm2depay = GST_RTP_QDM2_DEPAY (element);
388
389   switch (transition) {
390     case GST_STATE_CHANGE_NULL_TO_READY:
391       break;
392     case GST_STATE_CHANGE_READY_TO_PAUSED:
393       gst_adapter_clear (rtpqdm2depay->adapter);
394       break;
395     default:
396       break;
397   }
398
399   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
400
401   switch (transition) {
402     case GST_STATE_CHANGE_READY_TO_NULL:
403       break;
404     default:
405       break;
406   }
407   return ret;
408 }
409
410 gboolean
411 gst_rtp_qdm2_depay_plugin_init (GstPlugin * plugin)
412 {
413   GST_DEBUG_CATEGORY_INIT (rtpqdm2depay_debug, "rtpqdm2depay", 0,
414       "RTP QDM2 depayloader");
415
416   return gst_element_register (plugin, "rtpqdm2depay",
417       GST_RANK_SECONDARY, GST_TYPE_RTP_QDM2_DEPAY);
418 }