[MOVED FROM BAD] replace framerate aproximations by their real value (24000/1001...
[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 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
96     y4mencode_type = g_type_register_static (GST_TYPE_ELEMENT,
97         "GstY4mEncode", &y4mencode_info, 0);
98   }
99   return y4mencode_type;
100 }
101
102
103 static void
104 gst_y4mencode_base_init (gpointer g_class)
105 {
106   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
107
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);
113 }
114 static void
115 gst_y4mencode_class_init (GstY4mEncodeClass * klass)
116 {
117   GObjectClass *gobject_class;
118   GstElementClass *gstelement_class;
119
120   gobject_class = (GObjectClass *) klass;
121   gstelement_class = (GstElementClass *) klass;
122
123   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
124
125   gstelement_class->change_state = gst_y4mencode_change_state;
126
127   gobject_class->set_property = gst_y4mencode_set_property;
128   gobject_class->get_property = gst_y4mencode_get_property;
129 }
130
131 static GstPadLinkReturn
132 gst_y4mencode_sinkconnect (GstPad * pad, const GstCaps * caps)
133 {
134   GstY4mEncode *filter;
135   gint idx = -1, i;
136   gdouble fps;
137   gdouble framerates[] = {
138     00.000,
139     24000 / 1001, 24.000,       /* 24fps movie */
140     25.000,                     /* PAL */
141     30000 / 1001, 30.000,       /* NTSC */
142     50.000,
143     60000 / 1001, 60.000
144   };
145   GstStructure *structure;
146
147   filter = GST_Y4MENCODE (gst_pad_get_parent (pad));
148
149   structure = gst_caps_get_structure (caps, 0);
150
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;
155
156   /* find fps idx */
157   idx = 0;
158   for (i = 1; i < 9; i++) {
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   filter->fps_idx = idx;
167
168   return GST_PAD_LINK_OK;
169 }
170
171 static void
172 gst_y4mencode_init (GstY4mEncode * filter)
173 {
174   filter->sinkpad =
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);
180
181   filter->srcpad =
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);
185
186   filter->init = TRUE;
187 }
188
189 static void
190 gst_y4mencode_chain (GstPad * pad, GstData * _data)
191 {
192   GstBuffer *buf = GST_BUFFER (_data);
193   GstY4mEncode *filter;
194   GstBuffer *outbuf;
195   gchar *header;
196   gint len;
197
198   g_return_if_fail (pad != NULL);
199   g_return_if_fail (GST_IS_PAD (pad));
200   g_return_if_fail (buf != NULL);
201
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));
205
206   outbuf = gst_buffer_new ();
207   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (buf) + 256);
208
209   if (filter->init) {
210     header = "YUV4MPEG W%d H%d I? %d\nFRAME\n";
211     filter->init = FALSE;
212   } else {
213     header = "FRAME\n";
214   }
215
216   snprintf (GST_BUFFER_DATA (outbuf), 255, header,
217       filter->width, filter->height, filter->fps_idx);
218   len = strlen (GST_BUFFER_DATA (outbuf));
219
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;
223
224   gst_buffer_unref (buf);
225
226   gst_pad_push (filter->srcpad, GST_DATA (outbuf));
227 }
228
229 static void
230 gst_y4mencode_set_property (GObject * object, guint prop_id,
231     const GValue * value, GParamSpec * pspec)
232 {
233   GstY4mEncode *filter;
234
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);
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   /* 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);
254
255   switch (prop_id) {
256     default:
257       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258       break;
259   }
260 }
261
262 static GstElementStateReturn
263 gst_y4mencode_change_state (GstElement * element)
264 {
265   GstY4mEncode *filter;
266
267   g_return_val_if_fail (GST_IS_Y4MENCODE (element), GST_STATE_FAILURE);
268
269   filter = GST_Y4MENCODE (element);
270
271   if (GST_STATE_TRANSITION (element) == GST_STATE_NULL_TO_READY) {
272     filter->init = TRUE;
273   }
274
275   if (GST_ELEMENT_CLASS (parent_class)->change_state)
276     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
277
278   return GST_STATE_SUCCESS;
279 }
280
281 static gboolean
282 plugin_init (GstPlugin * plugin)
283 {
284   return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
285       GST_TYPE_Y4MENCODE);
286 }
287
288 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
289     GST_VERSION_MINOR,
290     "y4menc",
291     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
292     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)