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.

185 lines
5.8KB

  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Set timebase for the output link.
  23. */
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/eval.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/rational.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "audio.h"
  31. #include "video.h"
  32. static const char *const var_names[] = {
  33. "AVTB", /* default timebase 1/AV_TIME_BASE */
  34. "intb", /* input timebase */
  35. "sr", /* sample rate */
  36. NULL
  37. };
  38. enum var_name {
  39. VAR_AVTB,
  40. VAR_INTB,
  41. VAR_SR,
  42. VAR_VARS_NB
  43. };
  44. typedef struct {
  45. char tb_expr[256];
  46. double var_values[VAR_VARS_NB];
  47. } SetTBContext;
  48. static av_cold int init(AVFilterContext *ctx, const char *args)
  49. {
  50. SetTBContext *settb = ctx->priv;
  51. av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
  52. if (args)
  53. sscanf(args, "%255[^:]", settb->tb_expr);
  54. return 0;
  55. }
  56. static int config_output_props(AVFilterLink *outlink)
  57. {
  58. AVFilterContext *ctx = outlink->src;
  59. SetTBContext *settb = ctx->priv;
  60. AVFilterLink *inlink = ctx->inputs[0];
  61. AVRational time_base;
  62. int ret;
  63. double res;
  64. settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
  65. settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
  66. settb->var_values[VAR_SR] = inlink->sample_rate;
  67. outlink->w = inlink->w;
  68. outlink->h = inlink->h;
  69. if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
  70. NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
  71. av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
  72. return ret;
  73. }
  74. time_base = av_d2q(res, INT_MAX);
  75. if (time_base.num <= 0 || time_base.den <= 0) {
  76. av_log(ctx, AV_LOG_ERROR,
  77. "Invalid non-positive values for the timebase num:%d or den:%d.\n",
  78. time_base.num, time_base.den);
  79. return AVERROR(EINVAL);
  80. }
  81. outlink->time_base = time_base;
  82. av_log(outlink->src, AV_LOG_VERBOSE, "tb:%d/%d -> tb:%d/%d\n",
  83. inlink ->time_base.num, inlink ->time_base.den,
  84. outlink->time_base.num, outlink->time_base.den);
  85. return 0;
  86. }
  87. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  88. {
  89. AVFilterContext *ctx = inlink->dst;
  90. AVFilterLink *outlink = ctx->outputs[0];
  91. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  92. int64_t orig_pts = picref->pts;
  93. picref->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
  94. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  95. inlink ->time_base.num, inlink ->time_base.den, orig_pts,
  96. outlink->time_base.num, outlink->time_base.den, picref->pts);
  97. }
  98. inlink->cur_buf = NULL;
  99. return ff_start_frame(outlink, picref);
  100. }
  101. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  102. {
  103. AVFilterContext *ctx = inlink->dst;
  104. AVFilterLink *outlink = ctx->outputs[0];
  105. AVFilterBufferRef *outsamples = insamples;
  106. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  107. outsamples = avfilter_ref_buffer(insamples, ~0);
  108. outsamples->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
  109. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  110. inlink ->time_base.num, inlink ->time_base.den, insamples ->pts,
  111. outlink->time_base.num, outlink->time_base.den, outsamples->pts);
  112. avfilter_unref_buffer(insamples);
  113. }
  114. return ff_filter_samples(outlink, outsamples);
  115. }
  116. #if CONFIG_SETTB_FILTER
  117. AVFilter avfilter_vf_settb = {
  118. .name = "settb",
  119. .description = NULL_IF_CONFIG_SMALL("Set timebase for the video output link."),
  120. .init = init,
  121. .priv_size = sizeof(SetTBContext),
  122. .inputs = (const AVFilterPad[]) {
  123. { .name = "default",
  124. .type = AVMEDIA_TYPE_VIDEO,
  125. .get_video_buffer = ff_null_get_video_buffer,
  126. .start_frame = start_frame,
  127. .end_frame = ff_null_end_frame },
  128. { .name = NULL }
  129. },
  130. .outputs = (const AVFilterPad[]) {
  131. { .name = "default",
  132. .type = AVMEDIA_TYPE_VIDEO,
  133. .config_props = config_output_props, },
  134. { .name = NULL}
  135. },
  136. };
  137. #endif
  138. #if CONFIG_ASETTB_FILTER
  139. AVFilter avfilter_af_asettb = {
  140. .name = "asettb",
  141. .description = NULL_IF_CONFIG_SMALL("Set timebase for the audio output link."),
  142. .init = init,
  143. .priv_size = sizeof(SetTBContext),
  144. .inputs = (const AVFilterPad[]) {
  145. { .name = "default",
  146. .type = AVMEDIA_TYPE_AUDIO,
  147. .get_audio_buffer = ff_null_get_audio_buffer,
  148. .filter_samples = filter_samples, },
  149. { .name = NULL }
  150. },
  151. .outputs = (const AVFilterPad[]) {
  152. { .name = "default",
  153. .type = AVMEDIA_TYPE_AUDIO,
  154. .config_props = config_output_props, },
  155. { .name = NULL}
  156. },
  157. };
  158. #endif