• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2008 Rov Juvano <rovjuvano@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 /**
22  * SECTION:element-scaletempo
23  * @title: scaletempo
24  *
25  * Scale tempo while maintaining pitch
26  * (WSOLA-like technique with cross correlation)
27  * Inspired by SoundTouch library by Olli Parviainen
28  *
29  * Use Sceletempo to apply playback rates without the chipmunk effect.
30  *
31  * ## Example pipelines
32  *
33  * |[
34  * filesrc location=media.ext ! decodebin name=d \
35  *     d. ! queue ! audioconvert ! audioresample ! scaletempo ! audioconvert ! audioresample ! autoaudiosink \
36  *     d. ! queue ! videoconvert ! autovideosink
37  * ]|
38  * OR
39  * |[
40  * playbin uri=... audio_sink="scaletempo ! audioconvert ! audioresample ! autoaudiosink"
41  * ]|
42  * When an application sends a seek event with rate != 1.0, Scaletempo applies
43  * the rate change by scaling the tempo without scaling the pitch.
44  *
45  * Scaletempo works by producing audio in constant sized chunks
46  * (#GstScaletempo:stride) but consuming chunks proportional to the playback
47  * rate.
48  *
49  * Scaletempo then smooths the output by blending the end of one stride with
50  * the next (#GstScaletempo:overlap).
51  *
52  * Scaletempo smooths the overlap further by searching within the input buffer
53  * for the best overlap position.  Scaletempo uses a statistical cross
54  * correlation (roughly a dot-product).  Scaletempo consumes most of its CPU
55  * cycles here. One can use the #GstScaletempo:search propery to tune how far
56  * the algorithm looks.
57  *
58  */
59 
60 /*
61  * Note: frame = audio key unit (i.e. one sample for each channel)
62  */
63 
64 #ifdef HAVE_CONFIG_H
65 #include "config.h"
66 #endif
67 
68 #include <gst/gst.h>
69 #include <gst/base/gstbasetransform.h>
70 #include <gst/audio/audio.h>
71 #include <string.h>             /* for memset */
72 
73 #include "gstscaletempo.h"
74 
75 GST_DEBUG_CATEGORY_STATIC (gst_scaletempo_debug);
76 #define GST_CAT_DEFAULT gst_scaletempo_debug
77 
78 /* Filter signals and args */
79 enum
80 {
81   LAST_SIGNAL
82 };
83 
84 enum
85 {
86   PROP_0,
87   PROP_RATE,
88   PROP_STRIDE,
89   PROP_OVERLAP,
90   PROP_SEARCH,
91 };
92 
93 #define SUPPORTED_CAPS \
94 GST_STATIC_CAPS ( \
95     GST_AUDIO_CAPS_MAKE (GST_AUDIO_NE (F32)) ", layout=(string)interleaved; " \
96     GST_AUDIO_CAPS_MAKE (GST_AUDIO_NE (F64)) ", layout=(string)interleaved; " \
97     GST_AUDIO_CAPS_MAKE (GST_AUDIO_NE (S16)) ", layout=(string)interleaved" \
98 )
99 
100 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
101     GST_PAD_SINK,
102     GST_PAD_ALWAYS,
103     SUPPORTED_CAPS);
104 
105 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
106     GST_PAD_SRC,
107     GST_PAD_ALWAYS,
108     SUPPORTED_CAPS);
109 
110 #define DEBUG_INIT(bla) GST_DEBUG_CATEGORY_INIT (gst_scaletempo_debug, "scaletempo", 0, "scaletempo element");
111 
112 #define gst_scaletempo_parent_class parent_class
113 G_DEFINE_TYPE_WITH_CODE (GstScaletempo, gst_scaletempo,
114     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT (0));
115 GST_ELEMENT_REGISTER_DEFINE (scaletempo, "scaletempo",
116     GST_RANK_NONE, GST_TYPE_SCALETEMPO);
117 
118 #define CREATE_BEST_OVERLAP_OFFSET_FLOAT_FUNC(type) \
119 static guint \
120 best_overlap_offset_##type (GstScaletempo * st) \
121 { \
122   g##type *pw, *po, *ppc, *search_start; \
123   g##type best_corr = G_MININT; \
124   guint best_off = 0; \
125   gint i, off; \
126   \
127   pw = st->table_window; \
128   po = st->buf_overlap; \
129   po += st->samples_per_frame; \
130   ppc = st->buf_pre_corr; \
131   for (i = st->samples_per_frame; i < st->samples_overlap; i++) { \
132     *ppc++ = *pw++ * *po++; \
133   } \
134   \
135   search_start = (g##type *) st->buf_queue + st->samples_per_frame; \
136   for (off = 0; off < st->frames_search; off++) { \
137     g##type corr = 0; \
138     g##type *ps = search_start; \
139     ppc = st->buf_pre_corr; \
140     for (i = st->samples_per_frame; i < st->samples_overlap; i++) { \
141       corr += *ppc++ * *ps++; \
142     } \
143     if (corr > best_corr) { \
144       best_corr = corr; \
145       best_off = off; \
146     } \
147     search_start += st->samples_per_frame; \
148   } \
149   \
150   return best_off * st->bytes_per_frame; \
151 }
152 
153 CREATE_BEST_OVERLAP_OFFSET_FLOAT_FUNC (float);
154 CREATE_BEST_OVERLAP_OFFSET_FLOAT_FUNC (double);
155 
156 /* buffer padding for loop optimization: sizeof(gint32) * (loop_size - 1) */
157 #define UNROLL_PADDING (4*3)
158 static guint
best_overlap_offset_s16(GstScaletempo * st)159 best_overlap_offset_s16 (GstScaletempo * st)
160 {
161   gint32 *pw, *ppc;
162   gint16 *po, *search_start;
163   gint64 best_corr = G_MININT64;
164   guint best_off = 0;
165   guint off;
166   glong i;
167 
168   pw = st->table_window;
169   po = st->buf_overlap;
170   po += st->samples_per_frame;
171   ppc = st->buf_pre_corr;
172   for (i = st->samples_per_frame; i < st->samples_overlap; i++) {
173     *ppc++ = (*pw++ * *po++) >> 15;
174   }
175 
176   search_start = (gint16 *) st->buf_queue + st->samples_per_frame;
177   for (off = 0; off < st->frames_search; off++) {
178     gint64 corr = 0;
179     gint16 *ps = search_start;
180     ppc = st->buf_pre_corr;
181     ppc += st->samples_overlap - st->samples_per_frame;
182     ps += st->samples_overlap - st->samples_per_frame;
183     i = -((glong) st->samples_overlap - (glong) st->samples_per_frame);
184     do {
185       corr += ppc[i + 0] * ps[i + 0];
186       corr += ppc[i + 1] * ps[i + 1];
187       corr += ppc[i + 2] * ps[i + 2];
188       corr += ppc[i + 3] * ps[i + 3];
189       i += 4;
190     } while (i < 0);
191     if (corr > best_corr) {
192       best_corr = corr;
193       best_off = off;
194     }
195     search_start += st->samples_per_frame;
196   }
197 
198   return best_off * st->bytes_per_frame;
199 }
200 
201 #define CREATE_OUTPUT_OVERLAP_FLOAT_FUNC(type) \
202 static void \
203 output_overlap_##type (GstScaletempo * st, gpointer buf_out, guint bytes_off) \
204 { \
205   g##type *pout = buf_out; \
206   g##type *pb = st->table_blend; \
207   g##type *po = st->buf_overlap; \
208   g##type *pin = (g##type *) (st->buf_queue + bytes_off); \
209   gint i; \
210   for (i = 0; i < st->samples_overlap; i++) { \
211     *pout++ = *po - *pb++ * (*po - *pin++); \
212     po++; \
213   } \
214 }
215 
216 CREATE_OUTPUT_OVERLAP_FLOAT_FUNC (float);
217 CREATE_OUTPUT_OVERLAP_FLOAT_FUNC (double);
218 
219 static void
output_overlap_s16(GstScaletempo * st,gpointer buf_out,guint bytes_off)220 output_overlap_s16 (GstScaletempo * st, gpointer buf_out, guint bytes_off)
221 {
222   gint16 *pout = buf_out;
223   gint32 *pb = st->table_blend;
224   gint16 *po = st->buf_overlap;
225   gint16 *pin = (gint16 *) (st->buf_queue + bytes_off);
226   gint i;
227   for (i = 0; i < st->samples_overlap; i++) {
228     *pout++ = *po - ((*pb++ * (*po - *pin++)) >> 16);
229     po++;
230   }
231 }
232 
233 static guint
fill_queue(GstScaletempo * st,GstBuffer * buf_in,guint offset)234 fill_queue (GstScaletempo * st, GstBuffer * buf_in, guint offset)
235 {
236   guint bytes_in = gst_buffer_get_size (buf_in) - offset;
237   guint offset_unchanged = offset;
238   GstMapInfo map;
239 
240   gst_buffer_map (buf_in, &map, GST_MAP_READ);
241   if (st->bytes_to_slide > 0) {
242     if (st->bytes_to_slide < st->bytes_queued) {
243       guint bytes_in_move = st->bytes_queued - st->bytes_to_slide;
244       memmove (st->buf_queue, st->buf_queue + st->bytes_to_slide,
245           bytes_in_move);
246       st->bytes_to_slide = 0;
247       st->bytes_queued = bytes_in_move;
248     } else {
249       guint bytes_in_skip;
250       st->bytes_to_slide -= st->bytes_queued;
251       bytes_in_skip = MIN (st->bytes_to_slide, bytes_in);
252       st->bytes_queued = 0;
253       st->bytes_to_slide -= bytes_in_skip;
254       offset += bytes_in_skip;
255       bytes_in -= bytes_in_skip;
256     }
257   }
258 
259   if (bytes_in > 0) {
260     guint bytes_in_copy =
261         MIN (st->bytes_queue_max - st->bytes_queued, bytes_in);
262     memcpy (st->buf_queue + st->bytes_queued, map.data + offset, bytes_in_copy);
263     st->bytes_queued += bytes_in_copy;
264     offset += bytes_in_copy;
265   }
266   gst_buffer_unmap (buf_in, &map);
267 
268   return offset - offset_unchanged;
269 }
270 
271 static void
reinit_buffers(GstScaletempo * st)272 reinit_buffers (GstScaletempo * st)
273 {
274   gint i, j;
275   guint frames_overlap;
276   guint new_size;
277   GstClockTime latency;
278 
279   guint frames_stride = st->ms_stride * st->sample_rate / 1000.0;
280   st->bytes_stride = frames_stride * st->bytes_per_frame;
281 
282   /* overlap */
283   frames_overlap = frames_stride * st->percent_overlap;
284   if (frames_overlap < 1) {     /* if no overlap */
285     st->bytes_overlap = 0;
286     st->bytes_standing = st->bytes_stride;
287     st->samples_standing = st->bytes_standing / st->bytes_per_sample;
288     st->output_overlap = NULL;
289   } else {
290     guint prev_overlap = st->bytes_overlap;
291     st->bytes_overlap = frames_overlap * st->bytes_per_frame;
292     st->samples_overlap = frames_overlap * st->samples_per_frame;
293     st->bytes_standing = st->bytes_stride - st->bytes_overlap;
294     st->samples_standing = st->bytes_standing / st->bytes_per_sample;
295     st->buf_overlap = g_realloc (st->buf_overlap, st->bytes_overlap);
296     /* S16 uses gint32 blend table, floats/doubles use their respective type */
297     st->table_blend =
298         g_realloc (st->table_blend,
299         st->samples_overlap * (st->format ==
300             GST_AUDIO_FORMAT_S16 ? 4 : st->bytes_per_sample));
301     if (st->bytes_overlap > prev_overlap) {
302       memset ((guint8 *) st->buf_overlap + prev_overlap, 0,
303           st->bytes_overlap - prev_overlap);
304     }
305     if (st->format == GST_AUDIO_FORMAT_S16) {
306       gint32 *pb = st->table_blend;
307       gint64 blend = 0;
308       for (i = 0; i < frames_overlap; i++) {
309         gint32 v = blend / frames_overlap;
310         for (j = 0; j < st->samples_per_frame; j++) {
311           *pb++ = v;
312         }
313         blend += 65535;         /* 2^16 */
314       }
315       st->output_overlap = output_overlap_s16;
316     } else if (st->format == GST_AUDIO_FORMAT_F32) {
317       gfloat *pb = st->table_blend;
318       gfloat t = (gfloat) frames_overlap;
319       for (i = 0; i < frames_overlap; i++) {
320         gfloat v = i / t;
321         for (j = 0; j < st->samples_per_frame; j++) {
322           *pb++ = v;
323         }
324       }
325       st->output_overlap = output_overlap_float;
326     } else {
327       gdouble *pb = st->table_blend;
328       gdouble t = (gdouble) frames_overlap;
329       for (i = 0; i < frames_overlap; i++) {
330         gdouble v = i / t;
331         for (j = 0; j < st->samples_per_frame; j++) {
332           *pb++ = v;
333         }
334       }
335       st->output_overlap = output_overlap_double;
336     }
337   }
338 
339   /* best overlap */
340   st->frames_search =
341       (frames_overlap <= 1) ? 0 : st->ms_search * st->sample_rate / 1000.0;
342   if (st->frames_search < 1) {  /* if no search */
343     st->best_overlap_offset = NULL;
344   } else {
345     /* S16 uses gint32 buffer, floats/doubles use their respective type */
346     guint bytes_pre_corr =
347         (st->samples_overlap - st->samples_per_frame) * (st->format ==
348         GST_AUDIO_FORMAT_S16 ? 4 : st->bytes_per_sample);
349     st->buf_pre_corr =
350         g_realloc (st->buf_pre_corr, bytes_pre_corr + UNROLL_PADDING);
351     st->table_window = g_realloc (st->table_window, bytes_pre_corr);
352     if (st->format == GST_AUDIO_FORMAT_S16) {
353       gint64 t = frames_overlap;
354       gint32 n = 8589934588LL / (t * t);        /* 4 * (2^31 - 1) / t^2 */
355       gint32 *pw;
356 
357       memset ((guint8 *) st->buf_pre_corr + bytes_pre_corr, 0, UNROLL_PADDING);
358       pw = st->table_window;
359       for (i = 1; i < frames_overlap; i++) {
360         gint32 v = (i * (t - i) * n) >> 15;
361         for (j = 0; j < st->samples_per_frame; j++) {
362           *pw++ = v;
363         }
364       }
365       st->best_overlap_offset = best_overlap_offset_s16;
366     } else if (st->format == GST_AUDIO_FORMAT_F32) {
367       gfloat *pw = st->table_window;
368       for (i = 1; i < frames_overlap; i++) {
369         gfloat v = i * (frames_overlap - i);
370         for (j = 0; j < st->samples_per_frame; j++) {
371           *pw++ = v;
372         }
373       }
374       st->best_overlap_offset = best_overlap_offset_float;
375     } else {
376       gdouble *pw = st->table_window;
377       for (i = 1; i < frames_overlap; i++) {
378         gdouble v = i * (frames_overlap - i);
379         for (j = 0; j < st->samples_per_frame; j++) {
380           *pw++ = v;
381         }
382       }
383       st->best_overlap_offset = best_overlap_offset_double;
384     }
385   }
386 
387   new_size =
388       (st->frames_search + frames_stride +
389       frames_overlap) * st->bytes_per_frame;
390   if (st->bytes_queued > new_size) {
391     if (st->bytes_to_slide > st->bytes_queued) {
392       st->bytes_to_slide -= st->bytes_queued;
393       st->bytes_queued = 0;
394     } else {
395       guint new_queued = MIN (st->bytes_queued - st->bytes_to_slide, new_size);
396       memmove (st->buf_queue,
397           st->buf_queue + st->bytes_queued - new_queued, new_queued);
398       st->bytes_to_slide = 0;
399       st->bytes_queued = new_queued;
400     }
401   }
402 
403   st->bytes_queue_max = new_size;
404   st->buf_queue = g_realloc (st->buf_queue, st->bytes_queue_max);
405 
406   latency =
407       gst_util_uint64_scale (st->bytes_queue_max, GST_SECOND,
408       st->bytes_per_frame * st->sample_rate);
409   if (st->latency != latency) {
410     st->latency = latency;
411     gst_element_post_message (GST_ELEMENT (st),
412         gst_message_new_latency (GST_OBJECT (st)));
413   }
414 
415   st->bytes_stride_scaled = st->bytes_stride * st->scale;
416   st->frames_stride_scaled = st->bytes_stride_scaled / st->bytes_per_frame;
417 
418   GST_DEBUG
419       ("%.3f scale, %.3f stride_in, %i stride_out, %i standing, %i overlap, %i search, %i queue, %s mode",
420       st->scale, st->frames_stride_scaled,
421       (gint) (st->bytes_stride / st->bytes_per_frame),
422       (gint) (st->bytes_standing / st->bytes_per_frame),
423       (gint) (st->bytes_overlap / st->bytes_per_frame), st->frames_search,
424       (gint) (st->bytes_queue_max / st->bytes_per_frame),
425       gst_audio_format_to_string (st->format));
426 
427   st->reinit_buffers = FALSE;
428 }
429 
430 static GstBuffer *
reverse_buffer(GstScaletempo * st,GstBuffer * inbuf)431 reverse_buffer (GstScaletempo * st, GstBuffer * inbuf)
432 {
433   GstBuffer *outbuf;
434   GstMapInfo imap, omap;
435 
436   gst_buffer_map (inbuf, &imap, GST_MAP_READ);
437   outbuf = gst_buffer_new_and_alloc (imap.size);
438   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
439 
440   if (st->format == GST_AUDIO_FORMAT_F64) {
441     const gint64 *ip = (const gint64 *) imap.data;
442     gint64 *op = (gint64 *) (omap.data + omap.size - 8 * st->samples_per_frame);
443     guint i, n = imap.size / (8 * st->samples_per_frame);
444     guint j, c = st->samples_per_frame;
445 
446     for (i = 0; i < n; i++) {
447       for (j = 0; j < c; j++)
448         op[j] = ip[j];
449       op -= c;
450       ip += c;
451     }
452   } else {
453     const gint32 *ip = (const gint32 *) imap.data;
454     gint32 *op = (gint32 *) (omap.data + omap.size - 4 * st->samples_per_frame);
455     guint i, n = imap.size / (4 * st->samples_per_frame);
456     guint j, c = st->samples_per_frame;
457 
458     for (i = 0; i < n; i++) {
459       for (j = 0; j < c; j++)
460         op[j] = ip[j];
461       op -= c;
462       ip += c;
463     }
464   }
465 
466   gst_buffer_unmap (inbuf, &imap);
467   gst_buffer_unmap (outbuf, &omap);
468 
469   return outbuf;
470 }
471 
472 /* GstBaseTransform vmethod implementations */
473 static GstFlowReturn
gst_scaletempo_transform(GstBaseTransform * trans,GstBuffer * inbuf,GstBuffer * outbuf)474 gst_scaletempo_transform (GstBaseTransform * trans,
475     GstBuffer * inbuf, GstBuffer * outbuf)
476 {
477   GstScaletempo *st = GST_SCALETEMPO (trans);
478   gint8 *pout;
479   guint offset_in, bytes_out;
480   GstMapInfo omap;
481   GstClockTime timestamp;
482   GstBuffer *tmpbuf = NULL;
483 
484   if (st->reverse)
485     tmpbuf = reverse_buffer (st, inbuf);
486 
487   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
488   pout = (gint8 *) omap.data;
489   bytes_out = omap.size;
490 
491   offset_in = fill_queue (st, tmpbuf ? tmpbuf : inbuf, 0);
492   bytes_out = 0;
493   while (st->bytes_queued >= st->bytes_queue_max) {
494     guint bytes_off = 0;
495     gdouble frames_to_slide;
496     guint frames_to_stride_whole;
497 
498     /* output stride */
499     if (st->output_overlap) {
500       if (st->best_overlap_offset) {
501         bytes_off = st->best_overlap_offset (st);
502       }
503       st->output_overlap (st, pout, bytes_off);
504     }
505     memcpy (pout + st->bytes_overlap,
506         st->buf_queue + bytes_off + st->bytes_overlap, st->bytes_standing);
507     pout += st->bytes_stride;
508     bytes_out += st->bytes_stride;
509 
510     /* input stride */
511     memcpy (st->buf_overlap,
512         st->buf_queue + bytes_off + st->bytes_stride, st->bytes_overlap);
513     frames_to_slide = st->frames_stride_scaled + st->frames_stride_error;
514     frames_to_stride_whole = (gint) frames_to_slide;
515     st->bytes_to_slide = frames_to_stride_whole * st->bytes_per_frame;
516     st->frames_stride_error = frames_to_slide - frames_to_stride_whole;
517 
518     offset_in += fill_queue (st, tmpbuf ? tmpbuf : inbuf, offset_in);
519   }
520   gst_buffer_unmap (outbuf, &omap);
521 
522   if (st->reverse) {
523     timestamp = st->in_segment.stop - GST_BUFFER_TIMESTAMP (inbuf);
524     if (timestamp < st->latency)
525       timestamp = 0;
526     else
527       timestamp -= st->latency;
528   } else {
529     timestamp = GST_BUFFER_TIMESTAMP (inbuf) - st->in_segment.start;
530     if (timestamp < st->latency)
531       timestamp = 0;
532     else
533       timestamp -= st->latency;
534   }
535   GST_BUFFER_TIMESTAMP (outbuf) = timestamp / st->scale + st->in_segment.start;
536   GST_BUFFER_DURATION (outbuf) =
537       gst_util_uint64_scale (bytes_out, GST_SECOND,
538       st->bytes_per_frame * st->sample_rate);
539   gst_buffer_set_size (outbuf, bytes_out);
540 
541   if (tmpbuf)
542     gst_buffer_unref (tmpbuf);
543 
544   return GST_FLOW_OK;
545 }
546 
547 static GstFlowReturn
gst_scaletempo_submit_input_buffer(GstBaseTransform * trans,gboolean is_discont,GstBuffer * input)548 gst_scaletempo_submit_input_buffer (GstBaseTransform * trans,
549     gboolean is_discont, GstBuffer * input)
550 {
551   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
552 
553   if (scaletempo->in_segment.format == GST_FORMAT_TIME) {
554     input =
555         gst_audio_buffer_clip (input, &scaletempo->in_segment,
556         scaletempo->sample_rate, scaletempo->bytes_per_frame);
557     if (!input)
558       return GST_FLOW_OK;
559   }
560 
561   return GST_BASE_TRANSFORM_CLASS (parent_class)->submit_input_buffer (trans,
562       is_discont, input);
563 }
564 
565 static gboolean
gst_scaletempo_transform_size(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,gsize size,GstCaps * othercaps,gsize * othersize)566 gst_scaletempo_transform_size (GstBaseTransform * trans,
567     GstPadDirection direction,
568     GstCaps * caps, gsize size, GstCaps * othercaps, gsize * othersize)
569 {
570   if (direction == GST_PAD_SINK) {
571     GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
572     gint bytes_to_out;
573 
574     if (scaletempo->reinit_buffers)
575       reinit_buffers (scaletempo);
576 
577     bytes_to_out = size + scaletempo->bytes_queued - scaletempo->bytes_to_slide;
578     if (bytes_to_out < (gint) scaletempo->bytes_queue_max) {
579       *othersize = 0;
580     } else {
581       /* while (total_buffered - stride_length * n >= queue_max) n++ */
582       *othersize = scaletempo->bytes_stride * ((guint) (
583               (bytes_to_out - scaletempo->bytes_queue_max +
584                   /* rounding protection */ scaletempo->bytes_per_frame)
585               / scaletempo->bytes_stride_scaled) + 1);
586     }
587 
588     return TRUE;
589   }
590   return FALSE;
591 }
592 
593 static gboolean
gst_scaletempo_sink_event(GstBaseTransform * trans,GstEvent * event)594 gst_scaletempo_sink_event (GstBaseTransform * trans, GstEvent * event)
595 {
596   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
597 
598   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
599     GstSegment segment;
600 
601     gst_event_copy_segment (event, &segment);
602 
603     if (segment.format != GST_FORMAT_TIME
604         || scaletempo->scale != ABS (segment.rate)
605         || ! !scaletempo->reverse != ! !(segment.rate < 0.0)) {
606       if (segment.format != GST_FORMAT_TIME || ABS (segment.rate - 1.0) < 1e-10) {
607         scaletempo->scale = 1.0;
608         gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo),
609             TRUE);
610       } else {
611         gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (scaletempo),
612             FALSE);
613         scaletempo->scale = ABS (segment.rate);
614         scaletempo->reverse = segment.rate < 0.0;
615         scaletempo->bytes_stride_scaled =
616             scaletempo->bytes_stride * scaletempo->scale;
617         scaletempo->frames_stride_scaled =
618             scaletempo->bytes_stride_scaled / scaletempo->bytes_per_frame;
619         GST_DEBUG ("%.3f scale, %.3f stride_in, %i stride_out",
620             scaletempo->scale, scaletempo->frames_stride_scaled,
621             (gint) (scaletempo->bytes_stride / scaletempo->bytes_per_frame));
622 
623         scaletempo->bytes_to_slide = 0;
624       }
625     }
626 
627     scaletempo->in_segment = segment;
628     scaletempo->out_segment = segment;
629 
630     if (scaletempo->scale != 1.0 || scaletempo->reverse) {
631       guint32 seqnum;
632 
633       segment.applied_rate = segment.rate;
634       segment.rate = 1.0;
635 
636       if (segment.stop != -1) {
637         segment.stop =
638             (segment.stop - segment.start) / ABS (segment.applied_rate) +
639             segment.start;
640       }
641 
642       scaletempo->out_segment = segment;
643 
644       seqnum = gst_event_get_seqnum (event);
645       gst_event_unref (event);
646 
647       event = gst_event_new_segment (&segment);
648       gst_event_set_seqnum (event, seqnum);
649 
650       return gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (trans), event);
651     }
652   } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
653     gst_segment_init (&scaletempo->in_segment, GST_FORMAT_UNDEFINED);
654     gst_segment_init (&scaletempo->out_segment, GST_FORMAT_UNDEFINED);
655   } else if (GST_EVENT_TYPE (event) == GST_EVENT_GAP) {
656     if (scaletempo->scale != 1.0) {
657       GstClockTime gap_ts, gap_duration;
658       gst_event_parse_gap (event, &gap_ts, &gap_duration);
659       if (scaletempo->reverse) {
660 #ifdef OHOS_OPT_COMPAT
661         /* ohos.opt.compat.0050
662         * gstscaletempo does not guarantee gap_ts in current segmetn. For example, when tsdemux is
663         * accurate seeking, tsdemux needs to pull data from the previous keyframe, if there's a big
664         * gap in pts, tsdemux will push a GST_EVENT_GAP event, but in this case gap_ts not in current
665         * segment.
666         */
667         if (gap_ts > scaletempo->in_segment.stop) {
668           GST_WARNING ("gap_ts > scaletempo->in_segment.stop, maybe in seeking");
669           return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
670         }
671 #endif
672         gap_ts = scaletempo->in_segment.stop - gap_ts;
673       } else {
674 #ifdef OHOS_OPT_COMPAT
675         if (scaletempo->in_segment.start > gap_ts) {
676           GST_WARNING ("gap_ts > scaletempo->in_segment.stop, maybe in seeking");
677           return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
678         }
679 #endif
680         gap_ts = gap_ts - scaletempo->in_segment.start;
681       }
682       gap_ts = gap_ts / scaletempo->scale + scaletempo->in_segment.start;
683       if (GST_CLOCK_TIME_IS_VALID (gap_duration)) {
684         gap_duration = gap_duration / ABS (scaletempo->scale);
685       }
686       gst_event_unref (event);
687       event = gst_event_new_gap (gap_ts, gap_duration);
688     }
689   }
690 
691   return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
692 }
693 
694 static gboolean
gst_scaletempo_set_caps(GstBaseTransform * trans,GstCaps * incaps,GstCaps * outcaps)695 gst_scaletempo_set_caps (GstBaseTransform * trans,
696     GstCaps * incaps, GstCaps * outcaps)
697 {
698   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
699 
700   gint width, bps, nch, rate;
701   GstAudioInfo info;
702   GstAudioFormat format;
703 
704   if (!gst_audio_info_from_caps (&info, incaps))
705     return FALSE;
706 
707   nch = GST_AUDIO_INFO_CHANNELS (&info);
708   rate = GST_AUDIO_INFO_RATE (&info);
709   width = GST_AUDIO_INFO_WIDTH (&info);
710   format = GST_AUDIO_INFO_FORMAT (&info);
711 
712   bps = width / 8;
713 
714   GST_DEBUG ("caps: %" GST_PTR_FORMAT ", %d bps", incaps, bps);
715 
716   if (rate != scaletempo->sample_rate
717       || nch != scaletempo->samples_per_frame
718       || bps != scaletempo->bytes_per_sample || format != scaletempo->format) {
719     scaletempo->sample_rate = rate;
720     scaletempo->samples_per_frame = nch;
721     scaletempo->bytes_per_sample = bps;
722     scaletempo->bytes_per_frame = nch * bps;
723     scaletempo->format = format;
724     scaletempo->reinit_buffers = TRUE;
725   }
726 
727   return TRUE;
728 }
729 
730 static gboolean
gst_scaletempo_start(GstBaseTransform * trans)731 gst_scaletempo_start (GstBaseTransform * trans)
732 {
733   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
734 
735   gst_segment_init (&scaletempo->in_segment, GST_FORMAT_UNDEFINED);
736   gst_segment_init (&scaletempo->out_segment, GST_FORMAT_UNDEFINED);
737   scaletempo->reinit_buffers = TRUE;
738 
739   return TRUE;
740 }
741 
742 static gboolean
gst_scaletempo_stop(GstBaseTransform * trans)743 gst_scaletempo_stop (GstBaseTransform * trans)
744 {
745   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
746 
747   g_free (scaletempo->buf_queue);
748   scaletempo->buf_queue = NULL;
749   g_free (scaletempo->buf_overlap);
750   scaletempo->buf_overlap = NULL;
751   g_free (scaletempo->table_blend);
752   scaletempo->table_blend = NULL;
753   g_free (scaletempo->buf_pre_corr);
754   scaletempo->buf_pre_corr = NULL;
755   g_free (scaletempo->table_window);
756   scaletempo->table_window = NULL;
757   scaletempo->reinit_buffers = TRUE;
758 
759   return TRUE;
760 }
761 
762 static gboolean
gst_scaletempo_query(GstBaseTransform * trans,GstPadDirection direction,GstQuery * query)763 gst_scaletempo_query (GstBaseTransform * trans, GstPadDirection direction,
764     GstQuery * query)
765 {
766   GstScaletempo *scaletempo = GST_SCALETEMPO (trans);
767 
768   if (direction == GST_PAD_SRC) {
769     switch (GST_QUERY_TYPE (query)) {
770       case GST_QUERY_SEGMENT:
771       {
772         GstFormat format;
773         gint64 start, stop;
774 
775         format = scaletempo->out_segment.format;
776 
777         start =
778             gst_segment_to_stream_time (&scaletempo->out_segment, format,
779             scaletempo->out_segment.start);
780         if ((stop = scaletempo->out_segment.stop) == -1)
781           stop = scaletempo->out_segment.duration;
782         else
783           stop =
784               gst_segment_to_stream_time (&scaletempo->out_segment, format,
785               stop);
786 
787         gst_query_set_segment (query, scaletempo->out_segment.rate, format,
788             start, stop);
789         return TRUE;
790       }
791       case GST_QUERY_LATENCY:{
792         GstPad *peer;
793 
794         if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM_SINK_PAD (trans)))) {
795           if ((gst_pad_query (peer, query))) {
796             GstClockTime min, max;
797             gboolean live;
798 
799             gst_query_parse_latency (query, &live, &min, &max);
800 
801             GST_DEBUG_OBJECT (scaletempo, "Peer latency: min %"
802                 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
803                 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
804 
805             /* add our own latency */
806             GST_DEBUG_OBJECT (scaletempo, "Our latency: %" GST_TIME_FORMAT,
807                 GST_TIME_ARGS (scaletempo->latency));
808             min += scaletempo->latency;
809             if (max != GST_CLOCK_TIME_NONE)
810               max += scaletempo->latency;
811 
812             GST_DEBUG_OBJECT (scaletempo, "Calculated total latency : min %"
813                 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
814                 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
815             gst_query_set_latency (query, live, min, max);
816           }
817           gst_object_unref (peer);
818         }
819 
820         return TRUE;
821       }
822       default:{
823         return GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
824             query);
825       }
826     }
827   } else {
828     return GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
829         query);
830   }
831 }
832 
833 /* GObject vmethod implementations */
834 static void
gst_scaletempo_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)835 gst_scaletempo_get_property (GObject * object,
836     guint prop_id, GValue * value, GParamSpec * pspec)
837 {
838   GstScaletempo *scaletempo = GST_SCALETEMPO (object);
839 
840   switch (prop_id) {
841     case PROP_RATE:
842       g_value_set_double (value, scaletempo->scale);
843       break;
844     case PROP_STRIDE:
845       g_value_set_uint (value, scaletempo->ms_stride);
846       break;
847     case PROP_OVERLAP:
848       g_value_set_double (value, scaletempo->percent_overlap);
849       break;
850     case PROP_SEARCH:
851       g_value_set_uint (value, scaletempo->ms_search);
852       break;
853     default:
854       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
855       break;
856   }
857 }
858 
859 static void
gst_scaletempo_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)860 gst_scaletempo_set_property (GObject * object,
861     guint prop_id, const GValue * value, GParamSpec * pspec)
862 {
863   GstScaletempo *scaletempo = GST_SCALETEMPO (object);
864 
865   switch (prop_id) {
866     case PROP_STRIDE:{
867       guint new_value = g_value_get_uint (value);
868       if (scaletempo->ms_stride != new_value) {
869         scaletempo->ms_stride = new_value;
870         scaletempo->reinit_buffers = TRUE;
871       }
872       break;
873     }
874     case PROP_OVERLAP:{
875       gdouble new_value = g_value_get_double (value);
876       if (scaletempo->percent_overlap != new_value) {
877         scaletempo->percent_overlap = new_value;
878         scaletempo->reinit_buffers = TRUE;
879       }
880       break;
881     }
882     case PROP_SEARCH:{
883       guint new_value = g_value_get_uint (value);
884       if (scaletempo->ms_search != new_value) {
885         scaletempo->ms_search = new_value;
886         scaletempo->reinit_buffers = TRUE;
887       }
888       break;
889     }
890     default:
891       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
892       break;
893   }
894 }
895 
896 static void
gst_scaletempo_class_init(GstScaletempoClass * klass)897 gst_scaletempo_class_init (GstScaletempoClass * klass)
898 {
899   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
900   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
901   GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass);
902 
903   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_scaletempo_get_property);
904   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_scaletempo_set_property);
905 
906   g_object_class_install_property (gobject_class, PROP_RATE,
907       g_param_spec_double ("rate", "Playback Rate", "Current playback rate",
908           G_MININT, G_MAXINT, 1.0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
909 
910   g_object_class_install_property (gobject_class, PROP_STRIDE,
911       g_param_spec_uint ("stride", "Stride Length",
912           "Length in milliseconds to output each stride", 1, 5000, 30,
913           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
914 
915   g_object_class_install_property (gobject_class, PROP_OVERLAP,
916       g_param_spec_double ("overlap", "Overlap Length",
917           "Percentage of stride to overlap", 0, 1, .2,
918           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
919 
920   g_object_class_install_property (gobject_class, PROP_SEARCH,
921       g_param_spec_uint ("search", "Search Length",
922           "Length in milliseconds to search for best overlap position", 0, 500,
923           14, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
924 
925   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
926   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
927   gst_element_class_set_static_metadata (gstelement_class, "Scaletempo",
928       "Filter/Effect/Rate/Audio",
929       "Sync audio tempo with playback rate",
930       "Rov Juvano <rovjuvano@users.sourceforge.net>");
931 
932   basetransform_class->sink_event =
933       GST_DEBUG_FUNCPTR (gst_scaletempo_sink_event);
934   basetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_scaletempo_set_caps);
935   basetransform_class->transform_size =
936       GST_DEBUG_FUNCPTR (gst_scaletempo_transform_size);
937   basetransform_class->transform = GST_DEBUG_FUNCPTR (gst_scaletempo_transform);
938   basetransform_class->query = GST_DEBUG_FUNCPTR (gst_scaletempo_query);
939   basetransform_class->start = GST_DEBUG_FUNCPTR (gst_scaletempo_start);
940   basetransform_class->stop = GST_DEBUG_FUNCPTR (gst_scaletempo_stop);
941   basetransform_class->submit_input_buffer =
942       GST_DEBUG_FUNCPTR (gst_scaletempo_submit_input_buffer);
943 }
944 
945 static void
gst_scaletempo_init(GstScaletempo * scaletempo)946 gst_scaletempo_init (GstScaletempo * scaletempo)
947 {
948   /* defaults */
949   scaletempo->ms_stride = 30;
950   scaletempo->percent_overlap = .2;
951   scaletempo->ms_search = 14;
952 
953   /* uninitialized */
954   scaletempo->scale = 0;
955   scaletempo->sample_rate = 0;
956   scaletempo->frames_stride_error = 0;
957   scaletempo->bytes_stride = 0;
958   scaletempo->bytes_queued = 0;
959   scaletempo->bytes_to_slide = 0;
960   gst_segment_init (&scaletempo->in_segment, GST_FORMAT_UNDEFINED);
961   gst_segment_init (&scaletempo->out_segment, GST_FORMAT_UNDEFINED);
962 }
963