rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-rtpamrpay
22  * @title: rtpamrpay
23  * @see_also: rtpamrdepay
24  *
25  * Payload AMR audio into RTP packets according to RFC 3267.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3267.txt
27  *
28  * ## Example pipeline
29  * |[
30  * gst-launch-1.0 -v audiotestsrc ! amrnbenc ! rtpamrpay ! udpsink
31  * ]| This example pipeline will encode and payload an AMR stream. Refer to
32  * the rtpamrdepay example to depayload and decode the RTP stream.
33  *
34  */
35
36 /* references:
37  *
38  * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
39  *    Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive
40  *    Multi-Rate Wideband (AMR-WB) Audio Codecs.
41  *
42  * ETSI TS 126 201 V6.0.0 (2004-12) - Digital cellular telecommunications system (Phase 2+);
43  *                 Universal Mobile Telecommunications System (UMTS);
44  *                          AMR speech codec, wideband;
45  *                                 Frame structure
46  *                    (3GPP TS 26.201 version 6.0.0 Release 6)
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #  include "config.h"
51 #endif
52
53 #include <string.h>
54
55 #include <gst/rtp/gstrtpbuffer.h>
56 #include <gst/audio/audio.h>
57
58 #include "gstrtpamrpay.h"
59 #include "gstrtputils.h"
60
61 GST_DEBUG_CATEGORY_STATIC (rtpamrpay_debug);
62 #define GST_CAT_DEFAULT (rtpamrpay_debug)
63
64 static GstStaticPadTemplate gst_rtp_amr_pay_sink_template =
65     GST_STATIC_PAD_TEMPLATE ("sink",
66     GST_PAD_SINK,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS ("audio/AMR, channels=(int)1, rate=(int)8000; "
69         "audio/AMR-WB, channels=(int)1, rate=(int)16000")
70     );
71
72 static GstStaticPadTemplate gst_rtp_amr_pay_src_template =
73     GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS ("application/x-rtp, "
77         "media = (string) \"audio\", "
78         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
79         "clock-rate = (int) 8000, "
80         "encoding-name = (string) \"AMR\", "
81         "encoding-params = (string) \"1\", "
82         "octet-align = (string) \"1\", "
83         "crc = (string) \"0\", "
84         "robust-sorting = (string) \"0\", "
85         "interleaving = (string) \"0\", "
86         "mode-set = (int) [ 0, 7 ], "
87         "mode-change-period = (int) [ 1, MAX ], "
88         "mode-change-neighbor = (string) { \"0\", \"1\" }, "
89         "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ];"
90         "application/x-rtp, "
91         "media = (string) \"audio\", "
92         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
93         "clock-rate = (int) 16000, "
94         "encoding-name = (string) \"AMR-WB\", "
95         "encoding-params = (string) \"1\", "
96         "octet-align = (string) \"1\", "
97         "crc = (string) \"0\", "
98         "robust-sorting = (string) \"0\", "
99         "interleaving = (string) \"0\", "
100         "mode-set = (int) [ 0, 7 ], "
101         "mode-change-period = (int) [ 1, MAX ], "
102         "mode-change-neighbor = (string) { \"0\", \"1\" }, "
103         "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ]")
104     );
105
106 static gboolean gst_rtp_amr_pay_setcaps (GstRTPBasePayload * basepayload,
107     GstCaps * caps);
108 static GstFlowReturn gst_rtp_amr_pay_handle_buffer (GstRTPBasePayload * pad,
109     GstBuffer * buffer);
110
111 static GstStateChangeReturn
112 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition);
113
114 #define gst_rtp_amr_pay_parent_class parent_class
115 G_DEFINE_TYPE (GstRtpAMRPay, gst_rtp_amr_pay, GST_TYPE_RTP_BASE_PAYLOAD);
116
117 static void
118 gst_rtp_amr_pay_class_init (GstRtpAMRPayClass * klass)
119 {
120   GstElementClass *gstelement_class;
121   GstRTPBasePayloadClass *gstrtpbasepayload_class;
122
123   gstelement_class = (GstElementClass *) klass;
124   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
125
126   gstelement_class->change_state = gst_rtp_amr_pay_change_state;
127
128   gst_element_class_add_static_pad_template (gstelement_class,
129       &gst_rtp_amr_pay_src_template);
130   gst_element_class_add_static_pad_template (gstelement_class,
131       &gst_rtp_amr_pay_sink_template);
132
133   gst_element_class_set_static_metadata (gstelement_class, "RTP AMR payloader",
134       "Codec/Payloader/Network/RTP",
135       "Payload-encode AMR or AMR-WB audio into RTP packets (RFC 3267)",
136       "Wim Taymans <wim.taymans@gmail.com>");
137
138   gstrtpbasepayload_class->set_caps = gst_rtp_amr_pay_setcaps;
139   gstrtpbasepayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
140
141   GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
142       "AMR/AMR-WB RTP Payloader");
143 }
144
145 static void
146 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay)
147 {
148 }
149
150 static void
151 gst_rtp_amr_pay_reset (GstRtpAMRPay * pay)
152 {
153   pay->next_rtp_time = 0;
154   pay->first_ts = GST_CLOCK_TIME_NONE;
155   pay->first_rtp_time = 0;
156 }
157
158 static gboolean
159 gst_rtp_amr_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
160 {
161   GstRtpAMRPay *rtpamrpay;
162   gboolean res;
163   const GstStructure *s;
164   const gchar *str;
165
166   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
167
168   /* figure out the mode Narrow or Wideband */
169   s = gst_caps_get_structure (caps, 0);
170   if ((str = gst_structure_get_name (s))) {
171     if (strcmp (str, "audio/AMR") == 0)
172       rtpamrpay->mode = GST_RTP_AMR_P_MODE_NB;
173     else if (strcmp (str, "audio/AMR-WB") == 0)
174       rtpamrpay->mode = GST_RTP_AMR_P_MODE_WB;
175     else
176       goto wrong_type;
177   } else
178     goto wrong_type;
179
180   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
181     gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
182   else
183     gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "AMR-WB",
184         16000);
185
186   res = gst_rtp_base_payload_set_outcaps (basepayload,
187       "encoding-params", G_TYPE_STRING, "1", "octet-align", G_TYPE_STRING, "1",
188       /* don't set the defaults
189        *
190        * "crc", G_TYPE_STRING, "0",
191        * "robust-sorting", G_TYPE_STRING, "0",
192        * "interleaving", G_TYPE_STRING, "0",
193        */
194       NULL);
195
196   return res;
197
198   /* ERRORS */
199 wrong_type:
200   {
201     GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
202         GST_STR_NULL (str));
203     return FALSE;
204   }
205 }
206
207 static void
208 gst_rtp_amr_pay_recalc_rtp_time (GstRtpAMRPay * rtpamrpay,
209     GstClockTime timestamp)
210 {
211   /* re-sync rtp time */
212   if (GST_CLOCK_TIME_IS_VALID (rtpamrpay->first_ts) &&
213       GST_CLOCK_TIME_IS_VALID (timestamp) && timestamp >= rtpamrpay->first_ts) {
214     GstClockTime diff;
215     guint32 rtpdiff;
216
217     /* interpolate to reproduce gap from start, rather than intermediate
218      * intervals to avoid roundup accumulation errors */
219     diff = timestamp - rtpamrpay->first_ts;
220     rtpdiff = ((diff / GST_MSECOND) * 8) <<
221         (rtpamrpay->mode == GST_RTP_AMR_P_MODE_WB);
222     rtpamrpay->next_rtp_time = rtpamrpay->first_rtp_time + rtpdiff;
223     GST_DEBUG_OBJECT (rtpamrpay,
224         "elapsed time %" GST_TIME_FORMAT ", rtp %" G_GUINT32_FORMAT ", "
225         "new offset %" G_GUINT32_FORMAT, GST_TIME_ARGS (diff), rtpdiff,
226         rtpamrpay->next_rtp_time);
227   }
228 }
229
230 /* -1 is invalid */
231 static const gint nb_frame_size[16] = {
232   12, 13, 15, 17, 19, 20, 26, 31,
233   5, -1, -1, -1, -1, -1, -1, 0
234 };
235
236 static const gint wb_frame_size[16] = {
237   17, 23, 32, 36, 40, 46, 50, 58,
238   60, 5, -1, -1, -1, -1, -1, 0
239 };
240
241 static GstFlowReturn
242 gst_rtp_amr_pay_handle_buffer (GstRTPBasePayload * basepayload,
243     GstBuffer * buffer)
244 {
245   GstRtpAMRPay *rtpamrpay;
246   const gint *frame_size;
247   GstFlowReturn ret;
248   guint payload_len;
249   GstMapInfo map;
250   GstBuffer *outbuf;
251   guint8 *payload, *ptr, *payload_amr;
252   GstClockTime timestamp, duration;
253   guint packet_len, mtu;
254   gint i, num_packets, num_nonempty_packets;
255   gint amr_len;
256   gboolean sid = FALSE;
257   GstRTPBuffer rtp = { NULL };
258
259   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
260   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpamrpay);
261
262   gst_buffer_map (buffer, &map, GST_MAP_READ);
263
264   timestamp = GST_BUFFER_PTS (buffer);
265   duration = GST_BUFFER_DURATION (buffer);
266
267   /* setup frame size pointer */
268   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
269     frame_size = nb_frame_size;
270   else
271     frame_size = wb_frame_size;
272
273   GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", map.size);
274
275   /* FIXME, only
276    * octet aligned, no interleaving, single channel, no CRC,
277    * no robust-sorting. To fix this you need to implement the downstream
278    * negotiation function. */
279
280   /* first count number of packets and total amr frame size */
281   amr_len = num_packets = num_nonempty_packets = 0;
282   for (i = 0; i < map.size; i++) {
283     guint8 FT;
284     gint fr_size;
285
286     FT = (map.data[i] & 0x78) >> 3;
287
288     fr_size = frame_size[FT];
289     GST_DEBUG_OBJECT (basepayload, "frame type %d, frame size %d", FT, fr_size);
290     /* FIXME, we don't handle this yet.. */
291     if (fr_size <= 0)
292       goto wrong_size;
293
294     if (fr_size == 5)
295       sid = TRUE;
296
297     amr_len += fr_size;
298     num_nonempty_packets++;
299     num_packets++;
300     i += fr_size;
301   }
302   if (amr_len > map.size)
303     goto incomplete_frame;
304
305   /* we need one extra byte for the CMR, the ToC is in the input
306    * data */
307   payload_len = map.size + 1;
308
309   /* get packet len to check against MTU */
310   packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
311   if (packet_len > mtu)
312     goto too_big;
313
314   /* now alloc output buffer */
315   outbuf =
316       gst_rtp_base_payload_allocate_output_buffer (basepayload, payload_len, 0,
317       0);
318
319   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
320
321   /* copy timestamp */
322   GST_BUFFER_PTS (outbuf) = timestamp;
323
324   if (duration != GST_CLOCK_TIME_NONE)
325     GST_BUFFER_DURATION (outbuf) = duration;
326   else {
327     GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
328   }
329
330   if (GST_BUFFER_IS_DISCONT (buffer)) {
331     GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
332     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
333     gst_rtp_buffer_set_marker (&rtp, TRUE);
334     gst_rtp_amr_pay_recalc_rtp_time (rtpamrpay, timestamp);
335   }
336
337   if (G_UNLIKELY (sid)) {
338     gst_rtp_amr_pay_recalc_rtp_time (rtpamrpay, timestamp);
339   }
340
341   /* perfect rtptime */
342   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rtpamrpay->first_ts))) {
343     rtpamrpay->first_ts = timestamp;
344     rtpamrpay->first_rtp_time = rtpamrpay->next_rtp_time;
345   }
346   GST_BUFFER_OFFSET (outbuf) = rtpamrpay->next_rtp_time;
347   rtpamrpay->next_rtp_time +=
348       (num_packets * 160) << (rtpamrpay->mode == GST_RTP_AMR_P_MODE_WB);
349
350   /* get payload, this is now writable */
351   payload = gst_rtp_buffer_get_payload (&rtp);
352
353   /*   0 1 2 3 4 5 6 7
354    *  +-+-+-+-+-+-+-+-+
355    *  |  CMR  |R|R|R|R|
356    *  +-+-+-+-+-+-+-+-+
357    */
358   payload[0] = 0xF0;            /* CMR, no specific mode requested */
359
360   /* this is where we copy the AMR data, after num_packets FTs and the
361    * CMR. */
362   payload_amr = payload + num_packets + 1;
363
364   /* copy data in payload, first we copy all the FTs then all
365    * the AMR data. The last FT has to have the F flag cleared. */
366   ptr = map.data;
367   for (i = 1; i <= num_packets; i++) {
368     guint8 FT;
369     gint fr_size;
370
371     /*   0 1 2 3 4 5 6 7
372      *  +-+-+-+-+-+-+-+-+
373      *  |F|  FT   |Q|P|P| more FT...
374      *  +-+-+-+-+-+-+-+-+
375      */
376     FT = (*ptr & 0x78) >> 3;
377
378     fr_size = frame_size[FT];
379
380     if (i == num_packets)
381       /* last packet, clear F flag */
382       payload[i] = *ptr & 0x7f;
383     else
384       /* set F flag */
385       payload[i] = *ptr | 0x80;
386
387     memcpy (payload_amr, &ptr[1], fr_size);
388
389     /* all sizes are > 0 since we checked for that above */
390     ptr += fr_size + 1;
391     payload_amr += fr_size;
392   }
393
394   gst_buffer_unmap (buffer, &map);
395   gst_rtp_buffer_unmap (&rtp);
396
397   gst_rtp_copy_audio_meta (rtpamrpay, outbuf, buffer);
398
399   gst_buffer_unref (buffer);
400
401   ret = gst_rtp_base_payload_push (basepayload, outbuf);
402
403   return ret;
404
405   /* ERRORS */
406 wrong_size:
407   {
408     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
409         (NULL), ("received AMR frame with size <= 0"));
410     gst_buffer_unmap (buffer, &map);
411     gst_buffer_unref (buffer);
412
413     return GST_FLOW_ERROR;
414   }
415 incomplete_frame:
416   {
417     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
418         (NULL), ("received incomplete AMR frames"));
419     gst_buffer_unmap (buffer, &map);
420     gst_buffer_unref (buffer);
421
422     return GST_FLOW_ERROR;
423   }
424 too_big:
425   {
426     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
427         (NULL), ("received too many AMR frames for MTU"));
428     gst_buffer_unmap (buffer, &map);
429     gst_buffer_unref (buffer);
430
431     return GST_FLOW_ERROR;
432   }
433 }
434
435 static GstStateChangeReturn
436 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition)
437 {
438   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
439
440   /* handle upwards state changes here */
441   switch (transition) {
442     default:
443       break;
444   }
445
446   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
447
448   /* handle downwards state changes */
449   switch (transition) {
450     case GST_STATE_CHANGE_PAUSED_TO_READY:
451       gst_rtp_amr_pay_reset (GST_RTP_AMR_PAY (element));
452       break;
453     default:
454       break;
455   }
456
457   return ret;
458 }
459
460 gboolean
461 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
462 {
463   return gst_element_register (plugin, "rtpamrpay",
464       GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_PAY);
465 }