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