2 * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
3 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
4 * Contact: Stefan Kost <stefan.kost@nokia.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gstaudioencoder
24 * @short_description: Base class for audio encoders
25 * @see_also: #GstBaseTransform
28 * This base class is for audio encoders turning raw audio samples into
31 * GstAudioEncoder and subclass should cooperate as follows.
34 * <itemizedlist><title>Configuration</title>
36 * Initially, GstAudioEncoder calls @start when the encoder element
37 * is activated, which allows subclass to perform any global setup.
40 * GstAudioEncoder calls @set_format to inform subclass of the format
41 * of input audio data that it is about to receive. Subclass should
42 * setup for encoding and configure various base class parameters
43 * appropriately, notably those directing desired input data handling.
44 * While unlikely, it might be called more than once, if changing input
45 * parameters require reconfiguration.
48 * GstAudioEncoder calls @stop at end of all processing.
52 * As of configuration stage, and throughout processing, GstAudioEncoder
53 * maintains various parameters that provide required context,
54 * e.g. describing the format of input audio data.
55 * Conversely, subclass can and should configure these context parameters
56 * to inform base class of its expectation w.r.t. buffer handling.
59 * <title>Data processing</title>
61 * Base class gathers input sample data (as directed by the context's
62 * frame_samples and frame_max) and provides this to subclass' @handle_frame.
65 * If codec processing results in encoded data, subclass should call
66 * @gst_audio_encoder_finish_frame to have encoded data pushed
67 * downstream. Alternatively, it might also call to indicate dropped
68 * (non-encoded) samples.
71 * Just prior to actually pushing a buffer downstream,
72 * it is passed to @pre_push.
75 * During the parsing process GstAudioEncoderClass will handle both
76 * srcpad and sinkpad events. Sink events will be passed to subclass
77 * if @event callback has been provided.
82 * <itemizedlist><title>Shutdown phase</title>
84 * GstAudioEncoder class calls @stop to inform the subclass that data
85 * parsing will be stopped.
91 * Subclass is responsible for providing pad template caps for
92 * source and sink pads. The pads need to be named "sink" and "src". It also
93 * needs to set the fixed caps on srcpad, when the format is ensured. This
94 * is typically when base class calls subclass' @set_format function, though
95 * it might be delayed until calling @gst_audio_encoder_finish_frame.
97 * In summary, above process should have subclass concentrating on
98 * codec data processing while leaving other matters to base class,
99 * such as most notably timestamp handling. While it may exert more control
100 * in this area (see e.g. @pre_push), it is very much not recommended.
102 * In particular, base class will either favor tracking upstream timestamps
103 * (at the possible expense of jitter) or aim to arrange for a perfect stream of
104 * output timestamps, depending on #GstAudioEncoder:perfect-timestamp.
105 * However, in the latter case, the input may not be so perfect or ideal, which
106 * is handled as follows. An input timestamp is compared with the expected
107 * timestamp as dictated by input sample stream and if the deviation is less
108 * than #GstAudioEncoder:tolerance, the deviation is discarded.
109 * Otherwise, it is considered a discontuinity and subsequent output timestamp
110 * is resynced to the new position after performing configured discontinuity
111 * processing. In the non-perfect-timestamp case, an upstream variation
112 * exceeding tolerance only leads to marking DISCONT on subsequent outgoing
113 * (while timestamps are adjusted to upstream regardless of variation).
114 * While DISCONT is also marked in the perfect-timestamp case, this one
115 * optionally (see #GstAudioEncoder:hard-resync)
116 * performs some additional steps, such as clipping of (early) input samples
117 * or draining all currently remaining input data, depending on the direction
118 * of the discontuinity.
120 * If perfect timestamps are arranged, it is also possible to request baseclass
121 * (usually set by subclass) to provide additional buffer metadata (in OFFSET
122 * and OFFSET_END) fields according to granule defined semantics currently
123 * needed by oggmux. Specifically, OFFSET is set to granulepos (= sample count
124 * including buffer) and OFFSET_END to corresponding timestamp (as determined
125 * by same sample count and sample rate).
127 * Things that subclass need to take care of:
129 * <listitem><para>Provide pad templates</para></listitem>
131 * Set source pad caps when appropriate
134 * Inform base class of buffer processing needs using context's
135 * frame_samples and frame_bytes.
138 * Set user-configurable properties to sane defaults for format and
139 * implementing codec at hand, e.g. those controlling timestamp behaviour
140 * and discontinuity processing.
143 * Accept data in @handle_frame and provide encoded results to
144 * @gst_audio_encoder_finish_frame.
154 #include "gstaudioencoder.h"
155 #include <gst/base/gstadapter.h>
156 #include <gst/audio/audio.h>
157 #include <gst/pbutils/descriptions.h>
163 GST_DEBUG_CATEGORY_STATIC (gst_audio_encoder_debug);
164 #define GST_CAT_DEFAULT gst_audio_encoder_debug
166 #define GST_AUDIO_ENCODER_GET_PRIVATE(obj) \
167 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_AUDIO_ENCODER, \
168 GstAudioEncoderPrivate))
179 #define DEFAULT_PERFECT_TS FALSE
180 #define DEFAULT_GRANULE FALSE
181 #define DEFAULT_HARD_RESYNC FALSE
182 #define DEFAULT_TOLERANCE 40000000
184 typedef struct _GstAudioEncoderContext
190 gint frame_samples_min, frame_samples_max;
193 /* MT-protected (with LOCK) */
194 GstClockTime min_latency;
195 GstClockTime max_latency;
196 } GstAudioEncoderContext;
198 struct _GstAudioEncoderPrivate
200 /* activation status */
203 /* input base/first ts as basis for output ts;
204 * kept nearly constant for perfect_ts,
205 * otherwise resyncs to upstream ts */
206 GstClockTime base_ts;
207 /* corresponding base granulepos */
209 /* input samples processed and sent downstream so far (w.r.t. base_ts) */
212 /* currently collected sample data */
214 /* offset in adapter up to which already supplied to encoder */
216 /* mark outgoing discont */
218 /* to guess duration of drained data */
219 GstClockTime last_duration;
221 /* subclass provided data in processing round */
223 /* subclass gave all it could already */
225 /* subclass currently being forcibly drained */
228 /* output bps estimatation */
229 /* global in samples seen */
231 /* global bytes sent out */
234 /* context storage */
235 GstAudioEncoderContext ctx;
240 gboolean hard_resync;
245 /* pending serialized sink events, will be sent from finish_frame() */
246 GList *pending_events;
250 static GstElementClass *parent_class = NULL;
252 static void gst_audio_encoder_class_init (GstAudioEncoderClass * klass);
253 static void gst_audio_encoder_init (GstAudioEncoder * parse,
254 GstAudioEncoderClass * klass);
257 gst_audio_encoder_get_type (void)
259 static GType audio_encoder_type = 0;
261 if (!audio_encoder_type) {
262 static const GTypeInfo audio_encoder_info = {
263 sizeof (GstAudioEncoderClass),
264 (GBaseInitFunc) NULL,
265 (GBaseFinalizeFunc) NULL,
266 (GClassInitFunc) gst_audio_encoder_class_init,
269 sizeof (GstAudioEncoder),
271 (GInstanceInitFunc) gst_audio_encoder_init,
273 const GInterfaceInfo preset_interface_info = {
274 NULL, /* interface_init */
275 NULL, /* interface_finalize */
276 NULL /* interface_data */
279 audio_encoder_type = g_type_register_static (GST_TYPE_ELEMENT,
280 "GstAudioEncoder", &audio_encoder_info, G_TYPE_FLAG_ABSTRACT);
282 g_type_add_interface_static (audio_encoder_type, GST_TYPE_PRESET,
283 &preset_interface_info);
285 return audio_encoder_type;
288 static void gst_audio_encoder_finalize (GObject * object);
289 static void gst_audio_encoder_reset (GstAudioEncoder * enc, gboolean full);
291 static void gst_audio_encoder_set_property (GObject * object,
292 guint prop_id, const GValue * value, GParamSpec * pspec);
293 static void gst_audio_encoder_get_property (GObject * object,
294 guint prop_id, GValue * value, GParamSpec * pspec);
296 static gboolean gst_audio_encoder_sink_activate_mode (GstPad * pad,
297 GstObject * parent, GstPadMode mode, gboolean active);
299 static GstCaps *gst_audio_encoder_getcaps_default (GstAudioEncoder * enc,
302 static gboolean gst_audio_encoder_sink_event (GstPad * pad, GstObject * parent,
304 static gboolean gst_audio_encoder_sink_setcaps (GstAudioEncoder * enc,
306 static GstFlowReturn gst_audio_encoder_chain (GstPad * pad, GstObject * parent,
308 static gboolean gst_audio_encoder_src_query (GstPad * pad, GstObject * parent,
310 static gboolean gst_audio_encoder_sink_query (GstPad * pad, GstObject * parent,
314 gst_audio_encoder_class_init (GstAudioEncoderClass * klass)
316 GObjectClass *gobject_class;
318 gobject_class = G_OBJECT_CLASS (klass);
319 parent_class = g_type_class_peek_parent (klass);
321 GST_DEBUG_CATEGORY_INIT (gst_audio_encoder_debug, "audioencoder", 0,
322 "audio encoder base class");
324 g_type_class_add_private (klass, sizeof (GstAudioEncoderPrivate));
326 gobject_class->set_property = gst_audio_encoder_set_property;
327 gobject_class->get_property = gst_audio_encoder_get_property;
329 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_audio_encoder_finalize);
332 g_object_class_install_property (gobject_class, PROP_PERFECT_TS,
333 g_param_spec_boolean ("perfect-timestamp", "Perfect Timestamps",
334 "Favour perfect timestamps over tracking upstream timestamps",
335 DEFAULT_PERFECT_TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
336 g_object_class_install_property (gobject_class, PROP_GRANULE,
337 g_param_spec_boolean ("mark-granule", "Granule Marking",
338 "Apply granule semantics to buffer metadata (implies perfect-timestamp)",
339 DEFAULT_GRANULE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_HARD_RESYNC,
341 g_param_spec_boolean ("hard-resync", "Hard Resync",
342 "Perform clipping and sample flushing upon discontinuity",
343 DEFAULT_HARD_RESYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_TOLERANCE,
345 g_param_spec_int64 ("tolerance", "Tolerance",
346 "Consider discontinuity if timestamp jitter/imperfection exceeds tolerance (ns)",
347 0, G_MAXINT64, DEFAULT_TOLERANCE,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 klass->getcaps = gst_audio_encoder_getcaps_default;
354 gst_audio_encoder_init (GstAudioEncoder * enc, GstAudioEncoderClass * bclass)
356 GstPadTemplate *pad_template;
358 GST_DEBUG_OBJECT (enc, "gst_audio_encoder_init");
360 enc->priv = GST_AUDIO_ENCODER_GET_PRIVATE (enc);
362 /* only push mode supported */
364 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
365 g_return_if_fail (pad_template != NULL);
366 enc->sinkpad = gst_pad_new_from_template (pad_template, "sink");
367 gst_pad_set_event_function (enc->sinkpad,
368 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_event));
369 gst_pad_set_query_function (enc->sinkpad,
370 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_query));
371 gst_pad_set_chain_function (enc->sinkpad,
372 GST_DEBUG_FUNCPTR (gst_audio_encoder_chain));
373 gst_pad_set_activatemode_function (enc->sinkpad,
374 GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_activate_mode));
375 gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
377 GST_DEBUG_OBJECT (enc, "sinkpad created");
379 /* and we don't mind upstream traveling stuff that much ... */
381 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
382 g_return_if_fail (pad_template != NULL);
383 enc->srcpad = gst_pad_new_from_template (pad_template, "src");
384 gst_pad_set_query_function (enc->srcpad,
385 GST_DEBUG_FUNCPTR (gst_audio_encoder_src_query));
386 gst_pad_use_fixed_caps (enc->srcpad);
387 gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
388 GST_DEBUG_OBJECT (enc, "src created");
390 enc->priv->adapter = gst_adapter_new ();
392 g_static_rec_mutex_init (&enc->stream_lock);
394 /* property default */
395 enc->priv->granule = DEFAULT_GRANULE;
396 enc->priv->perfect_ts = DEFAULT_PERFECT_TS;
397 enc->priv->hard_resync = DEFAULT_HARD_RESYNC;
398 enc->priv->tolerance = DEFAULT_TOLERANCE;
401 gst_audio_encoder_reset (enc, TRUE);
402 GST_DEBUG_OBJECT (enc, "init ok");
406 gst_audio_encoder_reset (GstAudioEncoder * enc, gboolean full)
408 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
410 GST_LOG_OBJECT (enc, "reset full %d", full);
413 enc->priv->active = FALSE;
414 enc->priv->samples_in = 0;
415 enc->priv->bytes_out = 0;
416 gst_audio_info_init (&enc->priv->ctx.info);
417 memset (&enc->priv->ctx, 0, sizeof (enc->priv->ctx));
420 gst_tag_list_free (enc->priv->tags);
421 enc->priv->tags = NULL;
423 g_list_foreach (enc->priv->pending_events, (GFunc) gst_event_unref, NULL);
424 g_list_free (enc->priv->pending_events);
425 enc->priv->pending_events = NULL;
428 gst_segment_init (&enc->segment, GST_FORMAT_TIME);
430 gst_adapter_clear (enc->priv->adapter);
431 enc->priv->got_data = FALSE;
432 enc->priv->drained = TRUE;
433 enc->priv->offset = 0;
434 enc->priv->base_ts = GST_CLOCK_TIME_NONE;
435 enc->priv->base_gp = -1;
436 enc->priv->samples = 0;
437 enc->priv->discont = FALSE;
439 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
443 gst_audio_encoder_finalize (GObject * object)
445 GstAudioEncoder *enc = GST_AUDIO_ENCODER (object);
447 g_object_unref (enc->priv->adapter);
449 g_static_rec_mutex_free (&enc->stream_lock);
451 G_OBJECT_CLASS (parent_class)->finalize (object);
455 * gst_audio_encoder_finish_frame:
456 * @enc: a #GstAudioEncoder
457 * @buffer: encoded data
458 * @samples: number of samples (per channel) represented by encoded data
460 * Collects encoded data and pushes encoded data downstream.
461 * Source pad caps must be set when this is called.
463 * If @samples < 0, then best estimate is all samples provided to encoder
464 * (subclass) so far. @buf may be NULL, in which case next number of @samples
465 * are considered discarded, e.g. as a result of discontinuous transmission,
466 * and a discontinuity is marked.
468 * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
473 gst_audio_encoder_finish_frame (GstAudioEncoder * enc, GstBuffer * buf,
476 GstAudioEncoderClass *klass;
477 GstAudioEncoderPrivate *priv;
478 GstAudioEncoderContext *ctx;
479 GstFlowReturn ret = GST_FLOW_OK;
481 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
483 ctx = &enc->priv->ctx;
485 /* subclass should know what it is producing by now */
486 g_return_val_if_fail (gst_pad_has_current_caps (enc->srcpad), GST_FLOW_ERROR);
487 /* subclass should not hand us no data */
488 g_return_val_if_fail (buf == NULL || gst_buffer_get_size (buf) > 0,
491 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
493 GST_LOG_OBJECT (enc, "accepting %d bytes encoded data as %d samples",
494 buf ? gst_buffer_get_size (buf) : -1, samples);
496 /* mark subclass still alive and providing */
498 priv->got_data = TRUE;
500 if (priv->pending_events) {
501 GList *pending_events, *l;
503 pending_events = priv->pending_events;
504 priv->pending_events = NULL;
506 GST_DEBUG_OBJECT (enc, "Pushing pending events");
507 for (l = pending_events; l; l = l->next)
508 gst_pad_push_event (enc->srcpad, l->data);
509 g_list_free (pending_events);
512 /* send after pending events, which likely includes newsegment event */
513 if (G_UNLIKELY (enc->priv->tags)) {
519 /* add codec info to pending tags */
520 tags = enc->priv->tags;
521 /* no more pending */
522 enc->priv->tags = NULL;
524 caps = gst_pad_get_current_caps (enc->srcpad);
525 gst_pb_utils_add_codec_description_to_tag_list (tags, GST_TAG_CODEC, caps);
526 gst_pb_utils_add_codec_description_to_tag_list (tags, GST_TAG_AUDIO_CODEC,
529 GST_DEBUG_OBJECT (enc, "sending tags %" GST_PTR_FORMAT, tags);
530 gst_pad_push_event (enc->srcpad, gst_event_new_tag (tags));
533 /* remove corresponding samples from input */
535 samples = (enc->priv->offset / ctx->info.bpf);
537 if (G_LIKELY (samples)) {
538 /* track upstream ts if so configured */
539 if (!enc->priv->perfect_ts) {
540 guint64 ts, distance;
542 ts = gst_adapter_prev_timestamp (priv->adapter, &distance);
543 g_assert (distance % ctx->info.bpf == 0);
544 distance /= ctx->info.bpf;
545 GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past prev_ts %"
546 GST_TIME_FORMAT, distance, GST_TIME_ARGS (ts));
547 GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past base_ts %"
548 GST_TIME_FORMAT, priv->samples, GST_TIME_ARGS (priv->base_ts));
549 /* when draining adapter might be empty and no ts to offer */
550 if (GST_CLOCK_TIME_IS_VALID (ts) && ts != priv->base_ts) {
551 GstClockTimeDiff diff;
552 GstClockTime old_ts, next_ts;
554 /* passed into another buffer;
555 * mild check for discontinuity and only mark if so */
557 gst_util_uint64_scale (distance, GST_SECOND, ctx->info.rate);
558 old_ts = priv->base_ts +
559 gst_util_uint64_scale (priv->samples, GST_SECOND, ctx->info.rate);
560 diff = GST_CLOCK_DIFF (next_ts, old_ts);
561 GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
562 /* only mark discontinuity if beyond tolerance */
563 if (G_UNLIKELY (diff < -enc->priv->tolerance ||
564 diff > enc->priv->tolerance)) {
565 GST_DEBUG_OBJECT (enc, "marked discont");
566 priv->discont = TRUE;
568 if (diff > GST_SECOND / ctx->info.rate / 2 ||
569 diff < -GST_SECOND / ctx->info.rate / 2) {
570 GST_LOG_OBJECT (enc, "new upstream ts %" GST_TIME_FORMAT
571 " at distance %" G_GUINT64_FORMAT, GST_TIME_ARGS (ts), distance);
572 /* re-sync to upstream ts */
574 priv->samples = distance;
576 GST_LOG_OBJECT (enc, "new upstream ts only introduces jitter");
580 /* advance sample view */
581 if (G_UNLIKELY (samples * ctx->info.bpf > priv->offset)) {
582 if (G_LIKELY (!priv->force)) {
583 /* no way we can let this pass */
584 g_assert_not_reached ();
589 if (samples * ctx->info.bpf >= gst_adapter_available (priv->adapter))
590 gst_adapter_clear (priv->adapter);
592 gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
595 gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
596 priv->offset -= samples * ctx->info.bpf;
597 /* avoid subsequent stray prev_ts */
598 if (G_UNLIKELY (gst_adapter_available (priv->adapter) == 0))
599 gst_adapter_clear (priv->adapter);
601 /* sample count advanced below after buffer handling */
605 if (G_LIKELY (buf)) {
608 size = gst_buffer_get_size (buf);
610 GST_LOG_OBJECT (enc, "taking %d bytes for output", size);
611 buf = gst_buffer_make_writable (buf);
614 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (priv->base_ts))) {
615 /* FIXME ? lookahead could lead to weird ts and duration ?
616 * (particularly if not in perfect mode) */
617 /* mind sample rounding and produce perfect output */
618 GST_BUFFER_TIMESTAMP (buf) = priv->base_ts +
619 gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
621 GST_DEBUG_OBJECT (enc, "out samples %d", samples);
622 if (G_LIKELY (samples > 0)) {
623 priv->samples += samples;
624 GST_BUFFER_DURATION (buf) = priv->base_ts +
625 gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
626 ctx->info.rate) - GST_BUFFER_TIMESTAMP (buf);
627 priv->last_duration = GST_BUFFER_DURATION (buf);
629 /* duration forecast in case of handling remainder;
630 * the last one is probably like the previous one ... */
631 GST_BUFFER_DURATION (buf) = priv->last_duration;
633 if (priv->base_gp >= 0) {
635 /* FIXME: in longer run, muxer should take care of this ... */
636 /* offset_end = granulepos for ogg muxer */
637 GST_BUFFER_OFFSET_END (buf) = priv->base_gp + priv->samples -
638 enc->priv->ctx.lookahead;
639 /* offset = timestamp corresponding to granulepos for ogg muxer */
640 GST_BUFFER_OFFSET (buf) =
641 GST_FRAMES_TO_CLOCK_TIME (GST_BUFFER_OFFSET_END (buf),
644 GST_BUFFER_OFFSET (buf) = priv->bytes_out;
645 GST_BUFFER_OFFSET_END (buf) = priv->bytes_out + size;
649 priv->bytes_out += size;
651 if (G_UNLIKELY (priv->discont)) {
652 GST_LOG_OBJECT (enc, "marking discont");
653 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
654 priv->discont = FALSE;
657 if (klass->pre_push) {
658 /* last chance for subclass to do some dirty stuff */
659 ret = klass->pre_push (enc, &buf);
660 if (ret != GST_FLOW_OK || !buf) {
661 GST_DEBUG_OBJECT (enc, "subclass returned %s, buf %p",
662 gst_flow_get_name (ret), buf);
664 gst_buffer_unref (buf);
669 GST_LOG_OBJECT (enc, "pushing buffer of size %d with ts %" GST_TIME_FORMAT
670 ", duration %" GST_TIME_FORMAT, size,
671 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
672 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
674 ret = gst_pad_push (enc->srcpad, buf);
675 GST_LOG_OBJECT (enc, "buffer pushed: %s", gst_flow_get_name (ret));
677 /* merely advance samples, most work for that already done above */
678 priv->samples += samples;
682 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
689 GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
690 ("received more encoded samples %d than provided %d",
691 samples, priv->offset / ctx->info.bpf), (NULL));
693 gst_buffer_unref (buf);
694 ret = GST_FLOW_ERROR;
699 /* adapter tracking idea:
700 * - start of adapter corresponds with what has already been encoded
701 * (i.e. really returned by encoder subclass)
702 * - start + offset is what needs to be fed to subclass next */
704 gst_audio_encoder_push_buffers (GstAudioEncoder * enc, gboolean force)
706 GstAudioEncoderClass *klass;
707 GstAudioEncoderPrivate *priv;
708 GstAudioEncoderContext *ctx;
711 GstFlowReturn ret = GST_FLOW_OK;
713 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
715 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
718 ctx = &enc->priv->ctx;
720 while (ret == GST_FLOW_OK) {
723 av = gst_adapter_available (priv->adapter);
725 g_assert (priv->offset <= av);
729 ctx->frame_samples_min >
730 0 ? ctx->frame_samples_min * ctx->info.bpf : av;
731 GST_LOG_OBJECT (enc, "available: %d, needed: %d, force: %d", av, need,
734 if ((need > av) || !av) {
735 if (G_UNLIKELY (force)) {
745 if (ctx->frame_samples_max > 0)
746 need = MIN (av, ctx->frame_samples_max * ctx->info.bpf);
748 if (ctx->frame_samples_min == ctx->frame_samples_max) {
749 /* if we have some extra metadata,
750 * provide for integer multiple of frames to allow for better granularity
752 if (ctx->frame_samples_min > 0 && need) {
753 if (ctx->frame_max > 1)
754 need = need * MIN ((av / need), ctx->frame_max);
755 else if (ctx->frame_max == 0)
756 need = need * (av / need);
763 data = gst_adapter_map (priv->adapter, priv->offset + need);
765 gst_buffer_new_wrapped_full ((gpointer) data, NULL, priv->offset,
769 GST_LOG_OBJECT (enc, "providing subclass with %d bytes at offset %d",
772 /* mark this already as consumed,
773 * which it should be when subclass gives us data in exchange for samples */
774 priv->offset += need;
775 priv->samples_in += need / ctx->info.bpf;
777 priv->got_data = FALSE;
778 ret = klass->handle_frame (enc, buf);
780 if (G_LIKELY (buf)) {
781 gst_buffer_unref (buf);
782 gst_adapter_unmap (priv->adapter);
785 /* no data to feed, no leftover provided, then bail out */
786 if (G_UNLIKELY (!buf && !priv->got_data)) {
787 priv->drained = TRUE;
788 GST_LOG_OBJECT (enc, "no more data drained from subclass");
797 gst_audio_encoder_drain (GstAudioEncoder * enc)
799 if (enc->priv->drained)
802 return gst_audio_encoder_push_buffers (enc, TRUE);
806 gst_audio_encoder_set_base_gp (GstAudioEncoder * enc)
810 if (!enc->priv->granule)
813 /* use running time for granule */
814 /* incoming data is clipped, so a valid input should yield a valid output */
815 ts = gst_segment_to_running_time (&enc->segment, GST_FORMAT_TIME,
817 if (GST_CLOCK_TIME_IS_VALID (ts)) {
819 GST_CLOCK_TIME_TO_FRAMES (enc->priv->base_ts, enc->priv->ctx.info.rate);
820 GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT, enc->priv->base_gp);
822 /* should reasonably have a valid base,
823 * otherwise start at 0 if we did not already start there earlier */
824 if (enc->priv->base_gp < 0) {
825 enc->priv->base_gp = 0;
826 GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT,
833 gst_audio_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
835 GstAudioEncoder *enc;
836 GstAudioEncoderPrivate *priv;
837 GstAudioEncoderContext *ctx;
838 GstFlowReturn ret = GST_FLOW_OK;
842 enc = GST_AUDIO_ENCODER (parent);
845 ctx = &enc->priv->ctx;
847 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
849 /* should know what is coming by now */
853 size = gst_buffer_get_size (buffer);
856 "received buffer of size %d with ts %" GST_TIME_FORMAT
857 ", duration %" GST_TIME_FORMAT, size,
858 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
859 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
861 /* input shoud be whole number of sample frames */
862 if (size % ctx->info.bpf)
865 #ifndef GST_DISABLE_GST_DEBUG
867 GstClockTime duration;
868 GstClockTimeDiff diff;
870 /* verify buffer duration */
871 duration = gst_util_uint64_scale (size, GST_SECOND,
872 ctx->info.rate * ctx->info.bpf);
873 diff = GST_CLOCK_DIFF (duration, GST_BUFFER_DURATION (buffer));
874 if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE &&
875 (diff > GST_SECOND / ctx->info.rate / 2 ||
876 diff < -GST_SECOND / ctx->info.rate / 2)) {
877 GST_DEBUG_OBJECT (enc, "incoming buffer had incorrect duration %"
878 GST_TIME_FORMAT ", expected duration %" GST_TIME_FORMAT,
879 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
880 GST_TIME_ARGS (duration));
885 discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
886 if (G_UNLIKELY (discont)) {
887 GST_LOG_OBJECT (buffer, "marked discont");
888 enc->priv->discont = discont;
891 /* clip to segment */
892 /* NOTE: slightly painful linking -laudio only for this one ... */
893 buffer = gst_audio_buffer_clip (buffer, &enc->segment, ctx->info.rate,
895 if (G_UNLIKELY (!buffer)) {
896 GST_DEBUG_OBJECT (buffer, "no data after clipping to segment");
900 size = gst_buffer_get_size (buffer);
903 "buffer after segment clipping has size %d with ts %" GST_TIME_FORMAT
904 ", duration %" GST_TIME_FORMAT, size,
905 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
906 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
908 if (!GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
909 priv->base_ts = GST_BUFFER_TIMESTAMP (buffer);
910 GST_DEBUG_OBJECT (enc, "new base ts %" GST_TIME_FORMAT,
911 GST_TIME_ARGS (priv->base_ts));
912 gst_audio_encoder_set_base_gp (enc);
915 /* check for continuity;
916 * checked elsewhere in non-perfect case */
917 if (enc->priv->perfect_ts) {
918 GstClockTimeDiff diff = 0;
919 GstClockTime next_ts = 0;
921 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
922 GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
925 samples = priv->samples +
926 gst_adapter_available (priv->adapter) / ctx->info.bpf;
927 next_ts = priv->base_ts +
928 gst_util_uint64_scale (samples, GST_SECOND, ctx->info.rate);
929 GST_LOG_OBJECT (enc, "buffer is %" G_GUINT64_FORMAT
930 " samples past base_ts %" GST_TIME_FORMAT
931 ", expected ts %" GST_TIME_FORMAT, samples,
932 GST_TIME_ARGS (priv->base_ts), GST_TIME_ARGS (next_ts));
933 diff = GST_CLOCK_DIFF (next_ts, GST_BUFFER_TIMESTAMP (buffer));
934 GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
935 /* if within tolerance,
936 * discard buffer ts and carry on producing perfect stream,
937 * otherwise clip or resync to ts */
938 if (G_UNLIKELY (diff < -enc->priv->tolerance ||
939 diff > enc->priv->tolerance)) {
940 GST_DEBUG_OBJECT (enc, "marked discont");
945 /* do some fancy tweaking in hard resync case */
946 if (discont && enc->priv->hard_resync) {
950 GST_WARNING_OBJECT (enc, "Buffer is older than expected ts %"
951 GST_TIME_FORMAT ". Clipping buffer", GST_TIME_ARGS (next_ts));
954 GST_CLOCK_TIME_TO_FRAMES (-diff, ctx->info.rate) * ctx->info.bpf;
955 if (diff_bytes >= size) {
956 gst_buffer_unref (buffer);
959 buffer = gst_buffer_make_writable (buffer);
960 gst_buffer_resize (buffer, diff_bytes, size - diff_bytes);
962 GST_BUFFER_TIMESTAMP (buffer) += diff;
963 /* care even less about duration after this */
965 /* drain stuff prior to resync */
966 gst_audio_encoder_drain (enc);
971 priv->base_ts += diff;
972 gst_audio_encoder_set_base_gp (enc);
973 priv->discont |= discont;
977 gst_adapter_push (enc->priv->adapter, buffer);
978 /* new stuff, so we can push subclass again */
979 enc->priv->drained = FALSE;
981 ret = gst_audio_encoder_push_buffers (enc, FALSE);
984 GST_LOG_OBJECT (enc, "chain leaving");
986 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
993 GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
994 ("encoder not initialized"));
995 gst_buffer_unref (buffer);
996 ret = GST_FLOW_NOT_NEGOTIATED;
1001 GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
1002 ("buffer size %d not a multiple of %d", gst_buffer_get_size (buffer),
1004 gst_buffer_unref (buffer);
1005 ret = GST_FLOW_ERROR;
1011 audio_info_is_equal (GstAudioInfo * from, GstAudioInfo * to)
1015 if (from->finfo == NULL || to->finfo == NULL)
1017 if (GST_AUDIO_INFO_FORMAT (from) != GST_AUDIO_INFO_FORMAT (to))
1019 if (GST_AUDIO_INFO_RATE (from) != GST_AUDIO_INFO_RATE (to))
1021 if (GST_AUDIO_INFO_CHANNELS (from) != GST_AUDIO_INFO_CHANNELS (to))
1023 if (GST_AUDIO_INFO_CHANNELS (from) > 64)
1025 return memcmp (from->position, to->position,
1026 GST_AUDIO_INFO_CHANNELS (from) * sizeof (to->position[0]));
1030 gst_audio_encoder_sink_setcaps (GstAudioEncoder * enc, GstCaps * caps)
1032 GstAudioEncoderClass *klass;
1033 GstAudioEncoderContext *ctx;
1035 gboolean res = TRUE, changed = FALSE;
1038 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1040 /* subclass must do something here ... */
1041 g_return_val_if_fail (klass->set_format != NULL, FALSE);
1043 ctx = &enc->priv->ctx;
1045 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1047 GST_DEBUG_OBJECT (enc, "caps: %" GST_PTR_FORMAT, caps);
1049 if (!gst_caps_is_fixed (caps))
1052 /* adjust ts tracking to new sample rate */
1053 old_rate = GST_AUDIO_INFO_RATE (&ctx->info);
1054 if (GST_CLOCK_TIME_IS_VALID (enc->priv->base_ts) && old_rate) {
1055 enc->priv->base_ts +=
1056 GST_FRAMES_TO_CLOCK_TIME (enc->priv->samples, old_rate);
1057 enc->priv->samples = 0;
1060 if (!gst_audio_info_from_caps (&state, caps))
1063 changed = !audio_info_is_equal (&state, &ctx->info);
1066 GstClockTime old_min_latency;
1067 GstClockTime old_max_latency;
1069 /* drain any pending old data stuff */
1070 gst_audio_encoder_drain (enc);
1072 /* context defaults */
1073 enc->priv->ctx.frame_samples_min = 0;
1074 enc->priv->ctx.frame_samples_max = 0;
1075 enc->priv->ctx.frame_max = 0;
1076 enc->priv->ctx.lookahead = 0;
1078 /* element might report latency */
1079 GST_OBJECT_LOCK (enc);
1080 old_min_latency = ctx->min_latency;
1081 old_max_latency = ctx->max_latency;
1082 GST_OBJECT_UNLOCK (enc);
1084 if (klass->set_format)
1085 res = klass->set_format (enc, &state);
1090 /* invalidate state to ensure no casual carrying on */
1092 GST_DEBUG_OBJECT (enc, "subclass did not accept format");
1093 gst_audio_info_init (&state);
1097 /* notify if new latency */
1098 GST_OBJECT_LOCK (enc);
1099 if ((ctx->min_latency > 0 && ctx->min_latency != old_min_latency) ||
1100 (ctx->max_latency > 0 && ctx->max_latency != old_max_latency)) {
1101 GST_OBJECT_UNLOCK (enc);
1102 /* post latency message on the bus */
1103 gst_element_post_message (GST_ELEMENT (enc),
1104 gst_message_new_latency (GST_OBJECT (enc)));
1105 GST_OBJECT_LOCK (enc);
1107 GST_OBJECT_UNLOCK (enc);
1109 GST_DEBUG_OBJECT (enc, "new audio format identical to configured format");
1114 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1121 GST_WARNING_OBJECT (enc, "rejected caps %" GST_PTR_FORMAT, caps);
1128 * gst_audio_encoder_proxy_getcaps:
1129 * @enc: a #GstAudioEncoder
1130 * @caps: initial caps
1132 * Returns caps that express @caps (or sink template caps if @caps == NULL)
1133 * restricted to channel/rate combinations supported by downstream elements
1136 * Returns: a #GstCaps owned by caller
1141 gst_audio_encoder_proxy_getcaps (GstAudioEncoder * enc, GstCaps * caps)
1143 const GstCaps *templ_caps;
1144 GstCaps *allowed = NULL;
1145 GstCaps *fcaps, *filter_caps;
1148 /* we want to be able to communicate to upstream elements like audioconvert
1149 * and audioresample any rate/channel restrictions downstream (e.g. muxer
1150 * only accepting certain sample rates) */
1151 templ_caps = caps ? caps : gst_pad_get_pad_template_caps (enc->sinkpad);
1152 allowed = gst_pad_get_allowed_caps (enc->srcpad);
1153 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
1154 fcaps = gst_caps_copy (templ_caps);
1158 GST_LOG_OBJECT (enc, "template caps %" GST_PTR_FORMAT, templ_caps);
1159 GST_LOG_OBJECT (enc, "allowed caps %" GST_PTR_FORMAT, allowed);
1161 filter_caps = gst_caps_new_empty ();
1163 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
1166 q_name = gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
1168 /* pick rate + channel fields from allowed caps */
1169 for (j = 0; j < gst_caps_get_size (allowed); j++) {
1170 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
1174 s = gst_structure_new_id_empty (q_name);
1175 if ((val = gst_structure_get_value (allowed_s, "rate")))
1176 gst_structure_set_value (s, "rate", val);
1177 if ((val = gst_structure_get_value (allowed_s, "channels")))
1178 gst_structure_set_value (s, "channels", val);
1179 /* following might also make sense for some encoded formats,
1181 if ((val = gst_structure_get_value (allowed_s, "width")))
1182 gst_structure_set_value (s, "width", val);
1183 if ((val = gst_structure_get_value (allowed_s, "depth")))
1184 gst_structure_set_value (s, "depth", val);
1185 if ((val = gst_structure_get_value (allowed_s, "endianness")))
1186 gst_structure_set_value (s, "endianness", val);
1187 if ((val = gst_structure_get_value (allowed_s, "signed")))
1188 gst_structure_set_value (s, "signed", val);
1189 if ((val = gst_structure_get_value (allowed_s, "channel-positions")))
1190 gst_structure_set_value (s, "channel-positions", val);
1192 gst_caps_merge_structure (filter_caps, s);
1196 fcaps = gst_caps_intersect (filter_caps, templ_caps);
1197 gst_caps_unref (filter_caps);
1200 gst_caps_replace (&allowed, NULL);
1202 GST_LOG_OBJECT (enc, "proxy caps %" GST_PTR_FORMAT, fcaps);
1208 gst_audio_encoder_getcaps_default (GstAudioEncoder * enc, GstCaps * filter)
1212 caps = gst_audio_encoder_proxy_getcaps (enc, NULL);
1213 GST_LOG_OBJECT (enc, "returning caps %" GST_PTR_FORMAT, caps);
1219 gst_audio_encoder_sink_eventfunc (GstAudioEncoder * enc, GstEvent * event)
1221 GstAudioEncoderClass *klass;
1222 gboolean handled = FALSE;
1224 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1226 switch (GST_EVENT_TYPE (event)) {
1227 case GST_EVENT_SEGMENT:
1231 gst_event_copy_segment (event, &seg);
1233 if (seg.format == GST_FORMAT_TIME) {
1234 GST_DEBUG_OBJECT (enc, "received TIME SEGMENT %" GST_SEGMENT_FORMAT,
1237 GST_DEBUG_OBJECT (enc, "received SEGMENT %" GST_SEGMENT_FORMAT, &seg);
1238 GST_DEBUG_OBJECT (enc, "unsupported format; ignoring");
1242 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1243 /* finish current segment */
1244 gst_audio_encoder_drain (enc);
1245 /* reset partially for new segment */
1246 gst_audio_encoder_reset (enc, FALSE);
1247 /* and follow along with segment */
1249 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1253 case GST_EVENT_FLUSH_START:
1256 case GST_EVENT_FLUSH_STOP:
1257 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1258 /* discard any pending stuff */
1259 /* TODO route through drain ?? */
1260 if (!enc->priv->drained && klass->flush)
1262 /* and get (re)set for the sequel */
1263 gst_audio_encoder_reset (enc, FALSE);
1265 g_list_foreach (enc->priv->pending_events, (GFunc) gst_event_unref, NULL);
1266 g_list_free (enc->priv->pending_events);
1267 enc->priv->pending_events = NULL;
1268 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1273 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1274 gst_audio_encoder_drain (enc);
1275 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1282 gst_event_parse_tag (event, &tags);
1283 tags = gst_tag_list_copy (tags);
1284 gst_event_unref (event);
1286 /* FIXME: make generic based on GST_TAG_FLAG_ENCODED */
1287 gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
1288 gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
1289 gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
1290 gst_tag_list_remove_tag (tags, GST_TAG_SUBTITLE_CODEC);
1291 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1292 gst_tag_list_remove_tag (tags, GST_TAG_BITRATE);
1293 gst_tag_list_remove_tag (tags, GST_TAG_NOMINAL_BITRATE);
1294 gst_tag_list_remove_tag (tags, GST_TAG_MAXIMUM_BITRATE);
1295 gst_tag_list_remove_tag (tags, GST_TAG_MINIMUM_BITRATE);
1296 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER);
1297 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER_VERSION);
1298 event = gst_event_new_tag (tags);
1300 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1301 enc->priv->pending_events =
1302 g_list_append (enc->priv->pending_events, event);
1303 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1308 case GST_EVENT_CAPS:
1312 gst_event_parse_caps (event, &caps);
1313 gst_audio_encoder_sink_setcaps (enc, caps);
1314 gst_event_unref (event);
1327 gst_audio_encoder_sink_event (GstPad * pad, GstObject * parent,
1330 GstAudioEncoder *enc;
1331 GstAudioEncoderClass *klass;
1332 gboolean handled = FALSE;
1333 gboolean ret = TRUE;
1335 enc = GST_AUDIO_ENCODER (parent);
1336 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1338 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1339 GST_EVENT_TYPE_NAME (event));
1342 handled = klass->event (enc, event);
1345 handled = gst_audio_encoder_sink_eventfunc (enc, event);
1348 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1349 * For EOS this is required because no buffer or serialized event
1350 * will come after EOS and nothing could trigger another
1351 * _finish_frame() call.
1353 * For FLUSH_STOP this is required because it is expected
1354 * to be forwarded immediately and no buffers are queued anyway.
1356 if (!GST_EVENT_IS_SERIALIZED (event)
1357 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1358 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1359 ret = gst_pad_event_default (pad, parent, event);
1361 GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1362 enc->priv->pending_events =
1363 g_list_append (enc->priv->pending_events, event);
1364 GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1369 GST_DEBUG_OBJECT (enc, "event handled");
1375 gst_audio_encoder_sink_query (GstPad * pad, GstObject * parent,
1378 gboolean res = FALSE;
1379 GstAudioEncoder *enc;
1381 enc = GST_AUDIO_ENCODER (parent);
1383 switch (GST_QUERY_TYPE (query)) {
1384 case GST_QUERY_FORMATS:
1386 gst_query_set_formats (query, 3,
1387 GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT);
1391 case GST_QUERY_CONVERT:
1393 GstFormat src_fmt, dest_fmt;
1394 gint64 src_val, dest_val;
1396 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1397 if (!(res = gst_audio_info_convert (&enc->priv->ctx.info,
1398 src_fmt, src_val, dest_fmt, &dest_val)))
1400 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1404 case GST_QUERY_CAPS:
1406 GstCaps *filter, *caps;
1407 GstAudioEncoderClass *klass;
1409 gst_query_parse_caps (query, &filter);
1411 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1412 if (klass->getcaps) {
1413 caps = klass->getcaps (enc, filter);
1414 gst_query_set_caps_result (query, caps);
1415 gst_caps_unref (caps);
1421 res = gst_pad_query_default (pad, parent, query);
1430 * gst_audio_encoded_audio_convert:
1431 * @fmt: audio format of the encoded audio
1432 * @bytes: number of encoded bytes
1433 * @samples: number of encoded samples
1434 * @src_format: source format
1435 * @src_value: source value
1436 * @dest_format: destination format
1437 * @dest_value: destination format
1439 * Helper function to convert @src_value in @src_format to @dest_value in
1440 * @dest_format for encoded audio data. Conversion is possible between
1441 * BYTE and TIME format by using estimated bitrate based on
1442 * @samples and @bytes (and @fmt).
1446 /* FIXME: make gst_audio_encoded_audio_convert() public? */
1448 gst_audio_encoded_audio_convert (GstAudioInfo * fmt,
1449 gint64 bytes, gint64 samples, GstFormat src_format,
1450 gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
1452 gboolean res = FALSE;
1454 g_return_val_if_fail (dest_format != NULL, FALSE);
1455 g_return_val_if_fail (dest_value != NULL, FALSE);
1457 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
1460 *dest_value = src_value;
1464 if (samples == 0 || bytes == 0 || fmt->rate == 0) {
1465 GST_DEBUG ("not enough metadata yet to convert");
1471 switch (src_format) {
1472 case GST_FORMAT_BYTES:
1473 switch (*dest_format) {
1474 case GST_FORMAT_TIME:
1475 *dest_value = gst_util_uint64_scale (src_value,
1476 GST_SECOND * samples, bytes);
1483 case GST_FORMAT_TIME:
1484 switch (*dest_format) {
1485 case GST_FORMAT_BYTES:
1486 *dest_value = gst_util_uint64_scale (src_value, bytes,
1487 samples * GST_SECOND);
1502 /* FIXME ? are any of these queries (other than latency) an encoder's business
1503 * also, the conversion stuff might seem to make sense, but seems to not mind
1504 * segment stuff etc at all
1505 * Supposedly that's backward compatibility ... */
1507 gst_audio_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1509 GstAudioEncoder *enc;
1510 gboolean res = FALSE;
1512 enc = GST_AUDIO_ENCODER (parent);
1514 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1516 switch (GST_QUERY_TYPE (query)) {
1517 case GST_QUERY_POSITION:
1519 GstFormat fmt, req_fmt;
1522 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1523 GST_LOG_OBJECT (enc, "returning peer response");
1527 gst_query_parse_position (query, &req_fmt, NULL);
1528 fmt = GST_FORMAT_TIME;
1529 if (!(res = gst_pad_peer_query_position (enc->sinkpad, fmt, &pos)))
1533 gst_pad_peer_query_convert (enc->sinkpad, fmt, pos, req_fmt,
1535 gst_query_set_position (query, req_fmt, val);
1539 case GST_QUERY_DURATION:
1541 GstFormat fmt, req_fmt;
1544 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1545 GST_LOG_OBJECT (enc, "returning peer response");
1549 gst_query_parse_duration (query, &req_fmt, NULL);
1550 fmt = GST_FORMAT_TIME;
1551 if (!(res = gst_pad_peer_query_duration (enc->sinkpad, fmt, &dur)))
1555 gst_pad_peer_query_convert (enc->sinkpad, fmt, dur, req_fmt,
1557 gst_query_set_duration (query, req_fmt, val);
1561 case GST_QUERY_FORMATS:
1563 gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
1567 case GST_QUERY_CONVERT:
1569 GstFormat src_fmt, dest_fmt;
1570 gint64 src_val, dest_val;
1572 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1573 if (!(res = gst_audio_encoded_audio_convert (&enc->priv->ctx.info,
1574 enc->priv->bytes_out, enc->priv->samples_in, src_fmt, src_val,
1575 &dest_fmt, &dest_val)))
1577 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1580 case GST_QUERY_LATENCY:
1582 if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1584 GstClockTime min_latency, max_latency;
1586 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1587 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1588 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1589 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1591 GST_OBJECT_LOCK (enc);
1592 /* add our latency */
1593 if (min_latency != -1)
1594 min_latency += enc->priv->ctx.min_latency;
1595 if (max_latency != -1)
1596 max_latency += enc->priv->ctx.max_latency;
1597 GST_OBJECT_UNLOCK (enc);
1599 gst_query_set_latency (query, live, min_latency, max_latency);
1604 res = gst_pad_query_default (pad, parent, query);
1612 gst_audio_encoder_set_property (GObject * object, guint prop_id,
1613 const GValue * value, GParamSpec * pspec)
1615 GstAudioEncoder *enc;
1617 enc = GST_AUDIO_ENCODER (object);
1620 case PROP_PERFECT_TS:
1621 if (enc->priv->granule && !g_value_get_boolean (value))
1622 GST_WARNING_OBJECT (enc, "perfect-timestamp can not be set FALSE "
1623 "while granule handling is enabled");
1625 enc->priv->perfect_ts = g_value_get_boolean (value);
1627 case PROP_HARD_RESYNC:
1628 enc->priv->hard_resync = g_value_get_boolean (value);
1630 case PROP_TOLERANCE:
1631 enc->priv->tolerance = g_value_get_int64 (value);
1634 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1640 gst_audio_encoder_get_property (GObject * object, guint prop_id,
1641 GValue * value, GParamSpec * pspec)
1643 GstAudioEncoder *enc;
1645 enc = GST_AUDIO_ENCODER (object);
1648 case PROP_PERFECT_TS:
1649 g_value_set_boolean (value, enc->priv->perfect_ts);
1652 g_value_set_boolean (value, enc->priv->granule);
1654 case PROP_HARD_RESYNC:
1655 g_value_set_boolean (value, enc->priv->hard_resync);
1657 case PROP_TOLERANCE:
1658 g_value_set_int64 (value, enc->priv->tolerance);
1661 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1667 gst_audio_encoder_activate (GstAudioEncoder * enc, gboolean active)
1669 GstAudioEncoderClass *klass;
1670 gboolean result = FALSE;
1672 klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1674 g_return_val_if_fail (!enc->priv->granule || enc->priv->perfect_ts, FALSE);
1676 GST_DEBUG_OBJECT (enc, "activate %d", active);
1680 if (enc->priv->tags)
1681 gst_tag_list_free (enc->priv->tags);
1682 enc->priv->tags = gst_tag_list_new_empty ();
1684 if (!enc->priv->active && klass->start)
1685 result = klass->start (enc);
1687 /* We must make sure streaming has finished before resetting things
1688 * and calling the ::stop vfunc */
1689 GST_PAD_STREAM_LOCK (enc->sinkpad);
1690 GST_PAD_STREAM_UNLOCK (enc->sinkpad);
1692 if (enc->priv->active && klass->stop)
1693 result = klass->stop (enc);
1696 gst_audio_encoder_reset (enc, TRUE);
1698 GST_DEBUG_OBJECT (enc, "activate return: %d", result);
1704 gst_audio_encoder_sink_activate_mode (GstPad * pad, GstObject * parent,
1705 GstPadMode mode, gboolean active)
1707 gboolean result = TRUE;
1708 GstAudioEncoder *enc;
1710 enc = GST_AUDIO_ENCODER (parent);
1712 GST_DEBUG_OBJECT (enc, "sink activate push %d", active);
1714 result = gst_audio_encoder_activate (enc, active);
1717 enc->priv->active = active;
1719 GST_DEBUG_OBJECT (enc, "sink activate push return: %d", result);
1725 * gst_audio_encoder_get_audio_info:
1726 * @enc: a #GstAudioEncoder
1728 * Returns: a #GstAudioInfo describing the input audio format
1733 gst_audio_encoder_get_audio_info (GstAudioEncoder * enc)
1735 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), NULL);
1737 return &enc->priv->ctx.info;
1741 * gst_audio_encoder_set_frame_samples_min:
1742 * @enc: a #GstAudioEncoder
1743 * @num: number of samples per frame
1745 * Sets number of samples (per channel) subclass needs to be handed,
1746 * at least or will be handed all available if 0.
1748 * If an exact number of samples is required, gst_audio_encoder_set_frame_samples_max()
1749 * must be called with the same number.
1754 gst_audio_encoder_set_frame_samples_min (GstAudioEncoder * enc, gint num)
1756 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1758 enc->priv->ctx.frame_samples_min = num;
1762 * gst_audio_encoder_get_frame_samples_min:
1763 * @enc: a #GstAudioEncoder
1765 * Returns: currently minimum requested samples per frame
1770 gst_audio_encoder_get_frame_samples_min (GstAudioEncoder * enc)
1772 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1774 return enc->priv->ctx.frame_samples_min;
1778 * gst_audio_encoder_set_frame_samples_max:
1779 * @enc: a #GstAudioEncoder
1780 * @num: number of samples per frame
1782 * Sets number of samples (per channel) subclass needs to be handed,
1783 * at most or will be handed all available if 0.
1785 * If an exact number of samples is required, gst_audio_encoder_set_frame_samples_min()
1786 * must be called with the same number.
1791 gst_audio_encoder_set_frame_samples_max (GstAudioEncoder * enc, gint num)
1793 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1795 enc->priv->ctx.frame_samples_max = num;
1799 * gst_audio_encoder_get_frame_samples_min:
1800 * @enc: a #GstAudioEncoder
1802 * Returns: currently maximum requested samples per frame
1807 gst_audio_encoder_get_frame_samples_max (GstAudioEncoder * enc)
1809 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1811 return enc->priv->ctx.frame_samples_max;
1815 * gst_audio_encoder_set_frame_max:
1816 * @enc: a #GstAudioEncoder
1817 * @num: number of frames
1819 * Sets max number of frames accepted at once (assumed minimally 1).
1820 * Requires @frame_samples_min and @frame_samples_max to be the equal.
1825 gst_audio_encoder_set_frame_max (GstAudioEncoder * enc, gint num)
1827 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1829 enc->priv->ctx.frame_max = num;
1833 * gst_audio_encoder_get_frame_max:
1834 * @enc: a #GstAudioEncoder
1836 * Returns: currently configured maximum handled frames
1841 gst_audio_encoder_get_frame_max (GstAudioEncoder * enc)
1843 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1845 return enc->priv->ctx.frame_max;
1849 * gst_audio_encoder_set_lookahead:
1850 * @enc: a #GstAudioEncoder
1853 * Sets encoder lookahead (in units of input rate samples)
1858 gst_audio_encoder_set_lookahead (GstAudioEncoder * enc, gint num)
1860 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1862 enc->priv->ctx.lookahead = num;
1866 * gst_audio_encoder_get_lookahead:
1867 * @enc: a #GstAudioEncoder
1869 * Returns: currently configured encoder lookahead
1872 gst_audio_encoder_get_lookahead (GstAudioEncoder * enc)
1874 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1876 return enc->priv->ctx.lookahead;
1880 * gst_audio_encoder_set_latency:
1881 * @enc: a #GstAudioEncoder
1882 * @min: minimum latency
1883 * @max: maximum latency
1885 * Sets encoder latency.
1890 gst_audio_encoder_set_latency (GstAudioEncoder * enc,
1891 GstClockTime min, GstClockTime max)
1893 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1895 GST_OBJECT_LOCK (enc);
1896 enc->priv->ctx.min_latency = min;
1897 enc->priv->ctx.max_latency = max;
1898 GST_OBJECT_UNLOCK (enc);
1902 * gst_audio_encoder_get_latency:
1903 * @enc: a #GstAudioEncoder
1904 * @min: (out) (allow-none): a pointer to storage to hold minimum latency
1905 * @max: (out) (allow-none): a pointer to storage to hold maximum latency
1907 * Sets the variables pointed to by @min and @max to the currently configured
1913 gst_audio_encoder_get_latency (GstAudioEncoder * enc,
1914 GstClockTime * min, GstClockTime * max)
1916 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1918 GST_OBJECT_LOCK (enc);
1920 *min = enc->priv->ctx.min_latency;
1922 *max = enc->priv->ctx.max_latency;
1923 GST_OBJECT_UNLOCK (enc);
1927 * gst_audio_encoder_set_mark_granule:
1928 * @enc: a #GstAudioEncoder
1929 * @enabled: new state
1931 * Enable or disable encoder granule handling.
1938 gst_audio_encoder_set_mark_granule (GstAudioEncoder * enc, gboolean enabled)
1940 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1942 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1944 GST_OBJECT_LOCK (enc);
1945 enc->priv->granule = enabled;
1946 GST_OBJECT_UNLOCK (enc);
1950 * gst_audio_encoder_get_mark_granule:
1951 * @enc: a #GstAudioEncoder
1953 * Queries if the encoder will handle granule marking.
1955 * Returns: TRUE if granule marking is enabled.
1962 gst_audio_encoder_get_mark_granule (GstAudioEncoder * enc)
1966 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
1968 GST_OBJECT_LOCK (enc);
1969 result = enc->priv->granule;
1970 GST_OBJECT_UNLOCK (enc);
1976 * gst_audio_encoder_set_perfect_timestamp:
1977 * @enc: a #GstAudioEncoder
1978 * @enabled: new state
1980 * Enable or disable encoder perfect output timestamp preference.
1987 gst_audio_encoder_set_perfect_timestamp (GstAudioEncoder * enc,
1990 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1992 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1994 GST_OBJECT_LOCK (enc);
1995 enc->priv->perfect_ts = enabled;
1996 GST_OBJECT_UNLOCK (enc);
2000 * gst_audio_encoder_get_perfect_timestamp:
2001 * @enc: a #GstAudioEncoder
2003 * Queries encoder perfect timestamp behaviour.
2005 * Returns: TRUE if pefect timestamp setting enabled.
2012 gst_audio_encoder_get_perfect_timestamp (GstAudioEncoder * enc)
2016 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
2018 GST_OBJECT_LOCK (enc);
2019 result = enc->priv->perfect_ts;
2020 GST_OBJECT_UNLOCK (enc);
2026 * gst_audio_encoder_set_hard_sync:
2027 * @enc: a #GstAudioEncoder
2028 * @enabled: new state
2030 * Sets encoder hard resync handling.
2037 gst_audio_encoder_set_hard_resync (GstAudioEncoder * enc, gboolean enabled)
2039 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2041 GST_LOG_OBJECT (enc, "enabled: %d", enabled);
2043 GST_OBJECT_LOCK (enc);
2044 enc->priv->hard_resync = enabled;
2045 GST_OBJECT_UNLOCK (enc);
2049 * gst_audio_encoder_get_hard_sync:
2050 * @enc: a #GstAudioEncoder
2052 * Queries encoder's hard resync setting.
2054 * Returns: TRUE if hard resync is enabled.
2061 gst_audio_encoder_get_hard_resync (GstAudioEncoder * enc)
2065 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
2067 GST_OBJECT_LOCK (enc);
2068 result = enc->priv->hard_resync;
2069 GST_OBJECT_UNLOCK (enc);
2075 * gst_audio_encoder_set_tolerance:
2076 * @enc: a #GstAudioEncoder
2077 * @tolerance: new tolerance
2079 * Configures encoder audio jitter tolerance threshold.
2086 gst_audio_encoder_set_tolerance (GstAudioEncoder * enc, gint64 tolerance)
2088 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2090 GST_OBJECT_LOCK (enc);
2091 enc->priv->tolerance = tolerance;
2092 GST_OBJECT_UNLOCK (enc);
2096 * gst_audio_encoder_get_tolerance:
2097 * @enc: a #GstAudioEncoder
2099 * Queries current audio jitter tolerance threshold.
2101 * Returns: encoder audio jitter tolerance threshold.
2108 gst_audio_encoder_get_tolerance (GstAudioEncoder * enc)
2112 g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
2114 GST_OBJECT_LOCK (enc);
2115 result = enc->priv->tolerance;
2116 GST_OBJECT_UNLOCK (enc);
2122 * gst_audio_encoder_merge_tags:
2123 * @enc: a #GstAudioEncoder
2124 * @tags: a #GstTagList to merge
2125 * @mode: the #GstTagMergeMode to use
2127 * Adds tags to so-called pending tags, which will be processed
2128 * before pushing out data downstream.
2130 * Note that this is provided for convenience, and the subclass is
2131 * not required to use this and can still do tag handling on its own,
2132 * although it should be aware that baseclass already takes care
2133 * of the usual CODEC/AUDIO_CODEC tags.
2140 gst_audio_encoder_merge_tags (GstAudioEncoder * enc,
2141 const GstTagList * tags, GstTagMergeMode mode)
2145 g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2146 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2148 GST_OBJECT_LOCK (enc);
2150 GST_DEBUG_OBJECT (enc, "merging tags %" GST_PTR_FORMAT, tags);
2151 otags = enc->priv->tags;
2152 enc->priv->tags = gst_tag_list_merge (enc->priv->tags, tags, mode);
2154 gst_tag_list_free (otags);
2155 GST_OBJECT_UNLOCK (enc);