rtp: use boilerplate
[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 /* 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>");
52
53 static GstStaticPadTemplate gst_rtp_amr_pay_sink_template =
54     GST_STATIC_PAD_TEMPLATE ("sink",
55     GST_PAD_SINK,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("audio/AMR, channels=(int)1, rate=(int)8000; "
58         "audio/AMR-WB, channels=(int)1, rate=(int)16000")
59     );
60
61 static GstStaticPadTemplate gst_rtp_amr_pay_src_template =
62     GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
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 ];"
79         "application/x-rtp, "
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 ]")
93     );
94
95 static gboolean gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload,
96     GstCaps * caps);
97 static GstFlowReturn gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * pad,
98     GstBuffer * buffer);
99
100 GST_BOILERPLATE (GstRtpAMRPay, gst_rtp_amr_pay, GstBaseRTPPayload,
101     GST_TYPE_BASE_RTP_PAYLOAD);
102
103 static void
104 gst_rtp_amr_pay_base_init (gpointer klass)
105 {
106   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
107
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));
112
113   gst_element_class_set_details (element_class, &gst_rtp_amrpay_details);
114 }
115
116 static void
117 gst_rtp_amr_pay_class_init (GstRtpAMRPayClass * klass)
118 {
119   GstBaseRTPPayloadClass *gstbasertppayload_class;
120
121   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
122
123   gstbasertppayload_class->set_caps = gst_rtp_amr_pay_setcaps;
124   gstbasertppayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
125
126   GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
127       "AMR/AMR-WB RTP Payloader");
128 }
129
130 static void
131 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay, GstRtpAMRPayClass * klass)
132 {
133   /* needed because of GST_BOILERPLATE */
134 }
135
136 static gboolean
137 gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
138 {
139   GstRtpAMRPay *rtpamrpay;
140   const GstStructure *s;
141   const gchar *str;
142
143   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
144
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;
152     else
153       goto wrong_type;
154   } else
155     goto wrong_type;
156
157   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
158     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
159   else
160     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR-WB",
161         16000);
162
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
166        *
167        * "crc", G_TYPE_STRING, "0",
168        * "robust-sorting", G_TYPE_STRING, "0",
169        * "interleaving", G_TYPE_STRING, "0",
170        */
171       NULL);
172
173   return TRUE;
174
175   /* ERRORS */
176 wrong_type:
177   {
178     GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
179         GST_STR_NULL (str));
180     return FALSE;
181   }
182 }
183
184 /* -1 is invalid */
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
188 };
189
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
193 };
194
195 static GstFlowReturn
196 gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * basepayload,
197     GstBuffer * buffer)
198 {
199   GstRtpAMRPay *rtpamrpay;
200   GstFlowReturn ret;
201   guint size, payload_len;
202   GstBuffer *outbuf;
203   guint8 *payload, *data, *payload_amr;
204   GstClockTime timestamp, duration;
205   guint packet_len, mtu;
206   gint i, num_packets, num_nonempty_packets;
207   gint amr_len;
208   gint *frame_size;
209
210   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
211   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpamrpay);
212
213   size = GST_BUFFER_SIZE (buffer);
214   data = GST_BUFFER_DATA (buffer);
215   timestamp = GST_BUFFER_TIMESTAMP (buffer);
216   duration = GST_BUFFER_DURATION (buffer);
217
218   /* setup frame size pointer */
219   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
220     frame_size = nb_frame_size;
221   else
222     frame_size = wb_frame_size;
223
224   GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
225
226   /* FIXME, only
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. */
230
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++) {
234     guint8 FT;
235     gint fr_size;
236
237     FT = (data[i] & 0x78) >> 3;
238
239     fr_size = frame_size[FT];
240     GST_DEBUG_OBJECT (basepayload, "frame size %d", fr_size);
241     /* FIXME, we don't handle this yet.. */
242     if (fr_size <= 0)
243       goto wrong_size;
244
245     amr_len += fr_size;
246     num_nonempty_packets++;
247     num_packets++;
248     i += fr_size;
249   }
250   if (amr_len > size)
251     goto incomplete_frame;
252
253   /* we need one extra byte for the CMR, the ToC is in the input
254    * data */
255   payload_len = size + 1;
256
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)
260     goto too_big;
261
262   /* now alloc output buffer */
263   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
264
265   /* copy timestamp */
266   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
267
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;
271   else {
272     GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
273   }
274
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);
279   }
280
281   /* get payload, this is now writable */
282   payload = gst_rtp_buffer_get_payload (outbuf);
283
284   /*   0 1 2 3 4 5 6 7
285    *  +-+-+-+-+-+-+-+-+
286    *  |  CMR  |R|R|R|R|
287    *  +-+-+-+-+-+-+-+-+
288    */
289   payload[0] = 0xF0;            /* CMR, no specific mode requested */
290
291   /* this is where we copy the AMR data, after num_packets FTs and the
292    * CMR. */
293   payload_amr = payload + num_packets + 1;
294
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++) {
298     guint8 FT;
299     gint fr_size;
300
301     /*   0 1 2 3 4 5 6 7
302      *  +-+-+-+-+-+-+-+-+
303      *  |F|  FT   |Q|P|P| more FT...
304      *  +-+-+-+-+-+-+-+-+
305      */
306     FT = (*data & 0x78) >> 3;
307
308     fr_size = frame_size[FT];
309
310     if (i == num_packets)
311       /* last packet, clear F flag */
312       payload[i] = *data & 0x7f;
313     else
314       /* set F flag */
315       payload[i] = *data | 0x80;
316
317     memcpy (payload_amr, &data[1], fr_size);
318
319     /* all sizes are > 0 since we checked for that above */
320     data += fr_size + 1;
321     payload_amr += fr_size;
322   }
323
324   gst_buffer_unref (buffer);
325
326   ret = gst_basertppayload_push (basepayload, outbuf);
327
328   return ret;
329
330   /* ERRORS */
331 wrong_size:
332   {
333     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
334         (NULL), ("received AMR frame with size <= 0"));
335     gst_buffer_unref (buffer);
336
337     return GST_FLOW_ERROR;
338   }
339 incomplete_frame:
340   {
341     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
342         (NULL), ("received incomplete AMR frames"));
343     gst_buffer_unref (buffer);
344
345     return GST_FLOW_ERROR;
346   }
347 too_big:
348   {
349     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
350         (NULL), ("received too many AMR frames for MTU"));
351     gst_buffer_unref (buffer);
352
353     return GST_FLOW_ERROR;
354   }
355 }
356
357 gboolean
358 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
359 {
360   return gst_element_register (plugin, "rtpamrpay",
361       GST_RANK_NONE, GST_TYPE_RTP_AMR_PAY);
362 }