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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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,format=(string)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 /* Filter signals and args */
62 static GstStaticPadTemplate y4mencode_src_factory =
63 GST_STATIC_PAD_TEMPLATE ("src",
66 GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 2")
69 static GstStaticPadTemplate y4mencode_sink_factory =
70 GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ IYUV, I420, Y42B, Y41B, Y444 }"))
77 static void gst_y4m_encode_reset (GstY4mEncode * filter);
79 static gboolean gst_y4m_encode_sink_event (GstPad * pad, GstObject * parent,
81 static GstFlowReturn gst_y4m_encode_chain (GstPad * pad, GstObject * parent,
83 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
84 GstStateChange transition);
86 #define gst_y4m_encode_parent_class parent_class
87 G_DEFINE_TYPE (GstY4mEncode, gst_y4m_encode, GST_TYPE_ELEMENT);
90 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
92 GstElementClass *gstelement_class;
94 gstelement_class = (GstElementClass *) klass;
96 gstelement_class->change_state =
97 GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
99 gst_element_class_add_pad_template (gstelement_class,
100 gst_static_pad_template_get (&y4mencode_src_factory));
101 gst_element_class_add_pad_template (gstelement_class,
102 gst_static_pad_template_get (&y4mencode_sink_factory));
104 gst_element_class_set_static_metadata (gstelement_class,
105 "YUV4MPEG video encoder", "Codec/Encoder/Video",
106 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
107 "Wim Taymans <wim.taymans@gmail.com>");
111 gst_y4m_encode_init (GstY4mEncode * filter)
114 gst_pad_new_from_static_template (&y4mencode_sink_factory, "sink");
115 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
116 gst_pad_set_chain_function (filter->sinkpad,
117 GST_DEBUG_FUNCPTR (gst_y4m_encode_chain));
118 gst_pad_set_event_function (filter->sinkpad,
119 GST_DEBUG_FUNCPTR (gst_y4m_encode_sink_event));
122 gst_pad_new_from_static_template (&y4mencode_src_factory, "src");
123 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
124 gst_pad_use_fixed_caps (filter->srcpad);
126 /* init properties */
127 gst_y4m_encode_reset (filter);
131 gst_y4m_encode_reset (GstY4mEncode * filter)
133 filter->negotiated = FALSE;
137 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
140 GstY4mEncode *filter;
143 filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
145 if (!gst_video_info_from_caps (&info, vscaps))
148 switch (GST_VIDEO_INFO_FORMAT (&info)) {
149 case GST_VIDEO_FORMAT_I420:
150 filter->colorspace = "420";
152 case GST_VIDEO_FORMAT_Y42B:
153 filter->colorspace = "422";
155 case GST_VIDEO_FORMAT_Y41B:
156 filter->colorspace = "411";
158 case GST_VIDEO_FORMAT_Y444:
159 filter->colorspace = "444";
167 /* the template caps will do for the src pad, should always accept */
168 ret = gst_pad_set_caps (filter->srcpad,
169 gst_static_pad_template_get_caps (&y4mencode_src_factory));
171 filter->negotiated = ret;
178 GST_ERROR_OBJECT (filter, "got invalid caps");
184 gst_y4m_encode_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
188 switch (GST_EVENT_TYPE (event)) {
193 gst_event_parse_caps (event, &caps);
194 res = gst_y4m_encode_setcaps (pad, caps);
195 gst_event_unref (event);
199 res = gst_pad_event_default (pad, parent, event);
205 static inline GstBuffer *
206 gst_y4m_encode_get_stream_header (GstY4mEncode * filter, gboolean tff)
213 if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
222 header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
223 filter->colorspace, GST_VIDEO_INFO_WIDTH (&filter->info),
224 GST_VIDEO_INFO_HEIGHT (&filter->info), interlaced,
225 GST_VIDEO_INFO_FPS_N (&filter->info),
226 GST_VIDEO_INFO_FPS_D (&filter->info),
227 GST_VIDEO_INFO_PAR_N (&filter->info),
228 GST_VIDEO_INFO_PAR_D (&filter->info));
229 len = strlen (header);
231 buf = gst_buffer_new ();
232 gst_buffer_append_memory (buf,
233 gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
238 static inline GstBuffer *
239 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
245 header = g_strdup_printf ("FRAME\n");
246 len = strlen (header);
248 buf = gst_buffer_new ();
249 gst_buffer_append_memory (buf,
250 gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
256 gst_y4m_encode_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
258 GstY4mEncode *filter = GST_Y4M_ENCODE (parent);
260 GstClockTime timestamp;
262 /* check we got some decent info from caps */
263 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN)
266 timestamp = GST_BUFFER_TIMESTAMP (buf);
268 if (G_UNLIKELY (!filter->header)) {
269 gboolean tff = FALSE;
271 if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
272 tff = GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF);
274 outbuf = gst_y4m_encode_get_stream_header (filter, tff);
275 filter->header = TRUE;
277 gst_buffer_append (outbuf, gst_y4m_encode_get_frame_header (filter));
279 outbuf = gst_y4m_encode_get_frame_header (filter);
281 /* join with data, FIXME, strides are all wrong etc */
282 outbuf = gst_buffer_append (outbuf, buf);
284 outbuf = gst_buffer_make_writable (outbuf);
286 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
288 return gst_pad_push (filter->srcpad, outbuf);
293 GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
294 ("format wasn't negotiated before chain function"));
295 gst_buffer_unref (buf);
296 return GST_FLOW_NOT_NEGOTIATED;
300 static GstStateChangeReturn
301 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
303 GstY4mEncode *filter = GST_Y4M_ENCODE (element);
304 GstStateChangeReturn ret;
306 switch (transition) {
307 case GST_STATE_CHANGE_NULL_TO_READY:
308 case GST_STATE_CHANGE_READY_TO_PAUSED:
314 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
315 (element, transition), GST_STATE_CHANGE_SUCCESS);
316 if (ret != GST_STATE_CHANGE_SUCCESS)
319 switch (transition) {
320 case GST_STATE_CHANGE_PAUSED_TO_READY:
321 gst_y4m_encode_reset (filter);
327 return GST_STATE_CHANGE_SUCCESS;
331 plugin_init (GstPlugin * plugin)
333 return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
334 GST_TYPE_Y4M_ENCODE);
337 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
340 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
341 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)