Commit aa9969db authored by Ronald S. Bultje's avatar Ronald S. Bultje

This is a first attempt at a wrapper for the lib'ified mpeg2enc of mjpegtools....

This is a first attempt at a wrapper for the lib'ified mpeg2enc of mjpegtools. Currently, there's a few release candi...

Original commit message from CVS:
This is a first attempt at a wrapper for the lib'ified mpeg2enc of
mjpegtools. Currently, there's a few release candidates for mjpegtools-1.6.2
available, but no stable version yet.
I've made 4 small subclasses to wrap input, output, options and generic
encoding model. The last .cc file is the GStreamer plugin element.

Note that it doesn't actually work yet, I'm doing something wrong with
header parsing and Andrew asked me to commit so he could help debugging
that. Apart from that, we should soon be able to make top-quality MPEG
encodes! :).

mpeg2enc licensing is tricky, btw, I don't even want to start discussing
that...
parent 1c1767f8
......@@ -1029,6 +1029,16 @@ GST_CHECK_FEATURE(MPEG2DEC, [mpeg2dec], mpeg2dec, [
AC_SUBST(MPEG2DEC_LIBS)
])
dnl *** mpeg2enc ***
translit(dnm, m, l) AM_CONDITIONAL(USE_MPEG2ENC, true)
GST_CHECK_FEATURE(MPEG2ENC, [mpeg2enc], mpeg2enc, [
PKG_CHECK_MODULES(MPEG2ENC, mjpegtools >= 1.6.1.92,
HAVE_MPEG2ENC="yes", HAVE_MPEG2ENC="no")
MPEG2ENC_LIBS="$MPEG2ENC_LIBS -lmpeg2encpp"
AC_SUBST(MPEG2ENC_CFLAGS)
AC_SUBST(MPEG2ENC_LIBS)
])
dnl *** mplex ***
translit(dnm, m, l) AM_CONDITIONAL(USE_MPLEX, true)
GST_CHECK_FEATURE(MPLEX, [mplex], mplex, [HAVE_MPLEX=$HAVE_CXX])
......@@ -1460,6 +1470,7 @@ ext/mad/Makefile
ext/mas/Makefile
ext/mikmod/Makefile
ext/mpeg2dec/Makefile
ext/mpeg2enc/Makefile
ext/mplex/Makefile
ext/ogg/Makefile
ext/pango/Makefile
......
......@@ -190,6 +190,12 @@ else
MPEG2DEC_DIR=
endif
if USE_MPEG2ENC
MPEG2ENC_DIR=mpeg2enc
else
MPEG2ENC_DIR=
endif
if USE_MPLEX
MPLEX_DIR=mplex
else
......@@ -318,6 +324,7 @@ SUBDIRS=\
$(MAS_DIR) \
$(MIKMOD_DIR) \
$(MPEG2DEC_DIR) \
$(MPEG2ENC_DIR) \
$(MPLEX_DIR) \
$(OGG_DIR) \
$(PANGO_DIR) \
......@@ -367,6 +374,7 @@ DIST_SUBDIRS=\
mad \
mikmod \
mpeg2dec \
mpeg2enc \
mplex \
ogg \
pango \
......
plugin_LTLIBRARIES = libgstmpeg2enc.la
libgstmpeg2enc_la_SOURCES = \
gstmpeg2enc.cc \
gstmpeg2encoptions.cc \
gstmpeg2encoder.cc \
gstmpeg2encstreamwriter.cc \
gstmpeg2encpicturereader.cc
libgstmpeg2enc_la_CXXFLAGS = $(MPEG2ENC_CFLAGS) $(GST_CFLAGS)
libgstmpeg2enc_la_LIBADD = $(MPEG2ENC_LIBS)
libgstmpeg2enc_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
noinst_HEADERS = \
gstmpeg2enc.hh \
gstmpeg2encoder.hh \
gstmpeg2encoptions.hh \
gstmpeg2encstreamwriter.hh \
gstmpeg2encpicturereader.hh
/* GStreamer mpeg2enc (mjpegtools) wrapper
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* gstmpeg2enc.cc: gstreamer wrapping
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstmpeg2enc.hh"
GST_PAD_TEMPLATE_FACTORY (sink_templ,
"sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_CAPS_NEW (
"mpeg2enc_sink",
"video/x-raw-yuv",
"format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('I','4','2','0')),
"width", GST_PROPS_INT_RANGE (16, 4096),
"height", GST_PROPS_INT_RANGE (16, 4096),
"framerate", GST_PROPS_LIST (
GST_PROPS_FLOAT (24/1.001),
GST_PROPS_FLOAT (24.),
GST_PROPS_FLOAT (25.),
GST_PROPS_FLOAT (30/1.001),
GST_PROPS_FLOAT (30.),
GST_PROPS_FLOAT (50.),
GST_PROPS_FLOAT (60/1.001),
GST_PROPS_FLOAT (60.)
)
)
)
GST_PAD_TEMPLATE_FACTORY (src_templ,
"src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_CAPS_NEW (
"mpeg2enc_src",
"video/mpeg",
"systemstream", GST_PROPS_BOOLEAN (FALSE),
"mpegversion", GST_PROPS_INT_RANGE (1, 2),
"width", GST_PROPS_INT_RANGE (16, 4096),
"height", GST_PROPS_INT_RANGE (16, 4096),
"framerate", GST_PROPS_LIST (
GST_PROPS_FLOAT (24/1.001),
GST_PROPS_FLOAT (24.),
GST_PROPS_FLOAT (25.),
GST_PROPS_FLOAT (30/1.001),
GST_PROPS_FLOAT (30.),
GST_PROPS_FLOAT (50.),
GST_PROPS_FLOAT (60/1.001),
GST_PROPS_FLOAT (60.)
)
)
)
static void gst_mpeg2enc_base_init (GstMpeg2encClass *klass);
static void gst_mpeg2enc_class_init (GstMpeg2encClass *klass);
static void gst_mpeg2enc_init (GstMpeg2enc *enc);
static void gst_mpeg2enc_dispose (GObject *object);
static void gst_mpeg2enc_loop (GstElement *element);
static GstPadLinkReturn
gst_mpeg2enc_sink_link (GstPad *pad,
GstCaps *caps);
static GstCaps *
gst_mpeg2enc_src_getcaps (GstPad *pad,
GstCaps *caps);
static GstElementStateReturn
gst_mpeg2enc_change_state (GstElement *element);
static void gst_mpeg2enc_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gst_mpeg2enc_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static GstElementClass *parent_class = NULL;
GType
gst_mpeg2enc_get_type (void)
{
static GType gst_mpeg2enc_type = 0;
if (!gst_mpeg2enc_type) {
static const GTypeInfo gst_mpeg2enc_info = {
sizeof (GstMpeg2encClass),
(GBaseInitFunc) gst_mpeg2enc_base_init,
NULL,
(GClassInitFunc) gst_mpeg2enc_class_init,
NULL,
NULL,
sizeof (GstMpeg2enc),
0,
(GInstanceInitFunc) gst_mpeg2enc_init,
};
gst_mpeg2enc_type =
g_type_register_static (GST_TYPE_ELEMENT,
"GstMpeg2enc",
&gst_mpeg2enc_info,
(GTypeFlags) 0);
}
return gst_mpeg2enc_type;
}
static void
gst_mpeg2enc_base_init (GstMpeg2encClass *klass)
{
static GstElementDetails gst_mpeg2enc_details = {
"mpeg2enc video encoder",
"Codec/Video/Encoder",
"High-quality MPEG-1/2 video encoder",
"Ronald Bultje <rbultje@ronald.bitfreak.net>",
};
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gst_element_class_add_pad_template (element_class,
GST_PAD_TEMPLATE_GET (src_templ));
gst_element_class_add_pad_template (element_class,
GST_PAD_TEMPLATE_GET (sink_templ));
gst_element_class_set_details (element_class,
&gst_mpeg2enc_details);
}
static void
gst_mpeg2enc_class_init (GstMpeg2encClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
parent_class = GST_ELEMENT_CLASS (g_type_class_ref (GST_TYPE_ELEMENT));
/* register arguments */
mjpeg_default_handler_verbosity (0);
GstMpeg2EncOptions::initProperties (object_class);
object_class->set_property = gst_mpeg2enc_set_property;
object_class->get_property = gst_mpeg2enc_get_property;
object_class->dispose = gst_mpeg2enc_dispose;
element_class->change_state = gst_mpeg2enc_change_state;
}
static void
gst_mpeg2enc_dispose (GObject *object)
{
GstMpeg2enc *enc = GST_MPEG2ENC (object);
if (enc->encoder) {
delete enc->encoder;
enc->encoder = NULL;
}
delete enc->options;
}
static void
gst_mpeg2enc_init (GstMpeg2enc *enc)
{
GstElement *element = GST_ELEMENT (enc);
enc->sinkpad = gst_pad_new_from_template (
gst_element_get_pad_template (element, "sink"), "sink");
gst_pad_set_link_function (enc->sinkpad, gst_mpeg2enc_sink_link);
gst_element_add_pad (element, enc->sinkpad);
enc->srcpad = gst_pad_new_from_template (
gst_element_get_pad_template (element, "src"), "src");
gst_pad_set_getcaps_function (enc->srcpad, gst_mpeg2enc_src_getcaps);
gst_element_add_pad (element, enc->srcpad);
enc->options = new GstMpeg2EncOptions ();
gst_element_set_loop_function (element, gst_mpeg2enc_loop);
enc->encoder = NULL;
}
static void
gst_mpeg2enc_loop (GstElement *element)
{
GstMpeg2enc *enc = GST_MPEG2ENC (element);
if (!enc->encoder) {
GstCaps *caps;
/* create new encoder with these settings */
enc->encoder = new GstMpeg2Encoder (enc->options,
enc->sinkpad,
GST_PAD_CAPS (enc->sinkpad),
enc->srcpad);
/* and set caps on other side */
caps = enc->encoder->getFormat ();
if (gst_pad_try_set_caps (enc->srcpad, caps) <= 0) {
gst_element_error (element,
"Failed to set up encoder properly");
delete enc->encoder;
enc->encoder = NULL;
return;
}
}
enc->encoder->encodePicture ();
}
static GstPadLinkReturn
gst_mpeg2enc_sink_link (GstPad *pad,
GstCaps *caps)
{
GstMpeg2enc *enc = GST_MPEG2ENC (gst_pad_get_parent (pad));
if (!GST_CAPS_IS_FIXED (caps))
return GST_PAD_LINK_DELAYED;
if (enc->encoder) {
delete enc->encoder;
enc->encoder = NULL;
}
return GST_PAD_LINK_OK;
}
static GstCaps *
gst_mpeg2enc_src_getcaps (GstPad *pad,
GstCaps *caps)
{
GstMpeg2enc *enc = GST_MPEG2ENC (gst_pad_get_parent (pad));
if (enc->encoder) {
return enc->encoder->getFormat ();
}
return gst_caps_ref (gst_pad_template_get_caps (
gst_element_get_pad_template (gst_pad_get_parent (pad), "src")));
}
static void
gst_mpeg2enc_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GST_MPEG2ENC (object)->options->getProperty (prop_id, value);
}
static void
gst_mpeg2enc_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GST_MPEG2ENC (object)->options->setProperty (prop_id, value);
}
static GstElementStateReturn
gst_mpeg2enc_change_state (GstElement *element)
{
GstMpeg2enc *enc = GST_MPEG2ENC (element);
switch (GST_STATE_TRANSITION (element)) {
case GST_STATE_PAUSED_TO_READY:
delete enc->encoder;
enc->encoder = NULL;
break;
default:
break;
}
if (parent_class->change_state)
return parent_class->change_state (element);
return GST_STATE_SUCCESS;
}
static gboolean
plugin_init (GstPlugin *plugin)
{
return gst_element_register (plugin, "mpeg2enc",
GST_RANK_NONE,
GST_TYPE_MPEG2ENC);
}
GST_PLUGIN_DEFINE (
GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"mpeg2enc",
"High-quality MPEG-1/2 video encoder",
plugin_init,
VERSION,
"GPL",
GST_PACKAGE,
GST_ORIGIN
)
/* GStreamer mpeg2enc (mjpegtools) wrapper
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* gstmpeg2enc.hh: object definition
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_MPEG2ENC_H__
#define __GST_MPEG2ENC_H__
#include <gst/gst.h>
#include "gstmpeg2encoptions.hh"
#include "gstmpeg2encoder.hh"
G_BEGIN_DECLS
#define GST_TYPE_MPEG2ENC \
(gst_mpeg2enc_get_type ())
#define GST_MPEG2ENC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MPEG2ENC, GstMpeg2enc))
#define GST_MPEG2ENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MPEG2ENC, GstMpeg2enc))
#define GST_IS_MPEG2ENC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MPEG2ENC))
#define GST_IS_MPEG2ENC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MPEG2ENC))
typedef struct _GstMpeg2enc {
GstElement parent;
/* pads */
GstPad *sinkpad, *srcpad;
/* options wrapper */
GstMpeg2EncOptions *options;
/* general encoding object (contains rest) */
GstMpeg2Encoder *encoder;
} GstMpeg2enc;
typedef struct _GstMpeg2encClass {
GstElementClass parent;
} GstMpeg2encClass;
GType gst_mpeg2enc_get_type (void);
G_END_DECLS
#endif /* __GST_MPEG2ENC_H__ */
/* GStreamer mpeg2enc (mjpegtools) wrapper
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* gstmpeg2encoder.cc: gstreamer/mpeg2enc encoder class
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <mpegconsts.h>
#include <quantize.hh>
#include <ratectl.hh>
#include <seqencoder.hh>
#include <mpeg2coder.hh>
#include "gstmpeg2encoder.hh"
/*
* Class init stuff.
*/
GstMpeg2Encoder::GstMpeg2Encoder (GstMpeg2EncOptions *options,
GstPad *sinkpad,
GstCaps *caps,
GstPad *srcpad) :
MPEG2Encoder (*options)
{
MPEG2EncInVidParams strm;
/* I/O */
reader = new GstMpeg2EncPictureReader (sinkpad, caps, &parms);
reader->StreamPictureParams (strm);
if (options->SetFormatPresets (strm)) {
g_warning ("Eek! Format presets failed. This is really bad!");
}
writer = new GstMpeg2EncStreamWriter (srcpad, &parms);
/* encoding internals */
quantizer = new Quantizer (parms);
coder = new MPEG2Coder (parms, *writer);
bitrate_controller = new OnTheFlyRateCtl (parms);
/* sequencer */
seqencoder = new SeqEncoder (parms, *reader, *quantizer,
*writer, *coder, *bitrate_controller);
parms.Init (*options);
reader->Init ();
quantizer->Init ();
}
/*
* One image.
*/
void
GstMpeg2Encoder::encodePicture ()
{
/* hm, this is all... eek! */
seqencoder->Encode ();
}
/*
* Get current output format.
*/
GstCaps *
GstMpeg2Encoder::getFormat ()
{
gdouble fps = Y4M_RATIO_DBL (mpeg_framerate (options.frame_rate));
return GST_CAPS_NEW ("mpeg2enc_src",
"video/mpeg",
"systemstream", GST_PROPS_BOOLEAN (FALSE),
"mpegversion", GST_PROPS_INT (options.mpeg),
"width", GST_PROPS_INT (options.in_img_width),
"height", GST_PROPS_INT (options.in_img_height),
"framerate", GST_PROPS_FLOAT (fps));
}
/* GStreamer mpeg2enc (mjpegtools) wrapper
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* gstmpeg2encoder.hh: gstreamer/mpeg2enc encoder class
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_MPEG2ENCODER_H__
#define __GST_MPEG2ENCODER_H__
#include <mpeg2encoder.hh>
#include "gstmpeg2encoptions.hh"
#include "gstmpeg2encpicturereader.hh"
#include "gstmpeg2encstreamwriter.hh"
class GstMpeg2Encoder : public MPEG2Encoder {
public:
GstMpeg2Encoder (GstMpeg2EncOptions *options,
GstPad *sinkpad,
GstCaps *caps,
GstPad *srcpad);
/* one image */
void encodePicture ();
/* get current output format */
GstCaps *getFormat ();
};
#endif /* __GST_MPEG2ENCODER_H__ */
This diff is collapsed.
/* GStreamer mpeg2enc (mjpegtools) wrapper
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* gstmpeg2encoptions.hh: gobject/mpeg2enc option wrapping class
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_MPEG2ENCOPTIONS_H__
#define __GST_MPEG2ENCOPTIONS_H__
#include <glib-object.h>
#include <mpeg2encoptions.hh>
class GstMpeg2EncOptions : public MPEG2EncOptions {
public:
GstMpeg2EncOptions ();
/* Init properties (call once) */
static void initProperties (GObjectClass *klass);
/* GObject property foo, C++ style */
void getProperty (guint prop_id,
GValue *value);
void setProperty (guint prop_id,
const GValue *value);
};
#endif /* __GST_MPEG2ENCOPTIONS_H__ */
/* GStreamer mpeg2enc (mjpegtools) wrapper
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* gstmpeg2encpicturereader.cc: GStreamer/mpeg2enc input wrapper
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <encoderparams.hh>
#include "gstmpeg2encpicturereader.hh"
/*
* Class init stuff.
*/
GstMpeg2EncPictureReader::GstMpeg2EncPictureReader (GstPad *in_pad,
GstCaps *in_caps,
EncoderParams *params) :
PictureReader (*params)
{
pad = in_pad;
caps = gst_caps_copy (in_caps);
}
GstMpeg2EncPictureReader::~GstMpeg2EncPictureReader ()
{
gst_caps_unref (caps);
}
/*
* Get input picture parameters (width/height etc.).
*/
void
GstMpeg2EncPictureReader::StreamPictureParams (MPEG2EncInVidParams &strm)
{
gint width, height;