Remove trivial unused variables detected by CLang static analyzer.
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpdvpay.c
1 /* Farsight
2  * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
3  *           (C) 2008 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include "gstrtpdvpay.h"
30
31 GST_DEBUG_CATEGORY (rtpdvpay_debug);
32 #define GST_CAT_DEFAULT (rtpdvpay_debug)
33
34 /* Elementfactory information */
35 static GstElementDetails gst_rtp_dv_pay_details = {
36   "RTP DV Payloader",
37   "Codec/Payloader/Network",
38   "Payloads DV into RTP packets (RFC 3189)",
39   "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>"
40 };
41
42 #define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
43 enum
44 {
45   PROP_0,
46   PROP_MODE
47 };
48
49 /* takes both system and non-system streams */
50 static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("video/x-dv")
55     );
56
57 static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-rtp, "
62         "media = (string) { \"video\", \"audio\" } ,"
63         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
64         "encoding-name = (string) \"DV\", "
65         "clock-rate = (int) 90000,"
66         "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
67         "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
68         "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
69         "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
70         /* optional parameters can't go in the template
71          * "audio = (string) { \"bundled\", \"none\" }"
72          */
73     )
74     );
75
76 static gboolean gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload,
77     GstCaps * caps);
78 static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * payload,
79     GstBuffer * buffer);
80
81 #define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
82 static GType
83 gst_dv_pay_mode_get_type (void)
84 {
85   static GType dv_pay_mode_type = 0;
86   static const GEnumValue dv_pay_modes[] = {
87     {GST_DV_PAY_MODE_VIDEO, "Video only", "video"},
88     {GST_DV_PAY_MODE_BUNDLED, "Video and Audio bundled", "bundled"},
89     {GST_DV_PAY_MODE_AUDIO, "Audio only", "audio"},
90     {0, NULL, NULL},
91   };
92
93   if (!dv_pay_mode_type) {
94     dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
95   }
96   return dv_pay_mode_type;
97 }
98
99
100 static void gst_dv_pay_set_property (GObject * object,
101     guint prop_id, const GValue * value, GParamSpec * pspec);
102 static void gst_dv_pay_get_property (GObject * object,
103     guint prop_id, GValue * value, GParamSpec * pspec);
104
105 GST_BOILERPLATE (GstRTPDVPay, gst_rtp_dv_pay, GstBaseRTPPayload,
106     GST_TYPE_BASE_RTP_PAYLOAD)
107
108      static void gst_rtp_dv_pay_base_init (gpointer g_class)
109 {
110   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
111
112   gst_element_class_add_pad_template (element_class,
113       gst_static_pad_template_get (&gst_rtp_dv_pay_sink_template));
114   gst_element_class_add_pad_template (element_class,
115       gst_static_pad_template_get (&gst_rtp_dv_pay_src_template));
116   gst_element_class_set_details (element_class, &gst_rtp_dv_pay_details);
117 }
118
119 static void
120 gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
121 {
122   GObjectClass *gobject_class;
123   GstBaseRTPPayloadClass *gstbasertppayload_class;
124
125   gobject_class = (GObjectClass *) klass;
126   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
127
128   gobject_class->set_property = gst_dv_pay_set_property;
129   gobject_class->get_property = gst_dv_pay_get_property;
130
131   gstbasertppayload_class->set_caps = gst_rtp_dv_pay_setcaps;
132   gstbasertppayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
133
134   g_object_class_install_property (gobject_class, PROP_MODE,
135       g_param_spec_enum ("mode", "Mode",
136           "The payload mode of payloading",
137           GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
138           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139
140   GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
141 }
142
143 static void
144 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay, GstRTPDVPayClass * klass)
145 {
146 }
147
148 static void
149 gst_dv_pay_set_property (GObject * object,
150     guint prop_id, const GValue * value, GParamSpec * pspec)
151 {
152   GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
153
154   switch (prop_id) {
155     case PROP_MODE:
156       rtpdvpay->mode = g_value_get_enum (value);
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161   }
162 }
163
164 static void
165 gst_dv_pay_get_property (GObject * object,
166     guint prop_id, GValue * value, GParamSpec * pspec)
167 {
168   GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
169
170   switch (prop_id) {
171     case PROP_MODE:
172       g_value_set_enum (value, rtpdvpay->mode);
173       break;
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176       break;
177   }
178 }
179
180 static gboolean
181 gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
182 {
183   /* We don't do anything here, but we could check if it's a system stream and if
184    * it's not, default to sending the video only. We will negotiate downstream
185    * caps when we get to see the first frame. */
186
187   return TRUE;
188 }
189
190 static gboolean
191 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, guint size)
192 {
193   gchar *encode, *media;
194   gboolean audio_bundled;
195
196   if ((data[3] & 0x80) == 0) {  /* DSF flag */
197     /* it's an NTSC format */
198     if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
199       /* NTSC 50Mbps */
200       encode = "314M-25/525-60";
201     } else {                    /* 4:1:1 sampling */
202       /* NTSC 25Mbps */
203       encode = "SD-VCR/525-60";
204     }
205   } else {
206     /* it's a PAL format */
207     if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
208       /* PAL 50Mbps */
209       encode = "314M-50/625-50";
210     } else if ((data[5] & 0x07) == 0) { /* APT flag */
211       /* PAL 25Mbps 4:2:0 */
212       encode = "SD-VCR/625-50";
213     } else
214       /* PAL 25Mbps 4:1:1 */
215       encode = "314M-25/625-50";
216   }
217
218   media = "video";
219   audio_bundled = FALSE;
220
221   switch (rtpdvpay->mode) {
222     case GST_DV_PAY_MODE_AUDIO:
223       media = "audio";
224       break;
225     case GST_DV_PAY_MODE_BUNDLED:
226       audio_bundled = TRUE;
227       break;
228     default:
229       break;
230   }
231   gst_basertppayload_set_options (GST_BASE_RTP_PAYLOAD (rtpdvpay), media, TRUE,
232       "DV", 90000);
233
234   if (audio_bundled) {
235     gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
236         "encode", G_TYPE_STRING, encode,
237         "audio", G_TYPE_STRING, "bundled", NULL);
238   } else {
239     gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
240         "encode", G_TYPE_STRING, encode, NULL);
241   }
242   return TRUE;
243 }
244
245 static gboolean
246 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
247 {
248   gint block_type;
249   gboolean res;
250
251   block_type = data[0] >> 5;
252
253   switch (block_type) {
254     case 0:                    /* Header block */
255     case 1:                    /* Subcode block */
256     case 2:                    /* VAUX block */
257       /* always include these blocks */
258       res = TRUE;
259       break;
260     case 3:                    /* Audio block */
261       /* never include audio if we are doing video only */
262       if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
263         res = FALSE;
264       else
265         res = TRUE;
266       break;
267     case 4:                    /* Video block */
268       /* never include video if we are doing audio only */
269       if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
270         res = FALSE;
271       else
272         res = TRUE;
273       break;
274     default:                   /* Something bogus, just ignore */
275       res = FALSE;
276       break;
277   }
278   return res;
279 }
280
281 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
282  */
283 static GstFlowReturn
284 gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
285     GstBuffer * buffer)
286 {
287   GstRTPDVPay *rtpdvpay;
288   guint max_payload_size;
289   GstBuffer *outbuf;
290   GstFlowReturn ret = GST_FLOW_OK;
291   gint hdrlen;
292   guint size;
293   guint8 *data;
294   guint8 *dest;
295   guint filled;
296
297   rtpdvpay = GST_RTP_DV_PAY (basepayload);
298
299   hdrlen = gst_rtp_buffer_calc_header_len (0);
300   /* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
301    * each, and we should put an integral number of them in each RTP packet.
302    * Therefore, we round the available room down to the nearest multiple of 80.
303    *
304    * The available room is just the packet MTU, minus the RTP header length. */
305   max_payload_size = ((GST_BASE_RTP_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
306
307   /* The length of the buffer to transmit. */
308   size = GST_BUFFER_SIZE (buffer);
309   data = GST_BUFFER_DATA (buffer);
310
311   GST_DEBUG_OBJECT (rtpdvpay,
312       "DV RTP payloader got buffer of %u bytes, splitting in %u byte "
313       "payload fragments, at time %" GST_TIME_FORMAT, size, max_payload_size,
314       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
315
316   if (!rtpdvpay->negotiated) {
317     gst_dv_pay_negotiate (rtpdvpay, data, size);
318     /* if we have not yet scanned the stream for its type, do so now */
319     rtpdvpay->negotiated = TRUE;
320   }
321
322   outbuf = NULL;
323   dest = NULL;
324   filled = 0;
325
326   /* while we have a complete DIF chunks left */
327   while (size >= 80) {
328     /* Allocate a new buffer, set the timestamp */
329     if (outbuf == NULL) {
330       outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
331       GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
332       dest = gst_rtp_buffer_get_payload (outbuf);
333       filled = 0;
334     }
335
336     /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
337     if (include_dif (rtpdvpay, data)) {
338       /* copy data in packet */
339       memcpy (dest, data, 80);
340
341       dest += 80;
342       filled += 80;
343     }
344
345     /* go to next dif chunk */
346     size -= 80;
347     data += 80;
348
349     /* push out the buffer if the next one would exceed the max packet size or
350      * when we are pushing the last packet */
351     if (filled + 80 > max_payload_size || size < 80) {
352       if (size < 160) {
353         guint hlen;
354
355         /* set marker */
356         gst_rtp_buffer_set_marker (outbuf, TRUE);
357
358         /* shrink buffer to last packet */
359         hlen = gst_rtp_buffer_get_header_len (outbuf);
360         gst_rtp_buffer_set_packet_len (outbuf, hlen + filled);
361       }
362       /* Push out the created piece, and check for errors. */
363       ret = gst_basertppayload_push (basepayload, outbuf);
364       if (ret != GST_FLOW_OK)
365         break;
366
367       outbuf = NULL;
368     }
369   }
370   gst_buffer_unref (buffer);
371
372   return ret;
373 }
374
375 gboolean
376 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
377 {
378   return gst_element_register (plugin, "rtpdvpay",
379       GST_RANK_NONE, GST_TYPE_RTP_DV_PAY);
380 }