gstrtpmp4adepay.c 13.6 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) <2007> Nokia Corporation (contact <stefan.kost@nokia.com>)
3
 *               <2007> Wim Taymans <wim.taymans@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19 20 21 22 23
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

24
#include <gst/base/gstbitreader.h>
25
#include <gst/rtp/gstrtpbuffer.h>
26
#include <gst/audio/audio.h>
27 28 29

#include <string.h>
#include "gstrtpmp4adepay.h"
30
#include "gstrtputils.h"
31 32 33 34 35 36 37 38 39

GST_DEBUG_CATEGORY_STATIC (rtpmp4adepay_debug);
#define GST_CAT_DEFAULT (rtpmp4adepay_debug)

static GstStaticPadTemplate gst_rtp_mp4a_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/mpeg,"
40
        "mpegversion = (int) 4," "framed = (boolean) { false, true }, "
41
        "stream-format = (string) raw")
42 43 44 45 46 47 48 49 50 51 52 53 54
    );

static GstStaticPadTemplate gst_rtp_mp4a_depay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"audio\", "
        "clock-rate = (int) [1, MAX ], "
        "encoding-name = (string) \"MP4A-LATM\""
        /* All optional parameters
         *
         * "profile-level-id=[1,MAX]"
55
         * "config="
56 57 58 59
         */
    )
    );

60 61
#define gst_rtp_mp4a_depay_parent_class parent_class
G_DEFINE_TYPE (GstRtpMP4ADepay, gst_rtp_mp4a_depay,
62
    GST_TYPE_RTP_BASE_DEPAYLOAD);
63 64 65

static void gst_rtp_mp4a_depay_finalize (GObject * object);

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 72 73 74 75 76 77 78 79

static GstStateChangeReturn gst_rtp_mp4a_depay_change_state (GstElement *
    element, GstStateChange transition);


static void
gst_rtp_mp4a_depay_class_init (GstRtpMP4ADepayClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
80
  GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
81 82 83

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
84
  gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
85 86 87 88 89

  gobject_class->finalize = gst_rtp_mp4a_depay_finalize;

  gstelement_class->change_state = gst_rtp_mp4a_depay_change_state;

90
  gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_mp4a_depay_process;
91
  gstrtpbasedepayload_class->set_caps = gst_rtp_mp4a_depay_setcaps;
92

93 94 95 96
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_mp4a_depay_src_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_mp4a_depay_sink_template);
97

98
  gst_element_class_set_static_metadata (gstelement_class,
99 100 101 102 103
      "RTP MPEG4 audio depayloader", "Codec/Depayloader/Network/RTP",
      "Extracts MPEG4 audio from RTP packets (RFC 3016)",
      "Nokia Corporation (contact <stefan.kost@nokia.com>), "
      "Wim Taymans <wim.taymans@gmail.com>");

104 105 106 107 108
  GST_DEBUG_CATEGORY_INIT (rtpmp4adepay_debug, "rtpmp4adepay", 0,
      "MPEG4 audio RTP Depayloader");
}

static void
109
gst_rtp_mp4a_depay_init (GstRtpMP4ADepay * rtpmp4adepay)
110 111
{
  rtpmp4adepay->adapter = gst_adapter_new ();
112
  rtpmp4adepay->framed = FALSE;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

static void
gst_rtp_mp4a_depay_finalize (GObject * object)
{
  GstRtpMP4ADepay *rtpmp4adepay;

  rtpmp4adepay = GST_RTP_MP4A_DEPAY (object);

  g_object_unref (rtpmp4adepay->adapter);
  rtpmp4adepay->adapter = NULL;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

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 136 137 138
{
  GstStructure *structure;
  GstRtpMP4ADepay *rtpmp4adepay;
  GstCaps *srccaps;
  const gchar *str;
139 140
  gint clock_rate;
  gint object_type;
141
  gint channels = 2;            /* default */
142
  gboolean res;
143 144 145

  rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);

146 147
  rtpmp4adepay->framed = FALSE;

148 149
  structure = gst_caps_get_structure (caps, 0);

150 151
  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
    clock_rate = 90000;         /* default */
152 153
  depayload->clock_rate = clock_rate;

154 155
  if (!gst_structure_get_int (structure, "object", &object_type))
    object_type = 2;            /* AAC LC default */
156 157 158

  srccaps = gst_caps_new_simple ("audio/mpeg",
      "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 163 164 165 166 167

  if ((str = gst_structure_get_string (structure, "config"))) {
    GValue v = { 0 };

    g_value_init (&v, GST_TYPE_BUFFER);
    if (gst_value_deserialize (&v, str)) {
      GstBuffer *buffer;
Wim Taymans's avatar
Wim Taymans committed
168
      GstMapInfo map;
169
      guint8 *data;
170
      gsize size;
171
      gint i;
172 173
      guint32 rate = 0;
      guint8 obj_type = 0, sr_idx = 0, channels = 0;
174
      GstBitReader br;
175 176 177 178 179

      buffer = gst_value_get_buffer (&v);
      gst_buffer_ref (buffer);
      g_value_unset (&v);

Wim Taymans's avatar
Wim Taymans committed
180 181 182
      gst_buffer_map (buffer, &map, GST_MAP_READ);
      data = map.data;
      size = map.size;
183

184
      if (size < 2) {
185 186
        GST_WARNING_OBJECT (depayload, "config too short (%d < 2)",
            (gint) size);
187 188 189 190 191 192 193
        goto bad_config;
      }

      /* Parse StreamMuxConfig according to ISO/IEC 14496-3:
       *
       * audioMuxVersion           == 0 (1 bit)
       * allStreamsSameTimeFraming == 1 (1 bit)
194
       * numSubFrames              == rtpmp4adepay->numSubFrames (6 bits)
195 196 197 198 199 200 201 202 203 204 205 206 207
       * numProgram                == 0 (4 bits)
       * numLayer                  == 0 (3 bits)
       *
       * We only require audioMuxVersion == 0;
       *
       * The remaining bit of the second byte and the rest of the bits are used
       * for audioSpecificConfig which we need to set in codec_info.
       */
      if ((data[0] & 0x80) != 0x00) {
        GST_WARNING_OBJECT (depayload, "unknown audioMuxVersion 1");
        goto bad_config;
      }

208 209 210 211 212
      rtpmp4adepay->numSubFrames = (data[0] & 0x3F);

      GST_LOG_OBJECT (rtpmp4adepay, "numSubFrames %d",
          rtpmp4adepay->numSubFrames);

213 214 215 216 217
      /* shift rest of string 15 bits down */
      size -= 2;
      for (i = 0; i < size; i++) {
        data[i] = ((data[i + 1] & 1) << 7) | ((data[i + 2] & 0xfe) >> 1);
      }
218 219 220 221 222 223 224 225 226 227 228 229 230

      gst_bit_reader_init (&br, data, size);

      /* any object type is fine, we need to copy it to the profile-level-id field. */
      if (!gst_bit_reader_get_bits_uint8 (&br, &obj_type, 5))
        goto bad_config;
      if (obj_type == 0) {
        GST_WARNING_OBJECT (depayload, "invalid object type 0");
        goto bad_config;
      }

      if (!gst_bit_reader_get_bits_uint8 (&br, &sr_idx, 4))
        goto bad_config;
231
      if (sr_idx >= G_N_ELEMENTS (aac_sample_rates) && sr_idx != 15) {
232 233 234 235
        GST_WARNING_OBJECT (depayload, "invalid sample rate index %d", sr_idx);
        goto bad_config;
      }
      GST_LOG_OBJECT (rtpmp4adepay, "sample rate index %u", sr_idx);
236

237 238 239 240 241 242 243 244 245 246 247 248
      if (!gst_bit_reader_get_bits_uint8 (&br, &channels, 4))
        goto bad_config;
      if (channels > 7) {
        GST_WARNING_OBJECT (depayload, "invalid channels %u", (guint) channels);
        goto bad_config;
      }

      /* rtp rate depends on sampling rate of the audio */
      if (sr_idx == 15) {
        /* index of 15 means we get the rate in the next 24 bits */
        if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
          goto bad_config;
249 250
      } else if (sr_idx >= G_N_ELEMENTS (aac_sample_rates)) {
        goto bad_config;
251
      } else {
252 253
        /* else use the rate from the table */
        rate = aac_sample_rates[sr_idx];
254 255
      }

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
      rtpmp4adepay->frame_len = 1024;

      switch (obj_type) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 6:
        case 7:
        {
          guint8 frameLenFlag = 0;

          if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
            if (frameLenFlag)
              rtpmp4adepay->frame_len = 960;
          break;
        }
        default:
          break;
      }
276

277
      /* ignore remaining bit, we're only interested in full bytes */
Wim Taymans's avatar
Wim Taymans committed
278 279
      gst_buffer_resize (buffer, 0, size);
      gst_buffer_unmap (buffer, &map);
280 281
      data = NULL;

282
      gst_caps_set_simple (srccaps,
283 284
          "channels", G_TYPE_INT, (gint) channels,
          "rate", G_TYPE_INT, (gint) rate,
285
          "codec_data", GST_TYPE_BUFFER, buffer, NULL);
286 287
    bad_config:
      if (data)
Wim Taymans's avatar
Wim Taymans committed
288
        gst_buffer_unmap (buffer, &map);
289 290 291 292 293
      gst_buffer_unref (buffer);
    } else {
      g_warning ("cannot convert config to buffer");
    }
  }
294
  res = gst_pad_set_caps (depayload->srcpad, srccaps);
295 296
  gst_caps_unref (srccaps);

297
  return res;
298 299 300
}

static GstBuffer *
301
gst_rtp_mp4a_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
302 303 304
{
  GstRtpMP4ADepay *rtpmp4adepay;
  GstBuffer *outbuf;
Wim Taymans's avatar
Wim Taymans committed
305
  GstMapInfo map;
306 307 308 309

  rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);

  /* 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  if (!rtpmp4adepay->framed) {
    if (gst_rtp_buffer_get_marker (rtp)) {
      GstCaps *caps;

      rtpmp4adepay->framed = TRUE;

      gst_rtp_base_depayload_push (depayload, outbuf);

      caps = gst_pad_get_current_caps (depayload->srcpad);
      caps = gst_caps_make_writable (caps);
      gst_caps_set_simple (caps, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
      gst_pad_set_caps (depayload->srcpad, caps);
      gst_caps_unref (caps);
      return NULL;
    } else {
      return outbuf;
    }
  }

335
  outbuf = gst_buffer_make_writable (outbuf);
336
  GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (rtp->buffer);
337 338 339 340
  gst_adapter_push (rtpmp4adepay->adapter, outbuf);

  /* RTP marker bit indicates the last packet of the AudioMuxElement => create
   * 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);
Wim Taymans's avatar
Wim Taymans committed
354 355
    gst_buffer_map (outbuf, &map, GST_MAP_READ);
    data = map.data;
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    /* position in data we are at */
    pos = 0;

    /* looping through the number of sub-frames in the audio payload */
    for (i = 0; i <= rtpmp4adepay->numSubFrames; i++) {
      /* determine payload length and set buffer data pointer accordingly */
      guint skip;
      guint data_len;
      GstBuffer *tmp = NULL;

      /* each subframe starts with a variable length encoding */
      data_len = 0;
      for (skip = 0; skip < avail; skip++) {
        data_len += data[skip];
        if (data[skip] != 0xff)
          break;
      }
      skip++;

      /* this can not be possible, we have not enough data or the length
       * decoding failed because we ran out of data. */
377
      if (skip + data_len > avail)
378 379 380 381 382
        goto wrong_size;

      GST_LOG_OBJECT (rtpmp4adepay,
          "subframe %u, header len %u, data len %u, left %u", i, skip, data_len,
          avail);
383

384 385
      /* take data out, skip the header */
      pos += skip;
386
      tmp = gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, pos, data_len);
387

388 389 390
      /* skip data too */
      skip += data_len;
      pos += data_len;
391

392 393 394
      /* update our pointers whith what we consumed */
      data += skip;
      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 401 402 403 404 405
      /* shift ts for next buffers */
      if (rtpmp4adepay->frame_len && timestamp != -1
          && depayload->clock_rate != 0) {
        timestamp +=
            gst_util_uint64_scale_int (rtpmp4adepay->frame_len, GST_SECOND,
            depayload->clock_rate);
406
      }
407 408 409 410 411 412 413 414
    }

    /* just a check that lengths match */
    if (avail) {
      GST_ELEMENT_WARNING (depayload, STREAM, DECODE,
          ("Packet invalid"), ("Not all payload consumed: "
              "possible wrongly encoded packet."));
    }
415

Wim Taymans's avatar
Wim Taymans committed
416
    gst_buffer_unmap (outbuf, &map);
417
    gst_buffer_unref (outbuf);
418 419 420
  }
  return NULL;

421 422 423 424 425
  /* ERRORS */
wrong_size:
  {
    GST_ELEMENT_WARNING (rtpmp4adepay, STREAM, DECODE,
        ("Packet did not validate"), ("wrong packet size"));
Wim Taymans's avatar
Wim Taymans committed
426
    gst_buffer_unmap (outbuf, &map);
427
    gst_buffer_unref (outbuf);
428 429
    return NULL;
  }
430 431 432 433 434 435 436 437 438 439 440 441 442 443
}

static GstStateChangeReturn
gst_rtp_mp4a_depay_change_state (GstElement * element,
    GstStateChange transition)
{
  GstRtpMP4ADepay *rtpmp4adepay;
  GstStateChangeReturn ret;

  rtpmp4adepay = GST_RTP_MP4A_DEPAY (element);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      gst_adapter_clear (rtpmp4adepay->adapter);
444 445
      rtpmp4adepay->frame_len = 0;
      rtpmp4adepay->numSubFrames = 0;
446
      rtpmp4adepay->framed = FALSE;
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
      break;
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  switch (transition) {
    default:
      break;
  }
  return ret;
}

gboolean
gst_rtp_mp4a_depay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpmp4adepay",
465
      GST_RANK_SECONDARY, GST_TYPE_RTP_MP4A_DEPAY);
466
}