2 * Copyright (C) 2009 Igalia S.L.
3 * Author: Iago Toral Quiroga <itoral@igalia.com>
4 * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
5 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
6 * Contact: Stefan Kost <stefan.kost@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gstaudiodecoder
26 * @title: GstAudioDecoder
27 * @short_description: Base class for audio decoders
28 * @see_also: #GstBaseTransform
30 * This base class is for audio decoders turning encoded data into
33 * GstAudioDecoder and subclass should cooperate as follows.
37 * * Initially, GstAudioDecoder calls @start when the decoder element
38 * is activated, which allows subclass to perform any global setup.
39 * Base class (context) parameters can already be set according to subclass
40 * capabilities (or possibly upon receive more information in subsequent
42 * * GstAudioDecoder calls @set_format to inform subclass of the format
43 * of input audio data that it is about to receive.
44 * While unlikely, it might be called more than once, if changing input
45 * parameters require reconfiguration.
46 * * GstAudioDecoder calls @stop at end of all processing.
48 * As of configuration stage, and throughout processing, GstAudioDecoder
49 * provides various (context) parameters, e.g. describing the format of
50 * output audio data (valid when output caps have been set) or current parsing state.
51 * Conversely, subclass can and should configure context to inform
52 * base class of its expectation w.r.t. buffer handling.
55 * * Base class gathers input data, and optionally allows subclass
56 * to parse this into subsequently manageable (as defined by subclass)
57 * chunks. Such chunks are subsequently referred to as 'frames',
58 * though they may or may not correspond to 1 (or more) audio format frame.
59 * * Input frame is provided to subclass' @handle_frame.
60 * * If codec processing results in decoded data, subclass should call
61 * @gst_audio_decoder_finish_frame to have decoded data pushed
63 * * Just prior to actually pushing a buffer downstream,
64 * it is passed to @pre_push. Subclass should either use this callback
65 * to arrange for additional downstream pushing or otherwise ensure such
66 * custom pushing occurs after at least a method call has finished since
67 * setting src pad caps.
68 * * During the parsing process GstAudioDecoderClass will handle both
69 * srcpad and sinkpad events. Sink events will be passed to subclass
70 * if @event callback has been provided.
74 * * GstAudioDecoder class calls @stop to inform the subclass that data
75 * parsing will be stopped.
77 * Subclass is responsible for providing pad template caps for
78 * source and sink pads. The pads need to be named "sink" and "src". It also
79 * needs to set the fixed caps on srcpad, when the format is ensured. This
80 * is typically when base class calls subclass' @set_format function, though
81 * it might be delayed until calling @gst_audio_decoder_finish_frame.
83 * In summary, above process should have subclass concentrating on
84 * codec data processing while leaving other matters to base class,
85 * such as most notably timestamp handling. While it may exert more control
86 * in this area (see e.g. @pre_push), it is very much not recommended.
88 * In particular, base class will try to arrange for perfect output timestamps
89 * as much as possible while tracking upstream timestamps.
90 * To this end, if deviation between the next ideal expected perfect timestamp
91 * and upstream exceeds #GstAudioDecoder:tolerance, then resync to upstream
92 * occurs (which would happen always if the tolerance mechanism is disabled).
94 * In non-live pipelines, baseclass can also (configurably) arrange for
95 * output buffer aggregation which may help to redue large(r) numbers of
96 * small(er) buffers being pushed and processed downstream. Note that this
97 * feature is only available if the buffer layout is interleaved. For planar
98 * buffers, the decoder implementation is fully responsible for the output
101 * On the other hand, it should be noted that baseclass only provides limited
102 * seeking support (upon explicit subclass request), as full-fledged support
103 * should rather be left to upstream demuxer, parser or alike. This simple
104 * approach caters for seeking and duration reporting using estimated input
107 * Things that subclass need to take care of:
109 * * Provide pad templates
110 * * Set source pad caps when appropriate
111 * * Set user-configurable properties to sane defaults for format and
112 * implementing codec at hand, and convey some subclass capabilities and
113 * expectations in context.
115 * * Accept data in @handle_frame and provide encoded results to
116 * @gst_audio_decoder_finish_frame. If it is prepared to perform
117 * PLC, it should also accept NULL data in @handle_frame and provide for
118 * data for indicated duration.
126 #include "gstaudiodecoder.h"
127 #include "gstaudioutilsprivate.h"
128 #include <gst/pbutils/descriptions.h>
132 GST_DEBUG_CATEGORY (audiodecoder_debug);
133 #define GST_CAT_DEFAULT audiodecoder_debug
148 #define DEFAULT_LATENCY 0
149 #define DEFAULT_TOLERANCE 0
150 #define DEFAULT_PLC FALSE
151 #define DEFAULT_DRAINABLE TRUE
152 #define DEFAULT_NEEDS_FORMAT FALSE
154 typedef struct _GstAudioDecoderContext
156 /* last negotiated input caps */
159 /* (output) audio format */
162 gboolean output_format_changed;
168 gboolean had_output_data;
169 gboolean had_input_data;
176 gboolean do_estimate_rate;
178 GstCaps *allocation_caps;
179 /* MT-protected (with LOCK) */
180 GstClockTime min_latency;
181 GstClockTime max_latency;
183 GstAllocator *allocator;
184 GstAllocationParams params;
185 } GstAudioDecoderContext;
187 struct _GstAudioDecoderPrivate
189 /* activation status */
192 /* input base/first ts as basis for output ts */
193 GstClockTime base_ts;
194 /* input samples processed and sent downstream so far (w.r.t. base_ts) */
197 /* collected input data */
199 /* tracking input ts for changes */
200 GstClockTime prev_ts;
201 guint64 prev_distance;
202 /* frames obtained from input */
204 /* collected output data */
205 GstAdapter *adapter_out;
206 /* ts and duration for output data collected above */
207 GstClockTime out_ts, out_dur;
208 /* mark outgoing discont */
211 /* subclass gave all it could already */
213 /* subclass currently being forcibly drained */
215 /* input_segment are output_segment identical */
216 gboolean in_out_segment_sync;
217 /* expecting the buffer with DISCONT flag */
218 gboolean expecting_discont_buf;
221 /* input bps estimatation */
222 /* global in bytes seen */
224 /* global samples sent out */
226 /* bytes flushed during parsing */
231 /* upstream stream tags (global tags are passed through as-is) */
232 GstTagList *upstream_tags;
235 GstTagList *taglist; /* FIXME: rename to decoder_tags */
236 GstTagMergeMode decoder_tags_merge_mode;
238 gboolean taglist_changed; /* FIXME: rename to tags_changed */
240 /* whether circumstances allow output aggregation */
243 /* reverse playback queues */
248 /* reversed output */
251 /* context storage */
252 GstAudioDecoderContext ctx;
255 GstClockTime latency;
256 GstClockTime tolerance;
259 gboolean needs_format;
261 /* pending serialized sink events, will be sent from finish_frame() */
262 GList *pending_events;
265 gboolean use_default_pad_acceptcaps;
268 static void gst_audio_decoder_finalize (GObject * object);
269 static void gst_audio_decoder_set_property (GObject * object,
270 guint prop_id, const GValue * value, GParamSpec * pspec);
271 static void gst_audio_decoder_get_property (GObject * object,
272 guint prop_id, GValue * value, GParamSpec * pspec);
274 static void gst_audio_decoder_clear_queues (GstAudioDecoder * dec);
275 static GstFlowReturn gst_audio_decoder_chain_reverse (GstAudioDecoder *
276 dec, GstBuffer * buf);
278 static GstStateChangeReturn gst_audio_decoder_change_state (GstElement *
279 element, GstStateChange transition);
280 static gboolean gst_audio_decoder_sink_eventfunc (GstAudioDecoder * dec,
282 static gboolean gst_audio_decoder_src_eventfunc (GstAudioDecoder * dec,
284 static gboolean gst_audio_decoder_sink_event (GstPad * pad, GstObject * parent,
286 static gboolean gst_audio_decoder_src_event (GstPad * pad, GstObject * parent,
288 static gboolean gst_audio_decoder_sink_setcaps (GstAudioDecoder * dec,
290 static GstFlowReturn gst_audio_decoder_chain (GstPad * pad, GstObject * parent,
292 static gboolean gst_audio_decoder_src_query (GstPad * pad, GstObject * parent,
294 static gboolean gst_audio_decoder_sink_query (GstPad * pad, GstObject * parent,
296 static void gst_audio_decoder_reset (GstAudioDecoder * dec, gboolean full);
298 static gboolean gst_audio_decoder_decide_allocation_default (GstAudioDecoder *
299 dec, GstQuery * query);
300 static gboolean gst_audio_decoder_propose_allocation_default (GstAudioDecoder *
301 dec, GstQuery * query);
302 static gboolean gst_audio_decoder_negotiate_default (GstAudioDecoder * dec);
303 static gboolean gst_audio_decoder_negotiate_unlocked (GstAudioDecoder * dec);
304 static gboolean gst_audio_decoder_handle_gap (GstAudioDecoder * dec,
306 static gboolean gst_audio_decoder_sink_query_default (GstAudioDecoder * dec,
308 static gboolean gst_audio_decoder_src_query_default (GstAudioDecoder * dec,
311 static gboolean gst_audio_decoder_transform_meta_default (GstAudioDecoder *
312 decoder, GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf);
314 static GstElementClass *parent_class = NULL;
315 static gint private_offset = 0;
317 static void gst_audio_decoder_class_init (GstAudioDecoderClass * klass);
318 static void gst_audio_decoder_init (GstAudioDecoder * dec,
319 GstAudioDecoderClass * klass);
322 gst_audio_decoder_get_type (void)
324 static volatile gsize audio_decoder_type = 0;
326 if (g_once_init_enter (&audio_decoder_type)) {
328 static const GTypeInfo audio_decoder_info = {
329 sizeof (GstAudioDecoderClass),
332 (GClassInitFunc) gst_audio_decoder_class_init,
335 sizeof (GstAudioDecoder),
337 (GInstanceInitFunc) gst_audio_decoder_init,
340 _type = g_type_register_static (GST_TYPE_ELEMENT,
341 "GstAudioDecoder", &audio_decoder_info, G_TYPE_FLAG_ABSTRACT);
344 g_type_add_instance_private (_type, sizeof (GstAudioDecoderPrivate));
346 g_once_init_leave (&audio_decoder_type, _type);
348 return audio_decoder_type;
351 static inline GstAudioDecoderPrivate *
352 gst_audio_decoder_get_instance_private (GstAudioDecoder * self)
354 return (G_STRUCT_MEMBER_P (self, private_offset));
358 gst_audio_decoder_class_init (GstAudioDecoderClass * klass)
360 GObjectClass *gobject_class;
361 GstElementClass *element_class;
362 GstAudioDecoderClass *audiodecoder_class;
364 gobject_class = G_OBJECT_CLASS (klass);
365 element_class = GST_ELEMENT_CLASS (klass);
366 audiodecoder_class = GST_AUDIO_DECODER_CLASS (klass);
368 parent_class = g_type_class_peek_parent (klass);
370 if (private_offset != 0)
371 g_type_class_adjust_private_offset (klass, &private_offset);
373 GST_DEBUG_CATEGORY_INIT (audiodecoder_debug, "audiodecoder", 0,
374 "audio decoder base class");
376 gobject_class->set_property = gst_audio_decoder_set_property;
377 gobject_class->get_property = gst_audio_decoder_get_property;
378 gobject_class->finalize = gst_audio_decoder_finalize;
380 element_class->change_state =
381 GST_DEBUG_FUNCPTR (gst_audio_decoder_change_state);
384 g_object_class_install_property (gobject_class, PROP_LATENCY,
385 g_param_spec_int64 ("min-latency", "Minimum Latency",
386 "Aggregate output data to a minimum of latency time (ns)",
387 0, G_MAXINT64, DEFAULT_LATENCY,
388 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 g_object_class_install_property (gobject_class, PROP_TOLERANCE,
391 g_param_spec_int64 ("tolerance", "Tolerance",
392 "Perfect ts while timestamp jitter/imperfection within tolerance (ns)",
393 0, G_MAXINT64, DEFAULT_TOLERANCE,
394 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396 g_object_class_install_property (gobject_class, PROP_PLC,
397 g_param_spec_boolean ("plc", "Packet Loss Concealment",
398 "Perform packet loss concealment (if supported)",
399 DEFAULT_PLC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
401 audiodecoder_class->sink_event =
402 GST_DEBUG_FUNCPTR (gst_audio_decoder_sink_eventfunc);
403 audiodecoder_class->src_event =
404 GST_DEBUG_FUNCPTR (gst_audio_decoder_src_eventfunc);
405 audiodecoder_class->propose_allocation =
406 GST_DEBUG_FUNCPTR (gst_audio_decoder_propose_allocation_default);
407 audiodecoder_class->decide_allocation =
408 GST_DEBUG_FUNCPTR (gst_audio_decoder_decide_allocation_default);
409 audiodecoder_class->negotiate =
410 GST_DEBUG_FUNCPTR (gst_audio_decoder_negotiate_default);
411 audiodecoder_class->sink_query =
412 GST_DEBUG_FUNCPTR (gst_audio_decoder_sink_query_default);
413 audiodecoder_class->src_query =
414 GST_DEBUG_FUNCPTR (gst_audio_decoder_src_query_default);
415 audiodecoder_class->transform_meta =
416 GST_DEBUG_FUNCPTR (gst_audio_decoder_transform_meta_default);
420 gst_audio_decoder_init (GstAudioDecoder * dec, GstAudioDecoderClass * klass)
422 GstPadTemplate *pad_template;
424 GST_DEBUG_OBJECT (dec, "gst_audio_decoder_init");
426 dec->priv = gst_audio_decoder_get_instance_private (dec);
430 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
431 g_return_if_fail (pad_template != NULL);
433 dec->sinkpad = gst_pad_new_from_template (pad_template, "sink");
434 gst_pad_set_event_function (dec->sinkpad,
435 GST_DEBUG_FUNCPTR (gst_audio_decoder_sink_event));
436 gst_pad_set_chain_function (dec->sinkpad,
437 GST_DEBUG_FUNCPTR (gst_audio_decoder_chain));
438 gst_pad_set_query_function (dec->sinkpad,
439 GST_DEBUG_FUNCPTR (gst_audio_decoder_sink_query));
440 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
441 GST_DEBUG_OBJECT (dec, "sinkpad created");
443 /* Setup source pad */
445 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
446 g_return_if_fail (pad_template != NULL);
448 dec->srcpad = gst_pad_new_from_template (pad_template, "src");
449 gst_pad_set_event_function (dec->srcpad,
450 GST_DEBUG_FUNCPTR (gst_audio_decoder_src_event));
451 gst_pad_set_query_function (dec->srcpad,
452 GST_DEBUG_FUNCPTR (gst_audio_decoder_src_query));
453 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
454 GST_DEBUG_OBJECT (dec, "srcpad created");
456 dec->priv->adapter = gst_adapter_new ();
457 dec->priv->adapter_out = gst_adapter_new ();
458 g_queue_init (&dec->priv->frames);
460 g_rec_mutex_init (&dec->stream_lock);
462 /* property default */
463 dec->priv->latency = DEFAULT_LATENCY;
464 dec->priv->tolerance = DEFAULT_TOLERANCE;
465 dec->priv->plc = DEFAULT_PLC;
466 dec->priv->drainable = DEFAULT_DRAINABLE;
467 dec->priv->needs_format = DEFAULT_NEEDS_FORMAT;
470 dec->priv->ctx.min_latency = 0;
471 dec->priv->ctx.max_latency = 0;
472 gst_audio_decoder_reset (dec, TRUE);
473 GST_DEBUG_OBJECT (dec, "init ok");
477 gst_audio_decoder_reset (GstAudioDecoder * dec, gboolean full)
479 GST_DEBUG_OBJECT (dec, "gst_audio_decoder_reset");
481 GST_AUDIO_DECODER_STREAM_LOCK (dec);
484 dec->priv->active = FALSE;
485 GST_OBJECT_LOCK (dec);
486 dec->priv->bytes_in = 0;
487 dec->priv->samples_out = 0;
488 GST_OBJECT_UNLOCK (dec);
490 dec->priv->error_count = 0;
491 gst_audio_decoder_clear_queues (dec);
493 if (dec->priv->taglist) {
494 gst_tag_list_unref (dec->priv->taglist);
495 dec->priv->taglist = NULL;
497 dec->priv->decoder_tags_merge_mode = GST_TAG_MERGE_KEEP_ALL;
498 if (dec->priv->upstream_tags) {
499 gst_tag_list_unref (dec->priv->upstream_tags);
500 dec->priv->upstream_tags = NULL;
502 dec->priv->taglist_changed = FALSE;
504 gst_segment_init (&dec->input_segment, GST_FORMAT_TIME);
505 gst_segment_init (&dec->output_segment, GST_FORMAT_TIME);
506 dec->priv->in_out_segment_sync = TRUE;
508 g_list_foreach (dec->priv->pending_events, (GFunc) gst_event_unref, NULL);
509 g_list_free (dec->priv->pending_events);
510 dec->priv->pending_events = NULL;
512 if (dec->priv->ctx.allocator)
513 gst_object_unref (dec->priv->ctx.allocator);
515 GST_OBJECT_LOCK (dec);
516 gst_caps_replace (&dec->priv->ctx.input_caps, NULL);
517 gst_caps_replace (&dec->priv->ctx.caps, NULL);
518 gst_caps_replace (&dec->priv->ctx.allocation_caps, NULL);
520 memset (&dec->priv->ctx, 0, sizeof (dec->priv->ctx));
522 gst_audio_info_init (&dec->priv->ctx.info);
523 GST_OBJECT_UNLOCK (dec);
524 dec->priv->ctx.max_errors = GST_AUDIO_DECODER_MAX_ERRORS;
525 dec->priv->ctx.had_output_data = FALSE;
526 dec->priv->ctx.had_input_data = FALSE;
529 g_queue_foreach (&dec->priv->frames, (GFunc) gst_buffer_unref, NULL);
530 g_queue_clear (&dec->priv->frames);
531 gst_adapter_clear (dec->priv->adapter);
532 gst_adapter_clear (dec->priv->adapter_out);
533 dec->priv->out_ts = GST_CLOCK_TIME_NONE;
534 dec->priv->out_dur = 0;
535 dec->priv->prev_ts = GST_CLOCK_TIME_NONE;
536 dec->priv->prev_distance = 0;
537 dec->priv->drained = TRUE;
538 dec->priv->base_ts = GST_CLOCK_TIME_NONE;
539 dec->priv->samples = 0;
540 dec->priv->discont = TRUE;
541 dec->priv->sync_flush = FALSE;
543 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
547 gst_audio_decoder_finalize (GObject * object)
549 GstAudioDecoder *dec;
551 g_return_if_fail (GST_IS_AUDIO_DECODER (object));
552 dec = GST_AUDIO_DECODER (object);
554 if (dec->priv->adapter) {
555 g_object_unref (dec->priv->adapter);
557 if (dec->priv->adapter_out) {
558 g_object_unref (dec->priv->adapter_out);
561 g_rec_mutex_clear (&dec->stream_lock);
563 G_OBJECT_CLASS (parent_class)->finalize (object);
567 gst_audio_decoder_create_merged_tags_event (GstAudioDecoder * dec)
569 GstTagList *merged_tags;
571 GST_LOG_OBJECT (dec, "upstream : %" GST_PTR_FORMAT, dec->priv->upstream_tags);
572 GST_LOG_OBJECT (dec, "decoder : %" GST_PTR_FORMAT, dec->priv->taglist);
573 GST_LOG_OBJECT (dec, "mode : %d", dec->priv->decoder_tags_merge_mode);
576 gst_tag_list_merge (dec->priv->upstream_tags,
577 dec->priv->taglist, dec->priv->decoder_tags_merge_mode);
579 GST_DEBUG_OBJECT (dec, "merged : %" GST_PTR_FORMAT, merged_tags);
581 if (merged_tags == NULL)
584 if (gst_tag_list_is_empty (merged_tags)) {
585 gst_tag_list_unref (merged_tags);
589 return gst_event_new_tag (merged_tags);
593 gst_audio_decoder_push_event (GstAudioDecoder * dec, GstEvent * event)
595 switch (GST_EVENT_TYPE (event)) {
596 case GST_EVENT_SEGMENT:{
599 GST_AUDIO_DECODER_STREAM_LOCK (dec);
600 gst_event_copy_segment (event, &seg);
602 GST_DEBUG_OBJECT (dec, "starting segment %" GST_SEGMENT_FORMAT, &seg);
604 dec->output_segment = seg;
605 dec->priv->in_out_segment_sync =
606 gst_segment_is_equal (&dec->input_segment, &seg);
607 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
614 return gst_pad_push_event (dec->srcpad, event);
618 gst_audio_decoder_negotiate_default (GstAudioDecoder * dec)
620 GstAudioDecoderClass *klass;
624 GstQuery *query = NULL;
625 GstAllocator *allocator;
626 GstAllocationParams params;
628 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), FALSE);
629 g_return_val_if_fail (GST_AUDIO_INFO_IS_VALID (&dec->priv->ctx.info), FALSE);
630 g_return_val_if_fail (GST_IS_CAPS (dec->priv->ctx.caps), FALSE);
632 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
634 caps = dec->priv->ctx.caps;
635 if (dec->priv->ctx.allocation_caps == NULL)
636 dec->priv->ctx.allocation_caps = gst_caps_ref (caps);
638 GST_DEBUG_OBJECT (dec, "setting src caps %" GST_PTR_FORMAT, caps);
640 if (dec->priv->pending_events) {
641 GList **pending_events, *l;
643 pending_events = &dec->priv->pending_events;
645 GST_DEBUG_OBJECT (dec, "Pushing pending events");
646 for (l = *pending_events; l;) {
647 GstEvent *event = GST_EVENT (l->data);
650 if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
651 gst_audio_decoder_push_event (dec, l->data);
654 *pending_events = g_list_delete_link (*pending_events, tmp);
661 prevcaps = gst_pad_get_current_caps (dec->srcpad);
662 if (!prevcaps || !gst_caps_is_equal (prevcaps, caps))
663 res = gst_pad_set_caps (dec->srcpad, caps);
665 gst_caps_unref (prevcaps);
669 dec->priv->ctx.output_format_changed = FALSE;
671 query = gst_query_new_allocation (dec->priv->ctx.allocation_caps, TRUE);
672 if (!gst_pad_peer_query (dec->srcpad, query)) {
673 GST_DEBUG_OBJECT (dec, "didn't get downstream ALLOCATION hints");
676 g_assert (klass->decide_allocation != NULL);
677 res = klass->decide_allocation (dec, query);
679 GST_DEBUG_OBJECT (dec, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, res,
683 goto no_decide_allocation;
685 /* we got configuration from our peer or the decide_allocation method,
687 if (gst_query_get_n_allocation_params (query) > 0) {
688 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
691 gst_allocation_params_init (¶ms);
694 if (dec->priv->ctx.allocator)
695 gst_object_unref (dec->priv->ctx.allocator);
696 dec->priv->ctx.allocator = allocator;
697 dec->priv->ctx.params = params;
702 gst_query_unref (query);
707 no_decide_allocation:
709 GST_WARNING_OBJECT (dec, "Subclass failed to decide allocation");
715 gst_audio_decoder_negotiate_unlocked (GstAudioDecoder * dec)
717 GstAudioDecoderClass *klass = GST_AUDIO_DECODER_GET_CLASS (dec);
720 if (G_LIKELY (klass->negotiate))
721 ret = klass->negotiate (dec);
727 * gst_audio_decoder_negotiate:
728 * @dec: a #GstAudioDecoder
730 * Negotiate with downstream elements to currently configured #GstAudioInfo.
731 * Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
734 * Returns: %TRUE if the negotiation succeeded, else %FALSE.
737 gst_audio_decoder_negotiate (GstAudioDecoder * dec)
739 GstAudioDecoderClass *klass;
742 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), FALSE);
744 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
746 GST_AUDIO_DECODER_STREAM_LOCK (dec);
747 gst_pad_check_reconfigure (dec->srcpad);
748 if (klass->negotiate) {
749 res = klass->negotiate (dec);
751 gst_pad_mark_reconfigure (dec->srcpad);
753 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
759 * gst_audio_decoder_set_output_format:
760 * @dec: a #GstAudioDecoder
761 * @info: #GstAudioInfo
763 * Configure output info on the srcpad of @dec.
765 * Returns: %TRUE on success.
768 gst_audio_decoder_set_output_format (GstAudioDecoder * dec,
769 const GstAudioInfo * info)
772 GstCaps *caps = NULL;
774 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), FALSE);
775 g_return_val_if_fail (GST_AUDIO_INFO_IS_VALID (info), FALSE);
777 /* If the audio info can't be converted to caps,
779 caps = gst_audio_info_to_caps (info);
781 GST_WARNING_OBJECT (dec, "invalid output format");
785 res = gst_audio_decoder_set_output_caps (dec, caps);
786 gst_caps_unref (caps);
792 * gst_audio_decoder_set_output_caps:
793 * @dec: a #GstAudioDecoder
794 * @caps: (transfer none): (fixed) #GstCaps
796 * Configure output caps on the srcpad of @dec. Similar to
797 * gst_audio_decoder_set_output_format(), but allows subclasses to specify
798 * output caps that can't be expressed via #GstAudioInfo e.g. caps that have
801 * Returns: %TRUE on success.
806 gst_audio_decoder_set_output_caps (GstAudioDecoder * dec, GstCaps * caps)
813 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), FALSE);
815 GST_DEBUG_OBJECT (dec, "Setting srcpad caps %" GST_PTR_FORMAT, caps);
817 GST_AUDIO_DECODER_STREAM_LOCK (dec);
819 if (!gst_caps_is_fixed (caps))
822 /* check if caps can be parsed */
823 if (!gst_audio_info_from_caps (&info, caps))
826 /* Only allow caps that are a subset of the template caps */
827 templ_caps = gst_pad_get_pad_template_caps (dec->srcpad);
828 if (!gst_caps_is_subset (caps, templ_caps)) {
829 GST_WARNING_OBJECT (dec, "Requested output format %" GST_PTR_FORMAT
830 " do not match template %" GST_PTR_FORMAT, caps, templ_caps);
831 gst_caps_unref (templ_caps);
834 gst_caps_unref (templ_caps);
836 /* adjust ts tracking to new sample rate */
837 old_rate = GST_AUDIO_INFO_RATE (&dec->priv->ctx.info);
838 if (GST_CLOCK_TIME_IS_VALID (dec->priv->base_ts) && old_rate) {
839 dec->priv->base_ts +=
840 GST_FRAMES_TO_CLOCK_TIME (dec->priv->samples, old_rate);
841 dec->priv->samples = 0;
844 /* copy the GstAudioInfo */
845 GST_OBJECT_LOCK (dec);
846 dec->priv->ctx.info = info;
847 GST_OBJECT_UNLOCK (dec);
849 gst_caps_replace (&dec->priv->ctx.caps, caps);
850 dec->priv->ctx.output_format_changed = TRUE;
853 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
860 GST_WARNING_OBJECT (dec, "invalid output format");
867 gst_audio_decoder_sink_setcaps (GstAudioDecoder * dec, GstCaps * caps)
869 GstAudioDecoderClass *klass;
872 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
874 GST_DEBUG_OBJECT (dec, "caps: %" GST_PTR_FORMAT, caps);
876 GST_AUDIO_DECODER_STREAM_LOCK (dec);
878 if (dec->priv->ctx.input_caps
879 && gst_caps_is_equal (dec->priv->ctx.input_caps, caps)) {
880 GST_DEBUG_OBJECT (dec, "Caps did not change, not setting again");
884 /* NOTE pbutils only needed here */
885 /* TODO maybe (only) upstream demuxer/parser etc should handle this ? */
887 if (!dec->priv->taglist)
888 dec->priv->taglist = gst_tag_list_new ();
889 dec->priv->taglist = gst_tag_list_make_writable (dec->priv->taglist);
890 gst_pb_utils_add_codec_description_to_tag_list (dec->priv->taglist,
891 GST_TAG_AUDIO_CODEC, caps);
892 dec->priv->taglist_changed = TRUE;
895 if (klass->set_format)
896 res = klass->set_format (dec, caps);
899 gst_caps_replace (&dec->priv->ctx.input_caps, caps);
902 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
908 gst_audio_decoder_setup (GstAudioDecoder * dec)
913 /* check if in live pipeline, then latency messing is no-no */
914 query = gst_query_new_latency ();
915 res = gst_pad_peer_query (dec->sinkpad, query);
917 gst_query_parse_latency (query, &res, NULL, NULL);
920 gst_query_unref (query);
922 /* normalize to bool */
923 dec->priv->agg = ! !res;
927 gst_audio_decoder_push_forward (GstAudioDecoder * dec, GstBuffer * buf)
929 GstAudioDecoderClass *klass;
930 GstAudioDecoderPrivate *priv;
931 GstAudioDecoderContext *ctx;
932 GstFlowReturn ret = GST_FLOW_OK;
935 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
937 ctx = &dec->priv->ctx;
939 g_return_val_if_fail (ctx->info.bpf != 0, GST_FLOW_ERROR);
941 if (G_UNLIKELY (!buf)) {
942 g_assert_not_reached ();
946 ctx->had_output_data = TRUE;
947 ts = GST_BUFFER_TIMESTAMP (buf);
950 "clipping buffer of size %" G_GSIZE_FORMAT " with ts %" GST_TIME_FORMAT
951 ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buf),
952 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
953 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
956 buf = gst_audio_buffer_clip (buf, &dec->output_segment, ctx->info.rate,
958 if (G_UNLIKELY (!buf)) {
959 GST_DEBUG_OBJECT (dec, "no data after clipping to segment");
960 /* only check and return EOS if upstream still
961 * in the same segment and interested as such */
962 if (dec->priv->in_out_segment_sync) {
963 if (dec->output_segment.rate >= 0) {
964 if (ts >= dec->output_segment.stop)
966 } else if (ts < dec->output_segment.start) {
974 if (G_UNLIKELY (priv->discont)) {
975 GST_LOG_OBJECT (dec, "marking discont");
976 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
977 priv->discont = FALSE;
980 /* track where we are */
981 if (G_LIKELY (GST_BUFFER_TIMESTAMP_IS_VALID (buf))) {
982 /* duration should always be valid for raw audio */
983 g_assert (GST_BUFFER_DURATION_IS_VALID (buf));
984 dec->output_segment.position =
985 GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
988 if (klass->pre_push) {
989 /* last chance for subclass to do some dirty stuff */
990 ret = klass->pre_push (dec, &buf);
991 if (ret != GST_FLOW_OK || !buf) {
992 GST_DEBUG_OBJECT (dec, "subclass returned %s, buf %p",
993 gst_flow_get_name (ret), buf);
995 gst_buffer_unref (buf);
1000 GST_LOG_OBJECT (dec,
1001 "pushing buffer of size %" G_GSIZE_FORMAT " with ts %" GST_TIME_FORMAT
1002 ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buf),
1003 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1004 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1006 ret = gst_pad_push (dec->srcpad, buf);
1012 /* mini aggregator combining output buffers into fewer larger ones,
1013 * if so allowed/configured */
1014 static GstFlowReturn
1015 gst_audio_decoder_output (GstAudioDecoder * dec, GstBuffer * buf)
1017 GstAudioDecoderPrivate *priv;
1018 GstFlowReturn ret = GST_FLOW_OK;
1019 GstBuffer *inbuf = NULL;
1023 if (G_UNLIKELY (priv->agg < 0))
1024 gst_audio_decoder_setup (dec);
1026 if (G_LIKELY (buf)) {
1027 GST_LOG_OBJECT (dec,
1028 "output buffer of size %" G_GSIZE_FORMAT " with ts %" GST_TIME_FORMAT
1029 ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buf),
1030 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1031 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1036 if (priv->agg && dec->priv->latency > 0 &&
1037 priv->ctx.info.layout == GST_AUDIO_LAYOUT_INTERLEAVED) {
1039 gboolean assemble = FALSE;
1040 const GstClockTimeDiff tol = 10 * GST_MSECOND;
1041 GstClockTimeDiff diff = -100 * GST_MSECOND;
1043 av = gst_adapter_available (priv->adapter_out);
1044 if (G_UNLIKELY (!buf)) {
1045 /* forcibly send current */
1047 GST_LOG_OBJECT (dec, "forcing fragment flush");
1048 } else if (av && (!GST_BUFFER_TIMESTAMP_IS_VALID (buf) ||
1049 !GST_CLOCK_TIME_IS_VALID (priv->out_ts) ||
1050 ((diff = GST_CLOCK_DIFF (GST_BUFFER_TIMESTAMP (buf),
1051 priv->out_ts + priv->out_dur)) > tol) || diff < -tol)) {
1053 GST_LOG_OBJECT (dec, "buffer %d ms apart from current fragment",
1054 (gint) (diff / GST_MSECOND));
1056 /* add or start collecting */
1058 GST_LOG_OBJECT (dec, "starting new fragment");
1059 priv->out_ts = GST_BUFFER_TIMESTAMP (buf);
1061 GST_LOG_OBJECT (dec, "adding to fragment");
1063 gst_adapter_push (priv->adapter_out, buf);
1064 priv->out_dur += GST_BUFFER_DURATION (buf);
1065 av += gst_buffer_get_size (buf);
1068 if (priv->out_dur > dec->priv->latency)
1070 if (av && assemble) {
1071 GST_LOG_OBJECT (dec, "assembling fragment");
1073 buf = gst_adapter_take_buffer (priv->adapter_out, av);
1074 GST_BUFFER_TIMESTAMP (buf) = priv->out_ts;
1075 GST_BUFFER_DURATION (buf) = priv->out_dur;
1076 priv->out_ts = GST_CLOCK_TIME_NONE;
1081 if (G_LIKELY (buf)) {
1082 if (dec->output_segment.rate > 0.0) {
1083 ret = gst_audio_decoder_push_forward (dec, buf);
1084 GST_LOG_OBJECT (dec, "buffer pushed: %s", gst_flow_get_name (ret));
1087 priv->queued = g_list_prepend (priv->queued, buf);
1088 GST_LOG_OBJECT (dec, "buffer queued");
1101 send_pending_events (GstAudioDecoder * dec)
1103 GstAudioDecoderPrivate *priv = dec->priv;
1104 GList *pending_events, *l;
1106 pending_events = priv->pending_events;
1107 priv->pending_events = NULL;
1109 GST_DEBUG_OBJECT (dec, "Pushing pending events");
1110 for (l = pending_events; l; l = l->next)
1111 gst_audio_decoder_push_event (dec, l->data);
1112 g_list_free (pending_events);
1115 /* Iterate the list of pending events, and ensure
1116 * the current output segment is up to date for
1119 apply_pending_events (GstAudioDecoder * dec)
1121 GstAudioDecoderPrivate *priv = dec->priv;
1124 GST_DEBUG_OBJECT (dec, "Applying pending segments");
1125 for (l = priv->pending_events; l; l = l->next) {
1126 GstEvent *event = GST_EVENT (l->data);
1127 switch (GST_EVENT_TYPE (event)) {
1128 case GST_EVENT_SEGMENT:{
1131 GST_AUDIO_DECODER_STREAM_LOCK (dec);
1132 gst_event_copy_segment (event, &seg);
1134 GST_DEBUG_OBJECT (dec, "starting segment %" GST_SEGMENT_FORMAT, &seg);
1136 dec->output_segment = seg;
1137 dec->priv->in_out_segment_sync =
1138 gst_segment_is_equal (&dec->input_segment, &seg);
1139 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
1148 static GstFlowReturn
1149 check_pending_reconfigure (GstAudioDecoder * dec)
1151 GstFlowReturn ret = GST_FLOW_OK;
1152 GstAudioDecoderContext *ctx;
1153 gboolean needs_reconfigure;
1155 ctx = &dec->priv->ctx;
1157 needs_reconfigure = gst_pad_check_reconfigure (dec->srcpad);
1158 if (G_UNLIKELY (ctx->output_format_changed ||
1159 (GST_AUDIO_INFO_IS_VALID (&ctx->info)
1160 && needs_reconfigure))) {
1161 if (!gst_audio_decoder_negotiate_unlocked (dec)) {
1162 gst_pad_mark_reconfigure (dec->srcpad);
1163 if (GST_PAD_IS_FLUSHING (dec->srcpad))
1164 ret = GST_FLOW_FLUSHING;
1166 ret = GST_FLOW_NOT_NEGOTIATED;
1173 gst_audio_decoder_transform_meta_default (GstAudioDecoder *
1174 decoder, GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf)
1176 const GstMetaInfo *info = meta->info;
1177 const gchar *const *tags;
1179 tags = gst_meta_api_type_get_tags (info->api);
1181 if (!tags || (g_strv_length ((gchar **) tags) == 1
1182 && gst_meta_api_type_has_tag (info->api,
1183 g_quark_from_string (GST_META_TAG_AUDIO_STR))))
1191 GstAudioDecoder *decoder;
1196 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
1198 CopyMetaData *data = user_data;
1199 GstAudioDecoder *decoder = data->decoder;
1200 GstAudioDecoderClass *klass = GST_AUDIO_DECODER_GET_CLASS (decoder);
1201 GstBuffer *outbuf = data->outbuf;
1202 const GstMetaInfo *info = (*meta)->info;
1203 gboolean do_copy = FALSE;
1205 if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
1206 /* never call the transform_meta with memory specific metadata */
1207 GST_DEBUG_OBJECT (decoder, "not copying memory specific metadata %s",
1208 g_type_name (info->api));
1210 } else if (klass->transform_meta) {
1211 do_copy = klass->transform_meta (decoder, outbuf, *meta, inbuf);
1212 GST_DEBUG_OBJECT (decoder, "transformed metadata %s: copy: %d",
1213 g_type_name (info->api), do_copy);
1216 /* we only copy metadata when the subclass implemented a transform_meta
1217 * function and when it returns %TRUE */
1218 if (do_copy && info->transform_func) {
1219 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
1220 GST_DEBUG_OBJECT (decoder, "copy metadata %s", g_type_name (info->api));
1221 /* simply copy then */
1222 info->transform_func (outbuf, *meta, inbuf,
1223 _gst_meta_transform_copy, ©_data);
1229 * gst_audio_decoder_finish_frame:
1230 * @dec: a #GstAudioDecoder
1231 * @buf: decoded data
1232 * @frames: number of decoded frames represented by decoded data
1234 * Collects decoded data and pushes it downstream.
1236 * @buf may be NULL in which case the indicated number of frames
1237 * are discarded and considered to have produced no output
1238 * (e.g. lead-in or setup frames).
1239 * Otherwise, source pad caps must be set when it is called with valid
1242 * Note that a frame received in #GstAudioDecoderClass.handle_frame() may be
1243 * invalidated by a call to this function.
1245 * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
1248 gst_audio_decoder_finish_frame (GstAudioDecoder * dec, GstBuffer * buf,
1251 GstAudioDecoderPrivate *priv;
1252 GstAudioDecoderContext *ctx;
1253 GstAudioDecoderClass *klass = GST_AUDIO_DECODER_GET_CLASS (dec);
1255 GstClockTime ts, next_ts;
1256 gsize size, samples = 0;
1257 GstFlowReturn ret = GST_FLOW_OK;
1258 GQueue inbufs = G_QUEUE_INIT;
1260 /* subclass should not hand us no data */
1261 g_return_val_if_fail (buf == NULL || gst_buffer_get_size (buf) > 0,
1263 /* no dummy calls please */
1264 g_return_val_if_fail (frames != 0, GST_FLOW_ERROR);
1267 ctx = &dec->priv->ctx;
1268 meta = buf ? gst_buffer_get_audio_meta (buf) : NULL;
1269 size = buf ? gst_buffer_get_size (buf) : 0;
1270 samples = buf ? (meta ? meta->samples : size / ctx->info.bpf) : 0;
1272 /* must know the output format by now */
1273 g_return_val_if_fail (buf == NULL || GST_AUDIO_INFO_IS_VALID (&ctx->info),
1276 GST_LOG_OBJECT (dec,
1277 "accepting %" G_GSIZE_FORMAT " bytes == %" G_GSIZE_FORMAT
1278 " samples for %d frames", buf ? size : 0, samples, frames);
1280 GST_AUDIO_DECODER_STREAM_LOCK (dec);
1283 ret = check_pending_reconfigure (dec);
1284 if (ret == GST_FLOW_FLUSHING || ret == GST_FLOW_NOT_NEGOTIATED) {
1285 gst_buffer_unref (buf);
1289 if (priv->pending_events)
1290 send_pending_events (dec);
1293 /* sanity checking */
1294 if (G_LIKELY (buf && ctx->info.bpf)) {
1295 if (!meta || meta->info.layout == GST_AUDIO_LAYOUT_INTERLEAVED) {
1296 /* output shoud be whole number of sample frames */
1297 if (size % ctx->info.bpf)
1299 /* output should have no additional padding */
1300 if (samples != size / ctx->info.bpf)
1303 /* can't have more samples than what the buffer fits */
1304 if (samples > size / ctx->info.bpf)
1309 /* frame and ts book-keeping */
1310 if (G_UNLIKELY (frames < 0)) {
1311 if (G_UNLIKELY (-frames - 1 > priv->frames.length)) {
1312 GST_ELEMENT_WARNING (dec, STREAM, DECODE,
1313 ("received more decoded frames %d than provided %d", frames,
1314 priv->frames.length), (NULL));
1317 frames = priv->frames.length + frames + 1;
1319 } else if (G_UNLIKELY (frames > priv->frames.length)) {
1320 if (G_LIKELY (!priv->force)) {
1321 GST_ELEMENT_WARNING (dec, STREAM, DECODE,
1322 ("received more decoded frames %d than provided %d", frames,
1323 priv->frames.length), (NULL));
1325 frames = priv->frames.length;
1328 if (G_LIKELY (priv->frames.length))
1329 ts = GST_BUFFER_TIMESTAMP (priv->frames.head->data);
1331 ts = GST_CLOCK_TIME_NONE;
1333 GST_DEBUG_OBJECT (dec, "leading frame ts %" GST_TIME_FORMAT,
1334 GST_TIME_ARGS (ts));
1336 while (priv->frames.length && frames) {
1337 g_queue_push_tail (&inbufs, g_queue_pop_head (&priv->frames));
1338 dec->priv->ctx.delay = dec->priv->frames.length;
1342 if (G_UNLIKELY (!buf))
1346 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (priv->base_ts))) {
1348 GST_DEBUG_OBJECT (dec, "base_ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
1351 /* still no valid ts, track the segment one */
1352 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (priv->base_ts)) &&
1353 dec->output_segment.rate > 0.0) {
1354 priv->base_ts = dec->output_segment.start;
1357 /* slightly convoluted approach caters for perfect ts if subclass desires */
1358 if (GST_CLOCK_TIME_IS_VALID (ts)) {
1359 if (dec->priv->tolerance > 0) {
1360 GstClockTimeDiff diff;
1362 g_assert (GST_CLOCK_TIME_IS_VALID (priv->base_ts));
1363 next_ts = priv->base_ts +
1364 gst_util_uint64_scale (priv->samples, GST_SECOND, ctx->info.rate);
1365 GST_LOG_OBJECT (dec,
1366 "buffer is %" G_GUINT64_FORMAT " samples past base_ts %"
1367 GST_TIME_FORMAT ", expected ts %" GST_TIME_FORMAT, priv->samples,
1368 GST_TIME_ARGS (priv->base_ts), GST_TIME_ARGS (next_ts));
1369 diff = GST_CLOCK_DIFF (next_ts, ts);
1370 GST_LOG_OBJECT (dec, "ts diff %d ms", (gint) (diff / GST_MSECOND));
1371 /* if within tolerance,
1372 * discard buffer ts and carry on producing perfect stream,
1373 * otherwise resync to ts */
1374 if (G_UNLIKELY (diff < (gint64) - dec->priv->tolerance ||
1375 diff > (gint64) dec->priv->tolerance)) {
1376 GST_DEBUG_OBJECT (dec, "base_ts resync");
1381 GST_DEBUG_OBJECT (dec, "base_ts resync");
1387 /* delayed one-shot stuff until confirmed data */
1388 if (priv->taglist && priv->taglist_changed) {
1389 GstEvent *tags_event;
1391 tags_event = gst_audio_decoder_create_merged_tags_event (dec);
1393 if (tags_event != NULL)
1394 gst_audio_decoder_push_event (dec, tags_event);
1396 priv->taglist_changed = FALSE;
1399 buf = gst_buffer_make_writable (buf);
1400 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (priv->base_ts))) {
1401 GST_BUFFER_TIMESTAMP (buf) =
1403 GST_FRAMES_TO_CLOCK_TIME (priv->samples, ctx->info.rate);
1404 GST_BUFFER_DURATION (buf) = priv->base_ts +
1405 GST_FRAMES_TO_CLOCK_TIME (priv->samples + samples, ctx->info.rate) -
1406 GST_BUFFER_TIMESTAMP (buf);
1408 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
1409 GST_BUFFER_DURATION (buf) =
1410 GST_FRAMES_TO_CLOCK_TIME (samples, ctx->info.rate);
1413 if (klass->transform_meta) {
1414 if (inbufs.length) {
1416 for (l = inbufs.head; l; l = l->next) {
1421 gst_buffer_foreach_meta (l->data, foreach_metadata, &data);
1424 GST_WARNING_OBJECT (dec,
1425 "Can't copy metadata because input buffers disappeared");
1429 GST_OBJECT_LOCK (dec);
1430 priv->samples += samples;
1431 priv->samples_out += samples;
1432 GST_OBJECT_UNLOCK (dec);
1434 /* we got data, so note things are looking up */
1435 if (G_UNLIKELY (dec->priv->error_count))
1436 dec->priv->error_count = 0;
1438 ret = gst_audio_decoder_output (dec, buf);
1441 g_queue_foreach (&inbufs, (GFunc) gst_buffer_unref, NULL);
1442 g_queue_clear (&inbufs);
1444 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
1451 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
1452 ("buffer size %" G_GSIZE_FORMAT " not a multiple of %d", size,
1454 gst_buffer_unref (buf);
1455 ret = GST_FLOW_ERROR;
1460 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
1461 ("GstAudioMeta samples (%" G_GSIZE_FORMAT ") are inconsistent with "
1462 "the buffer size and layout (size/bpf = %" G_GSIZE_FORMAT ")",
1463 meta->samples, size / ctx->info.bpf));
1464 gst_buffer_unref (buf);
1465 ret = GST_FLOW_ERROR;
1470 static GstFlowReturn
1471 gst_audio_decoder_handle_frame (GstAudioDecoder * dec,
1472 GstAudioDecoderClass * klass, GstBuffer * buffer)
1474 /* Skip decoding and send a GAP instead if
1475 * GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO is set and we have timestamps
1476 * FIXME: We only do this for forward playback atm, because reverse
1477 * playback would require accumulating GAP events and pushing them
1478 * out in reverse order as for normal audio samples
1480 if (G_UNLIKELY (dec->input_segment.rate > 0.0
1481 && dec->input_segment.flags & GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO)) {
1483 GstClockTime ts = GST_BUFFER_PTS (buffer);
1484 if (GST_CLOCK_TIME_IS_VALID (ts)) {
1485 GstEvent *event = gst_event_new_gap (ts, GST_BUFFER_DURATION (buffer));
1487 gst_buffer_unref (buffer);
1488 GST_LOG_OBJECT (dec, "Skipping decode in trickmode and sending gap");
1489 gst_audio_decoder_handle_gap (dec, event);
1495 if (G_LIKELY (buffer)) {
1496 gsize size = gst_buffer_get_size (buffer);
1497 /* keep around for admin */
1498 GST_LOG_OBJECT (dec,
1499 "tracking frame size %" G_GSIZE_FORMAT ", ts %" GST_TIME_FORMAT, size,
1500 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
1501 g_queue_push_tail (&dec->priv->frames, buffer);
1502 dec->priv->ctx.delay = dec->priv->frames.length;
1503 GST_OBJECT_LOCK (dec);
1504 dec->priv->bytes_in += size;
1505 GST_OBJECT_UNLOCK (dec);
1507 GST_LOG_OBJECT (dec, "providing subclass with NULL frame");
1510 return klass->handle_frame (dec, buffer);
1513 /* maybe subclass configurable instead, but this allows for a whole lot of
1514 * raw samples, so at least quite some encoded ... */
1515 #define GST_AUDIO_DECODER_MAX_SYNC 10 * 8 * 2 * 1024
1517 static GstFlowReturn
1518 gst_audio_decoder_push_buffers (GstAudioDecoder * dec, gboolean force)
1520 GstAudioDecoderClass *klass;
1521 GstAudioDecoderPrivate *priv;
1522 GstAudioDecoderContext *ctx;
1523 GstFlowReturn ret = GST_FLOW_OK;
1527 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
1529 ctx = &dec->priv->ctx;
1531 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1533 av = gst_adapter_available (priv->adapter);
1534 GST_DEBUG_OBJECT (dec, "available: %d", av);
1536 while (ret == GST_FLOW_OK) {
1541 if (G_LIKELY (av)) {
1546 /* parse if needed */
1550 /* limited (legacy) parsing; avoid whole of baseparse */
1551 GST_DEBUG_OBJECT (dec, "parsing available: %d", av);
1552 /* piggyback sync state on discont */
1553 ctx->sync = !priv->discont;
1554 ret = klass->parse (dec, priv->adapter, &offset, &len);
1556 g_assert (offset <= av);
1559 GST_DEBUG_OBJECT (dec, "skipped %d; setting DISCONT", offset);
1560 gst_adapter_flush (priv->adapter, offset);
1562 /* avoid parsing indefinitely */
1563 priv->sync_flush += offset;
1564 if (priv->sync_flush > GST_AUDIO_DECODER_MAX_SYNC)
1568 if (ret == GST_FLOW_EOS) {
1569 GST_LOG_OBJECT (dec, "no frame yet");
1572 } else if (ret == GST_FLOW_OK) {
1573 GST_LOG_OBJECT (dec, "frame at offset %d of length %d", offset, len);
1575 g_assert (offset + len <= av);
1576 priv->sync_flush = 0;
1583 /* track upstream ts, but do not get stuck if nothing new upstream */
1584 ts = gst_adapter_prev_pts (priv->adapter, &distance);
1585 if (ts != priv->prev_ts || distance <= priv->prev_distance) {
1587 priv->prev_distance = distance;
1589 GST_LOG_OBJECT (dec, "ts == prev_ts; discarding");
1590 ts = GST_CLOCK_TIME_NONE;
1592 buffer = gst_adapter_take_buffer (priv->adapter, len);
1593 buffer = gst_buffer_make_writable (buffer);
1594 GST_BUFFER_TIMESTAMP (buffer) = ts;
1596 priv->force = FALSE;
1600 if (!priv->drainable) {
1601 priv->drained = TRUE;
1608 ret = gst_audio_decoder_handle_frame (dec, klass, buffer);
1610 /* do not keep pushing it ... */
1611 if (G_UNLIKELY (!av)) {
1612 priv->drained = TRUE;
1620 GST_LOG_OBJECT (dec, "done pushing to subclass");
1626 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("failed to parse stream"));
1627 return GST_FLOW_ERROR;
1631 static GstFlowReturn
1632 gst_audio_decoder_drain (GstAudioDecoder * dec)
1636 if (dec->priv->drained && !dec->priv->gather)
1639 /* Apply any pending events before draining, as that
1640 * may update the pending segment info */
1641 apply_pending_events (dec);
1643 /* dispatch reverse pending buffers */
1644 /* chain eventually calls upon drain as well, but by that time
1645 * gather list should be clear, so ok ... */
1646 if (dec->output_segment.rate < 0.0 && dec->priv->gather)
1647 gst_audio_decoder_chain_reverse (dec, NULL);
1648 /* have subclass give all it can */
1649 ret = gst_audio_decoder_push_buffers (dec, TRUE);
1650 if (ret != GST_FLOW_OK) {
1651 GST_WARNING_OBJECT (dec, "audio decoder push buffers failed");
1654 /* ensure all output sent */
1655 ret = gst_audio_decoder_output (dec, NULL);
1656 if (ret != GST_FLOW_OK)
1657 GST_WARNING_OBJECT (dec, "audio decoder output failed");
1660 /* everything should be away now */
1661 if (dec->priv->frames.length) {
1662 /* not fatal/impossible though if subclass/codec eats stuff */
1663 GST_WARNING_OBJECT (dec, "still %d frames left after draining",
1664 dec->priv->frames.length);
1665 g_queue_foreach (&dec->priv->frames, (GFunc) gst_buffer_unref, NULL);
1666 g_queue_clear (&dec->priv->frames);
1669 /* discard (unparsed) leftover */
1670 gst_adapter_clear (dec->priv->adapter);
1674 /* hard == FLUSH, otherwise discont */
1675 static GstFlowReturn
1676 gst_audio_decoder_flush (GstAudioDecoder * dec, gboolean hard)
1678 GstAudioDecoderClass *klass;
1679 GstFlowReturn ret = GST_FLOW_OK;
1681 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
1683 GST_LOG_OBJECT (dec, "flush hard %d", hard);
1686 ret = gst_audio_decoder_drain (dec);
1688 gst_audio_decoder_clear_queues (dec);
1689 gst_segment_init (&dec->input_segment, GST_FORMAT_TIME);
1690 gst_segment_init (&dec->output_segment, GST_FORMAT_TIME);
1691 dec->priv->error_count = 0;
1693 /* only bother subclass with flushing if known it is already alive
1694 * and kicking out stuff */
1695 if (klass->flush && dec->priv->samples_out > 0)
1696 klass->flush (dec, hard);
1697 /* and get (re)set for the sequel */
1698 gst_audio_decoder_reset (dec, FALSE);
1703 static GstFlowReturn
1704 gst_audio_decoder_chain_forward (GstAudioDecoder * dec, GstBuffer * buffer)
1706 GstFlowReturn ret = GST_FLOW_OK;
1708 /* discard silly case, though maybe ts may be of value ?? */
1709 if (G_UNLIKELY (gst_buffer_get_size (buffer) == 0)) {
1710 GST_DEBUG_OBJECT (dec, "discarding empty buffer");
1711 gst_buffer_unref (buffer);
1716 gst_adapter_push (dec->priv->adapter, buffer);
1718 /* new stuff, so we can push subclass again */
1719 dec->priv->drained = FALSE;
1721 /* hand to subclass */
1722 ret = gst_audio_decoder_push_buffers (dec, FALSE);
1725 GST_LOG_OBJECT (dec, "chain-done");
1730 gst_audio_decoder_clear_queues (GstAudioDecoder * dec)
1732 GstAudioDecoderPrivate *priv = dec->priv;
1734 g_list_foreach (priv->queued, (GFunc) gst_mini_object_unref, NULL);
1735 g_list_free (priv->queued);
1736 priv->queued = NULL;
1737 g_list_foreach (priv->gather, (GFunc) gst_mini_object_unref, NULL);
1738 g_list_free (priv->gather);
1739 priv->gather = NULL;
1740 g_list_foreach (priv->decode, (GFunc) gst_mini_object_unref, NULL);
1741 g_list_free (priv->decode);
1742 priv->decode = NULL;
1747 * Buffer decoding order: 7 8 9 4 5 6 3 1 2 EOS
1748 * Discont flag: D D D D
1750 * - Each Discont marks a discont in the decoding order.
1752 * for vorbis, each buffer is a keyframe when we have the previous
1753 * buffer. This means that to decode buffer 7, we need buffer 6, which
1754 * arrives out of order.
1756 * we first gather buffers in the gather queue until we get a DISCONT. We
1757 * prepend each incomming buffer so that they are in reversed order.
1759 * gather queue: 9 8 7
1763 * When a DISCONT is received (buffer 4), we move the gather queue to the
1764 * decode queue. This is simply done be taking the head of the gather queue
1765 * and prepending it to the decode queue. This yields:
1768 * decode queue: 7 8 9
1771 * Then we decode each buffer in the decode queue in order and put the output
1772 * buffer in the output queue. The first buffer (7) will not produce any output
1773 * because it needs the previous buffer (6) which did not arrive yet. This
1777 * decode queue: 7 8 9
1780 * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
1781 * completely consumed, we need to keep it around for when we receive buffer
1788 * Then we accumulate more buffers:
1790 * gather queue: 6 5 4
1794 * prepending to the decode queue on DISCONT yields:
1797 * decode queue: 4 5 6 7
1800 * after decoding and keeping buffer 4:
1804 * output queue: 7 6 5
1808 static GstFlowReturn
1809 gst_audio_decoder_flush_decode (GstAudioDecoder * dec)
1811 GstAudioDecoderPrivate *priv = dec->priv;
1812 GstFlowReturn res = GST_FLOW_OK;
1813 GstClockTime timestamp;
1816 walk = priv->decode;
1818 GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
1820 /* clear buffer and decoder state */
1821 gst_audio_decoder_flush (dec, FALSE);
1825 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1827 GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
1828 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1830 next = g_list_next (walk);
1831 /* decode buffer, resulting data prepended to output queue */
1832 gst_buffer_ref (buf);
1833 res = gst_audio_decoder_chain_forward (dec, buf);
1835 /* if we generated output, we can discard the buffer, else we
1836 * keep it in the queue */
1838 GST_DEBUG_OBJECT (dec, "decoded buffer to %p", priv->queued->data);
1839 priv->decode = g_list_delete_link (priv->decode, walk);
1840 gst_buffer_unref (buf);
1842 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1847 /* drain any aggregation (or otherwise) leftover */
1848 gst_audio_decoder_drain (dec);
1850 /* now send queued data downstream */
1851 timestamp = GST_CLOCK_TIME_NONE;
1852 while (priv->queued) {
1853 GstBuffer *buf = GST_BUFFER_CAST (priv->queued->data);
1854 GstClockTime duration;
1856 duration = GST_BUFFER_DURATION (buf);
1858 /* duration should always be valid for raw audio */
1859 g_assert (GST_CLOCK_TIME_IS_VALID (duration));
1861 /* interpolate (backward) if needed */
1862 if (G_LIKELY (timestamp != -1)) {
1863 if (timestamp > duration)
1864 timestamp -= duration;
1869 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1870 GST_LOG_OBJECT (dec, "applying reverse interpolated ts %"
1871 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
1872 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1874 /* track otherwise */
1875 timestamp = GST_BUFFER_TIMESTAMP (buf);
1876 GST_LOG_OBJECT (dec, "tracking ts %" GST_TIME_FORMAT,
1877 GST_TIME_ARGS (timestamp));
1880 if (G_LIKELY (res == GST_FLOW_OK)) {
1881 GST_DEBUG_OBJECT (dec, "pushing buffer %p of size %" G_GSIZE_FORMAT ", "
1882 "time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT, buf,
1883 gst_buffer_get_size (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1884 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1885 /* should be already, but let's be sure */
1886 buf = gst_buffer_make_writable (buf);
1887 /* avoid stray DISCONT from forward processing,
1888 * which have no meaning in reverse pushing */
1889 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
1890 res = gst_audio_decoder_push_forward (dec, buf);
1892 gst_buffer_unref (buf);
1895 priv->queued = g_list_delete_link (priv->queued, priv->queued);
1901 static GstFlowReturn
1902 gst_audio_decoder_chain_reverse (GstAudioDecoder * dec, GstBuffer * buf)
1904 GstAudioDecoderPrivate *priv = dec->priv;
1905 GstFlowReturn result = GST_FLOW_OK;
1907 /* if we have a discont, move buffers to the decode list */
1908 if (!buf || GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT)) {
1909 GST_DEBUG_OBJECT (dec, "received discont");
1910 while (priv->gather) {
1913 gbuf = GST_BUFFER_CAST (priv->gather->data);
1914 /* remove from the gather list */
1915 priv->gather = g_list_delete_link (priv->gather, priv->gather);
1916 /* copy to decode queue */
1917 priv->decode = g_list_prepend (priv->decode, gbuf);
1919 /* decode stuff in the decode queue */
1920 gst_audio_decoder_flush_decode (dec);
1923 if (G_LIKELY (buf)) {
1924 GST_DEBUG_OBJECT (dec, "gathering buffer %p of size %" G_GSIZE_FORMAT ", "
1925 "time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT, buf,
1926 gst_buffer_get_size (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1927 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1929 /* add buffer to gather queue */
1930 priv->gather = g_list_prepend (priv->gather, buf);
1936 static GstFlowReturn
1937 gst_audio_decoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1939 GstAudioDecoder *dec;
1942 dec = GST_AUDIO_DECODER (parent);
1944 GST_LOG_OBJECT (dec,
1945 "received buffer of size %" G_GSIZE_FORMAT " with ts %" GST_TIME_FORMAT
1946 ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buffer),
1947 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1948 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1950 GST_AUDIO_DECODER_STREAM_LOCK (dec);
1952 if (G_UNLIKELY (dec->priv->ctx.input_caps == NULL && dec->priv->needs_format))
1953 goto not_negotiated;
1955 dec->priv->ctx.had_input_data = TRUE;
1957 if (!dec->priv->expecting_discont_buf &&
1958 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
1961 /* track present position */
1962 ts = dec->priv->base_ts;
1963 samples = dec->priv->samples;
1965 GST_DEBUG_OBJECT (dec, "handling discont");
1966 gst_audio_decoder_flush (dec, FALSE);
1967 dec->priv->discont = TRUE;
1969 /* buffer may claim DISCONT loudly, if it can't tell us where we are now,
1970 * we'll stick to where we were ...
1971 * Particularly useful/needed for upstream BYTE based */
1972 if (dec->input_segment.rate > 0.0
1973 && !GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
1974 GST_DEBUG_OBJECT (dec, "... but restoring previous ts tracking");
1975 dec->priv->base_ts = ts;
1976 dec->priv->samples = samples;
1979 dec->priv->expecting_discont_buf = FALSE;
1981 if (dec->input_segment.rate > 0.0)
1982 ret = gst_audio_decoder_chain_forward (dec, buffer);
1984 ret = gst_audio_decoder_chain_reverse (dec, buffer);
1986 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
1993 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
1994 GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
1995 ("decoder not initialized"));
1996 gst_buffer_unref (buffer);
1997 return GST_FLOW_NOT_NEGOTIATED;
2001 /* perform upstream byte <-> time conversion (duration, seeking)
2002 * if subclass allows and if enough data for moderately decent conversion */
2003 static inline gboolean
2004 gst_audio_decoder_do_byte (GstAudioDecoder * dec)
2008 GST_OBJECT_LOCK (dec);
2009 ret = dec->priv->ctx.do_estimate_rate && dec->priv->ctx.info.bpf &&
2010 dec->priv->ctx.info.rate <= dec->priv->samples_out;
2011 GST_OBJECT_UNLOCK (dec);
2016 /* Must be called holding the GST_AUDIO_DECODER_STREAM_LOCK */
2018 gst_audio_decoder_negotiate_default_caps (GstAudioDecoder * dec)
2020 GstCaps *caps, *templcaps;
2024 guint64 channel_mask = 0;
2026 GstStructure *structure;
2029 templcaps = gst_pad_get_pad_template_caps (dec->srcpad);
2030 caps = gst_pad_peer_query_caps (dec->srcpad, templcaps);
2032 gst_caps_unref (templcaps);
2037 if (!caps || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
2040 GST_LOG_OBJECT (dec, "peer caps %" GST_PTR_FORMAT, caps);
2042 /* before fixating, try to use whatever upstream provided */
2043 caps = gst_caps_make_writable (caps);
2044 caps_size = gst_caps_get_size (caps);
2045 if (dec->priv->ctx.input_caps) {
2046 GstCaps *sinkcaps = dec->priv->ctx.input_caps;
2047 GstStructure *structure = gst_caps_get_structure (sinkcaps, 0);
2049 if (gst_structure_get_int (structure, "rate", &rate)) {
2050 for (i = 0; i < caps_size; i++) {
2051 gst_structure_set (gst_caps_get_structure (caps, i), "rate",
2052 G_TYPE_INT, rate, NULL);
2056 if (gst_structure_get_int (structure, "channels", &channels)) {
2057 for (i = 0; i < caps_size; i++) {
2058 gst_structure_set (gst_caps_get_structure (caps, i), "channels",
2059 G_TYPE_INT, channels, NULL);
2063 if (gst_structure_get (structure, "channel-mask", GST_TYPE_BITMASK,
2064 &channel_mask, NULL)) {
2065 for (i = 0; i < caps_size; i++) {
2066 gst_structure_set (gst_caps_get_structure (caps, i), "channel-mask",
2067 GST_TYPE_BITMASK, channel_mask, NULL);
2072 for (i = 0; i < caps_size; i++) {
2073 structure = gst_caps_get_structure (caps, i);
2074 if (gst_structure_has_field (structure, "channels"))
2075 gst_structure_fixate_field_nearest_int (structure,
2076 "channels", GST_AUDIO_DEF_CHANNELS);
2078 gst_structure_set (structure, "channels", G_TYPE_INT,
2079 GST_AUDIO_DEF_CHANNELS, NULL);
2080 if (gst_structure_has_field (structure, "rate"))
2081 gst_structure_fixate_field_nearest_int (structure,
2082 "rate", GST_AUDIO_DEF_RATE);
2084 gst_structure_set (structure, "rate", G_TYPE_INT, GST_AUDIO_DEF_RATE,
2087 caps = gst_caps_fixate (caps);
2088 structure = gst_caps_get_structure (caps, 0);
2090 /* Need to add a channel-mask if channels > 2 */
2091 gst_structure_get_int (structure, "channels", &channels);
2092 if (channels > 2 && !gst_structure_has_field (structure, "channel-mask")) {
2093 channel_mask = gst_audio_channel_get_fallback_mask (channels);
2094 if (channel_mask != 0) {
2095 gst_structure_set (structure, "channel-mask",
2096 GST_TYPE_BITMASK, channel_mask, NULL);
2098 GST_WARNING_OBJECT (dec, "No default channel-mask for %d channels",
2103 if (!caps || !gst_audio_info_from_caps (&info, caps))
2106 GST_OBJECT_LOCK (dec);
2107 dec->priv->ctx.info = info;
2108 GST_OBJECT_UNLOCK (dec);
2110 GST_INFO_OBJECT (dec,
2111 "Chose default caps %" GST_PTR_FORMAT " for initial gap", caps);
2112 gst_caps_unref (caps);
2119 gst_caps_unref (caps);
2125 gst_audio_decoder_handle_gap (GstAudioDecoder * dec, GstEvent * event)
2128 GstClockTime timestamp, duration;
2129 gboolean needs_reconfigure = FALSE;
2131 /* Ensure we have caps first */
2132 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2133 if (!GST_AUDIO_INFO_IS_VALID (&dec->priv->ctx.info)) {
2134 if (!gst_audio_decoder_negotiate_default_caps (dec)) {
2135 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2136 GST_ELEMENT_ERROR (dec, STREAM, FORMAT, (NULL),
2137 ("Decoder output not negotiated before GAP event."));
2140 needs_reconfigure = TRUE;
2142 needs_reconfigure = gst_pad_check_reconfigure (dec->srcpad)
2143 || needs_reconfigure;
2144 if (G_UNLIKELY (dec->priv->ctx.output_format_changed || needs_reconfigure)) {
2145 if (!gst_audio_decoder_negotiate_unlocked (dec)) {
2146 GST_WARNING_OBJECT (dec, "Failed to negotiate with downstream");
2147 gst_pad_mark_reconfigure (dec->srcpad);
2150 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2152 gst_event_parse_gap (event, ×tamp, &duration);
2154 /* time progressed without data, see if we can fill the gap with
2155 * some concealment data */
2156 GST_DEBUG_OBJECT (dec,
2157 "gap event: plc %d, do_plc %d, position %" GST_TIME_FORMAT
2158 " duration %" GST_TIME_FORMAT,
2159 dec->priv->plc, dec->priv->ctx.do_plc,
2160 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
2162 if (dec->priv->plc && dec->priv->ctx.do_plc && dec->input_segment.rate > 0.0) {
2163 GstAudioDecoderClass *klass = GST_AUDIO_DECODER_GET_CLASS (dec);
2166 /* hand subclass empty frame with duration that needs covering */
2167 buf = gst_buffer_new ();
2168 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2169 GST_BUFFER_DURATION (buf) = duration;
2170 /* best effort, not much error handling */
2171 gst_audio_decoder_handle_frame (dec, klass, buf);
2173 dec->priv->expecting_discont_buf = TRUE;
2174 gst_event_unref (event);
2176 GstFlowReturn flowret;
2178 /* sub-class doesn't know how to handle empty buffers,
2179 * so just try sending GAP downstream */
2180 flowret = check_pending_reconfigure (dec);
2181 if (flowret == GST_FLOW_OK) {
2182 send_pending_events (dec);
2183 ret = gst_audio_decoder_push_event (dec, event);
2192 _flush_events (GstPad * pad, GList * events)
2196 for (tmp = events; tmp; tmp = tmp->next) {
2197 if (GST_EVENT_TYPE (tmp->data) != GST_EVENT_EOS &&
2198 GST_EVENT_TYPE (tmp->data) != GST_EVENT_SEGMENT &&
2199 GST_EVENT_IS_STICKY (tmp->data)) {
2200 gst_pad_store_sticky_event (pad, GST_EVENT_CAST (tmp->data));
2202 gst_event_unref (tmp->data);
2204 g_list_free (events);
2210 gst_audio_decoder_sink_eventfunc (GstAudioDecoder * dec, GstEvent * event)
2214 switch (GST_EVENT_TYPE (event)) {
2215 case GST_EVENT_STREAM_START:
2216 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2217 /* finish any data in current segment and clear the decoder
2218 * to be ready for new stream data */
2219 gst_audio_decoder_drain (dec);
2220 gst_audio_decoder_flush (dec, FALSE);
2222 GST_DEBUG_OBJECT (dec, "received STREAM_START. Clearing taglist");
2223 /* Flush upstream tags after a STREAM_START */
2224 if (dec->priv->upstream_tags) {
2225 gst_tag_list_unref (dec->priv->upstream_tags);
2226 dec->priv->upstream_tags = NULL;
2227 dec->priv->taglist_changed = TRUE;
2229 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2231 ret = gst_audio_decoder_push_event (dec, event);
2233 case GST_EVENT_SEGMENT:
2238 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2239 gst_event_copy_segment (event, &seg);
2241 format = seg.format;
2242 if (format == GST_FORMAT_TIME) {
2243 GST_DEBUG_OBJECT (dec, "received TIME SEGMENT %" GST_SEGMENT_FORMAT,
2247 GST_DEBUG_OBJECT (dec, "received SEGMENT %" GST_SEGMENT_FORMAT, &seg);
2248 /* handle newsegment resulting from legacy simple seeking */
2249 /* note that we need to convert this whether or not enough data
2250 * to handle initial newsegment */
2251 if (dec->priv->ctx.do_estimate_rate &&
2252 gst_pad_query_convert (dec->sinkpad, GST_FORMAT_BYTES, seg.start,
2253 GST_FORMAT_TIME, &nstart)) {
2254 /* best attempt convert */
2255 /* as these are only estimates, stop is kept open-ended to avoid
2256 * premature cutting */
2257 GST_DEBUG_OBJECT (dec, "converted to TIME start %" GST_TIME_FORMAT,
2258 GST_TIME_ARGS (nstart));
2259 seg.format = GST_FORMAT_TIME;
2262 seg.stop = GST_CLOCK_TIME_NONE;
2264 gst_event_unref (event);
2265 event = gst_event_new_segment (&seg);
2267 GST_DEBUG_OBJECT (dec, "unsupported format; ignoring");
2268 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2269 gst_event_unref (event);
2275 /* prepare for next segment */
2276 /* Use the segment start as a base timestamp
2277 * in case upstream does not come up with anything better
2278 * (e.g. upstream BYTE) */
2279 if (format != GST_FORMAT_TIME) {
2280 dec->priv->base_ts = seg.start;
2281 dec->priv->samples = 0;
2284 /* and follow along with segment */
2285 dec->priv->in_out_segment_sync = FALSE;
2286 dec->input_segment = seg;
2287 dec->priv->pending_events =
2288 g_list_append (dec->priv->pending_events, event);
2289 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2295 ret = gst_audio_decoder_handle_gap (dec, event);
2297 case GST_EVENT_FLUSH_STOP:
2298 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2299 /* prepare for fresh start */
2300 gst_audio_decoder_flush (dec, TRUE);
2302 dec->priv->pending_events = _flush_events (dec->srcpad,
2303 dec->priv->pending_events);
2304 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2306 /* Forward FLUSH_STOP, it is expected to be forwarded immediately
2307 * and no buffers are queued anyway. */
2308 ret = gst_audio_decoder_push_event (dec, event);
2311 case GST_EVENT_SEGMENT_DONE:
2312 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2313 gst_audio_decoder_drain (dec);
2314 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2316 /* Forward SEGMENT_DONE because no buffer or serialized event might come after
2317 * SEGMENT_DONE and nothing could trigger another _finish_frame() call. */
2318 if (dec->priv->pending_events)
2319 send_pending_events (dec);
2320 ret = gst_audio_decoder_push_event (dec, event);
2324 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2325 gst_audio_decoder_drain (dec);
2326 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2328 if (dec->priv->ctx.had_input_data && !dec->priv->ctx.had_output_data) {
2329 GST_ELEMENT_ERROR (dec, STREAM, DECODE,
2330 ("No valid frames decoded before end of stream"),
2331 ("no valid frames found"));
2334 /* Forward EOS because no buffer or serialized event will come after
2335 * EOS and nothing could trigger another _finish_frame() call. */
2336 if (dec->priv->pending_events)
2337 send_pending_events (dec);
2338 ret = gst_audio_decoder_push_event (dec, event);
2341 case GST_EVENT_CAPS:
2345 gst_event_parse_caps (event, &caps);
2346 ret = gst_audio_decoder_sink_setcaps (dec, caps);
2347 gst_event_unref (event);
2354 gst_event_parse_tag (event, &tags);
2356 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
2357 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2358 if (dec->priv->upstream_tags != tags) {
2359 if (dec->priv->upstream_tags)
2360 gst_tag_list_unref (dec->priv->upstream_tags);
2361 dec->priv->upstream_tags = gst_tag_list_ref (tags);
2362 GST_INFO_OBJECT (dec, "upstream stream tags: %" GST_PTR_FORMAT, tags);
2364 gst_event_unref (event);
2365 event = gst_audio_decoder_create_merged_tags_event (dec);
2366 dec->priv->taglist_changed = FALSE;
2367 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2369 /* No tags, go out of here instead of fall through */
2379 if (!GST_EVENT_IS_SERIALIZED (event)) {
2381 gst_pad_event_default (dec->sinkpad, GST_OBJECT_CAST (dec), event);
2383 GST_DEBUG_OBJECT (dec, "Enqueuing event %d, %s", GST_EVENT_TYPE (event),
2384 GST_EVENT_TYPE_NAME (event));
2385 GST_AUDIO_DECODER_STREAM_LOCK (dec);
2386 dec->priv->pending_events =
2387 g_list_append (dec->priv->pending_events, event);
2388 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
2397 gst_audio_decoder_sink_event (GstPad * pad, GstObject * parent,
2400 GstAudioDecoder *dec;
2401 GstAudioDecoderClass *klass;
2404 dec = GST_AUDIO_DECODER (parent);
2405 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
2407 GST_DEBUG_OBJECT (dec, "received event %d, %s", GST_EVENT_TYPE (event),
2408 GST_EVENT_TYPE_NAME (event));
2410 if (klass->sink_event)
2411 ret = klass->sink_event (dec, event);
2413 gst_event_unref (event);
2420 gst_audio_decoder_do_seek (GstAudioDecoder * dec, GstEvent * event)
2423 GstSeekType start_type, end_type;
2426 gint64 start, start_time, end_time;
2427 GstSegment seek_segment;
2430 gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
2431 &start_time, &end_type, &end_time);
2433 /* we'll handle plain open-ended flushing seeks with the simple approach */
2435 GST_DEBUG_OBJECT (dec, "unsupported seek: rate");
2439 if (start_type != GST_SEEK_TYPE_SET) {
2440 GST_DEBUG_OBJECT (dec, "unsupported seek: start time");
2444 if ((end_type != GST_SEEK_TYPE_SET && end_type != GST_SEEK_TYPE_NONE) ||
2445 (end_type == GST_SEEK_TYPE_SET && end_time != GST_CLOCK_TIME_NONE)) {
2446 GST_DEBUG_OBJECT (dec, "unsupported seek: end time");
2450 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
2451 GST_DEBUG_OBJECT (dec, "unsupported seek: not flushing");
2455 memcpy (&seek_segment, &dec->output_segment, sizeof (seek_segment));
2456 gst_segment_do_seek (&seek_segment, rate, format, flags, start_type,
2457 start_time, end_type, end_time, NULL);
2458 start_time = seek_segment.position;
2460 if (!gst_pad_query_convert (dec->sinkpad, GST_FORMAT_TIME, start_time,
2461 GST_FORMAT_BYTES, &start)) {
2462 GST_DEBUG_OBJECT (dec, "conversion failed");
2466 seqnum = gst_event_get_seqnum (event);
2467 event = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags,
2468 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_NONE, -1);
2469 gst_event_set_seqnum (event, seqnum);
2471 GST_DEBUG_OBJECT (dec, "seeking to %" GST_TIME_FORMAT " at byte offset %"
2472 G_GINT64_FORMAT, GST_TIME_ARGS (start_time), start);
2474 return gst_pad_push_event (dec->sinkpad, event);
2478 gst_audio_decoder_src_eventfunc (GstAudioDecoder * dec, GstEvent * event)
2482 switch (GST_EVENT_TYPE (event)) {
2483 case GST_EVENT_SEEK:
2488 GstSeekType start_type, stop_type;
2490 gint64 tstart, tstop;
2493 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
2495 seqnum = gst_event_get_seqnum (event);
2497 /* upstream gets a chance first */
2498 if ((res = gst_pad_push_event (dec->sinkpad, event)))
2501 /* if upstream fails for a time seek, maybe we can help if allowed */
2502 if (format == GST_FORMAT_TIME) {
2503 if (gst_audio_decoder_do_byte (dec))
2504 res = gst_audio_decoder_do_seek (dec, event);
2508 /* ... though a non-time seek can be aided as well */
2509 /* First bring the requested format to time */
2511 gst_pad_query_convert (dec->srcpad, format, start,
2512 GST_FORMAT_TIME, &tstart)))
2515 gst_pad_query_convert (dec->srcpad, format, stop, GST_FORMAT_TIME,
2519 /* then seek with time on the peer */
2520 event = gst_event_new_seek (rate, GST_FORMAT_TIME,
2521 flags, start_type, tstart, stop_type, tstop);
2522 gst_event_set_seqnum (event, seqnum);
2524 res = gst_pad_push_event (dec->sinkpad, event);
2528 res = gst_pad_event_default (dec->srcpad, GST_OBJECT_CAST (dec), event);
2537 GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
2543 gst_audio_decoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2545 GstAudioDecoder *dec;
2546 GstAudioDecoderClass *klass;
2549 dec = GST_AUDIO_DECODER (parent);
2550 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
2552 GST_DEBUG_OBJECT (dec, "received event %d, %s", GST_EVENT_TYPE (event),
2553 GST_EVENT_TYPE_NAME (event));
2555 if (klass->src_event)
2556 ret = klass->src_event (dec, event);
2558 gst_event_unref (event);
2566 gst_audio_decoder_decide_allocation_default (GstAudioDecoder * dec,
2569 GstAllocator *allocator = NULL;
2570 GstAllocationParams params;
2571 gboolean update_allocator;
2573 /* we got configuration from our peer or the decide_allocation method,
2575 if (gst_query_get_n_allocation_params (query) > 0) {
2576 /* try the allocator */
2577 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2578 update_allocator = TRUE;
2581 gst_allocation_params_init (¶ms);
2582 update_allocator = FALSE;
2585 if (update_allocator)
2586 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
2588 gst_query_add_allocation_param (query, allocator, ¶ms);
2590 gst_object_unref (allocator);
2596 gst_audio_decoder_propose_allocation_default (GstAudioDecoder * dec,
2603 * gst_audio_decoder_proxy_getcaps:
2604 * @decoder: a #GstAudioDecoder
2605 * @caps: (allow-none): initial caps
2606 * @filter: (allow-none): filter caps
2608 * Returns caps that express @caps (or sink template caps if @caps == NULL)
2609 * restricted to rate/channels/... combinations supported by downstream
2612 * Returns: (transfer full): a #GstCaps owned by caller
2617 gst_audio_decoder_proxy_getcaps (GstAudioDecoder * decoder, GstCaps * caps,
2620 return __gst_audio_element_proxy_getcaps (GST_ELEMENT_CAST (decoder),
2621 GST_AUDIO_DECODER_SINK_PAD (decoder),
2622 GST_AUDIO_DECODER_SRC_PAD (decoder), caps, filter);
2626 gst_audio_decoder_sink_getcaps (GstAudioDecoder * decoder, GstCaps * filter)
2628 GstAudioDecoderClass *klass;
2631 klass = GST_AUDIO_DECODER_GET_CLASS (decoder);
2634 caps = klass->getcaps (decoder, filter);
2636 caps = gst_audio_decoder_proxy_getcaps (decoder, NULL, filter);
2638 GST_LOG_OBJECT (decoder, "Returning caps %" GST_PTR_FORMAT, caps);
2644 gst_audio_decoder_sink_query_default (GstAudioDecoder * dec, GstQuery * query)
2646 GstPad *pad = GST_AUDIO_DECODER_SINK_PAD (dec);
2647 gboolean res = FALSE;
2649 GST_LOG_OBJECT (dec, "handling query: %" GST_PTR_FORMAT, query);
2651 switch (GST_QUERY_TYPE (query)) {
2652 case GST_QUERY_FORMATS:
2654 gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
2658 case GST_QUERY_CONVERT:
2660 GstFormat src_fmt, dest_fmt;
2661 gint64 src_val, dest_val;
2663 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
2664 GST_OBJECT_LOCK (dec);
2665 res = __gst_audio_encoded_audio_convert (&dec->priv->ctx.info,
2666 dec->priv->bytes_in, dec->priv->samples_out,
2667 src_fmt, src_val, &dest_fmt, &dest_val);
2668 GST_OBJECT_UNLOCK (dec);
2671 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
2674 case GST_QUERY_ALLOCATION:
2676 GstAudioDecoderClass *klass = GST_AUDIO_DECODER_GET_CLASS (dec);
2678 if (klass->propose_allocation)
2679 res = klass->propose_allocation (dec, query);
2682 case GST_QUERY_CAPS:{
2683 GstCaps *filter, *caps;
2685 gst_query_parse_caps (query, &filter);
2686 caps = gst_audio_decoder_sink_getcaps (dec, filter);
2687 gst_query_set_caps_result (query, caps);
2688 gst_caps_unref (caps);
2692 case GST_QUERY_ACCEPT_CAPS:{
2693 if (dec->priv->use_default_pad_acceptcaps) {
2695 gst_pad_query_default (GST_AUDIO_DECODER_SINK_PAD (dec),
2696 GST_OBJECT_CAST (dec), query);
2699 GstCaps *allowed_caps;
2700 GstCaps *template_caps;
2703 gst_query_parse_accept_caps (query, &caps);
2705 template_caps = gst_pad_get_pad_template_caps (pad);
2706 accept = gst_caps_is_subset (caps, template_caps);
2707 gst_caps_unref (template_caps);
2710 allowed_caps = gst_pad_query_caps (GST_AUDIO_DECODER_SINK_PAD (dec),
2713 accept = gst_caps_can_intersect (caps, allowed_caps);
2715 gst_caps_unref (allowed_caps);
2718 gst_query_set_accept_caps_result (query, accept);
2723 case GST_QUERY_SEEKING:
2727 /* non-TIME segments are discarded, so we won't seek that way either */
2728 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2729 if (format != GST_FORMAT_TIME) {
2730 GST_DEBUG_OBJECT (dec, "discarding non-TIME SEEKING query");
2737 res = gst_pad_query_default (pad, GST_OBJECT_CAST (dec), query);
2746 gst_audio_decoder_sink_query (GstPad * pad, GstObject * parent,
2749 GstAudioDecoderClass *dec_class;
2750 GstAudioDecoder *dec;
2751 gboolean ret = FALSE;
2753 dec = GST_AUDIO_DECODER (parent);
2754 dec_class = GST_AUDIO_DECODER_GET_CLASS (dec);
2756 GST_DEBUG_OBJECT (pad, "received query %" GST_PTR_FORMAT, query);
2758 if (dec_class->sink_query)
2759 ret = dec_class->sink_query (dec, query);
2764 /* FIXME ? are any of these queries (other than latency) a decoder's business ??
2765 * also, the conversion stuff might seem to make sense, but seems to not mind
2766 * segment stuff etc at all
2767 * Supposedly that's backward compatibility ... */
2769 gst_audio_decoder_src_query_default (GstAudioDecoder * dec, GstQuery * query)
2771 GstPad *pad = GST_AUDIO_DECODER_SRC_PAD (dec);
2772 gboolean res = FALSE;
2774 GST_LOG_OBJECT (dec, "handling query: %" GST_PTR_FORMAT, query);
2776 switch (GST_QUERY_TYPE (query)) {
2777 case GST_QUERY_DURATION:
2781 /* upstream in any case */
2782 if ((res = gst_pad_query_default (pad, GST_OBJECT_CAST (dec), query)))
2785 gst_query_parse_duration (query, &format, NULL);
2786 /* try answering TIME by converting from BYTE if subclass allows */
2787 if (format == GST_FORMAT_TIME && gst_audio_decoder_do_byte (dec)) {
2790 if (gst_pad_peer_query_duration (dec->sinkpad, GST_FORMAT_BYTES,
2792 GST_LOG_OBJECT (dec, "upstream size %" G_GINT64_FORMAT, value);
2793 if (gst_pad_query_convert (dec->sinkpad, GST_FORMAT_BYTES, value,
2794 GST_FORMAT_TIME, &value)) {
2795 gst_query_set_duration (query, GST_FORMAT_TIME, value);
2802 case GST_QUERY_POSITION:
2807 if ((res = gst_pad_peer_query (dec->sinkpad, query))) {
2808 GST_LOG_OBJECT (dec, "returning peer response");
2812 /* Refuse BYTES format queries. If it made sense to
2813 * answer them, upstream would have already */
2814 gst_query_parse_position (query, &format, NULL);
2816 if (format == GST_FORMAT_BYTES) {
2817 GST_LOG_OBJECT (dec, "Ignoring BYTES position query");
2821 /* we start from the last seen time */
2822 time = dec->output_segment.position;
2823 /* correct for the segment values */
2825 gst_segment_to_stream_time (&dec->output_segment, GST_FORMAT_TIME,
2828 GST_LOG_OBJECT (dec,
2829 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
2831 /* and convert to the final format */
2832 if (!(res = gst_pad_query_convert (pad, GST_FORMAT_TIME, time,
2836 gst_query_set_position (query, format, value);
2838 GST_LOG_OBJECT (dec,
2839 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
2843 case GST_QUERY_FORMATS:
2845 gst_query_set_formats (query, 3,
2846 GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT);
2850 case GST_QUERY_CONVERT:
2852 GstFormat src_fmt, dest_fmt;
2853 gint64 src_val, dest_val;
2855 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
2856 GST_OBJECT_LOCK (dec);
2857 res = gst_audio_info_convert (&dec->priv->ctx.info,
2858 src_fmt, src_val, dest_fmt, &dest_val);
2859 GST_OBJECT_UNLOCK (dec);
2862 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
2865 case GST_QUERY_LATENCY:
2867 if ((res = gst_pad_peer_query (dec->sinkpad, query))) {
2869 GstClockTime min_latency, max_latency;
2871 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2872 GST_DEBUG_OBJECT (dec, "Peer latency: live %d, min %"
2873 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
2874 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
2876 GST_OBJECT_LOCK (dec);
2877 /* add our latency */
2878 min_latency += dec->priv->ctx.min_latency;
2879 if (max_latency == -1 || dec->priv->ctx.max_latency == -1)
2882 max_latency += dec->priv->ctx.max_latency;
2883 GST_OBJECT_UNLOCK (dec);
2885 gst_query_set_latency (query, live, min_latency, max_latency);
2890 res = gst_pad_query_default (pad, GST_OBJECT_CAST (dec), query);
2898 gst_audio_decoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2900 GstAudioDecoder *dec;
2901 GstAudioDecoderClass *dec_class;
2902 gboolean ret = FALSE;
2904 dec = GST_AUDIO_DECODER (parent);
2905 dec_class = GST_AUDIO_DECODER_GET_CLASS (dec);
2907 GST_DEBUG_OBJECT (pad, "received query %" GST_PTR_FORMAT, query);
2909 if (dec_class->src_query)
2910 ret = dec_class->src_query (dec, query);
2916 gst_audio_decoder_stop (GstAudioDecoder * dec)
2918 GstAudioDecoderClass *klass;
2919 gboolean ret = TRUE;
2921 GST_DEBUG_OBJECT (dec, "gst_audio_decoder_stop");
2923 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
2926 ret = klass->stop (dec);
2930 gst_audio_decoder_reset (dec, TRUE);
2933 dec->priv->active = FALSE;
2939 gst_audio_decoder_start (GstAudioDecoder * dec)
2941 GstAudioDecoderClass *klass;
2942 gboolean ret = TRUE;
2944 GST_DEBUG_OBJECT (dec, "gst_audio_decoder_start");
2946 klass = GST_AUDIO_DECODER_GET_CLASS (dec);
2948 /* arrange clean state */
2949 gst_audio_decoder_reset (dec, TRUE);
2952 ret = klass->start (dec);
2956 dec->priv->active = TRUE;
2962 gst_audio_decoder_get_property (GObject * object, guint prop_id,
2963 GValue * value, GParamSpec * pspec)
2965 GstAudioDecoder *dec;
2967 dec = GST_AUDIO_DECODER (object);
2971 g_value_set_int64 (value, dec->priv->latency);
2973 case PROP_TOLERANCE:
2974 g_value_set_int64 (value, dec->priv->tolerance);
2977 g_value_set_boolean (value, dec->priv->plc);
2980 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2986 gst_audio_decoder_set_property (GObject * object, guint prop_id,
2987 const GValue * value, GParamSpec * pspec)
2989 GstAudioDecoder *dec;
2991 dec = GST_AUDIO_DECODER (object);
2995 dec->priv->latency = g_value_get_int64 (value);
2997 case PROP_TOLERANCE:
2998 dec->priv->tolerance = g_value_get_int64 (value);
3001 dec->priv->plc = g_value_get_boolean (value);
3004 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3009 static GstStateChangeReturn
3010 gst_audio_decoder_change_state (GstElement * element, GstStateChange transition)
3012 GstAudioDecoder *codec;
3013 GstAudioDecoderClass *klass;
3014 GstStateChangeReturn ret;
3016 codec = GST_AUDIO_DECODER (element);
3017 klass = GST_AUDIO_DECODER_GET_CLASS (codec);
3019 switch (transition) {
3020 case GST_STATE_CHANGE_NULL_TO_READY:
3022 if (!klass->open (codec))
3026 case GST_STATE_CHANGE_READY_TO_PAUSED:
3027 if (!gst_audio_decoder_start (codec)) {
3031 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3037 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3039 switch (transition) {
3040 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3042 case GST_STATE_CHANGE_PAUSED_TO_READY:
3043 if (!gst_audio_decoder_stop (codec)) {
3047 case GST_STATE_CHANGE_READY_TO_NULL:
3049 if (!klass->close (codec))
3061 GST_ELEMENT_ERROR (codec, LIBRARY, INIT, (NULL), ("Failed to start codec"));
3062 return GST_STATE_CHANGE_FAILURE;
3066 GST_ELEMENT_ERROR (codec, LIBRARY, INIT, (NULL), ("Failed to stop codec"));
3067 return GST_STATE_CHANGE_FAILURE;
3071 GST_ELEMENT_ERROR (codec, LIBRARY, INIT, (NULL), ("Failed to open codec"));
3072 return GST_STATE_CHANGE_FAILURE;
3076 GST_ELEMENT_ERROR (codec, LIBRARY, INIT, (NULL), ("Failed to close codec"));
3077 return GST_STATE_CHANGE_FAILURE;
3082 _gst_audio_decoder_error (GstAudioDecoder * dec, gint weight,
3083 GQuark domain, gint code, gchar * txt, gchar * dbg, const gchar * file,
3084 const gchar * function, gint line)
3087 GST_WARNING_OBJECT (dec, "error: %s", txt);
3089 GST_WARNING_OBJECT (dec, "error: %s", dbg);
3090 dec->priv->error_count += weight;
3091 dec->priv->discont = TRUE;
3092 if (dec->priv->ctx.max_errors >= 0
3093 && dec->priv->ctx.max_errors < dec->priv->error_count) {
3094 gst_element_message_full (GST_ELEMENT (dec), GST_MESSAGE_ERROR, domain,
3095 code, txt, dbg, file, function, line);
3096 return GST_FLOW_ERROR;
3105 * gst_audio_decoder_get_audio_info:
3106 * @dec: a #GstAudioDecoder
3108 * Returns: a #GstAudioInfo describing the input audio format
3111 gst_audio_decoder_get_audio_info (GstAudioDecoder * dec)
3113 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), NULL);
3115 return &dec->priv->ctx.info;
3119 * gst_audio_decoder_set_plc_aware:
3120 * @dec: a #GstAudioDecoder
3121 * @plc: new plc state
3123 * Indicates whether or not subclass handles packet loss concealment (plc).
3126 gst_audio_decoder_set_plc_aware (GstAudioDecoder * dec, gboolean plc)
3128 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3130 dec->priv->ctx.do_plc = plc;
3134 * gst_audio_decoder_get_plc_aware:
3135 * @dec: a #GstAudioDecoder
3137 * Returns: currently configured plc handling
3140 gst_audio_decoder_get_plc_aware (GstAudioDecoder * dec)
3142 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), 0);
3144 return dec->priv->ctx.do_plc;
3148 * gst_audio_decoder_set_estimate_rate:
3149 * @dec: a #GstAudioDecoder
3150 * @enabled: whether to enable byte to time conversion
3152 * Allows baseclass to perform byte to time estimated conversion.
3155 gst_audio_decoder_set_estimate_rate (GstAudioDecoder * dec, gboolean enabled)
3157 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3159 dec->priv->ctx.do_estimate_rate = enabled;
3163 * gst_audio_decoder_get_estimate_rate:
3164 * @dec: a #GstAudioDecoder
3166 * Returns: currently configured byte to time conversion setting
3169 gst_audio_decoder_get_estimate_rate (GstAudioDecoder * dec)
3171 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), 0);
3173 return dec->priv->ctx.do_estimate_rate;
3177 * gst_audio_decoder_get_delay:
3178 * @dec: a #GstAudioDecoder
3180 * Returns: currently configured decoder delay
3183 gst_audio_decoder_get_delay (GstAudioDecoder * dec)
3185 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), 0);
3187 return dec->priv->ctx.delay;
3191 * gst_audio_decoder_set_max_errors:
3192 * @dec: a #GstAudioDecoder
3193 * @num: max tolerated errors
3195 * Sets numbers of tolerated decoder errors, where a tolerated one is then only
3196 * warned about, but more than tolerated will lead to fatal error. You can set
3197 * -1 for never returning fatal errors. Default is set to
3198 * GST_AUDIO_DECODER_MAX_ERRORS.
3201 gst_audio_decoder_set_max_errors (GstAudioDecoder * dec, gint num)
3203 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3205 dec->priv->ctx.max_errors = num;
3209 * gst_audio_decoder_get_max_errors:
3210 * @dec: a #GstAudioDecoder
3212 * Returns: currently configured decoder tolerated error count.
3215 gst_audio_decoder_get_max_errors (GstAudioDecoder * dec)
3217 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), 0);
3219 return dec->priv->ctx.max_errors;
3223 * gst_audio_decoder_set_latency:
3224 * @dec: a #GstAudioDecoder
3225 * @min: minimum latency
3226 * @max: maximum latency
3228 * Sets decoder latency.
3231 gst_audio_decoder_set_latency (GstAudioDecoder * dec,
3232 GstClockTime min, GstClockTime max)
3234 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3235 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min));
3236 g_return_if_fail (min <= max);
3238 GST_OBJECT_LOCK (dec);
3239 dec->priv->ctx.min_latency = min;
3240 dec->priv->ctx.max_latency = max;
3241 GST_OBJECT_UNLOCK (dec);
3243 /* post latency message on the bus */
3244 gst_element_post_message (GST_ELEMENT (dec),
3245 gst_message_new_latency (GST_OBJECT (dec)));
3249 * gst_audio_decoder_get_latency:
3250 * @dec: a #GstAudioDecoder
3251 * @min: (out) (allow-none): a pointer to storage to hold minimum latency
3252 * @max: (out) (allow-none): a pointer to storage to hold maximum latency
3254 * Sets the variables pointed to by @min and @max to the currently configured
3258 gst_audio_decoder_get_latency (GstAudioDecoder * dec,
3259 GstClockTime * min, GstClockTime * max)
3261 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3263 GST_OBJECT_LOCK (dec);
3265 *min = dec->priv->ctx.min_latency;
3267 *max = dec->priv->ctx.max_latency;
3268 GST_OBJECT_UNLOCK (dec);
3272 * gst_audio_decoder_get_parse_state:
3273 * @dec: a #GstAudioDecoder
3274 * @sync: (out) (optional): a pointer to a variable to hold the current sync state
3275 * @eos: (out) (optional): a pointer to a variable to hold the current eos state
3277 * Return current parsing (sync and eos) state.
3280 gst_audio_decoder_get_parse_state (GstAudioDecoder * dec,
3281 gboolean * sync, gboolean * eos)
3283 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3286 *sync = dec->priv->ctx.sync;
3288 *eos = dec->priv->ctx.eos;
3292 * gst_audio_decoder_set_allocation_caps:
3293 * @dec: a #GstAudioDecoder
3294 * @allocation_caps: (allow-none): a #GstCaps or %NULL
3296 * Sets a caps in allocation query which are different from the set
3297 * pad's caps. Use this function before calling
3298 * gst_audio_decoder_negotiate(). Setting to %NULL the allocation
3299 * query will use the caps from the pad.
3304 gst_audio_decoder_set_allocation_caps (GstAudioDecoder * dec,
3305 GstCaps * allocation_caps)
3307 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3309 gst_caps_replace (&dec->priv->ctx.allocation_caps, allocation_caps);
3313 * gst_audio_decoder_set_plc:
3314 * @dec: a #GstAudioDecoder
3315 * @enabled: new state
3317 * Enable or disable decoder packet loss concealment, provided subclass
3318 * and codec are capable and allow handling plc.
3323 gst_audio_decoder_set_plc (GstAudioDecoder * dec, gboolean enabled)
3325 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3327 GST_LOG_OBJECT (dec, "enabled: %d", enabled);
3329 GST_OBJECT_LOCK (dec);
3330 dec->priv->plc = enabled;
3331 GST_OBJECT_UNLOCK (dec);
3335 * gst_audio_decoder_get_plc:
3336 * @dec: a #GstAudioDecoder
3338 * Queries decoder packet loss concealment handling.
3340 * Returns: TRUE if packet loss concealment is enabled.
3345 gst_audio_decoder_get_plc (GstAudioDecoder * dec)
3349 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), FALSE);
3351 GST_OBJECT_LOCK (dec);
3352 result = dec->priv->plc;
3353 GST_OBJECT_UNLOCK (dec);
3359 * gst_audio_decoder_set_min_latency:
3360 * @dec: a #GstAudioDecoder
3361 * @num: new minimum latency
3363 * Sets decoder minimum aggregation latency.
3368 gst_audio_decoder_set_min_latency (GstAudioDecoder * dec, GstClockTime num)
3370 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3372 GST_OBJECT_LOCK (dec);
3373 dec->priv->latency = num;
3374 GST_OBJECT_UNLOCK (dec);
3378 * gst_audio_decoder_get_min_latency:
3379 * @dec: a #GstAudioDecoder
3381 * Queries decoder's latency aggregation.
3383 * Returns: aggregation latency.
3388 gst_audio_decoder_get_min_latency (GstAudioDecoder * dec)
3390 GstClockTime result;
3392 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), FALSE);
3394 GST_OBJECT_LOCK (dec);
3395 result = dec->priv->latency;
3396 GST_OBJECT_UNLOCK (dec);
3402 * gst_audio_decoder_set_tolerance:
3403 * @dec: a #GstAudioDecoder
3404 * @tolerance: new tolerance
3406 * Configures decoder audio jitter tolerance threshold.
3411 gst_audio_decoder_set_tolerance (GstAudioDecoder * dec, GstClockTime tolerance)
3413 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3415 GST_OBJECT_LOCK (dec);
3416 dec->priv->tolerance = tolerance;
3417 GST_OBJECT_UNLOCK (dec);
3421 * gst_audio_decoder_get_tolerance:
3422 * @dec: a #GstAudioDecoder
3424 * Queries current audio jitter tolerance threshold.
3426 * Returns: decoder audio jitter tolerance threshold.
3431 gst_audio_decoder_get_tolerance (GstAudioDecoder * dec)
3433 GstClockTime result;
3435 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), 0);
3437 GST_OBJECT_LOCK (dec);
3438 result = dec->priv->tolerance;
3439 GST_OBJECT_UNLOCK (dec);
3445 * gst_audio_decoder_set_drainable:
3446 * @dec: a #GstAudioDecoder
3447 * @enabled: new state
3449 * Configures decoder drain handling. If drainable, subclass might
3450 * be handed a NULL buffer to have it return any leftover decoded data.
3451 * Otherwise, it is not considered so capable and will only ever be passed
3457 gst_audio_decoder_set_drainable (GstAudioDecoder * dec, gboolean enabled)
3459 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3461 GST_OBJECT_LOCK (dec);
3462 dec->priv->drainable = enabled;
3463 GST_OBJECT_UNLOCK (dec);
3467 * gst_audio_decoder_get_drainable:
3468 * @dec: a #GstAudioDecoder
3470 * Queries decoder drain handling.
3472 * Returns: TRUE if drainable handling is enabled.
3477 gst_audio_decoder_get_drainable (GstAudioDecoder * dec)
3481 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), 0);
3483 GST_OBJECT_LOCK (dec);
3484 result = dec->priv->drainable;
3485 GST_OBJECT_UNLOCK (dec);
3491 * gst_audio_decoder_set_needs_format:
3492 * @dec: a #GstAudioDecoder
3493 * @enabled: new state
3495 * Configures decoder format needs. If enabled, subclass needs to be
3496 * negotiated with format caps before it can process any data. It will then
3497 * never be handed any data before it has been configured.
3498 * Otherwise, it might be handed data without having been configured and
3499 * is then expected being able to do so either by default
3500 * or based on the input data.
3505 gst_audio_decoder_set_needs_format (GstAudioDecoder * dec, gboolean enabled)
3507 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3509 GST_OBJECT_LOCK (dec);
3510 dec->priv->needs_format = enabled;
3511 GST_OBJECT_UNLOCK (dec);
3515 * gst_audio_decoder_get_needs_format:
3516 * @dec: a #GstAudioDecoder
3518 * Queries decoder required format handling.
3520 * Returns: TRUE if required format handling is enabled.
3525 gst_audio_decoder_get_needs_format (GstAudioDecoder * dec)
3529 g_return_val_if_fail (GST_IS_AUDIO_DECODER (dec), FALSE);
3531 GST_OBJECT_LOCK (dec);
3532 result = dec->priv->needs_format;
3533 GST_OBJECT_UNLOCK (dec);
3539 * gst_audio_decoder_merge_tags:
3540 * @dec: a #GstAudioDecoder
3541 * @tags: (allow-none): a #GstTagList to merge, or NULL
3542 * @mode: the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE
3544 * Sets the audio decoder tags and how they should be merged with any
3545 * upstream stream tags. This will override any tags previously-set
3546 * with gst_audio_decoder_merge_tags().
3548 * Note that this is provided for convenience, and the subclass is
3549 * not required to use this and can still do tag handling on its own.
3552 gst_audio_decoder_merge_tags (GstAudioDecoder * dec,
3553 const GstTagList * tags, GstTagMergeMode mode)
3555 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3556 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
3557 g_return_if_fail (mode != GST_TAG_MERGE_UNDEFINED);
3559 GST_AUDIO_DECODER_STREAM_LOCK (dec);
3560 if (dec->priv->taglist != tags) {
3561 if (dec->priv->taglist) {
3562 gst_tag_list_unref (dec->priv->taglist);
3563 dec->priv->taglist = NULL;
3564 dec->priv->decoder_tags_merge_mode = GST_TAG_MERGE_KEEP_ALL;
3567 dec->priv->taglist = gst_tag_list_ref ((GstTagList *) tags);
3568 dec->priv->decoder_tags_merge_mode = mode;
3571 GST_DEBUG_OBJECT (dec, "setting decoder tags to %" GST_PTR_FORMAT, tags);
3572 dec->priv->taglist_changed = TRUE;
3574 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
3578 * gst_audio_decoder_allocate_output_buffer:
3579 * @dec: a #GstAudioDecoder
3580 * @size: size of the buffer
3582 * Helper function that allocates a buffer to hold an audio frame
3583 * for @dec's current output format.
3585 * Returns: (transfer full): allocated buffer
3588 gst_audio_decoder_allocate_output_buffer (GstAudioDecoder * dec, gsize size)
3590 GstBuffer *buffer = NULL;
3591 gboolean needs_reconfigure = FALSE;
3593 g_return_val_if_fail (size > 0, NULL);
3595 GST_DEBUG ("alloc src buffer");
3597 GST_AUDIO_DECODER_STREAM_LOCK (dec);
3599 needs_reconfigure = gst_pad_check_reconfigure (dec->srcpad);
3600 if (G_UNLIKELY (dec->priv->ctx.output_format_changed ||
3601 (GST_AUDIO_INFO_IS_VALID (&dec->priv->ctx.info)
3602 && needs_reconfigure))) {
3603 if (!gst_audio_decoder_negotiate_unlocked (dec)) {
3604 GST_INFO_OBJECT (dec, "Failed to negotiate, fallback allocation");
3605 gst_pad_mark_reconfigure (dec->srcpad);
3611 gst_buffer_new_allocate (dec->priv->ctx.allocator, size,
3612 &dec->priv->ctx.params);
3614 GST_INFO_OBJECT (dec, "couldn't allocate output buffer");
3618 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
3622 buffer = gst_buffer_new_allocate (NULL, size, NULL);
3623 GST_AUDIO_DECODER_STREAM_UNLOCK (dec);
3629 * gst_audio_decoder_get_allocator:
3630 * @dec: a #GstAudioDecoder
3631 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3633 * @params: (out) (allow-none) (transfer full): the
3634 * #GstAllocationParams of @allocator
3636 * Lets #GstAudioDecoder sub-classes to know the memory @allocator
3637 * used by the base class and its @params.
3639 * Unref the @allocator after use it.
3642 gst_audio_decoder_get_allocator (GstAudioDecoder * dec,
3643 GstAllocator ** allocator, GstAllocationParams * params)
3645 g_return_if_fail (GST_IS_AUDIO_DECODER (dec));
3648 *allocator = dec->priv->ctx.allocator ?
3649 gst_object_ref (dec->priv->ctx.allocator) : NULL;
3652 *params = dec->priv->ctx.params;
3656 * gst_audio_decoder_set_use_default_pad_acceptcaps:
3657 * @decoder: a #GstAudioDecoder
3658 * @use: if the default pad accept-caps query handling should be used
3660 * Lets #GstAudioDecoder sub-classes decide if they want the sink pad
3661 * to use the default pad query handler to reply to accept-caps queries.
3663 * By setting this to true it is possible to further customize the default
3664 * handler with %GST_PAD_SET_ACCEPT_INTERSECT and
3665 * %GST_PAD_SET_ACCEPT_TEMPLATE
3670 gst_audio_decoder_set_use_default_pad_acceptcaps (GstAudioDecoder * decoder,
3673 decoder->priv->use_default_pad_acceptcaps = use;