c48c82b204fa27c01f9b33c30fd0470fbf1b6c67
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpmpvdepay.c
1 /* GStreamer
2  * Copyright (C) <2006> 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 "gstrtpmpvdepay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpmpvdepay_debug);
30 #define GST_CAT_DEFAULT (rtpmpvdepay_debug)
31
32 /* FIXME, we set the mpeg version to 2, we should ideally be looking at contents
33  * of the stream to figure out the version */
34 static GstStaticPadTemplate gst_rtp_mpv_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
36     GST_PAD_SRC,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS
39     ("video/mpeg, mpegversion = (int) 2, systemstream = (boolean) FALSE")
40     );
41
42 static GstStaticPadTemplate gst_rtp_mpv_depay_sink_template =
43     GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("application/x-rtp, "
47         "media = (string) \"video\", "
48         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
49         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\";"
50         "application/x-rtp, "
51         "media = (string) \"video\", "
52         "payload = (int) " GST_RTP_PAYLOAD_MPV_STRING ", "
53         "clock-rate = (int) 90000")
54     );
55
56 GST_BOILERPLATE (GstRtpMPVDepay, gst_rtp_mpv_depay, GstBaseRTPDepayload,
57     GST_TYPE_BASE_RTP_DEPAYLOAD);
58
59 static gboolean gst_rtp_mpv_depay_setcaps (GstBaseRTPDepayload * depayload,
60     GstCaps * caps);
61 static GstBuffer *gst_rtp_mpv_depay_process (GstBaseRTPDepayload * depayload,
62     GstBuffer * buf);
63
64 static void
65 gst_rtp_mpv_depay_base_init (gpointer klass)
66 {
67   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
68
69   gst_element_class_add_static_pad_template (element_class,
70       &gst_rtp_mpv_depay_src_template);
71   gst_element_class_add_static_pad_template (element_class,
72       &gst_rtp_mpv_depay_sink_template);
73
74   gst_element_class_set_details_simple (element_class,
75       "RTP MPEG video depayloader", "Codec/Depayloader/Network/RTP",
76       "Extracts MPEG video from RTP packets (RFC 2250)",
77       "Wim Taymans <wim.taymans@gmail.com>");
78 }
79
80 static void
81 gst_rtp_mpv_depay_class_init (GstRtpMPVDepayClass * klass)
82 {
83   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
84
85   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
86
87   gstbasertpdepayload_class->set_caps = gst_rtp_mpv_depay_setcaps;
88   gstbasertpdepayload_class->process = gst_rtp_mpv_depay_process;
89
90   GST_DEBUG_CATEGORY_INIT (rtpmpvdepay_debug, "rtpmpvdepay", 0,
91       "MPEG Video RTP Depayloader");
92 }
93
94 static void
95 gst_rtp_mpv_depay_init (GstRtpMPVDepay * rtpmpvdepay,
96     GstRtpMPVDepayClass * klass)
97 {
98   /* needed because of GST_BOILERPLATE */
99 }
100
101 static gboolean
102 gst_rtp_mpv_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
103 {
104   GstStructure *structure;
105   gint clock_rate;
106   GstCaps *outcaps;
107   gboolean res;
108
109   structure = gst_caps_get_structure (caps, 0);
110
111   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
112     clock_rate = 90000;         /* default */
113   depayload->clock_rate = clock_rate;
114
115   outcaps = gst_caps_new_simple ("video/mpeg",
116       "mpegversion", G_TYPE_INT, 2,
117       "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
118   res = gst_pad_set_caps (depayload->srcpad, outcaps);
119   gst_caps_unref (outcaps);
120
121   return res;
122 }
123
124 static GstBuffer *
125 gst_rtp_mpv_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
126 {
127   GstRtpMPVDepay *rtpmpvdepay;
128   GstBuffer *outbuf;
129
130   rtpmpvdepay = GST_RTP_MPV_DEPAY (depayload);
131
132   {
133     gint payload_len, payload_header;
134     guint8 *payload;
135     guint8 T;
136
137     payload_len = gst_rtp_buffer_get_payload_len (buf);
138     payload = gst_rtp_buffer_get_payload (buf);
139     payload_header = 0;
140
141     if (payload_len <= 4)
142       goto empty_packet;
143
144     /* 3.4 MPEG Video-specific header
145      *
146      *  0                   1                   2                   3
147      *  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
148      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149      * |    MBZ  |T|         TR        | |N|S|B|E|  P  | | BFC | | FFC |
150      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
151      *                                  AN              FBV     FFV
152      */
153     T = (payload[0] & 0x04);
154
155     payload_len -= 4;
156     payload_header += 4;
157     payload += 4;
158
159     if (T) {
160       /* 
161        * 3.4.1 MPEG-2 Video-specific header extension
162        *
163        *  0                   1                   2                   3
164        *  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
165        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166        * |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|
167        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
168        */
169       if (payload_len <= 4)
170         goto empty_packet;
171
172       payload_len -= 4;
173       payload_header += 4;
174       payload += 4;
175     }
176
177     outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, payload_header, -1);
178
179     if (outbuf) {
180       GST_DEBUG_OBJECT (rtpmpvdepay,
181           "gst_rtp_mpv_depay_chain: pushing buffer of size %d",
182           GST_BUFFER_SIZE (outbuf));
183     }
184
185     return outbuf;
186   }
187
188   return NULL;
189
190   /* ERRORS */
191 empty_packet:
192   {
193     GST_ELEMENT_WARNING (rtpmpvdepay, STREAM, DECODE,
194         (NULL), ("Empty payload."));
195     return NULL;
196   }
197 }
198
199 gboolean
200 gst_rtp_mpv_depay_plugin_init (GstPlugin * plugin)
201 {
202   return gst_element_register (plugin, "rtpmpvdepay",
203       GST_RANK_SECONDARY, GST_TYPE_RTP_MPV_DEPAY);
204 }