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