rtpamr(de)pay: support AMR-WB SID frame
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpamrpay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpamrpay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpamrpay_debug);
31 #define GST_CAT_DEFAULT (rtpamrpay_debug)
32
33 /* references:
34  *
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.
38  *
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;
42  *                                 Frame structure
43  *                    (3GPP TS 26.201 version 6.0.0 Release 6)
44  */
45
46 static GstStaticPadTemplate gst_rtp_amr_pay_sink_template =
47     GST_STATIC_PAD_TEMPLATE ("sink",
48     GST_PAD_SINK,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("audio/AMR, channels=(int)1, rate=(int)8000; "
51         "audio/AMR-WB, channels=(int)1, rate=(int)16000")
52     );
53
54 static GstStaticPadTemplate gst_rtp_amr_pay_src_template =
55     GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("application/x-rtp, "
59         "media = (string) \"audio\", "
60         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
61         "clock-rate = (int) 8000, "
62         "encoding-name = (string) \"AMR\", "
63         "encoding-params = (string) \"1\", "
64         "octet-align = (string) \"1\", "
65         "crc = (string) \"0\", "
66         "robust-sorting = (string) \"0\", "
67         "interleaving = (string) \"0\", "
68         "mode-set = (int) [ 0, 7 ], "
69         "mode-change-period = (int) [ 1, MAX ], "
70         "mode-change-neighbor = (string) { \"0\", \"1\" }, "
71         "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ];"
72         "application/x-rtp, "
73         "media = (string) \"audio\", "
74         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
75         "clock-rate = (int) 16000, "
76         "encoding-name = (string) \"AMR-WB\", "
77         "encoding-params = (string) \"1\", "
78         "octet-align = (string) \"1\", "
79         "crc = (string) \"0\", "
80         "robust-sorting = (string) \"0\", "
81         "interleaving = (string) \"0\", "
82         "mode-set = (int) [ 0, 7 ], "
83         "mode-change-period = (int) [ 1, MAX ], "
84         "mode-change-neighbor = (string) { \"0\", \"1\" }, "
85         "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ]")
86     );
87
88 static gboolean gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload,
89     GstCaps * caps);
90 static GstFlowReturn gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * pad,
91     GstBuffer * buffer);
92
93 GST_BOILERPLATE (GstRtpAMRPay, gst_rtp_amr_pay, GstBaseRTPPayload,
94     GST_TYPE_BASE_RTP_PAYLOAD);
95
96 static void
97 gst_rtp_amr_pay_base_init (gpointer klass)
98 {
99   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
100
101   gst_element_class_add_pad_template (element_class,
102       gst_static_pad_template_get (&gst_rtp_amr_pay_src_template));
103   gst_element_class_add_pad_template (element_class,
104       gst_static_pad_template_get (&gst_rtp_amr_pay_sink_template));
105
106   gst_element_class_set_details_simple (element_class, "RTP AMR payloader",
107       "Codec/Payloader/Network",
108       "Payload-encode AMR or AMR-WB audio into RTP packets (RFC 3267)",
109       "Wim Taymans <wim.taymans@gmail.com>");
110 }
111
112 static void
113 gst_rtp_amr_pay_class_init (GstRtpAMRPayClass * klass)
114 {
115   GstBaseRTPPayloadClass *gstbasertppayload_class;
116
117   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
118
119   gstbasertppayload_class->set_caps = gst_rtp_amr_pay_setcaps;
120   gstbasertppayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
121
122   GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
123       "AMR/AMR-WB RTP Payloader");
124 }
125
126 static void
127 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay, GstRtpAMRPayClass * klass)
128 {
129   /* needed because of GST_BOILERPLATE */
130 }
131
132 static gboolean
133 gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
134 {
135   GstRtpAMRPay *rtpamrpay;
136   gboolean res;
137   const GstStructure *s;
138   const gchar *str;
139
140   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
141
142   /* figure out the mode Narrow or Wideband */
143   s = gst_caps_get_structure (caps, 0);
144   if ((str = gst_structure_get_name (s))) {
145     if (strcmp (str, "audio/AMR") == 0)
146       rtpamrpay->mode = GST_RTP_AMR_P_MODE_NB;
147     else if (strcmp (str, "audio/AMR-WB") == 0)
148       rtpamrpay->mode = GST_RTP_AMR_P_MODE_WB;
149     else
150       goto wrong_type;
151   } else
152     goto wrong_type;
153
154   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
155     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
156   else
157     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR-WB",
158         16000);
159
160   res = gst_basertppayload_set_outcaps (basepayload,
161       "encoding-params", G_TYPE_STRING, "1", "octet-align", G_TYPE_STRING, "1",
162       /* don't set the defaults
163        *
164        * "crc", G_TYPE_STRING, "0",
165        * "robust-sorting", G_TYPE_STRING, "0",
166        * "interleaving", G_TYPE_STRING, "0",
167        */
168       NULL);
169
170   return res;
171
172   /* ERRORS */
173 wrong_type:
174   {
175     GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
176         GST_STR_NULL (str));
177     return FALSE;
178   }
179 }
180
181 /* -1 is invalid */
182 static gint nb_frame_size[16] = {
183   12, 13, 15, 17, 19, 20, 26, 31,
184   5, -1, -1, -1, -1, -1, -1, 0
185 };
186
187 static gint wb_frame_size[16] = {
188   17, 23, 32, 36, 40, 46, 50, 58,
189   60, 5, -1, -1, -1, -1, -1, 0
190 };
191
192 static GstFlowReturn
193 gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * basepayload,
194     GstBuffer * buffer)
195 {
196   GstRtpAMRPay *rtpamrpay;
197   GstFlowReturn ret;
198   guint size, payload_len;
199   GstBuffer *outbuf;
200   guint8 *payload, *data, *payload_amr;
201   GstClockTime timestamp, duration;
202   guint packet_len, mtu;
203   gint i, num_packets, num_nonempty_packets;
204   gint amr_len;
205   gint *frame_size;
206
207   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
208   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpamrpay);
209
210   size = GST_BUFFER_SIZE (buffer);
211   data = GST_BUFFER_DATA (buffer);
212   timestamp = GST_BUFFER_TIMESTAMP (buffer);
213   duration = GST_BUFFER_DURATION (buffer);
214
215   /* setup frame size pointer */
216   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
217     frame_size = nb_frame_size;
218   else
219     frame_size = wb_frame_size;
220
221   GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
222
223   /* FIXME, only
224    * octet aligned, no interleaving, single channel, no CRC,
225    * no robust-sorting. To fix this you need to implement the downstream
226    * negotiation function. */
227
228   /* first count number of packets and total amr frame size */
229   amr_len = num_packets = num_nonempty_packets = 0;
230   for (i = 0; i < size; i++) {
231     guint8 FT;
232     gint fr_size;
233
234     FT = (data[i] & 0x78) >> 3;
235
236     fr_size = frame_size[FT];
237     GST_DEBUG_OBJECT (basepayload, "frame size %d", fr_size);
238     /* FIXME, we don't handle this yet.. */
239     if (fr_size <= 0)
240       goto wrong_size;
241
242     amr_len += fr_size;
243     num_nonempty_packets++;
244     num_packets++;
245     i += fr_size;
246   }
247   if (amr_len > size)
248     goto incomplete_frame;
249
250   /* we need one extra byte for the CMR, the ToC is in the input
251    * data */
252   payload_len = size + 1;
253
254   /* get packet len to check against MTU */
255   packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
256   if (packet_len > mtu)
257     goto too_big;
258
259   /* now alloc output buffer */
260   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
261
262   /* copy timestamp */
263   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
264
265   /* FIXME: when we do more than one AMR frame per packet, fix this */
266   if (duration != GST_CLOCK_TIME_NONE)
267     GST_BUFFER_DURATION (outbuf) = duration;
268   else {
269     GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
270   }
271
272   if (GST_BUFFER_IS_DISCONT (buffer)) {
273     GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
274     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
275     gst_rtp_buffer_set_marker (outbuf, TRUE);
276   }
277
278   /* get payload, this is now writable */
279   payload = gst_rtp_buffer_get_payload (outbuf);
280
281   /*   0 1 2 3 4 5 6 7
282    *  +-+-+-+-+-+-+-+-+
283    *  |  CMR  |R|R|R|R|
284    *  +-+-+-+-+-+-+-+-+
285    */
286   payload[0] = 0xF0;            /* CMR, no specific mode requested */
287
288   /* this is where we copy the AMR data, after num_packets FTs and the
289    * CMR. */
290   payload_amr = payload + num_packets + 1;
291
292   /* copy data in payload, first we copy all the FTs then all
293    * the AMR data. The last FT has to have the F flag cleared. */
294   for (i = 1; i <= num_packets; i++) {
295     guint8 FT;
296     gint fr_size;
297
298     /*   0 1 2 3 4 5 6 7
299      *  +-+-+-+-+-+-+-+-+
300      *  |F|  FT   |Q|P|P| more FT...
301      *  +-+-+-+-+-+-+-+-+
302      */
303     FT = (*data & 0x78) >> 3;
304
305     fr_size = frame_size[FT];
306
307     if (i == num_packets)
308       /* last packet, clear F flag */
309       payload[i] = *data & 0x7f;
310     else
311       /* set F flag */
312       payload[i] = *data | 0x80;
313
314     memcpy (payload_amr, &data[1], fr_size);
315
316     /* all sizes are > 0 since we checked for that above */
317     data += fr_size + 1;
318     payload_amr += fr_size;
319   }
320
321   gst_buffer_unref (buffer);
322
323   ret = gst_basertppayload_push (basepayload, outbuf);
324
325   return ret;
326
327   /* ERRORS */
328 wrong_size:
329   {
330     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
331         (NULL), ("received AMR frame with size <= 0"));
332     gst_buffer_unref (buffer);
333
334     return GST_FLOW_ERROR;
335   }
336 incomplete_frame:
337   {
338     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
339         (NULL), ("received incomplete AMR frames"));
340     gst_buffer_unref (buffer);
341
342     return GST_FLOW_ERROR;
343   }
344 too_big:
345   {
346     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
347         (NULL), ("received too many AMR frames for MTU"));
348     gst_buffer_unref (buffer);
349
350     return GST_FLOW_ERROR;
351   }
352 }
353
354 gboolean
355 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
356 {
357   return gst_element_register (plugin, "rtpamrpay",
358       GST_RANK_NONE, GST_TYPE_RTP_AMR_PAY);
359 }