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.

408 lines
13KB

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