Merge remote-tracking branch 'origin/0.10'

Conflicts:
	common
	ext/ffmpeg/Makefile.am
	ext/ffmpeg/gstffmpegcfg.c
	ext/ffmpeg/gstffmpegcodecmap.c
	ext/ffmpeg/gstffmpegcodecmap.h
	ext/ffmpeg/gstffmpegdec.c
	ext/ffmpeg/gstffmpegenc.c
	ext/ffmpeg/gstffmpegenc.h
	tests/check/Makefile.am

Porting of the new video elements to 0.11 still pending.
This commit is contained in:
Sebastian Dröge 2012-06-14 12:41:34 +02:00
commit 4a58fd4be4
13 changed files with 3257 additions and 2902 deletions

View file

@ -11,7 +11,9 @@ libgstlibav_la_SOURCES = gstffmpeg.c \
gstffmpegcodecmap.c \
gstffmpegutils.c \
gstffmpegenc.c \
gstffmpegvidenc.c \
gstffmpegdec.c \
gstffmpegviddec.c \
gstffmpegcfg.c \
gstffmpegdemux.c \
gstffmpegmux.c \
@ -39,5 +41,6 @@ noinst_HEADERS = \
gstffmpegcodecmap.h \
gstffmpegutils.h \
gstffmpegenc.h \
gstffmpegvidenc.h \
gstffmpegcfg.h \
gstffmpegpipe.h

View file

@ -138,8 +138,10 @@ plugin_init (GstPlugin * plugin)
av_register_all ();
gst_ffmpegenc_register (plugin);
gst_ffmpegdec_register (plugin);
gst_ffmpegaudenc_register (plugin);
gst_ffmpegvidenc_register (plugin);
gst_ffmpegauddec_register (plugin);
gst_ffmpegviddec_register (plugin);
gst_ffmpegdemux_register (plugin);
gst_ffmpegmux_register (plugin);
gst_ffmpegdeinterlace_register (plugin);

View file

@ -44,8 +44,10 @@ extern gboolean _shut_up_I_am_probing;
#endif
extern gboolean gst_ffmpegdemux_register (GstPlugin * plugin);
extern gboolean gst_ffmpegdec_register (GstPlugin * plugin);
extern gboolean gst_ffmpegenc_register (GstPlugin * plugin);
extern gboolean gst_ffmpegauddec_register (GstPlugin * plugin);
extern gboolean gst_ffmpegviddec_register (GstPlugin * plugin);
extern gboolean gst_ffmpegaudenc_register (GstPlugin * plugin);
extern gboolean gst_ffmpegvidenc_register (GstPlugin * plugin);
extern gboolean gst_ffmpegmux_register (GstPlugin * plugin);
extern gboolean gst_ffmpegcsp_register (GstPlugin * plugin);
#if 0

View file

@ -26,7 +26,7 @@
#endif
#include "gstffmpeg.h"
#include "gstffmpegenc.h"
#include "gstffmpegvidenc.h"
#include "gstffmpegcfg.h"
#include <string.h>
@ -324,7 +324,7 @@ struct _GParamSpecData
/* properties whose member offset is higher than the config base
* can be copied directly at context configuration time;
* and can also retrieve a default value from lavc */
#define CONTEXT_CONFIG_OFFSET G_STRUCT_OFFSET (GstFFMpegEnc, config)
#define CONTEXT_CONFIG_OFFSET G_STRUCT_OFFSET (GstFFMpegVidEnc, config)
/* additional info is named pointer specified by the quark */
static GQuark quark;
@ -339,7 +339,7 @@ static GList *property_list;
default, include, exclude) \
G_STMT_START { \
GParamSpecData *_qdata = g_new0 (GParamSpecData, 1); \
GstFFMpegEnc _enc; \
GstFFMpegVidEnc _enc; \
_qdata->offset = G_STRUCT_OFFSET (struct_type, member); \
_qdata->size = sizeof (_enc.member); \
_qdata->lavc_default = default; \
@ -350,7 +350,7 @@ G_STMT_START { \
} G_STMT_END
#define gst_ffmpeg_add_pspec(pspec, member, default, include, exclude) \
gst_ffmpeg_add_pspec_full (pspec, property_list, GstFFMpegEnc, member, \
gst_ffmpeg_add_pspec_full (pspec, property_list, GstFFMpegVidEnc, member, \
default, include, exclude)
/* ==== BEGIN CONFIGURATION SECTION ==== */
@ -755,7 +755,7 @@ gst_ffmpeg_cfg_codec_has_pspec (enum CodecID codec_id, GParamSpec * pspec)
/* install all properties for klass that have been registered in property_list */
void
gst_ffmpeg_cfg_install_property (GstFFMpegEncClass * klass, guint base)
gst_ffmpeg_cfg_install_property (GstFFMpegVidEncClass * klass, guint base)
{
GParamSpec *pspec;
GList *list;
@ -878,7 +878,7 @@ gboolean
gst_ffmpeg_cfg_set_property (GObject * object,
const GValue * value, GParamSpec * pspec)
{
GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (object);
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) (object);
GParamSpecData *qdata;
qdata = g_param_spec_get_qdata (pspec, quark);
@ -941,7 +941,7 @@ gboolean
gst_ffmpeg_cfg_get_property (GObject * object,
GValue * value, GParamSpec * pspec)
{
GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (object);
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) (object);
GParamSpecData *qdata;
qdata = g_param_spec_get_qdata (pspec, quark);
@ -996,7 +996,7 @@ gst_ffmpeg_cfg_get_property (GObject * object,
}
void
gst_ffmpeg_cfg_set_defaults (GstFFMpegEnc * ffmpegenc)
gst_ffmpeg_cfg_set_defaults (GstFFMpegVidEnc * ffmpegenc)
{
GParamSpec **pspecs;
guint num_props, i;
@ -1024,10 +1024,11 @@ gst_ffmpeg_cfg_set_defaults (GstFFMpegEnc * ffmpegenc)
void
gst_ffmpeg_cfg_fill_context (GstFFMpegEnc * ffmpegenc, AVCodecContext * context)
gst_ffmpeg_cfg_fill_context (GstFFMpegVidEnc * ffmpegenc,
AVCodecContext * context)
{
GstFFMpegEncClass *klass
= (GstFFMpegEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
GstFFMpegVidEncClass *klass
= (GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
GParamSpec *pspec;
GParamSpecData *qdata;
GList *list;
@ -1059,7 +1060,7 @@ gst_ffmpeg_cfg_fill_context (GstFFMpegEnc * ffmpegenc, AVCodecContext * context)
}
void
gst_ffmpeg_cfg_finalize (GstFFMpegEnc * ffmpegenc)
gst_ffmpeg_cfg_finalize (GstFFMpegVidEnc * ffmpegenc)
{
GParamSpec **pspecs;
guint num_props, i;

View file

@ -25,7 +25,7 @@ G_BEGIN_DECLS
void gst_ffmpeg_cfg_init (void);
void gst_ffmpeg_cfg_install_property (GstFFMpegEncClass * klass, guint base);
void gst_ffmpeg_cfg_install_property (GstFFMpegVidEncClass * klass, guint base);
gboolean gst_ffmpeg_cfg_set_property (GObject * object,
const GValue * value, GParamSpec * pspec);
@ -33,9 +33,9 @@ gboolean gst_ffmpeg_cfg_set_property (GObject * object,
gboolean gst_ffmpeg_cfg_get_property (GObject * object,
GValue * value, GParamSpec * pspec);
void gst_ffmpeg_cfg_fill_context (GstFFMpegEnc * ffmpegenc, AVCodecContext * context);
void gst_ffmpeg_cfg_set_defaults (GstFFMpegEnc * ffmpegenc);
void gst_ffmpeg_cfg_finalize (GstFFMpegEnc * ffmpegenc);
void gst_ffmpeg_cfg_fill_context (GstFFMpegVidEnc * ffmpegenc, AVCodecContext * context);
void gst_ffmpeg_cfg_set_defaults (GstFFMpegVidEnc * ffmpegenc);
void gst_ffmpeg_cfg_finalize (GstFFMpegVidEnc * ffmpegenc);
G_END_DECLS

View file

@ -2076,6 +2076,128 @@ gst_ffmpeg_caps_to_pixfmt (const GstCaps * caps,
}
}
typedef struct
{
GstVideoFormat format;
enum PixelFormat pixfmt;
} PixToFmt;
/* FIXME : FILLME */
static const PixToFmt pixtofmttable[] = {
/* GST_VIDEO_FORMAT_I420, */
{GST_VIDEO_FORMAT_I420, PIX_FMT_YUV420P},
/* Note : this should use a different chroma placement */
{GST_VIDEO_FORMAT_I420, PIX_FMT_YUVJ420P},
/* GST_VIDEO_FORMAT_YV12, */
/* GST_VIDEO_FORMAT_YUY2, */
{GST_VIDEO_FORMAT_YUY2, PIX_FMT_YUYV422},
/* GST_VIDEO_FORMAT_UYVY, */
{GST_VIDEO_FORMAT_UYVY, PIX_FMT_UYVY422},
/* GST_VIDEO_FORMAT_AYUV, */
/* GST_VIDEO_FORMAT_RGBx, */
/* GST_VIDEO_FORMAT_BGRx, */
/* GST_VIDEO_FORMAT_xRGB, */
/* GST_VIDEO_FORMAT_xBGR, */
/* GST_VIDEO_FORMAT_RGBA, */
{GST_VIDEO_FORMAT_RGBA, PIX_FMT_RGB32},
/* GST_VIDEO_FORMAT_BGRA, */
{GST_VIDEO_FORMAT_BGRA, PIX_FMT_BGR32},
/* GST_VIDEO_FORMAT_ARGB, */
/* GST_VIDEO_FORMAT_ABGR, */
/* GST_VIDEO_FORMAT_RGB, */
{GST_VIDEO_FORMAT_RGB, PIX_FMT_RGB24},
/* GST_VIDEO_FORMAT_BGR, */
{GST_VIDEO_FORMAT_BGR, PIX_FMT_BGR24},
/* GST_VIDEO_FORMAT_Y41B, */
{GST_VIDEO_FORMAT_Y41B, PIX_FMT_YUV410P},
/* GST_VIDEO_FORMAT_Y42B, */
{GST_VIDEO_FORMAT_Y42B, PIX_FMT_YUV422P},
{GST_VIDEO_FORMAT_Y42B, PIX_FMT_YUVJ422P},
/* GST_VIDEO_FORMAT_YVYU, */
/* GST_VIDEO_FORMAT_Y444, */
{GST_VIDEO_FORMAT_Y444, PIX_FMT_YUV444P},
{GST_VIDEO_FORMAT_Y444, PIX_FMT_YUVJ444P},
/* GST_VIDEO_FORMAT_v210, */
/* GST_VIDEO_FORMAT_v216, */
/* GST_VIDEO_FORMAT_NV12, */
{GST_VIDEO_FORMAT_NV12, PIX_FMT_NV12},
/* GST_VIDEO_FORMAT_NV21, */
{GST_VIDEO_FORMAT_NV21, PIX_FMT_NV21},
/* GST_VIDEO_FORMAT_GRAY8, */
{GST_VIDEO_FORMAT_GRAY8, PIX_FMT_GRAY8},
/* GST_VIDEO_FORMAT_GRAY16_BE, */
{GST_VIDEO_FORMAT_GRAY16_BE, PIX_FMT_GRAY16BE},
/* GST_VIDEO_FORMAT_GRAY16_LE, */
{GST_VIDEO_FORMAT_GRAY16_LE, PIX_FMT_GRAY16LE},
/* GST_VIDEO_FORMAT_v308, */
/* GST_VIDEO_FORMAT_Y800, */
/* GST_VIDEO_FORMAT_Y16, */
/* GST_VIDEO_FORMAT_RGB16, */
{GST_VIDEO_FORMAT_RGB16, PIX_FMT_RGB565},
/* GST_VIDEO_FORMAT_BGR16, */
/* GST_VIDEO_FORMAT_RGB15, */
{GST_VIDEO_FORMAT_RGB15, PIX_FMT_RGB555},
/* GST_VIDEO_FORMAT_BGR15, */
/* GST_VIDEO_FORMAT_UYVP, */
/* GST_VIDEO_FORMAT_A420, */
{GST_VIDEO_FORMAT_A420, PIX_FMT_YUVA420P},
/* GST_VIDEO_FORMAT_RGB8_PALETTED, */
{GST_VIDEO_FORMAT_RGB8P, PIX_FMT_PAL8},
/* GST_VIDEO_FORMAT_YUV9, */
/* GST_VIDEO_FORMAT_YVU9, */
/* GST_VIDEO_FORMAT_IYU1, */
/* GST_VIDEO_FORMAT_ARGB64, */
/* GST_VIDEO_FORMAT_AYUV64, */
/* GST_VIDEO_FORMAT_r210, */
};
GstVideoFormat
gst_ffmpeg_pixfmt_to_videoformat (enum PixelFormat pixfmt)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (pixtofmttable); i++)
if (pixtofmttable[i].pixfmt == pixfmt)
return pixtofmttable[i].format;
GST_WARNING ("Unknown pixel format %d", pixfmt);
return GST_VIDEO_FORMAT_UNKNOWN;
}
enum PixelFormat
gst_ffmpeg_videoformat_to_pixfmt (GstVideoFormat format)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (pixtofmttable); i++)
if (pixtofmttable[i].format == format)
return pixtofmttable[i].pixfmt;
return PIX_FMT_NONE;
}
void
gst_ffmpeg_videoinfo_to_context (GstVideoInfo * info, AVCodecContext * context)
{
gint i, bpp = 0;
context->width = GST_VIDEO_INFO_WIDTH (info);
context->height = GST_VIDEO_INFO_HEIGHT (info);
for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (info); i++)
bpp += GST_VIDEO_INFO_COMP_DEPTH (info, i);
context->bits_per_coded_sample = bpp;
context->ticks_per_frame = 1;
context->time_base.den = GST_VIDEO_INFO_FPS_N (info);
context->time_base.num = GST_VIDEO_INFO_FPS_D (info);
context->sample_aspect_ratio.num = GST_VIDEO_INFO_PAR_N (info);
context->sample_aspect_ratio.den = GST_VIDEO_INFO_PAR_D (info);
context->pix_fmt =
gst_ffmpeg_videoformat_to_pixfmt (GST_VIDEO_INFO_FORMAT (info));
}
/* Convert a GstCaps and a FFMPEG codec Type to a
* AVCodecContext. If the context is ommitted, no fixed values
* for video/audio size will be included in the context

View file

@ -91,6 +91,13 @@ gst_ffmpeg_caps_with_codectype (enum AVMediaType type,
const GstCaps *caps,
AVCodecContext *context);
void
gst_ffmpeg_videoinfo_to_context (GstVideoInfo *info,
AVCodecContext *context);
GstVideoFormat gst_ffmpeg_pixfmt_to_videoformat (enum PixelFormat pixfmt);
enum PixelFormat gst_ffmpeg_videoformat_to_pixfmt (GstVideoFormat format);
/*
* _formatid_to_caps () is meant for muxers/demuxers, it
* transforms a name (ffmpeg way of ID'ing these, why don't

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -21,16 +21,16 @@
* object definition and other useful things.
*/
#ifndef __GST_FFMPEGENC_H__
#define __GST_FFMPEGENC_H__
#ifndef __GST_FFMPEGAUDENC_H__
#define __GST_FFMPEGAUDENC_H__
G_BEGIN_DECLS
#include <gst/base/gstadapter.h>
typedef struct _GstFFMpegEnc GstFFMpegEnc;
typedef struct _GstFFMpegAudEnc GstFFMpegAudEnc;
struct _GstFFMpegEnc
struct _GstFFMpegAudEnc
{
GstElement element;
@ -39,7 +39,6 @@ struct _GstFFMpegEnc
GstPad *sinkpad;
AVCodecContext *context;
AVFrame *picture;
gboolean opened;
GstClockTime adapter_ts;
guint64 adapter_consumed;
@ -48,39 +47,17 @@ struct _GstFFMpegEnc
/* cache */
gint bitrate;
gint me_method;
gint gop_size;
gint buffer_size;
gint rtp_payload_size;
guint8 *working_buf;
gulong working_buf_size;
/* settings with some special handling */
guint pass;
gfloat quantizer;
gchar *filename;
guint lmin;
guint lmax;
gint max_key_interval;
gboolean interlaced;
/* statistics file */
FILE *file;
/* for b-frame delay handling */
GQueue *delay;
/* other settings are copied over straight,
* include a context here, rather than copy-and-past it from avcodec.h */
AVCodecContext config;
gboolean force_keyframe;
};
typedef struct _GstFFMpegEncClass GstFFMpegEncClass;
typedef struct _GstFFMpegAudEncClass GstFFMpegAudEncClass;
struct _GstFFMpegEncClass
struct _GstFFMpegAudEncClass
{
GstElementClass parent_class;
@ -89,17 +66,17 @@ struct _GstFFMpegEncClass
GstCaps *sinkcaps;
};
#define GST_TYPE_FFMPEGENC \
(gst_ffmpegenc_get_type())
#define GST_FFMPEGENC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGENC,GstFFMpegEnc))
#define GST_FFMPEGENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGENC,GstFFMpegEncClass))
#define GST_IS_FFMPEGENC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGENC))
#define GST_IS_FFMPEGENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGENC))
#define GST_TYPE_FFMPEGAUDENC \
(gst_ffmpegaudenc_get_type())
#define GST_FFMPEGAUDENC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGAUDENC,GstFFMpegAudEnc))
#define GST_FFMPEGAUDENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGAUDENC,GstFFMpegAudEncClass))
#define GST_IS_FFMPEGAUDENC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGAUDENC))
#define GST_IS_FFMPEGAUDENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGAUDENC))
G_END_DECLS
#endif /* __GST_FFMPEGENC_H__ */
#endif /* __GST_FFMPEGAUDENC_H__ */

1620
ext/ffmpeg/gstffmpegviddec.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,987 @@
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
*
* 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 <assert.h>
#include <string.h>
/* for stats file handling */
#include <stdio.h>
#include <glib/gstdio.h>
#include <errno.h>
#ifdef HAVE_FFMPEG_UNINSTALLED
#include <avcodec.h>
#else
#include <libavcodec/avcodec.h>
#endif
#include <gst/gst.h>
#include "gstffmpeg.h"
#include "gstffmpegcodecmap.h"
#include "gstffmpegutils.h"
#include "gstffmpegvidenc.h"
#include "gstffmpegcfg.h"
#define DEFAULT_VIDEO_BITRATE 300000 /* in bps */
#define DEFAULT_VIDEO_GOP_SIZE 15
#define DEFAULT_WIDTH 352
#define DEFAULT_HEIGHT 288
#define VIDEO_BUFFER_SIZE (1024*1024)
enum
{
/* FILL ME */
LAST_SIGNAL
};
enum
{
ARG_0,
ARG_BIT_RATE,
ARG_GOP_SIZE,
ARG_ME_METHOD,
ARG_BUFSIZE,
ARG_RTP_PAYLOAD_SIZE,
ARG_CFG_BASE
};
#define GST_TYPE_ME_METHOD (gst_ffmpegvidenc_me_method_get_type())
static GType
gst_ffmpegvidenc_me_method_get_type (void)
{
static GType ffmpegenc_me_method_type = 0;
static GEnumValue ffmpegenc_me_methods[] = {
{ME_ZERO, "None (Very low quality)", "zero"},
{ME_FULL, "Full (Slow, unmaintained)", "full"},
{ME_LOG, "Logarithmic (Low quality, unmaintained)", "logarithmic"},
{ME_PHODS, "phods (Low quality, unmaintained)", "phods"},
{ME_EPZS, "EPZS (Best quality, Fast)", "epzs"},
{ME_X1, "X1 (Experimental)", "x1"},
{0, NULL, NULL},
};
if (!ffmpegenc_me_method_type) {
ffmpegenc_me_method_type =
g_enum_register_static ("GstFFMpegVidEncMeMethod",
ffmpegenc_me_methods);
}
return ffmpegenc_me_method_type;
}
/* A number of function prototypes are given so we can refer to them later. */
static void gst_ffmpegvidenc_class_init (GstFFMpegVidEncClass * klass);
static void gst_ffmpegvidenc_base_init (GstFFMpegVidEncClass * klass);
static void gst_ffmpegvidenc_init (GstFFMpegVidEnc * ffmpegenc);
static void gst_ffmpegvidenc_finalize (GObject * object);
static gboolean gst_ffmpegvidenc_stop (GstVideoEncoder * encoder);
static GstFlowReturn gst_ffmpegvidenc_finish (GstVideoEncoder * encoder);
static gboolean gst_ffmpegvidenc_set_format (GstVideoEncoder * encoder,
GstVideoCodecState * state);
static GstCaps *gst_ffmpegvidenc_getcaps (GstVideoEncoder * encoder);
static GstFlowReturn gst_ffmpegvidenc_handle_frame (GstVideoEncoder * encoder,
GstVideoCodecFrame * frame);
static void gst_ffmpegvidenc_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_ffmpegvidenc_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
#define GST_FFENC_PARAMS_QDATA g_quark_from_static_string("ffenc-params")
static GstElementClass *parent_class = NULL;
/*static guint gst_ffmpegvidenc_signals[LAST_SIGNAL] = { 0 }; */
static void
gst_ffmpegvidenc_base_init (GstFFMpegVidEncClass * klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
AVCodec *in_plugin;
GstPadTemplate *srctempl = NULL, *sinktempl = NULL;
GstCaps *srccaps = NULL, *sinkcaps = NULL;
gchar *longname, *description;
in_plugin =
(AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
GST_FFENC_PARAMS_QDATA);
g_assert (in_plugin != NULL);
/* construct the element details struct */
longname = g_strdup_printf ("FFmpeg %s encoder", in_plugin->long_name);
description = g_strdup_printf ("FFmpeg %s encoder", in_plugin->name);
gst_element_class_set_details_simple (element_class, longname,
"Codec/Encoder/Video", description,
"Wim Taymans <wim.taymans@gmail.com>, "
"Ronald Bultje <rbultje@ronald.bitfreak.net>");
g_free (longname);
g_free (description);
if (!(srccaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, TRUE))) {
GST_DEBUG ("Couldn't get source caps for encoder '%s'", in_plugin->name);
srccaps = gst_caps_new_simple ("unknown/unknown", NULL);
}
sinkcaps = gst_caps_from_string
("video/x-raw-rgb; video/x-raw-yuv; video/x-raw-gray");
/* pad templates */
sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
GST_PAD_ALWAYS, sinkcaps);
srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
gst_element_class_add_pad_template (element_class, srctempl);
gst_element_class_add_pad_template (element_class, sinktempl);
klass->in_plugin = in_plugin;
klass->srctempl = srctempl;
klass->sinktempl = sinktempl;
klass->sinkcaps = NULL;
return;
}
static void
gst_ffmpegvidenc_class_init (GstFFMpegVidEncClass * klass)
{
GObjectClass *gobject_class;
GstVideoEncoderClass *venc_class;
gobject_class = (GObjectClass *) klass;
venc_class = (GstVideoEncoderClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gobject_class->set_property = gst_ffmpegvidenc_set_property;
gobject_class->get_property = gst_ffmpegvidenc_get_property;
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BIT_RATE,
g_param_spec_ulong ("bitrate", "Bit Rate",
"Target Video Bitrate", 0, G_MAXULONG, DEFAULT_VIDEO_BITRATE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GOP_SIZE,
g_param_spec_int ("gop-size", "GOP Size",
"Number of frames within one GOP", 0, G_MAXINT,
DEFAULT_VIDEO_GOP_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ME_METHOD,
g_param_spec_enum ("me-method", "ME Method", "Motion Estimation Method",
GST_TYPE_ME_METHOD, ME_EPZS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/* FIXME 0.11: Make this property read-only */
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFSIZE,
g_param_spec_ulong ("buffer-size", "Buffer Size",
"Size of the video buffers. "
"Note: Setting this property has no effect "
"and is deprecated!", 0, G_MAXULONG, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass),
ARG_RTP_PAYLOAD_SIZE, g_param_spec_ulong ("rtp-payload-size",
"RTP Payload Size", "Target GOB length", 0, G_MAXULONG, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/* register additional properties, possibly dependent on the exact CODEC */
gst_ffmpeg_cfg_install_property (klass, ARG_CFG_BASE);
venc_class->stop = gst_ffmpegvidenc_stop;
venc_class->finish = gst_ffmpegvidenc_finish;
venc_class->handle_frame = gst_ffmpegvidenc_handle_frame;
venc_class->getcaps = gst_ffmpegvidenc_getcaps;
venc_class->set_format = gst_ffmpegvidenc_set_format;
gobject_class->finalize = gst_ffmpegvidenc_finalize;
}
static void
gst_ffmpegvidenc_init (GstFFMpegVidEnc * ffmpegenc)
{
/* ffmpeg objects */
ffmpegenc->context = avcodec_alloc_context ();
ffmpegenc->picture = avcodec_alloc_frame ();
ffmpegenc->opened = FALSE;
ffmpegenc->file = NULL;
ffmpegenc->bitrate = DEFAULT_VIDEO_BITRATE;
ffmpegenc->me_method = ME_EPZS;
ffmpegenc->buffer_size = 512 * 1024;
ffmpegenc->gop_size = DEFAULT_VIDEO_GOP_SIZE;
ffmpegenc->rtp_payload_size = 0;
ffmpegenc->lmin = 2;
ffmpegenc->lmax = 31;
ffmpegenc->max_key_interval = 0;
gst_ffmpeg_cfg_set_defaults (ffmpegenc);
}
static void
gst_ffmpegvidenc_finalize (GObject * object)
{
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) object;
gst_ffmpeg_cfg_finalize (ffmpegenc);
/* close old session */
if (ffmpegenc->opened) {
gst_ffmpeg_avcodec_close (ffmpegenc->context);
ffmpegenc->opened = FALSE;
}
/* clean up remaining allocated data */
av_free (ffmpegenc->context);
av_free (ffmpegenc->picture);
g_free (ffmpegenc->filename);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static GstCaps *
gst_ffmpegvidenc_getcaps (GstVideoEncoder * encoder)
{
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
GstFFMpegVidEncClass *oclass =
(GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
AVCodecContext *ctx = NULL;
enum PixelFormat pixfmt;
GstCaps *caps = NULL;
gint i;
GST_DEBUG_OBJECT (ffmpegenc, "getting caps");
/* cached */
if (oclass->sinkcaps) {
caps = gst_video_encoder_proxy_getcaps (encoder, oclass->sinkcaps);
GST_DEBUG_OBJECT (ffmpegenc, "return cached caps %" GST_PTR_FORMAT, caps);
return caps;
}
/* create cache etc. */
/* shut up the logging while we autoprobe; we don't want warnings and
* errors about unsupported formats */
/* FIXME: if someone cares about this disabling the logging for other
* instances/threads/..., one could investigate if there is a way to
* set this as a struct member on the av context, and check it from the
* log handler */
#ifndef GST_DISABLE_GST_DEBUG
_shut_up_I_am_probing = TRUE;
#endif
GST_DEBUG_OBJECT (ffmpegenc, "probing caps");
i = pixfmt = 0;
/* check pixfmt until deemed finished */
for (pixfmt = 0;; pixfmt++) {
GstCaps *tmpcaps;
/* override looping all pixfmt if codec declares pixfmts;
* these may not properly check and report supported pixfmt during _init */
if (oclass->in_plugin->pix_fmts) {
if ((pixfmt = oclass->in_plugin->pix_fmts[i++]) == PIX_FMT_NONE) {
GST_DEBUG_OBJECT (ffmpegenc,
"At the end of official pixfmt for this codec, breaking out");
break;
}
GST_DEBUG_OBJECT (ffmpegenc,
"Got an official pixfmt [%d], attempting to get caps", pixfmt);
tmpcaps = gst_ffmpeg_pixfmt_to_caps (pixfmt, NULL, oclass->in_plugin->id);
if (tmpcaps) {
GST_DEBUG_OBJECT (ffmpegenc, "Got caps, breaking out");
if (!caps)
caps = gst_caps_new_empty ();
gst_caps_append (caps, tmpcaps);
continue;
}
GST_DEBUG_OBJECT (ffmpegenc,
"Couldn't figure out caps without context, trying again with a context");
}
GST_DEBUG_OBJECT (ffmpegenc, "pixfmt :%d", pixfmt);
if (pixfmt >= PIX_FMT_NB) {
GST_WARNING ("Invalid pixfmt, breaking out");
break;
}
/* need to start with a fresh codec_context each time around, since
* codec_close may have released stuff causing the next pass to segfault */
ctx = avcodec_alloc_context ();
if (!ctx) {
GST_DEBUG_OBJECT (ffmpegenc, "no context");
break;
}
/* set some default properties */
ctx->width = DEFAULT_WIDTH;
ctx->height = DEFAULT_HEIGHT;
ctx->time_base.num = 1;
ctx->time_base.den = 25;
ctx->ticks_per_frame = 1;
ctx->bit_rate = DEFAULT_VIDEO_BITRATE;
/* makes it silent */
ctx->strict_std_compliance = -1;
ctx->pix_fmt = pixfmt;
GST_DEBUG ("Attempting to open codec");
if (gst_ffmpeg_avcodec_open (ctx, oclass->in_plugin) >= 0 &&
ctx->pix_fmt == pixfmt) {
ctx->width = -1;
if (!caps)
caps = gst_caps_new_empty ();
tmpcaps = gst_ffmpeg_codectype_to_caps (oclass->in_plugin->type, ctx,
oclass->in_plugin->id, TRUE);
if (tmpcaps)
gst_caps_append (caps, tmpcaps);
else
GST_LOG_OBJECT (ffmpegenc,
"Couldn't get caps for oclass->in_plugin->name:%s",
oclass->in_plugin->name);
gst_ffmpeg_avcodec_close (ctx);
} else {
GST_DEBUG_OBJECT (ffmpegenc, "Opening codec failed with pixfmt : %d",
pixfmt);
}
if (ctx->priv_data)
gst_ffmpeg_avcodec_close (ctx);
av_free (ctx);
}
#ifndef GST_DISABLE_GST_DEBUG
_shut_up_I_am_probing = FALSE;
#endif
oclass->sinkcaps = gst_video_encoder_proxy_getcaps (encoder, caps);
return gst_caps_ref (oclass->sinkcaps);
}
static gboolean
gst_ffmpegvidenc_set_format (GstVideoEncoder * encoder,
GstVideoCodecState * state)
{
GstCaps *other_caps;
GstCaps *allowed_caps;
GstCaps *icaps;
GstVideoCodecState *output_format;
enum PixelFormat pix_fmt;
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
GstFFMpegVidEncClass *oclass =
(GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
/* close old session */
if (ffmpegenc->opened) {
gst_ffmpeg_avcodec_close (ffmpegenc->context);
ffmpegenc->opened = FALSE;
#if 0
/* FIXME : Is this still needed with GstVideoEncoder ?? */
/* fixed src caps;
* so clear src caps for proper (re-)negotiation */
gst_pad_set_caps (ffmpegenc->srcpad, NULL);
#endif
}
/* set defaults */
avcodec_get_context_defaults (ffmpegenc->context);
/* if we set it in _getcaps we should set it also in _link */
ffmpegenc->context->strict_std_compliance = -1;
/* user defined properties */
ffmpegenc->context->bit_rate = ffmpegenc->bitrate;
ffmpegenc->context->bit_rate_tolerance = ffmpegenc->bitrate;
ffmpegenc->context->gop_size = ffmpegenc->gop_size;
ffmpegenc->context->me_method = ffmpegenc->me_method;
GST_DEBUG_OBJECT (ffmpegenc, "Setting avcontext to bitrate %lu, gop_size %d",
ffmpegenc->bitrate, ffmpegenc->gop_size);
/* RTP payload used for GOB production (for Asterisk) */
if (ffmpegenc->rtp_payload_size) {
ffmpegenc->context->rtp_payload_size = ffmpegenc->rtp_payload_size;
}
/* additional avcodec settings */
/* first fill in the majority by copying over */
gst_ffmpeg_cfg_fill_context (ffmpegenc, ffmpegenc->context);
/* then handle some special cases */
ffmpegenc->context->lmin = (ffmpegenc->lmin * FF_QP2LAMBDA + 0.5);
ffmpegenc->context->lmax = (ffmpegenc->lmax * FF_QP2LAMBDA + 0.5);
if (ffmpegenc->interlaced) {
ffmpegenc->context->flags |=
CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME;
ffmpegenc->picture->interlaced_frame = TRUE;
/* if this is not the case, a filter element should be used to swap fields */
ffmpegenc->picture->top_field_first = TRUE;
}
/* some other defaults */
ffmpegenc->context->rc_strategy = 2;
ffmpegenc->context->b_frame_strategy = 0;
ffmpegenc->context->coder_type = 0;
ffmpegenc->context->context_model = 0;
ffmpegenc->context->scenechange_threshold = 0;
ffmpegenc->context->inter_threshold = 0;
/* and last but not least the pass; CBR, 2-pass, etc */
ffmpegenc->context->flags |= ffmpegenc->pass;
switch (ffmpegenc->pass) {
/* some additional action depends on type of pass */
case CODEC_FLAG_QSCALE:
ffmpegenc->context->global_quality
= ffmpegenc->picture->quality = FF_QP2LAMBDA * ffmpegenc->quantizer;
break;
case CODEC_FLAG_PASS1: /* need to prepare a stats file */
/* we don't close when changing caps, fingers crossed */
if (!ffmpegenc->file)
ffmpegenc->file = g_fopen (ffmpegenc->filename, "w");
if (!ffmpegenc->file)
goto open_file_err;
break;
case CODEC_FLAG_PASS2:
{ /* need to read the whole stats file ! */
gsize size;
if (!g_file_get_contents (ffmpegenc->filename,
&ffmpegenc->context->stats_in, &size, NULL))
goto file_read_err;
break;
}
default:
break;
}
GST_DEBUG_OBJECT (ffmpegenc, "Extracting common video information");
/* fetch pix_fmt, fps, par, width, height... */
gst_ffmpeg_videoinfo_to_context (&state->info, ffmpegenc->context);
if ((oclass->in_plugin->id == CODEC_ID_MPEG4)
&& (ffmpegenc->context->time_base.den > 65535)) {
/* MPEG4 Standards do not support time_base denominator greater than
* (1<<16) - 1 . We therefore scale them down.
* Agreed, it will not be the exact framerate... but the difference
* shouldn't be that noticeable */
ffmpegenc->context->time_base.num =
(gint) gst_util_uint64_scale_int (ffmpegenc->context->time_base.num,
65535, ffmpegenc->context->time_base.den);
ffmpegenc->context->time_base.den = 65535;
GST_LOG_OBJECT (ffmpegenc, "MPEG4 : scaled down framerate to %d / %d",
ffmpegenc->context->time_base.den, ffmpegenc->context->time_base.num);
}
pix_fmt = ffmpegenc->context->pix_fmt;
/* max-key-interval may need the framerate set above */
if (ffmpegenc->max_key_interval) {
AVCodecContext *ctx;
/* override gop-size */
ctx = ffmpegenc->context;
ctx->gop_size = (ffmpegenc->max_key_interval < 0) ?
(-ffmpegenc->max_key_interval
* (ctx->time_base.den * ctx->ticks_per_frame / ctx->time_base.num))
: ffmpegenc->max_key_interval;
}
/* open codec */
if (gst_ffmpeg_avcodec_open (ffmpegenc->context, oclass->in_plugin) < 0)
goto open_codec_fail;
/* second pass stats buffer no longer needed */
if (ffmpegenc->context->stats_in)
g_free (ffmpegenc->context->stats_in);
/* is the colourspace correct? */
if (pix_fmt != ffmpegenc->context->pix_fmt)
goto pix_fmt_err;
/* we may have failed mapping caps to a pixfmt,
* and quite some codecs do not make up their own mind about that
* in any case, _NONE can never work out later on */
if (pix_fmt == PIX_FMT_NONE)
goto bad_input_fmt;
/* some codecs support more than one format, first auto-choose one */
GST_DEBUG_OBJECT (ffmpegenc, "picking an output format ...");
allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
if (!allowed_caps) {
GST_DEBUG_OBJECT (ffmpegenc, "... but no peer, using template caps");
/* we need to copy because get_allowed_caps returns a ref, and
* get_pad_template_caps doesn't */
allowed_caps =
gst_caps_copy (gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD
(encoder)));
}
GST_DEBUG_OBJECT (ffmpegenc, "chose caps %" GST_PTR_FORMAT, allowed_caps);
gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
oclass->in_plugin->type, allowed_caps, ffmpegenc->context);
/* try to set this caps on the other side */
other_caps = gst_ffmpeg_codecid_to_caps (oclass->in_plugin->id,
ffmpegenc->context, TRUE);
if (!other_caps)
goto unsupported_codec;
icaps = gst_caps_intersect (allowed_caps, other_caps);
gst_caps_unref (allowed_caps);
gst_caps_unref (other_caps);
if (gst_caps_is_empty (icaps)) {
gst_caps_unref (icaps);
return FALSE;
}
if (gst_caps_get_size (icaps) > 1) {
GstCaps *newcaps;
newcaps =
gst_caps_new_full (gst_structure_copy (gst_caps_get_structure (icaps,
0)), NULL);
gst_caps_unref (icaps);
icaps = newcaps;
}
/* Store input state and set output state */
if (ffmpegenc->input_state)
gst_video_codec_state_unref (ffmpegenc->input_state);
ffmpegenc->input_state = gst_video_codec_state_ref (state);
output_format = gst_video_encoder_set_output_state (encoder, icaps, state);
gst_video_codec_state_unref (output_format);
/* success! */
ffmpegenc->opened = TRUE;
return TRUE;
/* ERRORS */
open_file_err:
{
GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, OPEN_WRITE,
(("Could not open file \"%s\" for writing."), ffmpegenc->filename),
GST_ERROR_SYSTEM);
return FALSE;
}
file_read_err:
{
GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, READ,
(("Could not get contents of file \"%s\"."), ffmpegenc->filename),
GST_ERROR_SYSTEM);
return FALSE;
}
open_codec_fail:
{
if (ffmpegenc->context->priv_data)
gst_ffmpeg_avcodec_close (ffmpegenc->context);
if (ffmpegenc->context->stats_in)
g_free (ffmpegenc->context->stats_in);
GST_DEBUG_OBJECT (ffmpegenc, "ffenc_%s: Failed to open FFMPEG codec",
oclass->in_plugin->name);
return FALSE;
}
pix_fmt_err:
{
gst_ffmpeg_avcodec_close (ffmpegenc->context);
GST_DEBUG_OBJECT (ffmpegenc,
"ffenc_%s: AV wants different colourspace (%d given, %d wanted)",
oclass->in_plugin->name, pix_fmt, ffmpegenc->context->pix_fmt);
return FALSE;
}
bad_input_fmt:
{
GST_DEBUG_OBJECT (ffmpegenc, "ffenc_%s: Failed to determine input format",
oclass->in_plugin->name);
return FALSE;
}
unsupported_codec:
{
gst_ffmpeg_avcodec_close (ffmpegenc->context);
GST_DEBUG ("Unsupported codec - no caps found");
return FALSE;
}
}
static void
ffmpegenc_setup_working_buf (GstFFMpegVidEnc * ffmpegenc)
{
guint wanted_size =
ffmpegenc->context->width * ffmpegenc->context->height * 6 +
FF_MIN_BUFFER_SIZE;
/* Above is the buffer size used by ffmpeg/ffmpeg.c */
if (ffmpegenc->working_buf == NULL ||
ffmpegenc->working_buf_size != wanted_size) {
if (ffmpegenc->working_buf)
g_free (ffmpegenc->working_buf);
ffmpegenc->working_buf_size = wanted_size;
ffmpegenc->working_buf = g_malloc (ffmpegenc->working_buf_size);
}
ffmpegenc->buffer_size = wanted_size;
}
static GstFlowReturn
gst_ffmpegvidenc_handle_frame (GstVideoEncoder * encoder,
GstVideoCodecFrame * frame)
{
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
GstBuffer *outbuf;
gint ret_size = 0, c;
GstVideoInfo *info = &ffmpegenc->input_state->info;
guint8 *data = GST_BUFFER_DATA (frame->input_buffer);
if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame))
ffmpegenc->picture->pict_type = FF_I_TYPE;
/* Fill avpicture */
for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
if (c < GST_VIDEO_INFO_N_COMPONENTS (info)) {
ffmpegenc->picture->data[c] = data + GST_VIDEO_INFO_COMP_OFFSET (info, c);
ffmpegenc->picture->linesize[c] = GST_VIDEO_INFO_COMP_STRIDE (info, c);
} else {
ffmpegenc->picture->data[c] = NULL;
ffmpegenc->picture->linesize[c] = 0;
}
}
ffmpegenc->picture->pts =
gst_ffmpeg_time_gst_to_ff (frame->pts /
ffmpegenc->context->ticks_per_frame, ffmpegenc->context->time_base);
ffmpegenc_setup_working_buf (ffmpegenc);
ret_size = avcodec_encode_video (ffmpegenc->context,
ffmpegenc->working_buf, ffmpegenc->working_buf_size, ffmpegenc->picture);
if (ret_size < 0)
goto encode_fail;
/* Encoder needs more data */
if (!ret_size)
return GST_FLOW_OK;
/* save stats info if there is some as well as a stats file */
if (ffmpegenc->file && ffmpegenc->context->stats_out)
if (fprintf (ffmpegenc->file, "%s", ffmpegenc->context->stats_out) < 0)
GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, WRITE,
(("Could not write to file \"%s\"."), ffmpegenc->filename),
GST_ERROR_SYSTEM);
/* Get oldest frame */
frame = gst_video_encoder_get_oldest_frame (encoder);
/* Allocate output buffer */
frame->output_buffer = outbuf = gst_buffer_new_and_alloc (ret_size);
memcpy (GST_BUFFER_DATA (outbuf), ffmpegenc->working_buf, ret_size);
/* buggy codec may not set coded_frame */
if (ffmpegenc->context->coded_frame) {
if (ffmpegenc->context->coded_frame->key_frame)
GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
} else
GST_WARNING_OBJECT (ffmpegenc, "codec did not provide keyframe info");
/* Reset frame type */
if (ffmpegenc->picture->pict_type)
ffmpegenc->picture->pict_type = 0;
return gst_video_encoder_finish_frame (encoder, frame);
/* ERRORS */
encode_fail:
{
#ifndef GST_DISABLE_GST_DEBUG
GstFFMpegVidEncClass *oclass =
(GstFFMpegVidEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
GST_ERROR_OBJECT (ffmpegenc,
"ffenc_%s: failed to encode buffer", oclass->in_plugin->name);
#endif /* GST_DISABLE_GST_DEBUG */
return GST_FLOW_OK;
}
}
static void
gst_ffmpegvidenc_flush_buffers (GstFFMpegVidEnc * ffmpegenc, gboolean send)
{
GstVideoCodecFrame *frame;
GstBuffer *outbuf;
gint ret_size;
GST_DEBUG_OBJECT (ffmpegenc, "flushing buffers with sending %d", send);
/* no need to empty codec if there is none */
if (!ffmpegenc->opened)
return;
while ((frame =
gst_video_encoder_get_oldest_frame (GST_VIDEO_ENCODER (ffmpegenc)))) {
ffmpegenc_setup_working_buf (ffmpegenc);
ret_size = avcodec_encode_video (ffmpegenc->context,
ffmpegenc->working_buf, ffmpegenc->working_buf_size, NULL);
if (ret_size < 0) { /* there should be something, notify and give up */
#ifndef GST_DISABLE_GST_DEBUG
GstFFMpegVidEncClass *oclass =
(GstFFMpegVidEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
GST_WARNING_OBJECT (ffmpegenc,
"ffenc_%s: failed to flush buffer", oclass->in_plugin->name);
#endif /* GST_DISABLE_GST_DEBUG */
break;
}
/* save stats info if there is some as well as a stats file */
if (ffmpegenc->file && ffmpegenc->context->stats_out)
if (fprintf (ffmpegenc->file, "%s", ffmpegenc->context->stats_out) < 0)
GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, WRITE,
(("Could not write to file \"%s\"."), ffmpegenc->filename),
GST_ERROR_SYSTEM);
frame->output_buffer = outbuf = gst_buffer_new_and_alloc (ret_size);
memcpy (GST_BUFFER_DATA (outbuf), ffmpegenc->working_buf, ret_size);
if (ffmpegenc->context->coded_frame->key_frame)
GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (ffmpegenc), frame);
}
}
static void
gst_ffmpegvidenc_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec)
{
GstFFMpegVidEnc *ffmpegenc;
/* Get a pointer of the right type. */
ffmpegenc = (GstFFMpegVidEnc *) (object);
if (ffmpegenc->opened) {
GST_WARNING_OBJECT (ffmpegenc,
"Can't change properties once decoder is setup !");
return;
}
/* Check the argument id to see which argument we're setting. */
switch (prop_id) {
case ARG_BIT_RATE:
ffmpegenc->bitrate = g_value_get_ulong (value);
break;
case ARG_GOP_SIZE:
ffmpegenc->gop_size = g_value_get_int (value);
break;
case ARG_ME_METHOD:
ffmpegenc->me_method = g_value_get_enum (value);
break;
case ARG_BUFSIZE:
break;
case ARG_RTP_PAYLOAD_SIZE:
ffmpegenc->rtp_payload_size = g_value_get_ulong (value);
break;
default:
if (!gst_ffmpeg_cfg_set_property (object, value, pspec))
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* The set function is simply the inverse of the get fuction. */
static void
gst_ffmpegvidenc_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
{
GstFFMpegVidEnc *ffmpegenc;
/* It's not null if we got it, but it might not be ours */
ffmpegenc = (GstFFMpegVidEnc *) (object);
switch (prop_id) {
case ARG_BIT_RATE:
g_value_set_ulong (value, ffmpegenc->bitrate);
break;
case ARG_GOP_SIZE:
g_value_set_int (value, ffmpegenc->gop_size);
break;
case ARG_ME_METHOD:
g_value_set_enum (value, ffmpegenc->me_method);
break;
case ARG_BUFSIZE:
g_value_set_ulong (value, ffmpegenc->buffer_size);
break;
case ARG_RTP_PAYLOAD_SIZE:
g_value_set_ulong (value, ffmpegenc->rtp_payload_size);
break;
default:
if (!gst_ffmpeg_cfg_get_property (object, value, pspec))
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_ffmpegvidenc_stop (GstVideoEncoder * encoder)
{
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
gst_ffmpegvidenc_flush_buffers (ffmpegenc, FALSE);
if (ffmpegenc->opened) {
gst_ffmpeg_avcodec_close (ffmpegenc->context);
ffmpegenc->opened = FALSE;
}
if (ffmpegenc->file) {
fclose (ffmpegenc->file);
ffmpegenc->file = NULL;
}
if (ffmpegenc->working_buf) {
g_free (ffmpegenc->working_buf);
ffmpegenc->working_buf = NULL;
}
return TRUE;
}
static GstFlowReturn
gst_ffmpegvidenc_finish (GstVideoEncoder * encoder)
{
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
gst_ffmpegvidenc_flush_buffers (ffmpegenc, TRUE);
return GST_FLOW_OK;
}
gboolean
gst_ffmpegvidenc_register (GstPlugin * plugin)
{
GTypeInfo typeinfo = {
sizeof (GstFFMpegVidEncClass),
(GBaseInitFunc) gst_ffmpegvidenc_base_init,
NULL,
(GClassInitFunc) gst_ffmpegvidenc_class_init,
NULL,
NULL,
sizeof (GstFFMpegVidEnc),
0,
(GInstanceInitFunc) gst_ffmpegvidenc_init,
};
GType type;
AVCodec *in_plugin;
GST_LOG ("Registering encoders");
/* build global ffmpeg param/property info */
gst_ffmpeg_cfg_init ();
in_plugin = av_codec_next (NULL);
while (in_plugin) {
gchar *type_name;
/* Skip non-AV codecs */
if (in_plugin->type != AVMEDIA_TYPE_VIDEO)
goto next;
/* no quasi codecs, please */
if (in_plugin->id == CODEC_ID_RAWVIDEO ||
in_plugin->id == CODEC_ID_V210 ||
in_plugin->id == CODEC_ID_V210X ||
in_plugin->id == CODEC_ID_R210 ||
in_plugin->id == CODEC_ID_ZLIB ||
(in_plugin->id >= CODEC_ID_PCM_S16LE &&
in_plugin->id <= CODEC_ID_PCM_BLURAY)) {
goto next;
}
/* No encoders depending on external libraries (we don't build them, but
* people who build against an external ffmpeg might have them.
* We have native gstreamer plugins for all of those libraries anyway. */
if (!strncmp (in_plugin->name, "lib", 3)) {
GST_DEBUG
("Not using external library encoder %s. Use the gstreamer-native ones instead.",
in_plugin->name);
goto next;
}
/* only encoders */
if (!in_plugin->encode) {
goto next;
}
/* FIXME : We should have a method to know cheaply whether we have a mapping
* for the given plugin or not */
GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
/* no codecs for which we're GUARANTEED to have better alternatives */
if (!strcmp (in_plugin->name, "gif")) {
GST_LOG ("Ignoring encoder %s", in_plugin->name);
goto next;
}
/* construct the type */
type_name = g_strdup_printf ("ffenc_%s", in_plugin->name);
type = g_type_from_name (type_name);
if (!type) {
/* create the glib type now */
type =
g_type_register_static (GST_TYPE_VIDEO_ENCODER, type_name, &typeinfo,
0);
g_type_set_qdata (type, GST_FFENC_PARAMS_QDATA, (gpointer) in_plugin);
{
static const GInterfaceInfo preset_info = {
NULL,
NULL,
NULL
};
g_type_add_interface_static (type, GST_TYPE_PRESET, &preset_info);
}
}
if (!gst_element_register (plugin, type_name, GST_RANK_SECONDARY, type)) {
g_free (type_name);
return FALSE;
}
g_free (type_name);
next:
in_plugin = av_codec_next (in_plugin);
}
GST_LOG ("Finished registering encoders");
return TRUE;
}

View file

@ -0,0 +1,95 @@
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
*
* 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.
*/
/* First, include the header file for the plugin, to bring in the
* object definition and other useful things.
*/
#ifndef __GST_FFMPEGVIDENC_H__
#define __GST_FFMPEGVIDENC_H__
G_BEGIN_DECLS
#include <gst/video/gstvideoencoder.h>
typedef struct _GstFFMpegVidEnc GstFFMpegVidEnc;
struct _GstFFMpegVidEnc
{
GstVideoEncoder parent;
GstVideoCodecState *input_state;
AVCodecContext *context;
AVFrame *picture;
gboolean opened;
gboolean discont;
/* cache */
gulong bitrate;
gint me_method;
gint gop_size;
gulong buffer_size;
gulong rtp_payload_size;
guint8 *working_buf;
gulong working_buf_size;
/* settings with some special handling */
guint pass;
gfloat quantizer;
gchar *filename;
guint lmin;
guint lmax;
gint max_key_interval;
gboolean interlaced;
/* statistics file */
FILE *file;
/* other settings are copied over straight,
* include a context here, rather than copy-and-past it from avcodec.h */
AVCodecContext config;
};
typedef struct _GstFFMpegVidEncClass GstFFMpegVidEncClass;
struct _GstFFMpegVidEncClass
{
GstVideoEncoderClass parent_class;
AVCodec *in_plugin;
GstPadTemplate *srctempl, *sinktempl;
GstCaps *sinkcaps;
};
#define GST_TYPE_FFMPEGVIDENC \
(gst_ffmpegvidenc_get_type())
#define GST_FFMPEGVIDENC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGVIDENC,GstFFMpegVidEnc))
#define GST_FFMPEGVIDENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGVIDENC,GstFFMpegVidEncClass))
#define GST_IS_FFMPEGVIDENC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGVIDENC))
#define GST_IS_FFMPEGVIDENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGVIDENC))
G_END_DECLS
#endif /* __GST_FFMPEGVIDENC_H__ */