Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpceltdepay.c
1 /* GStreamer
2  * Copyright (C) <2009> 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 <string.h>
25 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpceltdepay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpceltdepay_debug);
31 #define GST_CAT_DEFAULT (rtpceltdepay_debug)
32
33 /* RtpCELTDepay signals and args */
34
35 #define DEFAULT_FRAMESIZE       480
36 #define DEFAULT_CHANNELS        1
37 #define DEFAULT_CLOCKRATE       32000
38
39 enum
40 {
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum
46 {
47   ARG_0
48 };
49
50 static GstStaticPadTemplate gst_rtp_celt_depay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-rtp, "
55         "media = (string) \"audio\", "
56         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
57         "clock-rate =  (int) [32000, 48000], "
58         "encoding-name = (string) \"CELT\"")
59     );
60
61 static GstStaticPadTemplate gst_rtp_celt_depay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("audio/x-celt")
66     );
67
68 static GstBuffer *gst_rtp_celt_depay_process (GstBaseRTPDepayload * depayload,
69     GstBuffer * buf);
70 static gboolean gst_rtp_celt_depay_setcaps (GstBaseRTPDepayload * depayload,
71     GstCaps * caps);
72
73 GST_BOILERPLATE (GstRtpCELTDepay, gst_rtp_celt_depay, GstBaseRTPDepayload,
74     GST_TYPE_BASE_RTP_DEPAYLOAD);
75
76 static void
77 gst_rtp_celt_depay_base_init (gpointer klass)
78 {
79   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
80
81   gst_element_class_add_static_pad_template (element_class,
82       &gst_rtp_celt_depay_src_template);
83   gst_element_class_add_static_pad_template (element_class,
84       &gst_rtp_celt_depay_sink_template);
85   gst_element_class_set_details_simple (element_class, "RTP CELT depayloader",
86       "Codec/Depayloader/Network/RTP",
87       "Extracts CELT audio from RTP packets",
88       "Wim Taymans <wim.taymans@gmail.com>");
89
90   GST_DEBUG_CATEGORY_INIT (rtpceltdepay_debug, "rtpceltdepay", 0,
91       "CELT RTP Depayloader");
92 }
93
94 static void
95 gst_rtp_celt_depay_class_init (GstRtpCELTDepayClass * klass)
96 {
97   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
98
99   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
100
101   gstbasertpdepayload_class->process = gst_rtp_celt_depay_process;
102   gstbasertpdepayload_class->set_caps = gst_rtp_celt_depay_setcaps;
103 }
104
105 static void
106 gst_rtp_celt_depay_init (GstRtpCELTDepay * rtpceltdepay,
107     GstRtpCELTDepayClass * klass)
108 {
109 }
110
111 /* len 4 bytes LE,
112  * vendor string (len bytes),
113  * user_len 4 (0) bytes LE
114  */
115 static const gchar gst_rtp_celt_comment[] =
116     "\045\0\0\0Depayloaded with GStreamer celtdepay\0\0\0\0";
117
118 static gboolean
119 gst_rtp_celt_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
120 {
121   GstStructure *structure;
122   GstRtpCELTDepay *rtpceltdepay;
123   gint clock_rate, nb_channels = 0, frame_size = 0;
124   GstBuffer *buf;
125   guint8 *data;
126   const gchar *params;
127   GstCaps *srccaps;
128   gboolean res;
129
130   rtpceltdepay = GST_RTP_CELT_DEPAY (depayload);
131
132   structure = gst_caps_get_structure (caps, 0);
133
134   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
135     goto no_clockrate;
136   depayload->clock_rate = clock_rate;
137
138   if ((params = gst_structure_get_string (structure, "encoding-params")))
139     nb_channels = atoi (params);
140   if (!nb_channels)
141     nb_channels = DEFAULT_CHANNELS;
142
143   if ((params = gst_structure_get_string (structure, "frame-size")))
144     frame_size = atoi (params);
145   if (!frame_size)
146     frame_size = DEFAULT_FRAMESIZE;
147   rtpceltdepay->frame_size = frame_size;
148
149   GST_DEBUG_OBJECT (depayload, "clock-rate=%d channels=%d frame-size=%d",
150       clock_rate, nb_channels, frame_size);
151
152   /* construct minimal header and comment packet for the decoder */
153   buf = gst_buffer_new_and_alloc (60);
154   data = GST_BUFFER_DATA (buf);
155   memcpy (data, "CELT    ", 8);
156   data += 8;
157   memcpy (data, "1.1.12", 7);
158   data += 20;
159   GST_WRITE_UINT32_LE (data, 0x80000006);       /* version */
160   data += 4;
161   GST_WRITE_UINT32_LE (data, 56);       /* header_size */
162   data += 4;
163   GST_WRITE_UINT32_LE (data, clock_rate);       /* rate */
164   data += 4;
165   GST_WRITE_UINT32_LE (data, nb_channels);      /* channels */
166   data += 4;
167   GST_WRITE_UINT32_LE (data, frame_size);       /* frame-size */
168   data += 4;
169   GST_WRITE_UINT32_LE (data, -1);       /* overlap */
170   data += 4;
171   GST_WRITE_UINT32_LE (data, -1);       /* bytes_per_packet */
172   data += 4;
173   GST_WRITE_UINT32_LE (data, 0);        /* extra headers */
174
175   srccaps = gst_caps_new_simple ("audio/x-celt", NULL);
176   res = gst_pad_set_caps (depayload->srcpad, srccaps);
177   gst_caps_unref (srccaps);
178
179   gst_buffer_set_caps (buf, GST_PAD_CAPS (depayload->srcpad));
180   gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpceltdepay), buf);
181
182   buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_celt_comment));
183   memcpy (GST_BUFFER_DATA (buf), gst_rtp_celt_comment,
184       sizeof (gst_rtp_celt_comment));
185
186   gst_buffer_set_caps (buf, GST_PAD_CAPS (depayload->srcpad));
187   gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpceltdepay), buf);
188
189   return res;
190
191   /* ERRORS */
192 no_clockrate:
193   {
194     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
195     return FALSE;
196   }
197 }
198
199 static GstBuffer *
200 gst_rtp_celt_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
201 {
202   GstBuffer *outbuf = NULL;
203   guint8 *payload;
204   guint offset, pos, payload_len, total_size, size;
205   guint8 s;
206   gint clock_rate = 0, frame_size = 0;
207   GstClockTime framesize_ns = 0, timestamp;
208   guint n = 0;
209   GstRtpCELTDepay *rtpceltdepay;
210
211   rtpceltdepay = GST_RTP_CELT_DEPAY (depayload);
212   clock_rate = depayload->clock_rate;
213   frame_size = rtpceltdepay->frame_size;
214   framesize_ns = gst_util_uint64_scale_int (frame_size, GST_SECOND, clock_rate);
215
216   timestamp = GST_BUFFER_TIMESTAMP (buf);
217
218   GST_LOG_OBJECT (depayload, "got %d bytes, mark %d ts %u seqn %d",
219       GST_BUFFER_SIZE (buf),
220       gst_rtp_buffer_get_marker (buf),
221       gst_rtp_buffer_get_timestamp (buf), gst_rtp_buffer_get_seq (buf));
222
223   GST_LOG_OBJECT (depayload, "got clock-rate=%d, frame_size=%d, "
224       "_ns=%" GST_TIME_FORMAT ", timestamp=%" GST_TIME_FORMAT, clock_rate,
225       frame_size, GST_TIME_ARGS (framesize_ns), GST_TIME_ARGS (timestamp));
226
227   payload = gst_rtp_buffer_get_payload (buf);
228   payload_len = gst_rtp_buffer_get_payload_len (buf);
229
230   /* first count how many bytes are consumed by the size headers and make offset
231    * point to the first data byte */
232   total_size = 0;
233   offset = 0;
234   while (total_size < payload_len) {
235     do {
236       s = payload[offset++];
237       total_size += s + 1;
238     } while (s == 0xff);
239   }
240
241   /* offset is now pointing to the payload */
242   total_size = 0;
243   pos = 0;
244   while (total_size < payload_len) {
245     n++;
246     size = 0;
247     do {
248       s = payload[pos++];
249       size += s;
250       total_size += size + 1;
251     } while (s == 0xff);
252
253     outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, offset, size);
254     offset += size;
255
256     if (frame_size != -1 && clock_rate != -1) {
257       GST_BUFFER_TIMESTAMP (outbuf) = timestamp + framesize_ns * n;
258       GST_BUFFER_DURATION (outbuf) = framesize_ns;
259     }
260     GST_LOG_OBJECT (depayload, "push timestamp=%"
261         GST_TIME_FORMAT ", duration=%" GST_TIME_FORMAT,
262         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
263         GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
264
265     gst_base_rtp_depayload_push (depayload, outbuf);
266   }
267   return NULL;
268 }
269
270 gboolean
271 gst_rtp_celt_depay_plugin_init (GstPlugin * plugin)
272 {
273   return gst_element_register (plugin, "rtpceltdepay",
274       GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_DEPAY);
275 }