[MOVED FROM BAD] rework build; add translations for v4l2
[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 #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 GstStateChangeReturn gst_y4mencode_change_state (GstElement * element,
72     GstStateChange transition);
73
74
75 static GstElementClass *parent_class = NULL;
76
77 /*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
78
79 GType
80 gst_y4mencode_get_type (void)
81 {
82   static GType y4mencode_type = 0;
83
84   if (!y4mencode_type) {
85     static const GTypeInfo y4mencode_info = {
86       sizeof (GstY4mEncodeClass),
87       gst_y4mencode_base_init,
88       NULL,
89       (GClassInitFunc) gst_y4mencode_class_init,
90       NULL,
91       NULL,
92       sizeof (GstY4mEncode),
93       0,
94       (GInstanceInitFunc) gst_y4mencode_init,
95     };
96
97     y4mencode_type = g_type_register_static (GST_TYPE_ELEMENT,
98         "GstY4mEncode", &y4mencode_info, 0);
99   }
100   return y4mencode_type;
101 }
102
103
104 static void
105 gst_y4mencode_base_init (gpointer g_class)
106 {
107   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108
109   gst_element_class_add_pad_template (element_class,
110       gst_static_pad_template_get (&y4mencode_src_factory));
111   gst_element_class_add_pad_template (element_class,
112       gst_static_pad_template_get (&y4mencode_sink_factory));
113   gst_element_class_set_details (element_class, &y4mencode_details);
114 }
115 static void
116 gst_y4mencode_class_init (GstY4mEncodeClass * klass)
117 {
118   GObjectClass *gobject_class;
119   GstElementClass *gstelement_class;
120
121   gobject_class = (GObjectClass *) klass;
122   gstelement_class = (GstElementClass *) klass;
123
124   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
125
126   gstelement_class->change_state = gst_y4mencode_change_state;
127
128   gobject_class->set_property = gst_y4mencode_set_property;
129   gobject_class->get_property = gst_y4mencode_get_property;
130 }
131
132 static GstPadLinkReturn
133 gst_y4mencode_sinkconnect (GstPad * pad, const GstCaps * caps)
134 {
135   GstY4mEncode *filter;
136   gint idx = -1, i;
137   gdouble fps;
138   gdouble framerates[] = {
139     00.000,
140     24000 / 1001., 24.000,      /* 24fps movie */
141     25.000,                     /* PAL */
142     30000 / 1001., 30.000,      /* NTSC */
143     50.000,
144     60000 / 1001., 60.000
145   };
146   GstStructure *structure;
147
148   filter = GST_Y4MENCODE (gst_pad_get_parent (pad));
149
150   structure = gst_caps_get_structure (caps, 0);
151
152   if (!gst_structure_get_int (structure, "width", &filter->width) ||
153       !gst_structure_get_int (structure, "height", &filter->height) ||
154       !gst_structure_get_double (structure, "framerate", &fps))
155     return GST_PAD_LINK_REFUSED;
156
157   /* find fps idx */
158   idx = 0;
159   for (i = 1; i < 9; i++) {
160     gdouble old_diff = fabs (framerates[idx] - fps),
161         new_diff = fabs (framerates[i] - fps);
162
163     if (new_diff < old_diff) {
164       idx = i;
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 W%d H%d I? %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   g_return_if_fail (GST_IS_Y4MENCODE (object));
237   filter = GST_Y4MENCODE (object);
238
239   switch (prop_id) {
240     default:
241       break;
242   }
243 }
244
245 static void
246 gst_y4mencode_get_property (GObject * object, guint prop_id, GValue * value,
247     GParamSpec * pspec)
248 {
249   GstY4mEncode *filter;
250
251   g_return_if_fail (GST_IS_Y4MENCODE (object));
252   filter = GST_Y4MENCODE (object);
253
254   switch (prop_id) {
255     default:
256       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257       break;
258   }
259 }
260
261 static GstStateChangeReturn
262 gst_y4mencode_change_state (GstElement * element, GstStateChange transition)
263 {
264   GstY4mEncode *filter;
265
266   g_return_val_if_fail (GST_IS_Y4MENCODE (element), GST_STATE_CHANGE_FAILURE);
267
268   filter = GST_Y4MENCODE (element);
269
270   if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
271     filter->init = TRUE;
272   }
273
274   if (GST_ELEMENT_CLASS (parent_class)->change_state)
275     return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
276
277   return GST_STATE_CHANGE_SUCCESS;
278 }
279
280 static gboolean
281 plugin_init (GstPlugin * plugin)
282 {
283   return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
284       GST_TYPE_Y4MENCODE);
285 }
286
287 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
288     GST_VERSION_MINOR,
289     "y4menc",
290     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
291     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)