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.

231 lines
7.9KB

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