gstrtpamrdepay.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* GStreamer
 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
12 13 14 15 16 17
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
18 19 20 21 22 23 24 25
 */

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

#include <gst/rtp/gstrtpbuffer.h>

26
#include <stdlib.h>
27
#include <string.h>
28
#include "gstrtpamrdepay.h"
29

30 31
/* references:
 *
32 33 34
 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
 * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive Multi-Rate
 * Wideband (AMR-WB) Audio Codecs.
35 36
 */

37
/* elementfactory information */
38
static const GstElementDetails gst_rtp_amrdepay_details =
39
GST_ELEMENT_DETAILS ("RTP packet depayloader",
Wim Taymans's avatar
Wim Taymans committed
40
    "Codec/Depayloader/Network",
Wim Taymans's avatar
Wim Taymans committed
41 42
    "Extracts AMR audio from RTP packets (RFC 3267)",
    "Wim Taymans <wim@fluendo.com>");
43

44
/* RtpAMRDepay signals and args */
45 46 47 48 49 50 51 52
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
53
  ARG_0
54 55
};

56
/* input is an RTP packet
57 58 59
 *
 * params see RFC 3267, section 8.1
 */
60
static GstStaticPadTemplate gst_rtp_amr_depay_sink_template =
61 62 63
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
64
    GST_STATIC_CAPS ("application/x-rtp, "
65
        "media = (string) \"audio\", "
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
66 67 68
        "clock-rate = (int) 8000, "
        "encoding-name = (string) \"AMR\", "
        "encoding-params = (string) \"1\", "
69 70
        /* NOTE that all values must be strings in orde to be able to do SDP <->
         * GstCaps mapping. */
71 72 73
        "octet-align = (string) \"1\", "
        "crc = (string) { \"0\", \"1\" }, "
        "robust-sorting = (string) \"0\", " "interleaving = (string) \"0\""
74
        /* following options are not needed for a decoder
75 76 77 78 79 80 81 82
         *
         "mode-set = (int) [ 0, 7 ], "
         "mode-change-period = (int) [ 1, MAX ], "
         "mode-change-neighbor = (boolean) { TRUE, FALSE }, "
         "maxptime = (int) [ 20, MAX ], "
         "ptime = (int) [ 20, MAX ]"
         */
    )
83 84
    );

85
static GstStaticPadTemplate gst_rtp_amr_depay_src_template =
86 87 88 89 90
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/AMR, " "channels = (int) 1," "rate = (int) 8000")
    );
91

92 93 94 95
static gboolean gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload,
    GstCaps * caps);
static GstBuffer *gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload,
    GstBuffer * buf);
96

97 98
GST_BOILERPLATE (GstRtpAMRDepay, gst_rtp_amr_depay, GstBaseRTPDepayload,
    GST_TYPE_BASE_RTP_DEPAYLOAD);
99 100

static void
101
gst_rtp_amr_depay_base_init (gpointer klass)
102 103 104 105
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
106
      gst_static_pad_template_get (&gst_rtp_amr_depay_src_template));
107
  gst_element_class_add_pad_template (element_class,
108
      gst_static_pad_template_get (&gst_rtp_amr_depay_sink_template));
109

110
  gst_element_class_set_details (element_class, &gst_rtp_amrdepay_details);
111 112 113
}

static void
114
gst_rtp_amr_depay_class_init (GstRtpAMRDepayClass * klass)
115 116 117
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
118
  GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
119 120 121

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
122
  gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
123

124
  parent_class = g_type_class_peek_parent (klass);
125

126 127
  gstbasertpdepayload_class->process = gst_rtp_amr_depay_process;
  gstbasertpdepayload_class->set_caps = gst_rtp_amr_depay_setcaps;
128 129 130
}

static void
131 132
gst_rtp_amr_depay_init (GstRtpAMRDepay * rtpamrdepay,
    GstRtpAMRDepayClass * klass)
133
{
134 135 136 137 138 139
  GstBaseRTPDepayload *depayload;

  depayload = GST_BASE_RTP_DEPAYLOAD (rtpamrdepay);

  depayload->clock_rate = 8000;
  gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
140 141
}

142
static gboolean
143
gst_rtp_amr_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
144 145 146
{
  GstStructure *structure;
  GstCaps *srccaps;
147
  GstRtpAMRDepay *rtpamrdepay;
148
  const gchar *params;
Wim Taymans's avatar
Wim Taymans committed
149
  const gchar *str;
150
  gint clock_rate;
151

152
  rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
153 154 155

  structure = gst_caps_get_structure (caps, 0);

Wim Taymans's avatar
Wim Taymans committed
156
  if (!(str = gst_structure_get_string (structure, "octet-align")))
157
    rtpamrdepay->octet_align = FALSE;
Wim Taymans's avatar
Wim Taymans committed
158
  else
159
    rtpamrdepay->octet_align = (atoi (str) == 1);
160

Wim Taymans's avatar
Wim Taymans committed
161
  if (!(str = gst_structure_get_string (structure, "crc")))
162
    rtpamrdepay->crc = FALSE;
Wim Taymans's avatar
Wim Taymans committed
163
  else
164
    rtpamrdepay->crc = (atoi (str) == 1);
165

166
  if (rtpamrdepay->crc) {
167
    /* crc mode implies octet aligned mode */
168
    rtpamrdepay->octet_align = TRUE;
169 170
  }

Wim Taymans's avatar
Wim Taymans committed
171
  if (!(str = gst_structure_get_string (structure, "robust-sorting")))
172
    rtpamrdepay->robust_sorting = FALSE;
Wim Taymans's avatar
Wim Taymans committed
173
  else
174
    rtpamrdepay->robust_sorting = (atoi (str) == 1);
175

176
  if (rtpamrdepay->robust_sorting) {
177
    /* robust_sorting mode implies octet aligned mode */
178
    rtpamrdepay->octet_align = TRUE;
179 180
  }

Wim Taymans's avatar
Wim Taymans committed
181
  if (!(str = gst_structure_get_string (structure, "interleaving")))
182
    rtpamrdepay->interleaving = FALSE;
Wim Taymans's avatar
Wim Taymans committed
183
  else
184
    rtpamrdepay->interleaving = (atoi (str) == 1);
185

186
  if (rtpamrdepay->interleaving) {
187
    /* interleaving mode implies octet aligned mode */
188
    rtpamrdepay->octet_align = TRUE;
189 190
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
191
  if (!(params = gst_structure_get_string (structure, "encoding-params")))
192
    rtpamrdepay->channels = 1;
193
  else {
194
    rtpamrdepay->channels = atoi (params);
195 196
  }

197 198
  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
    clock_rate = 8000;
199

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
200
  /* we require 1 channel, 8000 Hz, octet aligned, no CRC,
201
   * no robust sorting, no interleaving for now */
202
  if (rtpamrdepay->channels != 1)
203
    return FALSE;
204
  if (clock_rate != 8000)
205
    return FALSE;
206
  if (rtpamrdepay->octet_align != TRUE)
207
    return FALSE;
208
  if (rtpamrdepay->robust_sorting != FALSE)
209
    return FALSE;
210
  if (rtpamrdepay->interleaving != FALSE)
211 212 213
    return FALSE;

  srccaps = gst_caps_new_simple ("audio/AMR",
214
      "channels", G_TYPE_INT, rtpamrdepay->channels,
215 216
      "rate", G_TYPE_INT, clock_rate, NULL);
  gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
217 218
  gst_caps_unref (srccaps);

219
  rtpamrdepay->negotiated = TRUE;
220 221 222 223

  return TRUE;
}

224 225 226 227 228 229
/* -1 is invalid */
static gint frame_size[16] = {
  12, 13, 15, 17, 19, 20, 26, 31,
  5, -1, -1, -1, -1, -1, -1, 0
};

230 231
static GstBuffer *
gst_rtp_amr_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
232
{
233
  GstRtpAMRDepay *rtpamrdepay;
234
  GstBuffer *outbuf = NULL;
235

236
  rtpamrdepay = GST_RTP_AMR_DEPAY (depayload);
237

238
  if (!rtpamrdepay->negotiated)
239 240
    goto not_negotiated;

241 242 243
  if (!gst_rtp_buffer_validate (buf)) {
    GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
        (NULL), ("AMR RTP packet did not validate"));
244
    goto bad_packet;
245
  }
246

247
  /* when we get here, 1 channel, 8000 Hz, octet aligned, no CRC, 
248
   * no robust sorting, no interleaving data is to be depayloaded */
249 250
  {
    gint payload_len;
251
    guint8 *payload, *p, *dp;
252
    guint32 timestamp;
253 254 255 256
    guint8 CMR;
    gint i, num_packets, num_nonempty_packets;
    gint amr_len;
    gint ILL, ILP;
257

258
    payload_len = gst_rtp_buffer_get_payload_len (buf);
259 260

    /* need at least 2 bytes for the header */
261 262 263
    if (payload_len < 2) {
      GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
          (NULL), ("AMR RTP payload too small (%d)", payload_len));
264
      goto bad_packet;
265
    }
266

267
    payload = gst_rtp_buffer_get_payload (buf);
268

269
    /* depay CMR. The CMR is used by the sender to request
270 271 272 273 274 275
     * a new encoding mode.
     *
     *  0 1 2 3 4 5 6 7 
     * +-+-+-+-+-+-+-+-+
     * | CMR   |R|R|R|R|
     * +-+-+-+-+-+-+-+-+
276 277 278
     */
    CMR = (payload[0] & 0xf0) >> 4;

279
    /* strip CMR header now, pack FT and the data for the decoder */
280 281
    payload_len -= 1;
    payload += 1;
282

283 284
    GST_DEBUG_OBJECT (rtpamrdepay, "payload len %d", payload_len);

285
    if (rtpamrdepay->interleaving) {
286 287 288 289 290 291
      ILL = (payload[0] & 0xf0) >> 4;
      ILP = (payload[0] & 0x0f);

      payload_len -= 1;
      payload += 1;

292 293 294
      if (ILP > ILL) {
        GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
            (NULL), ("AMR RTP wrong interleaving"));
295
        goto bad_packet;
296
      }
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    }

    /* 
     *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 
     * +-+-+-+-+-+-+-+-+..
     * |F|  FT   |Q|P|P| more FT..
     * +-+-+-+-+-+-+-+-+..
     */
    /* count number of packets by counting the FTs. Also
     * count number of amr data bytes and number of non-empty
     * packets (this is also the number of CRCs if present). */
    amr_len = 0;
    num_nonempty_packets = 0;
    num_packets = 0;
    for (i = 0; i < payload_len; i++) {
      gint fr_size;
      guint8 FT;

      FT = (payload[i] & 0x78) >> 3;

      fr_size = frame_size[FT];
318 319 320 321
      GST_DEBUG_OBJECT (rtpamrdepay, "frame size %d", fr_size);
      if (fr_size == -1) {
        GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
            (NULL), ("AMR RTP frame size == -1"));
322
        goto bad_packet;
323
      }
324 325 326 327 328 329 330 331 332 333 334

      if (fr_size > 0) {
        amr_len += fr_size;
        num_nonempty_packets++;
      }
      num_packets++;

      if ((payload[i] & 0x80) == 0)
        break;
    }

335
    if (rtpamrdepay->crc) {
336
      /* data len + CRC len + header bytes should be smaller than payload_len */
337 338 339
      if (num_packets + num_nonempty_packets + amr_len > payload_len) {
        GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
            (NULL), ("AMR RTP wrong length 1"));
340
        goto bad_packet;
341
      }
342 343
    } else {
      /* data len + header bytes should be smaller than payload_len */
344 345 346
      if (num_packets + amr_len > payload_len) {
        GST_ELEMENT_WARNING (rtpamrdepay, STREAM, DECODE,
            (NULL), ("AMR RTP wrong length 2"));
347
        goto bad_packet;
348
      }
349 350
    }

351
    timestamp = gst_rtp_buffer_get_timestamp (buf);
352 353

    outbuf = gst_buffer_new_and_alloc (payload_len);
354
    GST_BUFFER_TIMESTAMP (outbuf) =
355 356
        gst_util_uint64_scale_int (timestamp, GST_SECOND,
        depayload->clock_rate);
357

358 359 360 361
    /* point to destination */
    p = GST_BUFFER_DATA (outbuf);
    /* point to first data packet */
    dp = payload + num_packets;
362
    if (rtpamrdepay->crc) {
363 364 365 366 367 368 369
      /* skip CRC if present */
      dp += num_nonempty_packets;
    }

    for (i = 0; i < num_packets; i++) {
      gint fr_size;

370 371 372
      /* copy FT, clear F bit */
      *p++ = payload[i] & 0x7f;

373 374 375 376
      fr_size = frame_size[(payload[i] & 0x78) >> 3];
      if (fr_size > 0) {
        /* copy data packet, FIXME, calc CRC here. */
        memcpy (p, dp, fr_size);
377

378 379 380 381
        p += fr_size;
        dp += fr_size;
      }
    }
382 383
    gst_buffer_set_caps (outbuf,
        GST_PAD_CAPS (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload)));
384

385
    GST_DEBUG ("gst_rtp_amr_depay_chain: pushing buffer of size %d",
386 387 388
        GST_BUFFER_SIZE (outbuf));
  }

389
  return outbuf;
390

391
  /* ERRORS */
392 393
not_negotiated:
  {
394
    GST_ELEMENT_ERROR (rtpamrdepay, STREAM, NOT_IMPLEMENTED,
395
        (NULL), ("not negotiated"));
396
    return NULL;
397
  }
398 399
bad_packet:
  {
400
    /* no fatal error */
401
    return NULL;
402 403 404 405
  }
}

gboolean
406
gst_rtp_amr_depay_plugin_init (GstPlugin * plugin)
407
{
408
  return gst_element_register (plugin, "rtpamrdepay",
Wim Taymans's avatar
Wim Taymans committed
409
      GST_RANK_MARGINAL, GST_TYPE_RTP_AMR_DEPAY);
410
}