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>
|
2018-06-30 21:27:06 +00:00
|
|
|
#include <libavutil/opt.h>
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
#include <gst/gst.h>
|
2021-10-05 10:43:50 +00:00
|
|
|
#include <gst/base/base.h>
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2012-09-10 15:53:54 +00:00
|
|
|
#include "gstav.h"
|
2018-06-30 21:27:06 +00:00
|
|
|
#include "gstavcfg.h"
|
2012-09-10 15:53:54 +00:00
|
|
|
#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
|
|
|
|
2004-03-29 16:39:18 +00:00
|
|
|
enum
|
|
|
|
{
|
2012-11-19 16:08:23 +00:00
|
|
|
PROP_0,
|
2018-06-30 21:27:06 +00:00
|
|
|
PROP_CFG_BASE,
|
2001-12-23 13:25:04 +00:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
|
2017-01-02 03:36:09 +00:00
|
|
|
gst_caps_unref (sinkcaps);
|
|
|
|
gst_caps_unref (srccaps);
|
|
|
|
|
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
|
|
|
|
2018-06-30 21:27:06 +00:00
|
|
|
gst_ffmpeg_cfg_install_properties (gobject_class, klass->in_plugin,
|
|
|
|
PROP_CFG_BASE, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM);
|
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);
|
2018-06-30 21:27:06 +00:00
|
|
|
ffmpegaudenc->refcontext = 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
|
|
|
|
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);
|
2022-01-16 17:33:47 +00:00
|
|
|
avcodec_free_context (&ffmpegaudenc->context);
|
|
|
|
avcodec_free_context (&ffmpegaudenc->refcontext);
|
2012-11-20 09:36:29 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-10-05 18:47:42 +00:00
|
|
|
ffmpegaudenc->opened = FALSE;
|
|
|
|
ffmpegaudenc->need_reopen = FALSE;
|
|
|
|
|
2022-01-16 17:33:47 +00:00
|
|
|
avcodec_free_context (&ffmpegaudenc->context);
|
|
|
|
ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
|
|
|
|
if (ffmpegaudenc->context == NULL) {
|
2013-08-29 14:53:28 +00:00
|
|
|
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;
|
2020-10-05 18:47:42 +00:00
|
|
|
ffmpegaudenc->need_reopen = 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
|
|
|
|
2020-10-05 18:47:42 +00:00
|
|
|
ffmpegaudenc->need_reopen = FALSE;
|
|
|
|
|
2003-06-07 20:46:24 +00:00
|
|
|
/* close old session */
|
2012-04-07 09:14:45 +00:00
|
|
|
if (ffmpegaudenc->opened) {
|
2022-01-16 17:33:47 +00:00
|
|
|
avcodec_free_context (&ffmpegaudenc->context);
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc->opened = FALSE;
|
2022-01-16 17:33:47 +00:00
|
|
|
ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
|
|
|
|
if (ffmpegaudenc->context == NULL) {
|
2013-08-29 14:53:28 +00:00
|
|
|
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
|
|
|
|
2018-06-30 21:27:06 +00:00
|
|
|
gst_ffmpeg_cfg_fill_context (G_OBJECT (ffmpegaudenc), ffmpegaudenc->context);
|
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);
|
2022-01-16 17:33:47 +00:00
|
|
|
avcodec_free_context (&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);
|
2022-01-16 17:33:47 +00:00
|
|
|
ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
|
|
|
|
if (ffmpegaudenc->context == NULL)
|
2013-08-29 14:53:28 +00:00
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
|
2013-10-26 08:34:30 +00:00
|
|
|
|
2018-04-30 19:54:44 +00:00
|
|
|
if ((oclass->in_plugin->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
|
2018-06-30 21:27:06 +00:00
|
|
|
ffmpegaudenc->context->strict_std_compliance !=
|
2020-05-31 06:59:29 +00:00
|
|
|
FF_COMPLIANCE_EXPERIMENTAL) {
|
2013-10-26 08:34:30 +00:00
|
|
|
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);
|
2022-01-16 17:33:47 +00:00
|
|
|
avcodec_free_context (&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");
|
2022-01-16 17:33:47 +00:00
|
|
|
ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
|
|
|
|
if (ffmpegaudenc->context == NULL)
|
2013-08-29 14:53:28 +00:00
|
|
|
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)) {
|
2022-01-16 17:33:47 +00:00
|
|
|
avcodec_free_context (&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);
|
2022-01-16 17:33:47 +00:00
|
|
|
ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
|
|
|
|
if (ffmpegaudenc->context == NULL)
|
2013-08-29 14:53:28 +00:00
|
|
|
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;
|
2020-10-05 18:47:42 +00:00
|
|
|
ffmpegaudenc->need_reopen = FALSE;
|
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 {
|
2022-01-16 17:33:47 +00:00
|
|
|
av_freep (&info->ext_data);
|
|
|
|
av_freep (&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
|
2018-07-01 15:09:05 +00:00
|
|
|
gst_ffmpegaudenc_send_frame (GstFFMpegAudEnc * ffmpegaudenc, GstBuffer * buffer)
|
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;
|
|
|
|
GstFlowReturn ret;
|
2018-07-01 15:09:05 +00:00
|
|
|
gint res;
|
2012-11-20 09:53:01 +00:00
|
|
|
GstAudioInfo *info;
|
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
|
|
|
|
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 */
|
2018-07-01 15:09:05 +00:00
|
|
|
res = avcodec_send_frame (ctx, frame);
|
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 {
|
2020-10-05 18:47:42 +00:00
|
|
|
GstFFMpegAudEncClass *oclass =
|
|
|
|
(GstFFMpegAudEncClass *) G_OBJECT_GET_CLASS (ffmpegaudenc);
|
|
|
|
|
2015-06-28 14:21:33 +00:00
|
|
|
GST_LOG_OBJECT (ffmpegaudenc, "draining");
|
2014-02-20 20:25:35 +00:00
|
|
|
/* flushing the encoder */
|
2018-07-01 15:09:05 +00:00
|
|
|
res = avcodec_send_frame (ctx, NULL);
|
2020-10-05 18:47:42 +00:00
|
|
|
|
|
|
|
/* If AV_CODEC_CAP_ENCODER_FLUSH wasn't set, we need to re-open
|
|
|
|
* encoder */
|
|
|
|
if (!(oclass->in_plugin->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)) {
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Encoder needs reopen later");
|
|
|
|
|
|
|
|
/* we will reopen later handle_frame() */
|
|
|
|
ffmpegaudenc->need_reopen = TRUE;
|
|
|
|
}
|
2012-12-05 18:28:33 +00:00
|
|
|
}
|
2009-05-12 23:04:30 +00:00
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
if (res == 0) {
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
} else if (res == AVERROR_EOF) {
|
|
|
|
ret = GST_FLOW_EOS;
|
|
|
|
} else { /* Any other return value is an error in our context */
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
GST_WARNING_OBJECT (ffmpegaudenc, "Failed to encode buffer");
|
2009-05-12 23:04:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_ffmpegaudenc_receive_packet (GstFFMpegAudEnc * ffmpegaudenc,
|
|
|
|
gboolean * got_packet)
|
|
|
|
{
|
|
|
|
GstAudioEncoder *enc;
|
|
|
|
AVCodecContext *ctx;
|
|
|
|
gint res;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
AVPacket *pkt;
|
|
|
|
|
|
|
|
enc = GST_AUDIO_ENCODER (ffmpegaudenc);
|
|
|
|
|
|
|
|
ctx = ffmpegaudenc->context;
|
|
|
|
|
|
|
|
pkt = g_slice_new0 (AVPacket);
|
|
|
|
|
|
|
|
res = avcodec_receive_packet (ctx, pkt);
|
|
|
|
|
|
|
|
if (res == 0) {
|
2012-11-20 09:53:01 +00:00
|
|
|
GstBuffer *outbuf;
|
2021-10-05 10:43:50 +00:00
|
|
|
const uint8_t *side_data;
|
|
|
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58,130,0)
|
|
|
|
size_t side_data_length = 0;
|
|
|
|
#else
|
|
|
|
int side_data_length = 0;
|
|
|
|
#endif
|
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
|
|
|
|
2021-10-05 10:43:50 +00:00
|
|
|
if ((side_data =
|
|
|
|
av_packet_get_side_data (pkt, AV_PKT_DATA_SKIP_SAMPLES,
|
|
|
|
&side_data_length)) && side_data_length == 10) {
|
|
|
|
GstByteReader reader = GST_BYTE_READER_INIT (pkt->data, pkt->size);
|
|
|
|
guint32 start, end;
|
|
|
|
|
|
|
|
start = gst_byte_reader_get_uint32_le_unchecked (&reader);
|
|
|
|
end = gst_byte_reader_get_uint32_le_unchecked (&reader);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (ffmpegaudenc,
|
|
|
|
"got skip samples side data with start %u and end %u", start, end);
|
|
|
|
gst_buffer_add_audio_clipping_meta (outbuf, GST_FORMAT_DEFAULT, start,
|
|
|
|
end);
|
|
|
|
}
|
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
ret =
|
|
|
|
gst_audio_encoder_finish_frame (enc, outbuf,
|
|
|
|
pkt->duration > 0 ? pkt->duration : -1);
|
|
|
|
*got_packet = TRUE;
|
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;
|
2018-07-01 15:09:05 +00:00
|
|
|
*got_packet = FALSE;
|
2012-11-20 09:36:29 +00:00
|
|
|
}
|
2009-05-12 23:04:30 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
static GstFlowReturn
|
2012-11-20 10:51:13 +00:00
|
|
|
gst_ffmpegaudenc_drain (GstFFMpegAudEnc * ffmpegaudenc)
|
|
|
|
{
|
2018-07-01 15:09:05 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
gboolean got_packet;
|
2012-11-20 10:51:13 +00:00
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
ret = gst_ffmpegaudenc_send_frame (ffmpegaudenc, NULL);
|
2012-11-20 10:51:13 +00:00
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
if (ret == GST_FLOW_OK) {
|
2012-11-20 10:51:13 +00:00
|
|
|
do {
|
2018-07-01 15:09:05 +00:00
|
|
|
ret = gst_ffmpegaudenc_receive_packet (ffmpegaudenc, &got_packet);
|
|
|
|
if (ret != GST_FLOW_OK)
|
2012-11-20 10:51:13 +00:00
|
|
|
break;
|
2018-07-01 15:09:05 +00:00
|
|
|
} while (got_packet);
|
2012-11-20 10:51:13 +00:00
|
|
|
}
|
2018-07-01 15:09:05 +00:00
|
|
|
|
2020-10-05 18:47:42 +00:00
|
|
|
/* NOTE: this may or may not work depending on capability */
|
2018-07-01 15:09:05 +00:00
|
|
|
avcodec_flush_buffers (ffmpegaudenc->context);
|
|
|
|
|
2020-09-30 13:13:28 +00:00
|
|
|
/* FFMpeg will return AVERROR_EOF if it's internal was fully drained
|
|
|
|
* then we are translating it to GST_FLOW_EOS. However, because this behavior
|
|
|
|
* is fully internal stuff of this implementation and gstaudioencoder
|
|
|
|
* baseclass doesn't convert this GST_FLOW_EOS to GST_FLOW_OK,
|
|
|
|
* convert this flow returned here */
|
|
|
|
if (ret == GST_FLOW_EOS)
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
return ret;
|
2012-11-20 10:51:13 +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 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;
|
2018-07-01 15:09:05 +00:00
|
|
|
gboolean got_packet;
|
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;
|
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
if (!inbuf)
|
|
|
|
return gst_ffmpegaudenc_drain (ffmpegaudenc);
|
2012-11-20 10:51:13 +00:00
|
|
|
|
2020-10-05 18:47:42 +00:00
|
|
|
/* endoder was drained or flushed, and ffmpeg encoder doesn't support
|
|
|
|
* flushing. We need to re-open encoder then */
|
|
|
|
if (ffmpegaudenc->need_reopen) {
|
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Open encoder again");
|
|
|
|
|
|
|
|
if (!gst_ffmpegaudenc_set_format (encoder,
|
|
|
|
gst_audio_encoder_get_audio_info (encoder))) {
|
|
|
|
GST_ERROR_OBJECT (ffmpegaudenc, "Couldn't re-open encoder");
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-07-01 15:09:05 +00:00
|
|
|
ret = gst_ffmpegaudenc_send_frame (ffmpegaudenc, 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-11-20 09:36:29 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
2018-07-01 15:09:05 +00:00
|
|
|
goto send_frame_failed;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = gst_ffmpegaudenc_receive_packet (ffmpegaudenc, &got_packet);
|
|
|
|
} while (got_packet);
|
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;
|
|
|
|
}
|
2018-07-01 15:09:05 +00:00
|
|
|
send_frame_failed:
|
2009-05-12 23:04:30 +00:00
|
|
|
{
|
2018-07-01 15:09:05 +00:00
|
|
|
GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to send frame %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
|
|
|
|
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,
|
2018-06-30 21:28:31 +00:00
|
|
|
"Can't change properties once encoder is setup !");
|
2009-04-21 16:38:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-23 13:25:04 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
default:
|
2018-06-30 21:27:06 +00:00
|
|
|
if (!gst_ffmpeg_cfg_set_property (ffmpegaudenc->refcontext, value, pspec))
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-12-23 13:25:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-04-07 09:14:45 +00:00
|
|
|
ffmpegaudenc = (GstFFMpegAudEnc *) (object);
|
2001-12-23 13:25:04 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
default:
|
2018-06-30 21:27:06 +00:00
|
|
|
if (!gst_ffmpeg_cfg_get_property (ffmpegaudenc->refcontext, value, pspec))
|
|
|
|
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;
|
2018-06-29 19:20:36 +00:00
|
|
|
void *i = 0;
|
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");
|
|
|
|
|
2018-06-29 19:20:36 +00:00
|
|
|
while ((in_plugin = (AVCodec *) av_codec_iterate (&i))) {
|
2001-12-23 13:25:04 +00:00
|
|
|
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)
|
2018-06-29 19:20:36 +00:00
|
|
|
continue;
|
2011-01-03 10:45:04 +00:00
|
|
|
|
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 &&
|
2017-04-13 12:23:45 +00:00
|
|
|
in_plugin->id <= AV_CODEC_ID_PCM_F24LE)) {
|
2018-06-29 19:20:36 +00:00
|
|
|
continue;
|
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);
|
2018-06-29 19:20:36 +00:00
|
|
|
continue;
|
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)) {
|
2018-06-29 19:20:36 +00:00
|
|
|
continue;
|
2001-12-23 13:25:04 +00:00
|
|
|
}
|
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);
|
2018-06-29 19:20:36 +00:00
|
|
|
continue;
|
2007-12-17 12:43:06 +00:00
|
|
|
}
|
|
|
|
|
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);
|
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;
|
|
|
|
}
|