f_loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. static void check_size(AVFilterContext *ctx)
  53. {
  54. LoopContext *s = ctx->priv;
  55. if (!s->size)
  56. av_log(ctx, AV_LOG_WARNING, "Number of %s to loop is not set!\n",
  57. ctx->input_pads[0].type == AVMEDIA_TYPE_VIDEO ? "frames" : "samples");
  58. }
  59. #if CONFIG_ALOOP_FILTER
  60. static int aconfig_input(AVFilterLink *inlink)
  61. {
  62. AVFilterContext *ctx = inlink->dst;
  63. LoopContext *s = ctx->priv;
  64. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
  65. s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
  66. if (!s->fifo || !s->left)
  67. return AVERROR(ENOMEM);
  68. check_size(ctx);
  69. return 0;
  70. }
  71. static av_cold void auninit(AVFilterContext *ctx)
  72. {
  73. LoopContext *s = ctx->priv;
  74. av_audio_fifo_free(s->fifo);
  75. av_audio_fifo_free(s->left);
  76. }
  77. static int push_samples(AVFilterContext *ctx, int nb_samples)
  78. {
  79. AVFilterLink *outlink = ctx->outputs[0];
  80. LoopContext *s = ctx->priv;
  81. AVFrame *out;
  82. int ret, i = 0;
  83. while (s->loop != 0 && i < nb_samples) {
  84. out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
  85. if (!out)
  86. return AVERROR(ENOMEM);
  87. ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
  88. if (ret < 0) {
  89. av_frame_free(&out);
  90. return ret;
  91. }
  92. out->pts = s->pts;
  93. out->nb_samples = ret;
  94. s->pts += out->nb_samples;
  95. i += out->nb_samples;
  96. s->current_sample += out->nb_samples;
  97. ret = ff_filter_frame(outlink, out);
  98. if (ret < 0)
  99. return ret;
  100. if (s->current_sample >= s->nb_samples) {
  101. s->current_sample = 0;
  102. if (s->loop > 0)
  103. s->loop--;
  104. }
  105. }
  106. return ret;
  107. }
  108. static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
  109. {
  110. AVFilterContext *ctx = inlink->dst;
  111. AVFilterLink *outlink = ctx->outputs[0];
  112. LoopContext *s = ctx->priv;
  113. int ret = 0;
  114. if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
  115. if (s->nb_samples < s->size) {
  116. int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
  117. int drain = 0;
  118. ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
  119. if (ret < 0)
  120. return ret;
  121. if (!s->nb_samples) {
  122. drain = FFMAX(0, s->start - s->ignored_samples);
  123. s->pts = frame->pts;
  124. av_audio_fifo_drain(s->fifo, drain);
  125. s->pts += s->start - s->ignored_samples;
  126. }
  127. s->nb_samples += ret - drain;
  128. drain = frame->nb_samples - written;
  129. if (s->nb_samples == s->size && drain > 0) {
  130. int ret2;
  131. ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
  132. if (ret2 < 0)
  133. return ret2;
  134. av_audio_fifo_drain(s->left, drain);
  135. }
  136. frame->nb_samples = ret;
  137. s->pts += ret;
  138. ret = ff_filter_frame(outlink, frame);
  139. } else {
  140. int nb_samples = frame->nb_samples;
  141. av_frame_free(&frame);
  142. ret = push_samples(ctx, nb_samples);
  143. }
  144. } else {
  145. s->ignored_samples += frame->nb_samples;
  146. frame->pts = s->pts;
  147. s->pts += frame->nb_samples;
  148. ret = ff_filter_frame(outlink, frame);
  149. }
  150. return ret;
  151. }
  152. static int arequest_frame(AVFilterLink *outlink)
  153. {
  154. AVFilterContext *ctx = outlink->src;
  155. LoopContext *s = ctx->priv;
  156. int ret = 0;
  157. if ((!s->size) ||
  158. (s->nb_samples < s->size) ||
  159. (s->nb_samples >= s->size && s->loop == 0)) {
  160. int nb_samples = av_audio_fifo_size(s->left);
  161. if (s->loop == 0 && nb_samples > 0) {
  162. AVFrame *out;
  163. out = ff_get_audio_buffer(outlink, nb_samples);
  164. if (!out)
  165. return AVERROR(ENOMEM);
  166. av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
  167. out->pts = s->pts;
  168. s->pts += nb_samples;
  169. ret = ff_filter_frame(outlink, out);
  170. if (ret < 0)
  171. return ret;
  172. }
  173. ret = ff_request_frame(ctx->inputs[0]);
  174. } else {
  175. ret = push_samples(ctx, 1024);
  176. }
  177. if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
  178. ret = push_samples(ctx, outlink->sample_rate);
  179. }
  180. return ret;
  181. }
  182. static const AVOption aloop_options[] = {
  183. { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
  184. { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
  185. { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
  186. { NULL }
  187. };
  188. AVFILTER_DEFINE_CLASS(aloop);
  189. static const AVFilterPad ainputs[] = {
  190. {
  191. .name = "default",
  192. .type = AVMEDIA_TYPE_AUDIO,
  193. .filter_frame = afilter_frame,
  194. .config_props = aconfig_input,
  195. },
  196. { NULL }
  197. };
  198. static const AVFilterPad aoutputs[] = {
  199. {
  200. .name = "default",
  201. .type = AVMEDIA_TYPE_AUDIO,
  202. .request_frame = arequest_frame,
  203. },
  204. { NULL }
  205. };
  206. AVFilter ff_af_aloop = {
  207. .name = "aloop",
  208. .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
  209. .priv_size = sizeof(LoopContext),
  210. .priv_class = &aloop_class,
  211. .uninit = auninit,
  212. .inputs = ainputs,
  213. .outputs = aoutputs,
  214. };
  215. #endif /* CONFIG_ALOOP_FILTER */
  216. #if CONFIG_LOOP_FILTER
  217. static av_cold int init(AVFilterContext *ctx)
  218. {
  219. LoopContext *s = ctx->priv;
  220. s->frames = av_calloc(s->size, sizeof(*s->frames));
  221. if (!s->frames)
  222. return AVERROR(ENOMEM);
  223. check_size(ctx);
  224. return 0;
  225. }
  226. static av_cold void uninit(AVFilterContext *ctx)
  227. {
  228. LoopContext *s = ctx->priv;
  229. int i;
  230. for (i = 0; i < s->nb_frames; i++)
  231. av_frame_free(&s->frames[i]);
  232. av_freep(&s->frames);
  233. s->nb_frames = 0;
  234. }
  235. static int push_frame(AVFilterContext *ctx)
  236. {
  237. AVFilterLink *outlink = ctx->outputs[0];
  238. LoopContext *s = ctx->priv;
  239. int64_t pts, duration;
  240. int ret;
  241. AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
  242. if (!out)
  243. return AVERROR(ENOMEM);
  244. out->pts += s->duration - s->start_pts;
  245. if (out->pkt_duration)
  246. duration = out->pkt_duration;
  247. else
  248. duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
  249. pts = out->pts + duration;
  250. ret = ff_filter_frame(outlink, out);
  251. s->current_frame++;
  252. if (s->current_frame >= s->nb_frames) {
  253. s->duration = pts;
  254. s->current_frame = 0;
  255. if (s->loop > 0)
  256. s->loop--;
  257. }
  258. return ret;
  259. }
  260. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  261. {
  262. AVFilterContext *ctx = inlink->dst;
  263. AVFilterLink *outlink = ctx->outputs[0];
  264. LoopContext *s = ctx->priv;
  265. int64_t duration;
  266. int ret = 0;
  267. if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
  268. if (s->nb_frames < s->size) {
  269. if (!s->nb_frames)
  270. s->start_pts = frame->pts;
  271. s->frames[s->nb_frames] = av_frame_clone(frame);
  272. if (!s->frames[s->nb_frames]) {
  273. av_frame_free(&frame);
  274. return AVERROR(ENOMEM);
  275. }
  276. s->nb_frames++;
  277. if (frame->pkt_duration)
  278. duration = frame->pkt_duration;
  279. else
  280. duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
  281. s->duration = frame->pts + duration;
  282. ret = ff_filter_frame(outlink, frame);
  283. } else {
  284. av_frame_free(&frame);
  285. ret = push_frame(ctx);
  286. }
  287. } else {
  288. frame->pts += s->duration;
  289. ret = ff_filter_frame(outlink, frame);
  290. }
  291. return ret;
  292. }
  293. static int activate(AVFilterContext *ctx)
  294. {
  295. AVFilterLink *inlink = ctx->inputs[0];
  296. AVFilterLink *outlink = ctx->outputs[0];
  297. LoopContext *s = ctx->priv;
  298. AVFrame *frame = NULL;
  299. int ret, status;
  300. int64_t pts;
  301. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  302. if (!s->eof && (s->nb_frames < s->size || !s->loop || !s->size)) {
  303. ret = ff_inlink_consume_frame(inlink, &frame);
  304. if (ret < 0)
  305. return ret;
  306. if (ret > 0)
  307. return filter_frame(inlink, frame);
  308. }
  309. if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  310. if (status == AVERROR_EOF) {
  311. s->size = s->nb_frames;
  312. s->eof = 1;
  313. }
  314. }
  315. if (s->eof && (!s->loop || !s->size)) {
  316. ff_outlink_set_status(outlink, AVERROR_EOF, s->duration);
  317. return 0;
  318. }
  319. if (!s->eof && (!s->size ||
  320. (s->nb_frames < s->size) ||
  321. (s->nb_frames >= s->size && s->loop == 0))) {
  322. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  323. } else if (s->loop && s->nb_frames == s->size) {
  324. return push_frame(ctx);
  325. }
  326. return FFERROR_NOT_READY;
  327. }
  328. static const AVOption loop_options[] = {
  329. { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
  330. { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
  331. { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
  332. { NULL }
  333. };
  334. AVFILTER_DEFINE_CLASS(loop);
  335. static const AVFilterPad inputs[] = {
  336. {
  337. .name = "default",
  338. .type = AVMEDIA_TYPE_VIDEO,
  339. },
  340. { NULL }
  341. };
  342. static const AVFilterPad outputs[] = {
  343. {
  344. .name = "default",
  345. .type = AVMEDIA_TYPE_VIDEO,
  346. },
  347. { NULL }
  348. };
  349. AVFilter ff_vf_loop = {
  350. .name = "loop",
  351. .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
  352. .priv_size = sizeof(LoopContext),
  353. .priv_class = &loop_class,
  354. .init = init,
  355. .uninit = uninit,
  356. .activate = activate,
  357. .inputs = inputs,
  358. .outputs = outputs,
  359. };
  360. #endif /* CONFIG_LOOP_FILTER */