2 * Copyright (C) <2007> Nokia Corporation (contact <stefan.kost@nokia.com>)
3 * <2007> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
24 #include <gst/base/gstbitreader.h>
25 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmp4adepay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4adepay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4adepay_debug)
33 static GstStaticPadTemplate gst_rtp_mp4a_depay_src_template =
34 GST_STATIC_PAD_TEMPLATE ("src",
37 GST_STATIC_CAPS ("audio/mpeg,"
38 "mpegversion = (int) 4," "framed = (boolean) true, "
39 "stream-format = (string) raw")
42 static GstStaticPadTemplate gst_rtp_mp4a_depay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"audio\", "
48 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
49 "clock-rate = (int) [1, MAX ], "
50 "encoding-name = (string) \"MP4A-LATM\""
51 /* All optional parameters
53 * "profile-level-id=[1,MAX]"
59 GST_BOILERPLATE (GstRtpMP4ADepay, gst_rtp_mp4a_depay, GstBaseRTPDepayload,
60 GST_TYPE_BASE_RTP_DEPAYLOAD);
62 static void gst_rtp_mp4a_depay_finalize (GObject * object);
64 static gboolean gst_rtp_mp4a_depay_setcaps (GstBaseRTPDepayload * depayload,
66 static GstBuffer *gst_rtp_mp4a_depay_process (GstBaseRTPDepayload * depayload,
69 static GstStateChangeReturn gst_rtp_mp4a_depay_change_state (GstElement *
70 element, GstStateChange transition);
74 gst_rtp_mp4a_depay_base_init (gpointer klass)
76 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
78 gst_element_class_add_static_pad_template (element_class,
79 &gst_rtp_mp4a_depay_src_template);
80 gst_element_class_add_static_pad_template (element_class,
81 &gst_rtp_mp4a_depay_sink_template);
83 gst_element_class_set_details_simple (element_class,
84 "RTP MPEG4 audio depayloader", "Codec/Depayloader/Network/RTP",
85 "Extracts MPEG4 audio from RTP packets (RFC 3016)",
86 "Nokia Corporation (contact <stefan.kost@nokia.com>), "
87 "Wim Taymans <wim.taymans@gmail.com>");
91 gst_rtp_mp4a_depay_class_init (GstRtpMP4ADepayClass * klass)
93 GObjectClass *gobject_class;
94 GstElementClass *gstelement_class;
95 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
97 gobject_class = (GObjectClass *) klass;
98 gstelement_class = (GstElementClass *) klass;
99 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
101 gobject_class->finalize = gst_rtp_mp4a_depay_finalize;
103 gstelement_class->change_state = gst_rtp_mp4a_depay_change_state;
105 gstbasertpdepayload_class->process = gst_rtp_mp4a_depay_process;
106 gstbasertpdepayload_class->set_caps = gst_rtp_mp4a_depay_setcaps;
108 GST_DEBUG_CATEGORY_INIT (rtpmp4adepay_debug, "rtpmp4adepay", 0,
109 "MPEG4 audio RTP Depayloader");
113 gst_rtp_mp4a_depay_init (GstRtpMP4ADepay * rtpmp4adepay,
114 GstRtpMP4ADepayClass * klass)
116 rtpmp4adepay->adapter = gst_adapter_new ();
120 gst_rtp_mp4a_depay_finalize (GObject * object)
122 GstRtpMP4ADepay *rtpmp4adepay;
124 rtpmp4adepay = GST_RTP_MP4A_DEPAY (object);
126 g_object_unref (rtpmp4adepay->adapter);
127 rtpmp4adepay->adapter = NULL;
129 G_OBJECT_CLASS (parent_class)->finalize (object);
132 static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000,
133 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000
137 gst_rtp_mp4a_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
139 GstStructure *structure;
140 GstRtpMP4ADepay *rtpmp4adepay;
145 gint channels = 2; /* default */
148 rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
150 structure = gst_caps_get_structure (caps, 0);
152 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
153 clock_rate = 90000; /* default */
154 depayload->clock_rate = clock_rate;
156 if (!gst_structure_get_int (structure, "object", &object_type))
157 object_type = 2; /* AAC LC default */
159 srccaps = gst_caps_new_simple ("audio/mpeg",
160 "mpegversion", G_TYPE_INT, 4,
161 "framed", G_TYPE_BOOLEAN, TRUE, "channels", G_TYPE_INT, channels,
162 "stream-format", G_TYPE_STRING, "raw", NULL);
164 if ((str = gst_structure_get_string (structure, "config"))) {
167 g_value_init (&v, GST_TYPE_BUFFER);
168 if (gst_value_deserialize (&v, str)) {
174 guint8 obj_type = 0, sr_idx = 0, channels = 0;
177 buffer = gst_value_get_buffer (&v);
178 gst_buffer_ref (buffer);
181 data = GST_BUFFER_DATA (buffer);
182 size = GST_BUFFER_SIZE (buffer);
185 GST_WARNING_OBJECT (depayload, "config too short (%d < 2)", size);
189 /* Parse StreamMuxConfig according to ISO/IEC 14496-3:
191 * audioMuxVersion == 0 (1 bit)
192 * allStreamsSameTimeFraming == 1 (1 bit)
193 * numSubFrames == rtpmp4adepay->numSubFrames (6 bits)
194 * numProgram == 0 (4 bits)
195 * numLayer == 0 (3 bits)
197 * We only require audioMuxVersion == 0;
199 * The remaining bit of the second byte and the rest of the bits are used
200 * for audioSpecificConfig which we need to set in codec_info.
202 if ((data[0] & 0x80) != 0x00) {
203 GST_WARNING_OBJECT (depayload, "unknown audioMuxVersion 1");
207 rtpmp4adepay->numSubFrames = (data[0] & 0x3F);
209 GST_LOG_OBJECT (rtpmp4adepay, "numSubFrames %d",
210 rtpmp4adepay->numSubFrames);
212 /* shift rest of string 15 bits down */
214 for (i = 0; i < size; i++) {
215 data[i] = ((data[i + 1] & 1) << 7) | ((data[i + 2] & 0xfe) >> 1);
217 /* ignore remaining bit, we're only interested in full bytes */
218 GST_BUFFER_SIZE (buffer) = size;
220 gst_bit_reader_init (&br, data, size);
222 /* any object type is fine, we need to copy it to the profile-level-id field. */
223 if (!gst_bit_reader_get_bits_uint8 (&br, &obj_type, 5))
226 GST_WARNING_OBJECT (depayload, "invalid object type 0");
230 if (!gst_bit_reader_get_bits_uint8 (&br, &sr_idx, 4))
232 if (sr_idx >= G_N_ELEMENTS (aac_sample_rates) && sr_idx != 15) {
233 GST_WARNING_OBJECT (depayload, "invalid sample rate index %d", sr_idx);
236 GST_LOG_OBJECT (rtpmp4adepay, "sample rate index %u", sr_idx);
238 if (!gst_bit_reader_get_bits_uint8 (&br, &channels, 4))
241 GST_WARNING_OBJECT (depayload, "invalid channels %u", (guint) channels);
245 /* rtp rate depends on sampling rate of the audio */
247 /* index of 15 means we get the rate in the next 24 bits */
248 if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
251 /* else use the rate from the table */
252 rate = aac_sample_rates[sr_idx];
255 rtpmp4adepay->frame_len = 1024;
265 guint8 frameLenFlag = 0;
267 if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
269 rtpmp4adepay->frame_len = 960;
276 gst_caps_set_simple (srccaps,
277 "channels", G_TYPE_INT, (gint) channels,
278 "rate", G_TYPE_INT, (gint) rate,
279 "codec_data", GST_TYPE_BUFFER, buffer, NULL);
280 gst_buffer_unref (buffer);
282 g_warning ("cannot convert config to buffer");
286 res = gst_pad_set_caps (depayload->srcpad, srccaps);
287 gst_caps_unref (srccaps);
293 gst_rtp_mp4a_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
295 GstRtpMP4ADepay *rtpmp4adepay;
298 rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
300 /* flush remaining data on discont */
301 if (GST_BUFFER_IS_DISCONT (buf)) {
302 gst_adapter_clear (rtpmp4adepay->adapter);
305 outbuf = gst_rtp_buffer_get_payload_buffer (buf);
307 gst_buffer_copy_metadata (outbuf, buf, GST_BUFFER_COPY_TIMESTAMPS);
308 gst_adapter_push (rtpmp4adepay->adapter, outbuf);
310 /* RTP marker bit indicates the last packet of the AudioMuxElement => create
311 * and push a buffer */
312 if (gst_rtp_buffer_get_marker (buf)) {
317 GstClockTime timestamp;
319 avail = gst_adapter_available (rtpmp4adepay->adapter);
320 timestamp = gst_adapter_prev_timestamp (rtpmp4adepay->adapter, NULL);
322 GST_LOG_OBJECT (rtpmp4adepay, "have marker and %u available", avail);
324 outbuf = gst_adapter_take_buffer (rtpmp4adepay->adapter, avail);
325 data = GST_BUFFER_DATA (outbuf);
326 /* position in data we are at */
329 /* looping through the number of sub-frames in the audio payload */
330 for (i = 0; i <= rtpmp4adepay->numSubFrames; i++) {
331 /* determine payload length and set buffer data pointer accordingly */
334 GstBuffer *tmp = NULL;
336 /* each subframe starts with a variable length encoding */
338 for (skip = 0; skip < avail; skip++) {
339 data_len += data[skip];
340 if (data[skip] != 0xff)
345 /* this can not be possible, we have not enough data or the length
346 * decoding failed because we ran out of data. */
347 if (skip + data_len > avail)
350 GST_LOG_OBJECT (rtpmp4adepay,
351 "subframe %u, header len %u, data len %u, left %u", i, skip, data_len,
354 /* take data out, skip the header */
356 tmp = gst_buffer_create_sub (outbuf, pos, data_len);
362 /* update our pointers whith what we consumed */
366 GST_BUFFER_TIMESTAMP (tmp) = timestamp;
367 gst_base_rtp_depayload_push (depayload, tmp);
369 /* shift ts for next buffers */
370 if (rtpmp4adepay->frame_len && timestamp != -1
371 && depayload->clock_rate != 0) {
373 gst_util_uint64_scale_int (rtpmp4adepay->frame_len, GST_SECOND,
374 depayload->clock_rate);
378 /* just a check that lengths match */
380 GST_ELEMENT_WARNING (depayload, STREAM, DECODE,
381 ("Packet invalid"), ("Not all payload consumed: "
382 "possible wrongly encoded packet."));
385 gst_buffer_unref (outbuf);
392 GST_ELEMENT_WARNING (rtpmp4adepay, STREAM, DECODE,
393 ("Packet did not validate"), ("wrong packet size"));
394 gst_buffer_unref (outbuf);
399 static GstStateChangeReturn
400 gst_rtp_mp4a_depay_change_state (GstElement * element,
401 GstStateChange transition)
403 GstRtpMP4ADepay *rtpmp4adepay;
404 GstStateChangeReturn ret;
406 rtpmp4adepay = GST_RTP_MP4A_DEPAY (element);
408 switch (transition) {
409 case GST_STATE_CHANGE_READY_TO_PAUSED:
410 gst_adapter_clear (rtpmp4adepay->adapter);
411 rtpmp4adepay->frame_len = 0;
412 rtpmp4adepay->numSubFrames = 0;
418 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
420 switch (transition) {
428 gst_rtp_mp4a_depay_plugin_init (GstPlugin * plugin)
430 return gst_element_register (plugin, "rtpmp4adepay",
431 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4A_DEPAY);