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.

383 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. 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. .inputs = ainputs,
  203. .outputs = aoutputs,
  204. };
  205. #endif /* CONFIG_ALOOP_FILTER */
  206. #if CONFIG_LOOP_FILTER
  207. static av_cold int init(AVFilterContext *ctx)
  208. {
  209. LoopContext *s = ctx->priv;
  210. s->frames = av_calloc(s->size, sizeof(*s->frames));
  211. if (!s->frames)
  212. return AVERROR(ENOMEM);
  213. return 0;
  214. }
  215. static av_cold void uninit(AVFilterContext *ctx)
  216. {
  217. LoopContext *s = ctx->priv;
  218. int i;
  219. for (i = 0; i < s->nb_frames; i++)
  220. av_frame_free(&s->frames[i]);
  221. av_freep(&s->frames);
  222. s->nb_frames = 0;
  223. }
  224. static int push_frame(AVFilterContext *ctx)
  225. {
  226. AVFilterLink *outlink = ctx->outputs[0];
  227. LoopContext *s = ctx->priv;
  228. int64_t pts;
  229. int ret;
  230. AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
  231. if (!out)
  232. return AVERROR(ENOMEM);
  233. out->pts += s->duration - s->start_pts;
  234. pts = out->pts + out->pkt_duration;
  235. ret = ff_filter_frame(outlink, out);
  236. s->current_frame++;
  237. if (s->current_frame >= s->nb_frames) {
  238. s->duration = pts;
  239. s->current_frame = 0;
  240. if (s->loop > 0)
  241. s->loop--;
  242. }
  243. return ret;
  244. }
  245. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  246. {
  247. AVFilterContext *ctx = inlink->dst;
  248. AVFilterLink *outlink = ctx->outputs[0];
  249. LoopContext *s = ctx->priv;
  250. int ret = 0;
  251. if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
  252. if (s->nb_frames < s->size) {
  253. if (!s->nb_frames)
  254. s->start_pts = frame->pts;
  255. s->frames[s->nb_frames] = av_frame_clone(frame);
  256. if (!s->frames[s->nb_frames]) {
  257. av_frame_free(&frame);
  258. return AVERROR(ENOMEM);
  259. }
  260. s->nb_frames++;
  261. s->duration = frame->pts + frame->pkt_duration;
  262. ret = ff_filter_frame(outlink, frame);
  263. } else {
  264. av_frame_free(&frame);
  265. ret = push_frame(ctx);
  266. }
  267. } else {
  268. frame->pts += s->duration;
  269. ret = ff_filter_frame(outlink, frame);
  270. }
  271. return ret;
  272. }
  273. static int request_frame(AVFilterLink *outlink)
  274. {
  275. AVFilterContext *ctx = outlink->src;
  276. LoopContext *s = ctx->priv;
  277. int ret = 0;
  278. if ((!s->size) ||
  279. (s->nb_frames < s->size) ||
  280. (s->nb_frames >= s->size && s->loop == 0)) {
  281. ret = ff_request_frame(ctx->inputs[0]);
  282. } else {
  283. ret = push_frame(ctx);
  284. }
  285. if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
  286. ret = push_frame(ctx);
  287. }
  288. return ret;
  289. }
  290. static const AVOption loop_options[] = {
  291. { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
  292. { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
  293. { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
  294. { NULL }
  295. };
  296. AVFILTER_DEFINE_CLASS(loop);
  297. static const AVFilterPad inputs[] = {
  298. {
  299. .name = "default",
  300. .type = AVMEDIA_TYPE_VIDEO,
  301. .filter_frame = filter_frame,
  302. },
  303. { NULL }
  304. };
  305. static const AVFilterPad outputs[] = {
  306. {
  307. .name = "default",
  308. .type = AVMEDIA_TYPE_VIDEO,
  309. .request_frame = request_frame,
  310. },
  311. { NULL }
  312. };
  313. AVFilter ff_vf_loop = {
  314. .name = "loop",
  315. .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
  316. .priv_size = sizeof(LoopContext),
  317. .priv_class = &loop_class,
  318. .init = init,
  319. .uninit = uninit,
  320. .inputs = inputs,
  321. .outputs = outputs,
  322. };
  323. #endif /* CONFIG_LOOP_FILTER */