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.

382 lines
10KB

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