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