2002-03-20 21:45:03 +00:00
|
|
|
/* GStreamer
|
2001-12-22 23:43:34 +00:00
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2001-12-22 23:43:34 +00:00
|
|
|
*/
|
2009-06-27 20:23:02 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstaudio
|
|
|
|
* @short_description: Support library for audio elements
|
|
|
|
*
|
|
|
|
* This library contains some helper functions for audio elements.
|
|
|
|
*/
|
2001-12-22 23:43:34 +00:00
|
|
|
|
2003-10-31 20:03:29 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-18 17:15:03 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2002-04-13 00:46:23 +00:00
|
|
|
#include "audio.h"
|
2008-12-31 11:20:26 +00:00
|
|
|
#include "audio-enumtypes.h"
|
2001-12-22 23:43:34 +00:00
|
|
|
|
2015-11-06 16:29:22 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
#define GST_CAT_DEFAULT ensure_debug_category()
|
|
|
|
static GstDebugCategory *
|
|
|
|
ensure_debug_category (void)
|
|
|
|
{
|
|
|
|
static gsize cat_gonce = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&cat_gonce)) {
|
|
|
|
gsize cat_done;
|
|
|
|
|
|
|
|
cat_done = (gsize) _gst_debug_category_new ("audio", 0, "audio library");
|
|
|
|
|
|
|
|
g_once_init_leave (&cat_gonce, cat_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (GstDebugCategory *) cat_gonce;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define ensure_debug_category() /* NOOP */
|
|
|
|
#endif /* GST_DISABLE_GST_DEBUG */
|
|
|
|
|
|
|
|
|
2007-07-23 18:26:09 +00:00
|
|
|
/**
|
|
|
|
* gst_audio_buffer_clip:
|
2012-09-13 21:11:56 +00:00
|
|
|
* @buffer: (transfer full): The buffer to clip.
|
2011-08-20 08:49:17 +00:00
|
|
|
* @segment: Segment in %GST_FORMAT_TIME or %GST_FORMAT_DEFAULT to which
|
|
|
|
* the buffer should be clipped.
|
2007-07-23 18:26:09 +00:00
|
|
|
* @rate: sample rate.
|
2011-08-20 08:49:17 +00:00
|
|
|
* @bpf: size of one audio frame in bytes. This is the size of one sample
|
|
|
|
* * channels.
|
2007-07-23 18:26:09 +00:00
|
|
|
*
|
2011-09-13 19:10:43 +00:00
|
|
|
* Clip the buffer to the given %GstSegment.
|
2007-07-23 18:26:09 +00:00
|
|
|
*
|
2011-08-20 08:49:17 +00:00
|
|
|
* After calling this function the caller does not own a reference to
|
2007-08-22 15:29:04 +00:00
|
|
|
* @buffer anymore.
|
2007-07-23 18:26:09 +00:00
|
|
|
*
|
2012-09-13 21:11:56 +00:00
|
|
|
* Returns: (transfer full): %NULL if the buffer is completely outside the configured segment,
|
2007-07-23 18:26:09 +00:00
|
|
|
* otherwise the clipped buffer is returned.
|
|
|
|
*
|
2007-07-27 17:10:47 +00:00
|
|
|
* If the buffer has no timestamp, it is assumed to be inside the segment and
|
2011-08-20 08:49:17 +00:00
|
|
|
* is not clipped
|
2007-07-23 18:26:09 +00:00
|
|
|
*/
|
|
|
|
GstBuffer *
|
|
|
|
gst_audio_buffer_clip (GstBuffer * buffer, GstSegment * segment, gint rate,
|
2011-08-20 08:49:17 +00:00
|
|
|
gint bpf)
|
2007-07-23 18:26:09 +00:00
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
|
|
|
GstClockTime timestamp = GST_CLOCK_TIME_NONE, duration = GST_CLOCK_TIME_NONE;
|
|
|
|
guint64 offset = GST_BUFFER_OFFSET_NONE, offset_end = GST_BUFFER_OFFSET_NONE;
|
2012-03-09 15:17:54 +00:00
|
|
|
gsize trim, size, osize;
|
2007-07-23 18:26:09 +00:00
|
|
|
gboolean change_duration = TRUE, change_offset = TRUE, change_offset_end =
|
|
|
|
TRUE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (segment->format == GST_FORMAT_TIME ||
|
|
|
|
segment->format == GST_FORMAT_DEFAULT, buffer);
|
|
|
|
g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
|
2007-07-27 17:10:47 +00:00
|
|
|
|
|
|
|
if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
|
|
|
|
/* No timestamp - assume the buffer is completely in the segment */
|
|
|
|
return buffer;
|
2007-07-23 18:26:09 +00:00
|
|
|
|
2013-12-20 21:54:39 +00:00
|
|
|
/* Get copies of the buffer metadata to change later.
|
2007-07-23 18:26:09 +00:00
|
|
|
* Calculate the missing values for the calculations,
|
|
|
|
* they won't be changed later though. */
|
|
|
|
|
2011-03-27 11:55:15 +00:00
|
|
|
trim = 0;
|
2012-03-09 15:17:54 +00:00
|
|
|
osize = size = gst_buffer_get_size (buffer);
|
2007-07-23 18:26:09 +00:00
|
|
|
|
2012-10-15 16:47:30 +00:00
|
|
|
/* no data, nothing to clip */
|
|
|
|
if (!size)
|
|
|
|
return buffer;
|
|
|
|
|
2007-07-23 18:26:09 +00:00
|
|
|
timestamp = GST_BUFFER_TIMESTAMP (buffer);
|
2011-03-28 17:23:38 +00:00
|
|
|
GST_DEBUG ("timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
|
2007-07-23 18:26:09 +00:00
|
|
|
if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
|
|
|
|
duration = GST_BUFFER_DURATION (buffer);
|
|
|
|
} else {
|
|
|
|
change_duration = FALSE;
|
2011-08-20 08:49:17 +00:00
|
|
|
duration = gst_util_uint64_scale (size / bpf, GST_SECOND, rate);
|
2007-07-23 18:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
|
|
|
|
offset = GST_BUFFER_OFFSET (buffer);
|
|
|
|
} else {
|
|
|
|
change_offset = FALSE;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
|
|
|
|
offset_end = GST_BUFFER_OFFSET_END (buffer);
|
|
|
|
} else {
|
|
|
|
change_offset_end = FALSE;
|
2011-08-20 08:49:17 +00:00
|
|
|
offset_end = offset + size / bpf;
|
2007-07-23 18:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (segment->format == GST_FORMAT_TIME) {
|
|
|
|
/* Handle clipping for GST_FORMAT_TIME */
|
|
|
|
|
2011-05-16 11:48:11 +00:00
|
|
|
guint64 start, stop, cstart, cstop, diff;
|
2007-07-23 18:26:09 +00:00
|
|
|
|
|
|
|
start = timestamp;
|
|
|
|
stop = timestamp + duration;
|
|
|
|
|
|
|
|
if (gst_segment_clip (segment, GST_FORMAT_TIME,
|
|
|
|
start, stop, &cstart, &cstop)) {
|
|
|
|
|
|
|
|
diff = cstart - start;
|
|
|
|
if (diff > 0) {
|
|
|
|
timestamp = cstart;
|
|
|
|
|
|
|
|
if (change_duration)
|
|
|
|
duration -= diff;
|
|
|
|
|
|
|
|
diff = gst_util_uint64_scale (diff, rate, GST_SECOND);
|
|
|
|
if (change_offset)
|
|
|
|
offset += diff;
|
2011-08-20 08:49:17 +00:00
|
|
|
trim += diff * bpf;
|
|
|
|
size -= diff * bpf;
|
2007-07-23 18:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
diff = stop - cstop;
|
|
|
|
if (diff > 0) {
|
|
|
|
/* duration is always valid if stop is valid */
|
|
|
|
duration -= diff;
|
|
|
|
|
|
|
|
diff = gst_util_uint64_scale (diff, rate, GST_SECOND);
|
|
|
|
if (change_offset_end)
|
|
|
|
offset_end -= diff;
|
2011-08-20 08:49:17 +00:00
|
|
|
size -= diff * bpf;
|
2007-07-23 18:26:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Handle clipping for GST_FORMAT_DEFAULT */
|
2011-05-16 11:48:11 +00:00
|
|
|
guint64 start, stop, cstart, cstop, diff;
|
2007-07-23 18:26:09 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_BUFFER_OFFSET_IS_VALID (buffer), buffer);
|
|
|
|
|
|
|
|
start = offset;
|
|
|
|
stop = offset_end;
|
|
|
|
|
|
|
|
if (gst_segment_clip (segment, GST_FORMAT_DEFAULT,
|
|
|
|
start, stop, &cstart, &cstop)) {
|
|
|
|
|
|
|
|
diff = cstart - start;
|
|
|
|
if (diff > 0) {
|
|
|
|
offset = cstart;
|
|
|
|
|
|
|
|
timestamp = gst_util_uint64_scale (cstart, GST_SECOND, rate);
|
|
|
|
|
|
|
|
if (change_duration)
|
|
|
|
duration -= gst_util_uint64_scale (diff, GST_SECOND, rate);
|
|
|
|
|
2011-08-20 08:49:17 +00:00
|
|
|
trim += diff * bpf;
|
|
|
|
size -= diff * bpf;
|
2007-07-23 18:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
diff = stop - cstop;
|
|
|
|
if (diff > 0) {
|
|
|
|
offset_end = cstop;
|
|
|
|
|
|
|
|
if (change_duration)
|
|
|
|
duration -= gst_util_uint64_scale (diff, GST_SECOND, rate);
|
|
|
|
|
2011-08-20 08:49:17 +00:00
|
|
|
size -= diff * bpf;
|
2007-07-23 18:26:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 15:17:54 +00:00
|
|
|
if (trim == 0 && size == osize) {
|
|
|
|
ret = buffer;
|
2013-09-27 20:40:28 +00:00
|
|
|
|
|
|
|
if (GST_BUFFER_TIMESTAMP (ret) != timestamp) {
|
|
|
|
ret = gst_buffer_make_writable (ret);
|
|
|
|
GST_BUFFER_TIMESTAMP (ret) = timestamp;
|
|
|
|
}
|
|
|
|
if (GST_BUFFER_DURATION (ret) != duration) {
|
|
|
|
ret = gst_buffer_make_writable (ret);
|
|
|
|
GST_BUFFER_DURATION (ret) = duration;
|
|
|
|
}
|
2012-03-09 15:17:54 +00:00
|
|
|
} else {
|
|
|
|
/* Get a writable buffer and apply all changes */
|
|
|
|
GST_DEBUG ("trim %" G_GSIZE_FORMAT " size %" G_GSIZE_FORMAT, trim, size);
|
|
|
|
ret = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, trim, size);
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
|
|
|
|
GST_DEBUG ("timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
|
|
|
|
GST_BUFFER_TIMESTAMP (ret) = timestamp;
|
|
|
|
|
|
|
|
if (change_duration)
|
|
|
|
GST_BUFFER_DURATION (ret) = duration;
|
|
|
|
if (change_offset)
|
|
|
|
GST_BUFFER_OFFSET (ret) = offset;
|
|
|
|
if (change_offset_end)
|
|
|
|
GST_BUFFER_OFFSET_END (ret) = offset_end;
|
|
|
|
}
|
2007-07-23 18:26:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|