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