1 /* GStreamer mpeg2enc (mjpegtools) wrapper
2 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * (c) 2006 Mark Nauwelaerts <manauw@skynet.be>
5 * gstmpeg2enc.cc: gstreamer mpeg2enc wrapping
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-mpeg2enc
27 * This element encodes raw video into an MPEG-1/2 elementary stream using the
28 * <ulink url="http://mjpeg.sourceforge.net/">mjpegtools</ulink> library.
29 * Documentation on MPEG encoding in general can be found in the
30 * <ulink url="https://sourceforge.net/docman/display_doc.php?docid=3456&group_id=5776">MJPEG Howto</ulink>
31 * and on the various available parameters in the documentation
32 * of the mpeg2enc tool in particular, which shares options with this element.
35 * <title>Example pipeline</title>
37 * gst-launch-0.10 videotestsrc num-buffers=1000 ! mpeg2enc ! filesink location=videotestsrc.m1v
38 * ]| This example pipeline will encode a test video source to a an MPEG1
39 * elementary stream (with Generic MPEG1 profile).
41 * Likely, the #GstMpeg2enc:format property
42 * is most important, as it selects the type of MPEG stream that is produced.
43 * In particular, default property values are dependent on the format,
44 * and can even be forcibly restrained to certain pre-sets (and thereby ignored).
45 * Note that the (S)VCD profiles also restrict the image size, so some scaling
46 * may be needed to accomodate this. The so-called generic profiles (as used
47 * in the example above) allow most parameters to be adjusted.
50 * gst-launch-0.10 videotestsrc num-buffers=1000 ! videoscale ! mpeg2enc format=1 norm=p ! filesink location=videotestsrc.m1v
51 * ]| This will produce an MPEG1 profile stream according to VCD2.0 specifications
52 * for PAL #GstMpeg2enc:norm (as the image height is dependent on video norm).
60 #include "gstmpeg2enc.hh"
62 GST_DEBUG_CATEGORY (mpeg2enc_debug);
64 #define COMMON_VIDEO_CAPS \
65 "width = (int) [ 16, 4096 ], " \
66 "height = (int) [ 16, 4096 ], " \
68 " (fraction) { 24000/1001, 24/1, 25/1, 30000/1001, 30/1, 50/1, 60000/1001 }"
70 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS ("video/x-raw-yuv, "
74 "format = (fourcc) { I420 }, " COMMON_VIDEO_CAPS)
77 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
80 GST_STATIC_CAPS ("video/mpeg, "
81 "systemstream = (boolean) false, "
82 "mpegversion = (int) { 1, 2 }, " COMMON_VIDEO_CAPS)
86 static void gst_mpeg2enc_finalize (GObject * object);
87 static void gst_mpeg2enc_reset (GstMpeg2enc * enc);
88 static gboolean gst_mpeg2enc_setcaps (GstPad * pad, GstCaps * caps);
89 static GstCaps *gst_mpeg2enc_getcaps (GstPad * pad);
90 static gboolean gst_mpeg2enc_sink_event (GstPad * pad, GstEvent * event);
91 static void gst_mpeg2enc_loop (GstMpeg2enc * enc);
92 static GstFlowReturn gst_mpeg2enc_chain (GstPad * pad, GstBuffer * buffer);
93 static gboolean gst_mpeg2enc_src_activate_push (GstPad * pad, gboolean active);
94 static GstStateChangeReturn gst_mpeg2enc_change_state (GstElement * element,
95 GstStateChange transition);
97 static void gst_mpeg2enc_get_property (GObject * object,
98 guint prop_id, GValue * value, GParamSpec * pspec);
99 static void gst_mpeg2enc_set_property (GObject * object,
100 guint prop_id, const GValue * value, GParamSpec * pspec);
103 _do_init (GType object_type)
105 const GInterfaceInfo preset_interface_info = {
106 NULL, /* interface_init */
107 NULL, /* interface_finalize */
108 NULL /* interface_data */
111 g_type_add_interface_static (object_type, GST_TYPE_PRESET,
112 &preset_interface_info);
115 GST_BOILERPLATE_FULL (GstMpeg2enc, gst_mpeg2enc, GstElement, GST_TYPE_ELEMENT,
119 gst_mpeg2enc_base_init (gpointer klass)
121 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
123 gst_element_class_set_details_simple (element_class,
124 "mpeg2enc video encoder", "Codec/Encoder/Video",
125 "High-quality MPEG-1/2 video encoder",
126 "Andrew Stevens <andrew.stevens@nexgo.de>\n"
127 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
129 gst_element_class_add_static_pad_template (element_class, &src_template);
130 gst_element_class_add_static_pad_template (element_class,
135 gst_mpeg2enc_class_init (GstMpeg2encClass * klass)
137 GObjectClass *object_class = G_OBJECT_CLASS (klass);
138 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
140 GST_DEBUG_CATEGORY_INIT (mpeg2enc_debug, "mpeg2enc", 0, "MPEG1/2 encoder");
142 object_class->set_property = gst_mpeg2enc_set_property;
143 object_class->get_property = gst_mpeg2enc_get_property;
145 /* register properties */
146 GstMpeg2EncOptions::initProperties (object_class);
148 object_class->finalize = GST_DEBUG_FUNCPTR (gst_mpeg2enc_finalize);
150 element_class->change_state = GST_DEBUG_FUNCPTR (gst_mpeg2enc_change_state);
154 gst_mpeg2enc_finalize (GObject * object)
156 GstMpeg2enc *enc = GST_MPEG2ENC (object);
165 g_mutex_free (enc->tlock);
166 g_cond_free (enc->cond);
167 g_queue_free (enc->time);
169 G_OBJECT_CLASS (parent_class)->finalize (object);
173 gst_mpeg2enc_init (GstMpeg2enc * enc, GstMpeg2encClass * g_class)
175 GstElement *element = GST_ELEMENT (enc);
176 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
179 gst_pad_new_from_template (gst_element_class_get_pad_template
180 (element_class, "sink"), "sink");
181 gst_pad_set_setcaps_function (enc->sinkpad,
182 GST_DEBUG_FUNCPTR (gst_mpeg2enc_setcaps));
183 gst_pad_set_getcaps_function (enc->sinkpad,
184 GST_DEBUG_FUNCPTR (gst_mpeg2enc_getcaps));
185 gst_pad_set_event_function (enc->sinkpad,
186 GST_DEBUG_FUNCPTR (gst_mpeg2enc_sink_event));
187 gst_pad_set_chain_function (enc->sinkpad,
188 GST_DEBUG_FUNCPTR (gst_mpeg2enc_chain));
189 gst_element_add_pad (element, enc->sinkpad);
192 gst_pad_new_from_template (gst_element_class_get_pad_template
193 (element_class, "src"), "src");
194 gst_pad_use_fixed_caps (enc->srcpad);
195 gst_pad_set_activatepush_function (enc->srcpad,
196 GST_DEBUG_FUNCPTR (gst_mpeg2enc_src_activate_push));
197 gst_element_add_pad (element, enc->srcpad);
199 enc->options = new GstMpeg2EncOptions ();
203 enc->tlock = g_mutex_new ();
204 enc->cond = g_cond_new ();
205 enc->time = g_queue_new ();
207 gst_mpeg2enc_reset (enc);
211 gst_mpeg2enc_reset (GstMpeg2enc * enc)
216 enc->srcresult = GST_FLOW_OK;
218 /* in case of error'ed ending */
220 gst_buffer_unref (enc->buffer);
222 while ((buf = (GstBuffer *) g_queue_pop_head (enc->time)))
223 gst_buffer_unref (buf);
232 /* some (!) coding to get caps depending on the video norm and chosen format */
234 gst_mpeg2enc_add_fps (GstStructure * structure, gint fpss[])
236 GValue list = { 0, }, fps = {
240 g_value_init (&list, GST_TYPE_LIST);
241 g_value_init (&fps, GST_TYPE_FRACTION);
242 for (n = 0; fpss[n] != 0; n++) {
243 gst_value_set_fraction (&fps, fpss[n], fpss[n + 1]);
244 gst_value_list_append_value (&list, &fps);
247 gst_structure_set_value (structure, "framerate", &list);
248 g_value_unset (&list);
249 g_value_unset (&fps);
253 gst_mpeg2enc_get_fps (GstMpeg2enc * enc)
255 static gint fps_pal[]
256 = { 24, 1, 25, 1, 50, 1, 0 };
257 static gint fps_ntsc[]
258 = { 24000, 1001, 24, 1, 30000, 1001, 30, 1, 60000, 1001, 0 };
259 static gint fps_all[]
260 = { 24000, 1001, 24, 1, 30000, 1001, 30, 1, 60000, 1001, 25, 1, 50, 1, 0 };
262 if (enc->options->norm == 'n')
264 else if (enc->options->norm == 0)
270 static GstStructure *
271 gst_mpeg2enc_structure_from_norm (GstMpeg2enc * enc, gint horiz,
272 gint pal_v, gint ntsc_v)
274 GstStructure *structure;
276 structure = gst_structure_new ("video/x-raw-yuv",
277 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I', '4', '2', '0'), NULL);
279 switch (enc->options->norm) {
286 g_value_init (&list, GST_TYPE_LIST);
287 g_value_init (&val, G_TYPE_INT);
288 g_value_set_int (&val, pal_v);
289 gst_value_list_append_value (&list, &val);
290 g_value_set_int (&val, ntsc_v);
291 gst_value_list_append_value (&list, &val);
292 gst_structure_set_value (structure, "height", &list);
293 g_value_unset (&list);
294 g_value_unset (&val);
298 gst_structure_set (structure, "height", G_TYPE_INT, ntsc_v,
302 gst_structure_set (structure, "height", G_TYPE_INT, pal_v, (void *) NULL);
305 gst_structure_set (structure, "width", G_TYPE_INT, horiz, (void *) NULL);
306 gst_mpeg2enc_add_fps (structure, gst_mpeg2enc_get_fps (enc));
312 gst_mpeg2enc_getcaps (GstPad * pad)
314 GstMpeg2enc *enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
317 caps = GST_PAD_CAPS (pad);
323 switch (enc->options->format) {
325 case 2: /* user vcd */
326 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
327 352, 288, 240), NULL);
330 case 5: /* user svcd */
331 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
332 480, 576, 480), NULL);
334 case 6: /* vcd stills */
336 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
337 352, 288, 240), NULL);
338 /* high resolution */
339 gst_caps_append_structure (caps,
340 gst_mpeg2enc_structure_from_norm (enc, 704, 576, 480));
342 case 7: /* svcd stills */
344 caps = gst_caps_new_full (gst_mpeg2enc_structure_from_norm (enc,
345 480, 576, 480), NULL);
346 /* high resolution */
347 gst_caps_append_structure (caps,
348 gst_mpeg2enc_structure_from_norm (enc, 704, 576, 480));
355 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
356 gst_mpeg2enc_add_fps (gst_caps_get_structure (caps, 0),
357 gst_mpeg2enc_get_fps (enc));
361 GST_DEBUG_OBJECT (enc, "returned caps %" GST_PTR_FORMAT, caps);
366 gst_mpeg2enc_setcaps (GstPad * pad, GstCaps * caps)
368 GstMpeg2enc *enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
369 GstCaps *othercaps = NULL, *mycaps;
372 /* does not go well to restart stream mid-way */
374 goto refuse_renegotiation;
376 /* since mpeg encoder does not really check, let's check caps */
377 mycaps = gst_pad_get_caps (pad);
378 othercaps = gst_caps_intersect (caps, mycaps);
379 gst_caps_unref (mycaps);
380 if (!othercaps || gst_caps_is_empty (othercaps))
382 gst_caps_unref (othercaps);
385 /* create new encoder with these settings */
386 enc->encoder = new GstMpeg2Encoder (enc->options, GST_ELEMENT (enc), caps);
388 if (!enc->encoder->setup ())
391 /* and set caps on other side, which should accept anyway */
392 othercaps = enc->encoder->getFormat ();
393 ret = gst_pad_set_caps (enc->srcpad, othercaps);
394 gst_caps_unref (othercaps);
399 /* now that we have all the setup and buffers are expected incoming;
400 * task can get going */
401 gst_pad_start_task (enc->srcpad, (GstTaskFunction) gst_mpeg2enc_loop, enc);
407 GST_WARNING_OBJECT (enc, "refused caps %" GST_PTR_FORMAT, caps);
410 gst_caps_unref (othercaps);
420 refuse_renegotiation:
422 GST_WARNING_OBJECT (enc, "refused renegotiation (to %" GST_PTR_FORMAT ")",
430 gst_mpeg2enc_sink_event (GstPad * pad, GstEvent * event)
433 gboolean result = TRUE;
435 enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
437 switch (GST_EVENT_TYPE (event)) {
438 case GST_EVENT_FLUSH_START:
440 result = gst_pad_push_event (enc->srcpad, event);
442 /* no special action as there is not much to flush;
443 * neither is it possible to halt the mpeg encoding loop */
446 case GST_EVENT_FLUSH_STOP:
448 result = gst_pad_push_event (enc->srcpad, event);
452 /* this clears the error state in case of a failure in encoding task;
453 * so chain function can carry on again */
454 GST_MPEG2ENC_MUTEX_LOCK (enc);
455 enc->srcresult = GST_FLOW_OK;
456 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
460 /* inform the encoding task that it can stop now */
461 GST_MPEG2ENC_MUTEX_LOCK (enc);
463 GST_MPEG2ENC_SIGNAL (enc);
464 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
466 /* eat this event for now, task will send eos when finished */
467 gst_event_unref (event);
471 /* for a serialized event, wait until an earlier buffer is gone,
472 * though this is no guarantee as to when the encoder is done with it */
473 if (GST_EVENT_IS_SERIALIZED (event)) {
474 GST_MPEG2ENC_MUTEX_LOCK (enc);
476 GST_MPEG2ENC_WAIT (enc);
477 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
482 result = gst_pad_push_event (enc->srcpad, event);
489 gst_mpeg2enc_loop (GstMpeg2enc * enc)
491 /* do not try to resume or start when output problems;
492 * also ensures a proper (forced) state change */
493 if (enc->srcresult != GST_FLOW_OK)
497 /* note that init performs a pre-fill and therefore needs buffers */
498 enc->encoder->init ();
499 /* task will stay in here during all of the encoding */
500 enc->encoder->encode ();
502 /* if not well and truly eos, something strange happened */
504 GST_ERROR_OBJECT (enc, "encoding task ended without being eos");
505 /* notify the chain function that it's over */
506 GST_MPEG2ENC_MUTEX_LOCK (enc);
507 enc->srcresult = GST_FLOW_ERROR;
508 GST_MPEG2ENC_SIGNAL (enc);
509 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
511 /* send eos if this was not a forced stop or other problem */
512 if (enc->srcresult == GST_FLOW_OK)
513 gst_pad_push_event (enc->srcpad, gst_event_new_eos ());
517 GST_WARNING_OBJECT (enc, "task started without Mpeg2Encoder");
523 /* no need to run wildly, stopped elsewhere, e.g. state change */
524 GST_DEBUG_OBJECT (enc, "pausing encoding task");
525 gst_pad_pause_task (enc->srcpad);
531 GST_DEBUG_OBJECT (enc, "encoding task reached eos");
536 GST_DEBUG_OBJECT (enc, "not looping because encoding task encountered %s",
537 gst_flow_get_name (enc->srcresult));
543 gst_mpeg2enc_chain (GstPad * pad, GstBuffer * buffer)
547 enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
549 if (G_UNLIKELY (!enc->encoder))
552 GST_MPEG2ENC_MUTEX_LOCK (enc);
554 if (G_UNLIKELY (enc->eos))
557 if (G_UNLIKELY (enc->srcresult != GST_FLOW_OK))
560 /* things look good, now inform the encoding task that a buffer is ready */
562 GST_MPEG2ENC_WAIT (enc);
563 enc->buffer = buffer;
564 g_queue_push_tail (enc->time, gst_buffer_ref (buffer));
565 GST_MPEG2ENC_SIGNAL (enc);
566 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
568 /* buffer will be released by task */
574 GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
575 ("format wasn't negotiated before chain function"));
577 gst_buffer_unref (buffer);
578 return GST_FLOW_NOT_NEGOTIATED;
582 GST_DEBUG_OBJECT (enc, "ignoring buffer at end-of-stream");
583 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
585 gst_buffer_unref (buffer);
586 return GST_FLOW_UNEXPECTED;
590 GstFlowReturn ret = enc->srcresult;
592 GST_DEBUG_OBJECT (enc,
593 "ignoring buffer because encoding task encountered %s",
594 gst_flow_get_name (enc->srcresult));
595 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
597 gst_buffer_unref (buffer);
603 gst_mpeg2enc_get_property (GObject * object,
604 guint prop_id, GValue * value, GParamSpec * pspec)
606 GST_MPEG2ENC (object)->options->getProperty (prop_id, value);
610 gst_mpeg2enc_set_property (GObject * object,
611 guint prop_id, const GValue * value, GParamSpec * pspec)
613 GST_MPEG2ENC (object)->options->setProperty (prop_id, value);
617 gst_mpeg2enc_src_activate_push (GstPad * pad, gboolean active)
619 gboolean result = TRUE;
622 enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
625 /* setcaps will start task once encoder is setup */
627 /* can only end the encoding loop by forcing eos */
628 GST_MPEG2ENC_MUTEX_LOCK (enc);
630 enc->srcresult = GST_FLOW_WRONG_STATE;
631 GST_MPEG2ENC_SIGNAL (enc);
632 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
634 /* encoding loop should have ended now and can be joined */
635 result = gst_pad_stop_task (pad);
641 static GstStateChangeReturn
642 gst_mpeg2enc_change_state (GstElement * element, GstStateChange transition)
644 GstMpeg2enc *enc = GST_MPEG2ENC (element);
645 GstStateChangeReturn ret;
647 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
648 if (ret == GST_STATE_CHANGE_FAILURE)
651 switch (transition) {
652 case GST_STATE_CHANGE_PAUSED_TO_READY:
653 gst_mpeg2enc_reset (enc);
663 #ifndef GST_DISABLE_GST_DEBUG
665 static mjpeg_log_handler_t old_handler = NULL;
667 /* note that this will affect all mjpegtools elements/threads */
669 gst_mpeg2enc_log_callback (log_level_t level, const char *message)
671 GstDebugLevel gst_level;
673 #if GST_MJPEGTOOLS_API >= 10903
674 static const gint mjpeg_log_error = mjpeg_loglev_t ("error");
675 static const gint mjpeg_log_warn = mjpeg_loglev_t ("warn");
676 static const gint mjpeg_log_info = mjpeg_loglev_t ("info");
677 static const gint mjpeg_log_debug = mjpeg_loglev_t ("debug");
679 static const gint mjpeg_log_error = LOG_ERROR;
680 static const gint mjpeg_log_warn = LOG_WARN;
681 static const gint mjpeg_log_info = LOG_INFO;
682 static const gint mjpeg_log_debug = LOG_DEBUG;
685 if (level == mjpeg_log_error) {
686 gst_level = GST_LEVEL_ERROR;
687 } else if (level == mjpeg_log_warn) {
688 gst_level = GST_LEVEL_WARNING;
689 } else if (level == mjpeg_log_info) {
690 gst_level = GST_LEVEL_INFO;
691 } else if (level == mjpeg_log_debug) {
692 gst_level = GST_LEVEL_DEBUG;
694 gst_level = GST_LEVEL_INFO;
697 /* message could have a % in it, do not segfault in such case */
698 gst_debug_log (mpeg2enc_debug, gst_level, "", "", 0, NULL, "%s", message);
700 /* chain up to the old handler;
701 * this could actually be a handler from another mjpegtools based
702 * plugin; in which case messages can come out double or from
703 * the wrong plugin (element)... */
704 old_handler (level, message);
709 plugin_init (GstPlugin * plugin)
711 #ifndef GST_DISABLE_GST_DEBUG
712 old_handler = mjpeg_log_set_handler (gst_mpeg2enc_log_callback);
713 g_assert (old_handler != NULL);
715 /* in any case, we do not want default handler output */
716 mjpeg_default_handler_verbosity (0);
718 return gst_element_register (plugin, "mpeg2enc",
719 GST_RANK_MARGINAL, GST_TYPE_MPEG2ENC);
722 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
725 "High-quality MPEG-1/2 video encoder",
726 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)