[MOVED FROM BAD] y4menc: don't strip timestamps
[platform/upstream/gstreamer.git] / gst / y4m / gsty4mencode.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2006> Mark Nauwelaerts <mnauw@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 /**
21  * SECTION:element-y4menc
22  *
23  * <refsect2>
24  * <para>
25  * Creates a YU4MPEG2 raw video stream as defined by the mjpegtools project.
26  * </para>
27  * <title>Example launch line</title>
28  * <para>
29  * (write everything in one line, without the backslash characters)
30  * <programlisting>
31  * gst-launch-0.10 videotestsrc num-buffers=250 \
32  * ! 'video/x-raw-yuv,format=(fourcc)I420,width=320,height=240,framerate=(fraction)25/1' \
33  * ! y4menc ! filesink location=test.yuv
34  * </programlisting>
35  * </para>
36  * </refsect2>
37  *
38  */
39
40 /* see mjpegtools/yuv4mpeg.h for yuv4mpeg format */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45 #include <string.h>
46 #include <gst/gst.h>
47 #include <gst/video/video.h>
48 #include "gsty4mencode.h"
49
50 static const GstElementDetails y4mencode_details =
51 GST_ELEMENT_DETAILS ("YUV4MPEG video encoder",
52     "Codec/Encoder/Video",
53     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
54     "Wim Taymans <wim.taymans@chello.be>");
55
56
57 /* Filter signals and args */
58 enum
59 {
60   /* FILL ME */
61   LAST_SIGNAL
62 };
63
64 enum
65 {
66   ARG_0
67 };
68
69 static GstStaticPadTemplate y4mencode_src_factory =
70 GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 2")
74     );
75
76 static GstStaticPadTemplate y4mencode_sink_factory =
77 GST_STATIC_PAD_TEMPLATE ("sink",
78     GST_PAD_SINK,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ IYUV, I420 }"))
81     );
82
83
84 static void gst_y4m_encode_set_property (GObject * object,
85     guint prop_id, const GValue * value, GParamSpec * pspec);
86 static void gst_y4m_encode_get_property (GObject * object,
87     guint prop_id, GValue * value, GParamSpec * pspec);
88
89 static void gst_y4m_encode_reset (GstY4mEncode * filter);
90
91 static gboolean gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps);
92 static GstFlowReturn gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf);
93 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
94     GstStateChange transition);
95
96 GST_BOILERPLATE (GstY4mEncode, gst_y4m_encode, GstElement, GST_TYPE_ELEMENT);
97
98
99 static void
100 gst_y4m_encode_base_init (gpointer g_class)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
103
104   gst_element_class_add_pad_template (element_class,
105       gst_static_pad_template_get (&y4mencode_src_factory));
106   gst_element_class_add_pad_template (element_class,
107       gst_static_pad_template_get (&y4mencode_sink_factory));
108   gst_element_class_set_details (element_class, &y4mencode_details);
109 }
110
111 static void
112 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
113 {
114   GObjectClass *gobject_class;
115   GstElementClass *gstelement_class;
116
117   gobject_class = (GObjectClass *) klass;
118   gstelement_class = (GstElementClass *) klass;
119
120   gstelement_class->change_state =
121       GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
122
123   gobject_class->set_property = gst_y4m_encode_set_property;
124   gobject_class->get_property = gst_y4m_encode_get_property;
125 }
126
127 static void
128 gst_y4m_encode_init (GstY4mEncode * filter, GstY4mEncodeClass * klass)
129 {
130   filter->sinkpad =
131       gst_pad_new_from_static_template (&y4mencode_sink_factory, "sink");
132   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
133   gst_pad_set_chain_function (filter->sinkpad,
134       GST_DEBUG_FUNCPTR (gst_y4m_encode_chain));
135   gst_pad_set_setcaps_function (filter->sinkpad,
136       GST_DEBUG_FUNCPTR (gst_y4m_encode_setcaps));
137
138   filter->srcpad =
139       gst_pad_new_from_static_template (&y4mencode_src_factory, "src");
140   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
141   gst_pad_use_fixed_caps (filter->srcpad);
142
143   /* init properties */
144   gst_y4m_encode_reset (filter);
145 }
146
147 static void
148 gst_y4m_encode_reset (GstY4mEncode * filter)
149 {
150   filter->width = filter->height = -1;
151   filter->fps_num = filter->fps_den = 1;
152   filter->par_num = filter->par_den = 1;
153 }
154
155 static gboolean
156 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
157 {
158   GstY4mEncode *filter;
159   GstStructure *structure;
160   gboolean res;
161   gint w, h;
162   const GValue *fps, *par;
163
164   filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
165
166   structure = gst_caps_get_structure (vscaps, 0);
167
168   res = gst_structure_get_int (structure, "width", &w);
169   res &= gst_structure_get_int (structure, "height", &h);
170   res &= ((fps = gst_structure_get_value (structure, "framerate")) != NULL);
171
172   if (!res || w <= 0 || h <= 0 || !GST_VALUE_HOLDS_FRACTION (fps))
173     return FALSE;
174
175   /* optional par info */
176   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
177
178   filter->width = w;
179   filter->height = h;
180   filter->fps_num = gst_value_get_fraction_numerator (fps);
181   filter->fps_den = gst_value_get_fraction_denominator (fps);
182   if ((par != NULL) && GST_VALUE_HOLDS_FRACTION (par)) {
183     filter->par_num = gst_value_get_fraction_numerator (par);
184     filter->par_den = gst_value_get_fraction_denominator (par);
185   } else {                      /* indicates unknown */
186     filter->par_num = 0;
187     filter->par_den = 0;
188   }
189
190   /* the template caps will do for the src pad, should always accept */
191   return gst_pad_set_caps (filter->srcpad,
192       gst_static_pad_template_get_caps (&y4mencode_src_factory));
193 }
194
195 static inline GstBuffer *
196 gst_y4m_encode_get_stream_header (GstY4mEncode * filter)
197 {
198   gpointer header;
199   GstBuffer *buf;
200
201   header = g_strdup_printf ("YUV4MPEG2 W%d H%d I? F%d:%d A%d:%d\n",
202       filter->width, filter->height,
203       filter->fps_num, filter->fps_den, filter->par_num, filter->par_den);
204
205   buf = gst_buffer_new ();
206   gst_buffer_set_data (buf, header, strlen (header));
207   /* so it gets free'd when needed */
208   GST_BUFFER_MALLOCDATA (buf) = header;
209
210   return buf;
211 }
212
213 static inline GstBuffer *
214 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
215 {
216   gpointer header;
217   GstBuffer *buf;
218
219   header = g_strdup_printf ("FRAME\n");
220
221   buf = gst_buffer_new ();
222   gst_buffer_set_data (buf, header, strlen (header));
223   /* so it gets free'd when needed */
224   GST_BUFFER_MALLOCDATA (buf) = header;
225
226   return buf;
227 }
228
229 static GstFlowReturn
230 gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf)
231 {
232   GstY4mEncode *filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
233   GstBuffer *outbuf;
234   GstClockTime timestamp;
235
236   /* check we got some decent info from caps */
237   if (filter->width < 0) {
238     GST_ELEMENT_ERROR ("filter", CORE, NEGOTIATION, (NULL),
239         ("format wasn't negotiated before chain function"));
240     gst_buffer_unref (buf);
241     return GST_FLOW_NOT_NEGOTIATED;
242   }
243
244   timestamp = GST_BUFFER_TIMESTAMP (buf);
245
246   if (G_UNLIKELY (!filter->header)) {
247     outbuf = gst_y4m_encode_get_stream_header (filter);
248     filter->header = TRUE;
249     outbuf = gst_buffer_join (outbuf, gst_y4m_encode_get_frame_header (filter));
250   } else {
251     outbuf = gst_y4m_encode_get_frame_header (filter);
252   }
253   /* join with data */
254   outbuf = gst_buffer_join (outbuf, buf);
255   /* decorate */
256   gst_buffer_make_metadata_writable (outbuf);
257   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
258
259   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
260
261   return gst_pad_push (filter->srcpad, outbuf);
262 }
263
264 static void
265 gst_y4m_encode_set_property (GObject * object, guint prop_id,
266     const GValue * value, GParamSpec * pspec)
267 {
268   GstY4mEncode *filter;
269
270   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
271   filter = GST_Y4M_ENCODE (object);
272
273   switch (prop_id) {
274     default:
275       break;
276   }
277 }
278
279 static void
280 gst_y4m_encode_get_property (GObject * object, guint prop_id, GValue * value,
281     GParamSpec * pspec)
282 {
283   GstY4mEncode *filter;
284
285   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
286   filter = GST_Y4M_ENCODE (object);
287
288   switch (prop_id) {
289     default:
290       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291       break;
292   }
293 }
294
295 static GstStateChangeReturn
296 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
297 {
298   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
299   GstStateChangeReturn ret;
300
301   switch (transition) {
302     case GST_STATE_CHANGE_NULL_TO_READY:
303     case GST_STATE_CHANGE_READY_TO_PAUSED:
304       break;
305     default:
306       break;
307   }
308
309   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
310       (element, transition), GST_STATE_CHANGE_SUCCESS);
311   if (ret != GST_STATE_CHANGE_SUCCESS)
312     return ret;
313
314   switch (transition) {
315     case GST_STATE_CHANGE_PAUSED_TO_READY:
316       gst_y4m_encode_reset (filter);
317       break;
318     default:
319       break;
320   }
321
322   return GST_STATE_CHANGE_SUCCESS;
323 }
324
325 static gboolean
326 plugin_init (GstPlugin * plugin)
327 {
328   return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
329       GST_TYPE_Y4M_ENCODE);
330 }
331
332 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
333     GST_VERSION_MINOR,
334     "y4menc",
335     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
336     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)