gstdepay: check for correct fragment offset
[platform/upstream/gstreamer.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 (GstRTPBaseDepayload * depayload,
69     GstBuffer * buf);
70 static gboolean gst_rtp_celt_depay_setcaps (GstRTPBaseDepayload * depayload,
71     GstCaps * caps);
72
73 #define gst_rtp_celt_depay_parent_class parent_class
74 G_DEFINE_TYPE (GstRtpCELTDepay, gst_rtp_celt_depay,
75     GST_TYPE_RTP_BASE_DEPAYLOAD);
76
77 static void
78 gst_rtp_celt_depay_class_init (GstRtpCELTDepayClass * klass)
79 {
80   GstElementClass *gstelement_class;
81   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
82
83   GST_DEBUG_CATEGORY_INIT (rtpceltdepay_debug, "rtpceltdepay", 0,
84       "CELT RTP Depayloader");
85
86   gstelement_class = (GstElementClass *) klass;
87   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
88
89   gst_element_class_add_pad_template (gstelement_class,
90       gst_static_pad_template_get (&gst_rtp_celt_depay_src_template));
91   gst_element_class_add_pad_template (gstelement_class,
92       gst_static_pad_template_get (&gst_rtp_celt_depay_sink_template));
93
94   gst_element_class_set_static_metadata (gstelement_class,
95       "RTP CELT depayloader", "Codec/Depayloader/Network/RTP",
96       "Extracts CELT audio from RTP packets",
97       "Wim Taymans <wim.taymans@gmail.com>");
98
99   gstrtpbasedepayload_class->process = gst_rtp_celt_depay_process;
100   gstrtpbasedepayload_class->set_caps = gst_rtp_celt_depay_setcaps;
101 }
102
103 static void
104 gst_rtp_celt_depay_init (GstRtpCELTDepay * rtpceltdepay)
105 {
106 }
107
108 /* len 4 bytes LE,
109  * vendor string (len bytes),
110  * user_len 4 (0) bytes LE
111  */
112 static const gchar gst_rtp_celt_comment[] =
113     "\045\0\0\0Depayloaded with GStreamer celtdepay\0\0\0\0";
114
115 static gboolean
116 gst_rtp_celt_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
117 {
118   GstStructure *structure;
119   GstRtpCELTDepay *rtpceltdepay;
120   gint clock_rate, nb_channels = 0, frame_size = 0;
121   GstBuffer *buf;
122   GstMapInfo map;
123   guint8 *ptr;
124   const gchar *params;
125   GstCaps *srccaps;
126   gboolean res;
127
128   rtpceltdepay = GST_RTP_CELT_DEPAY (depayload);
129
130   structure = gst_caps_get_structure (caps, 0);
131
132   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
133     goto no_clockrate;
134   depayload->clock_rate = clock_rate;
135
136   if ((params = gst_structure_get_string (structure, "encoding-params")))
137     nb_channels = atoi (params);
138   if (!nb_channels)
139     nb_channels = DEFAULT_CHANNELS;
140
141   if ((params = gst_structure_get_string (structure, "frame-size")))
142     frame_size = atoi (params);
143   if (!frame_size)
144     frame_size = DEFAULT_FRAMESIZE;
145   rtpceltdepay->frame_size = frame_size;
146
147   GST_DEBUG_OBJECT (depayload, "clock-rate=%d channels=%d frame-size=%d",
148       clock_rate, nb_channels, frame_size);
149
150   /* construct minimal header and comment packet for the decoder */
151   buf = gst_buffer_new_and_alloc (60);
152   gst_buffer_map (buf, &map, GST_MAP_WRITE);
153   ptr = map.data;
154   memcpy (ptr, "CELT    ", 8);
155   ptr += 8;
156   memcpy (ptr, "1.1.12", 7);
157   ptr += 20;
158   GST_WRITE_UINT32_LE (ptr, 0x80000006);        /* version */
159   ptr += 4;
160   GST_WRITE_UINT32_LE (ptr, 56);        /* header_size */
161   ptr += 4;
162   GST_WRITE_UINT32_LE (ptr, clock_rate);        /* rate */
163   ptr += 4;
164   GST_WRITE_UINT32_LE (ptr, nb_channels);       /* channels */
165   ptr += 4;
166   GST_WRITE_UINT32_LE (ptr, frame_size);        /* frame-size */
167   ptr += 4;
168   GST_WRITE_UINT32_LE (ptr, -1);        /* overlap */
169   ptr += 4;
170   GST_WRITE_UINT32_LE (ptr, -1);        /* bytes_per_packet */
171   ptr += 4;
172   GST_WRITE_UINT32_LE (ptr, 0); /* extra headers */
173   gst_buffer_unmap (buf, &map);
174
175   srccaps = gst_caps_new_empty_simple ("audio/x-celt");
176   res = gst_pad_set_caps (depayload->srcpad, srccaps);
177   gst_caps_unref (srccaps);
178
179   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpceltdepay), buf);
180
181   buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_celt_comment));
182   gst_buffer_fill (buf, 0, gst_rtp_celt_comment, sizeof (gst_rtp_celt_comment));
183
184   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpceltdepay), buf);
185
186   return res;
187
188   /* ERRORS */
189 no_clockrate:
190   {
191     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
192     return FALSE;
193   }
194 }
195
196 static GstBuffer *
197 gst_rtp_celt_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
198 {
199   GstBuffer *outbuf = NULL;
200   guint8 *payload;
201   guint offset, pos, payload_len, total_size, size;
202   guint8 s;
203   gint clock_rate = 0, frame_size = 0;
204   GstClockTime framesize_ns = 0, timestamp;
205   guint n = 0;
206   GstRtpCELTDepay *rtpceltdepay;
207   GstRTPBuffer rtp = { NULL, };
208
209   rtpceltdepay = GST_RTP_CELT_DEPAY (depayload);
210   clock_rate = depayload->clock_rate;
211   frame_size = rtpceltdepay->frame_size;
212   framesize_ns = gst_util_uint64_scale_int (frame_size, GST_SECOND, clock_rate);
213
214   timestamp = GST_BUFFER_TIMESTAMP (buf);
215
216   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
217
218   GST_LOG_OBJECT (depayload,
219       "got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
220       gst_buffer_get_size (buf), gst_rtp_buffer_get_marker (&rtp),
221       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
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 (&rtp);
228   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
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 += s + 1;
251     } while (s == 0xff);
252
253     outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 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_rtp_base_depayload_push (depayload, outbuf);
266   }
267   gst_rtp_buffer_unmap (&rtp);
268
269   return NULL;
270 }
271
272 gboolean
273 gst_rtp_celt_depay_plugin_init (GstPlugin * plugin)
274 {
275   return gst_element_register (plugin, "rtpceltdepay",
276       GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_DEPAY);
277 }