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,
95 y4mencode_type = g_type_register_static (GST_TYPE_ELEMENT,
96 "GstY4mEncode", &y4mencode_info, 0);
98 return y4mencode_type;
103 gst_y4mencode_base_init (gpointer g_class)
105 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
107 gst_element_class_add_pad_template (element_class,
108 gst_static_pad_template_get (&y4mencode_src_factory));
109 gst_element_class_add_pad_template (element_class,
110 gst_static_pad_template_get (&y4mencode_sink_factory));
111 gst_element_class_set_details (element_class, &y4mencode_details);
114 gst_y4mencode_class_init (GstY4mEncodeClass * klass)
116 GObjectClass *gobject_class;
117 GstElementClass *gstelement_class;
119 gobject_class = (GObjectClass *) klass;
120 gstelement_class = (GstElementClass *) klass;
122 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
124 gstelement_class->change_state = gst_y4mencode_change_state;
126 gobject_class->set_property = gst_y4mencode_set_property;
127 gobject_class->get_property = gst_y4mencode_get_property;
130 static GstPadLinkReturn
131 gst_y4mencode_sinkconnect (GstPad * pad, const GstCaps * caps)
133 GstY4mEncode *filter;
136 gdouble framerates[] = {
138 23.976, 24.000, /* 24fps movie */
140 29.970, 30.000, /* NTSC */
144 GstStructure *structure;
146 filter = GST_Y4MENCODE (gst_pad_get_parent (pad));
148 structure = gst_caps_get_structure (caps, 0);
150 gst_structure_get_int (structure, "width", &filter->width);
151 gst_structure_get_int (structure, "height", &filter->height);
152 gst_structure_get_double (structure, "framerate", &fps);
155 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) {
167 filter->fps_idx = idx;
169 return GST_PAD_LINK_OK;
173 gst_y4mencode_init (GstY4mEncode * filter)
176 gst_pad_new_from_template (gst_static_pad_template_get
177 (&y4mencode_sink_factory), "sink");
178 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
179 gst_pad_set_chain_function (filter->sinkpad, gst_y4mencode_chain);
180 gst_pad_set_link_function (filter->sinkpad, gst_y4mencode_sinkconnect);
183 gst_pad_new_from_template (gst_static_pad_template_get
184 (&y4mencode_src_factory), "src");
185 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
191 gst_y4mencode_chain (GstPad * pad, GstData * _data)
193 GstBuffer *buf = GST_BUFFER (_data);
194 GstY4mEncode *filter;
199 g_return_if_fail (pad != NULL);
200 g_return_if_fail (GST_IS_PAD (pad));
201 g_return_if_fail (buf != NULL);
203 filter = GST_Y4MENCODE (gst_pad_get_parent (pad));
204 g_return_if_fail (filter != NULL);
205 g_return_if_fail (GST_IS_Y4MENCODE (filter));
207 outbuf = gst_buffer_new ();
208 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (buf) + 256);
211 header = "YUV4MPEG %d %d %d\nFRAME\n";
212 filter->init = FALSE;
217 snprintf (GST_BUFFER_DATA (outbuf), 255, header,
218 filter->width, filter->height, filter->fps_idx);
219 len = strlen (GST_BUFFER_DATA (outbuf));
221 memcpy (GST_BUFFER_DATA (outbuf) + len, GST_BUFFER_DATA (buf),
222 GST_BUFFER_SIZE (buf));
223 GST_BUFFER_SIZE (outbuf) = GST_BUFFER_SIZE (buf) + len;
225 gst_buffer_unref (buf);
227 gst_pad_push (filter->srcpad, GST_DATA (outbuf));
231 gst_y4mencode_set_property (GObject * object, guint prop_id,
232 const GValue * value, GParamSpec * pspec)
234 GstY4mEncode *filter;
236 /* it's not null if we got it, but it might not be ours */
237 g_return_if_fail (GST_IS_Y4MENCODE (object));
238 filter = GST_Y4MENCODE (object);
247 gst_y4mencode_get_property (GObject * object, guint prop_id, GValue * value,
250 GstY4mEncode *filter;
252 /* it's not null if we got it, but it might not be ours */
253 g_return_if_fail (GST_IS_Y4MENCODE (object));
254 filter = GST_Y4MENCODE (object);
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 static GstElementStateReturn
264 gst_y4mencode_change_state (GstElement * element)
266 GstY4mEncode *filter;
268 g_return_val_if_fail (GST_IS_Y4MENCODE (element), GST_STATE_FAILURE);
270 filter = GST_Y4MENCODE (element);
272 if (GST_STATE_TRANSITION (element) == GST_STATE_NULL_TO_READY) {
276 if (GST_ELEMENT_CLASS (parent_class)->change_state)
277 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
279 return GST_STATE_SUCCESS;
283 plugin_init (GstPlugin * plugin)
285 return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
289 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
292 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
293 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)