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