summaryrefslogtreecommitdiff
path: root/ext/celt
diff options
context:
space:
mode:
authorWim Taymans <wim.taymans@collabora.co.uk>2011-11-17 17:32:42 +0100
committerWim Taymans <wim.taymans@collabora.co.uk>2011-11-17 17:32:42 +0100
commitbc6ed0bf97e60615b5ef5a86fed3cb48c70a0b4f (patch)
treeec064c97947078428c96c68ae67ab7caebe7de04 /ext/celt
parent940de5b195943ad3a64df3bc590916fe59c89688 (diff)
parentfefa1df8b9115404c94ec2aa2e9d1ff8ca79d34c (diff)
downloadgstreamer-plugins-bad-bc6ed0bf97e60615b5ef5a86fed3cb48c70a0b4f.tar.gz
Merge branch 'master' into 0.11
Conflicts: ext/celt/gstceltdec.c ext/opus/gstopusdec.c ext/opus/gstopusdec.h ext/opus/gstopusenc.c ext/opus/gstopusenc.h ext/opus/gstopusparse.c
Diffstat (limited to 'ext/celt')
-rw-r--r--ext/celt/Makefile.am5
-rw-r--r--ext/celt/gstceltdec.c654
-rw-r--r--ext/celt/gstceltdec.h12
-rw-r--r--ext/celt/gstceltenc.c974
-rw-r--r--ext/celt/gstceltenc.h27
5 files changed, 417 insertions, 1255 deletions
diff --git a/ext/celt/Makefile.am b/ext/celt/Makefile.am
index 38119d8d1..01153b135 100644
--- a/ext/celt/Makefile.am
+++ b/ext/celt/Makefile.am
@@ -1,12 +1,13 @@
plugin_LTLIBRARIES = libgstcelt.la
libgstcelt_la_SOURCES = gstcelt.c gstceltdec.c gstceltenc.c
-libgstcelt_la_CFLAGS = \
+libgstcelt_la_CFLAGS = -DGST_USE_UNSTABLE_API \
$(GST_PLUGINS_BASE_CFLAGS) \
$(GST_CFLAGS) \
$(CELT_CFLAGS)
libgstcelt_la_LIBADD = \
- $(GST_PLUGINS_BASE_LIBS) -lgsttag-$(GST_MAJORMINOR) \
+ $(GST_PLUGINS_BASE_LIBS) \
+ -lgstaudio-$(GST_MAJORMINOR) -lgsttag-$(GST_MAJORMINOR) \
$(GST_BASE_LIBS) \
$(GST_LIBS) \
$(CELT_LIBS)
diff --git a/ext/celt/gstceltdec.c b/ext/celt/gstceltdec.c
index 2c43fddab..7227ab5d6 100644
--- a/ext/celt/gstceltdec.c
+++ b/ext/celt/gstceltdec.c
@@ -68,38 +68,26 @@ GST_STATIC_PAD_TEMPLATE ("sink",
);
#define gst_celt_dec_parent_class parent_class
-G_DEFINE_TYPE (GstCeltDec, gst_celt_dec, GST_TYPE_ELEMENT);
-
-static gboolean celt_dec_sink_event (GstPad * pad, GstEvent * event);
-static GstFlowReturn celt_dec_chain (GstPad * pad, GstBuffer * buf);
-static gboolean celt_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
-static GstStateChangeReturn celt_dec_change_state (GstElement * element,
- GstStateChange transition);
-
-static gboolean celt_dec_src_event (GstPad * pad, GstEvent * event);
-static gboolean celt_dec_src_query (GstPad * pad, GstQuery * query);
-static gboolean celt_dec_sink_query (GstPad * pad, GstQuery * query);
-static const GstQueryType *celt_get_src_query_types (GstPad * pad);
-static const GstQueryType *celt_get_sink_query_types (GstPad * pad);
-static gboolean celt_dec_convert (GstPad * pad,
- GstFormat src_format, gint64 src_value,
- GstFormat * dest_format, gint64 * dest_value);
-
-static GstFlowReturn celt_dec_chain_parse_data (GstCeltDec * dec,
- GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
-static GstFlowReturn celt_dec_chain_parse_header (GstCeltDec * dec,
- GstBuffer * buf);
-static GstFlowReturn celt_dec_chain_parse_comments (GstCeltDec * dec,
- GstBuffer * buf);
+G_DEFINE_TYPE (GstCeltDec, gst_celt_dec, GST_TYPE_AUDIO_DECODER);
+
+static gboolean gst_celt_dec_start (GstAudioDecoder * dec);
+static gboolean gst_celt_dec_stop (GstAudioDecoder * dec);
+static gboolean gst_celt_dec_set_format (GstAudioDecoder * bdec,
+ GstCaps * caps);
+static GstFlowReturn gst_celt_dec_handle_frame (GstAudioDecoder * dec,
+ GstBuffer * buffer);
static void
gst_celt_dec_class_init (GstCeltDecClass * klass)
{
- GstElementClass *gstelement_class;
+ GstAudioDecoderClass *gstbase_class;
- gstelement_class = (GstElementClass *) klass;
+ gstbase_class = (GstAudioDecoderClass *) klass;
- gstelement_class->change_state = GST_DEBUG_FUNCPTR (celt_dec_change_state);
+ gstbase_class->start = GST_DEBUG_FUNCPTR (gst_celt_dec_start);
+ gstbase_class->stop = GST_DEBUG_FUNCPTR (gst_celt_dec_stop);
+ gstbase_class->set_format = GST_DEBUG_FUNCPTR (gst_celt_dec_set_format);
+ gstbase_class->handle_frame = GST_DEBUG_FUNCPTR (gst_celt_dec_handle_frame);
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&celt_dec_src_factory));
@@ -118,11 +106,8 @@ gst_celt_dec_class_init (GstCeltDecClass * klass)
static void
gst_celt_dec_reset (GstCeltDec * dec)
{
- gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
- dec->granulepos = -1;
dec->packetno = 0;
dec->frame_size = 0;
- dec->frame_duration = 0;
if (dec->state) {
celt_decoder_destroy (dec->state);
dec->state = NULL;
@@ -145,411 +130,36 @@ gst_celt_dec_reset (GstCeltDec * dec)
static void
gst_celt_dec_init (GstCeltDec * dec)
{
- dec->sinkpad =
- gst_pad_new_from_static_template (&celt_dec_sink_factory, "sink");
- gst_pad_set_chain_function (dec->sinkpad, GST_DEBUG_FUNCPTR (celt_dec_chain));
- gst_pad_set_event_function (dec->sinkpad,
- GST_DEBUG_FUNCPTR (celt_dec_sink_event));
- gst_pad_set_query_type_function (dec->sinkpad,
- GST_DEBUG_FUNCPTR (celt_get_sink_query_types));
- gst_pad_set_query_function (dec->sinkpad,
- GST_DEBUG_FUNCPTR (celt_dec_sink_query));
- gst_pad_set_setcaps_function (dec->sinkpad,
- GST_DEBUG_FUNCPTR (celt_dec_sink_setcaps));
- gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
-
- dec->srcpad = gst_pad_new_from_static_template (&celt_dec_src_factory, "src");
- gst_pad_use_fixed_caps (dec->srcpad);
- gst_pad_set_event_function (dec->srcpad,
- GST_DEBUG_FUNCPTR (celt_dec_src_event));
- gst_pad_set_query_type_function (dec->srcpad,
- GST_DEBUG_FUNCPTR (celt_get_src_query_types));
- gst_pad_set_query_function (dec->srcpad,
- GST_DEBUG_FUNCPTR (celt_dec_src_query));
- gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
-
gst_celt_dec_reset (dec);
}
static gboolean
-celt_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
-{
- GstCeltDec *dec = GST_CELT_DEC (gst_pad_get_parent (pad));
- gboolean ret = TRUE;
- GstStructure *s;
- const GValue *streamheader;
-
- s = gst_caps_get_structure (caps, 0);
- if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
- G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
- gst_value_array_get_size (streamheader) >= 2) {
- const GValue *header, *vorbiscomment;
- GstBuffer *buf;
- GstFlowReturn res = GST_FLOW_OK;
-
- header = gst_value_array_get_value (streamheader, 0);
- if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
- buf = gst_value_get_buffer (header);
- res = celt_dec_chain_parse_header (dec, buf);
- if (res != GST_FLOW_OK)
- goto done;
- gst_buffer_replace (&dec->streamheader, buf);
- }
-
- vorbiscomment = gst_value_array_get_value (streamheader, 1);
- if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
- buf = gst_value_get_buffer (vorbiscomment);
- res = celt_dec_chain_parse_comments (dec, buf);
- if (res != GST_FLOW_OK)
- goto done;
- gst_buffer_replace (&dec->vorbiscomment, buf);
- }
-
- g_list_foreach (dec->extra_headers, (GFunc) gst_mini_object_unref, NULL);
- g_list_free (dec->extra_headers);
- dec->extra_headers = NULL;
-
- if (gst_value_array_get_size (streamheader) > 2) {
- gint i, n;
-
- n = gst_value_array_get_size (streamheader);
- for (i = 2; i < n; i++) {
- header = gst_value_array_get_value (streamheader, i);
- buf = gst_value_get_buffer (header);
- dec->extra_headers =
- g_list_prepend (dec->extra_headers, gst_buffer_ref (buf));
- }
- }
- }
-
-done:
- gst_object_unref (dec);
- return ret;
-}
-
-static gboolean
-celt_dec_convert (GstPad * pad,
- GstFormat src_format, gint64 src_value,
- GstFormat * dest_format, gint64 * dest_value)
+gst_celt_dec_start (GstAudioDecoder * dec)
{
- gboolean res = TRUE;
- GstCeltDec *dec;
- guint64 scale = 1;
-
- dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
- if (dec->packetno < 1) {
- res = FALSE;
- goto cleanup;
- }
-
- if (src_format == *dest_format) {
- *dest_value = src_value;
- res = TRUE;
- goto cleanup;
- }
-
- if (pad == dec->sinkpad &&
- (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES)) {
- res = FALSE;
- goto cleanup;
- }
-
- switch (src_format) {
- case GST_FORMAT_TIME:
- switch (*dest_format) {
- case GST_FORMAT_BYTES:
- scale = sizeof (gint16) * dec->header.nb_channels;
- case GST_FORMAT_DEFAULT:
- *dest_value =
- gst_util_uint64_scale_int (scale * src_value,
- dec->header.sample_rate, GST_SECOND);
- break;
- default:
- res = FALSE;
- break;
- }
- break;
- case GST_FORMAT_DEFAULT:
- switch (*dest_format) {
- case GST_FORMAT_BYTES:
- *dest_value = src_value * sizeof (gint16) * dec->header.nb_channels;
- break;
- case GST_FORMAT_TIME:
- *dest_value =
- gst_util_uint64_scale_int (src_value, GST_SECOND,
- dec->header.sample_rate);
- break;
- default:
- res = FALSE;
- break;
- }
- break;
- case GST_FORMAT_BYTES:
- switch (*dest_format) {
- case GST_FORMAT_DEFAULT:
- *dest_value = src_value / (sizeof (gint16) * dec->header.nb_channels);
- break;
- case GST_FORMAT_TIME:
- *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
- dec->header.sample_rate * sizeof (gint16) *
- dec->header.nb_channels);
- break;
- default:
- res = FALSE;
- break;
- }
- break;
- default:
- res = FALSE;
- break;
- }
-
-cleanup:
- gst_object_unref (dec);
- return res;
-}
-
-static const GstQueryType *
-celt_get_sink_query_types (GstPad * pad)
-{
- static const GstQueryType celt_dec_sink_query_types[] = {
- GST_QUERY_CONVERT,
- 0
- };
-
- return celt_dec_sink_query_types;
-}
-
-static gboolean
-celt_dec_sink_query (GstPad * pad, GstQuery * query)
-{
- GstCeltDec *dec;
- gboolean res;
-
- dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
- switch (GST_QUERY_TYPE (query)) {
- case GST_QUERY_CONVERT:
- {
- GstFormat src_fmt, dest_fmt;
- gint64 src_val, dest_val;
-
- gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
- res = celt_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
- if (res) {
- gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
- }
- break;
- }
- default:
- res = gst_pad_query_default (pad, query);
- break;
- }
-
- gst_object_unref (dec);
- return res;
-}
-
-static const GstQueryType *
-celt_get_src_query_types (GstPad * pad)
-{
- static const GstQueryType celt_dec_src_query_types[] = {
- GST_QUERY_POSITION,
- GST_QUERY_DURATION,
- 0
- };
-
- return celt_dec_src_query_types;
-}
-
-static gboolean
-celt_dec_src_query (GstPad * pad, GstQuery * query)
-{
- GstCeltDec *dec;
- gboolean res = FALSE;
-
- dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
- switch (GST_QUERY_TYPE (query)) {
- case GST_QUERY_POSITION:{
- GstSegment segment;
- GstFormat format;
- gint64 cur;
-
- gst_query_parse_position (query, &format, NULL);
-
- GST_PAD_STREAM_LOCK (dec->sinkpad);
- segment = dec->segment;
- GST_PAD_STREAM_UNLOCK (dec->sinkpad);
-
- if (segment.format != GST_FORMAT_TIME) {
- GST_DEBUG_OBJECT (dec, "segment not initialised yet");
- break;
- }
-
- if ((res = celt_dec_convert (dec->srcpad, GST_FORMAT_TIME,
- segment.last_stop, &format, &cur))) {
- gst_query_set_position (query, format, cur);
- }
- break;
- }
- case GST_QUERY_DURATION:{
- GstFormat format = GST_FORMAT_TIME;
- gint64 dur;
-
- /* get duration from demuxer */
- if (!gst_pad_query_peer_duration (dec->sinkpad, &format, &dur))
- break;
-
- gst_query_parse_duration (query, &format, NULL);
-
- /* and convert it into the requested format */
- if ((res = celt_dec_convert (dec->srcpad, GST_FORMAT_TIME,
- dur, &format, &dur))) {
- gst_query_set_duration (query, format, dur);
- }
- break;
- }
- default:
- res = gst_pad_query_default (pad, query);
- break;
- }
+ GstCeltDec *cd = GST_CELT_DEC (dec);
- gst_object_unref (dec);
- return res;
-}
+ GST_DEBUG_OBJECT (dec, "start");
+ gst_celt_dec_reset (cd);
-static gboolean
-celt_dec_src_event (GstPad * pad, GstEvent * event)
-{
- gboolean res = FALSE;
- GstCeltDec *dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
- GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
-
- switch (GST_EVENT_TYPE (event)) {
- case GST_EVENT_SEEK:{
- GstFormat format, tformat;
- gdouble rate;
- GstEvent *real_seek;
- GstSeekFlags flags;
- GstSeekType cur_type, stop_type;
- gint64 cur, stop;
- gint64 tcur, tstop;
-
- gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
- &stop_type, &stop);
-
- /* we have to ask our peer to seek to time here as we know
- * nothing about how to generate a granulepos from the src
- * formats or anything.
- *
- * First bring the requested format to time
- */
- tformat = GST_FORMAT_TIME;
- if (!(res = celt_dec_convert (pad, format, cur, &tformat, &tcur)))
- break;
- if (!(res = celt_dec_convert (pad, format, stop, &tformat, &tstop)))
- break;
-
- /* then seek with time on the peer */
- real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
- flags, cur_type, tcur, stop_type, tstop);
-
- GST_LOG_OBJECT (dec, "seek to %" GST_TIME_FORMAT, GST_TIME_ARGS (tcur));
-
- res = gst_pad_push_event (dec->sinkpad, real_seek);
- gst_event_unref (event);
- break;
- }
- default:
- res = gst_pad_event_default (pad, event);
- break;
- }
+ /* we know about concealment */
+ gst_audio_decoder_set_plc_aware (dec, TRUE);
- gst_object_unref (dec);
- return res;
+ return TRUE;
}
static gboolean
-celt_dec_sink_event (GstPad * pad, GstEvent * event)
+gst_celt_dec_stop (GstAudioDecoder * dec)
{
- GstCeltDec *dec;
- gboolean ret = FALSE;
-
- dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
- GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
-
- switch (GST_EVENT_TYPE (event)) {
- case GST_EVENT_NEWSEGMENT:{
- GstFormat format;
- gdouble rate, arate;
- gint64 start, stop, time;
- gboolean update;
-
- gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
- &start, &stop, &time);
+ GstCeltDec *cd = GST_CELT_DEC (dec);
- if (format != GST_FORMAT_TIME)
- goto newseg_wrong_format;
-
- if (rate <= 0.0)
- goto newseg_wrong_rate;
-
- if (update) {
- /* time progressed without data, see if we can fill the gap with
- * some concealment data */
- if (dec->segment.last_stop < start) {
- GstClockTime duration;
-
- duration = start - dec->segment.last_stop;
- celt_dec_chain_parse_data (dec, NULL, dec->segment.last_stop,
- duration);
- }
- }
-
- /* now configure the values */
- gst_segment_set_newsegment_full (&dec->segment, update,
- rate, arate, GST_FORMAT_TIME, start, stop, time);
-
- dec->granulepos = -1;
-
- GST_DEBUG_OBJECT (dec, "segment now: cur = %" GST_TIME_FORMAT " [%"
- GST_TIME_FORMAT " - %" GST_TIME_FORMAT "]",
- GST_TIME_ARGS (dec->segment.last_stop),
- GST_TIME_ARGS (dec->segment.start),
- GST_TIME_ARGS (dec->segment.stop));
-
- ret = gst_pad_push_event (dec->srcpad, event);
- break;
- }
- default:
- ret = gst_pad_event_default (pad, event);
- break;
- }
-
- gst_object_unref (dec);
- return ret;
+ GST_DEBUG_OBJECT (dec, "stop");
+ gst_celt_dec_reset (cd);
- /* ERRORS */
-newseg_wrong_format:
- {
- GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
- gst_object_unref (dec);
- return FALSE;
- }
-newseg_wrong_rate:
- {
- GST_DEBUG_OBJECT (dec, "negative rates not supported yet");
- gst_object_unref (dec);
- return FALSE;
- }
+ return TRUE;
}
static GstFlowReturn
-celt_dec_chain_parse_header (GstCeltDec * dec, GstBuffer * buf)
+gst_celt_dec_parse_header (GstCeltDec * dec, GstBuffer * buf)
{
GstCaps *caps;
gint error = CELT_OK;
@@ -596,9 +206,6 @@ celt_dec_chain_parse_header (GstCeltDec * dec, GstBuffer * buf)
celt_mode_info (dec->mode, CELT_GET_FRAME_SIZE, &dec->frame_size);
#endif
- dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
- GST_SECOND, dec->header.sample_rate);
-
/* set caps */
caps = gst_caps_new_simple ("audio/x-raw-int",
"rate", G_TYPE_INT, dec->header.sample_rate,
@@ -610,7 +217,7 @@ celt_dec_chain_parse_header (GstCeltDec * dec, GstBuffer * buf)
GST_DEBUG_OBJECT (dec, "rate=%d channels=%d frame-size=%d",
dec->header.sample_rate, dec->header.nb_channels, dec->frame_size);
- if (!gst_pad_set_caps (dec->srcpad, caps))
+ if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps))
goto nego_failed;
gst_caps_unref (caps);
@@ -650,7 +257,7 @@ nego_failed:
}
static GstFlowReturn
-celt_dec_chain_parse_comments (GstCeltDec * dec, GstBuffer * buf)
+gst_celt_dec_parse_comments (GstCeltDec * dec, GstBuffer * buf)
{
GstTagList *list;
gchar *ver, *encoder = NULL;
@@ -685,7 +292,8 @@ celt_dec_chain_parse_comments (GstCeltDec * dec, GstBuffer * buf)
GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
- gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, list);
+ gst_element_found_tags_for_pad (GST_ELEMENT (dec),
+ GST_AUDIO_DECODER_SRC_PAD (dec), list);
g_free (encoder);
g_free (ver);
@@ -694,8 +302,7 @@ celt_dec_chain_parse_comments (GstCeltDec * dec, GstBuffer * buf)
}
static GstFlowReturn
-celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
- GstClockTime timestamp, GstClockTime duration)
+gst_celt_dec_parse_data (GstCeltDec * dec, GstBuffer * buf)
{
GstFlowReturn res = GST_FLOW_OK;
gint size;
@@ -705,33 +312,23 @@ celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
gint error = CELT_OK;
int skip = 0;
- if (timestamp != -1) {
- dec->segment.last_stop = timestamp;
- dec->granulepos = -1;
- }
+ if (!dec->frame_size)
+ goto not_negotiated;
- if (buf) {
+ if (G_LIKELY (GST_BUFFER_SIZE (buf))) {
data = GST_BUFFER_DATA (buf);
size = GST_BUFFER_SIZE (buf);
-
- GST_DEBUG_OBJECT (dec, "received buffer of size %u", size);
- if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf)
- && GST_BUFFER_OFFSET_END_IS_VALID (buf)) {
- dec->granulepos = GST_BUFFER_OFFSET_END (buf);
- GST_DEBUG_OBJECT (dec,
- "Taking granulepos from upstream: %" G_GUINT64_FORMAT,
- dec->granulepos);
- }
-
- /* copy timestamp */
} else {
+ /* FIXME ? actually consider how much concealment is needed */
/* concealment data, pass NULL as the bits parameters */
GST_DEBUG_OBJECT (dec, "creating concealment data");
data = NULL;
size = 0;
}
- if (dec->discont) {
+ /* FIXME really needed ?; this might lead to skipping samples below
+ * which kind of messes with subsequent timestamping */
+ if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))) {
#ifdef CELT_GET_LOOKAHEAD_REQUEST
/* what will be 0.11.5, I guess, but no versioning yet in git */
celt_decoder_ctl (dec->state, CELT_GET_LOOKAHEAD_REQUEST, &skip);
@@ -740,9 +337,9 @@ celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
#endif
}
- res = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
+ res = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header.nb_channels * 2,
- GST_PAD_CAPS (dec->srcpad), &outbuf);
+ GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
if (res != GST_FLOW_OK) {
GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
@@ -775,59 +372,88 @@ celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
skip * dec->header.nb_channels * 2;
}
- if (dec->granulepos == -1) {
- if (dec->segment.format != GST_FORMAT_TIME) {
- GST_WARNING_OBJECT (dec, "segment not initialized or not TIME format");
- dec->granulepos = dec->frame_size;
- } else {
- dec->granulepos = gst_util_uint64_scale_int (dec->segment.last_stop,
- dec->header.sample_rate, GST_SECOND) + dec->frame_size;
- }
- GST_DEBUG_OBJECT (dec, "granulepos=%" G_GINT64_FORMAT, dec->granulepos);
- }
+ res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
- if (!GST_CLOCK_TIME_IS_VALID (timestamp))
- timestamp = gst_util_uint64_scale_int (dec->granulepos - dec->frame_size,
- GST_SECOND, dec->header.sample_rate);
+ if (res != GST_FLOW_OK)
+ GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
- GST_DEBUG_OBJECT (dec, "timestamp=%" GST_TIME_FORMAT,
- GST_TIME_ARGS (timestamp));
+ return res;
- GST_BUFFER_OFFSET (outbuf) = dec->granulepos - dec->frame_size;
- GST_BUFFER_OFFSET_END (outbuf) = dec->granulepos;
- GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
- GST_BUFFER_DURATION (outbuf) = dec->frame_duration;
- if (dec->discont) {
- GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
- dec->discont = 0;
+ /* ERRORS */
+not_negotiated:
+ {
+ GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
+ ("decoder not initialized"));
+ return GST_FLOW_NOT_NEGOTIATED;
}
+}
- dec->granulepos += dec->frame_size;
- dec->segment.last_stop += dec->frame_duration;
+static gboolean
+gst_celt_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
+{
+ GstCeltDec *dec = GST_CELT_DEC (bdec);
+ gboolean ret = TRUE;
+ GstStructure *s;
+ const GValue *streamheader;
- GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%"
- GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
- GST_TIME_ARGS (dec->frame_duration));
+ s = gst_caps_get_structure (caps, 0);
+ if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
+ G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
+ gst_value_array_get_size (streamheader) >= 2) {
+ const GValue *header, *vorbiscomment;
+ GstBuffer *buf;
+ GstFlowReturn res = GST_FLOW_OK;
- res = gst_pad_push (dec->srcpad, outbuf);
+ header = gst_value_array_get_value (streamheader, 0);
+ if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
+ buf = gst_value_get_buffer (header);
+ res = gst_celt_dec_parse_header (dec, buf);
+ if (res != GST_FLOW_OK)
+ goto done;
+ gst_buffer_replace (&dec->streamheader, buf);
+ }
- if (res != GST_FLOW_OK)
- GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
+ vorbiscomment = gst_value_array_get_value (streamheader, 1);
+ if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
+ buf = gst_value_get_buffer (vorbiscomment);
+ res = gst_celt_dec_parse_comments (dec, buf);
+ if (res != GST_FLOW_OK)
+ goto done;
+ gst_buffer_replace (&dec->vorbiscomment, buf);
+ }
- return res;
+ g_list_foreach (dec->extra_headers, (GFunc) gst_mini_object_unref, NULL);
+ g_list_free (dec->extra_headers);
+ dec->extra_headers = NULL;
+
+ if (gst_value_array_get_size (streamheader) > 2) {
+ gint i, n;
+
+ n = gst_value_array_get_size (streamheader);
+ for (i = 2; i < n; i++) {
+ header = gst_value_array_get_value (streamheader, i);
+ buf = gst_value_get_buffer (header);
+ dec->extra_headers =
+ g_list_prepend (dec->extra_headers, gst_buffer_ref (buf));
+ }
+ }
+ }
+
+done:
+ return ret;
}
static GstFlowReturn
-celt_dec_chain (GstPad * pad, GstBuffer * buf)
+gst_celt_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
{
GstFlowReturn res;
GstCeltDec *dec;
- dec = GST_CELT_DEC (gst_pad_get_parent (pad));
+ dec = GST_CELT_DEC (bdec);
- if (GST_BUFFER_IS_DISCONT (buf)) {
- dec->discont = TRUE;
- }
+ /* no fancy draining */
+ if (G_UNLIKELY (!buf))
+ return GST_FLOW_OK;
/* If we have the streamheader and vorbiscomment from the caps already
* ignore them here */
@@ -835,10 +461,14 @@ celt_dec_chain (GstPad * pad, GstBuffer * buf)
if (GST_BUFFER_SIZE (dec->streamheader) == GST_BUFFER_SIZE (buf)
&& memcmp (GST_BUFFER_DATA (dec->streamheader), GST_BUFFER_DATA (buf),
GST_BUFFER_SIZE (buf)) == 0) {
+ GST_DEBUG_OBJECT (dec, "found streamheader");
+ gst_audio_decoder_finish_frame (bdec, NULL, 1);
res = GST_FLOW_OK;
} else if (GST_BUFFER_SIZE (dec->vorbiscomment) == GST_BUFFER_SIZE (buf)
&& memcmp (GST_BUFFER_DATA (dec->vorbiscomment), GST_BUFFER_DATA (buf),
GST_BUFFER_SIZE (buf)) == 0) {
+ GST_DEBUG_OBJECT (dec, "found vorbiscomments");
+ gst_audio_decoder_finish_frame (bdec, NULL, 1);
res = GST_FLOW_OK;
} else {
GList *l;
@@ -848,66 +478,36 @@ celt_dec_chain (GstPad * pad, GstBuffer * buf)
if (GST_BUFFER_SIZE (header) == GST_BUFFER_SIZE (buf) &&
memcmp (GST_BUFFER_DATA (header), GST_BUFFER_DATA (buf),
GST_BUFFER_SIZE (buf)) == 0) {
+ GST_DEBUG_OBJECT (dec, "found extra header buffer");
+ gst_audio_decoder_finish_frame (bdec, NULL, 1);
res = GST_FLOW_OK;
goto done;
}
}
- res =
- celt_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
- GST_BUFFER_DURATION (buf));
+ res = gst_celt_dec_parse_data (dec, buf);
}
} else {
/* Otherwise fall back to packet counting and assume that the
* first two packets are the headers. */
- if (dec->packetno == 0)
- res = celt_dec_chain_parse_header (dec, buf);
- else if (dec->packetno == 1)
- res = celt_dec_chain_parse_comments (dec, buf);
- else if (dec->packetno <= 1 + dec->header.extra_headers)
+ if (dec->packetno == 0) {
+ GST_DEBUG_OBJECT (dec, "counted streamheader");
+ res = gst_celt_dec_parse_header (dec, buf);
+ gst_audio_decoder_finish_frame (bdec, NULL, 1);
+ } else if (dec->packetno == 1) {
+ GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
+ res = gst_celt_dec_parse_comments (dec, buf);
+ gst_audio_decoder_finish_frame (bdec, NULL, 1);
+ } else if (dec->packetno <= 1 + dec->header.extra_headers) {
+ GST_DEBUG_OBJECT (dec, "counted extra header");
+ gst_audio_decoder_finish_frame (bdec, NULL, 1);
res = GST_FLOW_OK;
- else
- res = celt_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
- GST_BUFFER_DURATION (buf));
+ } else {
+ res = gst_celt_dec_parse_data (dec, buf);
+ }
}
done:
dec->packetno++;
- gst_buffer_unref (buf);
- gst_object_unref (dec);
-
return res;
}
-
-static GstStateChangeReturn
-celt_dec_change_state (GstElement * element, GstStateChange transition)
-{
- GstStateChangeReturn ret;
- GstCeltDec *dec = GST_CELT_DEC (element);
-
- switch (transition) {
- case GST_STATE_CHANGE_NULL_TO_READY:
- case GST_STATE_CHANGE_READY_TO_PAUSED:
- case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
- default:
- break;
- }
-
- ret = parent_class->change_state (element, transition);
- if (ret != GST_STATE_CHANGE_SUCCESS)
- return ret;
-
- switch (transition) {
- case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
- break;
- case GST_STATE_CHANGE_PAUSED_TO_READY:
- gst_celt_dec_reset (dec);
- break;
- case GST_STATE_CHANGE_READY_TO_NULL:
- break;
- default:
- break;
- }
-
- return ret;
-}
diff --git a/ext/celt/gstceltdec.h b/ext/celt/gstceltdec.h
index b6b49605d..9baf719d9 100644
--- a/ext/celt/gstceltdec.h
+++ b/ext/celt/gstceltdec.h
@@ -22,6 +22,7 @@
#define __GST_CELT_DEC_H__
#include <gst/gst.h>
+#include <gst/audio/gstaudiodecoder.h>
#include <celt/celt.h>
#include <celt/celt_header.h>
@@ -42,22 +43,15 @@ typedef struct _GstCeltDec GstCeltDec;
typedef struct _GstCeltDecClass GstCeltDecClass;
struct _GstCeltDec {
- GstElement element;
-
- /* pads */
- GstPad *sinkpad;
- GstPad *srcpad;
+ GstAudioDecoder element;
CELTDecoder *state;
CELTMode *mode;
CELTHeader header;
gint frame_size;
- GstClockTime frame_duration;
guint64 packetno;
- GstSegment segment; /* STREAM LOCK */
- gint64 granulepos; /* -1 = needs to be set from current time */
gboolean discont;
GstBuffer *streamheader;
@@ -66,7 +60,7 @@ struct _GstCeltDec {
};
struct _GstCeltDecClass {
- GstElementClass parent_class;
+ GstAudioDecoderClass parent_class;
};
GType gst_celt_dec_get_type (void);
diff --git a/ext/celt/gstceltenc.c b/ext/celt/gstceltenc.c
index c2011b31c..37366a2a5 100644
--- a/ext/celt/gstceltenc.c
+++ b/ext/celt/gstceltenc.c
@@ -19,7 +19,7 @@
*/
/*
- * Based on the speexenc element
+ * Based on the celtenc element
*/
/**
@@ -115,41 +115,35 @@ enum
PROP_START_BAND
};
-static void gst_celt_enc_finalize (GObject * object);
-
-static gboolean gst_celt_enc_sinkevent (GstPad * pad, GstEvent * event);
-static GstFlowReturn gst_celt_enc_chain (GstPad * pad, GstBuffer * buf);
-static gboolean gst_celt_enc_setup (GstCeltEnc * enc);
-
static void gst_celt_enc_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_celt_enc_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
-static GstStateChangeReturn gst_celt_enc_change_state (GstElement * element,
- GstStateChange transition);
-static GstFlowReturn gst_celt_enc_encode (GstCeltEnc * enc, gboolean flush);
+static gboolean gst_celt_enc_start (GstAudioEncoder * enc);
+static gboolean gst_celt_enc_stop (GstAudioEncoder * enc);
+static gboolean gst_celt_enc_set_format (GstAudioEncoder * enc,
+ GstAudioInfo * info);
+static GstFlowReturn gst_celt_enc_handle_frame (GstAudioEncoder * enc,
+ GstBuffer * in_buf);
+static gboolean gst_celt_enc_sink_event (GstAudioEncoder * enc,
+ GstEvent * event);
+static GstFlowReturn gst_celt_enc_pre_push (GstAudioEncoder * benc,
+ GstBuffer ** buffer);
static void
gst_celt_enc_setup_interfaces (GType celtenc_type)
{
static const GInterfaceInfo tag_setter_info = { NULL, NULL, NULL };
- const GInterfaceInfo preset_interface_info = {
- NULL, /* interface_init */
- NULL, /* interface_finalize */
- NULL /* interface_data */
- };
g_type_add_interface_static (celtenc_type, GST_TYPE_TAG_SETTER,
&tag_setter_info);
- g_type_add_interface_static (celtenc_type, GST_TYPE_PRESET,
- &preset_interface_info);
GST_DEBUG_CATEGORY_INIT (celtenc_debug, "celtenc", 0, "Celt encoder");
}
-GST_BOILERPLATE_FULL (GstCeltEnc, gst_celt_enc, GstElement, GST_TYPE_ELEMENT,
- gst_celt_enc_setup_interfaces);
+GST_BOILERPLATE_FULL (GstCeltEnc, gst_celt_enc, GstAudioEncoder,
+ GST_TYPE_AUDIO_ENCODER, gst_celt_enc_setup_interfaces);
static void
gst_celt_enc_base_init (gpointer g_class)
@@ -170,14 +164,21 @@ static void
gst_celt_enc_class_init (GstCeltEncClass * klass)
{
GObjectClass *gobject_class;
- GstElementClass *gstelement_class;
+ GstAudioEncoderClass *gstbase_class;
gobject_class = (GObjectClass *) klass;
- gstelement_class = (GstElementClass *) klass;
+ gstbase_class = (GstAudioEncoderClass *) klass;
gobject_class->set_property = gst_celt_enc_set_property;
gobject_class->get_property = gst_celt_enc_get_property;
+ gstbase_class->start = GST_DEBUG_FUNCPTR (gst_celt_enc_start);
+ gstbase_class->stop = GST_DEBUG_FUNCPTR (gst_celt_enc_stop);
+ gstbase_class->set_format = GST_DEBUG_FUNCPTR (gst_celt_enc_set_format);
+ gstbase_class->handle_frame = GST_DEBUG_FUNCPTR (gst_celt_enc_handle_frame);
+ gstbase_class->event = GST_DEBUG_FUNCPTR (gst_celt_enc_sink_event);
+ gstbase_class->pre_push = GST_DEBUG_FUNCPTR (gst_celt_enc_pre_push);
+
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BITRATE,
g_param_spec_int ("bitrate", "Encoding Bit-rate",
"Specify an encoding bit-rate (in bps).",
@@ -210,411 +211,55 @@ gst_celt_enc_class_init (GstCeltEncClass * klass)
"Controls the start band that should be used",
0, G_MAXINT, DEFAULT_START_BAND,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-
- gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_celt_enc_finalize);
-
- gstelement_class->change_state =
- GST_DEBUG_FUNCPTR (gst_celt_enc_change_state);
-}
-
-static void
-gst_celt_enc_finalize (GObject * object)
-{
- GstCeltEnc *enc;
-
- enc = GST_CELT_ENC (object);
-
- g_object_unref (enc->adapter);
-
- G_OBJECT_CLASS (parent_class)->finalize (object);
-}
-
-static gboolean
-gst_celt_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
-{
- GstCeltEnc *enc;
- GstStructure *structure;
- GstCaps *otherpadcaps;
-
- enc = GST_CELT_ENC (GST_PAD_PARENT (pad));
- enc->setup = FALSE;
- enc->frame_size = DEFAULT_FRAMESIZE;
- otherpadcaps = gst_pad_get_allowed_caps (pad);
-
- structure = gst_caps_get_structure (caps, 0);
- gst_structure_get_int (structure, "channels", &enc->channels);
- gst_structure_get_int (structure, "rate", &enc->rate);
-
- if (otherpadcaps) {
- if (!gst_caps_is_empty (otherpadcaps)) {
- GstStructure *ps = gst_caps_get_structure (otherpadcaps, 0);
- gst_structure_get_int (ps, "frame-size", &enc->frame_size);
- }
- gst_caps_unref (otherpadcaps);
- }
-
- if (enc->requested_frame_size > 0)
- enc->frame_size = enc->requested_frame_size;
-
- GST_DEBUG_OBJECT (pad, "channels=%d rate=%d frame-size=%d",
- enc->channels, enc->rate, enc->frame_size);
-
- gst_celt_enc_setup (enc);
-
- return enc->setup;
-}
-
-
-static GstCaps *
-gst_celt_enc_sink_getcaps (GstPad * pad)
-{
- GstCaps *caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
- GstCaps *peercaps = NULL;
- GstCeltEnc *enc = GST_CELT_ENC (gst_pad_get_parent_element (pad));
-
- peercaps = gst_pad_peer_get_caps (enc->srcpad);
-
- if (peercaps) {
- if (!gst_caps_is_empty (peercaps) && !gst_caps_is_any (peercaps)) {
- GstStructure *ps = gst_caps_get_structure (peercaps, 0);
- GstStructure *s = gst_caps_get_structure (caps, 0);
- gint rate, channels;
-
- if (gst_structure_get_int (ps, "rate", &rate)) {
- gst_structure_fixate_field_nearest_int (s, "rate", rate);
- }
-
- if (gst_structure_get_int (ps, "channels", &channels)) {
- gst_structure_fixate_field_nearest_int (s, "channels", channels);
- }
- }
- gst_caps_unref (peercaps);
- }
-
- gst_object_unref (enc);
-
- return caps;
-}
-
-
-static gboolean
-gst_celt_enc_convert_src (GstPad * pad, GstFormat src_format, gint64 src_value,
- GstFormat * dest_format, gint64 * dest_value)
-{
- gboolean res = TRUE;
- GstCeltEnc *enc;
- gint64 avg;
-
- enc = GST_CELT_ENC (GST_PAD_PARENT (pad));
-
- if (enc->samples_in == 0 || enc->bytes_out == 0 || enc->rate == 0)
- return FALSE;
-
- avg = (enc->bytes_out * enc->rate) / (enc->samples_in);
-
- switch (src_format) {
- case GST_FORMAT_BYTES:
- switch (*dest_format) {
- case GST_FORMAT_TIME:
- *dest_value = src_value * GST_SECOND / avg;
- break;
- default:
- res = FALSE;
- }
- break;
- case GST_FORMAT_TIME:
- switch (*dest_format) {
- case GST_FORMAT_BYTES:
- *dest_value = src_value * avg / GST_SECOND;
- break;
- default:
- res = FALSE;
- }
- break;
- default:
- res = FALSE;
- }
- return res;
-}
-
-static gboolean
-gst_celt_enc_convert_sink (GstPad * pad, GstFormat src_format,
- gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
-{
- gboolean res = TRUE;
- guint scale = 1;
- gint bytes_per_sample;
- GstCeltEnc *enc;
-
- enc = GST_CELT_ENC (GST_PAD_PARENT (pad));
-
- bytes_per_sample = enc->channels * 2;
-
- switch (src_format) {
- case GST_FORMAT_BYTES:
- switch (*dest_format) {
- case GST_FORMAT_DEFAULT:
- if (bytes_per_sample == 0)
- return FALSE;
- *dest_value = src_value / bytes_per_sample;
- break;
- case GST_FORMAT_TIME:
- {
- gint byterate = bytes_per_sample * enc->rate;
-
- if (byterate == 0)
- return FALSE;
- *dest_value = src_value * GST_SECOND / byterate;
- break;
- }
- default:
- res = FALSE;
- }
- break;
- case GST_FORMAT_DEFAULT:
- switch (*dest_format) {
- case GST_FORMAT_BYTES:
- *dest_value = src_value * bytes_per_sample;
- break;
- case GST_FORMAT_TIME:
- if (enc->rate == 0)
- return FALSE;
- *dest_value = src_value * GST_SECOND / enc->rate;
- break;
- default:
- res = FALSE;
- }
- break;
- case GST_FORMAT_TIME:
- switch (*dest_format) {
- case GST_FORMAT_BYTES:
- scale = bytes_per_sample;
- /* fallthrough */
- case GST_FORMAT_DEFAULT:
- *dest_value = src_value * scale * enc->rate / GST_SECOND;
- break;
- default:
- res = FALSE;
- }
- break;
- default:
- res = FALSE;
- }
- return res;
-}
-
-static gint64
-gst_celt_enc_get_latency (GstCeltEnc * enc)
-{
- return gst_util_uint64_scale (enc->frame_size, GST_SECOND, enc->rate);
-}
-
-static const GstQueryType *
-gst_celt_enc_get_query_types (GstPad * pad)
-{
- static const GstQueryType gst_celt_enc_src_query_types[] = {
- GST_QUERY_POSITION,
- GST_QUERY_DURATION,
- GST_QUERY_CONVERT,
- GST_QUERY_LATENCY,
- 0
- };
-
- return gst_celt_enc_src_query_types;
-}
-
-static gboolean
-gst_celt_enc_src_query (GstPad * pad, GstQuery * query)
-{
- gboolean res = TRUE;
- GstCeltEnc *enc;
-
- enc = GST_CELT_ENC (gst_pad_get_parent (pad));
-
- switch (GST_QUERY_TYPE (query)) {
- case GST_QUERY_POSITION:
- {
- GstFormat fmt, req_fmt;
- gint64 pos, val;
-
- gst_query_parse_position (query, &req_fmt, NULL);
- if ((res = gst_pad_query_peer_position (enc->sinkpad, &req_fmt, &val))) {
- gst_query_set_position (query, req_fmt, val);
- break;
- }
-
- fmt = GST_FORMAT_TIME;
- if (!(res = gst_pad_query_peer_position (enc->sinkpad, &fmt, &pos)))
- break;
-
- if ((res =
- gst_pad_query_peer_convert (enc->sinkpad, fmt, pos, &req_fmt,
- &val)))
- gst_query_set_position (query, req_fmt, val);
-
- break;
- }
- case GST_QUERY_DURATION:
- {
- GstFormat fmt, req_fmt;
- gint64 dur, val;
-
- gst_query_parse_duration (query, &req_fmt, NULL);
- if ((res = gst_pad_query_peer_duration (enc->sinkpad, &req_fmt, &val))) {
- gst_query_set_duration (query, req_fmt, val);
- break;
- }
-
- fmt = GST_FORMAT_TIME;
- if (!(res = gst_pad_query_peer_duration (enc->sinkpad, &fmt, &dur)))
- break;
-
- if ((res =
- gst_pad_query_peer_convert (enc->sinkpad, fmt, dur, &req_fmt,
- &val))) {
- gst_query_set_duration (query, req_fmt, val);
- }
- break;
- }
- case GST_QUERY_CONVERT:
- {
- GstFormat src_fmt, dest_fmt;
- gint64 src_val, dest_val;
-
- gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
- if (!(res = gst_celt_enc_convert_src (pad, src_fmt, src_val, &dest_fmt,
- &dest_val)))
- goto error;
- gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
- break;
- }
- case GST_QUERY_LATENCY:
- {
- gboolean live;
- GstClockTime min_latency, max_latency;
- gint64 latency;
-
- if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
- gst_query_parse_latency (query, &live, &min_latency, &max_latency);
-
- latency = gst_celt_enc_get_latency (enc);
-
- /* add our latency */
- min_latency += latency;
- if (max_latency != -1)
- max_latency += latency;
-
- gst_query_set_latency (query, live, min_latency, max_latency);
- }
- break;
- }
- default:
- res = gst_pad_peer_query (pad, query);
- break;
- }
-
-error:
-
- gst_object_unref (enc);
-
- return res;
-}
-
-static gboolean
-gst_celt_enc_sink_query (GstPad * pad, GstQuery * query)
-{
- gboolean res = TRUE;
-
- switch (GST_QUERY_TYPE (query)) {
- case GST_QUERY_CONVERT:
- {
- GstFormat src_fmt, dest_fmt;
- gint64 src_val, dest_val;
-
- gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
- if (!(res =
- gst_celt_enc_convert_sink (pad, src_fmt, src_val, &dest_fmt,
- &dest_val)))
- goto error;
- gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
- break;
- }
- default:
- res = gst_pad_query_default (pad, query);
- break;
- }
-
-error:
- return res;
}
static void
gst_celt_enc_init (GstCeltEnc * enc, GstCeltEncClass * klass)
{
- enc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
- gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
- gst_pad_set_event_function (enc->sinkpad,
- GST_DEBUG_FUNCPTR (gst_celt_enc_sinkevent));
- gst_pad_set_chain_function (enc->sinkpad,
- GST_DEBUG_FUNCPTR (gst_celt_enc_chain));
- gst_pad_set_setcaps_function (enc->sinkpad,
- GST_DEBUG_FUNCPTR (gst_celt_enc_sink_setcaps));
- gst_pad_set_getcaps_function (enc->sinkpad,
- GST_DEBUG_FUNCPTR (gst_celt_enc_sink_getcaps));
- gst_pad_set_query_function (enc->sinkpad,
- GST_DEBUG_FUNCPTR (gst_celt_enc_sink_query));
-
- enc->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
- gst_pad_set_query_function (enc->srcpad,
- GST_DEBUG_FUNCPTR (gst_celt_enc_src_query));
- gst_pad_set_query_type_function (enc->srcpad,
- GST_DEBUG_FUNCPTR (gst_celt_enc_get_query_types));
- gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
-
- enc->channels = -1;
- enc->rate = -1;
-
enc->bitrate = DEFAULT_BITRATE;
enc->frame_size = DEFAULT_FRAMESIZE;
- enc->requested_frame_size = -1;
enc->cbr = DEFAULT_CBR;
enc->complexity = DEFAULT_COMPLEXITY;
enc->max_bitrate = DEFAULT_MAX_BITRATE;
enc->prediction = DEFAULT_PREDICTION;
+}
- enc->setup = FALSE;
+static gboolean
+gst_celt_enc_start (GstAudioEncoder * benc)
+{
+ GstCeltEnc *enc = GST_CELT_ENC (benc);
+
+ GST_DEBUG_OBJECT (enc, "start");
+ enc->channels = -1;
+ enc->rate = -1;
enc->header_sent = FALSE;
- enc->adapter = gst_adapter_new ();
+ return TRUE;
}
-static GstBuffer *
-gst_celt_enc_create_metadata_buffer (GstCeltEnc * enc)
+static gboolean
+gst_celt_enc_stop (GstAudioEncoder * benc)
{
- const GstTagList *tags;
- GstTagList *empty_tags = NULL;
- GstBuffer *comments = NULL;
-
- tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (enc));
-
- GST_DEBUG_OBJECT (enc, "tags = %" GST_PTR_FORMAT, tags);
+ GstCeltEnc *enc = GST_CELT_ENC (benc);
- if (tags == NULL) {
- /* FIXME: better fix chain of callers to not write metadata at all,
- * if there is none */
- empty_tags = gst_tag_list_new ();
- tags = empty_tags;
+ GST_DEBUG_OBJECT (enc, "stop");
+ enc->header_sent = FALSE;
+ if (enc->state) {
+ celt_encoder_destroy (enc->state);
+ enc->state = NULL;
}
- comments = gst_tag_list_to_vorbiscomment_buffer (tags, NULL,
- 0, "Encoded with GStreamer Celtenc");
+ if (enc->mode) {
+ celt_mode_destroy (enc->mode);
+ enc->mode = NULL;
+ }
+ memset (&enc->header, 0, sizeof (enc->header));
- GST_BUFFER_OFFSET (comments) = enc->bytes_out;
- GST_BUFFER_OFFSET_END (comments) = 0;
+ g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
+ enc->headers = NULL;
- if (empty_tags)
- gst_tag_list_free (empty_tags);
+ gst_tag_setter_reset_tags (GST_TAG_SETTER (enc));
- return comments;
+ return TRUE;
}
static gboolean
@@ -622,8 +267,6 @@ gst_celt_enc_setup (GstCeltEnc * enc)
{
gint error = CELT_OK;
- enc->setup = FALSE;
-
#ifdef HAVE_CELT_0_7
enc->mode = celt_mode_create (enc->rate, enc->frame_size, &error);
#else
@@ -679,8 +322,6 @@ gst_celt_enc_setup (GstCeltEnc * enc)
GST_LOG_OBJECT (enc, "we have frame size %d", enc->frame_size);
- enc->setup = TRUE;
-
return TRUE;
mode_initialization_failed:
@@ -696,93 +337,70 @@ encoder_creation_failed:
return FALSE;
}
-/* prepare a buffer for transmission */
-static GstBuffer *
-gst_celt_enc_buffer_from_data (GstCeltEnc * enc, guchar * data,
- guint data_len, gint64 granulepos)
+static gint64
+gst_celt_enc_get_latency (GstCeltEnc * enc)
{
- GstBuffer *outbuf;
-
- outbuf = gst_buffer_new ();
- GST_BUFFER_DATA (outbuf) = data;
- GST_BUFFER_MALLOCDATA (outbuf) = data;
- GST_BUFFER_SIZE (outbuf) = data_len;
- GST_BUFFER_OFFSET (outbuf) = enc->bytes_out;
- GST_BUFFER_OFFSET_END (outbuf) = granulepos;
-
- GST_LOG_OBJECT (enc, "encoded buffer of %u bytes", GST_BUFFER_SIZE (outbuf));
- return outbuf;
+ return gst_util_uint64_scale (enc->frame_size, GST_SECOND, enc->rate);
}
-
-/* push out the buffer and do internal bookkeeping */
-static GstFlowReturn
-gst_celt_enc_push_buffer (GstCeltEnc * enc, GstBuffer * buffer)
+static gboolean
+gst_celt_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
{
- guint size;
-
- size = GST_BUFFER_SIZE (buffer);
+ GstCeltEnc *enc;
+ GstCaps *otherpadcaps;
- enc->bytes_out += size;
+ enc = GST_CELT_ENC (benc);
- GST_DEBUG_OBJECT (enc, "pushing output buffer of size %u", size);
+ enc->channels = GST_AUDIO_INFO_CHANNELS (info);
+ enc->rate = GST_AUDIO_INFO_RATE (info);
- return gst_pad_push (enc->srcpad, buffer);
-}
+ /* handle reconfigure */
+ if (enc->state) {
+ celt_encoder_destroy (enc->state);
+ enc->state = NULL;
+ }
+ if (enc->mode) {
+ celt_mode_destroy (enc->mode);
+ enc->mode = NULL;
+ }
+ memset (&enc->header, 0, sizeof (enc->header));
-static GstCaps *
-gst_celt_enc_set_header_on_caps (GstCaps * caps, GstBuffer * buf1,
- GstBuffer * buf2)
-{
- GstStructure *structure = NULL;
- GstBuffer *buf;
- GValue array = { 0 };
- GValue value = { 0 };
+ otherpadcaps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (enc));
+ if (otherpadcaps) {
+ if (!gst_caps_is_empty (otherpadcaps)) {
+ GstStructure *ps = gst_caps_get_structure (otherpadcaps, 0);
+ gst_structure_get_int (ps, "frame-size", &enc->frame_size);
+ }
+ gst_caps_unref (otherpadcaps);
+ }
- caps = gst_caps_make_writable (caps);
- structure = gst_caps_get_structure (caps, 0);
+ if (enc->requested_frame_size > 0)
+ enc->frame_size = enc->requested_frame_size;
- g_assert (gst_buffer_is_metadata_writable (buf1));
- g_assert (gst_buffer_is_metadata_writable (buf2));
+ GST_DEBUG_OBJECT (enc, "channels=%d rate=%d frame-size=%d",
+ enc->channels, enc->rate, enc->frame_size);
- /* mark buffers */
- GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_IN_CAPS);
- GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_IN_CAPS);
+ if (!gst_celt_enc_setup (enc))
+ return FALSE;
- /* put buffers in a fixed list */
- g_value_init (&array, GST_TYPE_ARRAY);
- g_value_init (&value, GST_TYPE_BUFFER);
- buf = gst_buffer_copy (buf1);
- gst_value_set_buffer (&value, buf);
- gst_buffer_unref (buf);
- gst_value_array_append_value (&array, &value);
- g_value_unset (&value);
- g_value_init (&value, GST_TYPE_BUFFER);
- buf = gst_buffer_copy (buf2);
- gst_value_set_buffer (&value, buf);
- gst_buffer_unref (buf);
- gst_value_array_append_value (&array, &value);
- gst_structure_set_value (structure, "streamheader", &array);
- g_value_unset (&value);
- g_value_unset (&array);
+ /* feedback to base class */
+ gst_audio_encoder_set_latency (benc,
+ gst_celt_enc_get_latency (enc), gst_celt_enc_get_latency (enc));
+ gst_audio_encoder_set_frame_samples_min (benc, enc->frame_size);
+ gst_audio_encoder_set_frame_samples_max (benc, enc->frame_size);
+ gst_audio_encoder_set_frame_max (benc, 1);
- return caps;
+ return TRUE;
}
-
static gboolean
-gst_celt_enc_sinkevent (GstPad * pad, GstEvent * event)
+gst_celt_enc_sink_event (GstAudioEncoder * benc, GstEvent * event)
{
- gboolean res = TRUE;
GstCeltEnc *enc;
- enc = GST_CELT_ENC (gst_pad_get_parent (pad));
+ enc = GST_CELT_ENC (benc);
switch (GST_EVENT_TYPE (event)) {
- case GST_EVENT_EOS:
- gst_celt_enc_encode (enc, TRUE);
- res = gst_pad_event_default (pad, event);
- break;
case GST_EVENT_TAG:
{
GstTagList *list;
@@ -791,114 +409,177 @@ gst_celt_enc_sinkevent (GstPad * pad, GstEvent * event)
gst_event_parse_tag (event, &list);
gst_tag_setter_merge_tags (setter, list, mode);
- res = gst_pad_event_default (pad, event);
break;
}
default:
- res = gst_pad_event_default (pad, event);
break;
}
- gst_object_unref (enc);
+ /* we only peeked, let base class handle it */
+ return FALSE;
+}
- return res;
+static GstBuffer *
+gst_celt_enc_create_metadata_buffer (GstCeltEnc * enc)
+{
+ const GstTagList *tags;
+ GstTagList *empty_tags = NULL;
+ GstBuffer *comments = NULL;
+
+ tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (enc));
+
+ GST_DEBUG_OBJECT (enc, "tags = %" GST_PTR_FORMAT, tags);
+
+ if (tags == NULL) {
+ /* FIXME: better fix chain of callers to not write metadata at all,
+ * if there is none */
+ empty_tags = gst_tag_list_new ();
+ tags = empty_tags;
+ }
+ comments = gst_tag_list_to_vorbiscomment_buffer (tags, NULL,
+ 0, "Encoded with GStreamer Celtenc");
+
+ GST_BUFFER_OFFSET (comments) = 0;
+ GST_BUFFER_OFFSET_END (comments) = 0;
+
+ if (empty_tags)
+ gst_tag_list_free (empty_tags);
+
+ return comments;
}
static GstFlowReturn
-gst_celt_enc_encode (GstCeltEnc * enc, gboolean flush)
+gst_celt_enc_encode (GstCeltEnc * enc, GstBuffer * buf)
{
-
GstFlowReturn ret = GST_FLOW_OK;
gint frame_size = enc->frame_size;
gint bytes = frame_size * 2 * enc->channels;
gint bytes_per_packet;
+ gint16 *data, *data0 = NULL;
+ gint outsize, size;
+ GstBuffer *outbuf;
+
+ if (G_LIKELY (buf)) {
+ data = (gint16 *) GST_BUFFER_DATA (buf);
+ size = GST_BUFFER_SIZE (buf);
+ if (G_UNLIKELY (size % bytes)) {
+ GST_DEBUG_OBJECT (enc, "draining; adding silence samples");
+ size = ((size / bytes) + 1) * bytes;
+ data0 = data = g_malloc0 (size);
+ memcpy (data, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
+ }
+ } else {
+ GST_DEBUG_OBJECT (enc, "nothing to drain");
+ goto done;
+ }
+
+ frame_size = size / (2 * enc->channels);
if (enc->cbr) {
- bytes_per_packet = (enc->bitrate * enc->frame_size / enc->rate + 4) / 8;
+ bytes_per_packet = (enc->bitrate * frame_size / enc->rate + 4) / 8;
} else {
- bytes_per_packet = (enc->max_bitrate * enc->frame_size / enc->rate + 4) / 8;
+ bytes_per_packet = (enc->max_bitrate * frame_size / enc->rate + 4) / 8;
}
- if (flush && gst_adapter_available (enc->adapter) % bytes != 0) {
- guint diff = bytes - gst_adapter_available (enc->adapter) % bytes;
- GstBuffer *buf = gst_buffer_new_and_alloc (diff);
+ ret = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc),
+ GST_BUFFER_OFFSET_NONE, bytes_per_packet,
+ GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (enc)), &outbuf);
+
+ if (GST_FLOW_OK != ret)
+ goto done;
+
+ GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", frame_size, bytes);
- memset (GST_BUFFER_DATA (buf), 0, diff);
- gst_adapter_push (enc->adapter, buf);
+#ifdef HAVE_CELT_0_8
+ outsize =
+ celt_encode (enc->state, data, frame_size,
+ GST_BUFFER_DATA (outbuf), bytes_per_packet);
+#else
+ outsize =
+ celt_encode (enc->state, data, NULL,
+ GST_BUFFER_DATA (outbuf), bytes_per_packet);
+#endif
+
+ if (outsize < 0) {
+ GST_ERROR_OBJECT (enc, "Encoding failed: %d", outsize);
+ ret = GST_FLOW_ERROR;
+ goto done;
}
+ GST_DEBUG_OBJECT (enc, "encoding %d bytes", bytes);
- while (gst_adapter_available (enc->adapter) >= bytes) {
- gint16 *data;
- gint outsize;
- GstBuffer *outbuf;
+ ret = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc),
+ outbuf, frame_size);
- ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad,
- GST_BUFFER_OFFSET_NONE, bytes_per_packet, GST_PAD_CAPS (enc->srcpad),
- &outbuf);
+done:
+ g_free (data0);
+ return ret;
+}
+
+/*
+ * (really really) FIXME: move into core (dixit tpm)
+ */
+/**
+ * _gst_caps_set_buffer_array:
+ * @caps: a #GstCaps
+ * @field: field in caps to set
+ * @buf: header buffers
+ *
+ * Adds given buffers to an array of buffers set as the given @field
+ * on the given @caps. List of buffer arguments must be NULL-terminated.
+ *
+ * Returns: input caps with a streamheader field added, or NULL if some error
+ */
+static GstCaps *
+_gst_caps_set_buffer_array (GstCaps * caps, const gchar * field,
+ GstBuffer * buf, ...)
+{
+ GstStructure *structure = NULL;
+ va_list va;
+ GValue array = { 0 };
+ GValue value = { 0 };
- if (GST_FLOW_OK != ret)
- goto done;
+ g_return_val_if_fail (caps != NULL, NULL);
+ g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
+ g_return_val_if_fail (field != NULL, NULL);
- data = (gint16 *) gst_adapter_take (enc->adapter, bytes);
- enc->samples_in += frame_size;
+ caps = gst_caps_make_writable (caps);
+ structure = gst_caps_get_structure (caps, 0);
- GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", frame_size, bytes);
+ g_value_init (&array, GST_TYPE_ARRAY);
-#ifdef HAVE_CELT_0_8
- outsize =
- celt_encode (enc->state, data, frame_size,
- GST_BUFFER_DATA (outbuf), bytes_per_packet);
-#else
- outsize =
- celt_encode (enc->state, data, NULL,
- GST_BUFFER_DATA (outbuf), bytes_per_packet);
-#endif
+ va_start (va, buf);
+ /* put buffers in a fixed list */
+ while (buf) {
+ g_assert (gst_buffer_is_metadata_writable (buf));
- g_free (data);
+ /* mark buffer */
+ GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
- if (outsize < 0) {
- GST_ERROR_OBJECT (enc, "Encoding failed: %d", outsize);
- ret = GST_FLOW_ERROR;
- goto done;
- }
+ g_value_init (&value, GST_TYPE_BUFFER);
+ buf = gst_buffer_copy (buf);
+ GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
+ gst_value_set_buffer (&value, buf);
+ gst_buffer_unref (buf);
+ gst_value_array_append_value (&array, &value);
+ g_value_unset (&value);
- GST_BUFFER_TIMESTAMP (outbuf) = enc->start_ts +
- gst_util_uint64_scale_int (enc->frameno_out * frame_size, GST_SECOND,
- enc->rate);
- GST_BUFFER_DURATION (outbuf) =
- gst_util_uint64_scale_int (frame_size, GST_SECOND, enc->rate);
- /* set gp time and granulepos; see gst-plugins-base/ext/ogg/README */
- GST_BUFFER_OFFSET_END (outbuf) = enc->granulepos_offset +
- ((enc->frameno + 1) * frame_size);
- GST_BUFFER_OFFSET (outbuf) =
- gst_util_uint64_scale_int (GST_BUFFER_OFFSET_END (outbuf), GST_SECOND,
- enc->rate);
-
- enc->frameno++;
- enc->frameno_out++;
-
- ret = gst_celt_enc_push_buffer (enc, outbuf);
-
- if ((GST_FLOW_OK != ret) && (GST_FLOW_NOT_LINKED != ret))
- goto done;
+ buf = va_arg (va, GstBuffer *);
}
-done:
+ gst_structure_set_value (structure, field, &array);
+ g_value_unset (&array);
- return ret;
+ return caps;
}
static GstFlowReturn
-gst_celt_enc_chain (GstPad * pad, GstBuffer * buf)
+gst_celt_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
{
GstCeltEnc *enc;
GstFlowReturn ret = GST_FLOW_OK;
- enc = GST_CELT_ENC (GST_PAD_PARENT (pad));
-
- if (!enc->setup)
- goto not_setup;
+ enc = GST_CELT_ENC (benc);
if (!enc->header_sent) {
/* Celt streams begin with two headers; the initial header (with
@@ -919,138 +600,53 @@ gst_celt_enc_chain (GstPad * pad, GstBuffer * buf)
g_free (data);
goto no_header;
}
- buf1 = gst_celt_enc_buffer_from_data (enc, data, header_size, 0);
+ buf1 = gst_buffer_new ();
+ GST_BUFFER_DATA (buf1) = GST_BUFFER_MALLOCDATA (buf1) = data;
+ GST_BUFFER_SIZE (buf1) = header_size;
+ GST_BUFFER_OFFSET_END (buf1) = 0;
+ GST_BUFFER_OFFSET (buf1) = 0;
/* create comment buffer */
buf2 = gst_celt_enc_create_metadata_buffer (enc);
/* mark and put on caps */
- caps = gst_pad_get_caps (enc->srcpad);
- caps = gst_celt_enc_set_header_on_caps (caps, buf1, buf2);
-
+ caps = gst_pad_get_caps (GST_AUDIO_ENCODER_SRC_PAD (enc));
gst_caps_set_simple (caps,
"rate", G_TYPE_INT, enc->rate,
"channels", G_TYPE_INT, enc->channels,
"frame-size", G_TYPE_INT, enc->frame_size, NULL);
+ caps = _gst_caps_set_buffer_array (caps, "streamheader", buf1, buf2, NULL);
/* negotiate with these caps */
GST_DEBUG_OBJECT (enc, "here are the caps: %" GST_PTR_FORMAT, caps);
GST_LOG_OBJECT (enc, "rate=%d channels=%d frame-size=%d",
enc->rate, enc->channels, enc->frame_size);
- gst_pad_set_caps (enc->srcpad, caps);
+ gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc), caps);
gst_buffer_set_caps (buf1, caps);
gst_buffer_set_caps (buf2, caps);
gst_caps_unref (caps);
/* push out buffers */
- ret = gst_celt_enc_push_buffer (enc, buf1);
-
- if (ret != GST_FLOW_OK) {
- gst_buffer_unref (buf2);
- goto done;
- }
-
- ret = gst_celt_enc_push_buffer (enc, buf2);
-
- if (ret != GST_FLOW_OK)
- goto done;
+ /* store buffers for later pre_push sending */
+ g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
+ enc->headers = NULL;
+ GST_DEBUG_OBJECT (enc, "storing header buffers");
+ enc->headers = g_slist_prepend (enc->headers, buf2);
+ enc->headers = g_slist_prepend (enc->headers, buf1);
enc->header_sent = TRUE;
}
- GST_DEBUG_OBJECT (enc, "received buffer of %u bytes", GST_BUFFER_SIZE (buf));
-
- /* Save the timestamp of the first buffer. This will be later
- * used as offset for all following buffers */
- if (enc->start_ts == GST_CLOCK_TIME_NONE) {
- if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
- enc->start_ts = GST_BUFFER_TIMESTAMP (buf);
- enc->granulepos_offset = gst_util_uint64_scale
- (GST_BUFFER_TIMESTAMP (buf), enc->rate, GST_SECOND);
- } else {
- enc->start_ts = 0;
- enc->granulepos_offset = 0;
- }
- }
-
-
- /* Check if we have a continous stream, if not drop some samples or the buffer or
- * insert some silence samples */
- if (enc->next_ts != GST_CLOCK_TIME_NONE &&
- GST_BUFFER_TIMESTAMP_IS_VALID (buf) &&
- GST_BUFFER_TIMESTAMP (buf) < enc->next_ts) {
- guint64 diff = enc->next_ts - GST_BUFFER_TIMESTAMP (buf);
- guint64 diff_bytes;
-
- GST_WARNING_OBJECT (enc, "Buffer is older than previous "
- "timestamp + duration (%" GST_TIME_FORMAT "< %" GST_TIME_FORMAT
- "), cannot handle. Clipping buffer.",
- GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
- GST_TIME_ARGS (enc->next_ts));
-
- diff_bytes = GST_CLOCK_TIME_TO_FRAMES (diff, enc->rate) * enc->channels * 2;
- if (diff_bytes >= GST_BUFFER_SIZE (buf)) {
- gst_buffer_unref (buf);
- return GST_FLOW_OK;
- }
- buf = gst_buffer_make_metadata_writable (buf);
- GST_BUFFER_DATA (buf) += diff_bytes;
- GST_BUFFER_SIZE (buf) -= diff_bytes;
-
- GST_BUFFER_TIMESTAMP (buf) += diff;
- if (GST_BUFFER_DURATION_IS_VALID (buf))
- GST_BUFFER_DURATION (buf) -= diff;
- }
-
- if (enc->next_ts != GST_CLOCK_TIME_NONE
- && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
- guint64 max_diff =
- gst_util_uint64_scale (enc->frame_size, GST_SECOND, enc->rate);
+ GST_DEBUG_OBJECT (enc, "received buffer %p of %u bytes", buf,
+ buf ? GST_BUFFER_SIZE (buf) : 0);
- if (GST_BUFFER_TIMESTAMP (buf) != enc->next_ts &&
- GST_BUFFER_TIMESTAMP (buf) - enc->next_ts > max_diff) {
- GST_WARNING_OBJECT (enc,
- "Discontinuity detected: %" G_GUINT64_FORMAT " > %" G_GUINT64_FORMAT,
- GST_BUFFER_TIMESTAMP (buf) - enc->next_ts, max_diff);
-
- gst_celt_enc_encode (enc, TRUE);
-
- enc->frameno_out = 0;
- enc->start_ts = GST_BUFFER_TIMESTAMP (buf);
- enc->granulepos_offset = gst_util_uint64_scale
- (GST_BUFFER_TIMESTAMP (buf), enc->rate, GST_SECOND);
- }
- }
-
- if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)
- && GST_BUFFER_DURATION_IS_VALID (buf))
- enc->next_ts = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
- else
- enc->next_ts = GST_CLOCK_TIME_NONE;
-
- /* push buffer to adapter */
- gst_adapter_push (enc->adapter, buf);
- buf = NULL;
-
- ret = gst_celt_enc_encode (enc, FALSE);
+ ret = gst_celt_enc_encode (enc, buf);
done:
-
- if (buf)
- gst_buffer_unref (buf);
-
return ret;
/* ERRORS */
-not_setup:
- {
- GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
- ("encoder not initialized (input is not audio?)"));
- ret = GST_FLOW_NOT_NEGOTIATED;
- goto done;
- }
-
no_header:
{
GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
@@ -1060,6 +656,48 @@ no_header:
}
}
+/* push out the buffer */
+static GstFlowReturn
+gst_celt_enc_push_buffer (GstCeltEnc * enc, GstBuffer * buffer)
+{
+ guint size;
+
+ size = GST_BUFFER_SIZE (buffer);
+ GST_DEBUG_OBJECT (enc, "pushing output buffer of size %u", size);
+
+ gst_buffer_set_caps (buffer, GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (enc)));
+ return gst_pad_push (GST_AUDIO_ENCODER_SRC_PAD (enc), buffer);
+}
+
+static GstFlowReturn
+gst_celt_enc_pre_push (GstAudioEncoder * benc, GstBuffer ** buffer)
+{
+ GstCeltEnc *enc;
+ GstFlowReturn ret = GST_FLOW_OK;
+
+ enc = GST_CELT_ENC (benc);
+
+ /* FIXME 0.11 ? get rid of this special ogg stuff and have it
+ * put and use 'codec data' in caps like anything else,
+ * with all the usual out-of-band advantage etc */
+ if (G_UNLIKELY (enc->headers)) {
+ GSList *header = enc->headers;
+
+ /* try to push all of these, if we lose one, might as well lose all */
+ while (header) {
+ if (ret == GST_FLOW_OK)
+ ret = gst_celt_enc_push_buffer (enc, header->data);
+ else
+ gst_celt_enc_push_buffer (enc, header->data);
+ header = g_slist_next (header);
+ }
+
+ g_slist_free (enc->headers);
+ enc->headers = NULL;
+ }
+
+ return ret;
+}
static void
gst_celt_enc_get_property (GObject * object, guint prop_id, GValue * value,
@@ -1133,55 +771,3 @@ gst_celt_enc_set_property (GObject * object, guint prop_id,
break;
}
}
-
-static GstStateChangeReturn
-gst_celt_enc_change_state (GstElement * element, GstStateChange transition)
-{
- GstCeltEnc *enc = GST_CELT_ENC (element);
- GstStateChangeReturn res;
-
- switch (transition) {
- case GST_STATE_CHANGE_NULL_TO_READY:
- break;
- case GST_STATE_CHANGE_READY_TO_PAUSED:
- enc->frameno = 0;
- enc->samples_in = 0;
- enc->frameno_out = 0;
- enc->start_ts = GST_CLOCK_TIME_NONE;
- enc->next_ts = GST_CLOCK_TIME_NONE;
- enc->granulepos_offset = 0;
- break;
- case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
- /* fall through */
- default:
- break;
- }
-
- res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
- if (res == GST_STATE_CHANGE_FAILURE)
- return res;
-
- switch (transition) {
- case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
- break;
- case GST_STATE_CHANGE_PAUSED_TO_READY:
- enc->setup = FALSE;
- enc->header_sent = FALSE;
- if (enc->state) {
- celt_encoder_destroy (enc->state);
- enc->state = NULL;
- }
- if (enc->mode) {
- celt_mode_destroy (enc->mode);
- enc->mode = NULL;
- }
- memset (&enc->header, 0, sizeof (enc->header));
- break;
- case GST_STATE_CHANGE_READY_TO_NULL:
- gst_tag_setter_reset_tags (GST_TAG_SETTER (enc));
- default:
- break;
- }
-
- return res;
-}
diff --git a/ext/celt/gstceltenc.h b/ext/celt/gstceltenc.h
index 86fbc36f3..d0174ffa7 100644
--- a/ext/celt/gstceltenc.h
+++ b/ext/celt/gstceltenc.h
@@ -24,7 +24,7 @@
#include <gst/gst.h>
-#include <gst/base/gstadapter.h>
+#include <gst/audio/gstaudioencoder.h>
#include <celt/celt.h>
#include <celt/celt_header.h>
@@ -49,16 +49,11 @@ typedef struct _GstCeltEnc GstCeltEnc;
typedef struct _GstCeltEncClass GstCeltEncClass;
struct _GstCeltEnc {
- GstElement element;
-
- /* pads */
- GstPad *sinkpad;
- GstPad *srcpad;
+ GstAudioEncoder element;
CELTHeader header;
CELTMode *mode;
CELTEncoder *state;
- GstAdapter *adapter;
gint bitrate;
gint frame_size;
@@ -72,26 +67,12 @@ struct _GstCeltEnc {
gint channels;
gint rate;
- gboolean setup;
gboolean header_sent;
- gboolean eos;
-
- guint64 samples_in;
- guint64 bytes_out;
-
- guint64 frameno;
- guint64 frameno_out;
-
- GstClockTime start_ts;
- GstClockTime next_ts;
- guint64 granulepos_offset;
+ GSList *headers;
};
struct _GstCeltEncClass {
- GstElementClass parent_class;
-
- /* signals */
- void (*frame_encoded) (GstElement *element);
+ GstAudioEncoderClass parent_class;
};
GType gst_celt_enc_get_type (void);