vf_settb.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/rational.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. static const char *var_names[] = {
  30. "E",
  31. "PHI",
  32. "PI",
  33. "AVTB", /* default timebase 1/AV_TIME_BASE */
  34. "intb", /* input timebase */
  35. NULL
  36. };
  37. enum var_name {
  38. VAR_E,
  39. VAR_PHI,
  40. VAR_PI,
  41. VAR_AVTB,
  42. VAR_INTB,
  43. VAR_VARS_NB
  44. };
  45. typedef struct {
  46. char tb_expr[256];
  47. double var_values[VAR_VARS_NB];
  48. } SetTBContext;
  49. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  50. {
  51. SetTBContext *settb = ctx->priv;
  52. av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
  53. if (args)
  54. sscanf(args, "%255[^:]", settb->tb_expr);
  55. return 0;
  56. }
  57. static int config_output_props(AVFilterLink *outlink)
  58. {
  59. AVFilterContext *ctx = outlink->src;
  60. SetTBContext *settb = ctx->priv;
  61. AVFilterLink *inlink = ctx->inputs[0];
  62. AVRational time_base;
  63. int ret;
  64. double res;
  65. settb->var_values[VAR_E] = M_E;
  66. settb->var_values[VAR_PHI] = M_PHI;
  67. settb->var_values[VAR_PI] = M_PI;
  68. settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
  69. settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
  70. outlink->w = inlink->w;
  71. outlink->h = inlink->h;
  72. if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
  73. NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
  74. av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
  75. return ret;
  76. }
  77. time_base = av_d2q(res, INT_MAX);
  78. if (time_base.num <= 0 || time_base.den <= 0) {
  79. av_log(ctx, AV_LOG_ERROR,
  80. "Invalid non-positive values for the timebase num:%d or den:%d.\n",
  81. time_base.num, time_base.den);
  82. return AVERROR(EINVAL);
  83. }
  84. outlink->time_base = time_base;
  85. av_log(outlink->src, AV_LOG_INFO, "tb:%d/%d -> tb:%d/%d\n",
  86. inlink ->time_base.num, inlink ->time_base.den,
  87. outlink->time_base.num, outlink->time_base.den);
  88. return 0;
  89. }
  90. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  91. {
  92. AVFilterContext *ctx = inlink->dst;
  93. AVFilterLink *outlink = ctx->outputs[0];
  94. AVFilterBufferRef *picref2 = picref;
  95. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  96. picref2 = avfilter_ref_buffer(picref, ~0);
  97. picref2->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
  98. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  99. inlink ->time_base.num, inlink ->time_base.den, picref ->pts,
  100. outlink->time_base.num, outlink->time_base.den, picref2->pts);
  101. avfilter_unref_buffer(picref);
  102. }
  103. avfilter_start_frame(outlink, picref2);
  104. }
  105. AVFilter avfilter_vf_settb = {
  106. .name = "settb",
  107. .description = NULL_IF_CONFIG_SMALL("Set timebase for the output link."),
  108. .init = init,
  109. .priv_size = sizeof(SetTBContext),
  110. .inputs = (AVFilterPad[]) {{ .name = "default",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .get_video_buffer = avfilter_null_get_video_buffer,
  113. .start_frame = start_frame,
  114. .end_frame = avfilter_null_end_frame },
  115. { .name = NULL }},
  116. .outputs = (AVFilterPad[]) {{ .name = "default",
  117. .type = AVMEDIA_TYPE_VIDEO,
  118. .config_props = config_output_props, },
  119. { .name = NULL}},
  120. };