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.

157 lines
4.4KB

  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 <inttypes.h>
  25. #include <stdio.h>
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/rational.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. static const char *const var_names[] = {
  35. "E",
  36. "PHI",
  37. "PI",
  38. "AVTB", /* default timebase 1/AV_TIME_BASE */
  39. "intb", /* input timebase */
  40. NULL
  41. };
  42. enum var_name {
  43. VAR_E,
  44. VAR_PHI,
  45. VAR_PI,
  46. VAR_AVTB,
  47. VAR_INTB,
  48. VAR_VARS_NB
  49. };
  50. typedef struct {
  51. char tb_expr[256];
  52. double var_values[VAR_VARS_NB];
  53. } SetTBContext;
  54. static av_cold int init(AVFilterContext *ctx, const char *args)
  55. {
  56. SetTBContext *settb = ctx->priv;
  57. av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
  58. if (args)
  59. sscanf(args, "%255[^:]", settb->tb_expr);
  60. return 0;
  61. }
  62. static int config_output_props(AVFilterLink *outlink)
  63. {
  64. AVFilterContext *ctx = outlink->src;
  65. SetTBContext *settb = ctx->priv;
  66. AVFilterLink *inlink = ctx->inputs[0];
  67. AVRational time_base;
  68. int ret;
  69. double res;
  70. settb->var_values[VAR_E] = M_E;
  71. settb->var_values[VAR_PHI] = M_PHI;
  72. settb->var_values[VAR_PI] = M_PI;
  73. settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
  74. settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
  75. outlink->w = inlink->w;
  76. outlink->h = inlink->h;
  77. if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
  78. NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
  79. av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
  80. return ret;
  81. }
  82. time_base = av_d2q(res, INT_MAX);
  83. if (time_base.num <= 0 || time_base.den <= 0) {
  84. av_log(ctx, AV_LOG_ERROR,
  85. "Invalid non-positive values for the timebase num:%d or den:%d.\n",
  86. time_base.num, time_base.den);
  87. return AVERROR(EINVAL);
  88. }
  89. outlink->time_base = time_base;
  90. av_log(outlink->src, AV_LOG_VERBOSE, "tb:%d/%d -> tb:%d/%d\n",
  91. inlink ->time_base.num, inlink ->time_base.den,
  92. outlink->time_base.num, outlink->time_base.den);
  93. return 0;
  94. }
  95. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  96. {
  97. AVFilterContext *ctx = inlink->dst;
  98. AVFilterLink *outlink = ctx->outputs[0];
  99. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  100. int64_t orig_pts = frame->pts;
  101. frame->pts = av_rescale_q(frame->pts, inlink->time_base, outlink->time_base);
  102. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  103. inlink ->time_base.num, inlink ->time_base.den, orig_pts,
  104. outlink->time_base.num, outlink->time_base.den, frame->pts);
  105. }
  106. return ff_filter_frame(outlink, frame);
  107. }
  108. static const AVFilterPad avfilter_vf_settb_inputs[] = {
  109. {
  110. .name = "default",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .get_video_buffer = ff_null_get_video_buffer,
  113. .filter_frame = filter_frame,
  114. },
  115. { NULL }
  116. };
  117. static const AVFilterPad avfilter_vf_settb_outputs[] = {
  118. {
  119. .name = "default",
  120. .type = AVMEDIA_TYPE_VIDEO,
  121. .config_props = config_output_props,
  122. },
  123. { NULL }
  124. };
  125. AVFilter avfilter_vf_settb = {
  126. .name = "settb",
  127. .description = NULL_IF_CONFIG_SMALL("Set timebase for the output link."),
  128. .init = init,
  129. .priv_size = sizeof(SetTBContext),
  130. .inputs = avfilter_vf_settb_inputs,
  131. .outputs = avfilter_vf_settb_outputs,
  132. };