1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-audiorate
22 * @title: audiorate
23 * @see_also: #GstVideoRate
24 *
25 * This element takes an incoming stream of timestamped raw audio frames and
26 * produces a perfect stream by inserting or dropping samples as needed.
27 *
28 * This operation may be of use to link to elements that require or otherwise
29 * implicitly assume a perfect stream as they do not store timestamps,
30 * but derive this by some means (e.g. bitrate for some AVI cases).
31 *
32 * The properties #GstAudioRate:in, #GstAudioRate:out, #GstAudioRate:add
33 * and #GstAudioRate:drop can be read to obtain information about number of
34 * input samples, output samples, dropped samples (i.e. the number of unused
35 * input samples) and inserted samples (i.e. the number of samples added to
36 * stream).
37 *
38 * When the #GstAudioRate:silent property is set to FALSE, a GObject property
39 * notification will be emitted whenever one of the #GstAudioRate:add or
40 * #GstAudioRate:drop values changes.
41 * This can potentially cause performance degradation.
42 * Note that property notification will happen from the streaming thread, so
43 * applications should be prepared for this.
44 *
45 * If the #GstAudioRate:tolerance property is non-zero, and an incoming buffer's
46 * timestamp deviates less than the property indicates from what would make a
47 * 'perfect time', then no samples will be added or dropped.
48 * Note that the output is still guaranteed to be a perfect stream, which means
49 * that the incoming data is then simply shifted (by less than the indicated
50 * tolerance) to a perfect time.
51 *
52 * ## Example pipelines
53 * |[
54 * gst-launch-1.0 -v autoaudiosrc ! audiorate ! audioconvert ! wavenc ! filesink location=alsa.wav
55 * ]|
56 * Capture audio from the sound card and turn it into a perfect stream
57 * for saving in a raw audio file.
58 * |[
59 * gst-launch-1.0 -v uridecodebin uri=file:///path/to/audio.file ! audiorate ! audioconvert ! wavenc ! filesink location=alsa.wav
60 * ]|
61 * Decodes an audio file and transforms it into a perfect stream for saving
62 * in a raw audio WAV file. Without the audio rate, the timing might not be
63 * preserved correctly in the WAV file in case the decoded stream is jittery
64 * or there are samples missing.
65 *
66 */
67
68 #ifdef HAVE_CONFIG_H
69 #include "config.h"
70 #endif
71
72 #include <string.h>
73 #include <stdlib.h>
74
75 #include "gstaudiorate.h"
76
77 #define GST_CAT_DEFAULT audio_rate_debug
78 GST_DEBUG_CATEGORY_STATIC (audio_rate_debug);
79
80 /* GstAudioRate signals and args */
81 enum
82 {
83 /* FILL ME */
84 LAST_SIGNAL
85 };
86
87 #define DEFAULT_SILENT TRUE
88 #define DEFAULT_TOLERANCE (40 * GST_MSECOND)
89 #define DEFAULT_SKIP_TO_FIRST FALSE
90
91 enum
92 {
93 PROP_0,
94 PROP_IN,
95 PROP_OUT,
96 PROP_ADD,
97 PROP_DROP,
98 PROP_SILENT,
99 PROP_TOLERANCE,
100 PROP_SKIP_TO_FIRST
101 };
102
103 static GstStaticPadTemplate gst_audio_rate_src_template =
104 GST_STATIC_PAD_TEMPLATE ("src",
105 GST_PAD_SRC,
106 GST_PAD_ALWAYS,
107 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL)
108 ", layout = (string) { interleaved, non-interleaved }")
109 );
110
111 static GstStaticPadTemplate gst_audio_rate_sink_template =
112 GST_STATIC_PAD_TEMPLATE ("sink",
113 GST_PAD_SINK,
114 GST_PAD_ALWAYS,
115 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL)
116 ", layout = (string) { interleaved, non-interleaved }")
117 );
118
119 static gboolean gst_audio_rate_sink_event (GstPad * pad, GstObject * parent,
120 GstEvent * event);
121 static gboolean gst_audio_rate_src_event (GstPad * pad, GstObject * parent,
122 GstEvent * event);
123 static GstFlowReturn gst_audio_rate_chain (GstPad * pad, GstObject * parent,
124 GstBuffer * buf);
125
126 static void gst_audio_rate_set_property (GObject * object,
127 guint prop_id, const GValue * value, GParamSpec * pspec);
128 static void gst_audio_rate_get_property (GObject * object,
129 guint prop_id, GValue * value, GParamSpec * pspec);
130
131 static GstStateChangeReturn gst_audio_rate_change_state (GstElement * element,
132 GstStateChange transition);
133
134 /*static guint gst_audio_rate_signals[LAST_SIGNAL] = { 0 }; */
135
136 static GParamSpec *pspec_drop = NULL;
137 static GParamSpec *pspec_add = NULL;
138
139 #define gst_audio_rate_parent_class parent_class
140 G_DEFINE_TYPE (GstAudioRate, gst_audio_rate, GST_TYPE_ELEMENT);
141 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (audiorate, "audiorate", GST_RANK_NONE,
142 GST_TYPE_AUDIO_RATE, GST_DEBUG_CATEGORY_INIT (audio_rate_debug, "audiorate",
143 0, "AudioRate stream fixer"));
144
145 static void
gst_audio_rate_class_init(GstAudioRateClass * klass)146 gst_audio_rate_class_init (GstAudioRateClass * klass)
147 {
148 GObjectClass *object_class = G_OBJECT_CLASS (klass);
149 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
150
151 object_class->set_property = gst_audio_rate_set_property;
152 object_class->get_property = gst_audio_rate_get_property;
153
154 g_object_class_install_property (object_class, PROP_IN,
155 g_param_spec_uint64 ("in", "In",
156 "Number of input samples", 0, G_MAXUINT64, 0,
157 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
158 g_object_class_install_property (object_class, PROP_OUT,
159 g_param_spec_uint64 ("out", "Out", "Number of output samples", 0,
160 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
161 pspec_add = g_param_spec_uint64 ("add", "Add", "Number of added samples",
162 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
163 g_object_class_install_property (object_class, PROP_ADD, pspec_add);
164 pspec_drop = g_param_spec_uint64 ("drop", "Drop", "Number of dropped samples",
165 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
166 g_object_class_install_property (object_class, PROP_DROP, pspec_drop);
167 g_object_class_install_property (object_class, PROP_SILENT,
168 g_param_spec_boolean ("silent", "silent",
169 "Don't emit notify for dropped and duplicated frames", DEFAULT_SILENT,
170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 /**
172 * GstAudioRate:tolerance:
173 *
174 * The difference between incoming timestamp and next timestamp must exceed
175 * the given value for audiorate to add or drop samples.
176 */
177 g_object_class_install_property (object_class, PROP_TOLERANCE,
178 g_param_spec_uint64 ("tolerance", "tolerance",
179 "Only act if timestamp jitter/imperfection exceeds indicated tolerance (ns)",
180 0, G_MAXUINT64, DEFAULT_TOLERANCE,
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182
183 /**
184 * GstAudioRate:skip-to-first:
185 *
186 * Don't produce buffers before the first one we receive.
187 */
188 g_object_class_install_property (object_class, PROP_SKIP_TO_FIRST,
189 g_param_spec_boolean ("skip-to-first", "Skip to first buffer",
190 "Don't produce buffers before the first one we receive",
191 DEFAULT_SKIP_TO_FIRST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192
193 gst_element_class_set_static_metadata (element_class,
194 "Audio rate adjuster", "Filter/Effect/Audio",
195 "Drops/duplicates/adjusts timestamps on audio samples to make a perfect stream",
196 "Wim Taymans <wim@fluendo.com>");
197
198 gst_element_class_add_static_pad_template (element_class,
199 &gst_audio_rate_sink_template);
200 gst_element_class_add_static_pad_template (element_class,
201 &gst_audio_rate_src_template);
202
203 element_class->change_state = gst_audio_rate_change_state;
204 }
205
206 static void
gst_audio_rate_reset(GstAudioRate * audiorate)207 gst_audio_rate_reset (GstAudioRate * audiorate)
208 {
209 audiorate->next_offset = -1;
210 audiorate->next_ts = -1;
211 audiorate->discont = TRUE;
212 gst_segment_init (&audiorate->sink_segment, GST_FORMAT_UNDEFINED);
213 gst_segment_init (&audiorate->src_segment, GST_FORMAT_TIME);
214
215 GST_DEBUG_OBJECT (audiorate, "handle reset");
216 }
217
218 static gboolean
gst_audio_rate_setcaps(GstAudioRate * audiorate,GstCaps * caps)219 gst_audio_rate_setcaps (GstAudioRate * audiorate, GstCaps * caps)
220 {
221 GstAudioInfo info;
222 gint prev_rate = 0;
223
224 if (!gst_audio_info_from_caps (&info, caps))
225 goto wrong_caps;
226
227 prev_rate = audiorate->info.rate;
228 audiorate->info = info;
229
230 if (audiorate->next_offset >= 0 && prev_rate > 0 && prev_rate != info.rate) {
231 GST_DEBUG_OBJECT (audiorate,
232 "rate changed from %d to %d", prev_rate, info.rate);
233
234 /* calculate next_offset based on new rate value */
235 audiorate->next_offset =
236 gst_util_uint64_scale_int_round (audiorate->next_ts,
237 info.rate, GST_SECOND);
238 }
239
240 return TRUE;
241
242 /* ERRORS */
243 wrong_caps:
244 {
245 GST_DEBUG_OBJECT (audiorate, "could not parse caps");
246 return FALSE;
247 }
248 }
249
250 static void
gst_audio_rate_init(GstAudioRate * audiorate)251 gst_audio_rate_init (GstAudioRate * audiorate)
252 {
253 audiorate->sinkpad =
254 gst_pad_new_from_static_template (&gst_audio_rate_sink_template, "sink");
255 gst_pad_set_event_function (audiorate->sinkpad, gst_audio_rate_sink_event);
256 gst_pad_set_chain_function (audiorate->sinkpad, gst_audio_rate_chain);
257 GST_PAD_SET_PROXY_CAPS (audiorate->sinkpad);
258 gst_element_add_pad (GST_ELEMENT (audiorate), audiorate->sinkpad);
259
260 audiorate->srcpad =
261 gst_pad_new_from_static_template (&gst_audio_rate_src_template, "src");
262 gst_pad_set_event_function (audiorate->srcpad, gst_audio_rate_src_event);
263 GST_PAD_SET_PROXY_CAPS (audiorate->srcpad);
264 gst_element_add_pad (GST_ELEMENT (audiorate), audiorate->srcpad);
265
266 audiorate->in = 0;
267 audiorate->out = 0;
268 audiorate->drop = 0;
269 audiorate->add = 0;
270 audiorate->silent = DEFAULT_SILENT;
271 audiorate->tolerance = DEFAULT_TOLERANCE;
272 }
273
274 static void
gst_audio_rate_fill_to_time(GstAudioRate * audiorate,GstClockTime time)275 gst_audio_rate_fill_to_time (GstAudioRate * audiorate, GstClockTime time)
276 {
277 GstBuffer *buf;
278
279 GST_DEBUG_OBJECT (audiorate, "next_ts: %" GST_TIME_FORMAT
280 ", filling to %" GST_TIME_FORMAT, GST_TIME_ARGS (audiorate->next_ts),
281 GST_TIME_ARGS (time));
282
283 if (!GST_CLOCK_TIME_IS_VALID (time) ||
284 !GST_CLOCK_TIME_IS_VALID (audiorate->next_ts))
285 return;
286
287 /* feed an empty buffer to chain with the given timestamp,
288 * it will take care of filling */
289 buf = gst_buffer_new ();
290 GST_BUFFER_TIMESTAMP (buf) = time;
291 gst_audio_rate_chain (audiorate->sinkpad, GST_OBJECT_CAST (audiorate), buf);
292 }
293
294 static gboolean
gst_audio_rate_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)295 gst_audio_rate_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
296 {
297 gboolean res;
298 GstAudioRate *audiorate;
299
300 audiorate = GST_AUDIO_RATE (parent);
301
302 switch (GST_EVENT_TYPE (event)) {
303 case GST_EVENT_CAPS:
304 {
305 GstCaps *caps;
306
307 gst_event_parse_caps (event, &caps);
308 if ((res = gst_audio_rate_setcaps (audiorate, caps))) {
309 res = gst_pad_push_event (audiorate->srcpad, event);
310 } else {
311 gst_event_unref (event);
312 }
313 break;
314 }
315 case GST_EVENT_FLUSH_STOP:
316 GST_DEBUG_OBJECT (audiorate, "handling FLUSH_STOP");
317 gst_audio_rate_reset (audiorate);
318 res = gst_pad_push_event (audiorate->srcpad, event);
319 break;
320 case GST_EVENT_SEGMENT:
321 {
322 gst_event_copy_segment (event, &audiorate->sink_segment);
323
324 GST_DEBUG_OBJECT (audiorate, "handle NEWSEGMENT");
325 #if 0
326 /* FIXME: bad things will likely happen if rate < 0 ... */
327 if (!update) {
328 /* a new segment starts. We need to figure out what will be the next
329 * sample offset. We mark the offsets as invalid so that the _chain
330 * function will perform this calculation. */
331 gst_audio_rate_fill_to_time (audiorate, audiorate->src_segment.stop);
332 #endif
333 audiorate->next_offset = -1;
334 audiorate->next_ts = -1;
335 #if 0
336 } else {
337 gst_audio_rate_fill_to_time (audiorate, audiorate->src_segment.start);
338 }
339 #endif
340
341 GST_DEBUG_OBJECT (audiorate, "updated segment: %" GST_SEGMENT_FORMAT,
342 &audiorate->sink_segment);
343
344 if (audiorate->sink_segment.format == GST_FORMAT_TIME) {
345 /* TIME formats can be copied to src and forwarded */
346 res = gst_pad_push_event (audiorate->srcpad, event);
347 gst_segment_copy_into (&audiorate->sink_segment,
348 &audiorate->src_segment);
349 } else {
350 /* other formats will be handled in the _chain function */
351 gst_event_unref (event);
352 res = TRUE;
353 }
354 break;
355 }
356 case GST_EVENT_EOS:
357 /* Fill segment until the end */
358 if (GST_CLOCK_TIME_IS_VALID (audiorate->src_segment.stop))
359 gst_audio_rate_fill_to_time (audiorate, audiorate->src_segment.stop);
360 res = gst_pad_push_event (audiorate->srcpad, event);
361 break;
362 case GST_EVENT_GAP:
363 {
364 /* Fill until end of gap */
365 GstClockTime timestamp, duration;
366 gst_event_parse_gap (event, ×tamp, &duration);
367 gst_event_unref (event);
368 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
369 if (GST_CLOCK_TIME_IS_VALID (duration))
370 timestamp += duration;
371 gst_audio_rate_fill_to_time (audiorate, timestamp);
372 }
373 res = TRUE;
374 break;
375 }
376 default:
377 res = gst_pad_event_default (pad, parent, event);
378 break;
379 }
380
381 return res;
382 }
383
384 static gboolean
gst_audio_rate_src_event(GstPad * pad,GstObject * parent,GstEvent * event)385 gst_audio_rate_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
386 {
387 gboolean res;
388 GstAudioRate *audiorate;
389
390 audiorate = GST_AUDIO_RATE (parent);
391
392 switch (GST_EVENT_TYPE (event)) {
393 default:
394 res = gst_pad_push_event (audiorate->sinkpad, event);
395 break;
396 }
397
398 return res;
399 }
400
401 static gboolean
gst_audio_rate_convert(GstAudioRate * audiorate,GstFormat src_fmt,guint64 src_val,GstFormat dest_fmt,guint64 * dest_val)402 gst_audio_rate_convert (GstAudioRate * audiorate,
403 GstFormat src_fmt, guint64 src_val, GstFormat dest_fmt, guint64 * dest_val)
404 {
405 return gst_audio_info_convert (&audiorate->info, src_fmt, src_val, dest_fmt,
406 (gint64 *) dest_val);
407 }
408
409
410 static gboolean
gst_audio_rate_convert_segments(GstAudioRate * audiorate)411 gst_audio_rate_convert_segments (GstAudioRate * audiorate)
412 {
413 GstFormat src_fmt, dst_fmt;
414
415 src_fmt = audiorate->sink_segment.format;
416 dst_fmt = audiorate->src_segment.format;
417
418 #define CONVERT_VAL(field) gst_audio_rate_convert (audiorate, \
419 src_fmt, audiorate->sink_segment.field, \
420 dst_fmt, &audiorate->src_segment.field);
421
422 audiorate->sink_segment.rate = audiorate->src_segment.rate;
423 audiorate->sink_segment.flags = audiorate->src_segment.flags;
424 audiorate->sink_segment.applied_rate = audiorate->src_segment.applied_rate;
425 CONVERT_VAL (start);
426 CONVERT_VAL (stop);
427 CONVERT_VAL (time);
428 CONVERT_VAL (base);
429 CONVERT_VAL (position);
430 #undef CONVERT_VAL
431
432 return TRUE;
433 }
434
435 static void
gst_audio_rate_notify_drop(GstAudioRate * audiorate)436 gst_audio_rate_notify_drop (GstAudioRate * audiorate)
437 {
438 g_object_notify_by_pspec ((GObject *) audiorate, pspec_drop);
439 }
440
441 static void
gst_audio_rate_notify_add(GstAudioRate * audiorate)442 gst_audio_rate_notify_add (GstAudioRate * audiorate)
443 {
444 g_object_notify_by_pspec ((GObject *) audiorate, pspec_add);
445 }
446
447 static GstFlowReturn
gst_audio_rate_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)448 gst_audio_rate_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
449 {
450 GstAudioRate *audiorate;
451 GstClockTime in_time;
452 guint64 in_offset, in_offset_end, in_samples;
453 guint in_size;
454 GstFlowReturn ret = GST_FLOW_OK;
455 GstClockTimeDiff diff;
456 gint rate, bpf;
457 GstAudioMeta *meta;
458
459 audiorate = GST_AUDIO_RATE (parent);
460
461 rate = GST_AUDIO_INFO_RATE (&audiorate->info);
462 bpf = GST_AUDIO_INFO_BPF (&audiorate->info);
463
464 /* need to be negotiated now */
465 if (bpf == 0)
466 goto not_negotiated;
467
468 /* we have a new pending segment */
469 if (audiorate->next_offset == -1) {
470 gint64 pos;
471
472 /* update the TIME segment */
473 gst_audio_rate_convert_segments (audiorate);
474
475 /* first buffer, we are negotiated and we have a segment, calculate the
476 * current expected offsets based on the segment.start, which is the first
477 * media time of the segment and should match the media time of the first
478 * buffer in that segment, which is the offset expressed in DEFAULT units.
479 */
480 /* convert first timestamp of segment to sample position */
481 pos = gst_util_uint64_scale_int_round (audiorate->src_segment.start,
482 GST_AUDIO_INFO_RATE (&audiorate->info), GST_SECOND);
483
484 GST_DEBUG_OBJECT (audiorate, "resync to offset %" G_GINT64_FORMAT, pos);
485
486 /* resyncing is a discont */
487 audiorate->discont = TRUE;
488
489 audiorate->next_offset = pos;
490 audiorate->next_ts =
491 gst_util_uint64_scale_int_round (audiorate->next_offset, GST_SECOND,
492 GST_AUDIO_INFO_RATE (&audiorate->info));
493
494 if (audiorate->skip_to_first && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
495 GST_DEBUG_OBJECT (audiorate, "but skipping to first buffer instead");
496 pos = gst_util_uint64_scale_int_round (GST_BUFFER_TIMESTAMP (buf),
497 GST_AUDIO_INFO_RATE (&audiorate->info), GST_SECOND);
498 GST_DEBUG_OBJECT (audiorate, "so resync to offset %" G_GINT64_FORMAT,
499 pos);
500 audiorate->next_offset = pos;
501 audiorate->next_ts = GST_BUFFER_TIMESTAMP (buf);
502 }
503 }
504
505 in_time = GST_BUFFER_TIMESTAMP (buf);
506 if (in_time == GST_CLOCK_TIME_NONE) {
507 GST_DEBUG_OBJECT (audiorate, "no timestamp, using expected next time");
508 in_time = audiorate->next_ts;
509 }
510
511 meta = gst_buffer_get_audio_meta (buf);
512 in_size = gst_buffer_get_size (buf);
513 in_samples = meta ? meta->samples : in_size / bpf;
514 audiorate->in += in_samples;
515
516 /* calculate the buffer offset */
517 in_offset = gst_util_uint64_scale_int_round (in_time, rate, GST_SECOND);
518 in_offset_end = in_offset + in_samples;
519
520 GST_LOG_OBJECT (audiorate,
521 "in_time:%" GST_TIME_FORMAT ", in_duration:%" GST_TIME_FORMAT
522 ", in_size:%u, in_offset:%" G_GUINT64_FORMAT ", in_offset_end:%"
523 G_GUINT64_FORMAT ", ->next_offset:%" G_GUINT64_FORMAT ", ->next_ts:%"
524 GST_TIME_FORMAT, GST_TIME_ARGS (in_time),
525 GST_TIME_ARGS (GST_FRAMES_TO_CLOCK_TIME (in_samples, rate)),
526 in_size, in_offset, in_offset_end, audiorate->next_offset,
527 GST_TIME_ARGS (audiorate->next_ts));
528
529 diff = in_time - audiorate->next_ts;
530 if (diff <= (GstClockTimeDiff) audiorate->tolerance &&
531 diff >= (GstClockTimeDiff) - audiorate->tolerance) {
532 /* buffer time close enough to expected time,
533 * so produce a perfect stream by simply 'shifting'
534 * it to next ts and offset and sending */
535 GST_LOG_OBJECT (audiorate, "within tolerance %" GST_TIME_FORMAT,
536 GST_TIME_ARGS (audiorate->tolerance));
537 /* The outgoing buffer's offset will be set to ->next_offset, we also
538 * need to adjust the offset_end value accordingly */
539 in_offset_end = audiorate->next_offset + in_samples;
540 audiorate->out += in_samples;
541 goto send;
542 }
543
544 /* do we need to insert samples */
545 if (in_offset > audiorate->next_offset) {
546 GstBuffer *fill;
547 gint fillsize;
548 guint64 fillsamples;
549
550 /* We don't want to allocate a single unreasonably huge buffer - it might
551 be hundreds of megabytes. So, limit each output buffer to one second of
552 audio */
553 fillsamples = in_offset - audiorate->next_offset;
554
555 while (fillsamples > 0) {
556 guint64 cursamples = MIN (fillsamples, rate);
557 GstMapInfo fillmap;
558
559 fillsamples -= cursamples;
560 fillsize = cursamples * bpf;
561
562 fill = gst_buffer_new_and_alloc (fillsize);
563
564 gst_buffer_map (fill, &fillmap, GST_MAP_WRITE);
565 gst_audio_format_info_fill_silence (audiorate->info.finfo, fillmap.data,
566 fillmap.size);
567 gst_buffer_unmap (fill, &fillmap);
568
569 if (audiorate->info.layout == GST_AUDIO_LAYOUT_NON_INTERLEAVED) {
570 gst_buffer_add_audio_meta (fill, &audiorate->info, cursamples, NULL);
571 }
572
573 GST_DEBUG_OBJECT (audiorate, "inserting %" G_GUINT64_FORMAT " samples",
574 cursamples);
575
576 GST_BUFFER_OFFSET (fill) = audiorate->next_offset;
577 audiorate->next_offset += cursamples;
578 GST_BUFFER_OFFSET_END (fill) = audiorate->next_offset;
579
580 /* Use next timestamp, then calculate following timestamp based on
581 * offset to get duration. Necessary complexity to get 'perfect'
582 * streams */
583 GST_BUFFER_TIMESTAMP (fill) = audiorate->next_ts;
584 audiorate->next_ts =
585 gst_util_uint64_scale_int_round (audiorate->next_offset, GST_SECOND,
586 rate);
587 GST_BUFFER_DURATION (fill) =
588 audiorate->next_ts - GST_BUFFER_TIMESTAMP (fill);
589
590 /* we created this buffer to fill a gap */
591 GST_BUFFER_FLAG_SET (fill, GST_BUFFER_FLAG_GAP);
592 /* set discont if it's pending, this is mostly done for the first buffer
593 * and after a flushing seek */
594 if (audiorate->discont) {
595 GST_BUFFER_FLAG_SET (fill, GST_BUFFER_FLAG_DISCONT);
596 audiorate->discont = FALSE;
597 }
598
599 fill = gst_audio_buffer_clip (fill, &audiorate->src_segment, rate, bpf);
600 if (fill)
601 ret = gst_pad_push (audiorate->srcpad, fill);
602
603 if (ret != GST_FLOW_OK)
604 goto beach;
605 audiorate->out += cursamples;
606 audiorate->add += cursamples;
607
608 if (!audiorate->silent)
609 gst_audio_rate_notify_add (audiorate);
610 }
611
612 } else if (in_offset < audiorate->next_offset) {
613 /* need to remove samples */
614 if (in_offset_end <= audiorate->next_offset) {
615 guint64 drop = in_samples;
616
617 audiorate->drop += drop;
618
619 GST_DEBUG_OBJECT (audiorate, "dropping %" G_GUINT64_FORMAT " samples",
620 drop);
621
622 /* we can drop the buffer completely */
623 gst_buffer_unref (buf);
624 buf = NULL;
625
626 if (!audiorate->silent)
627 gst_audio_rate_notify_drop (audiorate);
628
629 goto beach;
630 } else {
631 guint64 truncsamples, leftsamples;
632
633 /* truncate buffer */
634 truncsamples = audiorate->next_offset - in_offset;
635 leftsamples = in_samples - truncsamples;
636
637 buf = gst_audio_buffer_truncate (buf, bpf, truncsamples, leftsamples);
638
639 audiorate->drop += truncsamples;
640 audiorate->out += leftsamples;
641 GST_DEBUG_OBJECT (audiorate, "truncating %" G_GUINT64_FORMAT " samples",
642 truncsamples);
643
644 if (!audiorate->silent)
645 gst_audio_rate_notify_drop (audiorate);
646 }
647 }
648
649 send:
650 if (gst_buffer_get_size (buf) == 0)
651 goto beach;
652
653 buf = gst_buffer_make_writable (buf);
654
655 /* Now calculate parameters for whichever buffer (either the original
656 * or truncated one) we're pushing. */
657 GST_BUFFER_OFFSET (buf) = audiorate->next_offset;
658 GST_BUFFER_OFFSET_END (buf) = in_offset_end;
659
660 GST_BUFFER_TIMESTAMP (buf) = audiorate->next_ts;
661 audiorate->next_ts = gst_util_uint64_scale_int_round (in_offset_end,
662 GST_SECOND, rate);
663 GST_BUFFER_DURATION (buf) = audiorate->next_ts - GST_BUFFER_TIMESTAMP (buf);
664
665 if (audiorate->discont) {
666 /* we need to output a discont buffer, do so now */
667 GST_DEBUG_OBJECT (audiorate, "marking DISCONT on output buffer");
668 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
669 audiorate->discont = FALSE;
670 } else if (GST_BUFFER_IS_DISCONT (buf)) {
671 /* else we make everything continuous so we can safely remove the DISCONT
672 * flag from the buffer if there was one */
673 GST_DEBUG_OBJECT (audiorate, "removing DISCONT from buffer");
674 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
675 }
676
677 buf = gst_audio_buffer_clip (buf, &audiorate->src_segment, rate, bpf);
678 if (buf) {
679 /* set last_stop on segment */
680 audiorate->src_segment.position =
681 GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
682
683 ret = gst_pad_push (audiorate->srcpad, buf);
684 }
685 buf = NULL;
686
687 audiorate->next_offset = in_offset_end;
688 beach:
689
690 if (buf)
691 gst_buffer_unref (buf);
692
693 return ret;
694
695 /* ERRORS */
696 not_negotiated:
697 {
698 gst_buffer_unref (buf);
699
700 GST_ELEMENT_ERROR (audiorate, STREAM, FORMAT,
701 (NULL), ("pipeline error, format was not negotiated"));
702 return GST_FLOW_NOT_NEGOTIATED;
703 }
704 }
705
706 static void
gst_audio_rate_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)707 gst_audio_rate_set_property (GObject * object,
708 guint prop_id, const GValue * value, GParamSpec * pspec)
709 {
710 GstAudioRate *audiorate = GST_AUDIO_RATE (object);
711
712 switch (prop_id) {
713 case PROP_SILENT:
714 audiorate->silent = g_value_get_boolean (value);
715 break;
716 case PROP_TOLERANCE:
717 audiorate->tolerance = g_value_get_uint64 (value);
718 break;
719 case PROP_SKIP_TO_FIRST:
720 audiorate->skip_to_first = g_value_get_boolean (value);
721 break;
722 default:
723 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
724 break;
725 }
726 }
727
728 static void
gst_audio_rate_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)729 gst_audio_rate_get_property (GObject * object,
730 guint prop_id, GValue * value, GParamSpec * pspec)
731 {
732 GstAudioRate *audiorate = GST_AUDIO_RATE (object);
733
734 switch (prop_id) {
735 case PROP_IN:
736 g_value_set_uint64 (value, audiorate->in);
737 break;
738 case PROP_OUT:
739 g_value_set_uint64 (value, audiorate->out);
740 break;
741 case PROP_ADD:
742 g_value_set_uint64 (value, audiorate->add);
743 break;
744 case PROP_DROP:
745 g_value_set_uint64 (value, audiorate->drop);
746 break;
747 case PROP_SILENT:
748 g_value_set_boolean (value, audiorate->silent);
749 break;
750 case PROP_TOLERANCE:
751 g_value_set_uint64 (value, audiorate->tolerance);
752 break;
753 case PROP_SKIP_TO_FIRST:
754 g_value_set_boolean (value, audiorate->skip_to_first);
755 break;
756 default:
757 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
758 break;
759 }
760 }
761
762 static GstStateChangeReturn
gst_audio_rate_change_state(GstElement * element,GstStateChange transition)763 gst_audio_rate_change_state (GstElement * element, GstStateChange transition)
764 {
765 GstAudioRate *audiorate = GST_AUDIO_RATE (element);
766
767 switch (transition) {
768 case GST_STATE_CHANGE_PAUSED_TO_READY:
769 break;
770 case GST_STATE_CHANGE_READY_TO_PAUSED:
771 audiorate->in = 0;
772 audiorate->out = 0;
773 audiorate->drop = 0;
774 audiorate->add = 0;
775 gst_audio_info_init (&audiorate->info);
776 gst_audio_rate_reset (audiorate);
777 break;
778 default:
779 break;
780 }
781
782 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
783 }
784
785 static gboolean
plugin_init(GstPlugin * plugin)786 plugin_init (GstPlugin * plugin)
787 {
788 return GST_ELEMENT_REGISTER (audiorate, plugin);
789 }
790
791 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
792 GST_VERSION_MINOR,
793 audiorate,
794 "Adjusts audio frames",
795 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
796