y4mencode: Remove dead code
[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@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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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,format=(string)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_reset (GstY4mEncode * filter);
78
79 static gboolean gst_y4m_encode_sink_event (GstPad * pad, GstObject * parent,
80     GstEvent * event);
81 static GstFlowReturn gst_y4m_encode_chain (GstPad * pad, GstObject * parent,
82     GstBuffer * buf);
83 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
84     GstStateChange transition);
85
86 #define gst_y4m_encode_parent_class parent_class
87 G_DEFINE_TYPE (GstY4mEncode, gst_y4m_encode, GST_TYPE_ELEMENT);
88
89 static void
90 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
91 {
92   GstElementClass *gstelement_class;
93
94   gstelement_class = (GstElementClass *) klass;
95
96   gstelement_class->change_state =
97       GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
98
99   gst_element_class_add_pad_template (gstelement_class,
100       gst_static_pad_template_get (&y4mencode_src_factory));
101   gst_element_class_add_pad_template (gstelement_class,
102       gst_static_pad_template_get (&y4mencode_sink_factory));
103
104   gst_element_class_set_static_metadata (gstelement_class,
105       "YUV4MPEG video encoder", "Codec/Encoder/Video",
106       "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
107       "Wim Taymans <wim.taymans@gmail.com>");
108 }
109
110 static void
111 gst_y4m_encode_init (GstY4mEncode * filter)
112 {
113   filter->sinkpad =
114       gst_pad_new_from_static_template (&y4mencode_sink_factory, "sink");
115   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
116   gst_pad_set_chain_function (filter->sinkpad,
117       GST_DEBUG_FUNCPTR (gst_y4m_encode_chain));
118   gst_pad_set_event_function (filter->sinkpad,
119       GST_DEBUG_FUNCPTR (gst_y4m_encode_sink_event));
120
121   filter->srcpad =
122       gst_pad_new_from_static_template (&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->negotiated = FALSE;
134 }
135
136 static gboolean
137 gst_y4m_encode_setcaps (GstPad * pad, GstCaps * vscaps)
138 {
139   gboolean ret;
140   GstY4mEncode *filter;
141   GstVideoInfo info;
142
143   filter = GST_Y4M_ENCODE (GST_PAD_PARENT (pad));
144
145   if (!gst_video_info_from_caps (&info, vscaps))
146     goto invalid_format;
147
148   switch (GST_VIDEO_INFO_FORMAT (&info)) {
149     case GST_VIDEO_FORMAT_I420:
150       filter->colorspace = "420";
151       break;
152     case GST_VIDEO_FORMAT_Y42B:
153       filter->colorspace = "422";
154       break;
155     case GST_VIDEO_FORMAT_Y41B:
156       filter->colorspace = "411";
157       break;
158     case GST_VIDEO_FORMAT_Y444:
159       filter->colorspace = "444";
160       break;
161     default:
162       goto invalid_format;
163   }
164
165   filter->info = info;
166
167   /* the template caps will do for the src pad, should always accept */
168   ret = gst_pad_set_caps (filter->srcpad,
169       gst_static_pad_template_get_caps (&y4mencode_src_factory));
170
171   filter->negotiated = ret;
172
173   return ret;
174
175   /* ERRORS */
176 invalid_format:
177   {
178     GST_ERROR_OBJECT (filter, "got invalid caps");
179     return FALSE;
180   }
181 }
182
183 static gboolean
184 gst_y4m_encode_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
185 {
186   gboolean res;
187
188   switch (GST_EVENT_TYPE (event)) {
189     case GST_EVENT_CAPS:
190     {
191       GstCaps *caps;
192
193       gst_event_parse_caps (event, &caps);
194       res = gst_y4m_encode_setcaps (pad, caps);
195       gst_event_unref (event);
196       break;
197     }
198     default:
199       res = gst_pad_event_default (pad, parent, event);
200       break;
201   }
202   return res;
203 }
204
205 static inline GstBuffer *
206 gst_y4m_encode_get_stream_header (GstY4mEncode * filter, gboolean tff)
207 {
208   gpointer header;
209   GstBuffer *buf;
210   gchar interlaced;
211   gsize len;
212
213   if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
214     if (tff)
215       interlaced = 't';
216     else
217       interlaced = 'b';
218   } else {
219     interlaced = 'p';
220   }
221
222   header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
223       filter->colorspace, GST_VIDEO_INFO_WIDTH (&filter->info),
224       GST_VIDEO_INFO_HEIGHT (&filter->info), interlaced,
225       GST_VIDEO_INFO_FPS_N (&filter->info),
226       GST_VIDEO_INFO_FPS_D (&filter->info),
227       GST_VIDEO_INFO_PAR_N (&filter->info),
228       GST_VIDEO_INFO_PAR_D (&filter->info));
229   len = strlen (header);
230
231   buf = gst_buffer_new ();
232   gst_buffer_append_memory (buf,
233       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
234
235   return buf;
236 }
237
238 static inline GstBuffer *
239 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
240 {
241   gpointer header;
242   GstBuffer *buf;
243   gsize len;
244
245   header = g_strdup_printf ("FRAME\n");
246   len = strlen (header);
247
248   buf = gst_buffer_new ();
249   gst_buffer_append_memory (buf,
250       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
251
252   return buf;
253 }
254
255 static GstFlowReturn
256 gst_y4m_encode_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
257 {
258   GstY4mEncode *filter = GST_Y4M_ENCODE (parent);
259   GstBuffer *outbuf;
260   GstClockTime timestamp;
261
262   /* check we got some decent info from caps */
263   if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN)
264     goto not_negotiated;
265
266   timestamp = GST_BUFFER_TIMESTAMP (buf);
267
268   if (G_UNLIKELY (!filter->header)) {
269     gboolean tff = FALSE;
270
271     if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
272       tff = GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF);
273     }
274     outbuf = gst_y4m_encode_get_stream_header (filter, tff);
275     filter->header = TRUE;
276     outbuf =
277         gst_buffer_append (outbuf, gst_y4m_encode_get_frame_header (filter));
278   } else {
279     outbuf = gst_y4m_encode_get_frame_header (filter);
280   }
281   /* join with data, FIXME, strides are all wrong etc */
282   outbuf = gst_buffer_append (outbuf, buf);
283   /* decorate */
284   outbuf = gst_buffer_make_writable (outbuf);
285
286   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
287
288   return gst_pad_push (filter->srcpad, outbuf);
289
290   /* ERRORS */
291 not_negotiated:
292   {
293     GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
294         ("format wasn't negotiated before chain function"));
295     gst_buffer_unref (buf);
296     return GST_FLOW_NOT_NEGOTIATED;
297   }
298 }
299
300 static GstStateChangeReturn
301 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
302 {
303   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
304   GstStateChangeReturn ret;
305
306   switch (transition) {
307     case GST_STATE_CHANGE_NULL_TO_READY:
308     case GST_STATE_CHANGE_READY_TO_PAUSED:
309       break;
310     default:
311       break;
312   }
313
314   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
315       (element, transition), GST_STATE_CHANGE_SUCCESS);
316   if (ret != GST_STATE_CHANGE_SUCCESS)
317     return ret;
318
319   switch (transition) {
320     case GST_STATE_CHANGE_PAUSED_TO_READY:
321       gst_y4m_encode_reset (filter);
322       break;
323     default:
324       break;
325   }
326
327   return GST_STATE_CHANGE_SUCCESS;
328 }
329
330 static gboolean
331 plugin_init (GstPlugin * plugin)
332 {
333   return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
334       GST_TYPE_Y4M_ENCODE);
335 }
336
337 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
338     GST_VERSION_MINOR,
339     y4menc,
340     "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
341     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)