gst/rtp/gstrtpL16depay.c: Check if clock-rate and channels are valid.
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpmpvdepay.c
1 /* GStreamer
2  * Copyright (C) <2006> 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 "gstrtpmpvdepay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpmpvdepay_debug);
30 #define GST_CAT_DEFAULT (rtpmpvdepay_debug)
31
32 /* elementfactory information */
33 static const GstElementDetails gst_rtp_mpvdepay_details =
34 GST_ELEMENT_DETAILS ("RTP packet depayloader",
35     "Codec/Depayloader/Network",
36     "Extracts MPEG video from RTP packets (RFC 2250)",
37     "Wim Taymans <wim@fluendo.com>");
38
39 /* FIXME, we set the mpeg version to 2, we should ideally be looking at contents
40  * of the stream to figure out the version */
41 static GstStaticPadTemplate gst_rtp_mpv_depay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS
46     ("video/mpeg, mpegversion = (int) 2, systemstream = (boolean) FALSE")
47     );
48
49 static GstStaticPadTemplate gst_rtp_mpv_depay_sink_template =
50     GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("application/x-rtp, "
54         "media = (string) \"video\", "
55         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\";"
57         "application/x-rtp, "
58         "media = (string) \"video\", "
59         "payload = (int) " GST_RTP_PAYLOAD_MPV_STRING ", "
60         "clock-rate = (int) 90000")
61     );
62
63 GST_BOILERPLATE (GstRtpMPVDepay, gst_rtp_mpv_depay, GstBaseRTPDepayload,
64     GST_TYPE_BASE_RTP_DEPAYLOAD);
65
66 static gboolean gst_rtp_mpv_depay_setcaps (GstBaseRTPDepayload * depayload,
67     GstCaps * caps);
68 static GstBuffer *gst_rtp_mpv_depay_process (GstBaseRTPDepayload * depayload,
69     GstBuffer * buf);
70
71 static GstStateChangeReturn gst_rtp_mpv_depay_change_state (GstElement *
72     element, GstStateChange transition);
73
74 static void
75 gst_rtp_mpv_depay_base_init (gpointer klass)
76 {
77   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
78
79   gst_element_class_add_pad_template (element_class,
80       gst_static_pad_template_get (&gst_rtp_mpv_depay_src_template));
81   gst_element_class_add_pad_template (element_class,
82       gst_static_pad_template_get (&gst_rtp_mpv_depay_sink_template));
83
84   gst_element_class_set_details (element_class, &gst_rtp_mpvdepay_details);
85 }
86
87 static void
88 gst_rtp_mpv_depay_class_init (GstRtpMPVDepayClass * klass)
89 {
90   GObjectClass *gobject_class;
91   GstElementClass *gstelement_class;
92   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
93
94   gobject_class = (GObjectClass *) klass;
95   gstelement_class = (GstElementClass *) klass;
96   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
97
98   parent_class = g_type_class_peek_parent (klass);
99
100   gstelement_class->change_state = gst_rtp_mpv_depay_change_state;
101
102   gstbasertpdepayload_class->set_caps = gst_rtp_mpv_depay_setcaps;
103   gstbasertpdepayload_class->process = gst_rtp_mpv_depay_process;
104
105   GST_DEBUG_CATEGORY_INIT (rtpmpvdepay_debug, "rtpmpvdepay", 0,
106       "MPEG Video RTP Depayloader");
107 }
108
109 static void
110 gst_rtp_mpv_depay_init (GstRtpMPVDepay * rtpmpvdepay,
111     GstRtpMPVDepayClass * klass)
112 {
113   /* needed because of GST_BOILERPLATE */
114 }
115
116 static gboolean
117 gst_rtp_mpv_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
118 {
119   GstStructure *structure;
120   GstRtpMPVDepay *rtpmpvdepay;
121   gint clock_rate;
122   GstCaps *outcaps;
123   gboolean res;
124
125   rtpmpvdepay = GST_RTP_MPV_DEPAY (depayload);
126
127   structure = gst_caps_get_structure (caps, 0);
128
129   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
130     clock_rate = 90000;         /* default */
131   depayload->clock_rate = clock_rate;
132
133   outcaps = gst_caps_new_simple ("video/mpeg",
134       "mpegversion", G_TYPE_INT, 2,
135       "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
136   res = gst_pad_set_caps (depayload->srcpad, outcaps);
137   gst_caps_unref (outcaps);
138
139   return res;
140 }
141
142 static GstBuffer *
143 gst_rtp_mpv_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
144 {
145   GstRtpMPVDepay *rtpmpvdepay;
146   GstBuffer *outbuf;
147
148   rtpmpvdepay = GST_RTP_MPV_DEPAY (depayload);
149
150   {
151     gint payload_len, payload_header;
152     guint8 *payload;
153     guint8 T;
154
155     payload_len = gst_rtp_buffer_get_payload_len (buf);
156     payload = gst_rtp_buffer_get_payload (buf);
157     payload_header = 0;
158
159     if (payload_len <= 4)
160       goto empty_packet;
161
162     /* 3.4 MPEG Video-specific header
163      *
164      *  0                   1                   2                   3
165      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
166      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167      * |    MBZ  |T|         TR        | |N|S|B|E|  P  | | BFC | | FFC |
168      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169      *                                  AN              FBV     FFV
170      */
171     T = (payload[0] & 0x04);
172
173     payload_len -= 4;
174     payload_header += 4;
175     payload += 4;
176
177     if (T) {
178       /* 
179        * 3.4.1 MPEG-2 Video-specific header extension
180        *
181        *  0                   1                   2                   3
182        *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
183        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184        * |X|E|f_[0,0]|f_[0,1]|f_[1,0]|f_[1,1]| DC| PS|T|P|C|Q|V|A|R|H|G|D|
185        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
186        */
187       if (payload_len <= 4)
188         goto empty_packet;
189
190       payload_len -= 4;
191       payload_header += 4;
192       payload += 4;
193     }
194
195     outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, payload_header, -1);
196
197     GST_DEBUG_OBJECT (rtpmpvdepay,
198         "gst_rtp_mpv_depay_chain: pushing buffer of size %d",
199         GST_BUFFER_SIZE (outbuf));
200
201     return outbuf;
202   }
203
204   return NULL;
205
206   /* ERRORS */
207 empty_packet:
208   {
209     GST_ELEMENT_WARNING (rtpmpvdepay, STREAM, DECODE,
210         (NULL), ("Empty payload."));
211     return NULL;
212   }
213 }
214
215 static GstStateChangeReturn
216 gst_rtp_mpv_depay_change_state (GstElement * element, GstStateChange transition)
217 {
218   GstRtpMPVDepay *rtpmpvdepay;
219   GstStateChangeReturn ret;
220
221   rtpmpvdepay = GST_RTP_MPV_DEPAY (element);
222
223   switch (transition) {
224     case GST_STATE_CHANGE_NULL_TO_READY:
225       break;
226     default:
227       break;
228   }
229
230   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
231
232   switch (transition) {
233     case GST_STATE_CHANGE_READY_TO_NULL:
234       break;
235     default:
236       break;
237   }
238   return ret;
239 }
240
241 gboolean
242 gst_rtp_mpv_depay_plugin_init (GstPlugin * plugin)
243 {
244   return gst_element_register (plugin, "rtpmpvdepay",
245       GST_RANK_MARGINAL, GST_TYPE_RTP_MPV_DEPAY);
246 }