rtp: Update codes based on 1.18.4
[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) { false, 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   rtpmp4adepay->framed = FALSE;
113 }
114
115 static void
116 gst_rtp_mp4a_depay_finalize (GObject * object)
117 {
118   GstRtpMP4ADepay *rtpmp4adepay;
119
120   rtpmp4adepay = GST_RTP_MP4A_DEPAY (object);
121
122   g_object_unref (rtpmp4adepay->adapter);
123   rtpmp4adepay->adapter = NULL;
124
125   G_OBJECT_CLASS (parent_class)->finalize (object);
126 }
127
128 static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000,
129   44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350
130 };
131
132 static gboolean
133 gst_rtp_mp4a_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
134 {
135   GstStructure *structure;
136   GstRtpMP4ADepay *rtpmp4adepay;
137   GstCaps *srccaps;
138   const gchar *str;
139   gint clock_rate;
140   gint object_type;
141   gint channels = 2;            /* default */
142   gboolean res;
143
144   rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
145
146   rtpmp4adepay->framed = FALSE;
147
148   structure = gst_caps_get_structure (caps, 0);
149
150   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
151     clock_rate = 90000;         /* default */
152   depayload->clock_rate = clock_rate;
153
154   if (!gst_structure_get_int (structure, "object", &object_type))
155     object_type = 2;            /* AAC LC default */
156
157   srccaps = gst_caps_new_simple ("audio/mpeg",
158       "mpegversion", G_TYPE_INT, 4,
159       "framed", G_TYPE_BOOLEAN, FALSE, "channels", G_TYPE_INT, channels,
160       "stream-format", G_TYPE_STRING, "raw", NULL);
161
162   if ((str = gst_structure_get_string (structure, "config"))) {
163     GValue v = { 0 };
164
165     g_value_init (&v, GST_TYPE_BUFFER);
166     if (gst_value_deserialize (&v, str)) {
167       GstBuffer *buffer;
168       GstMapInfo map;
169       guint8 *data;
170       gsize size;
171       gint i;
172       guint32 rate = 0;
173       guint8 obj_type = 0, sr_idx = 0, channels = 0;
174       GstBitReader br;
175
176       buffer = gst_value_get_buffer (&v);
177       gst_buffer_ref (buffer);
178       g_value_unset (&v);
179
180       gst_buffer_map (buffer, &map, GST_MAP_READ);
181       data = map.data;
182       size = map.size;
183
184       if (size < 2) {
185         GST_WARNING_OBJECT (depayload, "config too short (%d < 2)",
186             (gint) size);
187         goto bad_config;
188       }
189
190       /* Parse StreamMuxConfig according to ISO/IEC 14496-3:
191        *
192        * audioMuxVersion           == 0 (1 bit)
193        * allStreamsSameTimeFraming == 1 (1 bit)
194        * numSubFrames              == rtpmp4adepay->numSubFrames (6 bits)
195        * numProgram                == 0 (4 bits)
196        * numLayer                  == 0 (3 bits)
197        *
198        * We only require audioMuxVersion == 0;
199        *
200        * The remaining bit of the second byte and the rest of the bits are used
201        * for audioSpecificConfig which we need to set in codec_info.
202        */
203       if ((data[0] & 0x80) != 0x00) {
204         GST_WARNING_OBJECT (depayload, "unknown audioMuxVersion 1");
205         goto bad_config;
206       }
207
208       rtpmp4adepay->numSubFrames = (data[0] & 0x3F);
209
210       GST_LOG_OBJECT (rtpmp4adepay, "numSubFrames %d",
211           rtpmp4adepay->numSubFrames);
212
213       /* shift rest of string 15 bits down */
214       size -= 2;
215       for (i = 0; i < size; i++) {
216         data[i] = ((data[i + 1] & 1) << 7) | ((data[i + 2] & 0xfe) >> 1);
217       }
218
219       gst_bit_reader_init (&br, data, size);
220
221       /* any object type is fine, we need to copy it to the profile-level-id field. */
222       if (!gst_bit_reader_get_bits_uint8 (&br, &obj_type, 5))
223         goto bad_config;
224       if (obj_type == 0) {
225         GST_WARNING_OBJECT (depayload, "invalid object type 0");
226         goto bad_config;
227       }
228
229       if (!gst_bit_reader_get_bits_uint8 (&br, &sr_idx, 4))
230         goto bad_config;
231       if (sr_idx >= G_N_ELEMENTS (aac_sample_rates) && sr_idx != 15) {
232         GST_WARNING_OBJECT (depayload, "invalid sample rate index %d", sr_idx);
233         goto bad_config;
234       }
235       GST_LOG_OBJECT (rtpmp4adepay, "sample rate index %u", sr_idx);
236
237       if (!gst_bit_reader_get_bits_uint8 (&br, &channels, 4))
238         goto bad_config;
239       if (channels > 7) {
240         GST_WARNING_OBJECT (depayload, "invalid channels %u", (guint) channels);
241         goto bad_config;
242       }
243
244       /* rtp rate depends on sampling rate of the audio */
245       if (sr_idx == 15) {
246         /* index of 15 means we get the rate in the next 24 bits */
247         if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
248           goto bad_config;
249       } else if (sr_idx >= G_N_ELEMENTS (aac_sample_rates)) {
250         goto bad_config;
251       } else {
252         /* else use the rate from the table */
253         rate = aac_sample_rates[sr_idx];
254       }
255
256       rtpmp4adepay->frame_len = 1024;
257
258       switch (obj_type) {
259         case 1:
260         case 2:
261         case 3:
262         case 4:
263         case 6:
264         case 7:
265         {
266           guint8 frameLenFlag = 0;
267
268           if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
269             if (frameLenFlag)
270               rtpmp4adepay->frame_len = 960;
271           break;
272         }
273         default:
274           break;
275       }
276
277       /* ignore remaining bit, we're only interested in full bytes */
278       gst_buffer_resize (buffer, 0, size);
279       gst_buffer_unmap (buffer, &map);
280       data = NULL;
281
282       gst_caps_set_simple (srccaps,
283           "channels", G_TYPE_INT, (gint) channels,
284           "rate", G_TYPE_INT, (gint) rate,
285           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
286     bad_config:
287       if (data)
288         gst_buffer_unmap (buffer, &map);
289       gst_buffer_unref (buffer);
290     } else {
291       g_warning ("cannot convert config to buffer");
292     }
293   }
294   res = gst_pad_set_caps (depayload->srcpad, srccaps);
295   gst_caps_unref (srccaps);
296
297   return res;
298 }
299
300 static GstBuffer *
301 gst_rtp_mp4a_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
302 {
303   GstRtpMP4ADepay *rtpmp4adepay;
304   GstBuffer *outbuf;
305   GstMapInfo map;
306
307   rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
308
309   /* flush remaining data on discont */
310   if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
311     gst_adapter_clear (rtpmp4adepay->adapter);
312   }
313
314   outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
315
316   if (!rtpmp4adepay->framed) {
317     if (gst_rtp_buffer_get_marker (rtp)) {
318       GstCaps *caps;
319
320       rtpmp4adepay->framed = TRUE;
321
322       gst_rtp_base_depayload_push (depayload, outbuf);
323
324       caps = gst_pad_get_current_caps (depayload->srcpad);
325       caps = gst_caps_make_writable (caps);
326       gst_caps_set_simple (caps, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
327       gst_pad_set_caps (depayload->srcpad, caps);
328       gst_caps_unref (caps);
329       return NULL;
330     } else {
331       return outbuf;
332     }
333   }
334
335   outbuf = gst_buffer_make_writable (outbuf);
336   GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (rtp->buffer);
337   gst_adapter_push (rtpmp4adepay->adapter, outbuf);
338
339   /* RTP marker bit indicates the last packet of the AudioMuxElement => create
340    * and push a buffer */
341   if (gst_rtp_buffer_get_marker (rtp)) {
342     guint avail;
343     guint i;
344     guint8 *data;
345     guint pos;
346     GstClockTime timestamp;
347
348     avail = gst_adapter_available (rtpmp4adepay->adapter);
349     timestamp = gst_adapter_prev_pts (rtpmp4adepay->adapter, NULL);
350
351     GST_LOG_OBJECT (rtpmp4adepay, "have marker and %u available", avail);
352
353     outbuf = gst_adapter_take_buffer (rtpmp4adepay->adapter, avail);
354     gst_buffer_map (outbuf, &map, GST_MAP_READ);
355     data = map.data;
356     /* position in data we are at */
357     pos = 0;
358
359     /* looping through the number of sub-frames in the audio payload */
360     for (i = 0; i <= rtpmp4adepay->numSubFrames; i++) {
361       /* determine payload length and set buffer data pointer accordingly */
362       guint skip;
363       guint data_len;
364       GstBuffer *tmp = NULL;
365
366       /* each subframe starts with a variable length encoding */
367       data_len = 0;
368       for (skip = 0; skip < avail; skip++) {
369         data_len += data[skip];
370         if (data[skip] != 0xff)
371           break;
372       }
373       skip++;
374
375       /* this can not be possible, we have not enough data or the length
376        * decoding failed because we ran out of data. */
377       if (skip + data_len > avail)
378         goto wrong_size;
379
380       GST_LOG_OBJECT (rtpmp4adepay,
381           "subframe %u, header len %u, data len %u, left %u", i, skip, data_len,
382           avail);
383
384       /* take data out, skip the header */
385       pos += skip;
386       tmp = gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, pos, data_len);
387
388       /* skip data too */
389       skip += data_len;
390       pos += data_len;
391
392       /* update our pointers with what we consumed */
393       data += skip;
394       avail -= skip;
395
396       GST_BUFFER_PTS (tmp) = timestamp;
397       gst_rtp_drop_non_audio_meta (depayload, tmp);
398       gst_rtp_base_depayload_push (depayload, tmp);
399
400       /* shift ts for next buffers */
401       if (rtpmp4adepay->frame_len && timestamp != -1
402           && depayload->clock_rate != 0) {
403         timestamp +=
404             gst_util_uint64_scale_int (rtpmp4adepay->frame_len, GST_SECOND,
405             depayload->clock_rate);
406       }
407     }
408
409     /* just a check that lengths match */
410     if (avail) {
411       GST_ELEMENT_WARNING (depayload, STREAM, DECODE,
412           ("Packet invalid"), ("Not all payload consumed: "
413               "possible wrongly encoded packet."));
414     }
415
416     gst_buffer_unmap (outbuf, &map);
417     gst_buffer_unref (outbuf);
418   }
419   return NULL;
420
421   /* ERRORS */
422 wrong_size:
423   {
424     GST_ELEMENT_WARNING (rtpmp4adepay, STREAM, DECODE,
425         ("Packet did not validate"), ("wrong packet size"));
426     gst_buffer_unmap (outbuf, &map);
427     gst_buffer_unref (outbuf);
428     return NULL;
429   }
430 }
431
432 static GstStateChangeReturn
433 gst_rtp_mp4a_depay_change_state (GstElement * element,
434     GstStateChange transition)
435 {
436   GstRtpMP4ADepay *rtpmp4adepay;
437   GstStateChangeReturn ret;
438
439   rtpmp4adepay = GST_RTP_MP4A_DEPAY (element);
440
441   switch (transition) {
442     case GST_STATE_CHANGE_READY_TO_PAUSED:
443       gst_adapter_clear (rtpmp4adepay->adapter);
444       rtpmp4adepay->frame_len = 0;
445       rtpmp4adepay->numSubFrames = 0;
446       rtpmp4adepay->framed = FALSE;
447       break;
448     default:
449       break;
450   }
451
452   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
453
454   switch (transition) {
455     default:
456       break;
457   }
458   return ret;
459 }
460
461 gboolean
462 gst_rtp_mp4a_depay_plugin_init (GstPlugin * plugin)
463 {
464   return gst_element_register (plugin, "rtpmp4adepay",
465       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4A_DEPAY);
466 }