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.

391 lines
14KB

  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 <stdint.h>
  21. #include "config.h"
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/log.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/samplefmt.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "internal.h"
  32. typedef struct TrimContext {
  33. const AVClass *class;
  34. /*
  35. * AVOptions
  36. */
  37. int64_t duration;
  38. int64_t start_time, end_time;
  39. int64_t start_frame, end_frame;
  40. double duration_dbl;
  41. double start_time_dbl, end_time_dbl;
  42. /*
  43. * in the link timebase for video,
  44. * in 1/samplerate for audio
  45. */
  46. int64_t start_pts, end_pts;
  47. int64_t start_sample, end_sample;
  48. /*
  49. * number of video frames that arrived on this filter so far
  50. */
  51. int64_t nb_frames;
  52. /*
  53. * number of audio samples that arrived on this filter so far
  54. */
  55. int64_t nb_samples;
  56. /*
  57. * timestamp of the first frame in the output, in the timebase units
  58. */
  59. int64_t first_pts;
  60. /*
  61. * duration in the timebase units
  62. */
  63. int64_t duration_tb;
  64. int64_t next_pts;
  65. int eof;
  66. } TrimContext;
  67. static av_cold int init(AVFilterContext *ctx)
  68. {
  69. TrimContext *s = ctx->priv;
  70. s->first_pts = AV_NOPTS_VALUE;
  71. return 0;
  72. }
  73. static int config_input(AVFilterLink *inlink)
  74. {
  75. AVFilterContext *ctx = inlink->dst;
  76. TrimContext *s = ctx->priv;
  77. AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
  78. inlink->time_base : (AVRational){ 1, inlink->sample_rate };
  79. if (s->start_time_dbl != DBL_MAX)
  80. s->start_time = s->start_time_dbl * 1e6;
  81. if (s->end_time_dbl != DBL_MAX)
  82. s->end_time = s->end_time_dbl * 1e6;
  83. if (s->duration_dbl != 0)
  84. s->duration = s->duration_dbl * 1e6;
  85. if (s->start_time != INT64_MAX) {
  86. int64_t start_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, tb);
  87. if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
  88. s->start_pts = start_pts;
  89. }
  90. if (s->end_time != INT64_MAX) {
  91. int64_t end_pts = av_rescale_q(s->end_time, AV_TIME_BASE_Q, tb);
  92. if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
  93. s->end_pts = end_pts;
  94. }
  95. if (s->duration)
  96. s->duration_tb = av_rescale_q(s->duration, AV_TIME_BASE_Q, tb);
  97. return 0;
  98. }
  99. #define OFFSET(x) offsetof(TrimContext, x)
  100. #define COMMON_OPTS \
  101. { "starti", "Timestamp of the first frame that " \
  102. "should be passed", OFFSET(start_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
  103. { "endi", "Timestamp of the first frame that " \
  104. "should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX }, INT64_MIN, INT64_MAX, FLAGS }, \
  105. { "start_pts", "Timestamp of the first frame that should be " \
  106. " passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
  107. { "end_pts", "Timestamp of the first frame that should be " \
  108. "dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
  109. { "durationi", "Maximum duration of the output", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
  110. #define COMPAT_OPTS \
  111. { "start", "Timestamp in seconds of the first frame that " \
  112. "should be passed", OFFSET(start_time_dbl),AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
  113. { "end", "Timestamp in seconds of the first frame that " \
  114. "should be dropped again", OFFSET(end_time_dbl), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
  115. { "duration", "Maximum duration of the output in seconds", OFFSET(duration_dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, DBL_MAX, FLAGS },
  116. #if CONFIG_TRIM_FILTER
  117. static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  118. {
  119. AVFilterContext *ctx = inlink->dst;
  120. TrimContext *s = ctx->priv;
  121. int drop;
  122. /* drop everything if EOF has already been returned */
  123. if (s->eof) {
  124. av_frame_free(&frame);
  125. return 0;
  126. }
  127. if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
  128. drop = 1;
  129. if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
  130. drop = 0;
  131. if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
  132. frame->pts >= s->start_pts)
  133. drop = 0;
  134. if (drop)
  135. goto drop;
  136. }
  137. if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
  138. s->first_pts = frame->pts;
  139. if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
  140. drop = 1;
  141. if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
  142. drop = 0;
  143. if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
  144. frame->pts < s->end_pts)
  145. drop = 0;
  146. if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
  147. frame->pts - s->first_pts < s->duration_tb)
  148. drop = 0;
  149. if (drop) {
  150. s->eof = 1;
  151. ff_avfilter_link_set_out_status(inlink, AVERROR_EOF, AV_NOPTS_VALUE);
  152. goto drop;
  153. }
  154. }
  155. s->nb_frames++;
  156. return ff_filter_frame(ctx->outputs[0], frame);
  157. drop:
  158. s->nb_frames++;
  159. av_frame_free(&frame);
  160. return 0;
  161. }
  162. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  163. static const AVOption trim_options[] = {
  164. COMMON_OPTS
  165. { "start_frame", "Number of the first frame that should be passed "
  166. "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
  167. { "end_frame", "Number of the first frame that should be dropped "
  168. "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
  169. COMPAT_OPTS
  170. { NULL }
  171. };
  172. #undef FLAGS
  173. AVFILTER_DEFINE_CLASS(trim);
  174. static const AVFilterPad trim_inputs[] = {
  175. {
  176. .name = "default",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. .filter_frame = trim_filter_frame,
  179. .config_props = config_input,
  180. },
  181. { NULL }
  182. };
  183. static const AVFilterPad trim_outputs[] = {
  184. {
  185. .name = "default",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. },
  188. { NULL }
  189. };
  190. AVFilter ff_vf_trim = {
  191. .name = "trim",
  192. .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
  193. .init = init,
  194. .priv_size = sizeof(TrimContext),
  195. .priv_class = &trim_class,
  196. .inputs = trim_inputs,
  197. .outputs = trim_outputs,
  198. };
  199. #endif // CONFIG_TRIM_FILTER
  200. #if CONFIG_ATRIM_FILTER
  201. static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  202. {
  203. AVFilterContext *ctx = inlink->dst;
  204. TrimContext *s = ctx->priv;
  205. int64_t start_sample, end_sample;
  206. int64_t pts;
  207. int drop;
  208. /* drop everything if EOF has already been returned */
  209. if (s->eof) {
  210. av_frame_free(&frame);
  211. return 0;
  212. }
  213. if (frame->pts != AV_NOPTS_VALUE)
  214. pts = av_rescale_q(frame->pts, inlink->time_base,
  215. (AVRational){ 1, inlink->sample_rate });
  216. else
  217. pts = s->next_pts;
  218. s->next_pts = pts + frame->nb_samples;
  219. /* check if at least a part of the frame is after the start time */
  220. if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
  221. start_sample = 0;
  222. } else {
  223. drop = 1;
  224. start_sample = frame->nb_samples;
  225. if (s->start_sample >= 0 &&
  226. s->nb_samples + frame->nb_samples > s->start_sample) {
  227. drop = 0;
  228. start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
  229. }
  230. if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
  231. pts + frame->nb_samples > s->start_pts) {
  232. drop = 0;
  233. start_sample = FFMIN(start_sample, s->start_pts - pts);
  234. }
  235. if (drop)
  236. goto drop;
  237. }
  238. if (s->first_pts == AV_NOPTS_VALUE)
  239. s->first_pts = pts + start_sample;
  240. /* check if at least a part of the frame is before the end time */
  241. if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
  242. end_sample = frame->nb_samples;
  243. } else {
  244. drop = 1;
  245. end_sample = 0;
  246. if (s->end_sample != INT64_MAX &&
  247. s->nb_samples < s->end_sample) {
  248. drop = 0;
  249. end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
  250. }
  251. if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
  252. pts < s->end_pts) {
  253. drop = 0;
  254. end_sample = FFMAX(end_sample, s->end_pts - pts);
  255. }
  256. if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
  257. drop = 0;
  258. end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
  259. }
  260. if (drop) {
  261. s->eof = 1;
  262. ff_avfilter_link_set_out_status(inlink, AVERROR_EOF, AV_NOPTS_VALUE);
  263. goto drop;
  264. }
  265. }
  266. s->nb_samples += frame->nb_samples;
  267. start_sample = FFMAX(0, start_sample);
  268. end_sample = FFMIN(frame->nb_samples, end_sample);
  269. av_assert0(start_sample < end_sample || (start_sample == end_sample && !frame->nb_samples));
  270. if (start_sample) {
  271. AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
  272. if (!out) {
  273. av_frame_free(&frame);
  274. return AVERROR(ENOMEM);
  275. }
  276. av_frame_copy_props(out, frame);
  277. av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
  278. out->nb_samples, inlink->channels,
  279. frame->format);
  280. if (out->pts != AV_NOPTS_VALUE)
  281. out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
  282. inlink->time_base);
  283. av_frame_free(&frame);
  284. frame = out;
  285. } else
  286. frame->nb_samples = end_sample;
  287. return ff_filter_frame(ctx->outputs[0], frame);
  288. drop:
  289. s->nb_samples += frame->nb_samples;
  290. av_frame_free(&frame);
  291. return 0;
  292. }
  293. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  294. static const AVOption atrim_options[] = {
  295. COMMON_OPTS
  296. { "start_sample", "Number of the first audio sample that should be "
  297. "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
  298. { "end_sample", "Number of the first audio sample that should be "
  299. "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
  300. COMPAT_OPTS
  301. { NULL }
  302. };
  303. #undef FLAGS
  304. AVFILTER_DEFINE_CLASS(atrim);
  305. static const AVFilterPad atrim_inputs[] = {
  306. {
  307. .name = "default",
  308. .type = AVMEDIA_TYPE_AUDIO,
  309. .filter_frame = atrim_filter_frame,
  310. .config_props = config_input,
  311. },
  312. { NULL }
  313. };
  314. static const AVFilterPad atrim_outputs[] = {
  315. {
  316. .name = "default",
  317. .type = AVMEDIA_TYPE_AUDIO,
  318. },
  319. { NULL }
  320. };
  321. AVFilter ff_af_atrim = {
  322. .name = "atrim",
  323. .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
  324. .init = init,
  325. .query_formats = ff_query_formats_all,
  326. .priv_size = sizeof(TrimContext),
  327. .priv_class = &atrim_class,
  328. .inputs = atrim_inputs,
  329. .outputs = atrim_outputs,
  330. };
  331. #endif // CONFIG_ATRIM_FILTER