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.

243 lines
8.1KB

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