Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.git] / gst / y4m / gsty4mencode.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2006> Mark Nauwelaerts <mnauw@users.sourceforge.net>
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  * SECTION:element-y4menc
22  *
23  * <refsect2>
24  * <para>
25  * Creates a YU4MPEG2 raw video stream as defined by the mjpegtools project.
26  * </para>
27  * <title>Example launch line</title>
28  * <para>
29  * (write everything in one line, without the backslash characters)
30  * <programlisting>
31  * gst-launch-0.10 videotestsrc num-buffers=250 \
32  * ! 'video/x-raw-yuv,format=(fourcc)I420,width=320,height=240,framerate=(fraction)25/1' \
33  * ! y4menc ! filesink location=test.yuv
34  * </programlisting>
35  * </para>
36  * </refsect2>
37  *
38  */
39
40 /* see mjpegtools/yuv4mpeg.h for yuv4mpeg format */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45 #include <string.h>
46 #include <gst/gst.h>
47 #include <gst/video/video.h>
48 #include "gsty4mencode.h"
49
50 /* Filter signals and args */
51 enum
52 {
53   /* FILL ME */
54   LAST_SIGNAL
55 };
56
57 enum
58 {
59   ARG_0
60 };
61
62 static GstStaticPadTemplate y4mencode_src_factory =
63 GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 2")
67     );
68
69 static GstStaticPadTemplate y4mencode_sink_factory =
70 GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ IYUV, I420, Y42B, Y41B, Y444 }"))
74     );
75
76
77 static void gst_y4m_encode_set_property (GObject * object,
78     guint prop_id, const GValue * value, GParamSpec * pspec);
79 static void gst_y4m_encode_get_property (GObject * object,
80     guint prop_id, GValue * value, GParamSpec * pspec);
81
82 static void gst_y4m_encode_reset (GstY4mEncode * filter);
83
84 static gboolean gst_y4m_encode_sink_event (GstPad * pad, GstObject * parent,
85     GstEvent * event);
86 static GstFlowReturn gst_y4m_encode_chain (GstPad * pad, GstObject * parent,
87     GstBuffer * buf);
88 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
89     GstStateChange transition);
90
91 #define gst_y4m_encode_parent_class parent_class
92 G_DEFINE_TYPE (GstY4mEncode, gst_y4m_encode, GST_TYPE_ELEMENT);
93
94 static void
95 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
96 {
97   GObjectClass *gobject_class;
98   GstElementClass *gstelement_class;
99
100   gobject_class = (GObjectClass *) klass;
101   gstelement_class = (GstElementClass *) klass;
102
103   gobject_class->set_property = gst_y4m_encode_set_property;
104   gobject_class->get_property = gst_y4m_encode_get_property;
105
106   gstelement_class->change_state =
107       GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
108
109   gst_element_class_add_pad_template (gstelement_class,
110       gst_static_pad_template_get (&y4mencode_src_factory));
111   gst_element_class_add_pad_template (gstelement_class,
112       gst_static_pad_template_get (&y4mencode_sink_factory));
113
114   gst_element_class_set_static_metadata (gstelement_class,
115       "YUV4MPEG video encoder", "Codec/Encoder/Video",
116       "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
117       "Wim Taymans <wim.taymans@gmail.com>");
118 }
119
120 static void
121 gst_y4m_encode_init (GstY4mEncode * filter)
122 {
123   filter->sinkpad =
124       gst_pad_new_from_static_template (&y4mencode_sink_factory, "sink");
125   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
126   gst_pad_set_chain_function (filter->sinkpad,
127       GST_DEBUG_FUNCPTR (gst_y4m_encode_chain));
128   gst_pad_set_event_function (filter->sinkpad,
129       GST_DEBUG_FUNCPTR (gst_y4m_encode_sink_event));
130
131   filter->srcpad =
132       gst_pad_new_from_static_template (&y4mencode_src_factory, "src");
133   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
134   gst_pad_use_fixed_caps (filter->srcpad);
135
136   /* init properties */
137   gst_y4m_encode_reset (filter);
138 }
139
140 static void
141 gst_y4m_encode_reset (GstY4mEncode * filter)
142 {
143   filter->negotiated = FALSE;
144 }
145
146 static gboolean
147 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
148 {
149   gboolean ret;
150   GstY4mEncode *filter;
151   GstVideoInfo info;
152
153   filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
154
155   if (!gst_video_info_from_caps (&info, vscaps))
156     goto invalid_format;
157
158   switch (GST_VIDEO_INFO_FORMAT (&info)) {
159     case GST_VIDEO_FORMAT_I420:
160       filter->colorspace = "420";
161       break;
162     case GST_VIDEO_FORMAT_Y42B:
163       filter->colorspace = "422";
164       break;
165     case GST_VIDEO_FORMAT_Y41B:
166       filter->colorspace = "411";
167       break;
168     case GST_VIDEO_FORMAT_Y444:
169       filter->colorspace = "444";
170       break;
171     default:
172       goto invalid_format;
173   }
174
175   filter->info = info;
176
177   /* the template caps will do for the src pad, should always accept */
178   ret = gst_pad_set_caps (filter->srcpad,
179       gst_static_pad_template_get_caps (&y4mencode_src_factory));
180
181   filter->negotiated = ret;
182
183   return ret;
184
185   /* ERRORS */
186 invalid_format:
187   {
188     GST_ERROR_OBJECT (filter, "got invalid caps");
189     return FALSE;
190   }
191 }
192
193 static gboolean
194 gst_y4m_encode_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
195 {
196   gboolean res;
197
198   switch (GST_EVENT_TYPE (event)) {
199     case GST_EVENT_CAPS:
200     {
201       GstCaps *caps;
202
203       gst_event_parse_caps (event, &caps);
204       res = gst_y4m_encode_setcaps (pad, caps);
205       gst_event_unref (event);
206       break;
207     }
208     default:
209       res = gst_pad_event_default (pad, parent, event);
210       break;
211   }
212   return res;
213 }
214
215 static inline GstBuffer *
216 gst_y4m_encode_get_stream_header (GstY4mEncode * filter, gboolean tff)
217 {
218   gpointer header;
219   GstBuffer *buf;
220   gchar interlaced;
221   gsize len;
222
223   if (filter->info.flags & GST_VIDEO_FLAG_INTERLACED) {
224     if (tff)
225       interlaced = 't';
226     else
227       interlaced = 'b';
228   } else {
229     interlaced = 'p';
230   }
231
232   header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
233       filter->colorspace, GST_VIDEO_INFO_WIDTH (&filter->info),
234       GST_VIDEO_INFO_HEIGHT (&filter->info), interlaced,
235       GST_VIDEO_INFO_FPS_N (&filter->info),
236       GST_VIDEO_INFO_FPS_D (&filter->info),
237       GST_VIDEO_INFO_PAR_N (&filter->info),
238       GST_VIDEO_INFO_PAR_D (&filter->info));
239   len = strlen (header);
240
241   buf = gst_buffer_new ();
242   gst_buffer_append_memory (buf,
243       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
244
245   return buf;
246 }
247
248 static inline GstBuffer *
249 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
250 {
251   gpointer header;
252   GstBuffer *buf;
253   gsize len;
254
255   header = g_strdup_printf ("FRAME\n");
256   len = strlen (header);
257
258   buf = gst_buffer_new ();
259   gst_buffer_append_memory (buf,
260       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
261
262   return buf;
263 }
264
265 static GstFlowReturn
266 gst_y4m_encode_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
267 {
268   GstY4mEncode *filter = GST_Y4M_ENCODE (parent);
269   GstBuffer *outbuf;
270   GstClockTime timestamp;
271
272   /* check we got some decent info from caps */
273   if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN)
274     goto not_negotiated;
275
276   timestamp = GST_BUFFER_TIMESTAMP (buf);
277
278   if (G_UNLIKELY (!filter->header)) {
279     gboolean tff;
280
281     if (filter->info.flags & GST_VIDEO_FLAG_INTERLACED) {
282       tff = GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF);
283     }
284     outbuf = gst_y4m_encode_get_stream_header (filter, tff);
285     filter->header = TRUE;
286     outbuf =
287         gst_buffer_append (outbuf, gst_y4m_encode_get_frame_header (filter));
288   } else {
289     outbuf = gst_y4m_encode_get_frame_header (filter);
290   }
291   /* join with data, FIXME, strides are all wrong etc */
292   outbuf = gst_buffer_append (outbuf, buf);
293   /* decorate */
294   outbuf = gst_buffer_make_writable (outbuf);
295
296   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
297
298   return gst_pad_push (filter->srcpad, outbuf);
299
300   /* ERRORS */
301 not_negotiated:
302   {
303     GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
304         ("format wasn't negotiated before chain function"));
305     gst_buffer_unref (buf);
306     return GST_FLOW_NOT_NEGOTIATED;
307   }
308 }
309
310 static void
311 gst_y4m_encode_set_property (GObject * object, guint prop_id,
312     const GValue * value, GParamSpec * pspec)
313 {
314   GstY4mEncode G_GNUC_UNUSED *filter;
315
316   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
317   filter = GST_Y4M_ENCODE (object);
318
319   switch (prop_id) {
320     default:
321       break;
322   }
323 }
324
325 static void
326 gst_y4m_encode_get_property (GObject * object, guint prop_id, GValue * value,
327     GParamSpec * pspec)
328 {
329   GstY4mEncode G_GNUC_UNUSED *filter;
330
331   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
332   filter = GST_Y4M_ENCODE (object);
333
334   switch (prop_id) {
335     default:
336       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337       break;
338   }
339 }
340
341 static GstStateChangeReturn
342 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
343 {
344   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
345   GstStateChangeReturn ret;
346
347   switch (transition) {
348     case GST_STATE_CHANGE_NULL_TO_READY:
349     case GST_STATE_CHANGE_READY_TO_PAUSED:
350       break;
351     default:
352       break;
353   }
354
355   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
356       (element, transition), GST_STATE_CHANGE_SUCCESS);
357   if (ret != GST_STATE_CHANGE_SUCCESS)
358     return ret;
359
360   switch (transition) {
361     case GST_STATE_CHANGE_PAUSED_TO_READY:
362       gst_y4m_encode_reset (filter);
363       break;
364     default:
365       break;
366   }
367
368   return GST_STATE_CHANGE_SUCCESS;
369 }
370
371 static gboolean
372 plugin_init (GstPlugin * plugin)
373 {
374   return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
375       GST_TYPE_Y4M_ENCODE);
376 }
377
378 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
379     GST_VERSION_MINOR,
380     y4menc,
381     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
382     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)