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.

209 lines
7.3KB

  1. /*
  2. * Filter layer - default implementations
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/audioconvert.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/samplefmt.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. #include "formats.h"
  27. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  28. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  29. {
  30. if (ptr->extended_data != ptr->data)
  31. av_freep(&ptr->extended_data);
  32. av_free(ptr->data[0]);
  33. av_free(ptr);
  34. }
  35. /* TODO: set the buffer's priv member to a context structure for the whole
  36. * filter chain. This will allow for a buffer pool instead of the constant
  37. * alloc & free cycle currently implemented. */
  38. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  39. {
  40. int linesize[4];
  41. uint8_t *data[4];
  42. AVFilterBufferRef *picref = NULL;
  43. // +2 is needed for swscaler, +16 to be SIMD-friendly
  44. if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
  45. return NULL;
  46. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  47. perms, w, h, link->format);
  48. if (!picref) {
  49. av_free(data[0]);
  50. return NULL;
  51. }
  52. return picref;
  53. }
  54. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  55. {
  56. AVFilterLink *outlink = NULL;
  57. if (inlink->dst->output_count)
  58. outlink = inlink->dst->outputs[0];
  59. if (outlink) {
  60. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  61. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  62. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  63. }
  64. }
  65. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  66. {
  67. AVFilterLink *outlink = NULL;
  68. if (inlink->dst->output_count)
  69. outlink = inlink->dst->outputs[0];
  70. if (outlink)
  71. avfilter_draw_slice(outlink, y, h, slice_dir);
  72. }
  73. void avfilter_default_end_frame(AVFilterLink *inlink)
  74. {
  75. AVFilterLink *outlink = NULL;
  76. if (inlink->dst->output_count)
  77. outlink = inlink->dst->outputs[0];
  78. avfilter_unref_buffer(inlink->cur_buf);
  79. inlink->cur_buf = NULL;
  80. if (outlink) {
  81. if (outlink->out_buf) {
  82. avfilter_unref_buffer(outlink->out_buf);
  83. outlink->out_buf = NULL;
  84. }
  85. avfilter_end_frame(outlink);
  86. }
  87. }
  88. /**
  89. * default config_link() implementation for output video links to simplify
  90. * the implementation of one input one output video filters */
  91. int avfilter_default_config_output_link(AVFilterLink *link)
  92. {
  93. if (link->src->input_count && link->src->inputs[0]) {
  94. if (link->type == AVMEDIA_TYPE_VIDEO) {
  95. link->w = link->src->inputs[0]->w;
  96. link->h = link->src->inputs[0]->h;
  97. link->time_base = link->src->inputs[0]->time_base;
  98. }
  99. } else {
  100. /* XXX: any non-simple filter which would cause this branch to be taken
  101. * really should implement its own config_props() for this link. */
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
  107. { \
  108. int count = 0, i; \
  109. \
  110. for (i = 0; i < ctx->input_count; i++) { \
  111. if (ctx->inputs[i]) { \
  112. ref(fmts, &ctx->inputs[i]->out_fmts); \
  113. count++; \
  114. } \
  115. } \
  116. for (i = 0; i < ctx->output_count; i++) { \
  117. if (ctx->outputs[i]) { \
  118. ref(fmts, &ctx->outputs[i]->in_fmts); \
  119. count++; \
  120. } \
  121. } \
  122. \
  123. if (!count) { \
  124. av_freep(&fmts->list); \
  125. av_freep(&fmts->refs); \
  126. av_freep(&fmts); \
  127. } \
  128. }
  129. void ff_set_common_channel_layouts(AVFilterContext *ctx,
  130. AVFilterChannelLayouts *layouts)
  131. {
  132. SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
  133. ff_channel_layouts_ref, channel_layouts);
  134. }
  135. void ff_set_common_samplerates(AVFilterContext *ctx,
  136. AVFilterFormats *samplerates)
  137. {
  138. SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
  139. avfilter_formats_ref, formats);
  140. }
  141. /**
  142. * A helper for query_formats() which sets all links to the same list of
  143. * formats. If there are no links hooked to this filter, the list of formats is
  144. * freed.
  145. */
  146. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  147. {
  148. SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
  149. avfilter_formats_ref, formats);
  150. }
  151. int avfilter_default_query_formats(AVFilterContext *ctx)
  152. {
  153. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  154. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  155. AVMEDIA_TYPE_VIDEO;
  156. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  157. if (type == AVMEDIA_TYPE_AUDIO) {
  158. ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
  159. ff_set_common_samplerates(ctx, ff_all_samplerates());
  160. }
  161. return 0;
  162. }
  163. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  164. {
  165. avfilter_start_frame(link->dst->outputs[0], picref);
  166. }
  167. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  168. {
  169. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  170. }
  171. void avfilter_null_end_frame(AVFilterLink *link)
  172. {
  173. avfilter_end_frame(link->dst->outputs[0]);
  174. }
  175. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  176. {
  177. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  178. }