gst/rtp/gstrtpL16depay.c: Check if clock-rate and channels are valid.
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpac3depay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim@fluendo.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 /* elementfactory information */
33 static const GstElementDetails gst_rtp_ac3depay_details =
34 GST_ELEMENT_DETAILS ("RTP packet depayloader",
35     "Codec/Depayloader/Network",
36     "Extracts AC3 audio from RTP packets (RFC 4184)",
37     "Wim Taymans <wim@fluendo.com>");
38
39 static GstStaticPadTemplate gst_rtp_ac3_depay_src_template =
40 GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("audio/ac3")
44     );
45
46 static GstStaticPadTemplate gst_rtp_ac3_depay_sink_template =
47 GST_STATIC_PAD_TEMPLATE ("sink",
48     GST_PAD_SINK,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("application/x-rtp, "
51         "media = (string) \"audio\", "
52         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53         "clock-rate = (int) { 32000, 44100, 48000 }, "
54         "encoding-name = (string) \"AC3\"")
55     );
56
57 GST_BOILERPLATE (GstRtpAC3Depay, gst_rtp_ac3_depay, GstBaseRTPDepayload,
58     GST_TYPE_BASE_RTP_DEPAYLOAD);
59
60 static gboolean gst_rtp_ac3_depay_setcaps (GstBaseRTPDepayload * depayload,
61     GstCaps * caps);
62 static GstBuffer *gst_rtp_ac3_depay_process (GstBaseRTPDepayload * depayload,
63     GstBuffer * buf);
64
65 static GstStateChangeReturn gst_rtp_ac3_depay_change_state (GstElement *
66     element, GstStateChange transition);
67
68 static void
69 gst_rtp_ac3_depay_base_init (gpointer klass)
70 {
71   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
72
73   gst_element_class_add_pad_template (element_class,
74       gst_static_pad_template_get (&gst_rtp_ac3_depay_src_template));
75   gst_element_class_add_pad_template (element_class,
76       gst_static_pad_template_get (&gst_rtp_ac3_depay_sink_template));
77
78   gst_element_class_set_details (element_class, &gst_rtp_ac3depay_details);
79 }
80
81 static void
82 gst_rtp_ac3_depay_class_init (GstRtpAC3DepayClass * klass)
83 {
84   GstElementClass *gstelement_class;
85   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
86
87   gstelement_class = (GstElementClass *) klass;
88   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
89
90   parent_class = g_type_class_peek_parent (klass);
91
92   gstelement_class->change_state = gst_rtp_ac3_depay_change_state;
93
94   gstbasertpdepayload_class->set_caps = gst_rtp_ac3_depay_setcaps;
95   gstbasertpdepayload_class->process = gst_rtp_ac3_depay_process;
96
97   GST_DEBUG_CATEGORY_INIT (rtpac3depay_debug, "rtpac3depay", 0,
98       "MPEG Audio RTP Depayloader");
99 }
100
101 static void
102 gst_rtp_ac3_depay_init (GstRtpAC3Depay * rtpac3depay,
103     GstRtpAC3DepayClass * klass)
104 {
105   /* needed because of GST_BOILERPLATE */
106 }
107
108 static gboolean
109 gst_rtp_ac3_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
110 {
111   GstStructure *structure;
112   GstRtpAC3Depay *rtpac3depay;
113   gint clock_rate;
114   GstCaps *srccaps;
115   gboolean res;
116
117   rtpac3depay = GST_RTP_AC3_DEPAY (depayload);
118
119   structure = gst_caps_get_structure (caps, 0);
120
121   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
122     clock_rate = 90000;         /* default */
123   depayload->clock_rate = clock_rate;
124
125   srccaps = gst_caps_new_simple ("audio/ac3", NULL);
126   res = gst_pad_set_caps (depayload->srcpad, srccaps);
127   gst_caps_unref (srccaps);
128
129   return res;
130 }
131
132 struct frmsize_s
133 {
134   guint16 bit_rate;
135   guint16 frm_size[3];
136 };
137
138 static const struct frmsize_s frmsizecod_tbl[] = {
139   {32, {64, 69, 96}},
140   {32, {64, 70, 96}},
141   {40, {80, 87, 120}},
142   {40, {80, 88, 120}},
143   {48, {96, 104, 144}},
144   {48, {96, 105, 144}},
145   {56, {112, 121, 168}},
146   {56, {112, 122, 168}},
147   {64, {128, 139, 192}},
148   {64, {128, 140, 192}},
149   {80, {160, 174, 240}},
150   {80, {160, 175, 240}},
151   {96, {192, 208, 288}},
152   {96, {192, 209, 288}},
153   {112, {224, 243, 336}},
154   {112, {224, 244, 336}},
155   {128, {256, 278, 384}},
156   {128, {256, 279, 384}},
157   {160, {320, 348, 480}},
158   {160, {320, 349, 480}},
159   {192, {384, 417, 576}},
160   {192, {384, 418, 576}},
161   {224, {448, 487, 672}},
162   {224, {448, 488, 672}},
163   {256, {512, 557, 768}},
164   {256, {512, 558, 768}},
165   {320, {640, 696, 960}},
166   {320, {640, 697, 960}},
167   {384, {768, 835, 1152}},
168   {384, {768, 836, 1152}},
169   {448, {896, 975, 1344}},
170   {448, {896, 976, 1344}},
171   {512, {1024, 1114, 1536}},
172   {512, {1024, 1115, 1536}},
173   {576, {1152, 1253, 1728}},
174   {576, {1152, 1254, 1728}},
175   {640, {1280, 1393, 1920}},
176   {640, {1280, 1394, 1920}}
177 };
178
179 static GstBuffer *
180 gst_rtp_ac3_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
181 {
182   GstRtpAC3Depay *rtpac3depay;
183   GstBuffer *outbuf;
184
185   rtpac3depay = GST_RTP_AC3_DEPAY (depayload);
186
187   {
188     gint payload_len;
189     guint8 *payload;
190     guint16 FT, NF;
191
192     payload_len = gst_rtp_buffer_get_payload_len (buf);
193     payload = gst_rtp_buffer_get_payload (buf);
194
195     if (payload_len <= 2)
196       goto empty_packet;
197
198     /* strip off header
199      *
200      *  0                   1
201      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
202      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203      * |    MBZ    | FT|       NF      |
204      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205      */
206     FT = payload[0] & 0x3;
207     NF = payload[1];
208
209     GST_DEBUG_OBJECT (rtpac3depay, "FT: %d, NF: %d", FT, NF);
210
211     payload_len -= 2;
212     payload += 2;
213
214     /* We don't bother with fragmented packets yet */
215     outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 2, -1);
216
217     GST_DEBUG_OBJECT (rtpac3depay, "pushing buffer of size %d",
218         GST_BUFFER_SIZE (outbuf));
219
220     return outbuf;
221   }
222
223   return NULL;
224
225   /* ERRORS */
226 empty_packet:
227   {
228     GST_ELEMENT_WARNING (rtpac3depay, STREAM, DECODE,
229         ("Empty Payload."), (NULL));
230     return NULL;
231   }
232 }
233
234 static GstStateChangeReturn
235 gst_rtp_ac3_depay_change_state (GstElement * element, GstStateChange transition)
236 {
237   GstRtpAC3Depay *rtpac3depay;
238   GstStateChangeReturn ret;
239
240   rtpac3depay = GST_RTP_AC3_DEPAY (element);
241
242   /*
243      switch (transition) {
244      case GST_STATE_CHANGE_NULL_TO_READY:
245      break;
246      default:
247      break;
248      }
249    */
250
251   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
252
253   /*
254      switch (transition) {
255      case GST_STATE_CHANGE_READY_TO_NULL:
256      break;
257      default:
258      break;
259      }
260    */
261   return ret;
262 }
263
264 gboolean
265 gst_rtp_ac3_depay_plugin_init (GstPlugin * plugin)
266 {
267   return gst_element_register (plugin, "rtpac3depay",
268       GST_RANK_MARGINAL, GST_TYPE_RTP_AC3_DEPAY);
269 }