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.

345 lines
11KB

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