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.

217 lines
7.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 "audio.h"
  31. #include "video.h"
  32. static const char *const var_names[] = {
  33. "INTERLACED", ///< tell if the current frame is interlaced
  34. "N", ///< frame number (starting at zero)
  35. "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
  36. "NB_SAMPLES", ///< number of samples in the current frame (only audio)
  37. "POS", ///< original position in the file of the frame
  38. "PREV_INPTS", ///< previous input PTS
  39. "PREV_OUTPTS", ///< previous output PTS
  40. "PTS", ///< original pts in the file of the frame
  41. "SAMPLE_RATE", ///< sample rate (only audio)
  42. "STARTPTS", ///< PTS at start of movie
  43. "TB", ///< timebase
  44. NULL
  45. };
  46. enum var_name {
  47. VAR_INTERLACED,
  48. VAR_N,
  49. VAR_NB_CONSUMED_SAMPLES,
  50. VAR_NB_SAMPLES,
  51. VAR_POS,
  52. VAR_PREV_INPTS,
  53. VAR_PREV_OUTPTS,
  54. VAR_PTS,
  55. VAR_SAMPLE_RATE,
  56. VAR_STARTPTS,
  57. VAR_TB,
  58. VAR_VARS_NB
  59. };
  60. typedef struct {
  61. AVExpr *expr;
  62. double var_values[VAR_VARS_NB];
  63. enum AVMediaType type;
  64. } SetPTSContext;
  65. static av_cold int init(AVFilterContext *ctx, const char *args)
  66. {
  67. SetPTSContext *setpts = ctx->priv;
  68. int ret;
  69. if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
  70. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  71. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  72. return ret;
  73. }
  74. setpts->var_values[VAR_N ] = 0.0;
  75. setpts->var_values[VAR_PREV_INPTS ] = NAN;
  76. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  77. setpts->var_values[VAR_STARTPTS ] = NAN;
  78. return 0;
  79. }
  80. static int config_input(AVFilterLink *inlink)
  81. {
  82. AVFilterContext *ctx = inlink->dst;
  83. SetPTSContext *setpts = ctx->priv;
  84. setpts->type = inlink->type;
  85. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  86. if (setpts->type == AVMEDIA_TYPE_AUDIO)
  87. setpts->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
  88. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f SAMPLE_RATE:%f\n",
  89. setpts->var_values[VAR_TB], setpts->var_values[VAR_SAMPLE_RATE]);
  90. return 0;
  91. }
  92. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  93. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  94. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  95. {
  96. SetPTSContext *setpts = inlink->dst->priv;
  97. double d;
  98. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  99. if (!outpicref)
  100. return AVERROR(ENOMEM);
  101. if (isnan(setpts->var_values[VAR_STARTPTS]))
  102. setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
  103. setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
  104. setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
  105. switch (inlink->type) {
  106. case AVMEDIA_TYPE_VIDEO:
  107. setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
  108. break;
  109. case AVMEDIA_TYPE_AUDIO:
  110. setpts->var_values[VAR_NB_SAMPLES] = inpicref->audio->nb_samples;
  111. break;
  112. }
  113. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  114. outpicref->pts = D2TS(d);
  115. #ifdef DEBUG
  116. av_log(inlink->dst, AV_LOG_DEBUG,
  117. "n:%"PRId64" interlaced:%d nb_samples:%d nb_consumed_samples:%d "
  118. "pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
  119. (int64_t)setpts->var_values[VAR_N],
  120. (int)setpts->var_values[VAR_INTERLACED],
  121. (int)setpts->var_values[VAR_NB_SAMPLES],
  122. (int)setpts->var_values[VAR_NB_CONSUMED_SAMPLES],
  123. inpicref ->pos,
  124. inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
  125. outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
  126. #endif
  127. setpts->var_values[VAR_N] += 1.0;
  128. setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
  129. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
  130. if (setpts->type == AVMEDIA_TYPE_AUDIO) {
  131. setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += inpicref->audio->nb_samples;
  132. return ff_filter_samples(inlink->dst->outputs[0], outpicref);
  133. } else
  134. return ff_start_frame (inlink->dst->outputs[0], outpicref);
  135. }
  136. static av_cold void uninit(AVFilterContext *ctx)
  137. {
  138. SetPTSContext *setpts = ctx->priv;
  139. av_expr_free(setpts->expr);
  140. setpts->expr = NULL;
  141. }
  142. #if CONFIG_ASETPTS_FILTER
  143. AVFilter avfilter_af_asetpts = {
  144. .name = "asetpts",
  145. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
  146. .init = init,
  147. .uninit = uninit,
  148. .priv_size = sizeof(SetPTSContext),
  149. .inputs = (const AVFilterPad[]) {
  150. {
  151. .name = "default",
  152. .type = AVMEDIA_TYPE_AUDIO,
  153. .get_audio_buffer = ff_null_get_audio_buffer,
  154. .config_props = config_input,
  155. .filter_samples = filter_frame,
  156. },
  157. { .name = NULL }
  158. },
  159. .outputs = (const AVFilterPad[]) {
  160. {
  161. .name = "default",
  162. .type = AVMEDIA_TYPE_AUDIO,
  163. },
  164. { .name = NULL }
  165. },
  166. };
  167. #endif /* CONFIG_ASETPTS_FILTER */
  168. #if CONFIG_SETPTS_FILTER
  169. AVFilter avfilter_vf_setpts = {
  170. .name = "setpts",
  171. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  172. .init = init,
  173. .uninit = uninit,
  174. .priv_size = sizeof(SetPTSContext),
  175. .inputs = (const AVFilterPad[]) {{ .name = "default",
  176. .type = AVMEDIA_TYPE_VIDEO,
  177. .get_video_buffer = ff_null_get_video_buffer,
  178. .config_props = config_input,
  179. .start_frame = filter_frame, },
  180. { .name = NULL }},
  181. .outputs = (const AVFilterPad[]) {{ .name = "default",
  182. .type = AVMEDIA_TYPE_VIDEO, },
  183. { .name = NULL}},
  184. };
  185. #endif /* CONFIG_SETPTS_FILTER */