Remove unused variables in _class_init
[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   parent_class = g_type_class_peek_parent (klass);
124
125   gstbasertppayload_class->set_caps = gst_rtp_amr_pay_setcaps;
126   gstbasertppayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
127
128   GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
129       "AMR/AMR-WB RTP Payloader");
130 }
131
132 static void
133 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay, GstRtpAMRPayClass * klass)
134 {
135   /* needed because of GST_BOILERPLATE */
136 }
137
138 static gboolean
139 gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
140 {
141   GstRtpAMRPay *rtpamrpay;
142   const GstStructure *s;
143   const gchar *str;
144
145   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
146
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;
154     else
155       goto wrong_type;
156   } else
157     goto wrong_type;
158
159   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
160     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
161   else
162     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR-WB",
163         16000);
164
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 
168        * 
169        * "crc", G_TYPE_STRING, "0",
170        * "robust-sorting", G_TYPE_STRING, "0",
171        * "interleaving", G_TYPE_STRING, "0", 
172        */
173       NULL);
174
175   return TRUE;
176
177   /* ERRORS */
178 wrong_type:
179   {
180     GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
181         GST_STR_NULL (str));
182     return FALSE;
183   }
184 }
185
186 /* -1 is invalid */
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
190 };
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
194 };
195
196 static GstFlowReturn
197 gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * basepayload,
198     GstBuffer * buffer)
199 {
200   GstRtpAMRPay *rtpamrpay;
201   GstFlowReturn ret;
202   guint size, payload_len;
203   GstBuffer *outbuf;
204   guint8 *payload, *data, *payload_amr;
205   GstClockTime timestamp, duration;
206   guint packet_len, mtu;
207   gint i, num_packets, num_nonempty_packets;
208   gint amr_len;
209   gint *frame_size;
210   gboolean discont;
211
212   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
213   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpamrpay);
214
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);
220
221   /* setup frame size pointer */
222   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
223     frame_size = nb_frame_size;
224   else
225     frame_size = wb_frame_size;
226
227   GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
228
229   /* FIXME, only 
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. */
233
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++) {
237     guint8 FT;
238     gint fr_size;
239
240     FT = (data[i] & 0x78) >> 3;
241
242     fr_size = frame_size[FT];
243     GST_DEBUG_OBJECT (basepayload, "frame size %d", fr_size);
244     /* FIXME, we don't handle this yet.. */
245     if (fr_size <= 0)
246       goto wrong_size;
247
248     amr_len += fr_size;
249     num_nonempty_packets++;
250     num_packets++;
251     i += fr_size;
252   }
253   if (amr_len > size)
254     goto incomplete_frame;
255
256   /* we need one extra byte for the CMR, the ToC is in the input
257    * data */
258   payload_len = size + 1;
259
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)
263     goto too_big;
264
265   /* now alloc output buffer */
266   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
267
268   /* copy timestamp */
269   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
270
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;
274   else {
275     GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
276   }
277
278   if (discont) {
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);
282     discont = FALSE;
283   }
284
285   /* get payload, this is now writable */
286   payload = gst_rtp_buffer_get_payload (outbuf);
287
288   /*   0 1 2 3 4 5 6 7 
289    *  +-+-+-+-+-+-+-+-+
290    *  |  CMR  |R|R|R|R|
291    *  +-+-+-+-+-+-+-+-+
292    */
293   payload[0] = 0xF0;            /* CMR, no specific mode requested */
294
295   /* this is where we copy the AMR data, after num_packets FTs and the
296    * CMR. */
297   payload_amr = payload + num_packets + 1;
298
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++) {
302     guint8 FT;
303     gint fr_size;
304
305     /*   0 1 2 3 4 5 6 7
306      *  +-+-+-+-+-+-+-+-+
307      *  |F|  FT   |Q|P|P| more FT...
308      *  +-+-+-+-+-+-+-+-+
309      */
310     FT = (*data & 0x78) >> 3;
311
312     fr_size = frame_size[FT];
313
314     if (i == num_packets)
315       /* last packet, clear F flag */
316       payload[i] = *data & 0x7f;
317     else
318       /* set F flag */
319       payload[i] = *data | 0x80;
320
321     memcpy (payload_amr, &data[1], fr_size);
322
323     /* all sizes are > 0 since we checked for that above */
324     data += fr_size + 1;
325     payload_amr += fr_size;
326   }
327
328   gst_buffer_unref (buffer);
329
330   ret = gst_basertppayload_push (basepayload, outbuf);
331
332   return ret;
333
334   /* ERRORS */
335 wrong_size:
336   {
337     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
338         (NULL), ("received AMR frame with size <= 0"));
339     gst_buffer_unref (buffer);
340
341     return GST_FLOW_ERROR;
342   }
343 incomplete_frame:
344   {
345     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
346         (NULL), ("received incomplete AMR frames"));
347     gst_buffer_unref (buffer);
348
349     return GST_FLOW_ERROR;
350   }
351 too_big:
352   {
353     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
354         (NULL), ("received too many AMR frames for MTU"));
355     gst_buffer_unref (buffer);
356
357     return GST_FLOW_ERROR;
358   }
359 }
360
361 gboolean
362 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
363 {
364   return gst_element_register (plugin, "rtpamrpay",
365       GST_RANK_NONE, GST_TYPE_RTP_AMR_PAY);
366 }