2 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpamrpay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpamrpay_debug);
31 #define GST_CAT_DEFAULT (rtpamrpay_debug)
35 * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
36 * Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive
37 * Multi-Rate Wideband (AMR-WB) Audio Codecs.
39 * ETSI TS 126 201 V6.0.0 (2004-12) - Digital cellular telecommunications system (Phase 2+);
40 * Universal Mobile Telecommunications System (UMTS);
41 * AMR speech codec, wideband;
43 * (3GPP TS 26.201 version 6.0.0 Release 6)
46 /* elementfactory information */
47 static const GstElementDetails gst_rtp_amrpay_details =
48 GST_ELEMENT_DETAILS ("RTP AMR payloader",
49 "Codec/Payloader/Network",
50 "Payload-encode AMR or AMR-WB audio into RTP packets (RFC 3267)",
51 "Wim Taymans <wim.taymans@gmail.com>");
53 static GstStaticPadTemplate gst_rtp_amr_pay_sink_template =
54 GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_STATIC_CAPS ("audio/AMR, channels=(int)1, rate=(int)8000; "
58 "audio/AMR-WB, channels=(int)1, rate=(int)16000")
61 static GstStaticPadTemplate gst_rtp_amr_pay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
65 GST_STATIC_CAPS ("application/x-rtp, "
66 "media = (string) \"audio\", "
67 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
68 "clock-rate = (int) 8000, "
69 "encoding-name = (string) \"AMR\", "
70 "encoding-params = (string) \"1\", "
71 "octet-align = (string) \"1\", "
72 "crc = (string) \"0\", "
73 "robust-sorting = (string) \"0\", "
74 "interleaving = (string) \"0\", "
75 "mode-set = (int) [ 0, 7 ], "
76 "mode-change-period = (int) [ 1, MAX ], "
77 "mode-change-neighbor = (string) { \"0\", \"1\" }, "
78 "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ];"
80 "media = (string) \"audio\", "
81 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
82 "clock-rate = (int) 16000, "
83 "encoding-name = (string) \"AMR-WB\", "
84 "encoding-params = (string) \"1\", "
85 "octet-align = (string) \"1\", "
86 "crc = (string) \"0\", "
87 "robust-sorting = (string) \"0\", "
88 "interleaving = (string) \"0\", "
89 "mode-set = (int) [ 0, 7 ], "
90 "mode-change-period = (int) [ 1, MAX ], "
91 "mode-change-neighbor = (string) { \"0\", \"1\" }, "
92 "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ]")
95 static gboolean gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload,
97 static GstFlowReturn gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * pad,
100 GST_BOILERPLATE (GstRtpAMRPay, gst_rtp_amr_pay, GstBaseRTPPayload,
101 GST_TYPE_BASE_RTP_PAYLOAD);
104 gst_rtp_amr_pay_base_init (gpointer klass)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
108 gst_element_class_add_pad_template (element_class,
109 gst_static_pad_template_get (&gst_rtp_amr_pay_src_template));
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&gst_rtp_amr_pay_sink_template));
113 gst_element_class_set_details (element_class, &gst_rtp_amrpay_details);
117 gst_rtp_amr_pay_class_init (GstRtpAMRPayClass * klass)
119 GstBaseRTPPayloadClass *gstbasertppayload_class;
121 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
123 parent_class = g_type_class_peek_parent (klass);
125 gstbasertppayload_class->set_caps = gst_rtp_amr_pay_setcaps;
126 gstbasertppayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
128 GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
129 "AMR/AMR-WB RTP Payloader");
133 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay, GstRtpAMRPayClass * klass)
135 /* needed because of GST_BOILERPLATE */
139 gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
141 GstRtpAMRPay *rtpamrpay;
142 const GstStructure *s;
145 rtpamrpay = GST_RTP_AMR_PAY (basepayload);
147 /* figure out the mode Narrow or Wideband */
148 s = gst_caps_get_structure (caps, 0);
149 if ((str = gst_structure_get_name (s))) {
150 if (strcmp (str, "audio/AMR") == 0)
151 rtpamrpay->mode = GST_RTP_AMR_P_MODE_NB;
152 else if (strcmp (str, "audio/AMR-WB") == 0)
153 rtpamrpay->mode = GST_RTP_AMR_P_MODE_WB;
159 if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
160 gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
162 gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR-WB",
165 gst_basertppayload_set_outcaps (basepayload,
166 "encoding-params", G_TYPE_STRING, "1", "octet-align", G_TYPE_STRING, "1",
167 /* don't set the defaults
169 * "crc", G_TYPE_STRING, "0",
170 * "robust-sorting", G_TYPE_STRING, "0",
171 * "interleaving", G_TYPE_STRING, "0",
180 GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
187 static gint nb_frame_size[16] = {
188 12, 13, 15, 17, 19, 20, 26, 31,
189 5, -1, -1, -1, -1, -1, -1, 0
191 static gint wb_frame_size[16] = {
192 17, 23, 32, 36, 40, 46, 50, 58,
193 60, -1, -1, -1, -1, -1, -1, 0
197 gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * basepayload,
200 GstRtpAMRPay *rtpamrpay;
202 guint size, payload_len;
204 guint8 *payload, *data, *payload_amr;
205 GstClockTime timestamp, duration;
206 guint packet_len, mtu;
207 gint i, num_packets, num_nonempty_packets;
212 rtpamrpay = GST_RTP_AMR_PAY (basepayload);
213 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpamrpay);
215 size = GST_BUFFER_SIZE (buffer);
216 data = GST_BUFFER_DATA (buffer);
217 timestamp = GST_BUFFER_TIMESTAMP (buffer);
218 duration = GST_BUFFER_DURATION (buffer);
219 discont = GST_BUFFER_IS_DISCONT (buffer);
221 /* setup frame size pointer */
222 if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
223 frame_size = nb_frame_size;
225 frame_size = wb_frame_size;
227 GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
230 * octet aligned, no interleaving, single channel, no CRC,
231 * no robust-sorting. To fix this you need to implement the downstream
232 * negotiation function. */
234 /* first count number of packets and total amr frame size */
235 amr_len = num_packets = num_nonempty_packets = 0;
236 for (i = 0; i < size; i++) {
240 FT = (data[i] & 0x78) >> 3;
242 fr_size = frame_size[FT];
243 GST_DEBUG_OBJECT (basepayload, "frame size %d", fr_size);
244 /* FIXME, we don't handle this yet.. */
249 num_nonempty_packets++;
254 goto incomplete_frame;
256 /* we need one extra byte for the CMR, the ToC is in the input
258 payload_len = size + 1;
260 /* get packet len to check against MTU */
261 packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
262 if (packet_len > mtu)
265 /* now alloc output buffer */
266 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
269 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
271 /* FIXME: when we do more than one AMR frame per packet, fix this */
272 if (duration != GST_CLOCK_TIME_NONE)
273 GST_BUFFER_DURATION (outbuf) = duration;
275 GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
279 GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
280 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
281 gst_rtp_buffer_set_marker (outbuf, TRUE);
285 /* get payload, this is now writable */
286 payload = gst_rtp_buffer_get_payload (outbuf);
293 payload[0] = 0xF0; /* CMR, no specific mode requested */
295 /* this is where we copy the AMR data, after num_packets FTs and the
297 payload_amr = payload + num_packets + 1;
299 /* copy data in payload, first we copy all the FTs then all
300 * the AMR data. The last FT has to have the F flag cleared. */
301 for (i = 1; i <= num_packets; i++) {
307 * |F| FT |Q|P|P| more FT...
310 FT = (*data & 0x78) >> 3;
312 fr_size = frame_size[FT];
314 if (i == num_packets)
315 /* last packet, clear F flag */
316 payload[i] = *data & 0x7f;
319 payload[i] = *data | 0x80;
321 memcpy (payload_amr, &data[1], fr_size);
323 /* all sizes are > 0 since we checked for that above */
325 payload_amr += fr_size;
328 gst_buffer_unref (buffer);
330 ret = gst_basertppayload_push (basepayload, outbuf);
337 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
338 (NULL), ("received AMR frame with size <= 0"));
339 gst_buffer_unref (buffer);
341 return GST_FLOW_ERROR;
345 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
346 (NULL), ("received incomplete AMR frames"));
347 gst_buffer_unref (buffer);
349 return GST_FLOW_ERROR;
353 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
354 (NULL), ("received too many AMR frames for MTU"));
355 gst_buffer_unref (buffer);
357 return GST_FLOW_ERROR;
362 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
364 return gst_element_register (plugin, "rtpamrpay",
365 GST_RANK_NONE, GST_TYPE_RTP_AMR_PAY);