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.

412 lines
11KB

  1. /*
  2. * Copyright (c) 2016 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/audio_fifo.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/fifo.h"
  23. #include "libavutil/internal.h"
  24. #include "libavutil/opt.h"
  25. #include "avfilter.h"
  26. #include "audio.h"
  27. #include "filters.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct LoopContext {
  32. const AVClass *class;
  33. AVAudioFifo *fifo;
  34. AVAudioFifo *left;
  35. AVFrame **frames;
  36. int nb_frames;
  37. int current_frame;
  38. int64_t start_pts;
  39. int64_t duration;
  40. int64_t current_sample;
  41. int64_t nb_samples;
  42. int64_t ignored_samples;
  43. int loop;
  44. int eof;
  45. int64_t size;
  46. int64_t start;
  47. int64_t pts;
  48. } LoopContext;
  49. #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  50. #define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  51. #define OFFSET(x) offsetof(LoopContext, x)
  52. #if CONFIG_ALOOP_FILTER
  53. static int aconfig_input(AVFilterLink *inlink)
  54. {
  55. AVFilterContext *ctx = inlink->dst;
  56. LoopContext *s = ctx->priv;
  57. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
  58. s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
  59. if (!s->fifo || !s->left)
  60. return AVERROR(ENOMEM);
  61. return 0;
  62. }
  63. static av_cold void auninit(AVFilterContext *ctx)
  64. {
  65. LoopContext *s = ctx->priv;
  66. av_audio_fifo_free(s->fifo);
  67. av_audio_fifo_free(s->left);
  68. }
  69. static int push_samples(AVFilterContext *ctx, int nb_samples)
  70. {
  71. AVFilterLink *outlink = ctx->outputs[0];
  72. LoopContext *s = ctx->priv;
  73. AVFrame *out;
  74. int ret, i = 0;
  75. while (s->loop != 0 && i < nb_samples) {
  76. out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
  77. if (!out)
  78. return AVERROR(ENOMEM);
  79. ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
  80. if (ret < 0) {
  81. av_frame_free(&out);
  82. return ret;
  83. }
  84. out->pts = s->pts;
  85. out->nb_samples = ret;
  86. s->pts += out->nb_samples;
  87. i += out->nb_samples;
  88. s->current_sample += out->nb_samples;
  89. ret = ff_filter_frame(outlink, out);
  90. if (ret < 0)
  91. return ret;
  92. if (s->current_sample >= s->nb_samples) {
  93. s->current_sample = 0;
  94. if (s->loop > 0)
  95. s->loop--;
  96. }
  97. }
  98. return ret;
  99. }
  100. static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
  101. {
  102. AVFilterContext *ctx = inlink->dst;
  103. AVFilterLink *outlink = ctx->outputs[0];
  104. LoopContext *s = ctx->priv;
  105. int ret = 0;
  106. if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
  107. if (s->nb_samples < s->size) {
  108. int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
  109. int drain = 0;
  110. ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
  111. if (ret < 0)
  112. return ret;
  113. if (!s->nb_samples) {
  114. drain = FFMAX(0, s->start - s->ignored_samples);
  115. s->pts = frame->pts;
  116. av_audio_fifo_drain(s->fifo, drain);
  117. s->pts += s->start - s->ignored_samples;
  118. }
  119. s->nb_samples += ret - drain;
  120. drain = frame->nb_samples - written;
  121. if (s->nb_samples == s->size && drain > 0) {
  122. int ret2;
  123. ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
  124. if (ret2 < 0)
  125. return ret2;
  126. av_audio_fifo_drain(s->left, drain);
  127. }
  128. frame->nb_samples = ret;
  129. s->pts += ret;
  130. ret = ff_filter_frame(outlink, frame);
  131. } else {
  132. int nb_samples = frame->nb_samples;
  133. av_frame_free(&frame);
  134. ret = push_samples(ctx, nb_samples);
  135. }
  136. } else {
  137. s->ignored_samples += frame->nb_samples;
  138. frame->pts = s->pts;
  139. s->pts += frame->nb_samples;
  140. ret = ff_filter_frame(outlink, frame);
  141. }
  142. return ret;
  143. }
  144. static int arequest_frame(AVFilterLink *outlink)
  145. {
  146. AVFilterContext *ctx = outlink->src;
  147. LoopContext *s = ctx->priv;
  148. int ret = 0;
  149. if ((!s->size) ||
  150. (s->nb_samples < s->size) ||
  151. (s->nb_samples >= s->size && s->loop == 0)) {
  152. int nb_samples = av_audio_fifo_size(s->left);
  153. if (s->loop == 0 && nb_samples > 0) {
  154. AVFrame *out;
  155. out = ff_get_audio_buffer(outlink, nb_samples);
  156. if (!out)
  157. return AVERROR(ENOMEM);
  158. av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
  159. out->pts = s->pts;
  160. s->pts += nb_samples;
  161. ret = ff_filter_frame(outlink, out);
  162. if (ret < 0)
  163. return ret;
  164. }
  165. ret = ff_request_frame(ctx->inputs[0]);
  166. } else {
  167. ret = push_samples(ctx, 1024);
  168. }
  169. if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
  170. ret = push_samples(ctx, outlink->sample_rate);
  171. }
  172. return ret;
  173. }
  174. static const AVOption aloop_options[] = {
  175. { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
  176. { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
  177. { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
  178. { NULL }
  179. };
  180. AVFILTER_DEFINE_CLASS(aloop);
  181. static const AVFilterPad ainputs[] = {
  182. {
  183. .name = "default",
  184. .type = AVMEDIA_TYPE_AUDIO,
  185. .filter_frame = afilter_frame,
  186. .config_props = aconfig_input,
  187. },
  188. { NULL }
  189. };
  190. static const AVFilterPad aoutputs[] = {
  191. {
  192. .name = "default",
  193. .type = AVMEDIA_TYPE_AUDIO,
  194. .request_frame = arequest_frame,
  195. },
  196. { NULL }
  197. };
  198. AVFilter ff_af_aloop = {
  199. .name = "aloop",
  200. .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
  201. .priv_size = sizeof(LoopContext),
  202. .priv_class = &aloop_class,
  203. .uninit = auninit,
  204. .inputs = ainputs,
  205. .outputs = aoutputs,
  206. };
  207. #endif /* CONFIG_ALOOP_FILTER */
  208. #if CONFIG_LOOP_FILTER
  209. static av_cold int init(AVFilterContext *ctx)
  210. {
  211. LoopContext *s = ctx->priv;
  212. s->frames = av_calloc(s->size, sizeof(*s->frames));
  213. if (!s->frames)
  214. return AVERROR(ENOMEM);
  215. return 0;
  216. }
  217. static av_cold void uninit(AVFilterContext *ctx)
  218. {
  219. LoopContext *s = ctx->priv;
  220. int i;
  221. for (i = 0; i < s->nb_frames; i++)
  222. av_frame_free(&s->frames[i]);
  223. av_freep(&s->frames);
  224. s->nb_frames = 0;
  225. }
  226. static int push_frame(AVFilterContext *ctx)
  227. {
  228. AVFilterLink *outlink = ctx->outputs[0];
  229. LoopContext *s = ctx->priv;
  230. int64_t pts, duration;
  231. int ret;
  232. AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
  233. if (!out)
  234. return AVERROR(ENOMEM);
  235. out->pts += s->duration - s->start_pts;
  236. if (out->pkt_duration)
  237. duration = out->pkt_duration;
  238. else
  239. duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
  240. pts = out->pts + duration;
  241. ret = ff_filter_frame(outlink, out);
  242. s->current_frame++;
  243. if (s->current_frame >= s->nb_frames) {
  244. s->duration = pts;
  245. s->current_frame = 0;
  246. if (s->loop > 0)
  247. s->loop--;
  248. }
  249. return ret;
  250. }
  251. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  252. {
  253. AVFilterContext *ctx = inlink->dst;
  254. AVFilterLink *outlink = ctx->outputs[0];
  255. LoopContext *s = ctx->priv;
  256. int64_t duration;
  257. int ret = 0;
  258. if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
  259. if (s->nb_frames < s->size) {
  260. if (!s->nb_frames)
  261. s->start_pts = frame->pts;
  262. s->frames[s->nb_frames] = av_frame_clone(frame);
  263. if (!s->frames[s->nb_frames]) {
  264. av_frame_free(&frame);
  265. return AVERROR(ENOMEM);
  266. }
  267. s->nb_frames++;
  268. if (frame->pkt_duration)
  269. duration = frame->pkt_duration;
  270. else
  271. duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
  272. s->duration = frame->pts + duration;
  273. ret = ff_filter_frame(outlink, frame);
  274. } else {
  275. av_frame_free(&frame);
  276. ret = push_frame(ctx);
  277. }
  278. } else {
  279. frame->pts += s->duration;
  280. ret = ff_filter_frame(outlink, frame);
  281. }
  282. return ret;
  283. }
  284. static int activate(AVFilterContext *ctx)
  285. {
  286. AVFilterLink *inlink = ctx->inputs[0];
  287. AVFilterLink *outlink = ctx->outputs[0];
  288. LoopContext *s = ctx->priv;
  289. AVFrame *frame = NULL;
  290. int ret, status;
  291. int64_t pts;
  292. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  293. if (!s->eof && (s->nb_frames < s->size || !s->loop)) {
  294. ret = ff_inlink_consume_frame(inlink, &frame);
  295. if (ret < 0)
  296. return ret;
  297. if (ret > 0)
  298. return filter_frame(inlink, frame);
  299. }
  300. if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  301. if (status == AVERROR_EOF)
  302. s->eof = 1;
  303. }
  304. if (s->eof && (s->loop == 0 || s->nb_frames < s->size)) {
  305. ff_outlink_set_status(outlink, AVERROR_EOF, s->duration);
  306. return 0;
  307. }
  308. if (!s->eof && (!s->size ||
  309. (s->nb_frames < s->size) ||
  310. (s->nb_frames >= s->size && s->loop == 0))) {
  311. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  312. } else if (s->loop && s->nb_frames == s->size) {
  313. return push_frame(ctx);
  314. }
  315. return FFERROR_NOT_READY;
  316. }
  317. static const AVOption loop_options[] = {
  318. { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
  319. { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
  320. { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
  321. { NULL }
  322. };
  323. AVFILTER_DEFINE_CLASS(loop);
  324. static const AVFilterPad inputs[] = {
  325. {
  326. .name = "default",
  327. .type = AVMEDIA_TYPE_VIDEO,
  328. },
  329. { NULL }
  330. };
  331. static const AVFilterPad outputs[] = {
  332. {
  333. .name = "default",
  334. .type = AVMEDIA_TYPE_VIDEO,
  335. },
  336. { NULL }
  337. };
  338. AVFilter ff_vf_loop = {
  339. .name = "loop",
  340. .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
  341. .priv_size = sizeof(LoopContext),
  342. .priv_class = &loop_class,
  343. .init = init,
  344. .uninit = uninit,
  345. .activate = activate,
  346. .inputs = inputs,
  347. .outputs = outputs,
  348. };
  349. #endif /* CONFIG_LOOP_FILTER */