f_setpts.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #include "libavutil/eval.h"
  26. #include "libavutil/internal.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. "FRAME_RATE", ///< defined only for constant frame-rate video
  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_FRAME_RATE,
  53. VAR_INTERLACED,
  54. VAR_N,
  55. VAR_NB_CONSUMED_SAMPLES,
  56. VAR_NB_SAMPLES,
  57. VAR_POS,
  58. VAR_PREV_INPTS,
  59. VAR_PREV_INT,
  60. VAR_PREV_OUTPTS,
  61. VAR_PREV_OUTT,
  62. VAR_PTS,
  63. VAR_SAMPLE_RATE,
  64. VAR_STARTPTS,
  65. VAR_STARTT,
  66. VAR_T,
  67. VAR_TB,
  68. VAR_VARS_NB
  69. };
  70. typedef struct {
  71. AVExpr *expr;
  72. double var_values[VAR_VARS_NB];
  73. enum AVMediaType type;
  74. } SetPTSContext;
  75. static av_cold int init(AVFilterContext *ctx, const char *args)
  76. {
  77. SetPTSContext *setpts = ctx->priv;
  78. int ret;
  79. if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
  80. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  81. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  82. return ret;
  83. }
  84. setpts->var_values[VAR_N ] = 0.0;
  85. setpts->var_values[VAR_PREV_INPTS ] = setpts->var_values[VAR_PREV_INT ] = NAN;
  86. setpts->var_values[VAR_PREV_OUTPTS] = setpts->var_values[VAR_PREV_OUTT] = NAN;
  87. setpts->var_values[VAR_STARTPTS ] = setpts->var_values[VAR_STARTT ] = NAN;
  88. return 0;
  89. }
  90. static int config_input(AVFilterLink *inlink)
  91. {
  92. AVFilterContext *ctx = inlink->dst;
  93. SetPTSContext *setpts = ctx->priv;
  94. setpts->type = inlink->type;
  95. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  96. setpts->var_values[VAR_SAMPLE_RATE] =
  97. setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
  98. setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
  99. av_q2d(inlink->frame_rate) : NAN;
  100. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
  101. setpts->var_values[VAR_TB],
  102. setpts->var_values[VAR_FRAME_RATE],
  103. setpts->var_values[VAR_SAMPLE_RATE]);
  104. return 0;
  105. }
  106. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  107. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  108. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  109. #define BUF_SIZE 64
  110. static inline char *double2int64str(char *buf, double v)
  111. {
  112. if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
  113. else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
  114. return buf;
  115. }
  116. #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
  117. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  118. {
  119. SetPTSContext *setpts = inlink->dst->priv;
  120. int64_t in_pts = frame->pts;
  121. double d;
  122. if (isnan(setpts->var_values[VAR_STARTPTS])) {
  123. setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
  124. setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
  125. }
  126. setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
  127. setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
  128. setpts->var_values[VAR_POS ] = frame->pos == -1 ? NAN : frame->pos;
  129. switch (inlink->type) {
  130. case AVMEDIA_TYPE_VIDEO:
  131. setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
  132. break;
  133. case AVMEDIA_TYPE_AUDIO:
  134. setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
  135. break;
  136. }
  137. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  138. av_log(inlink->dst, AV_LOG_DEBUG,
  139. "N:%"PRId64" PTS:%s T:%f POS:%s",
  140. (int64_t)setpts->var_values[VAR_N],
  141. d2istr(setpts->var_values[VAR_PTS]),
  142. setpts->var_values[VAR_T],
  143. d2istr(setpts->var_values[VAR_POS]));
  144. switch (inlink->type) {
  145. case AVMEDIA_TYPE_VIDEO:
  146. av_log(inlink->dst, AV_LOG_DEBUG, " INTERLACED:%"PRId64,
  147. (int64_t)setpts->var_values[VAR_INTERLACED]);
  148. break;
  149. case AVMEDIA_TYPE_AUDIO:
  150. av_log(inlink->dst, AV_LOG_DEBUG, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
  151. (int64_t)setpts->var_values[VAR_NB_SAMPLES],
  152. (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
  153. break;
  154. }
  155. av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
  156. frame->pts = D2TS(d);
  157. setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
  158. setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
  159. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
  160. setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
  161. setpts->var_values[VAR_N] += 1.0;
  162. if (setpts->type == AVMEDIA_TYPE_AUDIO) {
  163. setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->audio->nb_samples;
  164. }
  165. return ff_filter_frame(inlink->dst->outputs[0], frame);
  166. }
  167. static av_cold void uninit(AVFilterContext *ctx)
  168. {
  169. SetPTSContext *setpts = ctx->priv;
  170. av_expr_free(setpts->expr);
  171. setpts->expr = NULL;
  172. }
  173. #if CONFIG_ASETPTS_FILTER
  174. static const AVFilterPad avfilter_af_asetpts_inputs[] = {
  175. {
  176. .name = "default",
  177. .type = AVMEDIA_TYPE_AUDIO,
  178. .get_audio_buffer = ff_null_get_audio_buffer,
  179. .config_props = config_input,
  180. .filter_frame = filter_frame,
  181. },
  182. { NULL }
  183. };
  184. static const AVFilterPad avfilter_af_asetpts_outputs[] = {
  185. {
  186. .name = "default",
  187. .type = AVMEDIA_TYPE_AUDIO,
  188. },
  189. { NULL }
  190. };
  191. AVFilter avfilter_af_asetpts = {
  192. .name = "asetpts",
  193. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
  194. .init = init,
  195. .uninit = uninit,
  196. .priv_size = sizeof(SetPTSContext),
  197. .inputs = avfilter_af_asetpts_inputs,
  198. .outputs = avfilter_af_asetpts_outputs,
  199. };
  200. #endif /* CONFIG_ASETPTS_FILTER */
  201. #if CONFIG_SETPTS_FILTER
  202. static const AVFilterPad avfilter_vf_setpts_inputs[] = {
  203. {
  204. .name = "default",
  205. .type = AVMEDIA_TYPE_VIDEO,
  206. .get_video_buffer = ff_null_get_video_buffer,
  207. .config_props = config_input,
  208. .filter_frame = filter_frame,
  209. },
  210. { NULL }
  211. };
  212. static const AVFilterPad avfilter_vf_setpts_outputs[] = {
  213. {
  214. .name = "default",
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. },
  217. { NULL }
  218. };
  219. AVFilter avfilter_vf_setpts = {
  220. .name = "setpts",
  221. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  222. .init = init,
  223. .uninit = uninit,
  224. .priv_size = sizeof(SetPTSContext),
  225. .inputs = avfilter_vf_setpts_inputs,
  226. .outputs = avfilter_vf_setpts_outputs,
  227. };
  228. #endif /* CONFIG_SETPTS_FILTER */