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.

186 lines
5.8KB

  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Set timebase for the output link.
  23. */
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/eval.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/rational.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. "AVTB", /* default timebase 1/AV_TIME_BASE */
  34. "intb", /* input timebase */
  35. "sr", /* sample rate */
  36. NULL
  37. };
  38. enum var_name {
  39. VAR_AVTB,
  40. VAR_INTB,
  41. VAR_SR,
  42. VAR_VARS_NB
  43. };
  44. typedef struct {
  45. char tb_expr[256];
  46. double var_values[VAR_VARS_NB];
  47. } SetTBContext;
  48. static av_cold int init(AVFilterContext *ctx, const char *args)
  49. {
  50. SetTBContext *settb = ctx->priv;
  51. av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
  52. if (args)
  53. sscanf(args, "%255[^:]", settb->tb_expr);
  54. return 0;
  55. }
  56. static int config_output_props(AVFilterLink *outlink)
  57. {
  58. AVFilterContext *ctx = outlink->src;
  59. SetTBContext *settb = ctx->priv;
  60. AVFilterLink *inlink = ctx->inputs[0];
  61. AVRational time_base;
  62. int ret;
  63. double res;
  64. settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
  65. settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
  66. settb->var_values[VAR_SR] = inlink->sample_rate;
  67. outlink->w = inlink->w;
  68. outlink->h = inlink->h;
  69. if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
  70. NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
  71. av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
  72. return ret;
  73. }
  74. time_base = av_d2q(res, INT_MAX);
  75. if (time_base.num <= 0 || time_base.den <= 0) {
  76. av_log(ctx, AV_LOG_ERROR,
  77. "Invalid non-positive values for the timebase num:%d or den:%d.\n",
  78. time_base.num, time_base.den);
  79. return AVERROR(EINVAL);
  80. }
  81. outlink->time_base = time_base;
  82. av_log(outlink->src, AV_LOG_VERBOSE, "tb:%d/%d -> tb:%d/%d\n",
  83. inlink ->time_base.num, inlink ->time_base.den,
  84. outlink->time_base.num, outlink->time_base.den);
  85. return 0;
  86. }
  87. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  88. {
  89. AVFilterContext *ctx = inlink->dst;
  90. AVFilterLink *outlink = ctx->outputs[0];
  91. AVFilterBufferRef *picref2 = picref;
  92. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  93. picref2 = avfilter_ref_buffer(picref, ~0);
  94. picref2->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
  95. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  96. inlink ->time_base.num, inlink ->time_base.den, picref ->pts,
  97. outlink->time_base.num, outlink->time_base.den, picref2->pts);
  98. avfilter_unref_buffer(picref);
  99. }
  100. ff_start_frame(outlink, picref2);
  101. }
  102. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  103. {
  104. AVFilterContext *ctx = inlink->dst;
  105. AVFilterLink *outlink = ctx->outputs[0];
  106. AVFilterBufferRef *outsamples = insamples;
  107. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  108. outsamples = avfilter_ref_buffer(insamples, ~0);
  109. outsamples->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
  110. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  111. inlink ->time_base.num, inlink ->time_base.den, insamples ->pts,
  112. outlink->time_base.num, outlink->time_base.den, outsamples->pts);
  113. avfilter_unref_buffer(insamples);
  114. }
  115. return ff_filter_samples(outlink, outsamples);
  116. }
  117. #if CONFIG_SETTB_FILTER
  118. AVFilter avfilter_vf_settb = {
  119. .name = "settb",
  120. .description = NULL_IF_CONFIG_SMALL("Set timebase for the video output link."),
  121. .init = init,
  122. .priv_size = sizeof(SetTBContext),
  123. .inputs = (const AVFilterPad[]) {
  124. { .name = "default",
  125. .type = AVMEDIA_TYPE_VIDEO,
  126. .get_video_buffer = ff_null_get_video_buffer,
  127. .start_frame = start_frame,
  128. .end_frame = ff_null_end_frame },
  129. { .name = NULL }
  130. },
  131. .outputs = (const AVFilterPad[]) {
  132. { .name = "default",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .config_props = config_output_props, },
  135. { .name = NULL}
  136. },
  137. };
  138. #endif
  139. #if CONFIG_ASETTB_FILTER
  140. AVFilter avfilter_af_asettb = {
  141. .name = "asettb",
  142. .description = NULL_IF_CONFIG_SMALL("Set timebase for the audio output link."),
  143. .init = init,
  144. .priv_size = sizeof(SetTBContext),
  145. .inputs = (const AVFilterPad[]) {
  146. { .name = "default",
  147. .type = AVMEDIA_TYPE_AUDIO,
  148. .get_audio_buffer = ff_null_get_audio_buffer,
  149. .filter_samples = filter_samples, },
  150. { .name = NULL }
  151. },
  152. .outputs = (const AVFilterPad[]) {
  153. { .name = "default",
  154. .type = AVMEDIA_TYPE_AUDIO,
  155. .config_props = config_output_props, },
  156. { .name = NULL}
  157. },
  158. };
  159. #endif