2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <gst/video/video.h>
27 #include "gsty4mencode.h"
29 static GstElementDetails y4mencode_details = GST_ELEMENT_DETAILS ("Y4mEncode",
30 "Codec/Encoder/Video",
31 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
32 "Wim Taymans <wim.taymans@chello.be>");
35 /* Filter signals and args */
47 static GstStaticPadTemplate y4mencode_src_factory =
48 GST_STATIC_PAD_TEMPLATE ("src",
51 GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 1")
54 static GstStaticPadTemplate y4mencode_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
61 static void gst_y4mencode_base_init (gpointer g_class);
62 static void gst_y4mencode_class_init (GstY4mEncodeClass * klass);
63 static void gst_y4mencode_init (GstY4mEncode * filter);
65 static void gst_y4mencode_set_property (GObject * object,
66 guint prop_id, const GValue * value, GParamSpec * pspec);
67 static void gst_y4mencode_get_property (GObject * object,
68 guint prop_id, GValue * value, GParamSpec * pspec);
70 static void gst_y4mencode_chain (GstPad * pad, GstData * _data);
71 static GstElementStateReturn gst_y4mencode_change_state (GstElement * element);
74 static GstElementClass *parent_class = NULL;
76 /*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
79 gst_y4mencode_get_type (void)
81 static GType y4mencode_type = 0;
83 if (!y4mencode_type) {
84 static const GTypeInfo y4mencode_info = {
85 sizeof (GstY4mEncodeClass),
86 gst_y4mencode_base_init,
88 (GClassInitFunc) gst_y4mencode_class_init,
91 sizeof (GstY4mEncode),
93 (GInstanceInitFunc) gst_y4mencode_init,
96 y4mencode_type = g_type_register_static (GST_TYPE_ELEMENT,
97 "GstY4mEncode", &y4mencode_info, 0);
99 return y4mencode_type;
104 gst_y4mencode_base_init (gpointer g_class)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108 gst_element_class_add_pad_template (element_class,
109 gst_static_pad_template_get (&y4mencode_src_factory));
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&y4mencode_sink_factory));
112 gst_element_class_set_details (element_class, &y4mencode_details);
115 gst_y4mencode_class_init (GstY4mEncodeClass * klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
120 gobject_class = (GObjectClass *) klass;
121 gstelement_class = (GstElementClass *) klass;
123 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
125 gstelement_class->change_state = gst_y4mencode_change_state;
127 gobject_class->set_property = gst_y4mencode_set_property;
128 gobject_class->get_property = gst_y4mencode_get_property;
131 static GstPadLinkReturn
132 gst_y4mencode_sinkconnect (GstPad * pad, const GstCaps * caps)
134 GstY4mEncode *filter;
137 gdouble framerates[] = {
139 24000 / 1001, 24.000, /* 24fps movie */
141 30000 / 1001, 30.000, /* NTSC */
145 GstStructure *structure;
147 filter = GST_Y4MENCODE (gst_pad_get_parent (pad));
149 structure = gst_caps_get_structure (caps, 0);
151 if (!gst_structure_get_int (structure, "width", &filter->width) ||
152 !gst_structure_get_int (structure, "height", &filter->height) ||
153 !gst_structure_get_double (structure, "framerate", &fps))
154 return GST_PAD_LINK_REFUSED;
158 for (i = 1; i < 9; i++) {
159 gdouble old_diff = fabs (framerates[idx] - fps),
160 new_diff = fabs (framerates[i] - fps);
162 if (new_diff < old_diff) {
166 filter->fps_idx = idx;
168 return GST_PAD_LINK_OK;
172 gst_y4mencode_init (GstY4mEncode * filter)
175 gst_pad_new_from_template (gst_static_pad_template_get
176 (&y4mencode_sink_factory), "sink");
177 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
178 gst_pad_set_chain_function (filter->sinkpad, gst_y4mencode_chain);
179 gst_pad_set_link_function (filter->sinkpad, gst_y4mencode_sinkconnect);
182 gst_pad_new_from_template (gst_static_pad_template_get
183 (&y4mencode_src_factory), "src");
184 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
190 gst_y4mencode_chain (GstPad * pad, GstData * _data)
192 GstBuffer *buf = GST_BUFFER (_data);
193 GstY4mEncode *filter;
198 g_return_if_fail (pad != NULL);
199 g_return_if_fail (GST_IS_PAD (pad));
200 g_return_if_fail (buf != NULL);
202 filter = GST_Y4MENCODE (gst_pad_get_parent (pad));
203 g_return_if_fail (filter != NULL);
204 g_return_if_fail (GST_IS_Y4MENCODE (filter));
206 outbuf = gst_buffer_new ();
207 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (buf) + 256);
210 header = "YUV4MPEG W%d H%d I? %d\nFRAME\n";
211 filter->init = FALSE;
216 snprintf (GST_BUFFER_DATA (outbuf), 255, header,
217 filter->width, filter->height, filter->fps_idx);
218 len = strlen (GST_BUFFER_DATA (outbuf));
220 memcpy (GST_BUFFER_DATA (outbuf) + len, GST_BUFFER_DATA (buf),
221 GST_BUFFER_SIZE (buf));
222 GST_BUFFER_SIZE (outbuf) = GST_BUFFER_SIZE (buf) + len;
224 gst_buffer_unref (buf);
226 gst_pad_push (filter->srcpad, GST_DATA (outbuf));
230 gst_y4mencode_set_property (GObject * object, guint prop_id,
231 const GValue * value, GParamSpec * pspec)
233 GstY4mEncode *filter;
235 /* it's not null if we got it, but it might not be ours */
236 g_return_if_fail (GST_IS_Y4MENCODE (object));
237 filter = GST_Y4MENCODE (object);
246 gst_y4mencode_get_property (GObject * object, guint prop_id, GValue * value,
249 GstY4mEncode *filter;
251 /* it's not null if we got it, but it might not be ours */
252 g_return_if_fail (GST_IS_Y4MENCODE (object));
253 filter = GST_Y4MENCODE (object);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 static GstElementStateReturn
263 gst_y4mencode_change_state (GstElement * element)
265 GstY4mEncode *filter;
267 g_return_val_if_fail (GST_IS_Y4MENCODE (element), GST_STATE_FAILURE);
269 filter = GST_Y4MENCODE (element);
271 if (GST_STATE_TRANSITION (element) == GST_STATE_NULL_TO_READY) {
275 if (GST_ELEMENT_CLASS (parent_class)->change_state)
276 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
278 return GST_STATE_SUCCESS;
282 plugin_init (GstPlugin * plugin)
284 return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
288 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
291 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
292 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)