setpts.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <inttypes.h>
  26. #include "libavutil/eval.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/time.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. static const char *const var_names[] = {
  36. "FRAME_RATE", ///< defined only for constant frame-rate video
  37. "INTERLACED", ///< tell if the current frame is interlaced
  38. "N", ///< frame / sample number (starting at zero)
  39. "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
  40. "NB_SAMPLES", ///< number of samples in the current frame (only audio)
  41. "POS", ///< original position in the file of the frame
  42. "PREV_INPTS", ///< previous input PTS
  43. "PREV_INT", ///< previous input time in seconds
  44. "PREV_OUTPTS", ///< previous output PTS
  45. "PREV_OUTT", ///< previous output time in seconds
  46. "PTS", ///< original pts in the file of the frame
  47. "SAMPLE_RATE", ///< sample rate (only audio)
  48. "STARTPTS", ///< PTS at start of movie
  49. "STARTT", ///< time at start of movie
  50. "T", ///< original time in the file of the frame
  51. "TB", ///< timebase
  52. "RTCTIME", ///< wallclock (RTC) time in micro seconds
  53. "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
  54. "S", // Number of samples in the current frame
  55. "SR", // Audio sample rate
  56. NULL
  57. };
  58. enum var_name {
  59. VAR_FRAME_RATE,
  60. VAR_INTERLACED,
  61. VAR_N,
  62. VAR_NB_CONSUMED_SAMPLES,
  63. VAR_NB_SAMPLES,
  64. VAR_POS,
  65. VAR_PREV_INPTS,
  66. VAR_PREV_INT,
  67. VAR_PREV_OUTPTS,
  68. VAR_PREV_OUTT,
  69. VAR_PTS,
  70. VAR_SAMPLE_RATE,
  71. VAR_STARTPTS,
  72. VAR_STARTT,
  73. VAR_T,
  74. VAR_TB,
  75. VAR_RTCTIME,
  76. VAR_RTCSTART,
  77. VAR_S,
  78. VAR_SR,
  79. VAR_VARS_NB
  80. };
  81. typedef struct SetPTSContext {
  82. const AVClass *class;
  83. char *expr_str;
  84. AVExpr *expr;
  85. double var_values[VAR_VARS_NB];
  86. enum AVMediaType type;
  87. } SetPTSContext;
  88. static av_cold int init(AVFilterContext *ctx)
  89. {
  90. SetPTSContext *setpts = ctx->priv;
  91. int ret;
  92. if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
  93. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  94. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
  95. return ret;
  96. }
  97. setpts->var_values[VAR_N] = 0.0;
  98. setpts->var_values[VAR_S] = 0.0;
  99. setpts->var_values[VAR_PREV_INPTS] = NAN;
  100. setpts->var_values[VAR_PREV_INT] = NAN;
  101. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  102. setpts->var_values[VAR_PREV_OUTT] = NAN;
  103. setpts->var_values[VAR_STARTPTS] = NAN;
  104. setpts->var_values[VAR_STARTT] = NAN;
  105. return 0;
  106. }
  107. static int config_input(AVFilterLink *inlink)
  108. {
  109. AVFilterContext *ctx = inlink->dst;
  110. SetPTSContext *setpts = ctx->priv;
  111. setpts->type = inlink->type;
  112. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  113. setpts->var_values[VAR_RTCSTART] = av_gettime();
  114. setpts->var_values[VAR_SR] =
  115. setpts->var_values[VAR_SAMPLE_RATE] =
  116. setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
  117. setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
  118. av_q2d(inlink->frame_rate) : NAN;
  119. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
  120. setpts->var_values[VAR_TB],
  121. setpts->var_values[VAR_FRAME_RATE],
  122. setpts->var_values[VAR_SAMPLE_RATE]);
  123. return 0;
  124. }
  125. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  126. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  127. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  128. #define BUF_SIZE 64
  129. static inline char *double2int64str(char *buf, double v)
  130. {
  131. if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
  132. else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
  133. return buf;
  134. }
  135. #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
  136. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  137. {
  138. SetPTSContext *setpts = inlink->dst->priv;
  139. int64_t in_pts = frame->pts;
  140. double d;
  141. if (isnan(setpts->var_values[VAR_STARTPTS])) {
  142. setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
  143. setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
  144. }
  145. setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
  146. setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
  147. setpts->var_values[VAR_POS ] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
  148. setpts->var_values[VAR_RTCTIME ] = av_gettime();
  149. if (inlink->type == AVMEDIA_TYPE_VIDEO) {
  150. setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
  151. } else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
  152. setpts->var_values[VAR_S] = frame->nb_samples;
  153. setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
  154. }
  155. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  156. frame->pts = D2TS(d);
  157. av_dlog(inlink->dst,
  158. "N:%"PRId64" PTS:%s T:%f POS:%s",
  159. (int64_t)setpts->var_values[VAR_N],
  160. d2istr(setpts->var_values[VAR_PTS]),
  161. setpts->var_values[VAR_T],
  162. d2istr(setpts->var_values[VAR_POS]));
  163. switch (inlink->type) {
  164. case AVMEDIA_TYPE_VIDEO:
  165. av_dlog(inlink->dst, " INTERLACED:%"PRId64,
  166. (int64_t)setpts->var_values[VAR_INTERLACED]);
  167. break;
  168. case AVMEDIA_TYPE_AUDIO:
  169. av_dlog(inlink->dst, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
  170. (int64_t)setpts->var_values[VAR_NB_SAMPLES],
  171. (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
  172. break;
  173. }
  174. av_dlog(inlink->dst, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
  175. if (inlink->type == AVMEDIA_TYPE_VIDEO) {
  176. setpts->var_values[VAR_N] += 1.0;
  177. } else {
  178. setpts->var_values[VAR_N] += frame->nb_samples;
  179. }
  180. setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
  181. setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
  182. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
  183. setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
  184. if (setpts->type == AVMEDIA_TYPE_AUDIO) {
  185. setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
  186. }
  187. return ff_filter_frame(inlink->dst->outputs[0], frame);
  188. }
  189. static av_cold void uninit(AVFilterContext *ctx)
  190. {
  191. SetPTSContext *setpts = ctx->priv;
  192. av_expr_free(setpts->expr);
  193. setpts->expr = NULL;
  194. }
  195. #define OFFSET(x) offsetof(SetPTSContext, x)
  196. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  197. static const AVOption options[] = {
  198. { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
  199. { NULL }
  200. };
  201. #if CONFIG_SETPTS_FILTER
  202. #define setpts_options options
  203. AVFILTER_DEFINE_CLASS(setpts);
  204. static const AVFilterPad avfilter_vf_setpts_inputs[] = {
  205. {
  206. .name = "default",
  207. .type = AVMEDIA_TYPE_VIDEO,
  208. .config_props = config_input,
  209. .filter_frame = filter_frame,
  210. },
  211. { NULL }
  212. };
  213. static const AVFilterPad avfilter_vf_setpts_outputs[] = {
  214. {
  215. .name = "default",
  216. .type = AVMEDIA_TYPE_VIDEO,
  217. },
  218. { NULL }
  219. };
  220. AVFilter ff_vf_setpts = {
  221. .name = "setpts",
  222. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  223. .init = init,
  224. .uninit = uninit,
  225. .priv_size = sizeof(SetPTSContext),
  226. .priv_class = &setpts_class,
  227. .inputs = avfilter_vf_setpts_inputs,
  228. .outputs = avfilter_vf_setpts_outputs,
  229. };
  230. #endif /* CONFIG_SETPTS_FILTER */
  231. #if CONFIG_ASETPTS_FILTER
  232. #define asetpts_options options
  233. AVFILTER_DEFINE_CLASS(asetpts);
  234. static const AVFilterPad asetpts_inputs[] = {
  235. {
  236. .name = "default",
  237. .type = AVMEDIA_TYPE_AUDIO,
  238. .config_props = config_input,
  239. .filter_frame = filter_frame,
  240. },
  241. { NULL }
  242. };
  243. static const AVFilterPad asetpts_outputs[] = {
  244. {
  245. .name = "default",
  246. .type = AVMEDIA_TYPE_AUDIO,
  247. },
  248. { NULL }
  249. };
  250. AVFilter ff_af_asetpts = {
  251. .name = "asetpts",
  252. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
  253. .init = init,
  254. .uninit = uninit,
  255. .priv_size = sizeof(SetPTSContext),
  256. .priv_class = &asetpts_class,
  257. .inputs = asetpts_inputs,
  258. .outputs = asetpts_outputs,
  259. };
  260. #endif /* CONFIG_ASETPTS_FILTER */