rtpjpegdepay: fix logic error when checking if an EOI is present
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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., 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 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
28
29 #include "gstrtpelements.h"
30 #include "gstrtpceltdepay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpceltdepay_debug);
34 #define GST_CAT_DEFAULT (rtpceltdepay_debug)
35
36 /* RtpCELTDepay signals and args */
37
38 #define DEFAULT_FRAMESIZE       480
39 #define DEFAULT_CHANNELS        1
40 #define DEFAULT_CLOCKRATE       32000
41
42 enum
43 {
44   /* FILL ME */
45   LAST_SIGNAL
46 };
47
48 enum
49 {
50   PROP_0
51 };
52
53 static GstStaticPadTemplate gst_rtp_celt_depay_sink_template =
54 GST_STATIC_PAD_TEMPLATE ("sink",
55     GST_PAD_SINK,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("application/x-rtp, "
58         "media = (string) \"audio\", "
59         "clock-rate =  (int) [32000, 48000], "
60         "encoding-name = (string) \"CELT\"")
61     );
62
63 static GstStaticPadTemplate gst_rtp_celt_depay_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("audio/x-celt")
68     );
69
70 static GstBuffer *gst_rtp_celt_depay_process (GstRTPBaseDepayload * depayload,
71     GstRTPBuffer * rtp);
72 static gboolean gst_rtp_celt_depay_setcaps (GstRTPBaseDepayload * depayload,
73     GstCaps * caps);
74
75 #define gst_rtp_celt_depay_parent_class parent_class
76 G_DEFINE_TYPE (GstRtpCELTDepay, gst_rtp_celt_depay,
77     GST_TYPE_RTP_BASE_DEPAYLOAD);
78 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpceltdepay, "rtpceltdepay",
79     GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_DEPAY, rtp_element_init (plugin));
80 static void
81 gst_rtp_celt_depay_class_init (GstRtpCELTDepayClass * klass)
82 {
83   GstElementClass *gstelement_class;
84   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
85
86   GST_DEBUG_CATEGORY_INIT (rtpceltdepay_debug, "rtpceltdepay", 0,
87       "CELT RTP Depayloader");
88
89   gstelement_class = (GstElementClass *) klass;
90   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
91
92   gst_element_class_add_static_pad_template (gstelement_class,
93       &gst_rtp_celt_depay_src_template);
94   gst_element_class_add_static_pad_template (gstelement_class,
95       &gst_rtp_celt_depay_sink_template);
96
97   gst_element_class_set_static_metadata (gstelement_class,
98       "RTP CELT depayloader", "Codec/Depayloader/Network/RTP",
99       "Extracts CELT audio from RTP packets",
100       "Wim Taymans <wim.taymans@gmail.com>");
101
102   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_celt_depay_process;
103   gstrtpbasedepayload_class->set_caps = gst_rtp_celt_depay_setcaps;
104 }
105
106 static void
107 gst_rtp_celt_depay_init (GstRtpCELTDepay * rtpceltdepay)
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 (GstRTPBaseDepayload * depayload, GstCaps * caps)
120 {
121   GstStructure *structure;
122   GstRtpCELTDepay *rtpceltdepay;
123   gint clock_rate, nb_channels = 0, frame_size = 0;
124   GstBuffer *buf;
125   GstMapInfo map;
126   guint8 *ptr;
127   const gchar *params;
128   GstCaps *srccaps;
129   gboolean res;
130
131   rtpceltdepay = GST_RTP_CELT_DEPAY (depayload);
132
133   structure = gst_caps_get_structure (caps, 0);
134
135   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
136     goto no_clockrate;
137   depayload->clock_rate = clock_rate;
138
139   if ((params = gst_structure_get_string (structure, "encoding-params")))
140     nb_channels = atoi (params);
141   if (!nb_channels)
142     nb_channels = DEFAULT_CHANNELS;
143
144   if ((params = gst_structure_get_string (structure, "frame-size")))
145     frame_size = atoi (params);
146   if (!frame_size)
147     frame_size = DEFAULT_FRAMESIZE;
148   rtpceltdepay->frame_size = frame_size;
149
150   GST_DEBUG_OBJECT (depayload, "clock-rate=%d channels=%d frame-size=%d",
151       clock_rate, nb_channels, frame_size);
152
153   /* construct minimal header and comment packet for the decoder */
154   buf = gst_buffer_new_and_alloc (60);
155   gst_buffer_map (buf, &map, GST_MAP_WRITE);
156   ptr = map.data;
157   memcpy (ptr, "CELT    ", 8);
158   ptr += 8;
159   memcpy (ptr, "1.1.12", 7);
160   ptr += 20;
161   GST_WRITE_UINT32_LE (ptr, 0x80000006);        /* version */
162   ptr += 4;
163   GST_WRITE_UINT32_LE (ptr, 56);        /* header_size */
164   ptr += 4;
165   GST_WRITE_UINT32_LE (ptr, clock_rate);        /* rate */
166   ptr += 4;
167   GST_WRITE_UINT32_LE (ptr, nb_channels);       /* channels */
168   ptr += 4;
169   GST_WRITE_UINT32_LE (ptr, frame_size);        /* frame-size */
170   ptr += 4;
171   GST_WRITE_UINT32_LE (ptr, -1);        /* overlap */
172   ptr += 4;
173   GST_WRITE_UINT32_LE (ptr, -1);        /* bytes_per_packet */
174   ptr += 4;
175   GST_WRITE_UINT32_LE (ptr, 0); /* extra headers */
176   gst_buffer_unmap (buf, &map);
177
178   srccaps = gst_caps_new_empty_simple ("audio/x-celt");
179   res = gst_pad_set_caps (depayload->srcpad, srccaps);
180   gst_caps_unref (srccaps);
181
182   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpceltdepay), buf);
183
184   buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_celt_comment));
185   gst_buffer_fill (buf, 0, gst_rtp_celt_comment, sizeof (gst_rtp_celt_comment));
186
187   gst_rtp_base_depayload_push (GST_RTP_BASE_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 (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
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_PTS (rtp->buffer);
217
218   GST_LOG_OBJECT (depayload,
219       "got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
220       gst_buffer_get_size (rtp->buffer), 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_PTS (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_PTS (outbuf)),
263         GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
264
265     gst_rtp_drop_non_audio_meta (depayload, outbuf);
266
267     gst_rtp_base_depayload_push (depayload, outbuf);
268   }
269
270   return NULL;
271 }