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