• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @title: y4menc
23  *
24  * Creates a YU4MPEG2 raw video stream as defined by the mjpegtools project.
25  *
26  * ## Example launch line
27  *
28  * (write everything in one line, without the backslash characters)
29  * |[
30  * gst-launch-1.0 videotestsrc num-buffers=250 \
31  * ! 'video/x-raw,format=(string)I420,width=320,height=240,framerate=(fraction)25/1' \
32  * ! y4menc ! filesink location=test.yuv
33  * ]|
34  *
35  */
36 
37 /* see mjpegtools/yuv4mpeg.h for yuv4mpeg format */
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 #include <string.h>
43 #include <gst/gst.h>
44 #include <gst/video/video.h>
45 #include "gsty4mencode.h"
46 
47 /* Filter signals and args */
48 enum
49 {
50   /* FILL ME */
51   LAST_SIGNAL
52 };
53 
54 enum
55 {
56   PROP_0
57 };
58 
59 static GstStaticPadTemplate y4mencode_src_factory =
60 GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("application/x-yuv4mpeg, " "y4mversion = (int) 2")
64     );
65 
66 static GstStaticPadTemplate y4mencode_sink_factory =
67 GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ IYUV, I420, Y42B, Y41B, Y444 }"))
71     );
72 
73 
74 static void gst_y4m_encode_reset (GstY4mEncode * filter);
75 
76 static GstStateChangeReturn gst_y4m_encode_change_state (GstElement * element,
77     GstStateChange transition);
78 
79 static GstFlowReturn
80 gst_y4m_encode_handle_frame (GstVideoEncoder * encoder,
81     GstVideoCodecFrame * frame);
82 static gboolean gst_y4m_encode_set_format (GstVideoEncoder * encoder,
83     GstVideoCodecState * state);
84 
85 #define gst_y4m_encode_parent_class parent_class
86 G_DEFINE_TYPE (GstY4mEncode, gst_y4m_encode, GST_TYPE_VIDEO_ENCODER);
87 GST_ELEMENT_REGISTER_DEFINE (y4menc, "y4menc", GST_RANK_PRIMARY,
88     GST_TYPE_Y4M_ENCODE);
89 
90 static void
gst_y4m_encode_class_init(GstY4mEncodeClass * klass)91 gst_y4m_encode_class_init (GstY4mEncodeClass * klass)
92 {
93   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
94   GstVideoEncoderClass *venc_class = GST_VIDEO_ENCODER_CLASS (klass);
95 
96   element_class->change_state = GST_DEBUG_FUNCPTR (gst_y4m_encode_change_state);
97 
98   gst_element_class_add_static_pad_template (element_class,
99       &y4mencode_src_factory);
100   gst_element_class_add_static_pad_template (element_class,
101       &y4mencode_sink_factory);
102 
103   gst_element_class_set_static_metadata (element_class,
104       "YUV4MPEG video encoder", "Codec/Encoder/Video",
105       "Encodes a YUV frame into the yuv4mpeg format (mjpegtools)",
106       "Wim Taymans <wim.taymans@gmail.com>");
107   venc_class->set_format = gst_y4m_encode_set_format;
108   venc_class->handle_frame = gst_y4m_encode_handle_frame;
109 
110 }
111 
112 static void
gst_y4m_encode_init(GstY4mEncode * filter)113 gst_y4m_encode_init (GstY4mEncode * filter)
114 {
115   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (filter));
116 
117   /* init properties */
118   gst_y4m_encode_reset (filter);
119 }
120 
121 static void
gst_y4m_encode_reset(GstY4mEncode * filter)122 gst_y4m_encode_reset (GstY4mEncode * filter)
123 {
124   filter->header = FALSE;
125 }
126 
127 static gboolean
gst_y4m_encode_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)128 gst_y4m_encode_set_format (GstVideoEncoder * encoder,
129     GstVideoCodecState * state)
130 {
131   GstY4mEncode *y4menc;
132   GstVideoInfo *info;
133   GstVideoCodecState *output_state;
134 
135   y4menc = GST_Y4M_ENCODE (encoder);
136   info = &state->info;
137 
138   switch (GST_VIDEO_INFO_FORMAT (info)) {
139     case GST_VIDEO_FORMAT_I420:
140       y4menc->colorspace = "420";
141       break;
142     case GST_VIDEO_FORMAT_Y42B:
143       y4menc->colorspace = "422";
144       break;
145     case GST_VIDEO_FORMAT_Y41B:
146       y4menc->colorspace = "411";
147       break;
148     case GST_VIDEO_FORMAT_Y444:
149       y4menc->colorspace = "444";
150       break;
151     default:
152       goto invalid_format;
153   }
154 
155   y4menc->info = *info;
156 
157   output_state =
158       gst_video_encoder_set_output_state (encoder,
159       gst_static_pad_template_get_caps (&y4mencode_src_factory), state);
160   gst_video_codec_state_unref (output_state);
161 
162   return TRUE;
163 
164 invalid_format:
165   {
166     GST_ERROR_OBJECT (y4menc, "Invalid format");
167     return FALSE;
168   }
169 
170 }
171 
172 static inline GstBuffer *
gst_y4m_encode_get_stream_header(GstY4mEncode * filter,gboolean tff)173 gst_y4m_encode_get_stream_header (GstY4mEncode * filter, gboolean tff)
174 {
175   gpointer header;
176   GstBuffer *buf;
177   gchar interlaced;
178   gsize len;
179 
180   if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
181     if (tff)
182       interlaced = 't';
183     else
184       interlaced = 'b';
185   } else {
186     interlaced = 'p';
187   }
188 
189   header = g_strdup_printf ("YUV4MPEG2 C%s W%d H%d I%c F%d:%d A%d:%d\n",
190       filter->colorspace, GST_VIDEO_INFO_WIDTH (&filter->info),
191       GST_VIDEO_INFO_HEIGHT (&filter->info), interlaced,
192       GST_VIDEO_INFO_FPS_N (&filter->info),
193       GST_VIDEO_INFO_FPS_D (&filter->info),
194       GST_VIDEO_INFO_PAR_N (&filter->info),
195       GST_VIDEO_INFO_PAR_D (&filter->info));
196   len = strlen (header);
197 
198   buf = gst_buffer_new ();
199   gst_buffer_append_memory (buf,
200       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
201 
202   return buf;
203 }
204 
205 static inline GstBuffer *
gst_y4m_encode_get_frame_header(GstY4mEncode * filter)206 gst_y4m_encode_get_frame_header (GstY4mEncode * filter)
207 {
208   gpointer header;
209   GstBuffer *buf;
210   gsize len;
211 
212   header = g_strdup_printf ("FRAME\n");
213   len = strlen (header);
214 
215   buf = gst_buffer_new ();
216   gst_buffer_append_memory (buf,
217       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
218 
219   return buf;
220 }
221 
222 
223 static GstFlowReturn
gst_y4m_encode_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)224 gst_y4m_encode_handle_frame (GstVideoEncoder * encoder,
225     GstVideoCodecFrame * frame)
226 {
227   GstY4mEncode *filter = GST_Y4M_ENCODE (encoder);
228   GstClockTime timestamp;
229 
230   /* check we got some decent info from caps */
231   if (GST_VIDEO_INFO_FORMAT (&filter->info) == GST_VIDEO_FORMAT_UNKNOWN)
232     goto not_negotiated;
233 
234   timestamp = GST_BUFFER_TIMESTAMP (frame->input_buffer);
235 
236   if (G_UNLIKELY (!filter->header)) {
237     gboolean tff = FALSE;
238 
239     if (GST_VIDEO_INFO_IS_INTERLACED (&filter->info)) {
240       tff =
241           GST_BUFFER_FLAG_IS_SET (frame->input_buffer,
242           GST_VIDEO_BUFFER_FLAG_TFF);
243     }
244     frame->output_buffer = gst_y4m_encode_get_stream_header (filter, tff);
245     filter->header = TRUE;
246     frame->output_buffer =
247         gst_buffer_append (frame->output_buffer,
248         gst_y4m_encode_get_frame_header (filter));
249   } else {
250     frame->output_buffer = gst_y4m_encode_get_frame_header (filter);
251   }
252 
253   frame->output_buffer =
254       gst_buffer_append (frame->output_buffer,
255       gst_buffer_copy (frame->input_buffer));
256 
257   /* decorate */
258   frame->output_buffer = gst_buffer_make_writable (frame->output_buffer);
259   GST_BUFFER_TIMESTAMP (frame->output_buffer) = timestamp;
260 
261   return gst_video_encoder_finish_frame (encoder, frame);
262 
263 not_negotiated:
264   {
265     GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION, (NULL),
266         ("format wasn't negotiated"));
267 
268     return GST_FLOW_NOT_NEGOTIATED;
269   }
270 }
271 
272 static GstStateChangeReturn
gst_y4m_encode_change_state(GstElement * element,GstStateChange transition)273 gst_y4m_encode_change_state (GstElement * element, GstStateChange transition)
274 {
275   GstY4mEncode *filter = GST_Y4M_ENCODE (element);
276   GstStateChangeReturn ret;
277 
278   switch (transition) {
279     case GST_STATE_CHANGE_NULL_TO_READY:
280     case GST_STATE_CHANGE_READY_TO_PAUSED:
281       break;
282     default:
283       break;
284   }
285 
286   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
287       (element, transition), GST_STATE_CHANGE_SUCCESS);
288   if (ret != GST_STATE_CHANGE_SUCCESS)
289     return ret;
290 
291   switch (transition) {
292     case GST_STATE_CHANGE_PAUSED_TO_READY:
293       gst_y4m_encode_reset (filter);
294       break;
295     default:
296       break;
297   }
298 
299   return GST_STATE_CHANGE_SUCCESS;
300 }
301 
302 static gboolean
plugin_init(GstPlugin * plugin)303 plugin_init (GstPlugin * plugin)
304 {
305   return GST_ELEMENT_REGISTER (y4menc, plugin);
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)
313