tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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 static GstStateChangeReturn
94 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition);
95
96 GST_BOILERPLATE (GstRtpAMRPay, gst_rtp_amr_pay, GstBaseRTPPayload,
97     GST_TYPE_BASE_RTP_PAYLOAD);
98
99 static void
100 gst_rtp_amr_pay_base_init (gpointer klass)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
103
104   gst_element_class_add_static_pad_template (element_class,
105       &gst_rtp_amr_pay_src_template);
106   gst_element_class_add_static_pad_template (element_class,
107       &gst_rtp_amr_pay_sink_template);
108
109   gst_element_class_set_details_simple (element_class, "RTP AMR payloader",
110       "Codec/Payloader/Network/RTP",
111       "Payload-encode AMR or AMR-WB audio into RTP packets (RFC 3267)",
112       "Wim Taymans <wim.taymans@gmail.com>");
113 }
114
115 static void
116 gst_rtp_amr_pay_class_init (GstRtpAMRPayClass * klass)
117 {
118   GstBaseRTPPayloadClass *gstbasertppayload_class;
119   GstElementClass *gstelement_class;
120
121   gstelement_class = (GstElementClass *) klass;
122   gstelement_class->change_state = gst_rtp_amr_pay_change_state;
123
124   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
125
126   gstbasertppayload_class->set_caps = gst_rtp_amr_pay_setcaps;
127   gstbasertppayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
128
129   GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
130       "AMR/AMR-WB RTP Payloader");
131 }
132
133 static void
134 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay, GstRtpAMRPayClass * klass)
135 {
136   /* needed because of GST_BOILERPLATE */
137 }
138
139 static void
140 gst_rtp_amr_pay_reset (GstRtpAMRPay * pay)
141 {
142   pay->next_rtp_time = 0;
143   pay->first_ts = GST_CLOCK_TIME_NONE;
144   pay->first_rtp_time = 0;
145 }
146
147 static gboolean
148 gst_rtp_amr_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
149 {
150   GstRtpAMRPay *rtpamrpay;
151   gboolean res;
152   const GstStructure *s;
153   const gchar *str;
154
155   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
156
157   /* figure out the mode Narrow or Wideband */
158   s = gst_caps_get_structure (caps, 0);
159   if ((str = gst_structure_get_name (s))) {
160     if (strcmp (str, "audio/AMR") == 0)
161       rtpamrpay->mode = GST_RTP_AMR_P_MODE_NB;
162     else if (strcmp (str, "audio/AMR-WB") == 0)
163       rtpamrpay->mode = GST_RTP_AMR_P_MODE_WB;
164     else
165       goto wrong_type;
166   } else
167     goto wrong_type;
168
169   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
170     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
171   else
172     gst_basertppayload_set_options (basepayload, "audio", TRUE, "AMR-WB",
173         16000);
174
175   res = gst_basertppayload_set_outcaps (basepayload,
176       "encoding-params", G_TYPE_STRING, "1", "octet-align", G_TYPE_STRING, "1",
177       /* don't set the defaults
178        *
179        * "crc", G_TYPE_STRING, "0",
180        * "robust-sorting", G_TYPE_STRING, "0",
181        * "interleaving", G_TYPE_STRING, "0",
182        */
183       NULL);
184
185   return res;
186
187   /* ERRORS */
188 wrong_type:
189   {
190     GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
191         GST_STR_NULL (str));
192     return FALSE;
193   }
194 }
195
196 static void
197 gst_rtp_amr_pay_recalc_rtp_time (GstRtpAMRPay * rtpamrpay,
198     GstClockTime timestamp)
199 {
200   /* re-sync rtp time */
201   if (GST_CLOCK_TIME_IS_VALID (rtpamrpay->first_ts) &&
202       GST_CLOCK_TIME_IS_VALID (timestamp) && timestamp >= rtpamrpay->first_ts) {
203     GstClockTime diff;
204     guint32 rtpdiff;
205
206     /* interpolate to reproduce gap from start, rather than intermediate
207      * intervals to avoid roundup accumulation errors */
208     diff = timestamp - rtpamrpay->first_ts;
209     rtpdiff = ((diff / GST_MSECOND) * 8) <<
210         (rtpamrpay->mode == GST_RTP_AMR_P_MODE_WB);
211     rtpamrpay->next_rtp_time = rtpamrpay->first_rtp_time + rtpdiff;
212     GST_DEBUG_OBJECT (rtpamrpay,
213         "elapsed time %" GST_TIME_FORMAT ", rtp %" G_GUINT32_FORMAT ", "
214         "new offset %" G_GUINT32_FORMAT, GST_TIME_ARGS (diff), rtpdiff,
215         rtpamrpay->next_rtp_time);
216   }
217 }
218
219 /* -1 is invalid */
220 static const gint nb_frame_size[16] = {
221   12, 13, 15, 17, 19, 20, 26, 31,
222   5, -1, -1, -1, -1, -1, -1, 0
223 };
224
225 static const gint wb_frame_size[16] = {
226   17, 23, 32, 36, 40, 46, 50, 58,
227   60, 5, -1, -1, -1, -1, -1, 0
228 };
229
230 static GstFlowReturn
231 gst_rtp_amr_pay_handle_buffer (GstBaseRTPPayload * basepayload,
232     GstBuffer * buffer)
233 {
234   GstRtpAMRPay *rtpamrpay;
235   const gint *frame_size;
236   GstFlowReturn ret;
237   guint size, payload_len;
238   GstBuffer *outbuf;
239   guint8 *payload, *data, *payload_amr;
240   GstClockTime timestamp, duration;
241   guint packet_len, mtu;
242   gint i, num_packets, num_nonempty_packets;
243   gint amr_len;
244   gboolean sid = FALSE;
245
246   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
247   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpamrpay);
248
249   size = GST_BUFFER_SIZE (buffer);
250   data = GST_BUFFER_DATA (buffer);
251   timestamp = GST_BUFFER_TIMESTAMP (buffer);
252   duration = GST_BUFFER_DURATION (buffer);
253
254   /* setup frame size pointer */
255   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
256     frame_size = nb_frame_size;
257   else
258     frame_size = wb_frame_size;
259
260   GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
261
262   /* FIXME, only
263    * octet aligned, no interleaving, single channel, no CRC,
264    * no robust-sorting. To fix this you need to implement the downstream
265    * negotiation function. */
266
267   /* first count number of packets and total amr frame size */
268   amr_len = num_packets = num_nonempty_packets = 0;
269   for (i = 0; i < size; i++) {
270     guint8 FT;
271     gint fr_size;
272
273     FT = (data[i] & 0x78) >> 3;
274
275     fr_size = frame_size[FT];
276     GST_DEBUG_OBJECT (basepayload, "frame type %d, frame size %d", FT, fr_size);
277     /* FIXME, we don't handle this yet.. */
278     if (fr_size <= 0)
279       goto wrong_size;
280
281     if (fr_size == 5)
282       sid = TRUE;
283
284     amr_len += fr_size;
285     num_nonempty_packets++;
286     num_packets++;
287     i += fr_size;
288   }
289   if (amr_len > size)
290     goto incomplete_frame;
291
292   /* we need one extra byte for the CMR, the ToC is in the input
293    * data */
294   payload_len = size + 1;
295
296   /* get packet len to check against MTU */
297   packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
298   if (packet_len > mtu)
299     goto too_big;
300
301   /* now alloc output buffer */
302   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
303
304   /* copy timestamp */
305   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
306
307   if (duration != GST_CLOCK_TIME_NONE)
308     GST_BUFFER_DURATION (outbuf) = duration;
309   else {
310     GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
311   }
312
313   if (GST_BUFFER_IS_DISCONT (buffer)) {
314     GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
315     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
316     gst_rtp_buffer_set_marker (outbuf, TRUE);
317     gst_rtp_amr_pay_recalc_rtp_time (rtpamrpay, timestamp);
318   }
319
320   if (G_UNLIKELY (sid)) {
321     gst_rtp_amr_pay_recalc_rtp_time (rtpamrpay, timestamp);
322   }
323
324   /* perfect rtptime */
325   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rtpamrpay->first_ts))) {
326     rtpamrpay->first_ts = timestamp;
327     rtpamrpay->first_rtp_time = rtpamrpay->next_rtp_time;
328   }
329   GST_BUFFER_OFFSET (outbuf) = rtpamrpay->next_rtp_time;
330   rtpamrpay->next_rtp_time +=
331       (num_packets * 160) << (rtpamrpay->mode == GST_RTP_AMR_P_MODE_WB);
332
333   /* get payload, this is now writable */
334   payload = gst_rtp_buffer_get_payload (outbuf);
335
336   /*   0 1 2 3 4 5 6 7
337    *  +-+-+-+-+-+-+-+-+
338    *  |  CMR  |R|R|R|R|
339    *  +-+-+-+-+-+-+-+-+
340    */
341   payload[0] = 0xF0;            /* CMR, no specific mode requested */
342
343   /* this is where we copy the AMR data, after num_packets FTs and the
344    * CMR. */
345   payload_amr = payload + num_packets + 1;
346
347   /* copy data in payload, first we copy all the FTs then all
348    * the AMR data. The last FT has to have the F flag cleared. */
349   for (i = 1; i <= num_packets; i++) {
350     guint8 FT;
351     gint fr_size;
352
353     /*   0 1 2 3 4 5 6 7
354      *  +-+-+-+-+-+-+-+-+
355      *  |F|  FT   |Q|P|P| more FT...
356      *  +-+-+-+-+-+-+-+-+
357      */
358     FT = (*data & 0x78) >> 3;
359
360     fr_size = frame_size[FT];
361
362     if (i == num_packets)
363       /* last packet, clear F flag */
364       payload[i] = *data & 0x7f;
365     else
366       /* set F flag */
367       payload[i] = *data | 0x80;
368
369     memcpy (payload_amr, &data[1], fr_size);
370
371     /* all sizes are > 0 since we checked for that above */
372     data += fr_size + 1;
373     payload_amr += fr_size;
374   }
375
376   gst_buffer_unref (buffer);
377
378   ret = gst_basertppayload_push (basepayload, outbuf);
379
380   return ret;
381
382   /* ERRORS */
383 wrong_size:
384   {
385     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
386         (NULL), ("received AMR frame with size <= 0"));
387     gst_buffer_unref (buffer);
388
389     return GST_FLOW_ERROR;
390   }
391 incomplete_frame:
392   {
393     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
394         (NULL), ("received incomplete AMR frames"));
395     gst_buffer_unref (buffer);
396
397     return GST_FLOW_ERROR;
398   }
399 too_big:
400   {
401     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
402         (NULL), ("received too many AMR frames for MTU"));
403     gst_buffer_unref (buffer);
404
405     return GST_FLOW_ERROR;
406   }
407 }
408
409 static GstStateChangeReturn
410 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition)
411 {
412   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
413
414   /* handle upwards state changes here */
415   switch (transition) {
416     default:
417       break;
418   }
419
420   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
421
422   /* handle downwards state changes */
423   switch (transition) {
424     case GST_STATE_CHANGE_PAUSED_TO_READY:
425       gst_rtp_amr_pay_reset (GST_RTP_AMR_PAY (element));
426       break;
427     default:
428       break;
429   }
430
431   return ret;
432 }
433
434 gboolean
435 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
436 {
437   return gst_element_register (plugin, "rtpamrpay",
438       GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_PAY);
439 }