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.

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