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.

388 lines
13KB

  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 = inlink->closed = 1;
  151. goto drop;
  152. }
  153. }
  154. s->nb_frames++;
  155. return ff_filter_frame(ctx->outputs[0], frame);
  156. drop:
  157. s->nb_frames++;
  158. av_frame_free(&frame);
  159. return 0;
  160. }
  161. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  162. static const AVOption trim_options[] = {
  163. COMMON_OPTS
  164. { "start_frame", "Number of the first frame that should be passed "
  165. "to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
  166. { "end_frame", "Number of the first frame that should be dropped "
  167. "again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
  168. COMPAT_OPTS
  169. { NULL }
  170. };
  171. #undef FLAGS
  172. AVFILTER_DEFINE_CLASS(trim);
  173. static const AVFilterPad trim_inputs[] = {
  174. {
  175. .name = "default",
  176. .type = AVMEDIA_TYPE_VIDEO,
  177. .filter_frame = trim_filter_frame,
  178. .config_props = config_input,
  179. },
  180. { NULL }
  181. };
  182. static const AVFilterPad trim_outputs[] = {
  183. {
  184. .name = "default",
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. },
  187. { NULL }
  188. };
  189. AVFilter ff_vf_trim = {
  190. .name = "trim",
  191. .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
  192. .init = init,
  193. .priv_size = sizeof(TrimContext),
  194. .priv_class = &trim_class,
  195. .inputs = trim_inputs,
  196. .outputs = trim_outputs,
  197. };
  198. #endif // CONFIG_TRIM_FILTER
  199. #if CONFIG_ATRIM_FILTER
  200. static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  201. {
  202. AVFilterContext *ctx = inlink->dst;
  203. TrimContext *s = ctx->priv;
  204. int64_t start_sample, end_sample;
  205. int64_t pts;
  206. int drop;
  207. /* drop everything if EOF has already been returned */
  208. if (s->eof) {
  209. av_frame_free(&frame);
  210. return 0;
  211. }
  212. if (frame->pts != AV_NOPTS_VALUE)
  213. pts = av_rescale_q(frame->pts, inlink->time_base,
  214. (AVRational){ 1, inlink->sample_rate });
  215. else
  216. pts = s->next_pts;
  217. s->next_pts = pts + frame->nb_samples;
  218. /* check if at least a part of the frame is after the start time */
  219. if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
  220. start_sample = 0;
  221. } else {
  222. drop = 1;
  223. start_sample = frame->nb_samples;
  224. if (s->start_sample >= 0 &&
  225. s->nb_samples + frame->nb_samples > s->start_sample) {
  226. drop = 0;
  227. start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
  228. }
  229. if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
  230. pts + frame->nb_samples > s->start_pts) {
  231. drop = 0;
  232. start_sample = FFMIN(start_sample, s->start_pts - pts);
  233. }
  234. if (drop)
  235. goto drop;
  236. }
  237. if (s->first_pts == AV_NOPTS_VALUE)
  238. s->first_pts = pts + start_sample;
  239. /* check if at least a part of the frame is before the end time */
  240. if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
  241. end_sample = frame->nb_samples;
  242. } else {
  243. drop = 1;
  244. end_sample = 0;
  245. if (s->end_sample != INT64_MAX &&
  246. s->nb_samples < s->end_sample) {
  247. drop = 0;
  248. end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
  249. }
  250. if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
  251. pts < s->end_pts) {
  252. drop = 0;
  253. end_sample = FFMAX(end_sample, s->end_pts - pts);
  254. }
  255. if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
  256. drop = 0;
  257. end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
  258. }
  259. if (drop) {
  260. s->eof = inlink->closed = 1;
  261. goto drop;
  262. }
  263. }
  264. s->nb_samples += frame->nb_samples;
  265. start_sample = FFMAX(0, start_sample);
  266. end_sample = FFMIN(frame->nb_samples, end_sample);
  267. av_assert0(start_sample < end_sample || (start_sample == end_sample && !frame->nb_samples));
  268. if (start_sample) {
  269. AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
  270. if (!out) {
  271. av_frame_free(&frame);
  272. return AVERROR(ENOMEM);
  273. }
  274. av_frame_copy_props(out, frame);
  275. av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
  276. out->nb_samples, inlink->channels,
  277. frame->format);
  278. if (out->pts != AV_NOPTS_VALUE)
  279. out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
  280. inlink->time_base);
  281. av_frame_free(&frame);
  282. frame = out;
  283. } else
  284. frame->nb_samples = end_sample;
  285. return ff_filter_frame(ctx->outputs[0], frame);
  286. drop:
  287. s->nb_samples += frame->nb_samples;
  288. av_frame_free(&frame);
  289. return 0;
  290. }
  291. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  292. static const AVOption atrim_options[] = {
  293. COMMON_OPTS
  294. { "start_sample", "Number of the first audio sample that should be "
  295. "passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
  296. { "end_sample", "Number of the first audio sample that should be "
  297. "dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
  298. COMPAT_OPTS
  299. { NULL }
  300. };
  301. #undef FLAGS
  302. AVFILTER_DEFINE_CLASS(atrim);
  303. static const AVFilterPad atrim_inputs[] = {
  304. {
  305. .name = "default",
  306. .type = AVMEDIA_TYPE_AUDIO,
  307. .filter_frame = atrim_filter_frame,
  308. .config_props = config_input,
  309. },
  310. { NULL }
  311. };
  312. static const AVFilterPad atrim_outputs[] = {
  313. {
  314. .name = "default",
  315. .type = AVMEDIA_TYPE_AUDIO,
  316. },
  317. { NULL }
  318. };
  319. AVFilter ff_af_atrim = {
  320. .name = "atrim",
  321. .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
  322. .init = init,
  323. .priv_size = sizeof(TrimContext),
  324. .priv_class = &atrim_class,
  325. .inputs = atrim_inputs,
  326. .outputs = atrim_outputs,
  327. };
  328. #endif // CONFIG_ATRIM_FILTER