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.

382 lines
12KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <float.h>
  19. #include <math.h>
  20. #include "config.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/samplefmt.h"
  28. #include "audio.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. typedef struct TrimContext {
  32. const AVClass *class;
  33. /*
  34. * AVOptions
  35. */
  36. double duration;
  37. double start_time, end_time;
  38. int64_t start_frame, end_frame;
  39. /*
  40. * in the link timebase for video,
  41. * in 1/samplerate for audio
  42. */
  43. int64_t start_pts, end_pts;
  44. int64_t start_sample, end_sample;
  45. /*
  46. * number of video frames that arrived on this filter so far
  47. */
  48. int64_t nb_frames;
  49. /*
  50. * number of audio samples that arrived on this filter so far
  51. */
  52. int64_t nb_samples;
  53. /*
  54. * timestamp of the first frame in the output, in the timebase units
  55. */
  56. int64_t first_pts;
  57. /*
  58. * duration in the timebase units
  59. */
  60. int64_t duration_tb;
  61. int64_t next_pts;
  62. int eof;
  63. } TrimContext;
  64. static int init(AVFilterContext *ctx)
  65. {
  66. TrimContext *s = ctx->priv;
  67. s->first_pts = AV_NOPTS_VALUE;
  68. return 0;
  69. }
  70. static int config_input(AVFilterLink *inlink)
  71. {
  72. AVFilterContext *ctx = inlink->dst;
  73. TrimContext *s = ctx->priv;
  74. AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
  75. inlink->time_base : (AVRational){ 1, inlink->sample_rate };
  76. if (s->start_time != DBL_MAX) {
  77. int64_t start_pts = lrint(s->start_time / av_q2d(tb));
  78. if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
  79. s->start_pts = start_pts;
  80. }
  81. if (s->end_time != DBL_MAX) {
  82. int64_t end_pts = lrint(s->end_time / av_q2d(tb));
  83. if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
  84. s->end_pts = end_pts;
  85. }
  86. if (s->duration)
  87. s->duration_tb = lrint(s->duration / av_q2d(tb));
  88. return 0;
  89. }
  90. static int config_output(AVFilterLink *outlink)
  91. {
  92. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  93. return 0;
  94. }
  95. #define OFFSET(x) offsetof(TrimContext, x)
  96. #define COMMON_OPTS \
  97. { "start", "Timestamp in seconds of the first frame that " \
  98. "should be passed", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
  99. { "end", "Timestamp in seconds of the first frame that " \
  100. "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
  101. { "start_pts", "Timestamp of the first frame that should be " \
  102. " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
  103. { "end_pts", "Timestamp of the first frame that should be " \
  104. "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
  105. { "duration", "Maximum duration of the output in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, DBL_MAX, FLAGS },
  106. #if CONFIG_TRIM_FILTER
  107. static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  108. {
  109. AVFilterContext *ctx = inlink->dst;
  110. TrimContext *s = ctx->priv;
  111. int drop;
  112. /* drop everything if EOF has already been returned */
  113. if (s->eof) {
  114. av_frame_free(&frame);
  115. return 0;
  116. }
  117. if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
  118. drop = 1;
  119. if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
  120. drop = 0;
  121. if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
  122. frame->pts >= s->start_pts)
  123. drop = 0;
  124. if (drop)
  125. goto drop;
  126. }
  127. if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
  128. s->first_pts = frame->pts;
  129. if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
  130. drop = 1;
  131. if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
  132. drop = 0;
  133. if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
  134. frame->pts < s->end_pts)
  135. drop = 0;
  136. if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
  137. frame->pts - s->first_pts < s->duration_tb)
  138. drop = 0;
  139. if (drop) {
  140. s->eof = inlink->closed = 1;
  141. goto drop;
  142. }
  143. }
  144. s->nb_frames++;
  145. return ff_filter_frame(ctx->outputs[0], frame);
  146. drop:
  147. s->nb_frames++;
  148. av_frame_free(&frame);
  149. return 0;
  150. }
  151. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  152. static const AVOption trim_options[] = {
  153. COMMON_OPTS
  154. { "start_frame", "Number of the first frame that should be passed "
  155. "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
  156. { "end_frame", "Number of the first frame that should be dropped "
  157. "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
  158. { NULL },
  159. };
  160. #undef FLAGS
  161. AVFILTER_DEFINE_CLASS(trim);
  162. static const AVFilterPad trim_inputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .filter_frame = trim_filter_frame,
  167. .config_props = config_input,
  168. },
  169. { NULL }
  170. };
  171. static const AVFilterPad trim_outputs[] = {
  172. {
  173. .name = "default",
  174. .type = AVMEDIA_TYPE_VIDEO,
  175. .config_props = config_output,
  176. },
  177. { NULL }
  178. };
  179. AVFilter avfilter_vf_trim = {
  180. .name = "trim",
  181. .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
  182. .init = init,
  183. .priv_size = sizeof(TrimContext),
  184. .priv_class = &trim_class,
  185. .inputs = trim_inputs,
  186. .outputs = trim_outputs,
  187. };
  188. #endif // CONFIG_TRIM_FILTER
  189. #if CONFIG_ATRIM_FILTER
  190. static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  191. {
  192. AVFilterContext *ctx = inlink->dst;
  193. TrimContext *s = ctx->priv;
  194. int64_t start_sample, end_sample = frame->nb_samples;
  195. int64_t pts;
  196. int drop;
  197. /* drop everything if EOF has already been returned */
  198. if (s->eof) {
  199. av_frame_free(&frame);
  200. return 0;
  201. }
  202. if (frame->pts != AV_NOPTS_VALUE)
  203. pts = av_rescale_q(frame->pts, inlink->time_base,
  204. (AVRational){ 1, inlink->sample_rate });
  205. else
  206. pts = s->next_pts;
  207. s->next_pts = pts + frame->nb_samples;
  208. /* check if at least a part of the frame is after the start time */
  209. if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
  210. start_sample = 0;
  211. } else {
  212. drop = 1;
  213. start_sample = frame->nb_samples;
  214. if (s->start_sample >= 0 &&
  215. s->nb_samples + frame->nb_samples > s->start_sample) {
  216. drop = 0;
  217. start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
  218. }
  219. if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
  220. pts + frame->nb_samples > s->start_pts) {
  221. drop = 0;
  222. start_sample = FFMIN(start_sample, s->start_pts - pts);
  223. }
  224. if (drop)
  225. goto drop;
  226. }
  227. if (s->first_pts == AV_NOPTS_VALUE)
  228. s->first_pts = pts + start_sample;
  229. /* check if at least a part of the frame is before the end time */
  230. if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
  231. end_sample = frame->nb_samples;
  232. } else {
  233. drop = 1;
  234. end_sample = 0;
  235. if (s->end_sample != INT64_MAX &&
  236. s->nb_samples < s->end_sample) {
  237. drop = 0;
  238. end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
  239. }
  240. if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
  241. pts < s->end_pts) {
  242. drop = 0;
  243. end_sample = FFMAX(end_sample, s->end_pts - pts);
  244. }
  245. if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
  246. drop = 0;
  247. end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
  248. }
  249. if (drop) {
  250. s->eof = inlink->closed = 1;
  251. goto drop;
  252. }
  253. }
  254. s->nb_samples += frame->nb_samples;
  255. start_sample = FFMAX(0, start_sample);
  256. end_sample = FFMIN(frame->nb_samples, end_sample);
  257. av_assert0(start_sample < end_sample);
  258. if (start_sample) {
  259. AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
  260. if (!out) {
  261. av_frame_free(&frame);
  262. return AVERROR(ENOMEM);
  263. }
  264. av_frame_copy_props(out, frame);
  265. av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
  266. out->nb_samples, av_get_channel_layout_nb_channels(frame->channel_layout),
  267. frame->format);
  268. if (out->pts != AV_NOPTS_VALUE)
  269. out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
  270. inlink->time_base);
  271. av_frame_free(&frame);
  272. frame = out;
  273. } else
  274. frame->nb_samples = end_sample;
  275. return ff_filter_frame(ctx->outputs[0], frame);
  276. drop:
  277. s->nb_samples += frame->nb_samples;
  278. av_frame_free(&frame);
  279. return 0;
  280. }
  281. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  282. static const AVOption atrim_options[] = {
  283. COMMON_OPTS
  284. { "start_sample", "Number of the first audio sample that should be "
  285. "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
  286. { "end_sample", "Number of the first audio sample that should be "
  287. "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
  288. { NULL },
  289. };
  290. #undef FLAGS
  291. AVFILTER_DEFINE_CLASS(atrim);
  292. static const AVFilterPad atrim_inputs[] = {
  293. {
  294. .name = "default",
  295. .type = AVMEDIA_TYPE_AUDIO,
  296. .filter_frame = atrim_filter_frame,
  297. .config_props = config_input,
  298. },
  299. { NULL }
  300. };
  301. static const AVFilterPad atrim_outputs[] = {
  302. {
  303. .name = "default",
  304. .type = AVMEDIA_TYPE_AUDIO,
  305. .config_props = config_output,
  306. },
  307. { NULL }
  308. };
  309. AVFilter avfilter_af_atrim = {
  310. .name = "atrim",
  311. .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
  312. .init = init,
  313. .priv_size = sizeof(TrimContext),
  314. .priv_class = &atrim_class,
  315. .inputs = atrim_inputs,
  316. .outputs = atrim_outputs,
  317. };
  318. #endif // CONFIG_ATRIM_FILTER