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.

249 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/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 asyncts_options[] = {
  42. { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample), AV_OPT_TYPE_INT, { 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, { 0.1 }, 0, INT_MAX, A },
  45. { "max_comp", "Maximum compensation in samples per second.", OFFSET(max_comp), AV_OPT_TYPE_INT, { 500 }, 0, INT_MAX, A },
  46. { "first_pts", "Assume the first pts should be this value.", OFFSET(pts), AV_OPT_TYPE_INT64, { AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, A },
  47. { NULL },
  48. };
  49. AVFILTER_DEFINE_CLASS(asyncts);
  50. static int init(AVFilterContext *ctx, const char *args)
  51. {
  52. ASyncContext *s = ctx->priv;
  53. int ret;
  54. s->class = &asyncts_class;
  55. av_opt_set_defaults(s);
  56. if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
  57. return ret;
  58. av_opt_free(s);
  59. return 0;
  60. }
  61. static void uninit(AVFilterContext *ctx)
  62. {
  63. ASyncContext *s = ctx->priv;
  64. if (s->avr) {
  65. avresample_close(s->avr);
  66. avresample_free(&s->avr);
  67. }
  68. }
  69. static int config_props(AVFilterLink *link)
  70. {
  71. ASyncContext *s = link->src->priv;
  72. int ret;
  73. s->min_delta = s->min_delta_sec * link->sample_rate;
  74. link->time_base = (AVRational){1, link->sample_rate};
  75. s->avr = avresample_alloc_context();
  76. if (!s->avr)
  77. return AVERROR(ENOMEM);
  78. av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0);
  79. av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0);
  80. av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0);
  81. av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0);
  82. av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0);
  83. av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0);
  84. if (s->resample)
  85. av_opt_set_int(s->avr, "force_resampling", 1, 0);
  86. if ((ret = avresample_open(s->avr)) < 0)
  87. return ret;
  88. return 0;
  89. }
  90. static int request_frame(AVFilterLink *link)
  91. {
  92. AVFilterContext *ctx = link->src;
  93. ASyncContext *s = ctx->priv;
  94. int ret = 0;
  95. int nb_samples;
  96. s->got_output = 0;
  97. while (ret >= 0 && !s->got_output)
  98. ret = ff_request_frame(ctx->inputs[0]);
  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. return ff_filter_samples(link, buf);
  109. }
  110. return ret;
  111. }
  112. static int write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf)
  113. {
  114. int ret = avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
  115. buf->linesize[0], buf->audio->nb_samples);
  116. avfilter_unref_buffer(buf);
  117. return ret;
  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 int 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, ret;
  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. return write_to_fifo(s, buf);
  140. }
  141. /* now wait for the next timestamp */
  142. if (pts == AV_NOPTS_VALUE) {
  143. return write_to_fifo(s, buf);
  144. }
  145. /* when we have two timestamps, compute how many samples would we have
  146. * to add/remove to get proper sync between data and timestamps */
  147. delta = pts - s->pts - get_delay(s);
  148. out_size = avresample_available(s->avr);
  149. if (labs(delta) > s->min_delta) {
  150. av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta);
  151. out_size = av_clipl_int32((int64_t)out_size + delta);
  152. } else {
  153. if (s->resample) {
  154. int comp = av_clip(delta, -s->max_comp, s->max_comp);
  155. av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
  156. avresample_set_compensation(s->avr, delta, inlink->sample_rate);
  157. }
  158. delta = 0;
  159. }
  160. if (out_size > 0) {
  161. AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
  162. out_size);
  163. if (!buf_out) {
  164. ret = AVERROR(ENOMEM);
  165. goto fail;
  166. }
  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. ret = ff_filter_samples(outlink, buf_out);
  174. if (ret < 0)
  175. goto fail;
  176. s->got_output = 1;
  177. } else {
  178. av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
  179. "whole buffer.\n");
  180. }
  181. /* drain any remaining buffered data */
  182. avresample_read(s->avr, NULL, avresample_available(s->avr));
  183. s->pts = pts - avresample_get_delay(s->avr);
  184. ret = avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
  185. buf->linesize[0], buf->audio->nb_samples);
  186. fail:
  187. avfilter_unref_buffer(buf);
  188. return ret;
  189. }
  190. AVFilter avfilter_af_asyncts = {
  191. .name = "asyncts",
  192. .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"),
  193. .init = init,
  194. .uninit = uninit,
  195. .priv_size = sizeof(ASyncContext),
  196. .inputs = (const AVFilterPad[]) {{ .name = "default",
  197. .type = AVMEDIA_TYPE_AUDIO,
  198. .filter_samples = filter_samples },
  199. { NULL }},
  200. .outputs = (const AVFilterPad[]) {{ .name = "default",
  201. .type = AVMEDIA_TYPE_AUDIO,
  202. .config_props = config_props,
  203. .request_frame = request_frame },
  204. { NULL }},
  205. };