af_asyncts.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav 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. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavresample/avresample.h"
  19. #include "libavutil/audio_fifo.h"
  20. #include "libavutil/mathematics.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/samplefmt.h"
  23. #include "audio.h"
  24. #include "avfilter.h"
  25. typedef struct ASyncContext {
  26. const AVClass *class;
  27. AVAudioResampleContext *avr;
  28. int64_t pts; ///< timestamp in samples of the first sample in fifo
  29. int min_delta; ///< pad/trim min threshold in samples
  30. /* options */
  31. int resample;
  32. float min_delta_sec;
  33. int max_comp;
  34. } ASyncContext;
  35. #define OFFSET(x) offsetof(ASyncContext, x)
  36. #define A AV_OPT_FLAG_AUDIO_PARAM
  37. static const AVOption options[] = {
  38. { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample), AV_OPT_TYPE_INT, { 0 }, 0, 1, A },
  39. { "min_delta", "Minimum difference between timestamps and audio data "
  40. "(in seconds) to trigger padding/trimmin the data.", OFFSET(min_delta_sec), AV_OPT_TYPE_FLOAT, { 0.1 }, 0, INT_MAX, A },
  41. { "max_comp", "Maximum compensation in samples per second.", OFFSET(max_comp), AV_OPT_TYPE_INT, { 500 }, 0, INT_MAX, A },
  42. { NULL },
  43. };
  44. static const AVClass async_class = {
  45. .class_name = "asyncts filter",
  46. .item_name = av_default_item_name,
  47. .option = options,
  48. .version = LIBAVUTIL_VERSION_INT,
  49. };
  50. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  51. {
  52. ASyncContext *s = ctx->priv;
  53. int ret;
  54. s->class = &async_class;
  55. av_opt_set_defaults(s);
  56. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  57. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  58. return ret;
  59. }
  60. av_opt_free(s);
  61. s->pts = AV_NOPTS_VALUE;
  62. return 0;
  63. }
  64. static void uninit(AVFilterContext *ctx)
  65. {
  66. ASyncContext *s = ctx->priv;
  67. if (s->avr) {
  68. avresample_close(s->avr);
  69. avresample_free(&s->avr);
  70. }
  71. }
  72. static int config_props(AVFilterLink *link)
  73. {
  74. ASyncContext *s = link->src->priv;
  75. int ret;
  76. s->min_delta = s->min_delta_sec * link->sample_rate;
  77. link->time_base = (AVRational){1, link->sample_rate};
  78. s->avr = avresample_alloc_context();
  79. if (!s->avr)
  80. return AVERROR(ENOMEM);
  81. av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0);
  82. av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0);
  83. av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0);
  84. av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0);
  85. av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0);
  86. av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0);
  87. if (s->resample)
  88. av_opt_set_int(s->avr, "force_resampling", 1, 0);
  89. if ((ret = avresample_open(s->avr)) < 0)
  90. return ret;
  91. return 0;
  92. }
  93. static int request_frame(AVFilterLink *link)
  94. {
  95. AVFilterContext *ctx = link->src;
  96. ASyncContext *s = ctx->priv;
  97. int ret = avfilter_request_frame(ctx->inputs[0]);
  98. int nb_samples;
  99. /* flush the fifo */
  100. if (ret == AVERROR_EOF && (nb_samples = avresample_get_delay(s->avr))) {
  101. AVFilterBufferRef *buf = ff_get_audio_buffer(link, AV_PERM_WRITE,
  102. nb_samples);
  103. if (!buf)
  104. return AVERROR(ENOMEM);
  105. avresample_convert(s->avr, (void**)buf->extended_data, buf->linesize[0],
  106. nb_samples, NULL, 0, 0);
  107. buf->pts = s->pts;
  108. ff_filter_samples(link, buf);
  109. return 0;
  110. }
  111. return ret;
  112. }
  113. static void write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf)
  114. {
  115. avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
  116. buf->linesize[0], buf->audio->nb_samples);
  117. avfilter_unref_buffer(buf);
  118. }
  119. /* get amount of data currently buffered, in samples */
  120. static int64_t get_delay(ASyncContext *s)
  121. {
  122. return avresample_available(s->avr) + avresample_get_delay(s->avr);
  123. }
  124. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. ASyncContext *s = ctx->priv;
  128. AVFilterLink *outlink = ctx->outputs[0];
  129. int nb_channels = av_get_channel_layout_nb_channels(buf->audio->channel_layout);
  130. int64_t pts = (buf->pts == AV_NOPTS_VALUE) ? buf->pts :
  131. av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
  132. int out_size;
  133. int64_t delta;
  134. /* buffer data until we get the first timestamp */
  135. if (s->pts == AV_NOPTS_VALUE) {
  136. if (pts != AV_NOPTS_VALUE) {
  137. s->pts = pts - get_delay(s);
  138. }
  139. write_to_fifo(s, buf);
  140. return;
  141. }
  142. /* now wait for the next timestamp */
  143. if (pts == AV_NOPTS_VALUE) {
  144. write_to_fifo(s, buf);
  145. return;
  146. }
  147. /* when we have two timestamps, compute how many samples would we have
  148. * to add/remove to get proper sync between data and timestamps */
  149. delta = pts - s->pts - get_delay(s);
  150. out_size = avresample_available(s->avr);
  151. if (labs(delta) > s->min_delta) {
  152. av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta);
  153. out_size += delta;
  154. } else {
  155. if (s->resample) {
  156. int comp = av_clip(delta, -s->max_comp, s->max_comp);
  157. av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
  158. avresample_set_compensation(s->avr, delta, inlink->sample_rate);
  159. }
  160. delta = 0;
  161. }
  162. if (out_size > 0) {
  163. AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
  164. out_size);
  165. if (!buf_out)
  166. return;
  167. avresample_read(s->avr, (void**)buf_out->extended_data, out_size);
  168. buf_out->pts = s->pts;
  169. if (delta > 0) {
  170. av_samples_set_silence(buf_out->extended_data, out_size - delta,
  171. delta, nb_channels, buf->format);
  172. }
  173. ff_filter_samples(outlink, buf_out);
  174. } else {
  175. av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
  176. "whole buffer.\n");
  177. }
  178. /* drain any remaining buffered data */
  179. avresample_read(s->avr, NULL, avresample_available(s->avr));
  180. s->pts = pts - avresample_get_delay(s->avr);
  181. avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
  182. buf->linesize[0], buf->audio->nb_samples);
  183. avfilter_unref_buffer(buf);
  184. }
  185. AVFilter avfilter_af_asyncts = {
  186. .name = "asyncts",
  187. .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"),
  188. .init = init,
  189. .uninit = uninit,
  190. .priv_size = sizeof(ASyncContext),
  191. .inputs = (const AVFilterPad[]) {{ .name = "default",
  192. .type = AVMEDIA_TYPE_AUDIO,
  193. .filter_samples = filter_samples },
  194. { NULL }},
  195. .outputs = (const AVFilterPad[]) {{ .name = "default",
  196. .type = AVMEDIA_TYPE_AUDIO,
  197. .config_props = config_props,
  198. .request_frame = request_frame },
  199. { NULL }},
  200. };