[MOVED FROM BAD] Changed to the new props API
[platform/upstream/gstreamer.git] / gst / y4m / gsty4mencode.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
21 #include <gst/gst.h>
22 #include "gsty4mencode.h"
23
24 static GstElementDetails lavencode_details = {
25   "LavEncode",
26   "Filter/LAV/Encoder",
27   "Encodes a YUV frame into the lav format (mjpeg_tools)",
28   VERSION,
29   "Wim Taymans <wim.taymans@chello.be>",
30   "(C) 2001",
31 };
32
33
34 /* Filter signals and args */
35 enum {
36   /* FILL ME */
37   LAST_SIGNAL
38 };
39
40 enum {
41   ARG_0
42 };
43
44 GST_PADTEMPLATE_FACTORY (lavencode_src_factory,
45   "src",
46   GST_PAD_SRC,
47   GST_PAD_ALWAYS,
48   GST_CAPS_NEW (
49     "test_src",
50     "application/x-lav",
51     NULL
52   )
53 )
54
55 GST_PADTEMPLATE_FACTORY (lavencode_sink_factory,
56   "sink",
57   GST_PAD_SINK,
58   GST_PAD_ALWAYS,
59   GST_CAPS_NEW (
60     "test_src",
61     "video/raw",
62       "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")),
63       "width", GST_PROPS_INT_RANGE (0, G_MAXINT),
64       "height", GST_PROPS_INT_RANGE (0, G_MAXINT)
65   )
66 )
67
68 static void                     gst_lavencode_class_init        (GstLavEncodeClass *klass);
69 static void                     gst_lavencode_init              (GstLavEncode *filter);
70
71 static void                     gst_lavencode_set_property              (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
72 static void                     gst_lavencode_get_property              (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
73
74 static void                     gst_lavencode_chain             (GstPad *pad, GstBuffer *buf);
75 static GstElementStateReturn    gst_lavencode_change_state      (GstElement *element);
76
77
78 static GstElementClass *parent_class = NULL;
79 /*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
80
81 GType
82 gst_lavencode_get_type(void) {
83   static GType lavencode_type = 0;
84
85   if (!lavencode_type) {
86     static const GTypeInfo lavencode_info = {
87       sizeof(GstLavEncodeClass),      NULL,
88       NULL,
89       (GClassInitFunc)gst_lavencode_class_init,
90       NULL,
91       NULL,
92       sizeof(GstLavEncode),
93       0,
94       (GInstanceInitFunc)gst_lavencode_init,
95     };
96     lavencode_type = g_type_register_static(GST_TYPE_ELEMENT, "GstLavEncode", &lavencode_info, 0);
97   }
98   return lavencode_type;
99 }
100
101 static void
102 gst_lavencode_class_init (GstLavEncodeClass *klass)
103 {
104   GObjectClass *gobject_class;
105   GstElementClass *gstelement_class;
106
107   gobject_class = (GObjectClass*)klass;
108   gstelement_class = (GstElementClass*)klass;
109
110   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
111
112   gstelement_class->change_state = gst_lavencode_change_state;
113
114   gobject_class->set_property = gst_lavencode_set_property;
115   gobject_class->get_property = gst_lavencode_get_property;
116 }
117
118 static GstPadConnectReturn
119 gst_lavencode_sinkconnect (GstPad *pad, GstCaps *caps)
120 {
121   GstLavEncode *filter;
122
123   filter = GST_LAVENCODE (gst_pad_get_parent (pad));
124
125   if (!GST_CAPS_IS_FIXED (caps))
126     return GST_PAD_CONNECT_DELAYED;
127
128   gst_caps_get_int (caps, "width", &filter->width);
129   gst_caps_get_int (caps, "height", &filter->height);
130
131   return GST_PAD_CONNECT_OK;
132 }
133
134 static void
135 gst_lavencode_init (GstLavEncode *filter)
136 {
137   filter->sinkpad = gst_pad_new_from_template(
138                   GST_PADTEMPLATE_GET (lavencode_sink_factory), "sink");
139   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
140   gst_pad_set_chain_function (filter->sinkpad, gst_lavencode_chain);
141   gst_pad_set_connect_function (filter->sinkpad, gst_lavencode_sinkconnect);
142
143   filter->srcpad = gst_pad_new_from_template(
144                   GST_PADTEMPLATE_GET (lavencode_src_factory), "src");
145   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
146
147   filter->init = TRUE;
148 }
149
150 static void
151 gst_lavencode_chain (GstPad *pad,GstBuffer *buf)
152 {
153   GstLavEncode *filter;
154   GstBuffer* outbuf;
155   gchar *header;
156   gint len;
157
158   g_return_if_fail(pad != NULL);
159   g_return_if_fail(GST_IS_PAD(pad));
160   g_return_if_fail(buf != NULL);
161
162   filter = GST_LAVENCODE (gst_pad_get_parent (pad));
163   g_return_if_fail(filter != NULL);
164   g_return_if_fail(GST_IS_LAVENCODE(filter));
165
166   outbuf = gst_buffer_new ();
167   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (buf) + 256);
168
169   if (filter->init) {
170     header = "YUV4MPEG %d %d %d\nFRAME\n";
171     filter->init = FALSE;
172   }
173   else {
174     header = "FRAME\n";
175   }
176
177   snprintf (GST_BUFFER_DATA (outbuf), 255, header, filter->width, filter->height, 3);
178   len = strlen (GST_BUFFER_DATA (outbuf));
179
180   memcpy (GST_BUFFER_DATA (outbuf) + len, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
181   GST_BUFFER_SIZE (outbuf) = GST_BUFFER_SIZE (buf) + len;
182
183   gst_buffer_unref(buf);
184
185   gst_pad_push(filter->srcpad,outbuf);
186 }
187
188 static void
189 gst_lavencode_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
190 {
191   GstLavEncode *filter;
192
193   /* it's not null if we got it, but it might not be ours */
194   g_return_if_fail(GST_IS_LAVENCODE(object));
195   filter = GST_LAVENCODE(object);
196
197   switch (prop_id) {
198     default:
199       break;
200   }
201 }
202
203 static void
204 gst_lavencode_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
205 {
206   GstLavEncode *filter;
207
208   /* it's not null if we got it, but it might not be ours */
209   g_return_if_fail(GST_IS_LAVENCODE(object));
210   filter = GST_LAVENCODE(object);
211
212   switch (prop_id) {
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216   }
217 }
218
219 static GstElementStateReturn
220 gst_lavencode_change_state (GstElement *element)
221 {
222   GstLavEncode *filter;
223
224   g_return_val_if_fail (GST_IS_LAVENCODE (element), GST_STATE_FAILURE);
225
226   filter = GST_LAVENCODE(element);
227
228   if (GST_STATE_TRANSITION (element) == GST_STATE_NULL_TO_READY) {
229     filter->init = TRUE;
230   }
231
232   if (GST_ELEMENT_CLASS (parent_class)->change_state)
233     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
234
235   return GST_STATE_SUCCESS;
236 }
237
238 static gboolean
239 plugin_init (GModule *module, GstPlugin *plugin)
240 {
241   GstElementFactory *factory;
242
243   factory = gst_elementfactory_new("lavencode",GST_TYPE_LAVENCODE,
244                                    &lavencode_details);
245   g_return_val_if_fail(factory != NULL, FALSE);
246   
247   gst_elementfactory_add_padtemplate (factory, 
248                   GST_PADTEMPLATE_GET (lavencode_src_factory));
249   gst_elementfactory_add_padtemplate (factory, 
250                   GST_PADTEMPLATE_GET (lavencode_sink_factory));
251
252   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
253
254   return TRUE;
255 }
256
257 GstPluginDesc plugin_desc = {
258   GST_VERSION_MAJOR,
259   GST_VERSION_MINOR,
260   "lavencode",
261   plugin_init
262 };