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-1.0 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 GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
80 GstStateChange transition);
83 gst_y4m_encode_handle_frame (GstVideoEncoder * encoder,
84 GstVideoCodecFrame * frame);
85 static gboolean gst_y4m_encode_set_format (GstVideoEncoder * encoder,
86 GstVideoCodecState * state);
88 #define gst_y4m_encode_parent_class parent_class
89 G_DEFINE_TYPE (GstY4mEncode, gst_y4m_encode, GST_TYPE_VIDEO_ENCODER);
93 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
95 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
96 GstVideoEncoderClass *venc_class = GST_VIDEO_ENCODER_CLASS (klass);
98 element_class->change_state = GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
100 gst_element_class_add_static_pad_template (element_class,
101 &y4mencode_src_factory);
102 gst_element_class_add_static_pad_template (element_class,
103 &y4mencode_sink_factory);
105 gst_element_class_set_static_metadata (element_class,
106 "YUV4MPEG video encoder", "Codec/Encoder/Video",
107 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
108 "Wim Taymans <wim.taymans@gmail.com>");
109 venc_class->set_format = gst_y4m_encode_set_format;
110 venc_class->handle_frame = gst_y4m_encode_handle_frame;
115 gst_y4m_encode_init (GstY4mEncode * filter)
117 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (filter));
119 /* init properties */
120 gst_y4m_encode_reset (filter);
124 gst_y4m_encode_reset (GstY4mEncode * filter)
126 filter->header = FALSE;
130 gst_y4m_encode_set_format (GstVideoEncoder * encoder,
131 GstVideoCodecState * state)
133 GstY4mEncode *y4menc;
135 GstVideoCodecState *output_state;
137 y4menc = GST_Y4M_ENCODE (encoder);
140 switch (GST_VIDEO_INFO_FORMAT (info)) {
141 case GST_VIDEO_FORMAT_I420:
142 y4menc->colorspace = "420";
144 case GST_VIDEO_FORMAT_Y42B:
145 y4menc->colorspace = "422";
147 case GST_VIDEO_FORMAT_Y41B:
148 y4menc->colorspace = "411";
150 case GST_VIDEO_FORMAT_Y444:
151 y4menc->colorspace = "444";
157 y4menc->info = *info;
160 gst_video_encoder_set_output_state (encoder,
161 gst_static_pad_template_get_caps (&y4mencode_src_factory), state);
162 gst_video_codec_state_unref (output_state);
168 GST_ERROR_OBJECT (y4menc, "Invalid format");
174 static inline GstBuffer *
175 gst_y4m_encode_get_stream_header (GstY4mEncode * filter, gboolean tff)
182 if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
191 header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
192 filter->colorspace, GST_VIDEO_INFO_WIDTH (&filter->info),
193 GST_VIDEO_INFO_HEIGHT (&filter->info), interlaced,
194 GST_VIDEO_INFO_FPS_N (&filter->info),
195 GST_VIDEO_INFO_FPS_D (&filter->info),
196 GST_VIDEO_INFO_PAR_N (&filter->info),
197 GST_VIDEO_INFO_PAR_D (&filter->info));
198 len = strlen (header);
200 buf = gst_buffer_new ();
201 gst_buffer_append_memory (buf,
202 gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
207 static inline GstBuffer *
208 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
214 header = g_strdup_printf ("FRAME\n");
215 len = strlen (header);
217 buf = gst_buffer_new ();
218 gst_buffer_append_memory (buf,
219 gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
226 gst_y4m_encode_handle_frame (GstVideoEncoder * encoder,
227 GstVideoCodecFrame * frame)
229 GstY4mEncode *filter = GST_Y4M_ENCODE (encoder);
230 GstClockTime timestamp;
232 /* check we got some decent info from caps */
233 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN)
236 timestamp = GST_BUFFER_TIMESTAMP (frame->input_buffer);
238 if (G_UNLIKELY (!filter->header)) {
239 gboolean tff = FALSE;
241 if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
243 GST_BUFFER_FLAG_IS_SET (frame->input_buffer,
244 GST_VIDEO_BUFFER_FLAG_TFF);
246 frame->output_buffer = gst_y4m_encode_get_stream_header (filter, tff);
247 filter->header = TRUE;
248 frame->output_buffer =
249 gst_buffer_append (frame->output_buffer,
250 gst_y4m_encode_get_frame_header (filter));
252 frame->output_buffer = gst_y4m_encode_get_frame_header (filter);
255 frame->output_buffer =
256 gst_buffer_append (frame->output_buffer,
257 gst_buffer_copy (frame->input_buffer));
260 frame->output_buffer = gst_buffer_make_writable (frame->output_buffer);
261 GST_BUFFER_TIMESTAMP (frame->output_buffer) = timestamp;
263 return gst_video_encoder_finish_frame (encoder, frame);
267 GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
268 ("format wasn't negotiated"));
270 return GST_FLOW_NOT_NEGOTIATED;
274 static GstStateChangeReturn
275 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
277 GstY4mEncode *filter = GST_Y4M_ENCODE (element);
278 GstStateChangeReturn ret;
280 switch (transition) {
281 case GST_STATE_CHANGE_NULL_TO_READY:
282 case GST_STATE_CHANGE_READY_TO_PAUSED:
288 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
289 (element, transition), GST_STATE_CHANGE_SUCCESS);
290 if (ret != GST_STATE_CHANGE_SUCCESS)
293 switch (transition) {
294 case GST_STATE_CHANGE_PAUSED_TO_READY:
295 gst_y4m_encode_reset (filter);
301 return GST_STATE_CHANGE_SUCCESS;
305 plugin_init (GstPlugin * plugin)
307 return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
308 GST_TYPE_Y4M_ENCODE);
311 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
314 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
315 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)