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.

305 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 ] = setpts->var_values[VAR_PREV_INT ] = NAN;
  94. setpts->var_values[VAR_PREV_OUTPTS] = setpts->var_values[VAR_PREV_OUTT] = NAN;
  95. setpts->var_values[VAR_STARTPTS ] = setpts->var_values[VAR_STARTT ] = NAN;
  96. return 0;
  97. }
  98. static int config_input(AVFilterLink *inlink)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. SetPTSContext *setpts = ctx->priv;
  102. setpts->type = inlink->type;
  103. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  104. setpts->var_values[VAR_RTCSTART] = av_gettime();
  105. setpts->var_values[VAR_SAMPLE_RATE] =
  106. setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
  107. setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
  108. av_q2d(inlink->frame_rate) : NAN;
  109. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
  110. setpts->var_values[VAR_TB],
  111. setpts->var_values[VAR_FRAME_RATE],
  112. setpts->var_values[VAR_SAMPLE_RATE]);
  113. return 0;
  114. }
  115. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  116. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  117. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  118. #define BUF_SIZE 64
  119. static inline char *double2int64str(char *buf, double v)
  120. {
  121. if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
  122. else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
  123. return buf;
  124. }
  125. #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
  126. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  127. {
  128. SetPTSContext *setpts = inlink->dst->priv;
  129. int64_t in_pts = frame->pts;
  130. double d;
  131. if (isnan(setpts->var_values[VAR_STARTPTS])) {
  132. setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
  133. setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
  134. }
  135. setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
  136. setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
  137. setpts->var_values[VAR_POS ] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
  138. setpts->var_values[VAR_RTCTIME ] = av_gettime();
  139. switch (inlink->type) {
  140. case AVMEDIA_TYPE_VIDEO:
  141. setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
  142. break;
  143. case AVMEDIA_TYPE_AUDIO:
  144. setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
  145. break;
  146. }
  147. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  148. av_log(inlink->dst, AV_LOG_DEBUG,
  149. "N:%"PRId64" PTS:%s T:%f POS:%s",
  150. (int64_t)setpts->var_values[VAR_N],
  151. d2istr(setpts->var_values[VAR_PTS]),
  152. setpts->var_values[VAR_T],
  153. d2istr(setpts->var_values[VAR_POS]));
  154. switch (inlink->type) {
  155. case AVMEDIA_TYPE_VIDEO:
  156. av_log(inlink->dst, AV_LOG_DEBUG, " INTERLACED:%"PRId64,
  157. (int64_t)setpts->var_values[VAR_INTERLACED]);
  158. break;
  159. case AVMEDIA_TYPE_AUDIO:
  160. av_log(inlink->dst, AV_LOG_DEBUG, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
  161. (int64_t)setpts->var_values[VAR_NB_SAMPLES],
  162. (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
  163. break;
  164. }
  165. av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
  166. frame->pts = D2TS(d);
  167. setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
  168. setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
  169. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
  170. setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
  171. setpts->var_values[VAR_N] += 1.0;
  172. if (setpts->type == AVMEDIA_TYPE_AUDIO) {
  173. setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
  174. }
  175. return ff_filter_frame(inlink->dst->outputs[0], frame);
  176. }
  177. static av_cold void uninit(AVFilterContext *ctx)
  178. {
  179. SetPTSContext *setpts = ctx->priv;
  180. av_expr_free(setpts->expr);
  181. setpts->expr = NULL;
  182. }
  183. #if CONFIG_ASETPTS_FILTER
  184. #define OFFSET(x) offsetof(SetPTSContext, x)
  185. #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  186. static const AVOption aoptions[] = {
  187. { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = AFLAGS },
  188. { NULL },
  189. };
  190. static const AVClass asetpts_class = {
  191. .class_name = "asetpts",
  192. .item_name = av_default_item_name,
  193. .option = aoptions,
  194. .version = LIBAVUTIL_VERSION_INT,
  195. };
  196. static const AVFilterPad avfilter_af_asetpts_inputs[] = {
  197. {
  198. .name = "default",
  199. .type = AVMEDIA_TYPE_AUDIO,
  200. .get_audio_buffer = ff_null_get_audio_buffer,
  201. .config_props = config_input,
  202. .filter_frame = filter_frame,
  203. },
  204. { NULL }
  205. };
  206. static const AVFilterPad avfilter_af_asetpts_outputs[] = {
  207. {
  208. .name = "default",
  209. .type = AVMEDIA_TYPE_AUDIO,
  210. },
  211. { NULL }
  212. };
  213. AVFilter avfilter_af_asetpts = {
  214. .name = "asetpts",
  215. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
  216. .init = init,
  217. .uninit = uninit,
  218. .priv_size = sizeof(SetPTSContext),
  219. .priv_class= &asetpts_class,
  220. .inputs = avfilter_af_asetpts_inputs,
  221. .outputs = avfilter_af_asetpts_outputs,
  222. };
  223. #endif /* CONFIG_ASETPTS_FILTER */
  224. #if CONFIG_SETPTS_FILTER
  225. #define OFFSET(x) offsetof(SetPTSContext, x)
  226. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  227. static const AVOption options[] = {
  228. { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
  229. { NULL },
  230. };
  231. static const AVClass setpts_class = {
  232. .class_name = "setpts",
  233. .item_name = av_default_item_name,
  234. .option = options,
  235. .version = LIBAVUTIL_VERSION_INT,
  236. };
  237. static const AVFilterPad avfilter_vf_setpts_inputs[] = {
  238. {
  239. .name = "default",
  240. .type = AVMEDIA_TYPE_VIDEO,
  241. .get_video_buffer = ff_null_get_video_buffer,
  242. .config_props = config_input,
  243. .filter_frame = filter_frame,
  244. },
  245. { NULL }
  246. };
  247. static const AVFilterPad avfilter_vf_setpts_outputs[] = {
  248. {
  249. .name = "default",
  250. .type = AVMEDIA_TYPE_VIDEO,
  251. },
  252. { NULL }
  253. };
  254. AVFilter avfilter_vf_setpts = {
  255. .name = "setpts",
  256. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  257. .init = init,
  258. .uninit = uninit,
  259. .priv_size = sizeof(SetPTSContext),
  260. .priv_class = &setpts_class,
  261. .inputs = avfilter_vf_setpts_inputs,
  262. .outputs = avfilter_vf_setpts_outputs,
  263. };
  264. #endif /* CONFIG_SETPTS_FILTER */