rtpvorbisdepay: remove dead code
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4adepay.c
1 /* GStreamer
2  * Copyright (C) <2007> Nokia Corporation (contact <stefan.kost@nokia.com>)
3  *               <2007> Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
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 <gst/base/gstbitreader.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include <gst/audio/audio.h>
27
28 #include <string.h>
29 #include "gstrtpmp4adepay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpmp4adepay_debug);
33 #define GST_CAT_DEFAULT (rtpmp4adepay_debug)
34
35 static GstStaticPadTemplate gst_rtp_mp4a_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/mpeg,"
40         "mpegversion = (int) 4," "framed = (boolean) true, "
41         "stream-format = (string) raw")
42     );
43
44 static GstStaticPadTemplate gst_rtp_mp4a_depay_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) \"audio\", "
50         "clock-rate = (int) [1, MAX ], "
51         "encoding-name = (string) \"MP4A-LATM\""
52         /* All optional parameters
53          *
54          * "profile-level-id=[1,MAX]"
55          * "config="
56          */
57     )
58     );
59
60 #define gst_rtp_mp4a_depay_parent_class parent_class
61 G_DEFINE_TYPE (GstRtpMP4ADepay, gst_rtp_mp4a_depay,
62     GST_TYPE_RTP_BASE_DEPAYLOAD);
63
64 static void gst_rtp_mp4a_depay_finalize (GObject * object);
65
66 static gboolean gst_rtp_mp4a_depay_setcaps (GstRTPBaseDepayload * depayload,
67     GstCaps * caps);
68 static GstBuffer *gst_rtp_mp4a_depay_process (GstRTPBaseDepayload * depayload,
69     GstRTPBuffer * rtp);
70
71 static GstStateChangeReturn gst_rtp_mp4a_depay_change_state (GstElement *
72     element, GstStateChange transition);
73
74
75 static void
76 gst_rtp_mp4a_depay_class_init (GstRtpMP4ADepayClass * klass)
77 {
78   GObjectClass *gobject_class;
79   GstElementClass *gstelement_class;
80   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
81
82   gobject_class = (GObjectClass *) klass;
83   gstelement_class = (GstElementClass *) klass;
84   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
85
86   gobject_class->finalize = gst_rtp_mp4a_depay_finalize;
87
88   gstelement_class->change_state = gst_rtp_mp4a_depay_change_state;
89
90   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_mp4a_depay_process;
91   gstrtpbasedepayload_class->set_caps = gst_rtp_mp4a_depay_setcaps;
92
93   gst_element_class_add_static_pad_template (gstelement_class,
94       &gst_rtp_mp4a_depay_src_template);
95   gst_element_class_add_static_pad_template (gstelement_class,
96       &gst_rtp_mp4a_depay_sink_template);
97
98   gst_element_class_set_static_metadata (gstelement_class,
99       "RTP MPEG4 audio depayloader", "Codec/Depayloader/Network/RTP",
100       "Extracts MPEG4 audio from RTP packets (RFC 3016)",
101       "Nokia Corporation (contact <stefan.kost@nokia.com>), "
102       "Wim Taymans <wim.taymans@gmail.com>");
103
104   GST_DEBUG_CATEGORY_INIT (rtpmp4adepay_debug, "rtpmp4adepay", 0,
105       "MPEG4 audio RTP Depayloader");
106 }
107
108 static void
109 gst_rtp_mp4a_depay_init (GstRtpMP4ADepay * rtpmp4adepay)
110 {
111   rtpmp4adepay->adapter = gst_adapter_new ();
112 }
113
114 static void
115 gst_rtp_mp4a_depay_finalize (GObject * object)
116 {
117   GstRtpMP4ADepay *rtpmp4adepay;
118
119   rtpmp4adepay = GST_RTP_MP4A_DEPAY (object);
120
121   g_object_unref (rtpmp4adepay->adapter);
122   rtpmp4adepay->adapter = NULL;
123
124   G_OBJECT_CLASS (parent_class)->finalize (object);
125 }
126
127 static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000,
128   44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350
129 };
130
131 static gboolean
132 gst_rtp_mp4a_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
133 {
134   GstStructure *structure;
135   GstRtpMP4ADepay *rtpmp4adepay;
136   GstCaps *srccaps;
137   const gchar *str;
138   gint clock_rate;
139   gint object_type;
140   gint channels = 2;            /* default */
141   gboolean res;
142
143   rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
144
145   structure = gst_caps_get_structure (caps, 0);
146
147   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
148     clock_rate = 90000;         /* default */
149   depayload->clock_rate = clock_rate;
150
151   if (!gst_structure_get_int (structure, "object", &object_type))
152     object_type = 2;            /* AAC LC default */
153
154   srccaps = gst_caps_new_simple ("audio/mpeg",
155       "mpegversion", G_TYPE_INT, 4,
156       "framed", G_TYPE_BOOLEAN, TRUE, "channels", G_TYPE_INT, channels,
157       "stream-format", G_TYPE_STRING, "raw", NULL);
158
159   if ((str = gst_structure_get_string (structure, "config"))) {
160     GValue v = { 0 };
161
162     g_value_init (&v, GST_TYPE_BUFFER);
163     if (gst_value_deserialize (&v, str)) {
164       GstBuffer *buffer;
165       GstMapInfo map;
166       guint8 *data;
167       gsize size;
168       gint i;
169       guint32 rate = 0;
170       guint8 obj_type = 0, sr_idx = 0, channels = 0;
171       GstBitReader br;
172
173       buffer = gst_value_get_buffer (&v);
174       gst_buffer_ref (buffer);
175       g_value_unset (&v);
176
177       gst_buffer_map (buffer, &map, GST_MAP_READ);
178       data = map.data;
179       size = map.size;
180
181       if (size < 2) {
182         GST_WARNING_OBJECT (depayload, "config too short (%d < 2)",
183             (gint) size);
184         goto bad_config;
185       }
186
187       /* Parse StreamMuxConfig according to ISO/IEC 14496-3:
188        *
189        * audioMuxVersion           == 0 (1 bit)
190        * allStreamsSameTimeFraming == 1 (1 bit)
191        * numSubFrames              == rtpmp4adepay->numSubFrames (6 bits)
192        * numProgram                == 0 (4 bits)
193        * numLayer                  == 0 (3 bits)
194        *
195        * We only require audioMuxVersion == 0;
196        *
197        * The remaining bit of the second byte and the rest of the bits are used
198        * for audioSpecificConfig which we need to set in codec_info.
199        */
200       if ((data[0] & 0x80) != 0x00) {
201         GST_WARNING_OBJECT (depayload, "unknown audioMuxVersion 1");
202         goto bad_config;
203       }
204
205       rtpmp4adepay->numSubFrames = (data[0] & 0x3F);
206
207       GST_LOG_OBJECT (rtpmp4adepay, "numSubFrames %d",
208           rtpmp4adepay->numSubFrames);
209
210       /* shift rest of string 15 bits down */
211       size -= 2;
212       for (i = 0; i < size; i++) {
213         data[i] = ((data[i + 1] & 1) << 7) | ((data[i + 2] & 0xfe) >> 1);
214       }
215
216       gst_bit_reader_init (&br, data, size);
217
218       /* any object type is fine, we need to copy it to the profile-level-id field. */
219       if (!gst_bit_reader_get_bits_uint8 (&br, &obj_type, 5))
220         goto bad_config;
221       if (obj_type == 0) {
222         GST_WARNING_OBJECT (depayload, "invalid object type 0");
223         goto bad_config;
224       }
225
226       if (!gst_bit_reader_get_bits_uint8 (&br, &sr_idx, 4))
227         goto bad_config;
228       if (sr_idx >= G_N_ELEMENTS (aac_sample_rates) && sr_idx != 15) {
229         GST_WARNING_OBJECT (depayload, "invalid sample rate index %d", sr_idx);
230         goto bad_config;
231       }
232       GST_LOG_OBJECT (rtpmp4adepay, "sample rate index %u", sr_idx);
233
234       if (!gst_bit_reader_get_bits_uint8 (&br, &channels, 4))
235         goto bad_config;
236       if (channels > 7) {
237         GST_WARNING_OBJECT (depayload, "invalid channels %u", (guint) channels);
238         goto bad_config;
239       }
240
241       /* rtp rate depends on sampling rate of the audio */
242       if (sr_idx == 15) {
243         /* index of 15 means we get the rate in the next 24 bits */
244         if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
245           goto bad_config;
246       } else if (sr_idx >= G_N_ELEMENTS (aac_sample_rates)) {
247         goto bad_config;
248       } else {
249         /* else use the rate from the table */
250         rate = aac_sample_rates[sr_idx];
251       }
252
253       rtpmp4adepay->frame_len = 1024;
254
255       switch (obj_type) {
256         case 1:
257         case 2:
258         case 3:
259         case 4:
260         case 6:
261         case 7:
262         {
263           guint8 frameLenFlag = 0;
264
265           if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
266             if (frameLenFlag)
267               rtpmp4adepay->frame_len = 960;
268           break;
269         }
270         default:
271           break;
272       }
273
274       /* ignore remaining bit, we're only interested in full bytes */
275       gst_buffer_resize (buffer, 0, size);
276       gst_buffer_unmap (buffer, &map);
277       data = NULL;
278
279       gst_caps_set_simple (srccaps,
280           "channels", G_TYPE_INT, (gint) channels,
281           "rate", G_TYPE_INT, (gint) rate,
282           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
283     bad_config:
284       if (data)
285         gst_buffer_unmap (buffer, &map);
286       gst_buffer_unref (buffer);
287     } else {
288       g_warning ("cannot convert config to buffer");
289     }
290   }
291   res = gst_pad_set_caps (depayload->srcpad, srccaps);
292   gst_caps_unref (srccaps);
293
294   return res;
295 }
296
297 static GstBuffer *
298 gst_rtp_mp4a_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
299 {
300   GstRtpMP4ADepay *rtpmp4adepay;
301   GstBuffer *outbuf;
302   GstMapInfo map;
303
304   rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
305
306   /* flush remaining data on discont */
307   if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
308     gst_adapter_clear (rtpmp4adepay->adapter);
309   }
310
311   outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
312
313   outbuf = gst_buffer_make_writable (outbuf);
314   GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (rtp->buffer);
315   gst_adapter_push (rtpmp4adepay->adapter, outbuf);
316
317   /* RTP marker bit indicates the last packet of the AudioMuxElement => create
318    * and push a buffer */
319   if (gst_rtp_buffer_get_marker (rtp)) {
320     guint avail;
321     guint i;
322     guint8 *data;
323     guint pos;
324     GstClockTime timestamp;
325
326     avail = gst_adapter_available (rtpmp4adepay->adapter);
327     timestamp = gst_adapter_prev_pts (rtpmp4adepay->adapter, NULL);
328
329     GST_LOG_OBJECT (rtpmp4adepay, "have marker and %u available", avail);
330
331     outbuf = gst_adapter_take_buffer (rtpmp4adepay->adapter, avail);
332     gst_buffer_map (outbuf, &map, GST_MAP_READ);
333     data = map.data;
334     /* position in data we are at */
335     pos = 0;
336
337     /* looping through the number of sub-frames in the audio payload */
338     for (i = 0; i <= rtpmp4adepay->numSubFrames; i++) {
339       /* determine payload length and set buffer data pointer accordingly */
340       guint skip;
341       guint data_len;
342       GstBuffer *tmp = NULL;
343
344       /* each subframe starts with a variable length encoding */
345       data_len = 0;
346       for (skip = 0; skip < avail; skip++) {
347         data_len += data[skip];
348         if (data[skip] != 0xff)
349           break;
350       }
351       skip++;
352
353       /* this can not be possible, we have not enough data or the length
354        * decoding failed because we ran out of data. */
355       if (skip + data_len > avail)
356         goto wrong_size;
357
358       GST_LOG_OBJECT (rtpmp4adepay,
359           "subframe %u, header len %u, data len %u, left %u", i, skip, data_len,
360           avail);
361
362       /* take data out, skip the header */
363       pos += skip;
364       tmp = gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, pos, data_len);
365
366       /* skip data too */
367       skip += data_len;
368       pos += data_len;
369
370       /* update our pointers whith what we consumed */
371       data += skip;
372       avail -= skip;
373
374       GST_BUFFER_PTS (tmp) = timestamp;
375       gst_rtp_drop_meta (GST_ELEMENT_CAST (depayload), tmp,
376           g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
377       gst_rtp_base_depayload_push (depayload, tmp);
378
379       /* shift ts for next buffers */
380       if (rtpmp4adepay->frame_len && timestamp != -1
381           && depayload->clock_rate != 0) {
382         timestamp +=
383             gst_util_uint64_scale_int (rtpmp4adepay->frame_len, GST_SECOND,
384             depayload->clock_rate);
385       }
386     }
387
388     /* just a check that lengths match */
389     if (avail) {
390       GST_ELEMENT_WARNING (depayload, STREAM, DECODE,
391           ("Packet invalid"), ("Not all payload consumed: "
392               "possible wrongly encoded packet."));
393     }
394
395     gst_buffer_unmap (outbuf, &map);
396     gst_buffer_unref (outbuf);
397   }
398   return NULL;
399
400   /* ERRORS */
401 wrong_size:
402   {
403     GST_ELEMENT_WARNING (rtpmp4adepay, STREAM, DECODE,
404         ("Packet did not validate"), ("wrong packet size"));
405     gst_buffer_unmap (outbuf, &map);
406     gst_buffer_unref (outbuf);
407     return NULL;
408   }
409 }
410
411 static GstStateChangeReturn
412 gst_rtp_mp4a_depay_change_state (GstElement * element,
413     GstStateChange transition)
414 {
415   GstRtpMP4ADepay *rtpmp4adepay;
416   GstStateChangeReturn ret;
417
418   rtpmp4adepay = GST_RTP_MP4A_DEPAY (element);
419
420   switch (transition) {
421     case GST_STATE_CHANGE_READY_TO_PAUSED:
422       gst_adapter_clear (rtpmp4adepay->adapter);
423       rtpmp4adepay->frame_len = 0;
424       rtpmp4adepay->numSubFrames = 0;
425       break;
426     default:
427       break;
428   }
429
430   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
431
432   switch (transition) {
433     default:
434       break;
435   }
436   return ret;
437 }
438
439 gboolean
440 gst_rtp_mp4a_depay_plugin_init (GstPlugin * plugin)
441 {
442   return gst_element_register (plugin, "rtpmp4adepay",
443       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4A_DEPAY);
444 }