[MOVED FROM BAD] gst-indent
[platform/upstream/gst-plugins-good.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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
24 #include <math.h>
25 #include <gst/gst.h>
26 #include <gst/video/video.h>
27 #include "gsty4mencode.h"
28
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>");
33
34
35 /* Filter signals and args */
36 enum
37 {
38   /* FILL ME */
39   LAST_SIGNAL
40 };
41
42 enum
43 {
44   ARG_0
45 };
46
47 static GstStaticPadTemplate y4mencode_src_factory =
48 GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 1")
52     );
53
54 static GstStaticPadTemplate y4mencode_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
59     );
60
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);
64
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);
69
70 static void gst_y4mencode_chain (GstPad * pad, GstData * _data);
71 static GstElementStateReturn gst_y4mencode_change_state (GstElement * element);
72
73
74 static GstElementClass *parent_class = NULL;
75
76 /*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
77
78 GType
79 gst_y4mencode_get_type (void)
80 {
81   static GType y4mencode_type = 0;
82
83   if (!y4mencode_type) {
84     static const GTypeInfo y4mencode_info = {
85       sizeof (GstY4mEncodeClass),
86       gst_y4mencode_base_init,
87       NULL,
88       (GClassInitFunc) gst_y4mencode_class_init,
89       NULL,
90       NULL,
91       sizeof (GstY4mEncode),
92       0,
93       (GInstanceInitFunc) gst_y4mencode_init,
94     };
95     y4mencode_type = g_type_register_static (GST_TYPE_ELEMENT,
96         "GstY4mEncode", &y4mencode_info, 0);
97   }
98   return y4mencode_type;
99 }
100
101
102 static void
103 gst_y4mencode_base_init (gpointer g_class)
104 {
105   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
106
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);
112 }
113 static void
114 gst_y4mencode_class_init (GstY4mEncodeClass * klass)
115 {
116   GObjectClass *gobject_class;
117   GstElementClass *gstelement_class;
118
119   gobject_class = (GObjectClass *) klass;
120   gstelement_class = (GstElementClass *) klass;
121
122   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
123
124   gstelement_class->change_state = gst_y4mencode_change_state;
125
126   gobject_class->set_property = gst_y4mencode_set_property;
127   gobject_class->get_property = gst_y4mencode_get_property;
128 }
129
130 static GstPadLinkReturn
131 gst_y4mencode_sinkconnect (GstPad * pad, const GstCaps * caps)
132 {
133   GstY4mEncode *filter;
134   gint idx = -1, i;
135   gdouble fps;
136   gdouble framerates[] = {
137     00.000,
138     23.976, 24.000,             /* 24fps movie */
139     25.000,                     /* PAL */
140     29.970, 30.000,             /* NTSC */
141     50.000,
142     59.940, 60.000
143   };
144   GstStructure *structure;
145
146   filter = GST_Y4MENCODE (gst_pad_get_parent (pad));
147
148   structure = gst_caps_get_structure (caps, 0);
149
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);
153
154   /* find fps idx */
155   for (i = 1; i < 9; i++) {
156     if (idx == -1) {
157       idx = i;
158     } else {
159       gdouble old_diff = fabs (framerates[idx] - fps),
160           new_diff = fabs (framerates[i] - fps);
161
162       if (new_diff < old_diff) {
163         idx = i;
164       }
165     }
166   }
167   filter->fps_idx = idx;
168
169   return GST_PAD_LINK_OK;
170 }
171
172 static void
173 gst_y4mencode_init (GstY4mEncode * filter)
174 {
175   filter->sinkpad =
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);
181
182   filter->srcpad =
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);
186
187   filter->init = TRUE;
188 }
189
190 static void
191 gst_y4mencode_chain (GstPad * pad, GstData * _data)
192 {
193   GstBuffer *buf = GST_BUFFER (_data);
194   GstY4mEncode *filter;
195   GstBuffer *outbuf;
196   gchar *header;
197   gint len;
198
199   g_return_if_fail (pad != NULL);
200   g_return_if_fail (GST_IS_PAD (pad));
201   g_return_if_fail (buf != NULL);
202
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));
206
207   outbuf = gst_buffer_new ();
208   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (buf) + 256);
209
210   if (filter->init) {
211     header = "YUV4MPEG %d %d %d\nFRAME\n";
212     filter->init = FALSE;
213   } else {
214     header = "FRAME\n";
215   }
216
217   snprintf (GST_BUFFER_DATA (outbuf), 255, header,
218       filter->width, filter->height, filter->fps_idx);
219   len = strlen (GST_BUFFER_DATA (outbuf));
220
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;
224
225   gst_buffer_unref (buf);
226
227   gst_pad_push (filter->srcpad, GST_DATA (outbuf));
228 }
229
230 static void
231 gst_y4mencode_set_property (GObject * object, guint prop_id,
232     const GValue * value, GParamSpec * pspec)
233 {
234   GstY4mEncode *filter;
235
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);
239
240   switch (prop_id) {
241     default:
242       break;
243   }
244 }
245
246 static void
247 gst_y4mencode_get_property (GObject * object, guint prop_id, GValue * value,
248     GParamSpec * pspec)
249 {
250   GstY4mEncode *filter;
251
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);
255
256   switch (prop_id) {
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260   }
261 }
262
263 static GstElementStateReturn
264 gst_y4mencode_change_state (GstElement * element)
265 {
266   GstY4mEncode *filter;
267
268   g_return_val_if_fail (GST_IS_Y4MENCODE (element), GST_STATE_FAILURE);
269
270   filter = GST_Y4MENCODE (element);
271
272   if (GST_STATE_TRANSITION (element) == GST_STATE_NULL_TO_READY) {
273     filter->init = TRUE;
274   }
275
276   if (GST_ELEMENT_CLASS (parent_class)->change_state)
277     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
278
279   return GST_STATE_SUCCESS;
280 }
281
282 static gboolean
283 plugin_init (GstPlugin * plugin)
284 {
285   return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
286       GST_TYPE_Y4MENCODE);
287 }
288
289 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
290     GST_VERSION_MINOR,
291     "y4menc",
292     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
293     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)