scaletempo: no need for a private struct

This commit is contained in:
Tim-Philipp Müller 2012-12-15 21:27:01 +00:00
parent 6703e41b53
commit b19122bac8
2 changed files with 247 additions and 262 deletions

View file

@ -113,92 +113,45 @@ static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
G_DEFINE_TYPE_WITH_CODE (GstScaletempo, gst_scaletempo, G_DEFINE_TYPE_WITH_CODE (GstScaletempo, gst_scaletempo,
GST_TYPE_BASE_TRANSFORM, DEBUG_INIT (0)); GST_TYPE_BASE_TRANSFORM, DEBUG_INIT (0));
struct _GstScaletempoPrivate
{
gdouble scale;
/* parameters */
guint ms_stride;
gdouble percent_overlap;
guint ms_search;
/* caps */
gboolean use_int;
guint samples_per_frame; /* AKA number of channels */
guint bytes_per_sample;
guint bytes_per_frame;
guint sample_rate;
/* stride */
gdouble frames_stride_scaled;
gdouble frames_stride_error;
guint bytes_stride;
gdouble bytes_stride_scaled;
guint bytes_queue_max;
guint bytes_queued;
guint bytes_to_slide;
gint8 *buf_queue;
/* overlap */
guint samples_overlap;
guint samples_standing;
guint bytes_overlap;
guint bytes_standing;
gpointer buf_overlap;
gpointer table_blend;
void (*output_overlap) (GstScaletempo * scaletempo, gpointer out_buf,
guint bytes_off);
/* best overlap */
guint frames_search;
gpointer buf_pre_corr;
gpointer table_window;
guint (*best_overlap_offset) (GstScaletempo * scaletempo);
/* gstreamer */
gint64 segment_start;
GstClockTime latency;
/* threads */
gboolean reinit_buffers;
};
#define GST_SCALETEMPO_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_SCALETEMPO, GstScaletempoPrivate))
static guint static guint
best_overlap_offset_float (GstScaletempo * scaletempo) best_overlap_offset_float (GstScaletempo * st)
{ {
GstScaletempoPrivate *p = scaletempo->priv;
gfloat *pw, *po, *ppc, *search_start; gfloat *pw, *po, *ppc, *search_start;
gfloat best_corr = G_MININT; gfloat best_corr = G_MININT;
guint best_off = 0; guint best_off = 0;
gint i, off; gint i, off;
pw = p->table_window; pw = st->table_window;
po = p->buf_overlap; po = st->buf_overlap;
po += p->samples_per_frame; po += st->samples_per_frame;
ppc = p->buf_pre_corr; ppc = st->buf_pre_corr;
for (i = p->samples_per_frame; i < p->samples_overlap; i++) { for (i = st->samples_per_frame; i < st->samples_overlap; i++) {
*ppc++ = *pw++ * *po++; *ppc++ = *pw++ * *po++;
} }
search_start = (gfloat *) p->buf_queue + p->samples_per_frame; search_start = (gfloat *) st->buf_queue + st->samples_per_frame;
for (off = 0; off < p->frames_search; off++) { for (off = 0; off < st->frames_search; off++) {
gfloat corr = 0; gfloat corr = 0;
gfloat *ps = search_start; gfloat *ps = search_start;
ppc = p->buf_pre_corr; ppc = st->buf_pre_corr;
for (i = p->samples_per_frame; i < p->samples_overlap; i++) { for (i = st->samples_per_frame; i < st->samples_overlap; i++) {
corr += *ppc++ * *ps++; corr += *ppc++ * *ps++;
} }
if (corr > best_corr) { if (corr > best_corr) {
best_corr = corr; best_corr = corr;
best_off = off; best_off = off;
} }
search_start += p->samples_per_frame; search_start += st->samples_per_frame;
} }
return best_off * p->bytes_per_frame; return best_off * st->bytes_per_frame;
} }
/* buffer padding for loop optimization: sizeof(gint32) * (loop_size - 1) */ /* buffer padding for loop optimization: sizeof(gint32) * (loop_size - 1) */
#define UNROLL_PADDING (4*3) #define UNROLL_PADDING (4*3)
static guint static guint
best_overlap_offset_s16 (GstScaletempo * scaletempo) best_overlap_offset_s16 (GstScaletempo * st)
{ {
GstScaletempoPrivate *p = scaletempo->priv;
gint32 *pw, *ppc; gint32 *pw, *ppc;
gint16 *po, *search_start; gint16 *po, *search_start;
gint64 best_corr = G_MININT64; gint64 best_corr = G_MININT64;
@ -206,22 +159,22 @@ best_overlap_offset_s16 (GstScaletempo * scaletempo)
guint off; guint off;
glong i; glong i;
pw = p->table_window; pw = st->table_window;
po = p->buf_overlap; po = st->buf_overlap;
po += p->samples_per_frame; po += st->samples_per_frame;
ppc = p->buf_pre_corr; ppc = st->buf_pre_corr;
for (i = p->samples_per_frame; i < p->samples_overlap; i++) { for (i = st->samples_per_frame; i < st->samples_overlap; i++) {
*ppc++ = (*pw++ * *po++) >> 15; *ppc++ = (*pw++ * *po++) >> 15;
} }
search_start = (gint16 *) p->buf_queue + p->samples_per_frame; search_start = (gint16 *) st->buf_queue + st->samples_per_frame;
for (off = 0; off < p->frames_search; off++) { for (off = 0; off < st->frames_search; off++) {
gint64 corr = 0; gint64 corr = 0;
gint16 *ps = search_start; gint16 *ps = search_start;
ppc = p->buf_pre_corr; ppc = st->buf_pre_corr;
ppc += p->samples_overlap - p->samples_per_frame; ppc += st->samples_overlap - st->samples_per_frame;
ps += p->samples_overlap - p->samples_per_frame; ps += st->samples_overlap - st->samples_per_frame;
i = -((glong) p->samples_overlap - (glong) p->samples_per_frame); i = -((glong) st->samples_overlap - (glong) st->samples_per_frame);
do { do {
corr += ppc[i + 0] * ps[i + 0]; corr += ppc[i + 0] * ps[i + 0];
corr += ppc[i + 1] * ps[i + 1]; corr += ppc[i + 1] * ps[i + 1];
@ -233,74 +186,71 @@ best_overlap_offset_s16 (GstScaletempo * scaletempo)
best_corr = corr; best_corr = corr;
best_off = off; best_off = off;
} }
search_start += p->samples_per_frame; search_start += st->samples_per_frame;
} }
return best_off * p->bytes_per_frame; return best_off * st->bytes_per_frame;
} }
static void static void
output_overlap_float (GstScaletempo * scaletempo, output_overlap_float (GstScaletempo * st, gpointer buf_out, guint bytes_off)
gpointer buf_out, guint bytes_off)
{ {
GstScaletempoPrivate *p = scaletempo->priv;
gfloat *pout = buf_out; gfloat *pout = buf_out;
gfloat *pb = p->table_blend; gfloat *pb = st->table_blend;
gfloat *po = p->buf_overlap; gfloat *po = st->buf_overlap;
gfloat *pin = (gfloat *) (p->buf_queue + bytes_off); gfloat *pin = (gfloat *) (st->buf_queue + bytes_off);
gint i; gint i;
for (i = 0; i < p->samples_overlap; i++) { for (i = 0; i < st->samples_overlap; i++) {
*pout++ = *po - *pb++ * (*po - *pin++); *pout++ = *po - *pb++ * (*po - *pin++);
po++; po++;
} }
} }
static void static void
output_overlap_s16 (GstScaletempo * scaletempo, output_overlap_s16 (GstScaletempo * st, gpointer buf_out, guint bytes_off)
gpointer buf_out, guint bytes_off)
{ {
GstScaletempoPrivate *p = scaletempo->priv;
gint16 *pout = buf_out; gint16 *pout = buf_out;
gint32 *pb = p->table_blend; gint32 *pb = st->table_blend;
gint16 *po = p->buf_overlap; gint16 *po = st->buf_overlap;
gint16 *pin = (gint16 *) (p->buf_queue + bytes_off); gint16 *pin = (gint16 *) (st->buf_queue + bytes_off);
gint i; gint i;
for (i = 0; i < p->samples_overlap; i++) { for (i = 0; i < st->samples_overlap; i++) {
*pout++ = *po - ((*pb++ * (*po - *pin++)) >> 16); *pout++ = *po - ((*pb++ * (*po - *pin++)) >> 16);
po++; po++;
} }
} }
static guint static guint
fill_queue (GstScaletempo * scaletempo, GstBuffer * buf_in, guint offset) fill_queue (GstScaletempo * st, GstBuffer * buf_in, guint offset)
{ {
GstScaletempoPrivate *p = scaletempo->priv;
guint bytes_in = gst_buffer_get_size (buf_in) - offset; guint bytes_in = gst_buffer_get_size (buf_in) - offset;
guint offset_unchanged = offset; guint offset_unchanged = offset;
GstMapInfo map; GstMapInfo map;
gst_buffer_map (buf_in, &map, GST_MAP_READ); gst_buffer_map (buf_in, &map, GST_MAP_READ);
if (p->bytes_to_slide > 0) { if (st->bytes_to_slide > 0) {
if (p->bytes_to_slide < p->bytes_queued) { if (st->bytes_to_slide < st->bytes_queued) {
guint bytes_in_move = p->bytes_queued - p->bytes_to_slide; guint bytes_in_move = st->bytes_queued - st->bytes_to_slide;
memmove (p->buf_queue, p->buf_queue + p->bytes_to_slide, bytes_in_move); memmove (st->buf_queue, st->buf_queue + st->bytes_to_slide,
p->bytes_to_slide = 0; bytes_in_move);
p->bytes_queued = bytes_in_move; st->bytes_to_slide = 0;
st->bytes_queued = bytes_in_move;
} else { } else {
guint bytes_in_skip; guint bytes_in_skip;
p->bytes_to_slide -= p->bytes_queued; st->bytes_to_slide -= st->bytes_queued;
bytes_in_skip = MIN (p->bytes_to_slide, bytes_in); bytes_in_skip = MIN (st->bytes_to_slide, bytes_in);
p->bytes_queued = 0; st->bytes_queued = 0;
p->bytes_to_slide -= bytes_in_skip; st->bytes_to_slide -= bytes_in_skip;
offset += bytes_in_skip; offset += bytes_in_skip;
bytes_in -= bytes_in_skip; bytes_in -= bytes_in_skip;
} }
} }
if (bytes_in > 0) { if (bytes_in > 0) {
guint bytes_in_copy = MIN (p->bytes_queue_max - p->bytes_queued, bytes_in); guint bytes_in_copy =
memcpy (p->buf_queue + p->bytes_queued, map.data + offset, bytes_in_copy); MIN (st->bytes_queue_max - st->bytes_queued, bytes_in);
p->bytes_queued += bytes_in_copy; memcpy (st->buf_queue + st->bytes_queued, map.data + offset, bytes_in_copy);
st->bytes_queued += bytes_in_copy;
offset += bytes_in_copy; offset += bytes_in_copy;
} }
gst_buffer_unmap (buf_in, &map); gst_buffer_unmap (buf_in, &map);
@ -309,136 +259,136 @@ fill_queue (GstScaletempo * scaletempo, GstBuffer * buf_in, guint offset)
} }
static void static void
reinit_buffers (GstScaletempo * scaletempo) reinit_buffers (GstScaletempo * st)
{ {
GstScaletempoPrivate *p = scaletempo->priv;
gint i, j; gint i, j;
guint frames_overlap; guint frames_overlap;
guint new_size; guint new_size;
GstClockTime latency; GstClockTime latency;
guint frames_stride = p->ms_stride * p->sample_rate / 1000.0; guint frames_stride = st->ms_stride * st->sample_rate / 1000.0;
p->bytes_stride = frames_stride * p->bytes_per_frame; st->bytes_stride = frames_stride * st->bytes_per_frame;
/* overlap */ /* overlap */
frames_overlap = frames_stride * p->percent_overlap; frames_overlap = frames_stride * st->percent_overlap;
if (frames_overlap < 1) { /* if no overlap */ if (frames_overlap < 1) { /* if no overlap */
p->bytes_overlap = 0; st->bytes_overlap = 0;
p->bytes_standing = p->bytes_stride; st->bytes_standing = st->bytes_stride;
p->samples_standing = p->bytes_standing / p->bytes_per_sample; st->samples_standing = st->bytes_standing / st->bytes_per_sample;
p->output_overlap = NULL; st->output_overlap = NULL;
} else { } else {
guint prev_overlap = p->bytes_overlap; guint prev_overlap = st->bytes_overlap;
p->bytes_overlap = frames_overlap * p->bytes_per_frame; st->bytes_overlap = frames_overlap * st->bytes_per_frame;
p->samples_overlap = frames_overlap * p->samples_per_frame; st->samples_overlap = frames_overlap * st->samples_per_frame;
p->bytes_standing = p->bytes_stride - p->bytes_overlap; st->bytes_standing = st->bytes_stride - st->bytes_overlap;
p->samples_standing = p->bytes_standing / p->bytes_per_sample; st->samples_standing = st->bytes_standing / st->bytes_per_sample;
p->buf_overlap = g_realloc (p->buf_overlap, p->bytes_overlap); st->buf_overlap = g_realloc (st->buf_overlap, st->bytes_overlap);
p->table_blend = g_realloc (p->table_blend, p->samples_overlap * 4); /* sizeof (gint32|gfloat) */ st->table_blend = g_realloc (st->table_blend, st->samples_overlap * 4); /* sizeof (gint32|gfloat) */
if (p->bytes_overlap > prev_overlap) { if (st->bytes_overlap > prev_overlap) {
memset ((guint8 *) p->buf_overlap + prev_overlap, 0, memset ((guint8 *) st->buf_overlap + prev_overlap, 0,
p->bytes_overlap - prev_overlap); st->bytes_overlap - prev_overlap);
} }
if (p->use_int) { if (st->use_int) {
gint32 *pb = p->table_blend; gint32 *pb = st->table_blend;
gint64 blend = 0; gint64 blend = 0;
for (i = 0; i < frames_overlap; i++) { for (i = 0; i < frames_overlap; i++) {
gint32 v = blend / frames_overlap; gint32 v = blend / frames_overlap;
for (j = 0; j < p->samples_per_frame; j++) { for (j = 0; j < st->samples_per_frame; j++) {
*pb++ = v; *pb++ = v;
} }
blend += 65535; /* 2^16 */ blend += 65535; /* 2^16 */
} }
p->output_overlap = output_overlap_s16; st->output_overlap = output_overlap_s16;
} else { } else {
gfloat *pb = p->table_blend; gfloat *pb = st->table_blend;
gfloat t = (gfloat) frames_overlap; gfloat t = (gfloat) frames_overlap;
for (i = 0; i < frames_overlap; i++) { for (i = 0; i < frames_overlap; i++) {
gfloat v = i / t; gfloat v = i / t;
for (j = 0; j < p->samples_per_frame; j++) { for (j = 0; j < st->samples_per_frame; j++) {
*pb++ = v; *pb++ = v;
} }
} }
p->output_overlap = output_overlap_float; st->output_overlap = output_overlap_float;
} }
} }
/* best overlap */ /* best overlap */
p->frames_search = st->frames_search =
(frames_overlap <= 1) ? 0 : p->ms_search * p->sample_rate / 1000.0; (frames_overlap <= 1) ? 0 : st->ms_search * st->sample_rate / 1000.0;
if (p->frames_search < 1) { /* if no search */ if (st->frames_search < 1) { /* if no search */
p->best_overlap_offset = NULL; st->best_overlap_offset = NULL;
} else { } else {
guint bytes_pre_corr = (p->samples_overlap - p->samples_per_frame) * 4; /* sizeof (gint32|gfloat) */ guint bytes_pre_corr = (st->samples_overlap - st->samples_per_frame) * 4; /* sizeof (gint32|gfloat) */
p->buf_pre_corr = st->buf_pre_corr =
g_realloc (p->buf_pre_corr, bytes_pre_corr + UNROLL_PADDING); g_realloc (st->buf_pre_corr, bytes_pre_corr + UNROLL_PADDING);
p->table_window = g_realloc (p->table_window, bytes_pre_corr); st->table_window = g_realloc (st->table_window, bytes_pre_corr);
if (p->use_int) { if (st->use_int) {
gint64 t = frames_overlap; gint64 t = frames_overlap;
gint32 n = 8589934588LL / (t * t); /* 4 * (2^31 - 1) / t^2 */ gint32 n = 8589934588LL / (t * t); /* 4 * (2^31 - 1) / t^2 */
gint32 *pw; gint32 *pw;
memset ((guint8 *) p->buf_pre_corr + bytes_pre_corr, 0, UNROLL_PADDING); memset ((guint8 *) st->buf_pre_corr + bytes_pre_corr, 0, UNROLL_PADDING);
pw = p->table_window; pw = st->table_window;
for (i = 1; i < frames_overlap; i++) { for (i = 1; i < frames_overlap; i++) {
gint32 v = (i * (t - i) * n) >> 15; gint32 v = (i * (t - i) * n) >> 15;
for (j = 0; j < p->samples_per_frame; j++) { for (j = 0; j < st->samples_per_frame; j++) {
*pw++ = v; *pw++ = v;
} }
} }
p->best_overlap_offset = best_overlap_offset_s16; st->best_overlap_offset = best_overlap_offset_s16;
} else { } else {
gfloat *pw = p->table_window; gfloat *pw = st->table_window;
for (i = 1; i < frames_overlap; i++) { for (i = 1; i < frames_overlap; i++) {
gfloat v = i * (frames_overlap - i); gfloat v = i * (frames_overlap - i);
for (j = 0; j < p->samples_per_frame; j++) { for (j = 0; j < st->samples_per_frame; j++) {
*pw++ = v; *pw++ = v;
} }
} }
p->best_overlap_offset = best_overlap_offset_float; st->best_overlap_offset = best_overlap_offset_float;
} }
} }
new_size = new_size =
(p->frames_search + frames_stride + frames_overlap) * p->bytes_per_frame; (st->frames_search + frames_stride +
if (p->bytes_queued > new_size) { frames_overlap) * st->bytes_per_frame;
if (p->bytes_to_slide > p->bytes_queued) { if (st->bytes_queued > new_size) {
p->bytes_to_slide -= p->bytes_queued; if (st->bytes_to_slide > st->bytes_queued) {
p->bytes_queued = 0; st->bytes_to_slide -= st->bytes_queued;
st->bytes_queued = 0;
} else { } else {
guint new_queued = MIN (p->bytes_queued - p->bytes_to_slide, new_size); guint new_queued = MIN (st->bytes_queued - st->bytes_to_slide, new_size);
memmove (p->buf_queue, memmove (st->buf_queue,
p->buf_queue + p->bytes_queued - new_queued, new_queued); st->buf_queue + st->bytes_queued - new_queued, new_queued);
p->bytes_to_slide = 0; st->bytes_to_slide = 0;
p->bytes_queued = new_queued; st->bytes_queued = new_queued;
} }
} }
p->bytes_queue_max = new_size; st->bytes_queue_max = new_size;
p->buf_queue = g_realloc (p->buf_queue, p->bytes_queue_max); st->buf_queue = g_realloc (st->buf_queue, st->bytes_queue_max);
latency = latency =
gst_util_uint64_scale (p->bytes_queue_max, GST_SECOND, gst_util_uint64_scale (st->bytes_queue_max, GST_SECOND,
p->bytes_per_frame * p->sample_rate); st->bytes_per_frame * st->sample_rate);
if (p->latency != latency) { if (st->latency != latency) {
p->latency = latency; st->latency = latency;
gst_element_post_message (GST_ELEMENT (scaletempo), gst_element_post_message (GST_ELEMENT (st),
gst_message_new_latency (GST_OBJECT (scaletempo))); gst_message_new_latency (GST_OBJECT (st)));
} }
p->bytes_stride_scaled = p->bytes_stride * p->scale; st->bytes_stride_scaled = st->bytes_stride * st->scale;
p->frames_stride_scaled = p->bytes_stride_scaled / p->bytes_per_frame; st->frames_stride_scaled = st->bytes_stride_scaled / st->bytes_per_frame;
GST_DEBUG GST_DEBUG
("%.3f scale, %.3f stride_in, %i stride_out, %i standing, %i overlap, %i search, %i queue, %s mode", ("%.3f scale, %.3f stride_in, %i stride_out, %i standing, %i overlap, %i search, %i queue, %s mode",
p->scale, p->frames_stride_scaled, st->scale, st->frames_stride_scaled,
(gint) (p->bytes_stride / p->bytes_per_frame), (gint) (st->bytes_stride / st->bytes_per_frame),
(gint) (p->bytes_standing / p->bytes_per_frame), (gint) (st->bytes_standing / st->bytes_per_frame),
(gint) (p->bytes_overlap / p->bytes_per_frame), p->frames_search, (gint) (st->bytes_overlap / st->bytes_per_frame), st->frames_search,
(gint) (p->bytes_queue_max / p->bytes_per_frame), (gint) (st->bytes_queue_max / st->bytes_per_frame),
(p->use_int ? "s16" : "float")); (st->use_int ? "s16" : "float"));
p->reinit_buffers = FALSE; st->reinit_buffers = FALSE;
} }
@ -447,8 +397,7 @@ static GstFlowReturn
gst_scaletempo_transform (GstBaseTransform * trans, gst_scaletempo_transform (GstBaseTransform * trans,
GstBuffer * inbuf, GstBuffer * outbuf) GstBuffer * inbuf, GstBuffer * outbuf)
{ {
GstScaletempo *scaletempo = GST_SCALETEMPO (trans); GstScaletempo *st = GST_SCALETEMPO (trans);
GstScaletempoPrivate *p = scaletempo->priv;
gint8 *pout; gint8 *pout;
guint offset_in, bytes_out; guint offset_in, bytes_out;
GstMapInfo omap; GstMapInfo omap;
@ -456,47 +405,47 @@ gst_scaletempo_transform (GstBaseTransform * trans,
gst_buffer_map (outbuf, &omap, GST_MAP_WRITE); gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
pout = (gint8 *) omap.data; pout = (gint8 *) omap.data;
offset_in = fill_queue (scaletempo, inbuf, 0); offset_in = fill_queue (st, inbuf, 0);
bytes_out = 0; bytes_out = 0;
while (p->bytes_queued >= p->bytes_queue_max) { while (st->bytes_queued >= st->bytes_queue_max) {
guint bytes_off = 0; guint bytes_off = 0;
gdouble frames_to_slide; gdouble frames_to_slide;
guint frames_to_stride_whole; guint frames_to_stride_whole;
/* output stride */ /* output stride */
if (p->output_overlap) { if (st->output_overlap) {
if (p->best_overlap_offset) { if (st->best_overlap_offset) {
bytes_off = p->best_overlap_offset (scaletempo); bytes_off = st->best_overlap_offset (st);
} }
p->output_overlap (scaletempo, pout, bytes_off); st->output_overlap (st, pout, bytes_off);
} }
memcpy (pout + p->bytes_overlap, memcpy (pout + st->bytes_overlap,
p->buf_queue + bytes_off + p->bytes_overlap, p->bytes_standing); st->buf_queue + bytes_off + st->bytes_overlap, st->bytes_standing);
pout += p->bytes_stride; pout += st->bytes_stride;
bytes_out += p->bytes_stride; bytes_out += st->bytes_stride;
/* input stride */ /* input stride */
memcpy (p->buf_overlap, memcpy (st->buf_overlap,
p->buf_queue + bytes_off + p->bytes_stride, p->bytes_overlap); st->buf_queue + bytes_off + st->bytes_stride, st->bytes_overlap);
frames_to_slide = p->frames_stride_scaled + p->frames_stride_error; frames_to_slide = st->frames_stride_scaled + st->frames_stride_error;
frames_to_stride_whole = (gint) frames_to_slide; frames_to_stride_whole = (gint) frames_to_slide;
p->bytes_to_slide = frames_to_stride_whole * p->bytes_per_frame; st->bytes_to_slide = frames_to_stride_whole * st->bytes_per_frame;
p->frames_stride_error = frames_to_slide - frames_to_stride_whole; st->frames_stride_error = frames_to_slide - frames_to_stride_whole;
offset_in += fill_queue (scaletempo, inbuf, offset_in); offset_in += fill_queue (st, inbuf, offset_in);
} }
gst_buffer_unmap (outbuf, &omap); gst_buffer_unmap (outbuf, &omap);
timestamp = GST_BUFFER_TIMESTAMP (inbuf) - p->segment_start; timestamp = GST_BUFFER_TIMESTAMP (inbuf) - st->segment_start;
if (timestamp < p->latency) if (timestamp < st->latency)
timestamp = 0; timestamp = 0;
else else
timestamp -= p->latency; timestamp -= st->latency;
GST_BUFFER_TIMESTAMP (outbuf) = timestamp / p->scale + p->segment_start; GST_BUFFER_TIMESTAMP (outbuf) = timestamp / st->scale + st->segment_start;
GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (outbuf) =
gst_util_uint64_scale (bytes_out, GST_SECOND, gst_util_uint64_scale (bytes_out, GST_SECOND,
p->bytes_per_frame * p->sample_rate); st->bytes_per_frame * st->sample_rate);
gst_buffer_set_size (outbuf, bytes_out); gst_buffer_set_size (outbuf, bytes_out);
return GST_FLOW_OK; return GST_FLOW_OK;
@ -509,21 +458,20 @@ gst_scaletempo_transform_size (GstBaseTransform * trans,
{ {
if (direction == GST_PAD_SINK) { if (direction == GST_PAD_SINK) {
GstScaletempo *scaletempo = GST_SCALETEMPO (trans); GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
GstScaletempoPrivate *priv = scaletempo->priv;
gint bytes_to_out; gint bytes_to_out;
if (priv->reinit_buffers) if (scaletempo->reinit_buffers)
reinit_buffers (scaletempo); reinit_buffers (scaletempo);
bytes_to_out = size + priv->bytes_queued - priv->bytes_to_slide; bytes_to_out = size + scaletempo->bytes_queued - scaletempo->bytes_to_slide;
if (bytes_to_out < (gint) priv->bytes_queue_max) { if (bytes_to_out < (gint) scaletempo->bytes_queue_max) {
*othersize = 0; *othersize = 0;
} else { } else {
/* while (total_buffered - stride_length * n >= queue_max) n++ */ /* while (total_buffered - stride_length * n >= queue_max) n++ */
*othersize = priv->bytes_stride * ((guint) ( *othersize = scaletempo->bytes_stride * ((guint) (
(bytes_to_out - priv->bytes_queue_max + (bytes_to_out - scaletempo->bytes_queue_max +
/* rounding protection */ priv->bytes_per_frame) /* rounding protection */ scaletempo->bytes_per_frame)
/ priv->bytes_stride_scaled) + 1); / scaletempo->bytes_stride_scaled) + 1);
} }
return TRUE; return TRUE;
@ -536,34 +484,34 @@ gst_scaletempo_sink_event (GstBaseTransform * trans, GstEvent * event)
{ {
if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) { if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
GstScaletempo *scaletempo = GST_SCALETEMPO (trans); GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
GstScaletempoPrivate *priv = scaletempo->priv;
GstSegment segment; GstSegment segment;
gst_event_copy_segment (event, &segment); gst_event_copy_segment (event, &segment);
if (priv->scale != segment.rate) { if (scaletempo->scale != segment.rate) {
if (ABS (segment.rate - 1.0) < 1e-10) { if (ABS (segment.rate - 1.0) < 1e-10) {
priv->scale = 1.0; scaletempo->scale = 1.0;
gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo), gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo),
TRUE); TRUE);
} else { } else {
gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo), gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo),
FALSE); FALSE);
priv->scale = segment.rate; scaletempo->scale = segment.rate;
priv->bytes_stride_scaled = priv->bytes_stride * priv->scale; scaletempo->bytes_stride_scaled =
priv->frames_stride_scaled = scaletempo->bytes_stride * scaletempo->scale;
priv->bytes_stride_scaled / priv->bytes_per_frame; scaletempo->frames_stride_scaled =
GST_DEBUG ("%.3f scale, %.3f stride_in, %i stride_out", priv->scale, scaletempo->bytes_stride_scaled / scaletempo->bytes_per_frame;
priv->frames_stride_scaled, GST_DEBUG ("%.3f scale, %.3f stride_in, %i stride_out",
(gint) (priv->bytes_stride / priv->bytes_per_frame)); scaletempo->scale, scaletempo->frames_stride_scaled,
(gint) (scaletempo->bytes_stride / scaletempo->bytes_per_frame));
priv->bytes_to_slide = 0; scaletempo->bytes_to_slide = 0;
} }
} }
if (priv->scale != 1.0) { if (scaletempo->scale != 1.0) {
priv->segment_start = segment.start; scaletempo->segment_start = segment.start;
segment.applied_rate = priv->scale; segment.applied_rate = scaletempo->scale;
segment.rate = 1.0; segment.rate = 1.0;
gst_event_unref (event); gst_event_unref (event);
@ -585,7 +533,6 @@ gst_scaletempo_set_caps (GstBaseTransform * trans,
GstCaps * incaps, GstCaps * outcaps) GstCaps * incaps, GstCaps * outcaps)
{ {
GstScaletempo *scaletempo = GST_SCALETEMPO (trans); GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
GstScaletempoPrivate *priv = scaletempo->priv;
gint width, bps, nch, rate; gint width, bps, nch, rate;
gboolean use_int; gboolean use_int;
@ -603,15 +550,16 @@ gst_scaletempo_set_caps (GstBaseTransform * trans,
GST_DEBUG ("caps: %" GST_PTR_FORMAT ", %d bps", incaps, bps); GST_DEBUG ("caps: %" GST_PTR_FORMAT ", %d bps", incaps, bps);
if (rate != priv->sample_rate if (rate != scaletempo->sample_rate
|| nch != priv->samples_per_frame || nch != scaletempo->samples_per_frame
|| bps != priv->bytes_per_sample || use_int != priv->use_int) { || bps != scaletempo->bytes_per_sample
priv->sample_rate = rate; || use_int != scaletempo->use_int) {
priv->samples_per_frame = nch; scaletempo->sample_rate = rate;
priv->bytes_per_sample = bps; scaletempo->samples_per_frame = nch;
priv->bytes_per_frame = nch * bps; scaletempo->bytes_per_sample = bps;
priv->use_int = use_int; scaletempo->bytes_per_frame = nch * bps;
priv->reinit_buffers = TRUE; scaletempo->use_int = use_int;
scaletempo->reinit_buffers = TRUE;
} }
return TRUE; return TRUE;
@ -622,7 +570,6 @@ gst_scaletempo_query (GstBaseTransform * trans, GstPadDirection direction,
GstQuery * query) GstQuery * query)
{ {
GstScaletempo *scaletempo = GST_SCALETEMPO (trans); GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
GstScaletempoPrivate *p = scaletempo->priv;
if (direction == GST_PAD_SRC) { if (direction == GST_PAD_SRC) {
switch (GST_QUERY_TYPE (query)) { switch (GST_QUERY_TYPE (query)) {
@ -643,10 +590,10 @@ gst_scaletempo_query (GstBaseTransform * trans, GstPadDirection direction,
/* add our own latency */ /* add our own latency */
GST_DEBUG_OBJECT (scaletempo, "Our latency: %" GST_TIME_FORMAT, GST_DEBUG_OBJECT (scaletempo, "Our latency: %" GST_TIME_FORMAT,
GST_TIME_ARGS (p->latency)); GST_TIME_ARGS (scaletempo->latency));
min += p->latency; min += scaletempo->latency;
if (max != GST_CLOCK_TIME_NONE) if (max != GST_CLOCK_TIME_NONE)
max += p->latency; max += scaletempo->latency;
GST_DEBUG_OBJECT (scaletempo, "Calculated total latency : min %" GST_DEBUG_OBJECT (scaletempo, "Calculated total latency : min %"
GST_TIME_FORMAT " max %" GST_TIME_FORMAT, GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
@ -677,20 +624,19 @@ gst_scaletempo_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec) guint prop_id, GValue * value, GParamSpec * pspec)
{ {
GstScaletempo *scaletempo = GST_SCALETEMPO (object); GstScaletempo *scaletempo = GST_SCALETEMPO (object);
GstScaletempoPrivate *priv = scaletempo->priv;
switch (prop_id) { switch (prop_id) {
case PROP_RATE: case PROP_RATE:
g_value_set_double (value, priv->scale); g_value_set_double (value, scaletempo->scale);
break; break;
case PROP_STRIDE: case PROP_STRIDE:
g_value_set_uint (value, priv->ms_stride); g_value_set_uint (value, scaletempo->ms_stride);
break; break;
case PROP_OVERLAP: case PROP_OVERLAP:
g_value_set_double (value, priv->percent_overlap); g_value_set_double (value, scaletempo->percent_overlap);
break; break;
case PROP_SEARCH: case PROP_SEARCH:
g_value_set_uint (value, priv->ms_search); g_value_set_uint (value, scaletempo->ms_search);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -703,30 +649,29 @@ gst_scaletempo_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec) guint prop_id, const GValue * value, GParamSpec * pspec)
{ {
GstScaletempo *scaletempo = GST_SCALETEMPO (object); GstScaletempo *scaletempo = GST_SCALETEMPO (object);
GstScaletempoPrivate *priv = scaletempo->priv;
switch (prop_id) { switch (prop_id) {
case PROP_STRIDE:{ case PROP_STRIDE:{
guint new_value = g_value_get_uint (value); guint new_value = g_value_get_uint (value);
if (priv->ms_stride != new_value) { if (scaletempo->ms_stride != new_value) {
priv->ms_stride = new_value; scaletempo->ms_stride = new_value;
priv->reinit_buffers = TRUE; scaletempo->reinit_buffers = TRUE;
} }
break; break;
} }
case PROP_OVERLAP:{ case PROP_OVERLAP:{
gdouble new_value = g_value_get_double (value); gdouble new_value = g_value_get_double (value);
if (priv->percent_overlap != new_value) { if (scaletempo->percent_overlap != new_value) {
priv->percent_overlap = new_value; scaletempo->percent_overlap = new_value;
priv->reinit_buffers = TRUE; scaletempo->reinit_buffers = TRUE;
} }
break; break;
} }
case PROP_SEARCH:{ case PROP_SEARCH:{
guint new_value = g_value_get_uint (value); guint new_value = g_value_get_uint (value);
if (priv->ms_search != new_value) { if (scaletempo->ms_search != new_value) {
priv->ms_search = new_value; scaletempo->ms_search = new_value;
priv->reinit_buffers = TRUE; scaletempo->reinit_buffers = TRUE;
} }
break; break;
} }
@ -743,8 +688,6 @@ gst_scaletempo_class_init (GstScaletempoClass * klass)
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass); GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass);
g_type_class_add_private (klass, sizeof (GstScaletempoPrivate));
gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_scaletempo_get_property); gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_scaletempo_get_property);
gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_scaletempo_set_property); gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_scaletempo_set_property);
@ -788,21 +731,17 @@ gst_scaletempo_class_init (GstScaletempoClass * klass)
static void static void
gst_scaletempo_init (GstScaletempo * scaletempo) gst_scaletempo_init (GstScaletempo * scaletempo)
{ {
GstScaletempoPrivate *priv;
scaletempo->priv = priv = GST_SCALETEMPO_GET_PRIVATE (scaletempo);
/* defaults */ /* defaults */
priv->ms_stride = 30; scaletempo->ms_stride = 30;
priv->percent_overlap = .2; scaletempo->percent_overlap = .2;
priv->ms_search = 14; scaletempo->ms_search = 14;
/* uninitialized */ /* uninitialized */
priv->scale = 0; scaletempo->scale = 0;
priv->sample_rate = 0; scaletempo->sample_rate = 0;
priv->frames_stride_error = 0; scaletempo->frames_stride_error = 0;
priv->bytes_stride = 0; scaletempo->bytes_stride = 0;
priv->bytes_queued = 0; scaletempo->bytes_queued = 0;
priv->bytes_to_slide = 0; scaletempo->bytes_to_slide = 0;
priv->segment_start = 0; scaletempo->segment_start = 0;
} }

View file

@ -25,11 +25,13 @@
#include <gst/base/gstbasetransform.h> #include <gst/base/gstbasetransform.h>
G_BEGIN_DECLS G_BEGIN_DECLS
#define GST_TYPE_SCALETEMPO (gst_scaletempo_get_type()) #define GST_TYPE_SCALETEMPO (gst_scaletempo_get_type())
#define GST_SCALETEMPO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_SCALETEMPO, GstScaletempo)) #define GST_SCALETEMPO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_SCALETEMPO, GstScaletempo))
#define GST_SCALETEMPO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_SCALETEMPO, GstScaletempoClass)) #define GST_SCALETEMPO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_SCALETEMPO, GstScaletempoClass))
#define GST_IS_SCALETEMPO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_SCALETEMPO)) #define GST_IS_SCALETEMPO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_SCALETEMPO))
#define GST_IS_SCALETEMPO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_SCALETEMPO)) #define GST_IS_SCALETEMPO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_SCALETEMPO))
typedef struct _GstScaletempo GstScaletempo; typedef struct _GstScaletempo GstScaletempo;
typedef struct _GstScaletempoClass GstScaletempoClass; typedef struct _GstScaletempoClass GstScaletempoClass;
typedef struct _GstScaletempoPrivate GstScaletempoPrivate; typedef struct _GstScaletempoPrivate GstScaletempoPrivate;
@ -38,7 +40,51 @@ struct _GstScaletempo
{ {
GstBaseTransform element; GstBaseTransform element;
GstScaletempoPrivate *priv; gdouble scale;
/* parameters */
guint ms_stride;
gdouble percent_overlap;
guint ms_search;
/* caps */
gboolean use_int;
guint samples_per_frame; /* AKA number of channels */
guint bytes_per_sample;
guint bytes_per_frame;
guint sample_rate;
/* stride */
gdouble frames_stride_scaled;
gdouble frames_stride_error;
guint bytes_stride;
gdouble bytes_stride_scaled;
guint bytes_queue_max;
guint bytes_queued;
guint bytes_to_slide;
gint8 *buf_queue;
/* overlap */
guint samples_overlap;
guint samples_standing;
guint bytes_overlap;
guint bytes_standing;
gpointer buf_overlap;
gpointer table_blend;
void (*output_overlap) (GstScaletempo * scaletempo, gpointer out_buf, guint bytes_off);
/* best overlap */
guint frames_search;
gpointer buf_pre_corr;
gpointer table_window;
guint (*best_overlap_offset) (GstScaletempo * scaletempo);
/* gstreamer */
gint64 segment_start;
GstClockTime latency;
/* threads */
gboolean reinit_buffers;
}; };
struct _GstScaletempoClass struct _GstScaletempoClass