Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpac3depay.c
1 /* GStreamer
2  * Copyright (C) <2007> 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 <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
27 #include "gstrtpac3depay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpac3depay_debug);
30 #define GST_CAT_DEFAULT (rtpac3depay_debug)
31
32 static GstStaticPadTemplate gst_rtp_ac3_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("audio/ac3")
37     );
38
39 static GstStaticPadTemplate gst_rtp_ac3_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("application/x-rtp, "
44         "media = (string) \"audio\", "
45         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
46         "clock-rate = (int) { 32000, 44100, 48000 }, "
47         "encoding-name = (string) \"AC3\"")
48     );
49
50 GST_BOILERPLATE (GstRtpAC3Depay, gst_rtp_ac3_depay, GstBaseRTPDepayload,
51     GST_TYPE_BASE_RTP_DEPAYLOAD);
52
53 static gboolean gst_rtp_ac3_depay_setcaps (GstBaseRTPDepayload * depayload,
54     GstCaps * caps);
55 static GstBuffer *gst_rtp_ac3_depay_process (GstBaseRTPDepayload * depayload,
56     GstBuffer * buf);
57
58 static void
59 gst_rtp_ac3_depay_base_init (gpointer klass)
60 {
61   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
62
63   gst_element_class_add_static_pad_template (element_class,
64       &gst_rtp_ac3_depay_src_template);
65   gst_element_class_add_static_pad_template (element_class,
66       &gst_rtp_ac3_depay_sink_template);
67
68   gst_element_class_set_details_simple (element_class, "RTP AC3 depayloader",
69       "Codec/Depayloader/Network/RTP",
70       "Extracts AC3 audio from RTP packets (RFC 4184)",
71       "Wim Taymans <wim.taymans@gmail.com>");
72 }
73
74 static void
75 gst_rtp_ac3_depay_class_init (GstRtpAC3DepayClass * klass)
76 {
77   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
78
79   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
80
81   gstbasertpdepayload_class->set_caps = gst_rtp_ac3_depay_setcaps;
82   gstbasertpdepayload_class->process = gst_rtp_ac3_depay_process;
83
84   GST_DEBUG_CATEGORY_INIT (rtpac3depay_debug, "rtpac3depay", 0,
85       "AC3 Audio RTP Depayloader");
86 }
87
88 static void
89 gst_rtp_ac3_depay_init (GstRtpAC3Depay * rtpac3depay,
90     GstRtpAC3DepayClass * klass)
91 {
92   /* needed because of GST_BOILERPLATE */
93 }
94
95 static gboolean
96 gst_rtp_ac3_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
97 {
98   GstStructure *structure;
99   gint clock_rate;
100   GstCaps *srccaps;
101   gboolean res;
102
103   structure = gst_caps_get_structure (caps, 0);
104
105   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
106     clock_rate = 90000;         /* default */
107   depayload->clock_rate = clock_rate;
108
109   srccaps = gst_caps_new_simple ("audio/ac3", NULL);
110   res = gst_pad_set_caps (depayload->srcpad, srccaps);
111   gst_caps_unref (srccaps);
112
113   return res;
114 }
115
116 struct frmsize_s
117 {
118   guint16 bit_rate;
119   guint16 frm_size[3];
120 };
121
122 static const struct frmsize_s frmsizecod_tbl[] = {
123   {32, {64, 69, 96}},
124   {32, {64, 70, 96}},
125   {40, {80, 87, 120}},
126   {40, {80, 88, 120}},
127   {48, {96, 104, 144}},
128   {48, {96, 105, 144}},
129   {56, {112, 121, 168}},
130   {56, {112, 122, 168}},
131   {64, {128, 139, 192}},
132   {64, {128, 140, 192}},
133   {80, {160, 174, 240}},
134   {80, {160, 175, 240}},
135   {96, {192, 208, 288}},
136   {96, {192, 209, 288}},
137   {112, {224, 243, 336}},
138   {112, {224, 244, 336}},
139   {128, {256, 278, 384}},
140   {128, {256, 279, 384}},
141   {160, {320, 348, 480}},
142   {160, {320, 349, 480}},
143   {192, {384, 417, 576}},
144   {192, {384, 418, 576}},
145   {224, {448, 487, 672}},
146   {224, {448, 488, 672}},
147   {256, {512, 557, 768}},
148   {256, {512, 558, 768}},
149   {320, {640, 696, 960}},
150   {320, {640, 697, 960}},
151   {384, {768, 835, 1152}},
152   {384, {768, 836, 1152}},
153   {448, {896, 975, 1344}},
154   {448, {896, 976, 1344}},
155   {512, {1024, 1114, 1536}},
156   {512, {1024, 1115, 1536}},
157   {576, {1152, 1253, 1728}},
158   {576, {1152, 1254, 1728}},
159   {640, {1280, 1393, 1920}},
160   {640, {1280, 1394, 1920}}
161 };
162
163 static GstBuffer *
164 gst_rtp_ac3_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
165 {
166   GstRtpAC3Depay *rtpac3depay;
167   GstBuffer *outbuf;
168
169   rtpac3depay = GST_RTP_AC3_DEPAY (depayload);
170
171   {
172     guint8 *payload;
173     guint16 FT, NF;
174
175     if (gst_rtp_buffer_get_payload_len (buf) < 2)
176       goto empty_packet;
177
178     payload = gst_rtp_buffer_get_payload (buf);
179
180     /* strip off header
181      *
182      *  0                   1
183      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
184      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
185      * |    MBZ    | FT|       NF      |
186      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
187      */
188     FT = payload[0] & 0x3;
189     NF = payload[1];
190
191     GST_DEBUG_OBJECT (rtpac3depay, "FT: %d, NF: %d", FT, NF);
192
193     /* We don't bother with fragmented packets yet */
194     outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 2, -1);
195
196     if (outbuf)
197         GST_DEBUG_OBJECT (rtpac3depay, "pushing buffer of size %d",
198             GST_BUFFER_SIZE (outbuf));
199
200     return outbuf;
201   }
202
203   return NULL;
204
205   /* ERRORS */
206 empty_packet:
207   {
208     GST_ELEMENT_WARNING (rtpac3depay, STREAM, DECODE,
209         ("Empty Payload."), (NULL));
210     return NULL;
211   }
212 }
213
214 gboolean
215 gst_rtp_ac3_depay_plugin_init (GstPlugin * plugin)
216 {
217   return gst_element_register (plugin, "rtpac3depay",
218       GST_RANK_SECONDARY, GST_TYPE_RTP_AC3_DEPAY);
219 }