f_loop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. typedef struct LoopContext {
  31. const AVClass *class;
  32. AVAudioFifo *fifo;
  33. AVAudioFifo *left;
  34. AVFrame **frames;
  35. int nb_frames;
  36. int current_frame;
  37. int64_t start_pts;
  38. int64_t duration;
  39. int64_t current_sample;
  40. int64_t nb_samples;
  41. int64_t ignored_samples;
  42. int loop;
  43. int64_t size;
  44. int64_t start;
  45. int64_t pts;
  46. } LoopContext;
  47. #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  48. #define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  49. #define OFFSET(x) offsetof(LoopContext, x)
  50. #if CONFIG_ALOOP_FILTER
  51. static int aconfig_input(AVFilterLink *inlink)
  52. {
  53. AVFilterContext *ctx = inlink->dst;
  54. LoopContext *s = ctx->priv;
  55. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
  56. s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
  57. if (!s->fifo || !s->left)
  58. return AVERROR(ENOMEM);
  59. return 0;
  60. }
  61. static av_cold void auninit(AVFilterContext *ctx)
  62. {
  63. LoopContext *s = ctx->priv;
  64. av_audio_fifo_free(s->fifo);
  65. av_audio_fifo_free(s->left);
  66. }
  67. static int push_samples(AVFilterContext *ctx, int nb_samples)
  68. {
  69. AVFilterLink *outlink = ctx->outputs[0];
  70. LoopContext *s = ctx->priv;
  71. AVFrame *out;
  72. int ret, i = 0;
  73. while (s->loop != 0 && i < nb_samples) {
  74. out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
  75. if (!out)
  76. return AVERROR(ENOMEM);
  77. ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
  78. if (ret < 0) {
  79. av_frame_free(&out);
  80. return ret;
  81. }
  82. out->pts = s->pts;
  83. out->nb_samples = ret;
  84. s->pts += out->nb_samples;
  85. i += out->nb_samples;
  86. s->current_sample += out->nb_samples;
  87. ret = ff_filter_frame(outlink, out);
  88. if (ret < 0)
  89. return ret;
  90. if (s->current_sample >= s->nb_samples) {
  91. s->current_sample = 0;
  92. if (s->loop > 0)
  93. s->loop--;
  94. }
  95. }
  96. return ret;
  97. }
  98. static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. AVFilterLink *outlink = ctx->outputs[0];
  102. LoopContext *s = ctx->priv;
  103. int ret = 0;
  104. if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
  105. if (s->nb_samples < s->size) {
  106. int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
  107. int drain = 0;
  108. ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
  109. if (ret < 0)
  110. return ret;
  111. if (!s->nb_samples) {
  112. drain = FFMAX(0, s->start - s->ignored_samples);
  113. s->pts = frame->pts;
  114. av_audio_fifo_drain(s->fifo, drain);
  115. s->pts += s->start - s->ignored_samples;
  116. }
  117. s->nb_samples += ret - drain;
  118. drain = frame->nb_samples - written;
  119. if (s->nb_samples == s->size && drain > 0) {
  120. int ret2;
  121. ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
  122. if (ret2 < 0)
  123. return ret2;
  124. av_audio_fifo_drain(s->left, drain);
  125. }
  126. frame->nb_samples = ret;
  127. s->pts += ret;
  128. ret = ff_filter_frame(outlink, frame);
  129. } else {
  130. int nb_samples = frame->nb_samples;
  131. av_frame_free(&frame);
  132. ret = push_samples(ctx, nb_samples);
  133. }
  134. } else {
  135. s->ignored_samples += frame->nb_samples;
  136. frame->pts = s->pts;
  137. s->pts += frame->nb_samples;
  138. ret = ff_filter_frame(outlink, frame);
  139. }
  140. return ret;
  141. }
  142. static int arequest_frame(AVFilterLink *outlink)
  143. {
  144. AVFilterContext *ctx = outlink->src;
  145. LoopContext *s = ctx->priv;
  146. int ret = 0;
  147. if ((!s->size) ||
  148. (s->nb_samples < s->size) ||
  149. (s->nb_samples >= s->size && s->loop == 0)) {
  150. int nb_samples = av_audio_fifo_size(s->left);
  151. if (s->loop == 0 && nb_samples > 0) {
  152. AVFrame *out;
  153. out = ff_get_audio_buffer(outlink, nb_samples);
  154. if (!out)
  155. return AVERROR(ENOMEM);
  156. av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
  157. out->pts = s->pts;
  158. s->pts += nb_samples;
  159. ret = ff_filter_frame(outlink, out);
  160. if (ret < 0)
  161. return ret;
  162. }
  163. ret = ff_request_frame(ctx->inputs[0]);
  164. } else {
  165. ret = push_samples(ctx, 1024);
  166. }
  167. if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
  168. ret = push_samples(ctx, outlink->sample_rate);
  169. }
  170. return ret;
  171. }
  172. static const AVOption aloop_options[] = {
  173. { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
  174. { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
  175. { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
  176. { NULL }
  177. };
  178. AVFILTER_DEFINE_CLASS(aloop);
  179. static const AVFilterPad ainputs[] = {
  180. {
  181. .name = "default",
  182. .type = AVMEDIA_TYPE_AUDIO,
  183. .filter_frame = afilter_frame,
  184. .config_props = aconfig_input,
  185. },
  186. { NULL }
  187. };
  188. static const AVFilterPad aoutputs[] = {
  189. {
  190. .name = "default",
  191. .type = AVMEDIA_TYPE_AUDIO,
  192. .request_frame = arequest_frame,
  193. },
  194. { NULL }
  195. };
  196. AVFilter ff_af_aloop = {
  197. .name = "aloop",
  198. .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
  199. .priv_size = sizeof(LoopContext),
  200. .priv_class = &aloop_class,
  201. .uninit = auninit,
  202. .query_formats = ff_query_formats_all,
  203. .inputs = ainputs,
  204. .outputs = aoutputs,
  205. };
  206. #endif /* CONFIG_ALOOP_FILTER */
  207. #if CONFIG_LOOP_FILTER
  208. static av_cold int init(AVFilterContext *ctx)
  209. {
  210. LoopContext *s = ctx->priv;
  211. s->frames = av_calloc(s->size, sizeof(*s->frames));
  212. if (!s->frames)
  213. return AVERROR(ENOMEM);
  214. return 0;
  215. }
  216. static av_cold void uninit(AVFilterContext *ctx)
  217. {
  218. LoopContext *s = ctx->priv;
  219. int i;
  220. for (i = 0; i < s->nb_frames; i++)
  221. av_frame_free(&s->frames[i]);
  222. av_freep(&s->frames);
  223. s->nb_frames = 0;
  224. }
  225. static int push_frame(AVFilterContext *ctx)
  226. {
  227. AVFilterLink *outlink = ctx->outputs[0];
  228. LoopContext *s = ctx->priv;
  229. int64_t pts;
  230. int ret;
  231. AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
  232. if (!out)
  233. return AVERROR(ENOMEM);
  234. out->pts += s->duration - s->start_pts;
  235. pts = out->pts + av_frame_get_pkt_duration(out);
  236. ret = ff_filter_frame(outlink, out);
  237. s->current_frame++;
  238. if (s->current_frame >= s->nb_frames) {
  239. s->duration = pts;
  240. s->current_frame = 0;
  241. if (s->loop > 0)
  242. s->loop--;
  243. }
  244. return ret;
  245. }
  246. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  247. {
  248. AVFilterContext *ctx = inlink->dst;
  249. AVFilterLink *outlink = ctx->outputs[0];
  250. LoopContext *s = ctx->priv;
  251. int ret = 0;
  252. if (inlink->frame_count >= s->start && s->size > 0 && s->loop != 0) {
  253. if (s->nb_frames < s->size) {
  254. if (!s->nb_frames)
  255. s->start_pts = frame->pts;
  256. s->frames[s->nb_frames] = av_frame_clone(frame);
  257. if (!s->frames[s->nb_frames]) {
  258. av_frame_free(&frame);
  259. return AVERROR(ENOMEM);
  260. }
  261. s->nb_frames++;
  262. s->duration = frame->pts + av_frame_get_pkt_duration(frame);
  263. ret = ff_filter_frame(outlink, frame);
  264. } else {
  265. av_frame_free(&frame);
  266. ret = push_frame(ctx);
  267. }
  268. } else {
  269. frame->pts += s->duration;
  270. ret = ff_filter_frame(outlink, frame);
  271. }
  272. return ret;
  273. }
  274. static int request_frame(AVFilterLink *outlink)
  275. {
  276. AVFilterContext *ctx = outlink->src;
  277. LoopContext *s = ctx->priv;
  278. int ret = 0;
  279. if ((!s->size) ||
  280. (s->nb_frames < s->size) ||
  281. (s->nb_frames >= s->size && s->loop == 0)) {
  282. ret = ff_request_frame(ctx->inputs[0]);
  283. } else {
  284. ret = push_frame(ctx);
  285. }
  286. if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
  287. ret = push_frame(ctx);
  288. }
  289. return ret;
  290. }
  291. static const AVOption loop_options[] = {
  292. { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
  293. { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
  294. { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
  295. { NULL }
  296. };
  297. AVFILTER_DEFINE_CLASS(loop);
  298. static const AVFilterPad inputs[] = {
  299. {
  300. .name = "default",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. .filter_frame = filter_frame,
  303. },
  304. { NULL }
  305. };
  306. static const AVFilterPad outputs[] = {
  307. {
  308. .name = "default",
  309. .type = AVMEDIA_TYPE_VIDEO,
  310. .request_frame = request_frame,
  311. },
  312. { NULL }
  313. };
  314. AVFilter ff_vf_loop = {
  315. .name = "loop",
  316. .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
  317. .priv_size = sizeof(LoopContext),
  318. .priv_class = &loop_class,
  319. .init = init,
  320. .uninit = uninit,
  321. .inputs = inputs,
  322. .outputs = outputs,
  323. };
  324. #endif /* CONFIG_LOOP_FILTER */