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-1.0 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 PROP_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 GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
80 GstStateChange transition);
81
82 static GstFlowReturn
83 gst_y4m_encode_handle_frame (GstVideoEncoder * encoder,
84 GstVideoCodecFrame * frame);
85 static gboolean gst_y4m_encode_set_format (GstVideoEncoder * encoder,
86 GstVideoCodecState * state);
87
88 #define gst_y4m_encode_parent_class parent_class
89 G_DEFINE_TYPE (GstY4mEncode, gst_y4m_encode, GST_TYPE_VIDEO_ENCODER);
90
91
92 static void
gst_y4m_encode_class_init(GstY4mEncodeClass * klass)93 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
94 {
95 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
96 GstVideoEncoderClass *venc_class = GST_VIDEO_ENCODER_CLASS (klass);
97
98 element_class->change_state = GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
99
100 gst_element_class_add_static_pad_template (element_class,
101 &y4mencode_src_factory);
102 gst_element_class_add_static_pad_template (element_class,
103 &y4mencode_sink_factory);
104
105 gst_element_class_set_static_metadata (element_class,
106 "YUV4MPEG video encoder", "Codec/Encoder/Video",
107 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
108 "Wim Taymans <wim.taymans@gmail.com>");
109 venc_class->set_format = gst_y4m_encode_set_format;
110 venc_class->handle_frame = gst_y4m_encode_handle_frame;
111
112 }
113
114 static void
gst_y4m_encode_init(GstY4mEncode * filter)115 gst_y4m_encode_init (GstY4mEncode * filter)
116 {
117 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (filter));
118
119 /* init properties */
120 gst_y4m_encode_reset (filter);
121 }
122
123 static void
gst_y4m_encode_reset(GstY4mEncode * filter)124 gst_y4m_encode_reset (GstY4mEncode * filter)
125 {
126 filter->header = FALSE;
127 }
128
129 static gboolean
gst_y4m_encode_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)130 gst_y4m_encode_set_format (GstVideoEncoder * encoder,
131 GstVideoCodecState * state)
132 {
133 GstY4mEncode *y4menc;
134 GstVideoInfo *info;
135 GstVideoCodecState *output_state;
136
137 y4menc = GST_Y4M_ENCODE (encoder);
138 info = &state->info;
139
140 switch (GST_VIDEO_INFO_FORMAT (info)) {
141 case GST_VIDEO_FORMAT_I420:
142 y4menc->colorspace = "420";
143 break;
144 case GST_VIDEO_FORMAT_Y42B:
145 y4menc->colorspace = "422";
146 break;
147 case GST_VIDEO_FORMAT_Y41B:
148 y4menc->colorspace = "411";
149 break;
150 case GST_VIDEO_FORMAT_Y444:
151 y4menc->colorspace = "444";
152 break;
153 default:
154 goto invalid_format;
155 }
156
157 y4menc->info = *info;
158
159 output_state =
160 gst_video_encoder_set_output_state (encoder,
161 gst_static_pad_template_get_caps (&y4mencode_src_factory), state);
162 gst_video_codec_state_unref (output_state);
163
164 return TRUE;
165
166 invalid_format:
167 {
168 GST_ERROR_OBJECT (y4menc, "Invalid format");
169 return FALSE;
170 }
171
172 }
173
174 static inline GstBuffer *
gst_y4m_encode_get_stream_header(GstY4mEncode * filter,gboolean tff)175 gst_y4m_encode_get_stream_header (GstY4mEncode * filter, gboolean tff)
176 {
177 gpointer header;
178 GstBuffer *buf;
179 gchar interlaced;
180 gsize len;
181
182 if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
183 if (tff)
184 interlaced = 't';
185 else
186 interlaced = 'b';
187 } else {
188 interlaced = 'p';
189 }
190
191 header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
192 filter->colorspace, GST_VIDEO_INFO_WIDTH (&filter->info),
193 GST_VIDEO_INFO_HEIGHT (&filter->info), interlaced,
194 GST_VIDEO_INFO_FPS_N (&filter->info),
195 GST_VIDEO_INFO_FPS_D (&filter->info),
196 GST_VIDEO_INFO_PAR_N (&filter->info),
197 GST_VIDEO_INFO_PAR_D (&filter->info));
198 len = strlen (header);
199
200 buf = gst_buffer_new ();
201 gst_buffer_append_memory (buf,
202 gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
203
204 return buf;
205 }
206
207 static inline GstBuffer *
gst_y4m_encode_get_frame_header(GstY4mEncode * filter)208 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
209 {
210 gpointer header;
211 GstBuffer *buf;
212 gsize len;
213
214 header = g_strdup_printf ("FRAME\n");
215 len = strlen (header);
216
217 buf = gst_buffer_new ();
218 gst_buffer_append_memory (buf,
219 gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
220
221 return buf;
222 }
223
224
225 static GstFlowReturn
gst_y4m_encode_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)226 gst_y4m_encode_handle_frame (GstVideoEncoder * encoder,
227 GstVideoCodecFrame * frame)
228 {
229 GstY4mEncode *filter = GST_Y4M_ENCODE (encoder);
230 GstClockTime timestamp;
231
232 /* check we got some decent info from caps */
233 if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN)
234 goto not_negotiated;
235
236 timestamp = GST_BUFFER_TIMESTAMP (frame->input_buffer);
237
238 if (G_UNLIKELY (!filter->header)) {
239 gboolean tff = FALSE;
240
241 if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
242 tff =
243 GST_BUFFER_FLAG_IS_SET (frame->input_buffer,
244 GST_VIDEO_BUFFER_FLAG_TFF);
245 }
246 frame->output_buffer = gst_y4m_encode_get_stream_header (filter, tff);
247 filter->header = TRUE;
248 frame->output_buffer =
249 gst_buffer_append (frame->output_buffer,
250 gst_y4m_encode_get_frame_header (filter));
251 } else {
252 frame->output_buffer = gst_y4m_encode_get_frame_header (filter);
253 }
254
255 frame->output_buffer =
256 gst_buffer_append (frame->output_buffer,
257 gst_buffer_copy (frame->input_buffer));
258
259 /* decorate */
260 frame->output_buffer = gst_buffer_make_writable (frame->output_buffer);
261 GST_BUFFER_TIMESTAMP (frame->output_buffer) = timestamp;
262
263 return gst_video_encoder_finish_frame (encoder, frame);
264
265 not_negotiated:
266 {
267 GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
268 ("format wasn't negotiated"));
269
270 return GST_FLOW_NOT_NEGOTIATED;
271 }
272 }
273
274 static GstStateChangeReturn
gst_y4m_encode_change_state(GstElement * element,GstStateChange transition)275 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
276 {
277 GstY4mEncode *filter = GST_Y4M_ENCODE (element);
278 GstStateChangeReturn ret;
279
280 switch (transition) {
281 case GST_STATE_CHANGE_NULL_TO_READY:
282 case GST_STATE_CHANGE_READY_TO_PAUSED:
283 break;
284 default:
285 break;
286 }
287
288 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
289 (element, transition), GST_STATE_CHANGE_SUCCESS);
290 if (ret != GST_STATE_CHANGE_SUCCESS)
291 return ret;
292
293 switch (transition) {
294 case GST_STATE_CHANGE_PAUSED_TO_READY:
295 gst_y4m_encode_reset (filter);
296 break;
297 default:
298 break;
299 }
300
301 return GST_STATE_CHANGE_SUCCESS;
302 }
303
304 static gboolean
plugin_init(GstPlugin * plugin)305 plugin_init (GstPlugin * plugin)
306 {
307 return gst_element_register (plugin, "y4menc", GST_RANK_PRIMARY,
308 GST_TYPE_Y4M_ENCODE);
309 }
310
311 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
312 GST_VERSION_MINOR,
313 y4menc,
314 "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
315 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
316