f_setpts.c 8.2 KB

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