trim.c 13 KB

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