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.

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