[MOVED FROM BAD] configure.ac: Enable cdaudio and y4m.
[platform/upstream/gst-plugins-good.git] / gst / y4m / gsty4mencode.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2006> Mark Nauwelaerts <mnauw@skynet.be>
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 /* see mjpegtools/yuv4mpeg.h for yuv4mpeg format */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <string.h>
27 #include <gst/gst.h>
28 #include <gst/video/video.h>
29 #include "gsty4mencode.h"
30
31 static const GstElementDetails y4mencode_details =
32 GST_ELEMENT_DETAILS ("YUV4MPEG video encoder",
33     "Codec/Encoder/Video",
34     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
35     "Wim Taymans <wim.taymans@chello.be>");
36
37
38 /* Filter signals and args */
39 enum
40 {
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum
46 {
47   ARG_0
48 };
49
50 static GstStaticPadTemplate y4mencode_src_factory =
51 GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 2")
55     );
56
57 static GstStaticPadTemplate y4mencode_sink_factory =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ IYUV, I420 }"))
62     );
63
64
65 static void gst_y4m_encode_set_property (GObject * object,
66     guint prop_id, const GValue * value, GParamSpec * pspec);
67 static void gst_y4m_encode_get_property (GObject * object,
68     guint prop_id, GValue * value, GParamSpec * pspec);
69
70 static void gst_y4m_encode_reset (GstY4mEncode * filter);
71
72 static gboolean gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps);
73 static GstFlowReturn gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf);
74 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
75     GstStateChange transition);
76
77 GST_BOILERPLATE (GstY4mEncode, gst_y4m_encode, GstElement, GST_TYPE_ELEMENT);
78
79
80 static void
81 gst_y4m_encode_base_init (gpointer g_class)
82 {
83   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
84
85   gst_element_class_add_pad_template (element_class,
86       gst_static_pad_template_get (&y4mencode_src_factory));
87   gst_element_class_add_pad_template (element_class,
88       gst_static_pad_template_get (&y4mencode_sink_factory));
89   gst_element_class_set_details (element_class, &y4mencode_details);
90 }
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   gstelement_class->change_state =
102       GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
103
104   gobject_class->set_property = gst_y4m_encode_set_property;
105   gobject_class->get_property = gst_y4m_encode_get_property;
106 }
107
108 static void
109 gst_y4m_encode_init (GstY4mEncode * filter, GstY4mEncodeClass * klass)
110 {
111   filter->sinkpad =
112       gst_pad_new_from_template (gst_static_pad_template_get
113       (&y4mencode_sink_factory), "sink");
114   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
115   gst_pad_set_chain_function (filter->sinkpad,
116       GST_DEBUG_FUNCPTR (gst_y4m_encode_chain));
117   gst_pad_set_setcaps_function (filter->sinkpad,
118       GST_DEBUG_FUNCPTR (gst_y4m_encode_setcaps));
119
120   filter->srcpad =
121       gst_pad_new_from_template (gst_static_pad_template_get
122       (&y4mencode_src_factory), "src");
123   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
124   gst_pad_use_fixed_caps (filter->srcpad);
125
126   /* init properties */
127   gst_y4m_encode_reset (filter);
128 }
129
130 static void
131 gst_y4m_encode_reset (GstY4mEncode * filter)
132 {
133   filter->width = filter->height = -1;
134   filter->fps_num = filter->fps_den = 1;
135   filter->par_num = filter->par_den = 1;
136 }
137
138 static gboolean
139 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
140 {
141   GstY4mEncode *filter;
142   GstStructure *structure;
143   gint w, h;
144   const GValue *fps, *par;
145
146   filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
147
148   structure = gst_caps_get_structure (vscaps, 0);
149
150   g_return_val_if_fail (gst_structure_get_int (structure, "width", &w), FALSE);
151   g_return_val_if_fail (gst_structure_get_int (structure, "height", &h), FALSE);
152   fps = gst_structure_get_value (structure, "framerate");
153   g_return_val_if_fail (w > 0 && h > 0
154       && fps != NULL && GST_VALUE_HOLDS_FRACTION (fps), FALSE);
155   /* optional par info */
156   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
157
158   filter->width = w;
159   filter->height = h;
160   filter->fps_num = gst_value_get_fraction_numerator (fps);
161   filter->fps_den = gst_value_get_fraction_denominator (fps);
162   if ((par != NULL) && GST_VALUE_HOLDS_FRACTION (par)) {
163     filter->par_num = gst_value_get_fraction_numerator (par);
164     filter->par_den = gst_value_get_fraction_denominator (par);
165   } else {                      /* indicates unknown */
166     filter->par_num = 0;
167     filter->par_den = 0;
168   }
169
170   /* the template caps will do for the src pad, should always accept */
171   return gst_pad_set_caps (filter->srcpad,
172       gst_caps_copy (gst_pad_get_pad_template_caps (filter->srcpad)));
173 }
174
175 static inline GstBuffer *
176 gst_y4m_encode_get_stream_header (GstY4mEncode * filter)
177 {
178   gpointer header;
179   GstBuffer *buf;
180
181   header = g_strdup_printf ("YUV4MPEG2 W%d H%d I? F%d:%d A%d:%d\n",
182       filter->width, filter->height,
183       filter->fps_num, filter->fps_den, filter->par_num, filter->par_den);
184
185   buf = gst_buffer_new ();
186   gst_buffer_set_data (buf, header, strlen (header));
187   /* so it gets free'd when needed */
188   GST_BUFFER_MALLOCDATA (buf) = header;
189
190   return buf;
191 }
192
193 static inline GstBuffer *
194 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
195 {
196   gpointer header;
197   GstBuffer *buf;
198
199   header = g_strdup_printf ("FRAME\n");
200
201   buf = gst_buffer_new ();
202   gst_buffer_set_data (buf, header, strlen (header));
203   /* so it gets free'd when needed */
204   GST_BUFFER_MALLOCDATA (buf) = header;
205
206   return buf;
207 }
208
209 static GstFlowReturn
210 gst_y4m_encode_chain (GstPad * pad, GstBuffer * buf)
211 {
212   GstY4mEncode *filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
213   GstBuffer *outbuf;
214
215   /* check we got some decent info from caps */
216   if (filter->width < 0) {
217     GST_ELEMENT_ERROR ("filter", CORE, NEGOTIATION, (NULL),
218         ("format wasn't negotiated before chain function"));
219     gst_buffer_unref (buf);
220     return GST_FLOW_NOT_NEGOTIATED;
221   }
222
223   if (G_UNLIKELY (!filter->header)) {
224     outbuf = gst_y4m_encode_get_stream_header (filter);
225     filter->header = TRUE;
226     outbuf = gst_buffer_join (outbuf, gst_y4m_encode_get_frame_header (filter));
227   } else {
228     outbuf = gst_y4m_encode_get_frame_header (filter);
229   }
230   /* join with data */
231   outbuf = gst_buffer_join (outbuf, buf);
232   /* decorate */
233   gst_buffer_make_metadata_writable (outbuf);
234   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
235   /* strip to avoid sink dropping on time-base, decorate and send */
236   GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
237   return gst_pad_push (filter->srcpad, outbuf);
238 }
239
240 static void
241 gst_y4m_encode_set_property (GObject * object, guint prop_id,
242     const GValue * value, GParamSpec * pspec)
243 {
244   GstY4mEncode *filter;
245
246   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
247   filter = GST_Y4M_ENCODE (object);
248
249   switch (prop_id) {
250     default:
251       break;
252   }
253 }
254
255 static void
256 gst_y4m_encode_get_property (GObject * object, guint prop_id, GValue * value,
257     GParamSpec * pspec)
258 {
259   GstY4mEncode *filter;
260
261   g_return_if_fail (GST_IS_Y4M_ENCODE (object));
262   filter = GST_Y4M_ENCODE (object);
263
264   switch (prop_id) {
265     default:
266       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267       break;
268   }
269 }
270
271 static GstStateChangeReturn
272 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
273 {
274   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
275   GstStateChangeReturn ret;
276
277   switch (transition) {
278     case GST_STATE_CHANGE_NULL_TO_READY:
279     case GST_STATE_CHANGE_READY_TO_PAUSED:
280       break;
281     default:
282       break;
283   }
284
285   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
286       (element, transition), GST_STATE_CHANGE_SUCCESS);
287   if (ret != GST_STATE_CHANGE_SUCCESS)
288     return ret;
289
290   switch (transition) {
291     case GST_STATE_CHANGE_PAUSED_TO_READY:
292       gst_y4m_encode_reset (filter);
293       break;
294     default:
295       break;
296   }
297
298   return GST_STATE_CHANGE_SUCCESS;
299 }
300
301 static gboolean
302 plugin_init (GstPlugin * plugin)
303 {
304   return gst_element_register (plugin, "y4menc", GST_RANK_NONE,
305       GST_TYPE_Y4M_ENCODE);
306 }
307
308 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
309     GST_VERSION_MINOR,
310     "y4menc",
311     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
312     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)