Add 4:2:2, 4:1:1, and 4:4:4 output support
[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@gmail.com>");
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, Y42B, Y41B, Y444 }"))
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   filter->colorspace = "unknown";
154 }
155
156 static gboolean
157 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
158 {
159   GstY4mEncode *filter;
160   GstStructure *structure;
161   gboolean res;
162   gint w, h;
163   guint32 fourcc;
164   const GValue *fps, *par, *interlaced;
165
166   filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
167
168   structure = gst_caps_get_structure (vscaps, 0);
169
170   res = gst_structure_get_int (structure, "width", &w);
171   res &= gst_structure_get_int (structure, "height", &h);
172   res &= ((fps = gst_structure_get_value (structure, "framerate")) != NULL);
173   res &= gst_structure_get_fourcc (structure, "format", &fourcc);
174
175   switch (fourcc) {             /* Translate fourcc to Y4M colorspace code */
176     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
177     case GST_MAKE_FOURCC ('I', 'Y', 'U', 'V'):
178       filter->colorspace = "420";
179       break;
180     case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
181       filter->colorspace = "422";
182       break;
183     case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
184       filter->colorspace = "411";
185       break;
186     case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
187       filter->colorspace = "444";
188       break;
189     default:
190       res = FALSE;
191       break;
192   }
193
194   if (!res || w <= 0 || h <= 0 || !GST_VALUE_HOLDS_FRACTION (fps))
195     return FALSE;
196
197   /* optional interlaced info */
198   interlaced = gst_structure_get_value (structure, "interlaced");
199
200   /* optional par info */
201   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
202
203   filter->width = w;
204   filter->height = h;
205   filter->fps_num = gst_value_get_fraction_numerator (fps);
206   filter->fps_den = gst_value_get_fraction_denominator (fps);
207   if ((par != NULL) && GST_VALUE_HOLDS_FRACTION (par)) {
208     filter->par_num = gst_value_get_fraction_numerator (par);
209     filter->par_den = gst_value_get_fraction_denominator (par);
210   } else {                      /* indicates unknown */
211     filter->par_num = 0;
212     filter->par_den = 0;
213   }
214   if ((interlaced != NULL) && G_VALUE_HOLDS (interlaced, G_TYPE_BOOLEAN)) {
215     filter->interlaced = g_value_get_boolean (interlaced);
216   } else {
217     /* assume progressive if no interlaced property in caps */
218     filter->interlaced = FALSE;
219   }
220   /* the template caps will do for the src pad, should always accept */
221   return gst_pad_set_caps (filter->srcpad,
222       gst_static_pad_template_get_caps (&y4mencode_src_factory));
223 }
224
225 static inline GstBuffer *
226 gst_y4m_encode_get_stream_header (GstY4mEncode * filter)
227 {
228   gpointer header;
229   GstBuffer *buf;
230   gchar interlaced;
231
232   interlaced = 'p';
233
234   if (filter->interlaced && filter->top_field_first)
235     interlaced = 't';
236   else if (filter->interlaced)
237     interlaced = 'b';
238
239   header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
240       filter->colorspace, filter->width, filter->height, interlaced,
241       filter->fps_num, filter->fps_den, filter->par_num, filter->par_den);
242
243   buf = gst_buffer_new ();
244   gst_buffer_set_data (buf, header, strlen (header));
245   /* so it gets free'd when needed */
246   GST_BUFFER_MALLOCDATA (buf) = header;
247
248   return buf;
249 }
250
251 static inline GstBuffer *
252 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
253 {
254   gpointer header;
255   GstBuffer *buf;
256
257   header = g_strdup_printf ("FRAME\n");
258
259   buf = gst_buffer_new ();
260   gst_buffer_set_data (buf, header, strlen (header));
261   /* so it gets free'd when needed */
262   GST_BUFFER_MALLOCDATA (buf) = header;
263
264   return buf;
265 }
266
267 static GstFlowReturn
268 gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf)
269 {
270   GstY4mEncode *filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
271   GstBuffer *outbuf;
272   GstClockTime timestamp;
273
274   /* check we got some decent info from caps */
275   if (filter->width < 0) {
276     GST_ELEMENT_ERROR ("filter", CORE, NEGOTIATION, (NULL),
277         ("format wasn't negotiated before chain function"));
278     gst_buffer_unref (buf);
279     return GST_FLOW_NOT_NEGOTIATED;
280   }
281
282   timestamp = GST_BUFFER_TIMESTAMP (buf);
283
284   if (G_UNLIKELY (!filter->header)) {
285     if (filter->interlaced == TRUE) {
286       if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_TFF)) {
287         filter->top_field_first = TRUE;
288       } else {
289         filter->top_field_first = FALSE;
290       }
291     }
292     outbuf = gst_y4m_encode_get_stream_header (filter);
293     filter->header = TRUE;
294     outbuf = gst_buffer_join (outbuf, gst_y4m_encode_get_frame_header (filter));
295   } else {
296     outbuf = gst_y4m_encode_get_frame_header (filter);
297   }
298   /* join with data */
299   outbuf = gst_buffer_join (outbuf, buf);
300   /* decorate */
301   gst_buffer_make_metadata_writable (outbuf);
302   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
303
304   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
305
306   return gst_pad_push (filter->srcpad, outbuf);
307 }
308
309 static void
310 gst_y4m_encode_set_property (GObject * object, guint prop_id,
311     const GValue * value, GParamSpec * pspec)
312 {
313   GstY4mEncode *filter;
314
315   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
316   filter = GST_Y4M_ENCODE (object);
317
318   switch (prop_id) {
319     default:
320       break;
321   }
322 }
323
324 static void
325 gst_y4m_encode_get_property (GObject * object, guint prop_id, GValue * value,
326     GParamSpec * pspec)
327 {
328   GstY4mEncode *filter;
329
330   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
331   filter = GST_Y4M_ENCODE (object);
332
333   switch (prop_id) {
334     default:
335       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336       break;
337   }
338 }
339
340 static GstStateChangeReturn
341 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
342 {
343   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
344   GstStateChangeReturn ret;
345
346   switch (transition) {
347     case GST_STATE_CHANGE_NULL_TO_READY:
348     case GST_STATE_CHANGE_READY_TO_PAUSED:
349       break;
350     default:
351       break;
352   }
353
354   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
355       (element, transition), GST_STATE_CHANGE_SUCCESS);
356   if (ret != GST_STATE_CHANGE_SUCCESS)
357     return ret;
358
359   switch (transition) {
360     case GST_STATE_CHANGE_PAUSED_TO_READY:
361       gst_y4m_encode_reset (filter);
362       break;
363     default:
364       break;
365   }
366
367   return GST_STATE_CHANGE_SUCCESS;
368 }
369
370 static gboolean
371 plugin_init (GstPlugin * plugin)
372 {
373   return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
374       GST_TYPE_Y4M_ENCODE);
375 }
376
377 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
378     GST_VERSION_MINOR,
379     "y4menc",
380     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
381     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)