You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
8.6KB

  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/common.h"
  21. #include "libavutil/mathematics.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/samplefmt.h"
  24. #include "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. typedef struct ASyncContext {
  28. const AVClass *class;
  29. AVAudioResampleContext *avr;
  30. int64_t pts; ///< timestamp in samples of the first sample in fifo
  31. int min_delta; ///< pad/trim min threshold in samples
  32. /* options */
  33. int resample;
  34. float min_delta_sec;
  35. int max_comp;
  36. /* set by filter_samples() to signal an output frame to request_frame() */
  37. int got_output;
  38. } ASyncContext;
  39. #define OFFSET(x) offsetof(ASyncContext, x)
  40. #define A AV_OPT_FLAG_AUDIO_PARAM
  41. static const AVOption options[] = {
  42. { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, A },
  43. { "min_delta", "Minimum difference between timestamps and audio data "
  44. "(in seconds) to trigger padding/trimmin the data.", OFFSET(min_delta_sec), AV_OPT_TYPE_FLOAT, { .dbl = 0.1 }, 0, INT_MAX, A },
  45. { "max_comp", "Maximum compensation in samples per second.", OFFSET(max_comp), AV_OPT_TYPE_INT, { .i64 = 500 }, 0, INT_MAX, A },
  46. { "first_pts", "Assume the first pts should be this value.", OFFSET(pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, A },
  47. { NULL },
  48. };
  49. static const AVClass async_class = {
  50. .class_name = "asyncts filter",
  51. .item_name = av_default_item_name,
  52. .option = options,
  53. .version = LIBAVUTIL_VERSION_INT,
  54. };
  55. static int init(AVFilterContext *ctx, const char *args)
  56. {
  57. ASyncContext *s = ctx->priv;
  58. int ret;
  59. s->class = &async_class;
  60. av_opt_set_defaults(s);
  61. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  62. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  63. return ret;
  64. }
  65. av_opt_free(s);
  66. return 0;
  67. }
  68. static void uninit(AVFilterContext *ctx)
  69. {
  70. ASyncContext *s = ctx->priv;
  71. if (s->avr) {
  72. avresample_close(s->avr);
  73. avresample_free(&s->avr);
  74. }
  75. }
  76. static int config_props(AVFilterLink *link)
  77. {
  78. ASyncContext *s = link->src->priv;
  79. int ret;
  80. s->min_delta = s->min_delta_sec * link->sample_rate;
  81. link->time_base = (AVRational){1, link->sample_rate};
  82. s->avr = avresample_alloc_context();
  83. if (!s->avr)
  84. return AVERROR(ENOMEM);
  85. av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0);
  86. av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0);
  87. av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0);
  88. av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0);
  89. av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0);
  90. av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0);
  91. if (s->resample)
  92. av_opt_set_int(s->avr, "force_resampling", 1, 0);
  93. if ((ret = avresample_open(s->avr)) < 0)
  94. return ret;
  95. return 0;
  96. }
  97. static int request_frame(AVFilterLink *link)
  98. {
  99. AVFilterContext *ctx = link->src;
  100. ASyncContext *s = ctx->priv;
  101. int ret = 0;
  102. int nb_samples;
  103. s->got_output = 0;
  104. while (ret >= 0 && !s->got_output)
  105. ret = ff_request_frame(ctx->inputs[0]);
  106. /* flush the fifo */
  107. if (ret == AVERROR_EOF && (nb_samples = avresample_get_delay(s->avr))) {
  108. AVFilterBufferRef *buf = ff_get_audio_buffer(link, AV_PERM_WRITE,
  109. nb_samples);
  110. if (!buf)
  111. return AVERROR(ENOMEM);
  112. ret = avresample_convert(s->avr, buf->extended_data,
  113. buf->linesize[0], nb_samples, NULL, 0, 0);
  114. if (ret <= 0) {
  115. avfilter_unref_bufferp(&buf);
  116. return (ret < 0) ? ret : AVERROR_EOF;
  117. }
  118. buf->pts = s->pts;
  119. return ff_filter_samples(link, buf);
  120. }
  121. return ret;
  122. }
  123. static int write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf)
  124. {
  125. int ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
  126. buf->linesize[0], buf->audio->nb_samples);
  127. avfilter_unref_buffer(buf);
  128. return ret;
  129. }
  130. /* get amount of data currently buffered, in samples */
  131. static int64_t get_delay(ASyncContext *s)
  132. {
  133. return avresample_available(s->avr) + avresample_get_delay(s->avr);
  134. }
  135. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
  136. {
  137. AVFilterContext *ctx = inlink->dst;
  138. ASyncContext *s = ctx->priv;
  139. AVFilterLink *outlink = ctx->outputs[0];
  140. int nb_channels = av_get_channel_layout_nb_channels(buf->audio->channel_layout);
  141. int64_t pts = (buf->pts == AV_NOPTS_VALUE) ? buf->pts :
  142. av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
  143. int out_size, ret;
  144. int64_t delta;
  145. /* buffer data until we get the first timestamp */
  146. if (s->pts == AV_NOPTS_VALUE) {
  147. if (pts != AV_NOPTS_VALUE) {
  148. s->pts = pts - get_delay(s);
  149. }
  150. return write_to_fifo(s, buf);
  151. }
  152. /* now wait for the next timestamp */
  153. if (pts == AV_NOPTS_VALUE) {
  154. return write_to_fifo(s, buf);
  155. }
  156. /* when we have two timestamps, compute how many samples would we have
  157. * to add/remove to get proper sync between data and timestamps */
  158. delta = pts - s->pts - get_delay(s);
  159. out_size = avresample_available(s->avr);
  160. if (labs(delta) > s->min_delta) {
  161. av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta);
  162. out_size = av_clipl_int32((int64_t)out_size + delta);
  163. } else {
  164. if (s->resample) {
  165. int comp = av_clip(delta, -s->max_comp, s->max_comp);
  166. av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
  167. avresample_set_compensation(s->avr, delta, inlink->sample_rate);
  168. }
  169. delta = 0;
  170. }
  171. if (out_size > 0) {
  172. AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
  173. out_size);
  174. if (!buf_out) {
  175. ret = AVERROR(ENOMEM);
  176. goto fail;
  177. }
  178. avresample_read(s->avr, buf_out->extended_data, out_size);
  179. buf_out->pts = s->pts;
  180. if (delta > 0) {
  181. av_samples_set_silence(buf_out->extended_data, out_size - delta,
  182. delta, nb_channels, buf->format);
  183. }
  184. ret = ff_filter_samples(outlink, buf_out);
  185. if (ret < 0)
  186. goto fail;
  187. s->got_output = 1;
  188. } else {
  189. av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
  190. "whole buffer.\n");
  191. }
  192. /* drain any remaining buffered data */
  193. avresample_read(s->avr, NULL, avresample_available(s->avr));
  194. s->pts = pts - avresample_get_delay(s->avr);
  195. ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
  196. buf->linesize[0], buf->audio->nb_samples);
  197. fail:
  198. avfilter_unref_buffer(buf);
  199. return ret;
  200. }
  201. static const AVFilterPad avfilter_af_asyncts_inputs[] = {
  202. {
  203. .name = "default",
  204. .type = AVMEDIA_TYPE_AUDIO,
  205. .filter_samples = filter_samples
  206. },
  207. { NULL }
  208. };
  209. static const AVFilterPad avfilter_af_asyncts_outputs[] = {
  210. {
  211. .name = "default",
  212. .type = AVMEDIA_TYPE_AUDIO,
  213. .config_props = config_props,
  214. .request_frame = request_frame
  215. },
  216. { NULL }
  217. };
  218. AVFilter avfilter_af_asyncts = {
  219. .name = "asyncts",
  220. .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"),
  221. .init = init,
  222. .uninit = uninit,
  223. .priv_size = sizeof(ASyncContext),
  224. .inputs = avfilter_af_asyncts_inputs,
  225. .outputs = avfilter_af_asyncts_outputs,
  226. };