vf_setpts.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2008 Victor Paesa
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * video presentation timestamp (PTS) modification filter
  24. */
  25. /* #define DEBUG */
  26. #include "libavutil/eval.h"
  27. #include "avfilter.h"
  28. static const char *var_names[] = {
  29. "E", ///< Euler number
  30. "INTERLACED", ///< tell if the current frame is interlaced
  31. "N", ///< frame number (starting at zero)
  32. "PHI", ///< golden ratio
  33. "PI", ///< greek pi
  34. "POS", ///< original position in the file of the frame
  35. "PREV_INPTS", ///< previous input PTS
  36. "PREV_OUTPTS", ///< previous output PTS
  37. "PTS", ///< original pts in the file of the frame
  38. "STARTPTS", ///< PTS at start of movie
  39. "TB", ///< timebase
  40. NULL
  41. };
  42. enum var_name {
  43. VAR_E,
  44. VAR_INTERLACED,
  45. VAR_N,
  46. VAR_PHI,
  47. VAR_PI,
  48. VAR_POS,
  49. VAR_PREV_INPTS,
  50. VAR_PREV_OUTPTS,
  51. VAR_PTS,
  52. VAR_STARTPTS,
  53. VAR_TB,
  54. VAR_VARS_NB
  55. };
  56. typedef struct {
  57. AVExpr *expr;
  58. double var_values[VAR_VARS_NB];
  59. } SetPTSContext;
  60. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  61. {
  62. SetPTSContext *setpts = ctx->priv;
  63. int ret;
  64. if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
  65. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  66. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  67. return ret;
  68. }
  69. setpts->var_values[VAR_E ] = M_E;
  70. setpts->var_values[VAR_N ] = 0.0;
  71. setpts->var_values[VAR_PHI ] = M_PHI;
  72. setpts->var_values[VAR_PI ] = M_PI;
  73. setpts->var_values[VAR_PREV_INPTS ] = NAN;
  74. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  75. setpts->var_values[VAR_STARTPTS ] = NAN;
  76. return 0;
  77. }
  78. static int config_input(AVFilterLink *inlink)
  79. {
  80. SetPTSContext *setpts = inlink->dst->priv;
  81. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  82. av_log(inlink->src, AV_LOG_INFO, "TB:%f\n", setpts->var_values[VAR_TB]);
  83. return 0;
  84. }
  85. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  86. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  87. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  88. {
  89. SetPTSContext *setpts = inlink->dst->priv;
  90. double d;
  91. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  92. if (isnan(setpts->var_values[VAR_STARTPTS]))
  93. setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
  94. setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
  95. setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
  96. setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
  97. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  98. outpicref->pts = D2TS(d);
  99. #ifdef DEBUG
  100. av_log(inlink->dst, AV_LOG_DEBUG,
  101. "n:%"PRId64" interlaced:%d pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
  102. (int64_t)setpts->var_values[VAR_N],
  103. (int)setpts->var_values[VAR_INTERLACED],
  104. inpicref ->pos,
  105. inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
  106. outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
  107. #endif
  108. setpts->var_values[VAR_N] += 1.0;
  109. setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
  110. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
  111. avfilter_start_frame(inlink->dst->outputs[0], outpicref);
  112. }
  113. static av_cold void uninit(AVFilterContext *ctx)
  114. {
  115. SetPTSContext *setpts = ctx->priv;
  116. av_expr_free(setpts->expr);
  117. setpts->expr = NULL;
  118. }
  119. AVFilter avfilter_vf_setpts = {
  120. .name = "setpts",
  121. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  122. .init = init,
  123. .uninit = uninit,
  124. .priv_size = sizeof(SetPTSContext),
  125. .inputs = (AVFilterPad[]) {{ .name = "default",
  126. .type = AVMEDIA_TYPE_VIDEO,
  127. .get_video_buffer = avfilter_null_get_video_buffer,
  128. .config_props = config_input,
  129. .start_frame = start_frame, },
  130. { .name = NULL }},
  131. .outputs = (AVFilterPad[]) {{ .name = "default",
  132. .type = AVMEDIA_TYPE_VIDEO, },
  133. { .name = NULL}},
  134. };