rtpdepay: remove payload type restrictions
[platform/upstream/gstreamer.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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         "clock-rate = (int) { 32000, 44100, 48000 }, "
46         "encoding-name = (string) \"AC3\"")
47     );
48
49 G_DEFINE_TYPE (GstRtpAC3Depay, gst_rtp_ac3_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
50
51 static gboolean gst_rtp_ac3_depay_setcaps (GstRTPBaseDepayload * depayload,
52     GstCaps * caps);
53 static GstBuffer *gst_rtp_ac3_depay_process (GstRTPBaseDepayload * depayload,
54     GstBuffer * buf);
55
56 static void
57 gst_rtp_ac3_depay_class_init (GstRtpAC3DepayClass * klass)
58 {
59   GstElementClass *gstelement_class;
60   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
61
62   gstelement_class = (GstElementClass *) klass;
63   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
64
65   gst_element_class_add_pad_template (gstelement_class,
66       gst_static_pad_template_get (&gst_rtp_ac3_depay_src_template));
67   gst_element_class_add_pad_template (gstelement_class,
68       gst_static_pad_template_get (&gst_rtp_ac3_depay_sink_template));
69
70   gst_element_class_set_static_metadata (gstelement_class,
71       "RTP AC3 depayloader", "Codec/Depayloader/Network/RTP",
72       "Extracts AC3 audio from RTP packets (RFC 4184)",
73       "Wim Taymans <wim.taymans@gmail.com>");
74
75   gstrtpbasedepayload_class->set_caps = gst_rtp_ac3_depay_setcaps;
76   gstrtpbasedepayload_class->process = gst_rtp_ac3_depay_process;
77
78   GST_DEBUG_CATEGORY_INIT (rtpac3depay_debug, "rtpac3depay", 0,
79       "AC3 Audio RTP Depayloader");
80 }
81
82 static void
83 gst_rtp_ac3_depay_init (GstRtpAC3Depay * rtpac3depay)
84 {
85   /* needed because of G_DEFINE_TYPE */
86 }
87
88 static gboolean
89 gst_rtp_ac3_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
90 {
91   GstStructure *structure;
92   gint clock_rate;
93   GstCaps *srccaps;
94   gboolean res;
95
96   structure = gst_caps_get_structure (caps, 0);
97
98   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
99     clock_rate = 90000;         /* default */
100   depayload->clock_rate = clock_rate;
101
102   srccaps = gst_caps_new_empty_simple ("audio/ac3");
103   res = gst_pad_set_caps (depayload->srcpad, srccaps);
104   gst_caps_unref (srccaps);
105
106   return res;
107 }
108
109 struct frmsize_s
110 {
111   guint16 bit_rate;
112   guint16 frm_size[3];
113 };
114
115 static const struct frmsize_s frmsizecod_tbl[] = {
116   {32, {64, 69, 96}},
117   {32, {64, 70, 96}},
118   {40, {80, 87, 120}},
119   {40, {80, 88, 120}},
120   {48, {96, 104, 144}},
121   {48, {96, 105, 144}},
122   {56, {112, 121, 168}},
123   {56, {112, 122, 168}},
124   {64, {128, 139, 192}},
125   {64, {128, 140, 192}},
126   {80, {160, 174, 240}},
127   {80, {160, 175, 240}},
128   {96, {192, 208, 288}},
129   {96, {192, 209, 288}},
130   {112, {224, 243, 336}},
131   {112, {224, 244, 336}},
132   {128, {256, 278, 384}},
133   {128, {256, 279, 384}},
134   {160, {320, 348, 480}},
135   {160, {320, 349, 480}},
136   {192, {384, 417, 576}},
137   {192, {384, 418, 576}},
138   {224, {448, 487, 672}},
139   {224, {448, 488, 672}},
140   {256, {512, 557, 768}},
141   {256, {512, 558, 768}},
142   {320, {640, 696, 960}},
143   {320, {640, 697, 960}},
144   {384, {768, 835, 1152}},
145   {384, {768, 836, 1152}},
146   {448, {896, 975, 1344}},
147   {448, {896, 976, 1344}},
148   {512, {1024, 1114, 1536}},
149   {512, {1024, 1115, 1536}},
150   {576, {1152, 1253, 1728}},
151   {576, {1152, 1254, 1728}},
152   {640, {1280, 1393, 1920}},
153   {640, {1280, 1394, 1920}}
154 };
155
156 static GstBuffer *
157 gst_rtp_ac3_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
158 {
159   GstRtpAC3Depay *rtpac3depay;
160   GstBuffer *outbuf;
161   GstRTPBuffer rtp = { NULL, };
162   guint8 *payload;
163   guint16 FT, NF;
164
165   rtpac3depay = GST_RTP_AC3_DEPAY (depayload);
166
167   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
168
169   if (gst_rtp_buffer_get_payload_len (&rtp) < 2)
170     goto empty_packet;
171
172   payload = gst_rtp_buffer_get_payload (&rtp);
173
174   /* strip off header
175    *
176    *  0                   1
177    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
178    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
179    * |    MBZ    | FT|       NF      |
180    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
181    */
182   FT = payload[0] & 0x3;
183   NF = payload[1];
184
185   GST_DEBUG_OBJECT (rtpac3depay, "FT: %d, NF: %d", FT, NF);
186
187   /* We don't bother with fragmented packets yet */
188   outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 2, -1);
189
190   gst_rtp_buffer_unmap (&rtp);
191
192   if (outbuf)
193     GST_DEBUG_OBJECT (rtpac3depay, "pushing buffer of size %" G_GSIZE_FORMAT,
194         gst_buffer_get_size (outbuf));
195
196   return outbuf;
197
198   /* ERRORS */
199 empty_packet:
200   {
201     GST_ELEMENT_WARNING (rtpac3depay, STREAM, DECODE,
202         ("Empty Payload."), (NULL));
203     gst_rtp_buffer_unmap (&rtp);
204     return NULL;
205   }
206 }
207
208 gboolean
209 gst_rtp_ac3_depay_plugin_init (GstPlugin * plugin)
210 {
211   return gst_element_register (plugin, "rtpac3depay",
212       GST_RANK_SECONDARY, GST_TYPE_RTP_AC3_DEPAY);
213 }