rtpac3pay: Attach the payload to the output buffer instead of copying it
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpac3pay.c
1 /* GStreamer
2  * Copyright (C) <2010> 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-rtpac3pay
22  * @see_also: rtpac3depay
23  *
24  * Payload AC3 audio into RTP packets according to RFC 4184.
25  * For detailed information see: http://www.rfc-editor.org/rfc/rfc4184.txt
26  *
27  * <refsect2>
28  * <title>Example pipeline</title>
29  * |[
30  * gst-launch-1.0 -v audiotestsrc ! avenc_ac3 ! rtpac3pay ! udpsink
31  * ]| This example pipeline will encode and payload AC3 stream. Refer to
32  * the rtpac3depay example to depayload and decode the RTP stream.
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include <string.h>
41
42 #include <gst/rtp/gstrtpbuffer.h>
43
44 #include "gstrtpac3pay.h"
45
46 GST_DEBUG_CATEGORY_STATIC (rtpac3pay_debug);
47 #define GST_CAT_DEFAULT (rtpac3pay_debug)
48
49 static GstStaticPadTemplate gst_rtp_ac3_pay_sink_template =
50     GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("audio/ac3; " "audio/x-ac3; ")
54     );
55
56 static GstStaticPadTemplate gst_rtp_ac3_pay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("application/x-rtp, "
61         "media = (string) \"audio\", "
62         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
63         "clock-rate = (int) { 32000, 44100, 48000 }, "
64         "encoding-name = (string) \"AC3\"")
65     );
66
67 static void gst_rtp_ac3_pay_finalize (GObject * object);
68
69 static GstStateChangeReturn gst_rtp_ac3_pay_change_state (GstElement * element,
70     GstStateChange transition);
71
72 static gboolean gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload,
73     GstCaps * caps);
74 static gboolean gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload,
75     GstEvent * event);
76 static GstFlowReturn gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay);
77 static GstFlowReturn gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * payload,
78     GstBuffer * buffer);
79
80 #define gst_rtp_ac3_pay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpAC3Pay, gst_rtp_ac3_pay, GST_TYPE_RTP_BASE_PAYLOAD);
82
83 static void
84 gst_rtp_ac3_pay_class_init (GstRtpAC3PayClass * klass)
85 {
86   GObjectClass *gobject_class;
87   GstElementClass *gstelement_class;
88   GstRTPBasePayloadClass *gstrtpbasepayload_class;
89
90   GST_DEBUG_CATEGORY_INIT (rtpac3pay_debug, "rtpac3pay", 0,
91       "AC3 Audio RTP Depayloader");
92
93   gobject_class = (GObjectClass *) klass;
94   gstelement_class = (GstElementClass *) klass;
95   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
96
97   gobject_class->finalize = gst_rtp_ac3_pay_finalize;
98
99   gstelement_class->change_state = gst_rtp_ac3_pay_change_state;
100
101   gst_element_class_add_pad_template (gstelement_class,
102       gst_static_pad_template_get (&gst_rtp_ac3_pay_src_template));
103   gst_element_class_add_pad_template (gstelement_class,
104       gst_static_pad_template_get (&gst_rtp_ac3_pay_sink_template));
105
106   gst_element_class_set_static_metadata (gstelement_class,
107       "RTP AC3 audio payloader", "Codec/Payloader/Network/RTP",
108       "Payload AC3 audio as RTP packets (RFC 4184)",
109       "Wim Taymans <wim.taymans@gmail.com>");
110
111   gstrtpbasepayload_class->set_caps = gst_rtp_ac3_pay_setcaps;
112   gstrtpbasepayload_class->sink_event = gst_rtp_ac3_pay_sink_event;
113   gstrtpbasepayload_class->handle_buffer = gst_rtp_ac3_pay_handle_buffer;
114 }
115
116 static void
117 gst_rtp_ac3_pay_init (GstRtpAC3Pay * rtpac3pay)
118 {
119   rtpac3pay->adapter = gst_adapter_new ();
120 }
121
122 static void
123 gst_rtp_ac3_pay_finalize (GObject * object)
124 {
125   GstRtpAC3Pay *rtpac3pay;
126
127   rtpac3pay = GST_RTP_AC3_PAY (object);
128
129   g_object_unref (rtpac3pay->adapter);
130
131   G_OBJECT_CLASS (parent_class)->finalize (object);
132 }
133
134 static void
135 gst_rtp_ac3_pay_reset (GstRtpAC3Pay * pay)
136 {
137   pay->first_ts = -1;
138   pay->duration = 0;
139   gst_adapter_clear (pay->adapter);
140   GST_DEBUG_OBJECT (pay, "reset depayloader");
141 }
142
143 static gboolean
144 gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
145 {
146   gboolean res;
147   gint rate;
148   GstStructure *structure;
149
150   structure = gst_caps_get_structure (caps, 0);
151
152   if (!gst_structure_get_int (structure, "rate", &rate))
153     rate = 90000;               /* default */
154
155   gst_rtp_base_payload_set_options (payload, "audio", TRUE, "AC3", rate);
156   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
157
158   return res;
159 }
160
161 static gboolean
162 gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
163 {
164   gboolean res;
165   GstRtpAC3Pay *rtpac3pay;
166
167   rtpac3pay = GST_RTP_AC3_PAY (payload);
168
169   switch (GST_EVENT_TYPE (event)) {
170     case GST_EVENT_EOS:
171       /* make sure we push the last packets in the adapter on EOS */
172       gst_rtp_ac3_pay_flush (rtpac3pay);
173       break;
174     case GST_EVENT_FLUSH_STOP:
175       gst_rtp_ac3_pay_reset (rtpac3pay);
176       break;
177     default:
178       break;
179   }
180
181   res = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
182
183   return res;
184 }
185
186 struct frmsize_s
187 {
188   guint16 bit_rate;
189   guint16 frm_size[3];
190 };
191
192 static const struct frmsize_s frmsizecod_tbl[] = {
193   {32, {64, 69, 96}},
194   {32, {64, 70, 96}},
195   {40, {80, 87, 120}},
196   {40, {80, 88, 120}},
197   {48, {96, 104, 144}},
198   {48, {96, 105, 144}},
199   {56, {112, 121, 168}},
200   {56, {112, 122, 168}},
201   {64, {128, 139, 192}},
202   {64, {128, 140, 192}},
203   {80, {160, 174, 240}},
204   {80, {160, 175, 240}},
205   {96, {192, 208, 288}},
206   {96, {192, 209, 288}},
207   {112, {224, 243, 336}},
208   {112, {224, 244, 336}},
209   {128, {256, 278, 384}},
210   {128, {256, 279, 384}},
211   {160, {320, 348, 480}},
212   {160, {320, 349, 480}},
213   {192, {384, 417, 576}},
214   {192, {384, 418, 576}},
215   {224, {448, 487, 672}},
216   {224, {448, 488, 672}},
217   {256, {512, 557, 768}},
218   {256, {512, 558, 768}},
219   {320, {640, 696, 960}},
220   {320, {640, 697, 960}},
221   {384, {768, 835, 1152}},
222   {384, {768, 836, 1152}},
223   {448, {896, 975, 1344}},
224   {448, {896, 976, 1344}},
225   {512, {1024, 1114, 1536}},
226   {512, {1024, 1115, 1536}},
227   {576, {1152, 1253, 1728}},
228   {576, {1152, 1254, 1728}},
229   {640, {1280, 1393, 1920}},
230   {640, {1280, 1394, 1920}}
231 };
232
233 static GstFlowReturn
234 gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
235 {
236   guint avail, FT, NF, mtu;
237   GstBuffer *outbuf;
238   GstFlowReturn ret;
239
240   /* the data available in the adapter is either smaller
241    * than the MTU or bigger. In the case it is smaller, the complete
242    * adapter contents can be put in one packet. In the case the
243    * adapter has more than one MTU, we need to split the AC3 data
244    * over multiple packets. */
245   avail = gst_adapter_available (rtpac3pay->adapter);
246
247   ret = GST_FLOW_OK;
248
249   FT = 0;
250   /* number of frames */
251   NF = rtpac3pay->NF;
252
253   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpac3pay);
254
255   GST_LOG_OBJECT (rtpac3pay, "flushing %u bytes", avail);
256
257   while (avail > 0) {
258     guint towrite;
259     guint8 *payload;
260     guint payload_len;
261     guint packet_len;
262     GstRTPBuffer rtp = { NULL, };
263     GstBuffer *payload_buffer;
264
265     /* this will be the total length of the packet */
266     packet_len = gst_rtp_buffer_calc_packet_len (2 + avail, 0, 0);
267
268     /* fill one MTU or all available bytes */
269     towrite = MIN (packet_len, mtu);
270
271     /* this is the payload length */
272     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
273
274     /* create buffer to hold the payload */
275     outbuf = gst_rtp_buffer_new_allocate (2, 0, 0);
276
277     if (FT == 0) {
278       /* check if it all fits */
279       if (towrite < packet_len) {
280         guint maxlen;
281
282         GST_LOG_OBJECT (rtpac3pay, "we need to fragment");
283         /* check if we will be able to put at least 5/8th of the total
284          * frame in this first frame. */
285         if ((avail * 5) / 8 >= (payload_len - 2))
286           FT = 1;
287         else
288           FT = 2;
289         /* check how many fragments we will need */
290         maxlen = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
291         NF = (avail + maxlen - 1) / maxlen;
292       }
293     } else if (FT != 3) {
294       /* remaining fragment */
295       FT = 3;
296     }
297
298     /*
299      *  0                   1
300      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
301      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
302      * |    MBZ    | FT|       NF      |
303      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
304      *
305      * FT: 0: one or more complete frames
306      *     1: initial 5/8 fragment
307      *     2: initial fragment not 5/8
308      *     3: other fragment
309      * NF: amount of frames if FT = 0, else number of fragments.
310      */
311     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
312     GST_LOG_OBJECT (rtpac3pay, "FT %u, NF %u", FT, NF);
313     payload = gst_rtp_buffer_get_payload (&rtp);
314     payload[0] = (FT & 3);
315     payload[1] = NF;
316     payload_len -= 2;
317
318     if (avail == payload_len)
319       gst_rtp_buffer_set_marker (&rtp, TRUE);
320     gst_rtp_buffer_unmap (&rtp);
321
322     payload_buffer =
323         gst_adapter_take_buffer_fast (rtpac3pay->adapter, payload_len);
324     outbuf = gst_buffer_append (outbuf, payload_buffer);
325
326     avail -= payload_len;
327
328     GST_BUFFER_PTS (outbuf) = rtpac3pay->first_ts;
329     GST_BUFFER_DURATION (outbuf) = rtpac3pay->duration;
330
331     ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpac3pay), outbuf);
332   }
333
334   return ret;
335 }
336
337 static GstFlowReturn
338 gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * basepayload,
339     GstBuffer * buffer)
340 {
341   GstRtpAC3Pay *rtpac3pay;
342   GstFlowReturn ret;
343   gsize avail, left, NF;
344   GstMapInfo map;
345   guint8 *p;
346   guint packet_len;
347   GstClockTime duration, timestamp;
348
349   rtpac3pay = GST_RTP_AC3_PAY (basepayload);
350
351   gst_buffer_map (buffer, &map, GST_MAP_READ);
352   duration = GST_BUFFER_DURATION (buffer);
353   timestamp = GST_BUFFER_PTS (buffer);
354
355   if (GST_BUFFER_IS_DISCONT (buffer)) {
356     GST_DEBUG_OBJECT (rtpac3pay, "DISCONT");
357     gst_rtp_ac3_pay_reset (rtpac3pay);
358   }
359
360   /* count the amount of incomming packets */
361   NF = 0;
362   left = map.size;
363   p = map.data;
364   while (TRUE) {
365     guint bsid, fscod, frmsizecod, frame_size;
366
367     if (left < 6)
368       break;
369
370     if (p[0] != 0x0b || p[1] != 0x77)
371       break;
372
373     bsid = p[5] >> 3;
374     if (bsid > 8)
375       break;
376
377     frmsizecod = p[4] & 0x3f;
378     fscod = p[4] >> 6;
379
380     GST_DEBUG_OBJECT (rtpac3pay, "fscod %u, %u", fscod, frmsizecod);
381
382     if (fscod >= 3 || frmsizecod >= 38)
383       break;
384
385     frame_size = frmsizecod_tbl[frmsizecod].frm_size[fscod] * 2;
386     if (frame_size > left)
387       break;
388
389     NF++;
390     GST_DEBUG_OBJECT (rtpac3pay, "found frame %" G_GSIZE_FORMAT " of size %u",
391         NF, frame_size);
392
393     p += frame_size;
394     left -= frame_size;
395   }
396   gst_buffer_unmap (buffer, &map);
397   if (NF == 0)
398     goto no_frames;
399
400   avail = gst_adapter_available (rtpac3pay->adapter);
401
402   /* get packet length of previous data and this new data,
403    * payload length includes a 4 byte header */
404   packet_len = gst_rtp_buffer_calc_packet_len (2 + avail + map.size, 0, 0);
405
406   /* if this buffer is going to overflow the packet, flush what we
407    * have. */
408   if (gst_rtp_base_payload_is_filled (basepayload,
409           packet_len, rtpac3pay->duration + duration)) {
410     ret = gst_rtp_ac3_pay_flush (rtpac3pay);
411     avail = 0;
412   } else {
413     ret = GST_FLOW_OK;
414   }
415
416   if (avail == 0) {
417     GST_DEBUG_OBJECT (rtpac3pay,
418         "first packet, save timestamp %" GST_TIME_FORMAT,
419         GST_TIME_ARGS (timestamp));
420     rtpac3pay->first_ts = timestamp;
421     rtpac3pay->duration = 0;
422     rtpac3pay->NF = 0;
423   }
424
425   gst_adapter_push (rtpac3pay->adapter, buffer);
426   rtpac3pay->duration += duration;
427   rtpac3pay->NF += NF;
428
429   return ret;
430
431   /* ERRORS */
432 no_frames:
433   {
434     GST_WARNING_OBJECT (rtpac3pay, "no valid AC3 frames found");
435     return GST_FLOW_OK;
436   }
437 }
438
439 static GstStateChangeReturn
440 gst_rtp_ac3_pay_change_state (GstElement * element, GstStateChange transition)
441 {
442   GstRtpAC3Pay *rtpac3pay;
443   GstStateChangeReturn ret;
444
445   rtpac3pay = GST_RTP_AC3_PAY (element);
446
447   switch (transition) {
448     case GST_STATE_CHANGE_READY_TO_PAUSED:
449       gst_rtp_ac3_pay_reset (rtpac3pay);
450       break;
451     default:
452       break;
453   }
454
455   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
456
457   switch (transition) {
458     case GST_STATE_CHANGE_PAUSED_TO_READY:
459       gst_rtp_ac3_pay_reset (rtpac3pay);
460       break;
461     default:
462       break;
463   }
464   return ret;
465 }
466
467 gboolean
468 gst_rtp_ac3_pay_plugin_init (GstPlugin * plugin)
469 {
470   return gst_element_register (plugin, "rtpac3pay",
471       GST_RANK_SECONDARY, GST_TYPE_RTP_AC3_PAY);
472 }