b3ad1d4f6e5eb7b7b33072a2536790d651595bde
[platform/upstream/gstreamer.git] / gst-libs / gst / audio / gstaudioencoder.c
1 /* GStreamer
2  * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
3  * Copyright (C) 2011 Nokia Corporation. All rights reserved.
4  *   Contact: Stefan Kost <stefan.kost@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstaudioencoder
24  * @short_description: Base class for audio encoders
25  * @see_also: #GstBaseTransform
26  * @since: 0.10.36
27  *
28  * This base class is for audio encoders turning raw audio samples into
29  * encoded audio data.
30  *
31  * GstAudioEncoder and subclass should cooperate as follows.
32  * <orderedlist>
33  * <listitem>
34  *   <itemizedlist><title>Configuration</title>
35  *   <listitem><para>
36  *     Initially, GstAudioEncoder calls @start when the encoder element
37  *     is activated, which allows subclass to perform any global setup.
38  *   </para></listitem>
39  *   <listitem><para>
40  *     GstAudioEncoder calls @set_format to inform subclass of the format
41  *     of input audio data that it is about to receive.  Subclass should
42  *     setup for encoding and configure various base class parameters
43  *     appropriately, notably those directing desired input data handling.
44  *     While unlikely, it might be called more than once, if changing input
45  *     parameters require reconfiguration.
46  *   </para></listitem>
47  *   <listitem><para>
48  *     GstAudioEncoder calls @stop at end of all processing.
49  *   </para></listitem>
50  *   </itemizedlist>
51  * </listitem>
52  * As of configuration stage, and throughout processing, GstAudioEncoder
53  * maintains various parameters that provide required context,
54  * e.g. describing the format of input audio data.
55  * Conversely, subclass can and should configure these context parameters
56  * to inform base class of its expectation w.r.t. buffer handling.
57  * <listitem>
58  *   <itemizedlist>
59  *   <title>Data processing</title>
60  *     <listitem><para>
61  *       Base class gathers input sample data (as directed by the context's
62  *       frame_samples and frame_max) and provides this to subclass' @handle_frame.
63  *     </para></listitem>
64  *     <listitem><para>
65  *       If codec processing results in encoded data, subclass should call
66  *       @gst_audio_encoder_finish_frame to have encoded data pushed
67  *       downstream.  Alternatively, it might also call to indicate dropped
68  *       (non-encoded) samples.
69  *     </para></listitem>
70  *     <listitem><para>
71  *       Just prior to actually pushing a buffer downstream,
72  *       it is passed to @pre_push.
73  *     </para></listitem>
74  *     <listitem><para>
75  *       During the parsing process GstAudioEncoderClass will handle both
76  *       srcpad and sinkpad events. Sink events will be passed to subclass
77  *       if @event callback has been provided.
78  *     </para></listitem>
79  *   </itemizedlist>
80  * </listitem>
81  * <listitem>
82  *   <itemizedlist><title>Shutdown phase</title>
83  *   <listitem><para>
84  *     GstAudioEncoder class calls @stop to inform the subclass that data
85  *     parsing will be stopped.
86  *   </para></listitem>
87  *   </itemizedlist>
88  * </listitem>
89  * </orderedlist>
90  *
91  * Subclass is responsible for providing pad template caps for
92  * source and sink pads. The pads need to be named "sink" and "src". It also 
93  * needs to set the fixed caps on srcpad, when the format is ensured.  This
94  * is typically when base class calls subclass' @set_format function, though
95  * it might be delayed until calling @gst_audio_encoder_finish_frame.
96  *
97  * In summary, above process should have subclass concentrating on
98  * codec data processing while leaving other matters to base class,
99  * such as most notably timestamp handling.  While it may exert more control
100  * in this area (see e.g. @pre_push), it is very much not recommended.
101  *
102  * In particular, base class will either favor tracking upstream timestamps
103  * (at the possible expense of jitter) or aim to arrange for a perfect stream of
104  * output timestamps, depending on #GstAudioEncoder:perfect-timestamp.
105  * However, in the latter case, the input may not be so perfect or ideal, which
106  * is handled as follows.  An input timestamp is compared with the expected
107  * timestamp as dictated by input sample stream and if the deviation is less
108  * than #GstAudioEncoder:tolerance, the deviation is discarded.
109  * Otherwise, it is considered a discontuinity and subsequent output timestamp
110  * is resynced to the new position after performing configured discontinuity
111  * processing.  In the non-perfect-timestamp case, an upstream variation
112  * exceeding tolerance only leads to marking DISCONT on subsequent outgoing
113  * (while timestamps are adjusted to upstream regardless of variation).
114  * While DISCONT is also marked in the perfect-timestamp case, this one
115  * optionally (see #GstAudioEncoder:hard-resync)
116  * performs some additional steps, such as clipping of (early) input samples
117  * or draining all currently remaining input data, depending on the direction
118  * of the discontuinity.
119  *
120  * If perfect timestamps are arranged, it is also possible to request baseclass
121  * (usually set by subclass) to provide additional buffer metadata (in OFFSET
122  * and OFFSET_END) fields according to granule defined semantics currently
123  * needed by oggmux.  Specifically, OFFSET is set to granulepos (= sample count
124  * including buffer) and OFFSET_END to corresponding timestamp (as determined
125  * by same sample count and sample rate).
126  *
127  * Things that subclass need to take care of:
128  * <itemizedlist>
129  *   <listitem><para>Provide pad templates</para></listitem>
130  *   <listitem><para>
131  *      Set source pad caps when appropriate
132  *   </para></listitem>
133  *   <listitem><para>
134  *      Inform base class of buffer processing needs using context's
135  *      frame_samples and frame_bytes.
136  *   </para></listitem>
137  *   <listitem><para>
138  *      Set user-configurable properties to sane defaults for format and
139  *      implementing codec at hand, e.g. those controlling timestamp behaviour
140  *      and discontinuity processing.
141  *   </para></listitem>
142  *   <listitem><para>
143  *      Accept data in @handle_frame and provide encoded results to
144  *      @gst_audio_encoder_finish_frame.
145  *   </para></listitem>
146  * </itemizedlist>
147  *
148  */
149
150 #ifdef HAVE_CONFIG_H
151 #  include "config.h"
152 #endif
153
154 #define GST_USE_UNSTABLE_API
155 #include "gstaudioencoder.h"
156 #include <gst/base/gstadapter.h>
157 #include <gst/audio/audio.h>
158 #include <gst/pbutils/descriptions.h>
159
160 #include <stdlib.h>
161 #include <string.h>
162
163
164 GST_DEBUG_CATEGORY_STATIC (gst_audio_encoder_debug);
165 #define GST_CAT_DEFAULT gst_audio_encoder_debug
166
167 #define GST_AUDIO_ENCODER_GET_PRIVATE(obj)  \
168     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_AUDIO_ENCODER, \
169         GstAudioEncoderPrivate))
170
171 enum
172 {
173   PROP_0,
174   PROP_PERFECT_TS,
175   PROP_GRANULE,
176   PROP_HARD_RESYNC,
177   PROP_TOLERANCE
178 };
179
180 #define DEFAULT_PERFECT_TS   FALSE
181 #define DEFAULT_GRANULE      FALSE
182 #define DEFAULT_HARD_RESYNC  FALSE
183 #define DEFAULT_TOLERANCE    40000000
184
185 typedef struct _GstAudioEncoderContext
186 {
187   /* input */
188   GstAudioInfo info;
189
190   /* output */
191   gint frame_samples_min, frame_samples_max;
192   gint frame_max;
193   gint lookahead;
194   /* MT-protected (with LOCK) */
195   GstClockTime min_latency;
196   GstClockTime max_latency;
197 } GstAudioEncoderContext;
198
199 struct _GstAudioEncoderPrivate
200 {
201   /* activation status */
202   gboolean active;
203
204   /* input base/first ts as basis for output ts;
205    * kept nearly constant for perfect_ts,
206    * otherwise resyncs to upstream ts */
207   GstClockTime base_ts;
208   /* corresponding base granulepos */
209   gint64 base_gp;
210   /* input samples processed and sent downstream so far (w.r.t. base_ts) */
211   guint64 samples;
212
213   /* currently collected sample data */
214   GstAdapter *adapter;
215   /* offset in adapter up to which already supplied to encoder */
216   gint offset;
217   /* mark outgoing discont */
218   gboolean discont;
219   /* to guess duration of drained data */
220   GstClockTime last_duration;
221
222   /* subclass provided data in processing round */
223   gboolean got_data;
224   /* subclass gave all it could already */
225   gboolean drained;
226   /* subclass currently being forcibly drained */
227   gboolean force;
228
229   /* output bps estimatation */
230   /* global in samples seen */
231   guint64 samples_in;
232   /* global bytes sent out */
233   guint64 bytes_out;
234
235   /* context storage */
236   GstAudioEncoderContext ctx;
237
238   /* properties */
239   gint64 tolerance;
240   gboolean perfect_ts;
241   gboolean hard_resync;
242   gboolean granule;
243
244   /* pending tags */
245   GstTagList *tags;
246   /* pending serialized sink events, will be sent from finish_frame() */
247   GList *pending_events;
248 };
249
250 static void gst_audio_encoder_finalize (GObject * object);
251 static void gst_audio_encoder_reset (GstAudioEncoder * enc, gboolean full);
252
253 static void gst_audio_encoder_set_property (GObject * object,
254     guint prop_id, const GValue * value, GParamSpec * pspec);
255 static void gst_audio_encoder_get_property (GObject * object,
256     guint prop_id, GValue * value, GParamSpec * pspec);
257
258 static gboolean gst_audio_encoder_sink_activate_push (GstPad * pad,
259     gboolean active);
260
261 static gboolean gst_audio_encoder_sink_event (GstPad * pad, GstEvent * event);
262 static gboolean gst_audio_encoder_sink_setcaps (GstPad * pad, GstCaps * caps);
263 static GstFlowReturn gst_audio_encoder_chain (GstPad * pad, GstBuffer * buffer);
264 static gboolean gst_audio_encoder_src_query (GstPad * pad, GstQuery * query);
265 static gboolean gst_audio_encoder_sink_query (GstPad * pad, GstQuery * query);
266 static const GstQueryType *gst_audio_encoder_get_query_types (GstPad * pad);
267 static GstCaps *gst_audio_encoder_sink_getcaps (GstPad * pad);
268
269 static void
270 do_init (GType gtype)
271 {
272   const GInterfaceInfo preset_interface_info = {
273     NULL,                       /* interface_init */
274     NULL,                       /* interface_finalize */
275     NULL                        /* interface_data */
276   };
277
278   g_type_add_interface_static (gtype, GST_TYPE_PRESET, &preset_interface_info);
279 }
280
281 GST_BOILERPLATE_FULL (GstAudioEncoder, gst_audio_encoder, GstElement,
282     GST_TYPE_ELEMENT, do_init);
283
284 static void
285 gst_audio_encoder_class_init (GstAudioEncoderClass * klass)
286 {
287   GObjectClass *gobject_class;
288
289   gobject_class = G_OBJECT_CLASS (klass);
290
291   GST_DEBUG_CATEGORY_INIT (gst_audio_encoder_debug, "audioencoder", 0,
292       "audio encoder base class");
293
294   g_type_class_add_private (klass, sizeof (GstAudioEncoderPrivate));
295
296   gobject_class->set_property = gst_audio_encoder_set_property;
297   gobject_class->get_property = gst_audio_encoder_get_property;
298
299   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_audio_encoder_finalize);
300
301   /* properties */
302   g_object_class_install_property (gobject_class, PROP_PERFECT_TS,
303       g_param_spec_boolean ("perfect-timestamp", "Perfect Timestamps",
304           "Favour perfect timestamps over tracking upstream timestamps",
305           DEFAULT_PERFECT_TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306   g_object_class_install_property (gobject_class, PROP_GRANULE,
307       g_param_spec_boolean ("mark-granule", "Granule Marking",
308           "Apply granule semantics to buffer metadata (implies perfect-timestamp)",
309           DEFAULT_GRANULE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
310   g_object_class_install_property (gobject_class, PROP_HARD_RESYNC,
311       g_param_spec_boolean ("hard-resync", "Hard Resync",
312           "Perform clipping and sample flushing upon discontinuity",
313           DEFAULT_HARD_RESYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314   g_object_class_install_property (gobject_class, PROP_TOLERANCE,
315       g_param_spec_int64 ("tolerance", "Tolerance",
316           "Consider discontinuity if timestamp jitter/imperfection exceeds tolerance (ns)",
317           0, G_MAXINT64, DEFAULT_TOLERANCE,
318           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 }
320
321 static void
322 gst_audio_encoder_base_init (gpointer g_class)
323 {
324 }
325
326 static void
327 gst_audio_encoder_init (GstAudioEncoder * enc, GstAudioEncoderClass * bclass)
328 {
329   GstPadTemplate *pad_template;
330
331   GST_DEBUG_OBJECT (enc, "gst_audio_encoder_init");
332
333   enc->priv = GST_AUDIO_ENCODER_GET_PRIVATE (enc);
334
335   /* only push mode supported */
336   pad_template =
337       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
338   g_return_if_fail (pad_template != NULL);
339   enc->sinkpad = gst_pad_new_from_template (pad_template, "sink");
340   gst_pad_set_event_function (enc->sinkpad,
341       GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_event));
342   gst_pad_set_setcaps_function (enc->sinkpad,
343       GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_setcaps));
344   gst_pad_set_getcaps_function (enc->sinkpad,
345       GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_getcaps));
346   gst_pad_set_query_function (enc->sinkpad,
347       GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_query));
348   gst_pad_set_chain_function (enc->sinkpad,
349       GST_DEBUG_FUNCPTR (gst_audio_encoder_chain));
350   gst_pad_set_activatepush_function (enc->sinkpad,
351       GST_DEBUG_FUNCPTR (gst_audio_encoder_sink_activate_push));
352   gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
353
354   GST_DEBUG_OBJECT (enc, "sinkpad created");
355
356   /* and we don't mind upstream traveling stuff that much ... */
357   pad_template =
358       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
359   g_return_if_fail (pad_template != NULL);
360   enc->srcpad = gst_pad_new_from_template (pad_template, "src");
361   gst_pad_set_query_function (enc->srcpad,
362       GST_DEBUG_FUNCPTR (gst_audio_encoder_src_query));
363   gst_pad_set_query_type_function (enc->srcpad,
364       GST_DEBUG_FUNCPTR (gst_audio_encoder_get_query_types));
365   gst_pad_use_fixed_caps (enc->srcpad);
366   gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
367   GST_DEBUG_OBJECT (enc, "src created");
368
369   enc->priv->adapter = gst_adapter_new ();
370
371   g_static_rec_mutex_init (&enc->stream_lock);
372
373   /* property default */
374   enc->priv->granule = DEFAULT_GRANULE;
375   enc->priv->perfect_ts = DEFAULT_PERFECT_TS;
376   enc->priv->hard_resync = DEFAULT_HARD_RESYNC;
377   enc->priv->tolerance = DEFAULT_TOLERANCE;
378
379   /* init state */
380   gst_audio_encoder_reset (enc, TRUE);
381   GST_DEBUG_OBJECT (enc, "init ok");
382 }
383
384 static void
385 gst_audio_encoder_reset (GstAudioEncoder * enc, gboolean full)
386 {
387   GST_AUDIO_ENCODER_STREAM_LOCK (enc);
388
389   GST_LOG_OBJECT (enc, "reset full %d", full);
390
391   if (full) {
392     enc->priv->active = FALSE;
393     enc->priv->samples_in = 0;
394     enc->priv->bytes_out = 0;
395     gst_audio_info_clear (&enc->priv->ctx.info);
396     memset (&enc->priv->ctx, 0, sizeof (enc->priv->ctx));
397
398     if (enc->priv->tags)
399       gst_tag_list_free (enc->priv->tags);
400     enc->priv->tags = NULL;
401
402     g_list_foreach (enc->priv->pending_events, (GFunc) gst_event_unref, NULL);
403     g_list_free (enc->priv->pending_events);
404     enc->priv->pending_events = NULL;
405   }
406
407   gst_segment_init (&enc->segment, GST_FORMAT_TIME);
408
409   gst_adapter_clear (enc->priv->adapter);
410   enc->priv->got_data = FALSE;
411   enc->priv->drained = TRUE;
412   enc->priv->offset = 0;
413   enc->priv->base_ts = GST_CLOCK_TIME_NONE;
414   enc->priv->base_gp = -1;
415   enc->priv->samples = 0;
416   enc->priv->discont = FALSE;
417
418   GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
419 }
420
421 static void
422 gst_audio_encoder_finalize (GObject * object)
423 {
424   GstAudioEncoder *enc = GST_AUDIO_ENCODER (object);
425
426   g_object_unref (enc->priv->adapter);
427
428   g_static_rec_mutex_free (&enc->stream_lock);
429
430   G_OBJECT_CLASS (parent_class)->finalize (object);
431 }
432
433 /**
434  * gst_audio_encoder_finish_frame:
435  * @enc: a #GstAudioEncoder
436  * @buffer: encoded data
437  * @samples: number of samples (per channel) represented by encoded data
438  *
439  * Collects encoded data and/or pushes encoded data downstream.
440  * Source pad caps must be set when this is called.  Depending on the nature
441  * of the (framing of) the format, subclass can decide whether to push
442  * encoded data directly or to collect various "frames" in a single buffer.
443  * Note that the latter behaviour is recommended whenever the format is allowed,
444  * as it incurs no additional latency and avoids otherwise generating a
445  * a multitude of (small) output buffers.  If not explicitly pushed,
446  * any available encoded data is pushed at the end of each processing cycle,
447  * i.e. which encodes as much data as available input data allows.
448  *
449  * If @samples < 0, then best estimate is all samples provided to encoder
450  * (subclass) so far.  @buf may be NULL, in which case next number of @samples
451  * are considered discarded, e.g. as a result of discontinuous transmission,
452  * and a discontinuity is marked (note that @buf == NULL => push == TRUE).
453  *
454  * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
455  *
456  * Since: 0.10.36
457  */
458 GstFlowReturn
459 gst_audio_encoder_finish_frame (GstAudioEncoder * enc, GstBuffer * buf,
460     gint samples)
461 {
462   GstAudioEncoderClass *klass;
463   GstAudioEncoderPrivate *priv;
464   GstAudioEncoderContext *ctx;
465   GstFlowReturn ret = GST_FLOW_OK;
466
467   klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
468   priv = enc->priv;
469   ctx = &enc->priv->ctx;
470
471   /* subclass should know what it is producing by now */
472   g_return_val_if_fail (GST_PAD_CAPS (enc->srcpad) != NULL, GST_FLOW_ERROR);
473   /* subclass should not hand us no data */
474   g_return_val_if_fail (buf == NULL || GST_BUFFER_SIZE (buf) > 0,
475       GST_FLOW_ERROR);
476
477   GST_AUDIO_ENCODER_STREAM_LOCK (enc);
478
479   if (G_UNLIKELY (enc->priv->tags)) {
480     GstTagList *tags;
481
482     /* add codec info to pending tags */
483     tags = enc->priv->tags;
484     /* no more pending */
485     enc->priv->tags = NULL;
486     gst_pb_utils_add_codec_description_to_tag_list (tags, GST_TAG_CODEC,
487         GST_PAD_CAPS (enc->srcpad));
488     gst_pb_utils_add_codec_description_to_tag_list (tags, GST_TAG_AUDIO_CODEC,
489         GST_PAD_CAPS (enc->srcpad));
490     GST_DEBUG_OBJECT (enc, "sending tags %" GST_PTR_FORMAT, tags);
491     gst_element_found_tags_for_pad (GST_ELEMENT (enc), enc->srcpad, tags);
492   }
493
494   GST_LOG_OBJECT (enc, "accepting %d bytes encoded data as %d samples",
495       buf ? GST_BUFFER_SIZE (buf) : -1, samples);
496
497   /* mark subclass still alive and providing */
498   priv->got_data = TRUE;
499
500   if (priv->pending_events) {
501     GList *pending_events, *l;
502
503     pending_events = priv->pending_events;
504     priv->pending_events = NULL;
505
506     GST_DEBUG_OBJECT (enc, "Pushing pending events");
507     for (l = priv->pending_events; l; l = l->next)
508       gst_pad_push_event (enc->srcpad, l->data);
509     g_list_free (pending_events);
510   }
511
512   /* remove corresponding samples from input */
513   if (samples < 0)
514     samples = (enc->priv->offset / ctx->info.bpf);
515
516   if (G_LIKELY (samples)) {
517     /* track upstream ts if so configured */
518     if (!enc->priv->perfect_ts) {
519       guint64 ts, distance;
520
521       ts = gst_adapter_prev_timestamp (priv->adapter, &distance);
522       g_assert (distance % ctx->info.bpf == 0);
523       distance /= ctx->info.bpf;
524       GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past prev_ts %"
525           GST_TIME_FORMAT, distance, GST_TIME_ARGS (ts));
526       GST_LOG_OBJECT (enc, "%" G_GUINT64_FORMAT " samples past base_ts %"
527           GST_TIME_FORMAT, priv->samples, GST_TIME_ARGS (priv->base_ts));
528       /* when draining adapter might be empty and no ts to offer */
529       if (GST_CLOCK_TIME_IS_VALID (ts) && ts != priv->base_ts) {
530         GstClockTimeDiff diff;
531         GstClockTime old_ts, next_ts;
532
533         /* passed into another buffer;
534          * mild check for discontinuity and only mark if so */
535         next_ts = ts +
536             gst_util_uint64_scale (distance, GST_SECOND, ctx->info.rate);
537         old_ts = priv->base_ts +
538             gst_util_uint64_scale (priv->samples, GST_SECOND, ctx->info.rate);
539         diff = GST_CLOCK_DIFF (next_ts, old_ts);
540         GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
541         /* only mark discontinuity if beyond tolerance */
542         if (G_UNLIKELY (diff < -enc->priv->tolerance ||
543                 diff > enc->priv->tolerance)) {
544           GST_DEBUG_OBJECT (enc, "marked discont");
545           priv->discont = TRUE;
546         }
547         if (diff > GST_SECOND / ctx->info.rate / 2 ||
548             diff < -GST_SECOND / ctx->info.rate / 2) {
549           GST_LOG_OBJECT (enc, "new upstream ts %" GST_TIME_FORMAT
550               " at distance %" G_GUINT64_FORMAT, GST_TIME_ARGS (ts), distance);
551           /* re-sync to upstream ts */
552           priv->base_ts = ts;
553           priv->samples = distance;
554         } else {
555           GST_LOG_OBJECT (enc, "new upstream ts only introduces jitter");
556         }
557       }
558     }
559     /* advance sample view */
560     if (G_UNLIKELY (samples * ctx->info.bpf > priv->offset)) {
561       if (G_LIKELY (!priv->force)) {
562         /* no way we can let this pass */
563         g_assert_not_reached ();
564         /* really no way */
565         goto overflow;
566       } else {
567         priv->offset = 0;
568         if (samples * ctx->info.bpf >= gst_adapter_available (priv->adapter))
569           gst_adapter_clear (priv->adapter);
570         else
571           gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
572       }
573     } else {
574       gst_adapter_flush (priv->adapter, samples * ctx->info.bpf);
575       priv->offset -= samples * ctx->info.bpf;
576       /* avoid subsequent stray prev_ts */
577       if (G_UNLIKELY (gst_adapter_available (priv->adapter) == 0))
578         gst_adapter_clear (priv->adapter);
579     }
580     /* sample count advanced below after buffer handling */
581   }
582
583   /* collect output */
584   if (G_LIKELY (buf)) {
585     GST_LOG_OBJECT (enc, "taking %d bytes for output", GST_BUFFER_SIZE (buf));
586     buf = gst_buffer_make_metadata_writable (buf);
587
588     /* decorate */
589     gst_buffer_set_caps (buf, GST_PAD_CAPS (enc->srcpad));
590     if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (priv->base_ts))) {
591       /* FIXME ? lookahead could lead to weird ts and duration ?
592        * (particularly if not in perfect mode) */
593       /* mind sample rounding and produce perfect output */
594       GST_BUFFER_TIMESTAMP (buf) = priv->base_ts +
595           gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
596           ctx->info.rate);
597       GST_DEBUG_OBJECT (enc, "out samples %d", samples);
598       if (G_LIKELY (samples > 0)) {
599         priv->samples += samples;
600         GST_BUFFER_DURATION (buf) = priv->base_ts +
601             gst_util_uint64_scale (priv->samples - ctx->lookahead, GST_SECOND,
602             ctx->info.rate) - GST_BUFFER_TIMESTAMP (buf);
603         priv->last_duration = GST_BUFFER_DURATION (buf);
604       } else {
605         /* duration forecast in case of handling remainder;
606          * the last one is probably like the previous one ... */
607         GST_BUFFER_DURATION (buf) = priv->last_duration;
608       }
609       if (priv->base_gp >= 0) {
610         /* pamper oggmux */
611         /* FIXME: in longer run, muxer should take care of this ... */
612         /* offset_end = granulepos for ogg muxer */
613         GST_BUFFER_OFFSET_END (buf) = priv->base_gp + priv->samples -
614             enc->priv->ctx.lookahead;
615         /* offset = timestamp corresponding to granulepos for ogg muxer */
616         GST_BUFFER_OFFSET (buf) =
617             GST_FRAMES_TO_CLOCK_TIME (GST_BUFFER_OFFSET_END (buf),
618             ctx->info.rate);
619       } else {
620         GST_BUFFER_OFFSET (buf) = priv->bytes_out;
621         GST_BUFFER_OFFSET_END (buf) = priv->bytes_out + GST_BUFFER_SIZE (buf);
622       }
623     }
624
625     priv->bytes_out += GST_BUFFER_SIZE (buf);
626
627     if (G_UNLIKELY (priv->discont)) {
628       GST_LOG_OBJECT (enc, "marking discont");
629       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
630       priv->discont = FALSE;
631     }
632
633     if (klass->pre_push) {
634       /* last chance for subclass to do some dirty stuff */
635       ret = klass->pre_push (enc, &buf);
636       if (ret != GST_FLOW_OK || !buf) {
637         GST_DEBUG_OBJECT (enc, "subclass returned %s, buf %p",
638             gst_flow_get_name (ret), buf);
639         if (buf)
640           gst_buffer_unref (buf);
641         goto exit;
642       }
643     }
644
645     GST_LOG_OBJECT (enc, "pushing buffer of size %d with ts %" GST_TIME_FORMAT
646         ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buf),
647         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
648         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
649
650     ret = gst_pad_push (enc->srcpad, buf);
651     GST_LOG_OBJECT (enc, "buffer pushed: %s", gst_flow_get_name (ret));
652   } else {
653     /* merely advance samples, most work for that already done above */
654     priv->samples += samples;
655   }
656
657 exit:
658   GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
659
660   return ret;
661
662   /* ERRORS */
663 overflow:
664   {
665     GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
666         ("received more encoded samples %d than provided %d",
667             samples, priv->offset / ctx->info.bpf), (NULL));
668     if (buf)
669       gst_buffer_unref (buf);
670     ret = GST_FLOW_ERROR;
671     goto exit;
672   }
673 }
674
675  /* adapter tracking idea:
676   * - start of adapter corresponds with what has already been encoded
677   * (i.e. really returned by encoder subclass)
678   * - start + offset is what needs to be fed to subclass next */
679 static GstFlowReturn
680 gst_audio_encoder_push_buffers (GstAudioEncoder * enc, gboolean force)
681 {
682   GstAudioEncoderClass *klass;
683   GstAudioEncoderPrivate *priv;
684   GstAudioEncoderContext *ctx;
685   gint av, need;
686   GstBuffer *buf;
687   GstFlowReturn ret = GST_FLOW_OK;
688
689   klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
690
691   g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
692
693   priv = enc->priv;
694   ctx = &enc->priv->ctx;
695
696   while (ret == GST_FLOW_OK) {
697
698     buf = NULL;
699     av = gst_adapter_available (priv->adapter);
700
701     g_assert (priv->offset <= av);
702     av -= priv->offset;
703
704     need =
705         ctx->frame_samples_min >
706         0 ? ctx->frame_samples_min * ctx->info.bpf : av;
707     GST_LOG_OBJECT (enc, "available: %d, needed: %d, force: %d", av, need,
708         force);
709
710     if ((need > av) || !av) {
711       if (G_UNLIKELY (force)) {
712         priv->force = TRUE;
713         need = av;
714       } else {
715         break;
716       }
717     } else {
718       priv->force = FALSE;
719     }
720
721     if (ctx->frame_samples_max > 0)
722       need = MIN (av, ctx->frame_samples_max * ctx->info.bpf);
723
724     if (ctx->frame_samples_min == ctx->frame_samples_max) {
725       /* if we have some extra metadata,
726        * provide for integer multiple of frames to allow for better granularity
727        * of processing */
728       if (ctx->frame_samples_min > 0 && need) {
729         if (ctx->frame_max > 1)
730           need = need * MIN ((av / need), ctx->frame_max);
731         else if (ctx->frame_max == 0)
732           need = need * (av / need);
733       }
734     }
735
736     if (need) {
737       buf = gst_buffer_new ();
738       GST_BUFFER_DATA (buf) = (guint8 *)
739           gst_adapter_peek (priv->adapter, priv->offset + need) + priv->offset;
740       GST_BUFFER_SIZE (buf) = need;
741     }
742
743     GST_LOG_OBJECT (enc, "providing subclass with %d bytes at offset %d",
744         need, priv->offset);
745
746     /* mark this already as consumed,
747      * which it should be when subclass gives us data in exchange for samples */
748     priv->offset += need;
749     priv->samples_in += need / ctx->info.bpf;
750
751     priv->got_data = FALSE;
752     ret = klass->handle_frame (enc, buf);
753
754     if (G_LIKELY (buf))
755       gst_buffer_unref (buf);
756
757     /* no data to feed, no leftover provided, then bail out */
758     if (G_UNLIKELY (!buf && !priv->got_data)) {
759       priv->drained = TRUE;
760       GST_LOG_OBJECT (enc, "no more data drained from subclass");
761       break;
762     }
763   }
764
765   return ret;
766 }
767
768 static GstFlowReturn
769 gst_audio_encoder_drain (GstAudioEncoder * enc)
770 {
771   if (enc->priv->drained)
772     return GST_FLOW_OK;
773   else
774     return gst_audio_encoder_push_buffers (enc, TRUE);
775 }
776
777 static void
778 gst_audio_encoder_set_base_gp (GstAudioEncoder * enc)
779 {
780   GstClockTime ts;
781
782   if (!enc->priv->granule)
783     return;
784
785   /* use running time for granule */
786   /* incoming data is clipped, so a valid input should yield a valid output */
787   ts = gst_segment_to_running_time (&enc->segment, GST_FORMAT_TIME,
788       enc->priv->base_ts);
789   if (GST_CLOCK_TIME_IS_VALID (ts)) {
790     enc->priv->base_gp =
791         GST_CLOCK_TIME_TO_FRAMES (enc->priv->base_ts, enc->priv->ctx.info.rate);
792     GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT, enc->priv->base_gp);
793   } else {
794     /* should reasonably have a valid base,
795      * otherwise start at 0 if we did not already start there earlier */
796     if (enc->priv->base_gp < 0) {
797       enc->priv->base_gp = 0;
798       GST_DEBUG_OBJECT (enc, "new base gp %" G_GINT64_FORMAT,
799           enc->priv->base_gp);
800     }
801   }
802 }
803
804 static GstFlowReturn
805 gst_audio_encoder_chain (GstPad * pad, GstBuffer * buffer)
806 {
807   GstAudioEncoder *enc;
808   GstAudioEncoderPrivate *priv;
809   GstAudioEncoderContext *ctx;
810   GstFlowReturn ret = GST_FLOW_OK;
811   gboolean discont;
812
813   enc = GST_AUDIO_ENCODER (GST_OBJECT_PARENT (pad));
814
815   priv = enc->priv;
816   ctx = &enc->priv->ctx;
817
818   GST_AUDIO_ENCODER_STREAM_LOCK (enc);
819
820   /* should know what is coming by now */
821   if (!ctx->info.bpf)
822     goto not_negotiated;
823
824   GST_LOG_OBJECT (enc,
825       "received buffer of size %d with ts %" GST_TIME_FORMAT
826       ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
827       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
828       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
829
830   /* input shoud be whole number of sample frames */
831   if (GST_BUFFER_SIZE (buffer) % ctx->info.bpf)
832     goto wrong_buffer;
833
834 #ifndef GST_DISABLE_GST_DEBUG
835   {
836     GstClockTime duration;
837     GstClockTimeDiff diff;
838
839     /* verify buffer duration */
840     duration = gst_util_uint64_scale (GST_BUFFER_SIZE (buffer), GST_SECOND,
841         ctx->info.rate * ctx->info.bpf);
842     diff = GST_CLOCK_DIFF (duration, GST_BUFFER_DURATION (buffer));
843     if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE &&
844         (diff > GST_SECOND / ctx->info.rate / 2 ||
845             diff < -GST_SECOND / ctx->info.rate / 2)) {
846       GST_DEBUG_OBJECT (enc, "incoming buffer had incorrect duration %"
847           GST_TIME_FORMAT ", expected duration %" GST_TIME_FORMAT,
848           GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
849           GST_TIME_ARGS (duration));
850     }
851   }
852 #endif
853
854   discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
855   if (G_UNLIKELY (discont)) {
856     GST_LOG_OBJECT (buffer, "marked discont");
857     enc->priv->discont = discont;
858   }
859
860   /* clip to segment */
861   /* NOTE: slightly painful linking -laudio only for this one ... */
862   buffer = gst_audio_buffer_clip (buffer, &enc->segment, ctx->info.rate,
863       ctx->info.bpf);
864   if (G_UNLIKELY (!buffer)) {
865     GST_DEBUG_OBJECT (buffer, "no data after clipping to segment");
866     goto done;
867   }
868
869   GST_LOG_OBJECT (enc,
870       "buffer after segment clipping has size %d with ts %" GST_TIME_FORMAT
871       ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
872       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
873       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
874
875   if (!GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
876     priv->base_ts = GST_BUFFER_TIMESTAMP (buffer);
877     GST_DEBUG_OBJECT (enc, "new base ts %" GST_TIME_FORMAT,
878         GST_TIME_ARGS (priv->base_ts));
879     gst_audio_encoder_set_base_gp (enc);
880   }
881
882   /* check for continuity;
883    * checked elsewhere in non-perfect case */
884   if (enc->priv->perfect_ts) {
885     GstClockTimeDiff diff = 0;
886     GstClockTime next_ts = 0;
887
888     if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
889         GST_CLOCK_TIME_IS_VALID (priv->base_ts)) {
890       guint64 samples;
891
892       samples = priv->samples +
893           gst_adapter_available (priv->adapter) / ctx->info.bpf;
894       next_ts = priv->base_ts +
895           gst_util_uint64_scale (samples, GST_SECOND, ctx->info.rate);
896       GST_LOG_OBJECT (enc, "buffer is %" G_GUINT64_FORMAT
897           " samples past base_ts %" GST_TIME_FORMAT
898           ", expected ts %" GST_TIME_FORMAT, samples,
899           GST_TIME_ARGS (priv->base_ts), GST_TIME_ARGS (next_ts));
900       diff = GST_CLOCK_DIFF (next_ts, GST_BUFFER_TIMESTAMP (buffer));
901       GST_LOG_OBJECT (enc, "ts diff %d ms", (gint) (diff / GST_MSECOND));
902       /* if within tolerance,
903        * discard buffer ts and carry on producing perfect stream,
904        * otherwise clip or resync to ts */
905       if (G_UNLIKELY (diff < -enc->priv->tolerance ||
906               diff > enc->priv->tolerance)) {
907         GST_DEBUG_OBJECT (enc, "marked discont");
908         discont = TRUE;
909       }
910     }
911
912     /* do some fancy tweaking in hard resync case */
913     if (discont && enc->priv->hard_resync) {
914       if (diff < 0) {
915         guint64 diff_bytes;
916
917         GST_WARNING_OBJECT (enc, "Buffer is older than expected ts %"
918             GST_TIME_FORMAT ".  Clipping buffer", GST_TIME_ARGS (next_ts));
919
920         diff_bytes =
921             GST_CLOCK_TIME_TO_FRAMES (-diff, ctx->info.rate) * ctx->info.bpf;
922         if (diff_bytes >= GST_BUFFER_SIZE (buffer)) {
923           gst_buffer_unref (buffer);
924           goto done;
925         }
926         buffer = gst_buffer_make_metadata_writable (buffer);
927         GST_BUFFER_DATA (buffer) += diff_bytes;
928         GST_BUFFER_SIZE (buffer) -= diff_bytes;
929
930         GST_BUFFER_TIMESTAMP (buffer) += diff;
931         /* care even less about duration after this */
932       } else {
933         /* drain stuff prior to resync */
934         gst_audio_encoder_drain (enc);
935       }
936     }
937     /* now re-sync ts */
938     priv->base_ts += diff;
939     gst_audio_encoder_set_base_gp (enc);
940     priv->discont |= discont;
941   }
942
943   gst_adapter_push (enc->priv->adapter, buffer);
944   /* new stuff, so we can push subclass again */
945   enc->priv->drained = FALSE;
946
947   ret = gst_audio_encoder_push_buffers (enc, FALSE);
948
949 done:
950   GST_LOG_OBJECT (enc, "chain leaving");
951
952   GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
953
954   return ret;
955
956   /* ERRORS */
957 not_negotiated:
958   {
959     GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
960         ("encoder not initialized"));
961     gst_buffer_unref (buffer);
962     ret = GST_FLOW_NOT_NEGOTIATED;
963     goto done;
964   }
965 wrong_buffer:
966   {
967     GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
968         ("buffer size %d not a multiple of %d", GST_BUFFER_SIZE (buffer),
969             ctx->info.bpf));
970     gst_buffer_unref (buffer);
971     ret = GST_FLOW_ERROR;
972     goto done;
973   }
974 }
975
976 static gboolean
977 audio_info_is_equal (GstAudioInfo * from, GstAudioInfo * to)
978 {
979   if (from == to)
980     return TRUE;
981   if (from->finfo == NULL || to->finfo == NULL)
982     return FALSE;
983   if (GST_AUDIO_INFO_FORMAT (from) != GST_AUDIO_INFO_FORMAT (to))
984     return FALSE;
985   if (GST_AUDIO_INFO_RATE (from) != GST_AUDIO_INFO_RATE (to))
986     return FALSE;
987   if (GST_AUDIO_INFO_CHANNELS (from) != GST_AUDIO_INFO_CHANNELS (to))
988     return FALSE;
989   if (GST_AUDIO_INFO_CHANNELS (from) > 64)
990     return TRUE;
991   return memcmp (from->position, to->position,
992       GST_AUDIO_INFO_CHANNELS (from) * sizeof (to->position[0]));
993 }
994
995 static gboolean
996 gst_audio_encoder_sink_setcaps (GstPad * pad, GstCaps * caps)
997 {
998   GstAudioEncoder *enc;
999   GstAudioEncoderClass *klass;
1000   GstAudioEncoderContext *ctx;
1001   GstAudioInfo *state, *old_state;
1002   gboolean res = TRUE, changed = FALSE;
1003   guint old_rate;
1004
1005   enc = GST_AUDIO_ENCODER (GST_PAD_PARENT (pad));
1006   klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1007
1008   /* subclass must do something here ... */
1009   g_return_val_if_fail (klass->set_format != NULL, FALSE);
1010
1011   ctx = &enc->priv->ctx;
1012   state = &ctx->info;
1013
1014   GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1015
1016   GST_DEBUG_OBJECT (enc, "caps: %" GST_PTR_FORMAT, caps);
1017
1018   if (!gst_caps_is_fixed (caps))
1019     goto refuse_caps;
1020
1021   /* adjust ts tracking to new sample rate */
1022   old_rate = GST_AUDIO_INFO_RATE (state);
1023   if (GST_CLOCK_TIME_IS_VALID (enc->priv->base_ts) && old_rate) {
1024     enc->priv->base_ts +=
1025         GST_FRAMES_TO_CLOCK_TIME (enc->priv->samples, old_rate);
1026     enc->priv->samples = 0;
1027   }
1028
1029   old_state = gst_audio_info_copy (state);
1030   if (!gst_audio_info_from_caps (state, caps))
1031     goto refuse_caps;
1032
1033   changed = !audio_info_is_equal (state, old_state);
1034   gst_audio_info_free (old_state);
1035
1036   if (changed) {
1037     GstClockTime old_min_latency;
1038     GstClockTime old_max_latency;
1039
1040     /* drain any pending old data stuff */
1041     gst_audio_encoder_drain (enc);
1042
1043     /* context defaults */
1044     enc->priv->ctx.frame_samples_min = 0;
1045     enc->priv->ctx.frame_samples_max = 0;
1046     enc->priv->ctx.frame_max = 0;
1047     enc->priv->ctx.lookahead = 0;
1048
1049     /* element might report latency */
1050     GST_OBJECT_LOCK (enc);
1051     old_min_latency = ctx->min_latency;
1052     old_max_latency = ctx->max_latency;
1053     GST_OBJECT_UNLOCK (enc);
1054
1055     if (klass->set_format)
1056       res = klass->set_format (enc, state);
1057
1058     /* notify if new latency */
1059     GST_OBJECT_LOCK (enc);
1060     if ((ctx->min_latency > 0 && ctx->min_latency != old_min_latency) ||
1061         (ctx->max_latency > 0 && ctx->max_latency != old_max_latency)) {
1062       GST_OBJECT_UNLOCK (enc);
1063       /* post latency message on the bus */
1064       gst_element_post_message (GST_ELEMENT (enc),
1065           gst_message_new_latency (GST_OBJECT (enc)));
1066       GST_OBJECT_LOCK (enc);
1067     }
1068     GST_OBJECT_UNLOCK (enc);
1069   } else {
1070     GST_DEBUG_OBJECT (enc, "new audio format identical to configured format");
1071   }
1072
1073 exit:
1074
1075   GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1076
1077   return res;
1078
1079   /* ERRORS */
1080 refuse_caps:
1081   {
1082     GST_WARNING_OBJECT (enc, "rejected caps %" GST_PTR_FORMAT, caps);
1083     goto exit;
1084   }
1085 }
1086
1087
1088 /**
1089  * gst_audio_encoder_proxy_getcaps:
1090  * @enc: a #GstAudioEncoder
1091  * @caps: initial caps
1092  *
1093  * Returns caps that express @caps (or sink template caps if @caps == NULL)
1094  * restricted to channel/rate combinations supported by downstream elements
1095  * (e.g. muxers).
1096  *
1097  * Returns: a #GstCaps owned by caller
1098  *
1099  * Since: 0.10.36
1100  */
1101 GstCaps *
1102 gst_audio_encoder_proxy_getcaps (GstAudioEncoder * enc, GstCaps * caps)
1103 {
1104   const GstCaps *templ_caps;
1105   GstCaps *allowed = NULL;
1106   GstCaps *fcaps, *filter_caps;
1107   gint i, j;
1108
1109   /* we want to be able to communicate to upstream elements like audioconvert
1110    * and audioresample any rate/channel restrictions downstream (e.g. muxer
1111    * only accepting certain sample rates) */
1112   templ_caps = caps ? caps : gst_pad_get_pad_template_caps (enc->sinkpad);
1113   allowed = gst_pad_get_allowed_caps (enc->srcpad);
1114   if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
1115     fcaps = gst_caps_copy (templ_caps);
1116     goto done;
1117   }
1118
1119   GST_LOG_OBJECT (enc, "template caps %" GST_PTR_FORMAT, templ_caps);
1120   GST_LOG_OBJECT (enc, "allowed caps %" GST_PTR_FORMAT, allowed);
1121
1122   filter_caps = gst_caps_new_empty ();
1123
1124   for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
1125     GQuark q_name;
1126
1127     q_name = gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
1128
1129     /* pick rate + channel fields from allowed caps */
1130     for (j = 0; j < gst_caps_get_size (allowed); j++) {
1131       const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
1132       const GValue *val;
1133       GstStructure *s;
1134
1135       s = gst_structure_id_empty_new (q_name);
1136       if ((val = gst_structure_get_value (allowed_s, "rate")))
1137         gst_structure_set_value (s, "rate", val);
1138       if ((val = gst_structure_get_value (allowed_s, "channels")))
1139         gst_structure_set_value (s, "channels", val);
1140       /* following might also make sense for some encoded formats,
1141        * e.g. wavpack */
1142       if ((val = gst_structure_get_value (allowed_s, "width")))
1143         gst_structure_set_value (s, "width", val);
1144       if ((val = gst_structure_get_value (allowed_s, "depth")))
1145         gst_structure_set_value (s, "depth", val);
1146       if ((val = gst_structure_get_value (allowed_s, "endianness")))
1147         gst_structure_set_value (s, "endianness", val);
1148       if ((val = gst_structure_get_value (allowed_s, "signed")))
1149         gst_structure_set_value (s, "signed", val);
1150       if ((val = gst_structure_get_value (allowed_s, "channel-positions")))
1151         gst_structure_set_value (s, "channel-positions", val);
1152
1153       gst_caps_merge_structure (filter_caps, s);
1154     }
1155   }
1156
1157   fcaps = gst_caps_intersect (filter_caps, templ_caps);
1158   gst_caps_unref (filter_caps);
1159
1160 done:
1161   gst_caps_replace (&allowed, NULL);
1162
1163   GST_LOG_OBJECT (enc, "proxy caps %" GST_PTR_FORMAT, fcaps);
1164
1165   return fcaps;
1166 }
1167
1168 static GstCaps *
1169 gst_audio_encoder_sink_getcaps (GstPad * pad)
1170 {
1171   GstAudioEncoder *enc;
1172   GstAudioEncoderClass *klass;
1173   GstCaps *caps;
1174
1175   enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1176   klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1177   g_assert (pad == enc->sinkpad);
1178
1179   if (klass->getcaps)
1180     caps = klass->getcaps (enc);
1181   else
1182     caps = gst_audio_encoder_proxy_getcaps (enc, NULL);
1183   gst_object_unref (enc);
1184
1185   GST_LOG_OBJECT (enc, "returning caps %" GST_PTR_FORMAT, caps);
1186
1187   return caps;
1188 }
1189
1190 static gboolean
1191 gst_audio_encoder_sink_eventfunc (GstAudioEncoder * enc, GstEvent * event)
1192 {
1193   GstAudioEncoderClass *klass;
1194   gboolean handled = FALSE;
1195
1196   klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1197
1198   switch (GST_EVENT_TYPE (event)) {
1199     case GST_EVENT_NEWSEGMENT:
1200     {
1201       GstFormat format;
1202       gdouble rate, arate;
1203       gint64 start, stop, time;
1204       gboolean update;
1205
1206       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1207           &start, &stop, &time);
1208
1209       if (format == GST_FORMAT_TIME) {
1210         GST_DEBUG_OBJECT (enc, "received TIME NEW_SEGMENT %" GST_TIME_FORMAT
1211             " -- %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT
1212             ", rate %g, applied_rate %g",
1213             GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
1214             rate, arate);
1215       } else {
1216         GST_DEBUG_OBJECT (enc, "received NEW_SEGMENT %" G_GINT64_FORMAT
1217             " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT
1218             ", rate %g, applied_rate %g", start, stop, time, rate, arate);
1219         GST_DEBUG_OBJECT (enc, "unsupported format; ignoring");
1220         break;
1221       }
1222
1223       GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1224       /* finish current segment */
1225       gst_audio_encoder_drain (enc);
1226       /* reset partially for new segment */
1227       gst_audio_encoder_reset (enc, FALSE);
1228       /* and follow along with segment */
1229       gst_segment_set_newsegment_full (&enc->segment, update, rate, arate,
1230           format, start, stop, time);
1231       GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1232       break;
1233     }
1234
1235     case GST_EVENT_FLUSH_START:
1236       break;
1237
1238     case GST_EVENT_FLUSH_STOP:
1239       GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1240       /* discard any pending stuff */
1241       /* TODO route through drain ?? */
1242       if (!enc->priv->drained && klass->flush)
1243         klass->flush (enc);
1244       /* and get (re)set for the sequel */
1245       gst_audio_encoder_reset (enc, FALSE);
1246
1247       g_list_foreach (enc->priv->pending_events, (GFunc) gst_event_unref, NULL);
1248       g_list_free (enc->priv->pending_events);
1249       enc->priv->pending_events = NULL;
1250       GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1251
1252       break;
1253
1254     case GST_EVENT_EOS:
1255       GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1256       gst_audio_encoder_drain (enc);
1257       GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1258       break;
1259
1260     case GST_EVENT_TAG:
1261     {
1262       GstTagList *tags;
1263
1264       gst_event_parse_tag (event, &tags);
1265       tags = gst_tag_list_copy (tags);
1266       gst_event_unref (event);
1267       gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
1268       gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
1269       event = gst_event_new_tag (tags);
1270
1271       GST_OBJECT_LOCK (enc);
1272       enc->priv->pending_events =
1273           g_list_append (enc->priv->pending_events, event);
1274       GST_OBJECT_UNLOCK (enc);
1275       handled = TRUE;
1276       break;
1277     }
1278
1279     default:
1280       break;
1281   }
1282
1283   return handled;
1284 }
1285
1286 static gboolean
1287 gst_audio_encoder_sink_event (GstPad * pad, GstEvent * event)
1288 {
1289   GstAudioEncoder *enc;
1290   GstAudioEncoderClass *klass;
1291   gboolean handled = FALSE;
1292   gboolean ret = TRUE;
1293
1294   enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1295   klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1296
1297   GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1298       GST_EVENT_TYPE_NAME (event));
1299
1300   if (klass->event)
1301     handled = klass->event (enc, event);
1302
1303   if (!handled)
1304     handled = gst_audio_encoder_sink_eventfunc (enc, event);
1305
1306   if (!handled) {
1307     /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1308      * For EOS this is required because no buffer or serialized event
1309      * will come after EOS and nothing could trigger another
1310      * _finish_frame() call.
1311      *
1312      * For FLUSH_STOP this is required because it is expected
1313      * to be forwarded immediately and no buffers are queued anyway.
1314      */
1315     if (!GST_EVENT_IS_SERIALIZED (event)
1316         || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1317         || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1318       ret = gst_pad_event_default (pad, event);
1319     } else {
1320       GST_AUDIO_ENCODER_STREAM_LOCK (enc);
1321       enc->priv->pending_events =
1322           g_list_append (enc->priv->pending_events, event);
1323       GST_AUDIO_ENCODER_STREAM_UNLOCK (enc);
1324       ret = TRUE;
1325     }
1326   }
1327
1328   GST_DEBUG_OBJECT (enc, "event handled");
1329
1330   gst_object_unref (enc);
1331   return ret;
1332 }
1333
1334 static gboolean
1335 gst_audio_encoder_sink_query (GstPad * pad, GstQuery * query)
1336 {
1337   gboolean res = TRUE;
1338   GstAudioEncoder *enc;
1339
1340   enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1341
1342   switch (GST_QUERY_TYPE (query)) {
1343     case GST_QUERY_FORMATS:
1344     {
1345       gst_query_set_formats (query, 3,
1346           GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT);
1347       res = TRUE;
1348       break;
1349     }
1350     case GST_QUERY_CONVERT:
1351     {
1352       GstFormat src_fmt, dest_fmt;
1353       gint64 src_val, dest_val;
1354
1355       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1356       if (!(res = gst_audio_info_convert (&enc->priv->ctx.info,
1357                   src_fmt, src_val, dest_fmt, &dest_val)))
1358         goto error;
1359       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1360       break;
1361     }
1362     default:
1363       res = gst_pad_query_default (pad, query);
1364       break;
1365   }
1366
1367 error:
1368   gst_object_unref (enc);
1369   return res;
1370 }
1371
1372 static const GstQueryType *
1373 gst_audio_encoder_get_query_types (GstPad * pad)
1374 {
1375   static const GstQueryType gst_audio_encoder_src_query_types[] = {
1376     GST_QUERY_POSITION,
1377     GST_QUERY_DURATION,
1378     GST_QUERY_CONVERT,
1379     GST_QUERY_LATENCY,
1380     0
1381   };
1382
1383   return gst_audio_encoder_src_query_types;
1384 }
1385
1386 /*
1387  * gst_audio_encoded_audio_convert:
1388  * @fmt: audio format of the encoded audio
1389  * @bytes: number of encoded bytes
1390  * @samples: number of encoded samples
1391  * @src_format: source format
1392  * @src_value: source value
1393  * @dest_format: destination format
1394  * @dest_value: destination format
1395  *
1396  * Helper function to convert @src_value in @src_format to @dest_value in
1397  * @dest_format for encoded audio data.  Conversion is possible between
1398  * BYTE and TIME format by using estimated bitrate based on
1399  * @samples and @bytes (and @fmt).
1400  *
1401  * Since: 0.10.36
1402  */
1403 /* FIXME: make gst_audio_encoded_audio_convert() public? */
1404 static gboolean
1405 gst_audio_encoded_audio_convert (GstAudioInfo * fmt,
1406     gint64 bytes, gint64 samples, GstFormat src_format,
1407     gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
1408 {
1409   gboolean res = FALSE;
1410
1411   g_return_val_if_fail (dest_format != NULL, FALSE);
1412   g_return_val_if_fail (dest_value != NULL, FALSE);
1413
1414   if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
1415           src_value == -1)) {
1416     if (dest_value)
1417       *dest_value = src_value;
1418     return TRUE;
1419   }
1420
1421   if (samples == 0 || bytes == 0 || fmt->rate == 0) {
1422     GST_DEBUG ("not enough metadata yet to convert");
1423     goto exit;
1424   }
1425
1426   bytes *= fmt->rate;
1427
1428   switch (src_format) {
1429     case GST_FORMAT_BYTES:
1430       switch (*dest_format) {
1431         case GST_FORMAT_TIME:
1432           *dest_value = gst_util_uint64_scale (src_value,
1433               GST_SECOND * samples, bytes);
1434           res = TRUE;
1435           break;
1436         default:
1437           res = FALSE;
1438       }
1439       break;
1440     case GST_FORMAT_TIME:
1441       switch (*dest_format) {
1442         case GST_FORMAT_BYTES:
1443           *dest_value = gst_util_uint64_scale (src_value, bytes,
1444               samples * GST_SECOND);
1445           res = TRUE;
1446           break;
1447         default:
1448           res = FALSE;
1449       }
1450       break;
1451     default:
1452       res = FALSE;
1453   }
1454
1455 exit:
1456   return res;
1457 }
1458
1459 /* FIXME ? are any of these queries (other than latency) an encoder's business
1460  * also, the conversion stuff might seem to make sense, but seems to not mind
1461  * segment stuff etc at all
1462  * Supposedly that's backward compatibility ... */
1463 static gboolean
1464 gst_audio_encoder_src_query (GstPad * pad, GstQuery * query)
1465 {
1466   GstAudioEncoder *enc;
1467   GstPad *peerpad;
1468   gboolean res = FALSE;
1469
1470   enc = GST_AUDIO_ENCODER (GST_PAD_PARENT (pad));
1471   peerpad = gst_pad_get_peer (GST_PAD (enc->sinkpad));
1472
1473   GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1474
1475   switch (GST_QUERY_TYPE (query)) {
1476     case GST_QUERY_POSITION:
1477     {
1478       GstFormat fmt, req_fmt;
1479       gint64 pos, val;
1480
1481       if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1482         GST_LOG_OBJECT (enc, "returning peer response");
1483         break;
1484       }
1485
1486       if (!peerpad) {
1487         GST_LOG_OBJECT (enc, "no peer");
1488         break;
1489       }
1490
1491       gst_query_parse_position (query, &req_fmt, NULL);
1492       fmt = GST_FORMAT_TIME;
1493       if (!(res = gst_pad_query_position (peerpad, &fmt, &pos)))
1494         break;
1495
1496       if ((res = gst_pad_query_convert (peerpad, fmt, pos, &req_fmt, &val))) {
1497         gst_query_set_position (query, req_fmt, val);
1498       }
1499       break;
1500     }
1501     case GST_QUERY_DURATION:
1502     {
1503       GstFormat fmt, req_fmt;
1504       gint64 dur, val;
1505
1506       if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1507         GST_LOG_OBJECT (enc, "returning peer response");
1508         break;
1509       }
1510
1511       if (!peerpad) {
1512         GST_LOG_OBJECT (enc, "no peer");
1513         break;
1514       }
1515
1516       gst_query_parse_duration (query, &req_fmt, NULL);
1517       fmt = GST_FORMAT_TIME;
1518       if (!(res = gst_pad_query_duration (peerpad, &fmt, &dur)))
1519         break;
1520
1521       if ((res = gst_pad_query_convert (peerpad, fmt, dur, &req_fmt, &val))) {
1522         gst_query_set_duration (query, req_fmt, val);
1523       }
1524       break;
1525     }
1526     case GST_QUERY_FORMATS:
1527     {
1528       gst_query_set_formats (query, 2, GST_FORMAT_TIME, GST_FORMAT_BYTES);
1529       res = TRUE;
1530       break;
1531     }
1532     case GST_QUERY_CONVERT:
1533     {
1534       GstFormat src_fmt, dest_fmt;
1535       gint64 src_val, dest_val;
1536
1537       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1538       if (!(res = gst_audio_encoded_audio_convert (&enc->priv->ctx.info,
1539                   enc->priv->bytes_out, enc->priv->samples_in, src_fmt, src_val,
1540                   &dest_fmt, &dest_val)))
1541         break;
1542       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1543       break;
1544     }
1545     case GST_QUERY_LATENCY:
1546     {
1547       if ((res = gst_pad_peer_query (enc->sinkpad, query))) {
1548         gboolean live;
1549         GstClockTime min_latency, max_latency;
1550
1551         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1552         GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1553             GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1554             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1555
1556         GST_OBJECT_LOCK (enc);
1557         /* add our latency */
1558         if (min_latency != -1)
1559           min_latency += enc->priv->ctx.min_latency;
1560         if (max_latency != -1)
1561           max_latency += enc->priv->ctx.max_latency;
1562         GST_OBJECT_UNLOCK (enc);
1563
1564         gst_query_set_latency (query, live, min_latency, max_latency);
1565       }
1566       break;
1567     }
1568     default:
1569       res = gst_pad_query_default (pad, query);
1570       break;
1571   }
1572
1573   gst_object_unref (peerpad);
1574   return res;
1575 }
1576
1577 static void
1578 gst_audio_encoder_set_property (GObject * object, guint prop_id,
1579     const GValue * value, GParamSpec * pspec)
1580 {
1581   GstAudioEncoder *enc;
1582
1583   enc = GST_AUDIO_ENCODER (object);
1584
1585   switch (prop_id) {
1586     case PROP_PERFECT_TS:
1587       if (enc->priv->granule && !g_value_get_boolean (value))
1588         GST_WARNING_OBJECT (enc, "perfect-timestamp can not be set FALSE "
1589             "while granule handling is enabled");
1590       else
1591         enc->priv->perfect_ts = g_value_get_boolean (value);
1592       break;
1593     case PROP_HARD_RESYNC:
1594       enc->priv->hard_resync = g_value_get_boolean (value);
1595       break;
1596     case PROP_TOLERANCE:
1597       enc->priv->tolerance = g_value_get_int64 (value);
1598       break;
1599     default:
1600       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1601       break;
1602   }
1603 }
1604
1605 static void
1606 gst_audio_encoder_get_property (GObject * object, guint prop_id,
1607     GValue * value, GParamSpec * pspec)
1608 {
1609   GstAudioEncoder *enc;
1610
1611   enc = GST_AUDIO_ENCODER (object);
1612
1613   switch (prop_id) {
1614     case PROP_PERFECT_TS:
1615       g_value_set_boolean (value, enc->priv->perfect_ts);
1616       break;
1617     case PROP_GRANULE:
1618       g_value_set_boolean (value, enc->priv->granule);
1619       break;
1620     case PROP_HARD_RESYNC:
1621       g_value_set_boolean (value, enc->priv->hard_resync);
1622       break;
1623     case PROP_TOLERANCE:
1624       g_value_set_int64 (value, enc->priv->tolerance);
1625       break;
1626     default:
1627       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1628       break;
1629   }
1630 }
1631
1632 static gboolean
1633 gst_audio_encoder_activate (GstAudioEncoder * enc, gboolean active)
1634 {
1635   GstAudioEncoderClass *klass;
1636   gboolean result = FALSE;
1637
1638   klass = GST_AUDIO_ENCODER_GET_CLASS (enc);
1639
1640   g_return_val_if_fail (!enc->priv->granule || enc->priv->perfect_ts, FALSE);
1641
1642   GST_DEBUG_OBJECT (enc, "activate %d", active);
1643
1644   if (active) {
1645
1646     if (enc->priv->tags)
1647       gst_tag_list_free (enc->priv->tags);
1648     enc->priv->tags = gst_tag_list_new ();
1649
1650     if (!enc->priv->active && klass->start)
1651       result = klass->start (enc);
1652   } else {
1653     /* We must make sure streaming has finished before resetting things
1654      * and calling the ::stop vfunc */
1655     GST_PAD_STREAM_LOCK (enc->sinkpad);
1656     GST_PAD_STREAM_UNLOCK (enc->sinkpad);
1657
1658     if (enc->priv->active && klass->stop)
1659       result = klass->stop (enc);
1660
1661     /* clean up */
1662     gst_audio_encoder_reset (enc, TRUE);
1663   }
1664   GST_DEBUG_OBJECT (enc, "activate return: %d", result);
1665   return result;
1666 }
1667
1668
1669 static gboolean
1670 gst_audio_encoder_sink_activate_push (GstPad * pad, gboolean active)
1671 {
1672   gboolean result = TRUE;
1673   GstAudioEncoder *enc;
1674
1675   enc = GST_AUDIO_ENCODER (gst_pad_get_parent (pad));
1676
1677   GST_DEBUG_OBJECT (enc, "sink activate push %d", active);
1678
1679   result = gst_audio_encoder_activate (enc, active);
1680
1681   if (result)
1682     enc->priv->active = active;
1683
1684   GST_DEBUG_OBJECT (enc, "sink activate push return: %d", result);
1685
1686   gst_object_unref (enc);
1687   return result;
1688 }
1689
1690 /**
1691  * gst_audio_encoder_get_audio_info:
1692  * @enc: a #GstAudioEncoder
1693  *
1694  * Returns: a #GstAudioInfo describing the input audio format
1695  *
1696  * Since: 0.10.36
1697  */
1698 GstAudioInfo *
1699 gst_audio_encoder_get_audio_info (GstAudioEncoder * enc)
1700 {
1701   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), NULL);
1702
1703   return &enc->priv->ctx.info;
1704 }
1705
1706 /**
1707  * gst_audio_encoder_set_frame_samples_min:
1708  * @enc: a #GstAudioEncoder
1709  * @num: number of samples per frame
1710  *
1711  * Sets number of samples (per channel) subclass needs to be handed,
1712  * at least or will be handed all available if 0.
1713  *
1714  * Since: 0.10.36
1715  */
1716 void
1717 gst_audio_encoder_set_frame_samples_min (GstAudioEncoder * enc, gint num)
1718 {
1719   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1720
1721   enc->priv->ctx.frame_samples_min = num;
1722 }
1723
1724 /**
1725  * gst_audio_encoder_get_frame_samples_min:
1726  * @enc: a #GstAudioEncoder
1727  *
1728  * Returns: currently minimum requested samples per frame
1729  *
1730  * Since: 0.10.36
1731  */
1732 gint
1733 gst_audio_encoder_get_frame_samples_min (GstAudioEncoder * enc)
1734 {
1735   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1736
1737   return enc->priv->ctx.frame_samples_min;
1738 }
1739
1740 /**
1741  * gst_audio_encoder_set_frame_samples_max:
1742  * @enc: a #GstAudioEncoder
1743  * @num: number of samples per frame
1744  *
1745  * Sets number of samples (per channel) subclass needs to be handed,
1746  * at most or will be handed all available if 0.
1747  *
1748  * Since: 0.10.36
1749  */
1750 void
1751 gst_audio_encoder_set_frame_samples_max (GstAudioEncoder * enc, gint num)
1752 {
1753   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1754
1755   enc->priv->ctx.frame_samples_max = num;
1756 }
1757
1758 /**
1759  * gst_audio_encoder_get_frame_samples_min:
1760  * @enc: a #GstAudioEncoder
1761  *
1762  * Returns: currently maximum requested samples per frame
1763  *
1764  * Since: 0.10.36
1765  */
1766 gint
1767 gst_audio_encoder_get_frame_samples_max (GstAudioEncoder * enc)
1768 {
1769   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1770
1771   return enc->priv->ctx.frame_samples_max;
1772 }
1773
1774 /**
1775  * gst_audio_encoder_set_frame_max:
1776  * @enc: a #GstAudioEncoder
1777  * @num: number of frames
1778  *
1779  * Sets max number of frames accepted at once (assumed minimally 1).
1780  * Requires @frame_samples_min and @frame_samples_max to be the equal.
1781  *
1782  * Since: 0.10.36
1783  */
1784 void
1785 gst_audio_encoder_set_frame_max (GstAudioEncoder * enc, gint num)
1786 {
1787   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1788
1789   enc->priv->ctx.frame_max = num;
1790 }
1791
1792 /**
1793  * gst_audio_encoder_get_frame_max:
1794  * @enc: a #GstAudioEncoder
1795  *
1796  * Returns: currently configured maximum handled frames
1797  *
1798  * Since: 0.10.36
1799  */
1800 gint
1801 gst_audio_encoder_get_frame_max (GstAudioEncoder * enc)
1802 {
1803   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1804
1805   return enc->priv->ctx.frame_max;
1806 }
1807
1808 /**
1809  * gst_audio_encoder_set_lookahead:
1810  * @enc: a #GstAudioEncoder
1811  * @num: lookahead
1812  *
1813  * Sets encoder lookahead (in units of input rate samples)
1814  *
1815  * Since: 0.10.36
1816  */
1817 void
1818 gst_audio_encoder_set_lookahead (GstAudioEncoder * enc, gint num)
1819 {
1820   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1821
1822   enc->priv->ctx.lookahead = num;
1823 }
1824
1825 /**
1826  * gst_audio_encoder_get_lookahead:
1827  * @enc: a #GstAudioEncoder
1828  *
1829  * Returns: currently configured encoder lookahead
1830  */
1831 gint
1832 gst_audio_encoder_get_lookahead (GstAudioEncoder * enc)
1833 {
1834   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
1835
1836   return enc->priv->ctx.lookahead;
1837 }
1838
1839 /**
1840  * gst_audio_encoder_set_latency:
1841  * @enc: a #GstAudioEncoder
1842  * @min: minimum latency
1843  * @max: maximum latency
1844  *
1845  * Sets encoder latency.
1846  *
1847  * Since: 0.10.36
1848  */
1849 void
1850 gst_audio_encoder_set_latency (GstAudioEncoder * enc,
1851     GstClockTime min, GstClockTime max)
1852 {
1853   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1854
1855   GST_OBJECT_LOCK (enc);
1856   enc->priv->ctx.min_latency = min;
1857   enc->priv->ctx.max_latency = max;
1858   GST_OBJECT_UNLOCK (enc);
1859 }
1860
1861 /**
1862  * gst_audio_encoder_get_latency:
1863  * @enc: a #GstAudioEncoder
1864  * @min: (out) (allow-none): a pointer to storage to hold minimum latency
1865  * @max: (out) (allow-none): a pointer to storage to hold maximum latency
1866  *
1867  * Sets the variables pointed to by @min and @max to the currently configured
1868  * latency.
1869  *
1870  * Since: 0.10.36
1871  */
1872 void
1873 gst_audio_encoder_get_latency (GstAudioEncoder * enc,
1874     GstClockTime * min, GstClockTime * max)
1875 {
1876   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1877
1878   GST_OBJECT_LOCK (enc);
1879   if (min)
1880     *min = enc->priv->ctx.min_latency;
1881   if (max)
1882     *max = enc->priv->ctx.max_latency;
1883   GST_OBJECT_UNLOCK (enc);
1884 }
1885
1886 /**
1887  * gst_audio_encoder_set_mark_granule:
1888  * @enc: a #GstAudioEncoder
1889  * @enabled: new state
1890  *
1891  * Enable or disable encoder granule handling.
1892  *
1893  * MT safe.
1894  *
1895  * Since: 0.10.36
1896  */
1897 void
1898 gst_audio_encoder_set_mark_granule (GstAudioEncoder * enc, gboolean enabled)
1899 {
1900   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1901
1902   GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1903
1904   GST_OBJECT_LOCK (enc);
1905   enc->priv->granule = enabled;
1906   GST_OBJECT_UNLOCK (enc);
1907 }
1908
1909 /**
1910  * gst_audio_encoder_get_mark_granule:
1911  * @enc: a #GstAudioEncoder
1912  *
1913  * Queries if the encoder will handle granule marking.
1914  *
1915  * Returns: TRUE if granule marking is enabled.
1916  *
1917  * MT safe.
1918  *
1919  * Since: 0.10.36
1920  */
1921 gboolean
1922 gst_audio_encoder_get_mark_granule (GstAudioEncoder * enc)
1923 {
1924   gboolean result;
1925
1926   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
1927
1928   GST_OBJECT_LOCK (enc);
1929   result = enc->priv->granule;
1930   GST_OBJECT_UNLOCK (enc);
1931
1932   return result;
1933 }
1934
1935 /**
1936  * gst_audio_encoder_set_perfect_timestamp:
1937  * @enc: a #GstAudioEncoder
1938  * @enabled: new state
1939  *
1940  * Enable or disable encoder perfect output timestamp preference.
1941  *
1942  * MT safe.
1943  *
1944  * Since: 0.10.36
1945  */
1946 void
1947 gst_audio_encoder_set_perfect_timestamp (GstAudioEncoder * enc,
1948     gboolean enabled)
1949 {
1950   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
1951
1952   GST_LOG_OBJECT (enc, "enabled: %d", enabled);
1953
1954   GST_OBJECT_LOCK (enc);
1955   enc->priv->perfect_ts = enabled;
1956   GST_OBJECT_UNLOCK (enc);
1957 }
1958
1959 /**
1960  * gst_audio_encoder_get_perfect_timestamp:
1961  * @enc: a #GstAudioEncoder
1962  *
1963  * Queries encoder perfect timestamp behaviour.
1964  *
1965  * Returns: TRUE if pefect timestamp setting enabled.
1966  *
1967  * MT safe.
1968  *
1969  * Since: 0.10.36
1970  */
1971 gboolean
1972 gst_audio_encoder_get_perfect_timestamp (GstAudioEncoder * enc)
1973 {
1974   gboolean result;
1975
1976   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
1977
1978   GST_OBJECT_LOCK (enc);
1979   result = enc->priv->perfect_ts;
1980   GST_OBJECT_UNLOCK (enc);
1981
1982   return result;
1983 }
1984
1985 /**
1986  * gst_audio_encoder_set_hard_sync:
1987  * @enc: a #GstAudioEncoder
1988  * @enabled: new state
1989  *
1990  * Sets encoder hard resync handling.
1991  *
1992  * MT safe.
1993  *
1994  * Since: 0.10.36
1995  */
1996 void
1997 gst_audio_encoder_set_hard_resync (GstAudioEncoder * enc, gboolean enabled)
1998 {
1999   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2000
2001   GST_LOG_OBJECT (enc, "enabled: %d", enabled);
2002
2003   GST_OBJECT_LOCK (enc);
2004   enc->priv->hard_resync = enabled;
2005   GST_OBJECT_UNLOCK (enc);
2006 }
2007
2008 /**
2009  * gst_audio_encoder_get_hard_sync:
2010  * @enc: a #GstAudioEncoder
2011  *
2012  * Queries encoder's hard resync setting.
2013  *
2014  * Returns: TRUE if hard resync is enabled.
2015  *
2016  * MT safe.
2017  *
2018  * Since: 0.10.36
2019  */
2020 gboolean
2021 gst_audio_encoder_get_hard_resync (GstAudioEncoder * enc)
2022 {
2023   gboolean result;
2024
2025   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), FALSE);
2026
2027   GST_OBJECT_LOCK (enc);
2028   result = enc->priv->hard_resync;
2029   GST_OBJECT_UNLOCK (enc);
2030
2031   return result;
2032 }
2033
2034 /**
2035  * gst_audio_encoder_set_tolerance:
2036  * @enc: a #GstAudioEncoder
2037  * @tolerance: new tolerance
2038  *
2039  * Configures encoder audio jitter tolerance threshold.
2040  *
2041  * MT safe.
2042  *
2043  * Since: 0.10.36
2044  */
2045 void
2046 gst_audio_encoder_set_tolerance (GstAudioEncoder * enc, gint64 tolerance)
2047 {
2048   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2049
2050   GST_OBJECT_LOCK (enc);
2051   enc->priv->tolerance = tolerance;
2052   GST_OBJECT_UNLOCK (enc);
2053 }
2054
2055 /**
2056  * gst_audio_encoder_get_tolerance:
2057  * @enc: a #GstAudioEncoder
2058  *
2059  * Queries current audio jitter tolerance threshold.
2060  *
2061  * Returns: encoder audio jitter tolerance threshold.
2062  *
2063  * MT safe.
2064  *
2065  * Since: 0.10.36
2066  */
2067 gint64
2068 gst_audio_encoder_get_tolerance (GstAudioEncoder * enc)
2069 {
2070   gint64 result;
2071
2072   g_return_val_if_fail (GST_IS_AUDIO_ENCODER (enc), 0);
2073
2074   GST_OBJECT_LOCK (enc);
2075   result = enc->priv->tolerance;
2076   GST_OBJECT_UNLOCK (enc);
2077
2078   return result;
2079 }
2080
2081 /**
2082  * gst_audio_encoder_merge_tags:
2083  * @enc: a #GstAudioEncoder
2084  * @tags: a #GstTagList to merge
2085  * @mode: the #GstTagMergeMode to use
2086  *
2087  * Adds tags to so-called pending tags, which will be processed
2088  * before pushing out data downstream.
2089  *
2090  * Note that this is provided for convenience, and the subclass is
2091  * not required to use this and can still do tag handling on its own,
2092  * although it should be aware that baseclass already takes care
2093  * of the usual CODEC/AUDIO_CODEC tags.
2094  *
2095  * MT safe.
2096  *
2097  * Since: 0.10.36
2098  */
2099 void
2100 gst_audio_encoder_merge_tags (GstAudioEncoder * enc,
2101     const GstTagList * tags, GstTagMergeMode mode)
2102 {
2103   GstTagList *otags;
2104
2105   g_return_if_fail (GST_IS_AUDIO_ENCODER (enc));
2106   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2107
2108   GST_OBJECT_LOCK (enc);
2109   if (tags)
2110     GST_DEBUG_OBJECT (enc, "merging tags %" GST_PTR_FORMAT, tags);
2111   otags = enc->priv->tags;
2112   enc->priv->tags = gst_tag_list_merge (enc->priv->tags, tags, mode);
2113   if (otags)
2114     gst_tag_list_free (otags);
2115   GST_OBJECT_UNLOCK (enc);
2116 }