2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2006> Mark Nauwelaerts <mnauw@users.sourceforge.net>
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.
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.
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.
21 * SECTION:element-y4menc
25 * Creates a YU4MPEG2 raw video stream as defined by the mjpegtools project.
27 * <title>Example launch line</title>
29 * (write everything in one line, without the backslash characters)
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
40 /* see mjpegtools/yuv4mpeg.h for yuv4mpeg format */
47 #include <gst/video/video.h>
48 #include "gsty4mencode.h"
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@chello.be>");
57 /* Filter signals and args */
69 static GstStaticPadTemplate y4mencode_src_factory =
70 GST_STATIC_PAD_TEMPLATE ("src",
73 GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 2")
76 static GstStaticPadTemplate y4mencode_sink_factory =
77 GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ IYUV, I420 }"))
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);
89 static void gst_y4m_encode_reset (GstY4mEncode * filter);
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);
96 GST_BOILERPLATE (GstY4mEncode, gst_y4m_encode, GstElement, GST_TYPE_ELEMENT);
100 gst_y4m_encode_base_init (gpointer g_class)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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);
112 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
114 GObjectClass *gobject_class;
115 GstElementClass *gstelement_class;
117 gobject_class = (GObjectClass *) klass;
118 gstelement_class = (GstElementClass *) klass;
120 gstelement_class->change_state =
121 GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
123 gobject_class->set_property = gst_y4m_encode_set_property;
124 gobject_class->get_property = gst_y4m_encode_get_property;
128 gst_y4m_encode_init (GstY4mEncode * filter, GstY4mEncodeClass * klass)
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));
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);
143 /* init properties */
144 gst_y4m_encode_reset (filter);
148 gst_y4m_encode_reset (GstY4mEncode * filter)
150 filter->width = filter->height = -1;
151 filter->fps_num = filter->fps_den = 1;
152 filter->par_num = filter->par_den = 1;
156 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
158 GstY4mEncode *filter;
159 GstStructure *structure;
162 const GValue *fps, *par;
164 filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
166 structure = gst_caps_get_structure (vscaps, 0);
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);
172 if (!res || w <= 0 || h <= 0 || !GST_VALUE_HOLDS_FRACTION (fps))
175 /* optional par info */
176 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
180 filter->fps_num = gst_value_get_fraction_numerator (fps);
181 filter->fps_den = gst_value_get_fraction_denominator (fps);
182 if ((par != NULL) && GST_VALUE_HOLDS_FRACTION (par)) {
183 filter->par_num = gst_value_get_fraction_numerator (par);
184 filter->par_den = gst_value_get_fraction_denominator (par);
185 } else { /* indicates unknown */
190 /* the template caps will do for the src pad, should always accept */
191 return gst_pad_set_caps (filter->srcpad,
192 gst_static_pad_template_get_caps (&y4mencode_src_factory));
195 static inline GstBuffer *
196 gst_y4m_encode_get_stream_header (GstY4mEncode * filter)
201 header = g_strdup_printf ("YUV4MPEG2 W%d H%d I? F%d:%d A%d:%d\n",
202 filter->width, filter->height,
203 filter->fps_num, filter->fps_den, filter->par_num, filter->par_den);
205 buf = gst_buffer_new ();
206 gst_buffer_set_data (buf, header, strlen (header));
207 /* so it gets free'd when needed */
208 GST_BUFFER_MALLOCDATA (buf) = header;
213 static inline GstBuffer *
214 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
219 header = g_strdup_printf ("FRAME\n");
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;
230 gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf)
232 GstY4mEncode *filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
234 GstClockTime timestamp;
236 /* check we got some decent info from caps */
237 if (filter->width < 0) {
238 GST_ELEMENT_ERROR ("filter", CORE, NEGOTIATION, (NULL),
239 ("format wasn't negotiated before chain function"));
240 gst_buffer_unref (buf);
241 return GST_FLOW_NOT_NEGOTIATED;
244 timestamp = GST_BUFFER_TIMESTAMP (buf);
246 if (G_UNLIKELY (!filter->header)) {
247 outbuf = gst_y4m_encode_get_stream_header (filter);
248 filter->header = TRUE;
249 outbuf = gst_buffer_join (outbuf, gst_y4m_encode_get_frame_header (filter));
251 outbuf = gst_y4m_encode_get_frame_header (filter);
254 outbuf = gst_buffer_join (outbuf, buf);
256 gst_buffer_make_metadata_writable (outbuf);
257 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
259 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
261 return gst_pad_push (filter->srcpad, outbuf);
265 gst_y4m_encode_set_property (GObject * object, guint prop_id,
266 const GValue * value, GParamSpec * pspec)
268 GstY4mEncode *filter;
270 g_return_if_fail (GST_IS_Y4M_ENCODE (object));
271 filter = GST_Y4M_ENCODE (object);
280 gst_y4m_encode_get_property (GObject * object, guint prop_id, GValue * value,
283 GstY4mEncode *filter;
285 g_return_if_fail (GST_IS_Y4M_ENCODE (object));
286 filter = GST_Y4M_ENCODE (object);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 static GstStateChangeReturn
296 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
298 GstY4mEncode *filter = GST_Y4M_ENCODE (element);
299 GstStateChangeReturn ret;
301 switch (transition) {
302 case GST_STATE_CHANGE_NULL_TO_READY:
303 case GST_STATE_CHANGE_READY_TO_PAUSED:
309 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
310 (element, transition), GST_STATE_CHANGE_SUCCESS);
311 if (ret != GST_STATE_CHANGE_SUCCESS)
314 switch (transition) {
315 case GST_STATE_CHANGE_PAUSED_TO_READY:
316 gst_y4m_encode_reset (filter);
322 return GST_STATE_CHANGE_SUCCESS;
326 plugin_init (GstPlugin * plugin)
328 return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
329 GST_TYPE_Y4M_ENCODE);
332 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
335 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
336 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)