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.

155 lines
5.0KB

  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 "libavutil/mathematics.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. static const char *const var_names[] = {
  32. "INTERLACED", ///< tell if the current frame is interlaced
  33. "N", ///< frame number (starting at zero)
  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_INTERLACED,
  44. VAR_N,
  45. VAR_POS,
  46. VAR_PREV_INPTS,
  47. VAR_PREV_OUTPTS,
  48. VAR_PTS,
  49. VAR_STARTPTS,
  50. VAR_TB,
  51. VAR_VARS_NB
  52. };
  53. typedef struct {
  54. AVExpr *expr;
  55. double var_values[VAR_VARS_NB];
  56. } SetPTSContext;
  57. static av_cold int init(AVFilterContext *ctx, const char *args)
  58. {
  59. SetPTSContext *setpts = ctx->priv;
  60. int ret;
  61. if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
  62. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  63. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  64. return ret;
  65. }
  66. setpts->var_values[VAR_N ] = 0.0;
  67. setpts->var_values[VAR_PREV_INPTS ] = NAN;
  68. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  69. setpts->var_values[VAR_STARTPTS ] = NAN;
  70. return 0;
  71. }
  72. static int config_input(AVFilterLink *inlink)
  73. {
  74. SetPTSContext *setpts = inlink->dst->priv;
  75. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  76. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
  77. return 0;
  78. }
  79. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  80. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  81. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  82. {
  83. SetPTSContext *setpts = inlink->dst->priv;
  84. double d;
  85. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  86. if (!outpicref)
  87. return AVERROR(ENOMEM);
  88. if (isnan(setpts->var_values[VAR_STARTPTS]))
  89. setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
  90. setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
  91. setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
  92. setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
  93. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  94. outpicref->pts = D2TS(d);
  95. #ifdef DEBUG
  96. av_log(inlink->dst, AV_LOG_DEBUG,
  97. "n:%"PRId64" interlaced:%d pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
  98. (int64_t)setpts->var_values[VAR_N],
  99. (int)setpts->var_values[VAR_INTERLACED],
  100. inpicref ->pos,
  101. inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
  102. outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
  103. #endif
  104. setpts->var_values[VAR_N] += 1.0;
  105. setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
  106. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
  107. return ff_start_frame(inlink->dst->outputs[0], outpicref);
  108. }
  109. static av_cold void uninit(AVFilterContext *ctx)
  110. {
  111. SetPTSContext *setpts = ctx->priv;
  112. av_expr_free(setpts->expr);
  113. setpts->expr = NULL;
  114. }
  115. AVFilter avfilter_vf_setpts = {
  116. .name = "setpts",
  117. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  118. .init = init,
  119. .uninit = uninit,
  120. .priv_size = sizeof(SetPTSContext),
  121. .inputs = (const AVFilterPad[]) {{ .name = "default",
  122. .type = AVMEDIA_TYPE_VIDEO,
  123. .get_video_buffer = ff_null_get_video_buffer,
  124. .config_props = config_input,
  125. .start_frame = start_frame, },
  126. { .name = NULL }},
  127. .outputs = (const AVFilterPad[]) {{ .name = "default",
  128. .type = AVMEDIA_TYPE_VIDEO, },
  129. { .name = NULL}},
  130. };