d007a1d2b5aaea4b7176d63a8e2c03bbce3e0632
[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 /* Filter signals and args */
51 enum
52 {
53   /* FILL ME */
54   LAST_SIGNAL
55 };
56
57 enum
58 {
59   ARG_0
60 };
61
62 static GstStaticPadTemplate y4mencode_src_factory =
63 GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 2")
67     );
68
69 static GstStaticPadTemplate y4mencode_sink_factory =
70 GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ IYUV, I420, Y42B, Y41B, Y444 }"))
74     );
75
76
77 static void gst_y4m_encode_set_property (GObject * object,
78     guint prop_id, const GValue * value, GParamSpec * pspec);
79 static void gst_y4m_encode_get_property (GObject * object,
80     guint prop_id, GValue * value, GParamSpec * pspec);
81
82 static void gst_y4m_encode_reset (GstY4mEncode * filter);
83
84 static gboolean gst_y4m_encode_sink_event (GstPad * pad, GstEvent * event);
85 static GstFlowReturn gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf);
86 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
87     GstStateChange transition);
88
89 #define gst_y4m_encode_parent_class parent_class
90 G_DEFINE_TYPE (GstY4mEncode, gst_y4m_encode, GST_TYPE_ELEMENT);
91
92 static void
93 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
94 {
95   GObjectClass *gobject_class;
96   GstElementClass *gstelement_class;
97
98   gobject_class = (GObjectClass *) klass;
99   gstelement_class = (GstElementClass *) klass;
100
101   gobject_class->set_property = gst_y4m_encode_set_property;
102   gobject_class->get_property = gst_y4m_encode_get_property;
103
104   gstelement_class->change_state =
105       GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
106
107   gst_element_class_add_pad_template (gstelement_class,
108       gst_static_pad_template_get (&y4mencode_src_factory));
109   gst_element_class_add_pad_template (gstelement_class,
110       gst_static_pad_template_get (&y4mencode_sink_factory));
111
112   gst_element_class_set_details_simple (gstelement_class,
113       "YUV4MPEG video encoder", "Codec/Encoder/Video",
114       "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
115       "Wim Taymans <wim.taymans@gmail.com>");
116 }
117
118 static void
119 gst_y4m_encode_init (GstY4mEncode * filter)
120 {
121   filter->sinkpad =
122       gst_pad_new_from_static_template (&y4mencode_sink_factory, "sink");
123   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
124   gst_pad_set_chain_function (filter->sinkpad,
125       GST_DEBUG_FUNCPTR (gst_y4m_encode_chain));
126   gst_pad_set_event_function (filter->sinkpad,
127       GST_DEBUG_FUNCPTR (gst_y4m_encode_sink_event));
128
129   filter->srcpad =
130       gst_pad_new_from_static_template (&y4mencode_src_factory, "src");
131   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
132   gst_pad_use_fixed_caps (filter->srcpad);
133
134   /* init properties */
135   gst_y4m_encode_reset (filter);
136 }
137
138 static void
139 gst_y4m_encode_reset (GstY4mEncode * filter)
140 {
141   filter->negotiated = FALSE;
142 }
143
144 static gboolean
145 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
146 {
147   gboolean ret;
148   GstY4mEncode *filter;
149   GstVideoInfo info;
150
151   filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
152
153   if (!gst_video_info_from_caps (&info, vscaps))
154     goto invalid_format;
155
156   filter->info = info;
157
158   switch (GST_VIDEO_INFO_FORMAT (&filter->info)) {
159     case GST_VIDEO_FORMAT_I420:
160       filter->colorspace = "420";
161       break;
162     case GST_VIDEO_FORMAT_Y42B:
163       filter->colorspace = "422";
164       break;
165     case GST_VIDEO_FORMAT_Y41B:
166       filter->colorspace = "411";
167       break;
168     case GST_VIDEO_FORMAT_Y444:
169       filter->colorspace = "444";
170       break;
171     default:
172       goto invalid_format;
173   }
174
175   /* the template caps will do for the src pad, should always accept */
176   ret = gst_pad_set_caps (filter->srcpad,
177       gst_static_pad_template_get_caps (&y4mencode_src_factory));
178
179   filter->negotiated = ret;
180
181   return ret;
182
183   /* ERRORS */
184 invalid_format:
185   {
186     GST_ERROR_OBJECT (filter, "got invalid caps");
187     return FALSE;
188   }
189 }
190
191 static gboolean
192 gst_y4m_encode_sink_event (GstPad * pad, GstEvent * event)
193 {
194   gboolean res;
195
196   switch (GST_EVENT_TYPE (event)) {
197     case GST_EVENT_CAPS:
198     {
199       GstCaps *caps;
200
201       gst_event_parse_caps (event, &caps);
202       res = gst_y4m_encode_setcaps (pad, caps);
203       gst_event_unref (event);
204       break;
205     }
206     default:
207       res = gst_pad_event_default (pad, event);
208       break;
209   }
210   return res;
211 }
212
213 static inline GstBuffer *
214 gst_y4m_encode_get_stream_header (GstY4mEncode * filter, gboolean tff)
215 {
216   gpointer header;
217   GstBuffer *buf;
218   gchar interlaced;
219   gsize len;
220
221   if (filter->info.flags & GST_VIDEO_FLAG_INTERLACED) {
222     if (tff)
223       interlaced = 't';
224     else
225       interlaced = 'b';
226   } else {
227     interlaced = 'p';
228   }
229
230   header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
231       filter->colorspace, GST_VIDEO_INFO_WIDTH (&filter->info),
232       GST_VIDEO_INFO_HEIGHT (&filter->info), interlaced,
233       GST_VIDEO_INFO_FPS_N (&filter->info),
234       GST_VIDEO_INFO_FPS_D (&filter->info),
235       GST_VIDEO_INFO_PAR_N (&filter->info),
236       GST_VIDEO_INFO_PAR_D (&filter->info));
237   len = strlen (header);
238
239   buf = gst_buffer_new ();
240   gst_buffer_take_memory (buf, -1,
241       gst_memory_new_wrapped (0, header, g_free, len, 0, len));
242
243   return buf;
244 }
245
246 static inline GstBuffer *
247 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
248 {
249   gpointer header;
250   GstBuffer *buf;
251   gsize len;
252
253   header = g_strdup_printf ("FRAME\n");
254   len = strlen (header);
255
256   buf = gst_buffer_new ();
257   gst_buffer_take_memory (buf, -1,
258       gst_memory_new_wrapped (0, header, g_free, len, 0, len));
259
260   return buf;
261 }
262
263 static GstFlowReturn
264 gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf)
265 {
266   GstY4mEncode *filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
267   GstBuffer *outbuf;
268   GstClockTime timestamp;
269
270   /* check we got some decent info from caps */
271   if (GST_VIDEO_INFO_FORMAT (&filter->info) != GST_VIDEO_FORMAT_UNKNOWN)
272     goto not_negotiated;
273
274   timestamp = GST_BUFFER_TIMESTAMP (buf);
275
276   if (G_UNLIKELY (!filter->header)) {
277     gboolean tff;
278
279     if (filter->info.flags & GST_VIDEO_FLAG_INTERLACED) {
280       tff = GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_TFF);
281     }
282     outbuf = gst_y4m_encode_get_stream_header (filter, tff);
283     filter->header = TRUE;
284     outbuf = gst_buffer_join (outbuf, gst_y4m_encode_get_frame_header (filter));
285   } else {
286     outbuf = gst_y4m_encode_get_frame_header (filter);
287   }
288   /* join with data, FIXME, strides are all wrong etc */
289   outbuf = gst_buffer_join (outbuf, buf);
290   /* decorate */
291   outbuf = gst_buffer_make_writable (outbuf);
292
293   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
294
295   return gst_pad_push (filter->srcpad, outbuf);
296
297   /* ERRORS */
298 not_negotiated:
299   {
300     GST_ELEMENT_ERROR ("filter", CORE, NEGOTIATION, (NULL),
301         ("format wasn't negotiated before chain function"));
302     gst_buffer_unref (buf);
303     return GST_FLOW_NOT_NEGOTIATED;
304   }
305 }
306
307 static void
308 gst_y4m_encode_set_property (GObject * object, guint prop_id,
309     const GValue * value, GParamSpec * pspec)
310 {
311   GstY4mEncode G_GNUC_UNUSED *filter;
312
313   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
314   filter = GST_Y4M_ENCODE (object);
315
316   switch (prop_id) {
317     default:
318       break;
319   }
320 }
321
322 static void
323 gst_y4m_encode_get_property (GObject * object, guint prop_id, GValue * value,
324     GParamSpec * pspec)
325 {
326   GstY4mEncode G_GNUC_UNUSED *filter;
327
328   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
329   filter = GST_Y4M_ENCODE (object);
330
331   switch (prop_id) {
332     default:
333       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334       break;
335   }
336 }
337
338 static GstStateChangeReturn
339 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
340 {
341   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
342   GstStateChangeReturn ret;
343
344   switch (transition) {
345     case GST_STATE_CHANGE_NULL_TO_READY:
346     case GST_STATE_CHANGE_READY_TO_PAUSED:
347       break;
348     default:
349       break;
350   }
351
352   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
353       (element, transition), GST_STATE_CHANGE_SUCCESS);
354   if (ret != GST_STATE_CHANGE_SUCCESS)
355     return ret;
356
357   switch (transition) {
358     case GST_STATE_CHANGE_PAUSED_TO_READY:
359       gst_y4m_encode_reset (filter);
360       break;
361     default:
362       break;
363   }
364
365   return GST_STATE_CHANGE_SUCCESS;
366 }
367
368 static gboolean
369 plugin_init (GstPlugin * plugin)
370 {
371   return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
372       GST_TYPE_Y4M_ENCODE);
373 }
374
375 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
376     GST_VERSION_MINOR,
377     "y4menc",
378     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
379     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)