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.

248 lines
8.3KB

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