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.

308 lines
9.7KB

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