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