2002-03-20 21:44:42 +00:00
|
|
|
/* GStreamer
|
2001-12-23 13:25:04 +00:00
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
2012-11-20 09:36:29 +00:00
|
|
|
* Copyright (C) <2012> Collabora Ltd.
|
|
|
|
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
2001-12-23 13:25:04 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-04 00:22:16 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2001-12-23 13:25:04 +00:00
|
|
|
*/
|
|
|
|
|
2004-01-12 04:15:46 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2002-11-26 14:50:05 +00:00
|
|
|
#include "config.h"
|
2004-01-12 04:15:46 +00:00
|
|
|
#endif
|
2003-06-07 00:41:32 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
ext/ffmpeg/: Added helper object to handle extra properties.
Original commit message from CVS:
Patch by: Mark Nauwelaerts <manauw at skynet dot be>
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpegcfg.c: (gst_ffmpeg_pass_get_type),
(gst_ffmpeg_lim_pass_get_type), (gst_ffmpeg_mb_decision_get_type),
(gst_ffmpeg_mb_cmp_get_type), (gst_ffmpeg_dct_algo_get_type),
(gst_ffmpeg_idct_algo_get_type), (gst_ffmpeg_quant_type_get_type),
(gst_ffmpeg_pre_me_get_type), (gst_ffmpeg_pred_method_get_type),
(gst_ffmpeg_flags_get_type), (gst_ffmpeg_cfg_init),
(gst_ffmpeg_cfg_codec_has_pspec),
(gst_ffmpeg_cfg_install_property), (gst_ffmpeg_cfg_set_property),
(gst_ffmpeg_cfg_get_property), (gst_ffmpeg_cfg_set_defaults),
(gst_ffmpeg_cfg_fill_context):
* ext/ffmpeg/gstffmpegcfg.h:
Added helper object to handle extra properties.
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_dispose),
(gst_ffmpegenc_getcaps), (gst_ffmpegenc_setcaps),
(gst_ffmpegenc_chain_video), (gst_ffmpegenc_chain_audio),
(gst_ffmpegenc_flush_buffers), (gst_ffmpegenc_event_video),
(gst_ffmpegenc_set_property), (gst_ffmpegenc_get_property),
(gst_ffmpegenc_change_state), (gst_ffmpegenc_register):
* ext/ffmpeg/gstffmpegenc.h:
Exposes (quite some of) the AVCodecContext configurable
settings, as well as some additional (though simple) logic for b-frame
delay handling.
Most of the properties are only enabled for mpeg4-like codecs.
Fixes #344583.
2006-09-06 14:51:13 +00:00
|
|
|
/* for stats file handling */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <errno.h>
|
2003-06-07 00:41:32 +00:00
|
|
|
|
2008-10-30 12:05:45 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2012-09-10 15:53:54 +00:00
|
|
|
#include "gstav.h"
|
|
|
|
#include "gstavcodecmap.h"
|
|
|
|
#include "gstavutils.h"
|
2012-11-19 09:29:00 +00:00
|
|
|
#include "gstavaudenc.h"
|
2003-06-07 00:41:32 +00:00
|
|
|
|
2006-02-11 12:04:31 +00:00
|
|
|
#define DEFAULT_AUDIO_BITRATE 128000
|
|
|
|
|
2004-03-29 16:39:18 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-23 13:25:04 +00:00
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2004-03-29 16:39:18 +00:00
|
|
|
enum
|
|
|
|
{
|
2012-11-19 16:08:23 +00:00
|
|
|
PROP_0,
|
|
|
|
PROP_BIT_RATE,
|
|
|
|
PROP_RTP_PAYLOAD_SIZE,
|
2013-10-14 20:50:57 +00:00
|
|
|
PROP_COMPLIANCE,
|
2001-12-23 13:25:04 +00:00
|
|
|
};
|
|
|
|
|
2007-02-16 11:48:15 +00:00
|
|
|
/* A number of function prototypes are given so we can refer to them later. */
|
2012-04-07 09:14:45 +00:00
|
|
|
static void gst_ffmpegaudenc_class_init (GstFFMpegAudEncClass * klass);
|
|
|
|
static void gst_ffmpegaudenc_base_init (GstFFMpegAudEncClass * klass);
|
|
|
|
static void gst_ffmpegaudenc_init (GstFFMpegAudEnc * ffmpegaudenc);
|
|
|
|
static void gst_ffmpegaudenc_finalize (GObject * object);
|
2003-06-07 20:46:24 +00:00
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
static gboolean gst_ffmpegaudenc_set_format (GstAudioEncoder * encoder,
|
|
|
|
GstAudioInfo * info);
|
|
|
|
static GstFlowReturn gst_ffmpegaudenc_handle_frame (GstAudioEncoder * encoder,
|
|
|
|
GstBuffer * inbuf);
|
2013-08-29 14:53:28 +00:00
|
|
|
static gboolean gst_ffmpegaudenc_start (GstAudioEncoder * encoder);
|
2012-11-20 09:36:29 +00:00
|
|
|
static gboolean gst_ffmpegaudenc_stop (GstAudioEncoder * encoder);
|
2012-11-20 10:45:34 +00:00
|
|
|
static void gst_ffmpegaudenc_flush (GstAudioEncoder * encoder);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
static void gst_ffmpegaudenc_set_property (GObject * object,
|
2004-03-29 16:39:18 +00:00
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
2012-04-07 09:14:45 +00:00
|
|
|
static void gst_ffmpegaudenc_get_property (GObject * object,
|
2004-03-29 16:39:18 +00:00
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
|
2012-04-12 17:41:52 +00:00
|
|
|
#define GST_FFENC_PARAMS_QDATA g_quark_from_static_string("avenc-params")
|
2007-02-16 11:48:15 +00:00
|
|
|
|
2001-12-23 13:25:04 +00:00
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
/*static guint gst_ffmpegaudenc_signals[LAST_SIGNAL] = { 0 }; */
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2003-11-02 13:12:14 +00:00
|
|
|
static void
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_base_init (GstFFMpegAudEncClass * klass)
|
2003-11-02 13:12:14 +00:00
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2009-11-02 07:43:17 +00:00
|
|
|
AVCodec *in_plugin;
|
|
|
|
GstPadTemplate *srctempl = NULL, *sinktempl = NULL;
|
|
|
|
GstCaps *srccaps = NULL, *sinkcaps = NULL;
|
2012-04-07 09:14:45 +00:00
|
|
|
gchar *longname, *description;
|
2003-11-02 13:12:14 +00:00
|
|
|
|
2009-11-02 07:43:17 +00:00
|
|
|
in_plugin =
|
|
|
|
(AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
|
2007-02-16 11:48:15 +00:00
|
|
|
GST_FFENC_PARAMS_QDATA);
|
2009-11-02 07:43:17 +00:00
|
|
|
g_assert (in_plugin != NULL);
|
2003-11-02 13:12:14 +00:00
|
|
|
|
|
|
|
/* construct the element details struct */
|
2012-04-12 17:41:52 +00:00
|
|
|
longname = g_strdup_printf ("libav %s encoder", in_plugin->long_name);
|
|
|
|
description = g_strdup_printf ("libav %s encoder", in_plugin->name);
|
2012-04-09 23:59:24 +00:00
|
|
|
gst_element_class_set_metadata (element_class, longname,
|
2012-04-07 09:14:45 +00:00
|
|
|
"Codec/Encoder/Audio", description,
|
2010-03-24 10:51:26 +00:00
|
|
|
"Wim Taymans <wim.taymans@gmail.com>, "
|
|
|
|
"Ronald Bultje <rbultje@ronald.bitfreak.net>");
|
|
|
|
g_free (longname);
|
|
|
|
g_free (description);
|
2003-11-02 13:12:14 +00:00
|
|
|
|
2009-11-02 07:43:17 +00:00
|
|
|
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);
|
2011-10-28 14:35:54 +00:00
|
|
|
srccaps = gst_caps_new_empty_simple ("unknown/unknown");
|
2009-11-02 07:43:17 +00:00
|
|
|
}
|
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
sinkcaps = gst_ffmpeg_codectype_to_audio_caps (NULL,
|
|
|
|
in_plugin->id, TRUE, in_plugin);
|
2009-11-02 07:43:17 +00:00
|
|
|
if (!sinkcaps) {
|
|
|
|
GST_DEBUG ("Couldn't get sink caps for encoder '%s'", in_plugin->name);
|
2011-10-28 14:35:54 +00:00
|
|
|
sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
|
2009-11-02 07:43:17 +00:00
|
|
|
}
|
|
|
|
|
2003-11-02 13:12:14 +00:00
|
|
|
/* pad templates */
|
|
|
|
sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
|
2009-11-02 07:43:17 +00:00
|
|
|
GST_PAD_ALWAYS, sinkcaps);
|
|
|
|
srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
|
2003-11-02 13:12:14 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class, srctempl);
|
|
|
|
gst_element_class_add_pad_template (element_class, sinktempl);
|
|
|
|
|
2009-11-02 07:43:17 +00:00
|
|
|
klass->in_plugin = in_plugin;
|
2003-11-02 13:12:14 +00:00
|
|
|
klass->srctempl = srctempl;
|
|
|
|
klass->sinktempl = sinktempl;
|
2009-11-02 07:43:17 +00:00
|
|
|
|
|
|
|
return;
|
2003-11-02 13:12:14 +00:00
|
|
|
}
|
|
|
|
|
2001-12-23 13:25:04 +00:00
|
|
|
static void
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_class_init (GstFFMpegAudEncClass * klass)
|
2001-12-23 13:25:04 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2012-11-20 09:36:29 +00:00
|
|
|
GstAudioEncoderClass *gstaudioencoder_class;
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2004-03-29 16:39:18 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2012-11-20 09:36:29 +00:00
|
|
|
gstaudioencoder_class = (GstAudioEncoderClass *) klass;
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2006-04-08 21:55:22 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
gobject_class->set_property = gst_ffmpegaudenc_set_property;
|
|
|
|
gobject_class->get_property = gst_ffmpegaudenc_get_property;
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
|
2012-06-14 10:41:34 +00:00
|
|
|
/* FIXME: could use -1 for a sensible per-codec defaults */
|
2012-11-19 16:08:23 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BIT_RATE,
|
2012-06-14 10:41:34 +00:00
|
|
|
g_param_spec_int ("bitrate", "Bit Rate",
|
|
|
|
"Target Audio Bitrate", 0, G_MAXINT, DEFAULT_AUDIO_BITRATE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2013-10-14 20:50:57 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_COMPLIANCE,
|
|
|
|
g_param_spec_enum ("compliance", "Compliance",
|
|
|
|
"Adherence of the encoder to the specifications",
|
|
|
|
GST_TYPE_FFMPEG_COMPLIANCE, FFMPEG_DEFAULT_COMPLIANCE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
gobject_class->finalize = gst_ffmpegaudenc_finalize;
|
2012-11-20 09:36:29 +00:00
|
|
|
|
2013-08-29 14:53:28 +00:00
|
|
|
gstaudioencoder_class->start = GST_DEBUG_FUNCPTR (gst_ffmpegaudenc_start);
|
2012-11-20 09:36:29 +00:00
|
|
|
gstaudioencoder_class->stop = GST_DEBUG_FUNCPTR (gst_ffmpegaudenc_stop);
|
2012-11-20 10:45:34 +00:00
|
|
|
gstaudioencoder_class->flush = GST_DEBUG_FUNCPTR (gst_ffmpegaudenc_flush);
|
2012-11-20 09:36:29 +00:00
|
|
|
gstaudioencoder_class->set_format =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_ffmpegaudenc_set_format);
|
|
|
|
gstaudioencoder_class->handle_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_ffmpegaudenc_handle_frame);
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_init (GstFFMpegAudEnc * ffmpegaudenc)
|
2001-12-23 13:25:04 +00:00
|
|
|
{
|
2012-12-05 18:25:37 +00:00
|
|
|
GstFFMpegAudEncClass *klass =
|
|
|
|
(GstFFMpegAudEncClass *) G_OBJECT_GET_CLASS (ffmpegaudenc);
|
|
|
|
|
2015-08-17 07:35:41 +00:00
|
|
|
GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (ffmpegaudenc));
|
|
|
|
|
2003-06-07 20:46:24 +00:00
|
|
|
/* ffmpeg objects */
|
2012-12-05 18:25:37 +00:00
|
|
|
ffmpegaudenc->context = avcodec_alloc_context3 (klass->in_plugin);
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc->opened = FALSE;
|
2015-06-28 13:37:23 +00:00
|
|
|
ffmpegaudenc->frame = av_frame_alloc ();
|
2004-10-11 08:21:16 +00:00
|
|
|
|
2013-10-14 20:50:57 +00:00
|
|
|
ffmpegaudenc->compliance = FFMPEG_DEFAULT_COMPLIANCE;
|
|
|
|
|
2012-11-20 10:51:13 +00:00
|
|
|
gst_audio_encoder_set_drainable (GST_AUDIO_ENCODER (ffmpegaudenc), TRUE);
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_finalize (GObject * object)
|
2001-12-23 13:25:04 +00:00
|
|
|
{
|
2012-04-07 09:14:45 +00:00
|
|
|
GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) object;
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
/* clean up remaining allocated data */
|
2015-06-28 13:37:23 +00:00
|
|
|
av_frame_free (&ffmpegaudenc->frame);
|
2014-01-15 09:02:33 +00:00
|
|
|
gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
|
2012-11-20 09:36:29 +00:00
|
|
|
av_free (ffmpegaudenc->context);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2013-08-29 14:53:28 +00:00
|
|
|
static gboolean
|
|
|
|
gst_ffmpegaudenc_start (GstAudioEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) encoder;
|
|
|
|
GstFFMpegAudEncClass *oclass =
|
|
|
|
(GstFFMpegAudEncClass *) G_OBJECT_GET_CLASS (ffmpegaudenc);
|
|
|
|
|
2014-01-15 09:02:33 +00:00
|
|
|
gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
|
2013-08-29 14:53:28 +00:00
|
|
|
if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
|
|
|
|
oclass->in_plugin) < 0) {
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
static gboolean
|
|
|
|
gst_ffmpegaudenc_stop (GstAudioEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) encoder;
|
2007-08-01 17:43:39 +00:00
|
|
|
|
2003-06-07 20:46:24 +00:00
|
|
|
/* close old session */
|
2012-12-17 15:35:36 +00:00
|
|
|
gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
|
|
|
|
ffmpegaudenc->opened = FALSE;
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
return TRUE;
|
2003-06-07 20:46:24 +00:00
|
|
|
}
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-11-20 10:45:34 +00:00
|
|
|
static void
|
|
|
|
gst_ffmpegaudenc_flush (GstAudioEncoder * encoder)
|
|
|
|
{
|
2012-11-20 10:56:57 +00:00
|
|
|
GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) encoder;
|
2012-11-20 10:45:34 +00:00
|
|
|
|
2012-11-20 10:56:57 +00:00
|
|
|
if (ffmpegaudenc->opened) {
|
|
|
|
avcodec_flush_buffers (ffmpegaudenc->context);
|
2012-11-20 10:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
static gboolean
|
2012-11-20 09:36:29 +00:00
|
|
|
gst_ffmpegaudenc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
|
2003-06-07 20:46:24 +00:00
|
|
|
{
|
2012-11-20 09:36:29 +00:00
|
|
|
GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) encoder;
|
2003-12-22 01:46:58 +00:00
|
|
|
GstCaps *other_caps;
|
2004-01-09 01:53:31 +00:00
|
|
|
GstCaps *allowed_caps;
|
|
|
|
GstCaps *icaps;
|
2012-11-20 09:36:29 +00:00
|
|
|
gsize frame_size;
|
2012-04-07 09:14:45 +00:00
|
|
|
GstFFMpegAudEncClass *oclass =
|
|
|
|
(GstFFMpegAudEncClass *) G_OBJECT_GET_CLASS (ffmpegaudenc);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2003-06-07 20:46:24 +00:00
|
|
|
/* close old session */
|
2012-04-07 09:14:45 +00:00
|
|
|
if (ffmpegaudenc->opened) {
|
|
|
|
gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
|
|
|
|
ffmpegaudenc->opened = FALSE;
|
2013-08-29 14:53:28 +00:00
|
|
|
if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
|
|
|
|
oclass->in_plugin) < 0) {
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-06-07 20:46:24 +00:00
|
|
|
}
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2005-01-16 11:29:28 +00:00
|
|
|
/* if we set it in _getcaps we should set it also in _link */
|
2013-10-14 20:50:57 +00:00
|
|
|
ffmpegaudenc->context->strict_std_compliance = ffmpegaudenc->compliance;
|
2005-01-16 11:29:28 +00:00
|
|
|
|
2003-06-07 20:46:24 +00:00
|
|
|
/* user defined properties */
|
2012-07-23 22:29:26 +00:00
|
|
|
if (ffmpegaudenc->bitrate > 0) {
|
|
|
|
GST_INFO_OBJECT (ffmpegaudenc, "Setting avcontext to bitrate %d",
|
|
|
|
ffmpegaudenc->bitrate);
|
|
|
|
ffmpegaudenc->context->bit_rate = ffmpegaudenc->bitrate;
|
|
|
|
ffmpegaudenc->context->bit_rate_tolerance = ffmpegaudenc->bitrate;
|
|
|
|
} else {
|
2016-01-16 14:40:52 +00:00
|
|
|
GST_INFO_OBJECT (ffmpegaudenc,
|
|
|
|
"Using avcontext default bitrate %" G_GINT64_FORMAT,
|
|
|
|
(gint64) ffmpegaudenc->context->bit_rate);
|
2012-07-23 22:29:26 +00:00
|
|
|
}
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2005-11-28 17:12:13 +00:00
|
|
|
/* RTP payload used for GOB production (for Asterisk) */
|
2012-04-07 09:14:45 +00:00
|
|
|
if (ffmpegaudenc->rtp_payload_size) {
|
|
|
|
ffmpegaudenc->context->rtp_payload_size = ffmpegaudenc->rtp_payload_size;
|
ext/ffmpeg/: Added helper object to handle extra properties.
Original commit message from CVS:
Patch by: Mark Nauwelaerts <manauw at skynet dot be>
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpegcfg.c: (gst_ffmpeg_pass_get_type),
(gst_ffmpeg_lim_pass_get_type), (gst_ffmpeg_mb_decision_get_type),
(gst_ffmpeg_mb_cmp_get_type), (gst_ffmpeg_dct_algo_get_type),
(gst_ffmpeg_idct_algo_get_type), (gst_ffmpeg_quant_type_get_type),
(gst_ffmpeg_pre_me_get_type), (gst_ffmpeg_pred_method_get_type),
(gst_ffmpeg_flags_get_type), (gst_ffmpeg_cfg_init),
(gst_ffmpeg_cfg_codec_has_pspec),
(gst_ffmpeg_cfg_install_property), (gst_ffmpeg_cfg_set_property),
(gst_ffmpeg_cfg_get_property), (gst_ffmpeg_cfg_set_defaults),
(gst_ffmpeg_cfg_fill_context):
* ext/ffmpeg/gstffmpegcfg.h:
Added helper object to handle extra properties.
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_dispose),
(gst_ffmpegenc_getcaps), (gst_ffmpegenc_setcaps),
(gst_ffmpegenc_chain_video), (gst_ffmpegenc_chain_audio),
(gst_ffmpegenc_flush_buffers), (gst_ffmpegenc_event_video),
(gst_ffmpegenc_set_property), (gst_ffmpegenc_get_property),
(gst_ffmpegenc_change_state), (gst_ffmpegenc_register):
* ext/ffmpeg/gstffmpegenc.h:
Exposes (quite some of) the AVCodecContext configurable
settings, as well as some additional (though simple) logic for b-frame
delay handling.
Most of the properties are only enabled for mpeg4-like codecs.
Fixes #344583.
2006-09-06 14:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* some other defaults */
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc->context->rc_strategy = 2;
|
|
|
|
ffmpegaudenc->context->b_frame_strategy = 0;
|
|
|
|
ffmpegaudenc->context->coder_type = 0;
|
|
|
|
ffmpegaudenc->context->context_model = 0;
|
|
|
|
ffmpegaudenc->context->scenechange_threshold = 0;
|
ext/ffmpeg/: Added helper object to handle extra properties.
Original commit message from CVS:
Patch by: Mark Nauwelaerts <manauw at skynet dot be>
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpegcfg.c: (gst_ffmpeg_pass_get_type),
(gst_ffmpeg_lim_pass_get_type), (gst_ffmpeg_mb_decision_get_type),
(gst_ffmpeg_mb_cmp_get_type), (gst_ffmpeg_dct_algo_get_type),
(gst_ffmpeg_idct_algo_get_type), (gst_ffmpeg_quant_type_get_type),
(gst_ffmpeg_pre_me_get_type), (gst_ffmpeg_pred_method_get_type),
(gst_ffmpeg_flags_get_type), (gst_ffmpeg_cfg_init),
(gst_ffmpeg_cfg_codec_has_pspec),
(gst_ffmpeg_cfg_install_property), (gst_ffmpeg_cfg_set_property),
(gst_ffmpeg_cfg_get_property), (gst_ffmpeg_cfg_set_defaults),
(gst_ffmpeg_cfg_fill_context):
* ext/ffmpeg/gstffmpegcfg.h:
Added helper object to handle extra properties.
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_dispose),
(gst_ffmpegenc_getcaps), (gst_ffmpegenc_setcaps),
(gst_ffmpegenc_chain_video), (gst_ffmpegenc_chain_audio),
(gst_ffmpegenc_flush_buffers), (gst_ffmpegenc_event_video),
(gst_ffmpegenc_set_property), (gst_ffmpegenc_get_property),
(gst_ffmpegenc_change_state), (gst_ffmpegenc_register):
* ext/ffmpeg/gstffmpegenc.h:
Exposes (quite some of) the AVCodecContext configurable
settings, as well as some additional (though simple) logic for b-frame
delay handling.
Most of the properties are only enabled for mpeg4-like codecs.
Fixes #344583.
2006-09-06 14:51:13 +00:00
|
|
|
|
2003-12-22 01:46:58 +00:00
|
|
|
/* fetch pix_fmt and so on */
|
2012-11-20 09:36:29 +00:00
|
|
|
gst_ffmpeg_audioinfo_to_context (info, ffmpegaudenc->context);
|
2012-04-07 09:14:45 +00:00
|
|
|
if (!ffmpegaudenc->context->time_base.den) {
|
2012-12-01 23:21:41 +00:00
|
|
|
ffmpegaudenc->context->time_base.den = GST_AUDIO_INFO_RATE (info);
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc->context->time_base.num = 1;
|
|
|
|
ffmpegaudenc->context->ticks_per_frame = 1;
|
ext/ffmpeg/: Added helper object to handle extra properties.
Original commit message from CVS:
Patch by: Mark Nauwelaerts <manauw at skynet dot be>
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpegcfg.c: (gst_ffmpeg_pass_get_type),
(gst_ffmpeg_lim_pass_get_type), (gst_ffmpeg_mb_decision_get_type),
(gst_ffmpeg_mb_cmp_get_type), (gst_ffmpeg_dct_algo_get_type),
(gst_ffmpeg_idct_algo_get_type), (gst_ffmpeg_quant_type_get_type),
(gst_ffmpeg_pre_me_get_type), (gst_ffmpeg_pred_method_get_type),
(gst_ffmpeg_flags_get_type), (gst_ffmpeg_cfg_init),
(gst_ffmpeg_cfg_codec_has_pspec),
(gst_ffmpeg_cfg_install_property), (gst_ffmpeg_cfg_set_property),
(gst_ffmpeg_cfg_get_property), (gst_ffmpeg_cfg_set_defaults),
(gst_ffmpeg_cfg_fill_context):
* ext/ffmpeg/gstffmpegcfg.h:
Added helper object to handle extra properties.
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_dispose),
(gst_ffmpegenc_getcaps), (gst_ffmpegenc_setcaps),
(gst_ffmpegenc_chain_video), (gst_ffmpegenc_chain_audio),
(gst_ffmpegenc_flush_buffers), (gst_ffmpegenc_event_video),
(gst_ffmpegenc_set_property), (gst_ffmpegenc_get_property),
(gst_ffmpegenc_change_state), (gst_ffmpegenc_register):
* ext/ffmpeg/gstffmpegenc.h:
Exposes (quite some of) the AVCodecContext configurable
settings, as well as some additional (though simple) logic for b-frame
delay handling.
Most of the properties are only enabled for mpeg4-like codecs.
Fixes #344583.
2006-09-06 14:51:13 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 18:07:34 +00:00
|
|
|
if (ffmpegaudenc->context->channel_layout) {
|
|
|
|
gst_ffmpeg_channel_layout_to_gst (ffmpegaudenc->context->channel_layout,
|
|
|
|
ffmpegaudenc->context->channels, ffmpegaudenc->ffmpeg_layout);
|
|
|
|
ffmpegaudenc->needs_reorder =
|
|
|
|
(memcmp (ffmpegaudenc->ffmpeg_layout, info->position,
|
|
|
|
sizeof (GstAudioChannelPosition) *
|
|
|
|
ffmpegaudenc->context->channels) != 0);
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:16:09 +00:00
|
|
|
/* some codecs support more than one format, first auto-choose one */
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "picking an output format ...");
|
|
|
|
allowed_caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (encoder));
|
|
|
|
if (!allowed_caps) {
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "... 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_pad_get_pad_template_caps (GST_AUDIO_ENCODER_SRC_PAD (encoder));
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "chose caps %" GST_PTR_FORMAT, allowed_caps);
|
|
|
|
gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
|
|
|
|
oclass->in_plugin->type, allowed_caps, ffmpegaudenc->context);
|
|
|
|
|
2003-12-22 01:46:58 +00:00
|
|
|
/* open codec */
|
2012-04-07 09:14:45 +00:00
|
|
|
if (gst_ffmpeg_avcodec_open (ffmpegaudenc->context, oclass->in_plugin) < 0) {
|
2016-07-05 17:16:09 +00:00
|
|
|
gst_caps_unref (allowed_caps);
|
2013-08-29 14:53:28 +00:00
|
|
|
gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
|
2012-06-14 10:41:34 +00:00
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "avenc_%s: Failed to open FFMPEG codec",
|
2004-03-29 16:39:18 +00:00
|
|
|
oclass->in_plugin->name);
|
2013-08-29 14:53:28 +00:00
|
|
|
if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
|
|
|
|
oclass->in_plugin) < 0)
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
|
2013-10-26 08:34:30 +00:00
|
|
|
|
|
|
|
if ((oclass->in_plugin->capabilities & CODEC_CAP_EXPERIMENTAL) &&
|
|
|
|
ffmpegaudenc->compliance != GST_FFMPEG_EXPERIMENTAL) {
|
|
|
|
GST_ELEMENT_ERROR (ffmpegaudenc, LIBRARY, SETTINGS,
|
|
|
|
("Codec is experimental, but settings don't allow encoders to "
|
|
|
|
"produce output of experimental quality"),
|
|
|
|
("This codec may not create output that is conformant to the specs "
|
|
|
|
"or of good quality. If you must use it anyway, set the "
|
|
|
|
"compliance property to experimental"));
|
|
|
|
}
|
2005-08-19 15:23:39 +00:00
|
|
|
return FALSE;
|
2003-06-07 21:06:05 +00:00
|
|
|
}
|
|
|
|
|
2003-06-07 20:46:24 +00:00
|
|
|
/* try to set this caps on the other side */
|
2003-12-22 01:46:58 +00:00
|
|
|
other_caps = gst_ffmpeg_codecid_to_caps (oclass->in_plugin->id,
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc->context, TRUE);
|
2004-10-25 11:56:52 +00:00
|
|
|
|
2003-12-22 01:46:58 +00:00
|
|
|
if (!other_caps) {
|
2012-03-27 14:39:09 +00:00
|
|
|
gst_caps_unref (allowed_caps);
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
GST_DEBUG ("Unsupported codec - no caps found");
|
2013-08-29 14:53:28 +00:00
|
|
|
if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
|
|
|
|
oclass->in_plugin) < 0)
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
|
2005-08-19 15:23:39 +00:00
|
|
|
return FALSE;
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
2003-06-07 20:46:24 +00:00
|
|
|
|
2004-01-09 01:53:31 +00:00
|
|
|
icaps = gst_caps_intersect (allowed_caps, other_caps);
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
gst_caps_unref (allowed_caps);
|
|
|
|
gst_caps_unref (other_caps);
|
2004-01-09 01:53:31 +00:00
|
|
|
if (gst_caps_is_empty (icaps)) {
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
gst_caps_unref (icaps);
|
2005-08-19 15:23:39 +00:00
|
|
|
return FALSE;
|
2004-01-09 01:53:31 +00:00
|
|
|
}
|
2016-07-05 17:16:45 +00:00
|
|
|
icaps = gst_caps_fixate (icaps);
|
2004-01-09 01:53:31 +00:00
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
if (!gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (ffmpegaudenc),
|
|
|
|
icaps)) {
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
gst_caps_unref (icaps);
|
2013-08-29 14:53:28 +00:00
|
|
|
if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
|
|
|
|
oclass->in_plugin) < 0)
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
|
2005-08-19 15:23:39 +00:00
|
|
|
return FALSE;
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
gst_caps_unref (icaps);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
frame_size = ffmpegaudenc->context->frame_size;
|
|
|
|
if (frame_size > 1) {
|
|
|
|
gst_audio_encoder_set_frame_samples_min (GST_AUDIO_ENCODER (ffmpegaudenc),
|
|
|
|
frame_size);
|
|
|
|
gst_audio_encoder_set_frame_samples_max (GST_AUDIO_ENCODER (ffmpegaudenc),
|
|
|
|
frame_size);
|
|
|
|
gst_audio_encoder_set_frame_max (GST_AUDIO_ENCODER (ffmpegaudenc), 1);
|
|
|
|
} else {
|
|
|
|
gst_audio_encoder_set_frame_samples_min (GST_AUDIO_ENCODER (ffmpegaudenc),
|
|
|
|
0);
|
|
|
|
gst_audio_encoder_set_frame_samples_max (GST_AUDIO_ENCODER (ffmpegaudenc),
|
|
|
|
0);
|
|
|
|
gst_audio_encoder_set_frame_max (GST_AUDIO_ENCODER (ffmpegaudenc), 0);
|
|
|
|
}
|
|
|
|
|
2015-07-29 15:24:54 +00:00
|
|
|
/* Store some tags */
|
|
|
|
{
|
|
|
|
GstTagList *tags = gst_tag_list_new_empty ();
|
|
|
|
const gchar *codec;
|
|
|
|
|
|
|
|
gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_NOMINAL_BITRATE,
|
2016-09-14 09:20:48 +00:00
|
|
|
(guint) ffmpegaudenc->context->bit_rate, NULL);
|
2015-07-29 15:24:54 +00:00
|
|
|
|
|
|
|
if ((codec =
|
|
|
|
gst_ffmpeg_get_codecid_longname (ffmpegaudenc->context->codec_id)))
|
|
|
|
gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_AUDIO_CODEC, codec,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gst_audio_encoder_merge_tags (encoder, tags, GST_TAG_MERGE_REPLACE);
|
|
|
|
gst_tag_list_unref (tags);
|
|
|
|
}
|
|
|
|
|
2003-06-07 20:46:24 +00:00
|
|
|
/* success! */
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc->opened = TRUE;
|
2003-06-07 20:46:24 +00:00
|
|
|
|
2005-08-19 15:23:39 +00:00
|
|
|
return TRUE;
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 07:13:44 +00:00
|
|
|
static void
|
|
|
|
gst_ffmpegaudenc_free_avpacket (gpointer pkt)
|
|
|
|
{
|
|
|
|
av_packet_unref ((AVPacket *) pkt);
|
|
|
|
g_slice_free (AVPacket, pkt);
|
|
|
|
}
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstMapInfo map;
|
|
|
|
|
|
|
|
guint8 **ext_data_array, *ext_data;
|
|
|
|
} BufferInfo;
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_info_free (void *opaque, guint8 * data)
|
|
|
|
{
|
|
|
|
BufferInfo *info = opaque;
|
|
|
|
|
|
|
|
if (info->buffer) {
|
|
|
|
gst_buffer_unmap (info->buffer, &info->map);
|
|
|
|
gst_buffer_unref (info->buffer);
|
|
|
|
} else {
|
2016-01-25 19:13:10 +00:00
|
|
|
av_free (info->ext_data);
|
|
|
|
av_free (info->ext_data_array);
|
2015-06-28 14:21:33 +00:00
|
|
|
}
|
|
|
|
g_slice_free (BufferInfo, info);
|
|
|
|
}
|
|
|
|
|
2009-05-12 23:04:30 +00:00
|
|
|
static GstFlowReturn
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_encode_audio (GstFFMpegAudEnc * ffmpegaudenc,
|
2015-06-28 14:21:33 +00:00
|
|
|
GstBuffer * buffer, gint * have_data)
|
2009-05-12 23:04:30 +00:00
|
|
|
{
|
2012-12-01 23:05:23 +00:00
|
|
|
GstAudioEncoder *enc;
|
2009-05-12 23:04:30 +00:00
|
|
|
AVCodecContext *ctx;
|
|
|
|
gint res;
|
|
|
|
GstFlowReturn ret;
|
2012-11-20 09:53:01 +00:00
|
|
|
GstAudioInfo *info;
|
2014-04-29 07:13:44 +00:00
|
|
|
AVPacket *pkt;
|
2015-06-28 13:37:23 +00:00
|
|
|
AVFrame *frame = ffmpegaudenc->frame;
|
2012-12-05 18:28:33 +00:00
|
|
|
gboolean planar;
|
2015-06-30 17:59:54 +00:00
|
|
|
gint nsamples = -1;
|
2009-05-12 23:04:30 +00:00
|
|
|
|
2012-12-01 23:05:23 +00:00
|
|
|
enc = GST_AUDIO_ENCODER (ffmpegaudenc);
|
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
ctx = ffmpegaudenc->context;
|
2009-05-12 23:04:30 +00:00
|
|
|
|
2014-04-29 07:13:44 +00:00
|
|
|
pkt = g_slice_new0 (AVPacket);
|
2012-11-20 09:53:01 +00:00
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
if (buffer != NULL) {
|
|
|
|
BufferInfo *buffer_info = g_slice_new0 (BufferInfo);
|
|
|
|
guint8 *audio_in;
|
|
|
|
guint in_size;
|
|
|
|
|
|
|
|
buffer_info->buffer = buffer;
|
|
|
|
gst_buffer_map (buffer, &buffer_info->map, GST_MAP_READ);
|
|
|
|
audio_in = buffer_info->map.data;
|
|
|
|
in_size = buffer_info->map.size;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (ffmpegaudenc, "encoding buffer %p size:%u", audio_in,
|
|
|
|
in_size);
|
|
|
|
|
2014-02-20 20:25:35 +00:00
|
|
|
info = gst_audio_encoder_get_audio_info (enc);
|
|
|
|
planar = av_sample_fmt_is_planar (ffmpegaudenc->context->sample_fmt);
|
2016-04-28 15:43:37 +00:00
|
|
|
frame->format = ffmpegaudenc->context->sample_fmt;
|
|
|
|
frame->sample_rate = ffmpegaudenc->context->sample_rate;
|
|
|
|
frame->channels = ffmpegaudenc->context->channels;
|
|
|
|
frame->channel_layout = ffmpegaudenc->context->channel_layout;
|
2012-12-05 18:28:33 +00:00
|
|
|
|
2014-02-20 20:25:35 +00:00
|
|
|
if (planar && info->channels > 1) {
|
2015-06-30 17:59:54 +00:00
|
|
|
gint channels;
|
2014-02-20 20:25:35 +00:00
|
|
|
gint i, j;
|
2012-12-05 18:28:33 +00:00
|
|
|
|
2015-06-28 13:37:23 +00:00
|
|
|
nsamples = frame->nb_samples = in_size / info->bpf;
|
2014-02-20 20:25:35 +00:00
|
|
|
channels = info->channels;
|
2012-12-05 18:28:33 +00:00
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
frame->buf[0] =
|
|
|
|
av_buffer_create (NULL, 0, buffer_info_free, buffer_info, 0);
|
|
|
|
|
2014-02-20 20:25:35 +00:00
|
|
|
if (info->channels > AV_NUM_DATA_POINTERS) {
|
2015-06-28 14:21:33 +00:00
|
|
|
buffer_info->ext_data_array = frame->extended_data =
|
2016-01-25 19:08:11 +00:00
|
|
|
av_malloc_array (info->channels, sizeof (uint8_t *));
|
2014-02-20 20:25:35 +00:00
|
|
|
} else {
|
2015-06-28 13:37:23 +00:00
|
|
|
frame->extended_data = frame->data;
|
2014-02-20 20:25:35 +00:00
|
|
|
}
|
2012-12-12 13:04:54 +00:00
|
|
|
|
2016-01-25 19:08:11 +00:00
|
|
|
buffer_info->ext_data = frame->extended_data[0] = av_malloc (in_size);
|
2015-06-28 13:37:23 +00:00
|
|
|
frame->linesize[0] = in_size / channels;
|
2014-02-20 20:25:35 +00:00
|
|
|
for (i = 1; i < channels; i++)
|
2015-06-28 13:37:23 +00:00
|
|
|
frame->extended_data[i] =
|
|
|
|
frame->extended_data[i - 1] + frame->linesize[0];
|
2012-12-05 18:28:33 +00:00
|
|
|
|
2014-02-20 20:25:35 +00:00
|
|
|
switch (info->finfo->width) {
|
|
|
|
case 8:{
|
|
|
|
const guint8 *idata = (const guint8 *) audio_in;
|
2012-12-05 18:28:33 +00:00
|
|
|
|
2014-02-20 20:25:35 +00:00
|
|
|
for (i = 0; i < nsamples; i++) {
|
|
|
|
for (j = 0; j < channels; j++) {
|
2015-06-28 13:37:23 +00:00
|
|
|
((guint8 *) frame->extended_data[j])[i] = idata[j];
|
2014-02-20 20:25:35 +00:00
|
|
|
}
|
|
|
|
idata += channels;
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2014-02-20 20:25:35 +00:00
|
|
|
break;
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2014-02-20 20:25:35 +00:00
|
|
|
case 16:{
|
|
|
|
const guint16 *idata = (const guint16 *) audio_in;
|
|
|
|
|
|
|
|
for (i = 0; i < nsamples; i++) {
|
|
|
|
for (j = 0; j < channels; j++) {
|
2015-06-28 13:37:23 +00:00
|
|
|
((guint16 *) frame->extended_data[j])[i] = idata[j];
|
2014-02-20 20:25:35 +00:00
|
|
|
}
|
|
|
|
idata += channels;
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2014-02-20 20:25:35 +00:00
|
|
|
break;
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2014-02-20 20:25:35 +00:00
|
|
|
case 32:{
|
|
|
|
const guint32 *idata = (const guint32 *) audio_in;
|
|
|
|
|
|
|
|
for (i = 0; i < nsamples; i++) {
|
|
|
|
for (j = 0; j < channels; j++) {
|
2015-06-28 13:37:23 +00:00
|
|
|
((guint32 *) frame->extended_data[j])[i] = idata[j];
|
2014-02-20 20:25:35 +00:00
|
|
|
}
|
|
|
|
idata += channels;
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2014-02-20 20:25:35 +00:00
|
|
|
|
|
|
|
break;
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2014-02-20 20:25:35 +00:00
|
|
|
case 64:{
|
|
|
|
const guint64 *idata = (const guint64 *) audio_in;
|
|
|
|
|
|
|
|
for (i = 0; i < nsamples; i++) {
|
|
|
|
for (j = 0; j < channels; j++) {
|
2015-06-28 13:37:23 +00:00
|
|
|
((guint64 *) frame->extended_data[j])[i] = idata[j];
|
2014-02-20 20:25:35 +00:00
|
|
|
}
|
|
|
|
idata += channels;
|
|
|
|
}
|
2012-12-05 18:28:33 +00:00
|
|
|
|
2014-02-20 20:25:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2014-02-20 20:25:35 +00:00
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
gst_buffer_unmap (buffer, &buffer_info->map);
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
buffer_info->buffer = NULL;
|
2014-02-20 20:25:35 +00:00
|
|
|
} else {
|
2015-06-28 13:37:23 +00:00
|
|
|
frame->data[0] = audio_in;
|
|
|
|
frame->extended_data = frame->data;
|
|
|
|
frame->linesize[0] = in_size;
|
2015-06-30 17:59:54 +00:00
|
|
|
frame->nb_samples = nsamples = in_size / info->bpf;
|
2015-06-28 14:21:33 +00:00
|
|
|
frame->buf[0] =
|
|
|
|
av_buffer_create (NULL, 0, buffer_info_free, buffer_info, 0);
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 20:25:35 +00:00
|
|
|
/* we have a frame to feed the encoder */
|
2015-06-28 13:37:23 +00:00
|
|
|
res = avcodec_encode_audio2 (ctx, pkt, frame, have_data);
|
2014-02-20 20:25:35 +00:00
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
av_frame_unref (frame);
|
2012-12-05 18:28:33 +00:00
|
|
|
} else {
|
2015-06-28 14:21:33 +00:00
|
|
|
GST_LOG_OBJECT (ffmpegaudenc, "draining");
|
2014-02-20 20:25:35 +00:00
|
|
|
/* flushing the encoder */
|
2014-04-29 07:13:44 +00:00
|
|
|
res = avcodec_encode_audio2 (ctx, pkt, NULL, have_data);
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2009-05-12 23:04:30 +00:00
|
|
|
|
|
|
|
if (res < 0) {
|
2012-12-02 12:31:43 +00:00
|
|
|
char error_str[128] = { 0, };
|
|
|
|
|
2014-04-29 07:13:44 +00:00
|
|
|
g_slice_free (AVPacket, pkt);
|
2012-12-02 12:31:43 +00:00
|
|
|
av_strerror (res, error_str, sizeof (error_str));
|
|
|
|
GST_ERROR_OBJECT (enc, "Failed to encode buffer: %d - %s", res, error_str);
|
2009-05-12 23:04:30 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2012-04-07 09:14:45 +00:00
|
|
|
GST_LOG_OBJECT (ffmpegaudenc, "got output size %d", res);
|
2009-05-12 23:04:30 +00:00
|
|
|
|
2012-11-20 10:51:13 +00:00
|
|
|
if (*have_data) {
|
2012-11-20 09:53:01 +00:00
|
|
|
GstBuffer *outbuf;
|
2012-12-04 17:28:11 +00:00
|
|
|
const AVCodec *codec;
|
2012-11-20 09:53:01 +00:00
|
|
|
|
2014-04-29 07:13:44 +00:00
|
|
|
GST_LOG_OBJECT (ffmpegaudenc, "pushing size %d", pkt->size);
|
2012-11-20 09:53:01 +00:00
|
|
|
|
|
|
|
outbuf =
|
2015-06-28 14:32:02 +00:00
|
|
|
gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, pkt->data,
|
|
|
|
pkt->size, 0, pkt->size, pkt, gst_ffmpegaudenc_free_avpacket);
|
2012-12-01 23:05:23 +00:00
|
|
|
|
|
|
|
codec = ffmpegaudenc->context->codec;
|
2015-06-28 14:21:33 +00:00
|
|
|
if ((codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) || !buffer) {
|
2014-06-06 11:40:57 +00:00
|
|
|
/* FIXME: Not really correct, as -1 means "all the samples we got
|
|
|
|
given so far", which may not be true depending on the codec,
|
|
|
|
but we have no way to know AFAICT */
|
2012-12-01 23:05:23 +00:00
|
|
|
ret = gst_audio_encoder_finish_frame (enc, outbuf, -1);
|
|
|
|
} else {
|
2015-06-30 17:59:54 +00:00
|
|
|
ret = gst_audio_encoder_finish_frame (enc, outbuf, nsamples);
|
2012-12-01 23:05:23 +00:00
|
|
|
}
|
2012-11-20 09:36:29 +00:00
|
|
|
} else {
|
|
|
|
GST_LOG_OBJECT (ffmpegaudenc, "no output produced");
|
2014-04-29 07:13:44 +00:00
|
|
|
g_slice_free (AVPacket, pkt);
|
2012-11-20 09:36:29 +00:00
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
}
|
2009-05-12 23:04:30 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-20 10:51:13 +00:00
|
|
|
static void
|
|
|
|
gst_ffmpegaudenc_drain (GstFFMpegAudEnc * ffmpegaudenc)
|
|
|
|
{
|
|
|
|
GstFFMpegAudEncClass *oclass;
|
|
|
|
|
|
|
|
oclass = (GstFFMpegAudEncClass *) (G_OBJECT_GET_CLASS (ffmpegaudenc));
|
|
|
|
|
|
|
|
if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
|
|
|
|
gint have_data, try = 0;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (ffmpegaudenc,
|
|
|
|
"codec has delay capabilities, calling until libav has drained everything");
|
|
|
|
|
|
|
|
do {
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
ret = gst_ffmpegaudenc_encode_audio (ffmpegaudenc, NULL, &have_data);
|
2012-11-20 10:51:13 +00:00
|
|
|
if (ret != GST_FLOW_OK || have_data == 0)
|
|
|
|
break;
|
|
|
|
} while (try++ < 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
static GstFlowReturn
|
2012-11-20 09:36:29 +00:00
|
|
|
gst_ffmpegaudenc_handle_frame (GstAudioEncoder * encoder, GstBuffer * inbuf)
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
{
|
2012-04-07 09:14:45 +00:00
|
|
|
GstFFMpegAudEnc *ffmpegaudenc;
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
GstFlowReturn ret;
|
2012-11-20 10:51:13 +00:00
|
|
|
gint have_data;
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
ffmpegaudenc = (GstFFMpegAudEnc *) encoder;
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
|
2012-06-14 10:41:34 +00:00
|
|
|
if (G_UNLIKELY (!ffmpegaudenc->opened))
|
2011-06-30 07:18:42 +00:00
|
|
|
goto not_negotiated;
|
|
|
|
|
2012-11-20 10:51:13 +00:00
|
|
|
if (!inbuf) {
|
|
|
|
gst_ffmpegaudenc_drain (ffmpegaudenc);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
inbuf = gst_buffer_ref (inbuf);
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc,
|
2009-05-12 23:04:30 +00:00
|
|
|
"Received time %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
|
2012-11-20 09:36:29 +00:00
|
|
|
", size %" G_GSIZE_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)),
|
2012-11-20 09:55:55 +00:00
|
|
|
GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)), gst_buffer_get_size (inbuf));
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
|
2012-12-11 18:07:34 +00:00
|
|
|
/* Reorder channels to the GStreamer channel order */
|
|
|
|
if (ffmpegaudenc->needs_reorder) {
|
|
|
|
GstAudioInfo *info = gst_audio_encoder_get_audio_info (encoder);
|
|
|
|
|
|
|
|
inbuf = gst_buffer_make_writable (inbuf);
|
|
|
|
gst_audio_buffer_reorder_channels (inbuf, info->finfo->format,
|
|
|
|
info->channels, info->position, ffmpegaudenc->ffmpeg_layout);
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
ret = gst_ffmpegaudenc_encode_audio (ffmpegaudenc, inbuf, &have_data);
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
|
2012-11-20 09:36:29 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto push_failed;
|
Backport BRANCH-THREADED to HEAD, fix a bit.
Original commit message from CVS:
* configure.ac:
* ext/ffmpeg/Makefile.am:
* ext/ffmpeg/gstffmpeg.c: (plugin_init):
* ext/ffmpeg/gstffmpegcodecmap.c: (gst_ffmpeg_caps_with_codecid):
* ext/ffmpeg/gstffmpegdec.c: (gst_ffmpegdec_class_init),
(gst_ffmpegdec_init), (gst_ffmpegdec_query), (gst_ffmpegdec_event),
(gst_ffmpegdec_open), (gst_ffmpegdec_setcaps),
(gst_ffmpegdec_get_buffer), (gst_ffmpegdec_negotiate),
(gst_ffmpegdec_frame), (gst_ffmpegdec_sink_event),
(gst_ffmpegdec_chain), (gst_ffmpegdec_change_state),
(gst_ffmpegdec_register):
* ext/ffmpeg/gstffmpegenc.c: (gst_ffmpegenc_class_init),
(gst_ffmpegenc_init), (gst_ffmpegenc_getcaps),
(gst_ffmpegenc_setcaps), (gst_ffmpegenc_chain_video),
(gst_ffmpegenc_chain_audio):
* ext/libpostproc/Makefile.am:
Backport BRANCH-THREADED to HEAD, fix a bit.
2005-08-05 15:29:56 +00:00
|
|
|
|
2009-05-12 23:04:30 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* ERRORS */
|
2011-06-30 07:18:42 +00:00
|
|
|
not_negotiated:
|
|
|
|
{
|
2012-06-14 10:41:34 +00:00
|
|
|
GST_ELEMENT_ERROR (ffmpegaudenc, CORE, NEGOTIATION, (NULL),
|
2011-06-30 07:18:42 +00:00
|
|
|
("not configured to input format before data start"));
|
|
|
|
gst_buffer_unref (inbuf);
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
2009-05-12 23:04:30 +00:00
|
|
|
push_failed:
|
|
|
|
{
|
2012-04-07 09:14:45 +00:00
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to push buffer %d (%s)", ret,
|
2009-05-12 23:04:30 +00:00
|
|
|
gst_flow_get_name (ret));
|
|
|
|
return ret;
|
|
|
|
}
|
Several encoding changes: for video, double check the pix_fmt after opening the AVCodec. The pix_fmt will be changed ...
Original commit message from CVS:
Several encoding changes:
* for video, double check the pix_fmt after opening the AVCodec. The pix_fmt
will be changed to the preferred pix_fmt instead of that opening the
codec fails. We fail when it has changed (then, ffmpeg doesn't like it).
* for video, set the correct timestamp.
* for audio, do *not* set the frame_size: most codecs don't like it. Instead,
keep a cache of buffers and always give data of size "frame_size". Maybe
I should have used bytestream here, I'm not sure. This works, though.
I can now create mpeg1 video, mp2 and ac3 audio. I didn't test any others yet.
I also didn't start working on integration with any of the muxers yet, that's
all one big TODO. One thing at a time, please. :).
2003-11-12 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
2001-12-23 13:25:04 +00:00
|
|
|
static void
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_set_property (GObject * object,
|
2004-03-29 16:39:18 +00:00
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec)
|
2001-12-23 13:25:04 +00:00
|
|
|
{
|
2012-04-07 09:14:45 +00:00
|
|
|
GstFFMpegAudEnc *ffmpegaudenc;
|
2001-12-23 13:25:04 +00:00
|
|
|
|
|
|
|
/* Get a pointer of the right type. */
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc = (GstFFMpegAudEnc *) (object);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
if (ffmpegaudenc->opened) {
|
|
|
|
GST_WARNING_OBJECT (ffmpegaudenc,
|
2009-04-21 16:38:03 +00:00
|
|
|
"Can't change properties once decoder is setup !");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-23 13:25:04 +00:00
|
|
|
/* Check the argument id to see which argument we're setting. */
|
|
|
|
switch (prop_id) {
|
2012-11-19 16:08:23 +00:00
|
|
|
case PROP_BIT_RATE:
|
2012-06-14 10:41:34 +00:00
|
|
|
ffmpegaudenc->bitrate = g_value_get_int (value);
|
2003-06-07 20:46:24 +00:00
|
|
|
break;
|
2012-11-19 16:08:23 +00:00
|
|
|
case PROP_RTP_PAYLOAD_SIZE:
|
2012-06-14 10:41:34 +00:00
|
|
|
ffmpegaudenc->rtp_payload_size = g_value_get_int (value);
|
2005-11-28 17:12:13 +00:00
|
|
|
break;
|
2013-10-14 20:50:57 +00:00
|
|
|
case PROP_COMPLIANCE:
|
|
|
|
ffmpegaudenc->compliance = g_value_get_enum (value);
|
|
|
|
break;
|
2001-12-23 13:25:04 +00:00
|
|
|
default:
|
2012-04-07 09:14:45 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-12-23 13:25:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The set function is simply the inverse of the get fuction. */
|
|
|
|
static void
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_get_property (GObject * object,
|
2004-03-29 16:39:18 +00:00
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec)
|
2001-12-23 13:25:04 +00:00
|
|
|
{
|
2012-04-07 09:14:45 +00:00
|
|
|
GstFFMpegAudEnc *ffmpegaudenc;
|
2001-12-23 13:25:04 +00:00
|
|
|
|
|
|
|
/* It's not null if we got it, but it might not be ours */
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc = (GstFFMpegAudEnc *) (object);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
2012-11-19 16:08:23 +00:00
|
|
|
case PROP_BIT_RATE:
|
2012-06-14 10:41:34 +00:00
|
|
|
g_value_set_int (value, ffmpegaudenc->bitrate);
|
2001-12-23 13:25:04 +00:00
|
|
|
break;
|
2003-06-07 20:46:24 +00:00
|
|
|
break;
|
2012-11-19 16:08:23 +00:00
|
|
|
case PROP_RTP_PAYLOAD_SIZE:
|
2012-06-14 10:41:34 +00:00
|
|
|
g_value_set_int (value, ffmpegaudenc->rtp_payload_size);
|
2005-11-28 17:12:13 +00:00
|
|
|
break;
|
2013-10-14 20:50:57 +00:00
|
|
|
case PROP_COMPLIANCE:
|
|
|
|
g_value_set_enum (value, ffmpegaudenc->compliance);
|
|
|
|
break;
|
2001-12-23 13:25:04 +00:00
|
|
|
default:
|
2012-04-07 09:14:45 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-12-23 13:25:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2012-04-07 09:14:45 +00:00
|
|
|
gst_ffmpegaudenc_register (GstPlugin * plugin)
|
2001-12-23 13:25:04 +00:00
|
|
|
{
|
|
|
|
GTypeInfo typeinfo = {
|
2012-04-07 09:14:45 +00:00
|
|
|
sizeof (GstFFMpegAudEncClass),
|
|
|
|
(GBaseInitFunc) gst_ffmpegaudenc_base_init,
|
2001-12-23 13:25:04 +00:00
|
|
|
NULL,
|
2012-04-07 09:14:45 +00:00
|
|
|
(GClassInitFunc) gst_ffmpegaudenc_class_init,
|
2001-12-23 13:25:04 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2012-04-07 09:14:45 +00:00
|
|
|
sizeof (GstFFMpegAudEnc),
|
2001-12-23 13:25:04 +00:00
|
|
|
0,
|
2012-04-07 09:14:45 +00:00
|
|
|
(GInstanceInitFunc) gst_ffmpegaudenc_init,
|
2001-12-23 13:25:04 +00:00
|
|
|
};
|
|
|
|
GType type;
|
|
|
|
AVCodec *in_plugin;
|
2004-03-29 16:39:18 +00:00
|
|
|
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2007-12-17 12:43:06 +00:00
|
|
|
GST_LOG ("Registering encoders");
|
|
|
|
|
2008-10-30 12:05:45 +00:00
|
|
|
in_plugin = av_codec_next (NULL);
|
2001-12-23 13:25:04 +00:00
|
|
|
while (in_plugin) {
|
|
|
|
gchar *type_name;
|
2013-01-22 12:57:41 +00:00
|
|
|
guint rank;
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2011-01-03 10:45:04 +00:00
|
|
|
/* Skip non-AV codecs */
|
2012-04-07 09:14:45 +00:00
|
|
|
if (in_plugin->type != AVMEDIA_TYPE_AUDIO)
|
2011-01-03 10:45:04 +00:00
|
|
|
goto next;
|
|
|
|
|
2003-06-17 11:44:38 +00:00
|
|
|
/* no quasi codecs, please */
|
2016-07-22 11:54:21 +00:00
|
|
|
if (in_plugin->id == AV_CODEC_ID_PCM_S16LE_PLANAR ||
|
|
|
|
(in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
|
|
|
|
in_plugin->id <= AV_CODEC_ID_PCM_BLURAY) ||
|
|
|
|
(in_plugin->id >= AV_CODEC_ID_PCM_S8_PLANAR &&
|
2016-10-26 20:29:18 +00:00
|
|
|
#if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= AV_VERSION_INT (57,54,0)
|
2016-10-26 20:17:28 +00:00
|
|
|
in_plugin->id <= AV_CODEC_ID_PCM_S64BE)) {
|
2016-10-26 20:29:18 +00:00
|
|
|
#else
|
|
|
|
in_plugin->id <= AV_CODEC_ID_PCM_S16BE_PLANAR)) {
|
|
|
|
#endif
|
2003-06-17 11:44:38 +00:00
|
|
|
goto next;
|
2009-03-10 20:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
2003-06-17 11:44:38 +00:00
|
|
|
|
|
|
|
/* only encoders */
|
2012-12-05 09:52:34 +00:00
|
|
|
if (!av_codec_is_encoder (in_plugin)) {
|
2001-12-23 13:25:04 +00:00
|
|
|
goto next;
|
|
|
|
}
|
2003-06-07 20:46:24 +00:00
|
|
|
|
2009-11-02 07:43:17 +00:00
|
|
|
/* FIXME : We should have a method to know cheaply whether we have a mapping
|
|
|
|
* for the given plugin or not */
|
|
|
|
|
2008-10-30 12:05:45 +00:00
|
|
|
GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
|
2008-06-13 17:06:55 +00:00
|
|
|
|
2007-12-17 12:43:06 +00:00
|
|
|
/* no codecs for which we're GUARANTEED to have better alternatives */
|
2012-06-14 10:41:34 +00:00
|
|
|
if (!strcmp (in_plugin->name, "vorbis")
|
|
|
|
|| !strcmp (in_plugin->name, "flac")) {
|
2007-12-17 12:43:06 +00:00
|
|
|
GST_LOG ("Ignoring encoder %s", in_plugin->name);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2002-03-19 04:09:41 +00:00
|
|
|
/* construct the type */
|
2012-04-12 17:41:52 +00:00
|
|
|
type_name = g_strdup_printf ("avenc_%s", in_plugin->name);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
2009-06-05 11:19:03 +00:00
|
|
|
type = g_type_from_name (type_name);
|
|
|
|
|
|
|
|
if (!type) {
|
|
|
|
|
|
|
|
/* create the glib type now */
|
2012-11-20 09:36:29 +00:00
|
|
|
type =
|
|
|
|
g_type_register_static (GST_TYPE_AUDIO_ENCODER, type_name, &typeinfo,
|
|
|
|
0);
|
2009-11-02 07:43:17 +00:00
|
|
|
g_type_set_qdata (type, GST_FFENC_PARAMS_QDATA, (gpointer) in_plugin);
|
2009-06-05 11:19:03 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
static const GInterfaceInfo preset_info = {
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
g_type_add_interface_static (type, GST_TYPE_PRESET, &preset_info);
|
|
|
|
}
|
2009-05-04 11:00:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 12:57:41 +00:00
|
|
|
switch (in_plugin->id) {
|
|
|
|
/* avenc_aac: see https://bugzilla.gnome.org/show_bug.cgi?id=691617 */
|
2013-05-06 22:56:22 +00:00
|
|
|
case AV_CODEC_ID_AAC:
|
2013-01-22 12:57:41 +00:00
|
|
|
rank = GST_RANK_NONE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rank = GST_RANK_SECONDARY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_element_register (plugin, type_name, rank, type)) {
|
2004-02-02 20:27:13 +00:00
|
|
|
g_free (type_name);
|
2003-11-02 19:07:41 +00:00
|
|
|
return FALSE;
|
2004-02-02 20:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (type_name);
|
2003-11-02 19:07:41 +00:00
|
|
|
|
2004-03-29 16:39:18 +00:00
|
|
|
next:
|
2008-10-30 12:05:45 +00:00
|
|
|
in_plugin = av_codec_next (in_plugin);
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 12:43:06 +00:00
|
|
|
GST_LOG ("Finished registering encoders");
|
|
|
|
|
2001-12-23 13:25:04 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|