tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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_YUV ("{ 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_setcaps (GstPad * pad, GstCaps * vscaps);
85 static GstFlowReturn gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf);
86 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
87     GstStateChange transition);
88
89 GST_BOILERPLATE (GstY4mEncode, gst_y4m_encode, GstElement, GST_TYPE_ELEMENT);
90
91
92 static void
93 gst_y4m_encode_base_init (gpointer g_class)
94 {
95   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
96
97   gst_element_class_add_static_pad_template (element_class,
98       &y4mencode_src_factory);
99   gst_element_class_add_static_pad_template (element_class,
100       &y4mencode_sink_factory);
101   gst_element_class_set_details_simple (element_class, "YUV4MPEG video encoder",
102       "Codec/Encoder/Video",
103       "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
104       "Wim Taymans <wim.taymans@gmail.com>");
105 }
106
107 static void
108 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
109 {
110   GObjectClass *gobject_class;
111   GstElementClass *gstelement_class;
112
113   gobject_class = (GObjectClass *) klass;
114   gstelement_class = (GstElementClass *) klass;
115
116   gstelement_class->change_state =
117       GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
118
119   gobject_class->set_property = gst_y4m_encode_set_property;
120   gobject_class->get_property = gst_y4m_encode_get_property;
121 }
122
123 static void
124 gst_y4m_encode_init (GstY4mEncode * filter, GstY4mEncodeClass * klass)
125 {
126   filter->sinkpad =
127       gst_pad_new_from_static_template (&y4mencode_sink_factory, "sink");
128   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
129   gst_pad_set_chain_function (filter->sinkpad,
130       GST_DEBUG_FUNCPTR (gst_y4m_encode_chain));
131   gst_pad_set_setcaps_function (filter->sinkpad,
132       GST_DEBUG_FUNCPTR (gst_y4m_encode_setcaps));
133
134   filter->srcpad =
135       gst_pad_new_from_static_template (&y4mencode_src_factory, "src");
136   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
137   gst_pad_use_fixed_caps (filter->srcpad);
138
139   /* init properties */
140   gst_y4m_encode_reset (filter);
141 }
142
143 static void
144 gst_y4m_encode_reset (GstY4mEncode * filter)
145 {
146   filter->width = filter->height = -1;
147   filter->fps_num = filter->fps_den = 1;
148   filter->par_num = filter->par_den = 1;
149   filter->colorspace = "unknown";
150 }
151
152 static gboolean
153 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
154 {
155   GstY4mEncode *filter;
156   GstStructure *structure;
157   gboolean res;
158   gint w, h;
159   guint32 fourcc;
160   const GValue *fps, *par, *interlaced;
161
162   filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
163
164   structure = gst_caps_get_structure (vscaps, 0);
165
166   res = gst_structure_get_int (structure, "width", &w);
167   res &= gst_structure_get_int (structure, "height", &h);
168   res &= ((fps = gst_structure_get_value (structure, "framerate")) != NULL);
169   res &= gst_structure_get_fourcc (structure, "format", &fourcc);
170
171   switch (fourcc) {             /* Translate fourcc to Y4M colorspace code */
172     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
173     case GST_MAKE_FOURCC ('I', 'Y', 'U', 'V'):
174       filter->colorspace = "420";
175       break;
176     case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
177       filter->colorspace = "422";
178       break;
179     case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
180       filter->colorspace = "411";
181       break;
182     case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
183       filter->colorspace = "444";
184       break;
185     default:
186       res = FALSE;
187       break;
188   }
189
190   if (!res || w <= 0 || h <= 0 || !GST_VALUE_HOLDS_FRACTION (fps))
191     return FALSE;
192
193   /* optional interlaced info */
194   interlaced = gst_structure_get_value (structure, "interlaced");
195
196   /* optional par info */
197   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
198
199   filter->width = w;
200   filter->height = h;
201   filter->fps_num = gst_value_get_fraction_numerator (fps);
202   filter->fps_den = gst_value_get_fraction_denominator (fps);
203   if ((par != NULL) && GST_VALUE_HOLDS_FRACTION (par)) {
204     filter->par_num = gst_value_get_fraction_numerator (par);
205     filter->par_den = gst_value_get_fraction_denominator (par);
206   } else {                      /* indicates unknown */
207     filter->par_num = 0;
208     filter->par_den = 0;
209   }
210   if ((interlaced != NULL) && G_VALUE_HOLDS (interlaced, G_TYPE_BOOLEAN)) {
211     filter->interlaced = g_value_get_boolean (interlaced);
212   } else {
213     /* assume progressive if no interlaced property in caps */
214     filter->interlaced = FALSE;
215   }
216   /* the template caps will do for the src pad, should always accept */
217   return gst_pad_set_caps (filter->srcpad,
218       gst_static_pad_template_get_caps (&y4mencode_src_factory));
219 }
220
221 static inline GstBuffer *
222 gst_y4m_encode_get_stream_header (GstY4mEncode * filter)
223 {
224   gpointer header;
225   GstBuffer *buf;
226   gchar interlaced;
227
228   interlaced = 'p';
229
230   if (filter->interlaced && filter->top_field_first)
231     interlaced = 't';
232   else if (filter->interlaced)
233     interlaced = 'b';
234
235   header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
236       filter->colorspace, filter->width, filter->height, interlaced,
237       filter->fps_num, filter->fps_den, filter->par_num, filter->par_den);
238
239   buf = gst_buffer_new ();
240   gst_buffer_set_data (buf, header, strlen (header));
241   /* so it gets free'd when needed */
242   GST_BUFFER_MALLOCDATA (buf) = header;
243
244   return buf;
245 }
246
247 static inline GstBuffer *
248 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
249 {
250   gpointer header;
251   GstBuffer *buf;
252
253   header = g_strdup_printf ("FRAME\n");
254
255   buf = gst_buffer_new ();
256   gst_buffer_set_data (buf, header, strlen (header));
257   /* so it gets free'd when needed */
258   GST_BUFFER_MALLOCDATA (buf) = header;
259
260   return buf;
261 }
262
263 static GstFlowReturn
264 gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf)
265 {
266   GstY4mEncode *filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
267   GstBuffer *outbuf;
268   GstClockTime timestamp;
269
270   /* check we got some decent info from caps */
271   if (filter->width < 0) {
272     GST_ELEMENT_ERROR ("filter", CORE, NEGOTIATION, (NULL),
273         ("format wasn't negotiated before chain function"));
274     gst_buffer_unref (buf);
275     return GST_FLOW_NOT_NEGOTIATED;
276   }
277
278   timestamp = GST_BUFFER_TIMESTAMP (buf);
279
280   if (G_UNLIKELY (!filter->header)) {
281     if (filter->interlaced == TRUE) {
282       if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_TFF)) {
283         filter->top_field_first = TRUE;
284       } else {
285         filter->top_field_first = FALSE;
286       }
287     }
288     outbuf = gst_y4m_encode_get_stream_header (filter);
289     filter->header = TRUE;
290     outbuf = gst_buffer_join (outbuf, gst_y4m_encode_get_frame_header (filter));
291   } else {
292     outbuf = gst_y4m_encode_get_frame_header (filter);
293   }
294   /* join with data */
295   outbuf = gst_buffer_join (outbuf, buf);
296   /* decorate */
297   gst_buffer_make_metadata_writable (outbuf);
298   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
299
300   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
301
302   return gst_pad_push (filter->srcpad, outbuf);
303 }
304
305 static void
306 gst_y4m_encode_set_property (GObject * object, guint prop_id,
307     const GValue * value, GParamSpec * pspec)
308 {
309   GstY4mEncode G_GNUC_UNUSED *filter;
310
311   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
312   filter = GST_Y4M_ENCODE (object);
313
314   switch (prop_id) {
315     default:
316       break;
317   }
318 }
319
320 static void
321 gst_y4m_encode_get_property (GObject * object, guint prop_id, GValue * value,
322     GParamSpec * pspec)
323 {
324   GstY4mEncode G_GNUC_UNUSED *filter;
325
326   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
327   filter = GST_Y4M_ENCODE (object);
328
329   switch (prop_id) {
330     default:
331       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
332       break;
333   }
334 }
335
336 static GstStateChangeReturn
337 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
338 {
339   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
340   GstStateChangeReturn ret;
341
342   switch (transition) {
343     case GST_STATE_CHANGE_NULL_TO_READY:
344     case GST_STATE_CHANGE_READY_TO_PAUSED:
345       break;
346     default:
347       break;
348   }
349
350   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
351       (element, transition), GST_STATE_CHANGE_SUCCESS);
352   if (ret != GST_STATE_CHANGE_SUCCESS)
353     return ret;
354
355   switch (transition) {
356     case GST_STATE_CHANGE_PAUSED_TO_READY:
357       gst_y4m_encode_reset (filter);
358       break;
359     default:
360       break;
361   }
362
363   return GST_STATE_CHANGE_SUCCESS;
364 }
365
366 static gboolean
367 plugin_init (GstPlugin * plugin)
368 {
369   return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
370       GST_TYPE_Y4M_ENCODE);
371 }
372
373 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
374     GST_VERSION_MINOR,
375     "y4menc",
376     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
377     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)