Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.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 G_DEFINE_TYPE (GstRtpMPVDepay, gst_rtp_mpv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
57
58 static gboolean gst_rtp_mpv_depay_setcaps (GstRTPBaseDepayload * depayload,
59     GstCaps * caps);
60 static GstBuffer *gst_rtp_mpv_depay_process (GstRTPBaseDepayload * depayload,
61     GstBuffer * buf);
62
63 static void
64 gst_rtp_mpv_depay_class_init (GstRtpMPVDepayClass * klass)
65 {
66   GstElementClass *gstelement_class;
67   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
68
69   gstelement_class = (GstElementClass *) klass;
70   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
71
72   gst_element_class_add_pad_template (gstelement_class,
73       gst_static_pad_template_get (&gst_rtp_mpv_depay_src_template));
74   gst_element_class_add_pad_template (gstelement_class,
75       gst_static_pad_template_get (&gst_rtp_mpv_depay_sink_template));
76
77   gst_element_class_set_static_metadata (gstelement_class,
78       "RTP MPEG video depayloader", "Codec/Depayloader/Network/RTP",
79       "Extracts MPEG video from RTP packets (RFC 2250)",
80       "Wim Taymans <wim.taymans@gmail.com>");
81
82   gstrtpbasedepayload_class->set_caps = gst_rtp_mpv_depay_setcaps;
83   gstrtpbasedepayload_class->process = gst_rtp_mpv_depay_process;
84
85   GST_DEBUG_CATEGORY_INIT (rtpmpvdepay_debug, "rtpmpvdepay", 0,
86       "MPEG Video RTP Depayloader");
87 }
88
89 static void
90 gst_rtp_mpv_depay_init (GstRtpMPVDepay * rtpmpvdepay)
91 {
92 }
93
94 static gboolean
95 gst_rtp_mpv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
96 {
97   GstStructure *structure;
98   gint clock_rate;
99   GstCaps *outcaps;
100   gboolean res;
101
102   structure = gst_caps_get_structure (caps, 0);
103
104   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
105     clock_rate = 90000;         /* default */
106   depayload->clock_rate = clock_rate;
107
108   outcaps = gst_caps_new_simple ("video/mpeg",
109       "mpegversion", G_TYPE_INT, 2,
110       "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
111   res = gst_pad_set_caps (depayload->srcpad, outcaps);
112   gst_caps_unref (outcaps);
113
114   return res;
115 }
116
117 static GstBuffer *
118 gst_rtp_mpv_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
119 {
120   GstRtpMPVDepay *rtpmpvdepay;
121   GstBuffer *outbuf;
122   GstRTPBuffer rtp = { NULL };
123
124   rtpmpvdepay = GST_RTP_MPV_DEPAY (depayload);
125
126   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
127
128   {
129     gint payload_len, payload_header;
130     guint8 *payload;
131     guint8 T;
132
133     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
134     payload = gst_rtp_buffer_get_payload (&rtp);
135     payload_header = 0;
136
137     if (payload_len <= 4)
138       goto empty_packet;
139
140     /* 3.4 MPEG Video-specific header
141      *
142      *  0                   1                   2                   3
143      *  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
144      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145      * |    MBZ  |T|         TR        | |N|S|B|E|  P  | | BFC | | FFC |
146      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147      *                                  AN              FBV     FFV
148      */
149     T = (payload[0] & 0x04);
150
151     payload_len -= 4;
152     payload_header += 4;
153     payload += 4;
154
155     if (T) {
156       /* 
157        * 3.4.1 MPEG-2 Video-specific header extension
158        *
159        *  0                   1                   2                   3
160        *  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
161        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162        * |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|
163        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164        */
165       if (payload_len <= 4)
166         goto empty_packet;
167
168       payload_len -= 4;
169       payload_header += 4;
170       payload += 4;
171     }
172
173     outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, payload_header, -1);
174
175     if (outbuf) {
176       GST_DEBUG_OBJECT (rtpmpvdepay,
177           "gst_rtp_mpv_depay_chain: pushing buffer of size %" G_GSIZE_FORMAT,
178           gst_buffer_get_size (outbuf));
179     }
180     return outbuf;
181   }
182
183   return NULL;
184
185   /* ERRORS */
186 empty_packet:
187   {
188     GST_ELEMENT_WARNING (rtpmpvdepay, STREAM, DECODE,
189         (NULL), ("Empty payload."));
190     return NULL;
191   }
192 }
193
194 gboolean
195 gst_rtp_mpv_depay_plugin_init (GstPlugin * plugin)
196 {
197   return gst_element_register (plugin, "rtpmpvdepay",
198       GST_RANK_SECONDARY, GST_TYPE_RTP_MPV_DEPAY);
199 }