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 gstbasertppayload_class->set_caps = gst_rtp_amr_pay_setcaps;
124 gstbasertppayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
126 GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
127 "AMR/AMR-WB RTP Payloader");
131 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay, GstRtpAMRPayClass * klass)
133 /* needed because of GST_BOILERPLATE */
137 gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
139 GstRtpAMRPay *rtpamrpay;
140 const GstStructure *s;
143 rtpamrpay = GST_RTP_AMR_PAY (basepayload);
145 /* figure out the mode Narrow or Wideband */
146 s = gst_caps_get_structure (caps, 0);
147 if ((str = gst_structure_get_name (s))) {
148 if (strcmp (str, "audio/AMR") == 0)
149 rtpamrpay->mode = GST_RTP_AMR_P_MODE_NB;
150 else if (strcmp (str, "audio/AMR-WB") == 0)
151 rtpamrpay->mode = GST_RTP_AMR_P_MODE_WB;
157 if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
158 gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
160 gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR-WB",
163 gst_basertppayload_set_outcaps (basepayload,
164 "encoding-params", G_TYPE_STRING, "1", "octet-align", G_TYPE_STRING, "1",
165 /* don't set the defaults
167 * "crc", G_TYPE_STRING, "0",
168 * "robust-sorting", G_TYPE_STRING, "0",
169 * "interleaving", G_TYPE_STRING, "0",
178 GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
185 static gint nb_frame_size[16] = {
186 12, 13, 15, 17, 19, 20, 26, 31,
187 5, -1, -1, -1, -1, -1, -1, 0
190 static gint wb_frame_size[16] = {
191 17, 23, 32, 36, 40, 46, 50, 58,
192 60, -1, -1, -1, -1, -1, -1, 0
196 gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * basepayload,
199 GstRtpAMRPay *rtpamrpay;
201 guint size, payload_len;
203 guint8 *payload, *data, *payload_amr;
204 GstClockTime timestamp, duration;
205 guint packet_len, mtu;
206 gint i, num_packets, num_nonempty_packets;
210 rtpamrpay = GST_RTP_AMR_PAY (basepayload);
211 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpamrpay);
213 size = GST_BUFFER_SIZE (buffer);
214 data = GST_BUFFER_DATA (buffer);
215 timestamp = GST_BUFFER_TIMESTAMP (buffer);
216 duration = GST_BUFFER_DURATION (buffer);
218 /* setup frame size pointer */
219 if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
220 frame_size = nb_frame_size;
222 frame_size = wb_frame_size;
224 GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
227 * octet aligned, no interleaving, single channel, no CRC,
228 * no robust-sorting. To fix this you need to implement the downstream
229 * negotiation function. */
231 /* first count number of packets and total amr frame size */
232 amr_len = num_packets = num_nonempty_packets = 0;
233 for (i = 0; i < size; i++) {
237 FT = (data[i] & 0x78) >> 3;
239 fr_size = frame_size[FT];
240 GST_DEBUG_OBJECT (basepayload, "frame size %d", fr_size);
241 /* FIXME, we don't handle this yet.. */
246 num_nonempty_packets++;
251 goto incomplete_frame;
253 /* we need one extra byte for the CMR, the ToC is in the input
255 payload_len = size + 1;
257 /* get packet len to check against MTU */
258 packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
259 if (packet_len > mtu)
262 /* now alloc output buffer */
263 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
266 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
268 /* FIXME: when we do more than one AMR frame per packet, fix this */
269 if (duration != GST_CLOCK_TIME_NONE)
270 GST_BUFFER_DURATION (outbuf) = duration;
272 GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
275 if (GST_BUFFER_IS_DISCONT (buffer)) {
276 GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
277 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
278 gst_rtp_buffer_set_marker (outbuf, TRUE);
281 /* get payload, this is now writable */
282 payload = gst_rtp_buffer_get_payload (outbuf);
289 payload[0] = 0xF0; /* CMR, no specific mode requested */
291 /* this is where we copy the AMR data, after num_packets FTs and the
293 payload_amr = payload + num_packets + 1;
295 /* copy data in payload, first we copy all the FTs then all
296 * the AMR data. The last FT has to have the F flag cleared. */
297 for (i = 1; i <= num_packets; i++) {
303 * |F| FT |Q|P|P| more FT...
306 FT = (*data & 0x78) >> 3;
308 fr_size = frame_size[FT];
310 if (i == num_packets)
311 /* last packet, clear F flag */
312 payload[i] = *data & 0x7f;
315 payload[i] = *data | 0x80;
317 memcpy (payload_amr, &data[1], fr_size);
319 /* all sizes are > 0 since we checked for that above */
321 payload_amr += fr_size;
324 gst_buffer_unref (buffer);
326 ret = gst_basertppayload_push (basepayload, outbuf);
333 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
334 (NULL), ("received AMR frame with size <= 0"));
335 gst_buffer_unref (buffer);
337 return GST_FLOW_ERROR;
341 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
342 (NULL), ("received incomplete AMR frames"));
343 gst_buffer_unref (buffer);
345 return GST_FLOW_ERROR;
349 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
350 (NULL), ("received too many AMR frames for MTU"));
351 gst_buffer_unref (buffer);
353 return GST_FLOW_ERROR;
358 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
360 return gst_element_register (plugin, "rtpamrpay",
361 GST_RANK_NONE, GST_TYPE_RTP_AMR_PAY);