f_reverse.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2015 Derek Buitenhuis
  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/opt.h"
  21. #include "avfilter.h"
  22. #include "formats.h"
  23. #include "internal.h"
  24. #include "video.h"
  25. #define DEFAULT_LENGTH 300
  26. typedef struct ReverseContext {
  27. int nb_frames;
  28. AVFrame **frames;
  29. unsigned int frames_size;
  30. unsigned int pts_size;
  31. int64_t *pts;
  32. int flush_idx;
  33. } ReverseContext;
  34. static av_cold int init(AVFilterContext *ctx)
  35. {
  36. ReverseContext *s = ctx->priv;
  37. s->pts = av_fast_realloc(NULL, &s->pts_size,
  38. DEFAULT_LENGTH * sizeof(*(s->pts)));
  39. if (!s->pts)
  40. return AVERROR(ENOMEM);
  41. s->frames = av_fast_realloc(NULL, &s->frames_size,
  42. DEFAULT_LENGTH * sizeof(*(s->frames)));
  43. if (!s->frames) {
  44. av_freep(&s->pts);
  45. return AVERROR(ENOMEM);
  46. }
  47. return 0;
  48. }
  49. static av_cold void uninit(AVFilterContext *ctx)
  50. {
  51. ReverseContext *s = ctx->priv;
  52. av_freep(&s->pts);
  53. av_freep(&s->frames);
  54. }
  55. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  56. {
  57. AVFilterContext *ctx = inlink->dst;
  58. ReverseContext *s = ctx->priv;
  59. void *ptr;
  60. if (s->nb_frames + 1 > s->pts_size / sizeof(*(s->pts))) {
  61. ptr = av_fast_realloc(s->pts, &s->pts_size, s->pts_size * 2);
  62. if (!ptr)
  63. return AVERROR(ENOMEM);
  64. s->pts = ptr;
  65. }
  66. if (s->nb_frames + 1 > s->frames_size / sizeof(*(s->frames))) {
  67. ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2);
  68. if (!ptr)
  69. return AVERROR(ENOMEM);
  70. s->frames = ptr;
  71. }
  72. s->frames[s->nb_frames] = in;
  73. s->pts[s->nb_frames] = in->pts;
  74. s->nb_frames++;
  75. return 0;
  76. }
  77. #if CONFIG_REVERSE_FILTER
  78. static int request_frame(AVFilterLink *outlink)
  79. {
  80. AVFilterContext *ctx = outlink->src;
  81. ReverseContext *s = ctx->priv;
  82. int ret;
  83. ret = ff_request_frame(ctx->inputs[0]);
  84. if (ret == AVERROR_EOF && s->nb_frames > 0) {
  85. AVFrame *out = s->frames[s->nb_frames - 1];
  86. out->pts = s->pts[s->flush_idx++];
  87. ret = ff_filter_frame(outlink, out);
  88. s->nb_frames--;
  89. }
  90. return ret;
  91. }
  92. static const AVFilterPad reverse_inputs[] = {
  93. {
  94. .name = "default",
  95. .type = AVMEDIA_TYPE_VIDEO,
  96. .filter_frame = filter_frame,
  97. },
  98. { NULL }
  99. };
  100. static const AVFilterPad reverse_outputs[] = {
  101. {
  102. .name = "default",
  103. .type = AVMEDIA_TYPE_VIDEO,
  104. .request_frame = request_frame,
  105. },
  106. { NULL }
  107. };
  108. AVFilter ff_vf_reverse = {
  109. .name = "reverse",
  110. .description = NULL_IF_CONFIG_SMALL("Reverse a clip."),
  111. .priv_size = sizeof(ReverseContext),
  112. .init = init,
  113. .uninit = uninit,
  114. .inputs = reverse_inputs,
  115. .outputs = reverse_outputs,
  116. };
  117. #endif /* CONFIG_REVERSE_FILTER */
  118. #if CONFIG_AREVERSE_FILTER
  119. static int query_formats(AVFilterContext *ctx)
  120. {
  121. AVFilterFormats *formats;
  122. AVFilterChannelLayouts *layouts;
  123. int ret;
  124. layouts = ff_all_channel_counts();
  125. if (!layouts)
  126. return AVERROR(ENOMEM);
  127. ret = ff_set_common_channel_layouts(ctx, layouts);
  128. if (ret < 0)
  129. return ret;
  130. ret = ff_set_common_formats(ctx, ff_planar_sample_fmts());
  131. if (ret < 0)
  132. return ret;
  133. formats = ff_all_samplerates();
  134. if (!formats)
  135. return AVERROR(ENOMEM);
  136. return ff_set_common_samplerates(ctx, formats);
  137. }
  138. static int areverse_request_frame(AVFilterLink *outlink)
  139. {
  140. AVFilterContext *ctx = outlink->src;
  141. ReverseContext *s = ctx->priv;
  142. int ret, p, i, j;
  143. ret = ff_request_frame(ctx->inputs[0]);
  144. if (ret == AVERROR_EOF && s->nb_frames > 0) {
  145. AVFrame *out = s->frames[s->nb_frames - 1];
  146. out->pts = s->pts[s->flush_idx++];
  147. for (p = 0; p < outlink->channels; p++) {
  148. switch (outlink->format) {
  149. case AV_SAMPLE_FMT_U8P: {
  150. uint8_t *dst = (uint8_t *)out->extended_data[p];
  151. for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
  152. FFSWAP(uint8_t, dst[i], dst[j]);
  153. }
  154. break;
  155. case AV_SAMPLE_FMT_S16P: {
  156. int16_t *dst = (int16_t *)out->extended_data[p];
  157. for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
  158. FFSWAP(int16_t, dst[i], dst[j]);
  159. }
  160. break;
  161. case AV_SAMPLE_FMT_S32P: {
  162. int32_t *dst = (int32_t *)out->extended_data[p];
  163. for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
  164. FFSWAP(int32_t, dst[i], dst[j]);
  165. }
  166. break;
  167. case AV_SAMPLE_FMT_FLTP: {
  168. float *dst = (float *)out->extended_data[p];
  169. for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
  170. FFSWAP(float, dst[i], dst[j]);
  171. }
  172. break;
  173. case AV_SAMPLE_FMT_DBLP: {
  174. double *dst = (double *)out->extended_data[p];
  175. for (i = 0, j = out->nb_samples - 1; i < j; i++, j--)
  176. FFSWAP(double, dst[i], dst[j]);
  177. }
  178. break;
  179. }
  180. }
  181. ret = ff_filter_frame(outlink, out);
  182. s->nb_frames--;
  183. }
  184. return ret;
  185. }
  186. static const AVFilterPad areverse_inputs[] = {
  187. {
  188. .name = "default",
  189. .type = AVMEDIA_TYPE_AUDIO,
  190. .filter_frame = filter_frame,
  191. .needs_writable = 1,
  192. },
  193. { NULL }
  194. };
  195. static const AVFilterPad areverse_outputs[] = {
  196. {
  197. .name = "default",
  198. .type = AVMEDIA_TYPE_AUDIO,
  199. .request_frame = areverse_request_frame,
  200. },
  201. { NULL }
  202. };
  203. AVFilter ff_af_areverse = {
  204. .name = "areverse",
  205. .description = NULL_IF_CONFIG_SMALL("Reverse an audio clip."),
  206. .query_formats = query_formats,
  207. .priv_size = sizeof(ReverseContext),
  208. .init = init,
  209. .uninit = uninit,
  210. .inputs = areverse_inputs,
  211. .outputs = areverse_outputs,
  212. };
  213. #endif /* CONFIG_AREVERSE_FILTER */