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.

255 lines
8.4KB

  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. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  27. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  28. {
  29. if (ptr->extended_data != ptr->data)
  30. av_freep(&ptr->extended_data);
  31. av_free(ptr->data[0]);
  32. av_free(ptr);
  33. }
  34. /* TODO: set the buffer's priv member to a context structure for the whole
  35. * filter chain. This will allow for a buffer pool instead of the constant
  36. * alloc & free cycle currently implemented. */
  37. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  38. {
  39. int linesize[4];
  40. uint8_t *data[4];
  41. AVFilterBufferRef *picref = NULL;
  42. // +2 is needed for swscaler, +16 to be SIMD-friendly
  43. if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
  44. return NULL;
  45. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  46. perms, w, h, link->format);
  47. if (!picref) {
  48. av_free(data[0]);
  49. return NULL;
  50. }
  51. return picref;
  52. }
  53. AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
  54. enum AVSampleFormat sample_fmt, int nb_samples,
  55. uint64_t channel_layout)
  56. {
  57. AVFilterBufferRef *samplesref = NULL;
  58. uint8_t **data;
  59. int planar = av_sample_fmt_is_planar(sample_fmt);
  60. int nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  61. int planes = planar ? nb_channels : 1;
  62. int linesize;
  63. if (!(data = av_mallocz(sizeof(*data) * planes)))
  64. goto fail;
  65. if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, sample_fmt, 0) < 0)
  66. goto fail;
  67. samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
  68. nb_samples, sample_fmt,
  69. channel_layout);
  70. if (!samplesref)
  71. goto fail;
  72. av_freep(&data);
  73. fail:
  74. if (data)
  75. av_freep(&data[0]);
  76. av_freep(&data);
  77. return samplesref;
  78. }
  79. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  80. {
  81. AVFilterLink *outlink = NULL;
  82. if (inlink->dst->output_count)
  83. outlink = inlink->dst->outputs[0];
  84. if (outlink) {
  85. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  86. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  87. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  88. }
  89. }
  90. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  91. {
  92. AVFilterLink *outlink = NULL;
  93. if (inlink->dst->output_count)
  94. outlink = inlink->dst->outputs[0];
  95. if (outlink)
  96. avfilter_draw_slice(outlink, y, h, slice_dir);
  97. }
  98. void avfilter_default_end_frame(AVFilterLink *inlink)
  99. {
  100. AVFilterLink *outlink = NULL;
  101. if (inlink->dst->output_count)
  102. outlink = inlink->dst->outputs[0];
  103. avfilter_unref_buffer(inlink->cur_buf);
  104. inlink->cur_buf = NULL;
  105. if (outlink) {
  106. if (outlink->out_buf) {
  107. avfilter_unref_buffer(outlink->out_buf);
  108. outlink->out_buf = NULL;
  109. }
  110. avfilter_end_frame(outlink);
  111. }
  112. }
  113. /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
  114. void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  115. {
  116. AVFilterLink *outlink = NULL;
  117. if (inlink->dst->output_count)
  118. outlink = inlink->dst->outputs[0];
  119. if (outlink) {
  120. outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
  121. samplesref->audio->nb_samples,
  122. samplesref->audio->channel_layout);
  123. outlink->out_buf->pts = samplesref->pts;
  124. outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
  125. avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  126. avfilter_unref_buffer(outlink->out_buf);
  127. outlink->out_buf = NULL;
  128. }
  129. avfilter_unref_buffer(samplesref);
  130. inlink->cur_buf = NULL;
  131. }
  132. /**
  133. * default config_link() implementation for output video links to simplify
  134. * the implementation of one input one output video filters */
  135. int avfilter_default_config_output_link(AVFilterLink *link)
  136. {
  137. if (link->src->input_count && link->src->inputs[0]) {
  138. if (link->type == AVMEDIA_TYPE_VIDEO) {
  139. link->w = link->src->inputs[0]->w;
  140. link->h = link->src->inputs[0]->h;
  141. link->time_base = link->src->inputs[0]->time_base;
  142. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  143. link->channel_layout = link->src->inputs[0]->channel_layout;
  144. link->sample_rate = link->src->inputs[0]->sample_rate;
  145. }
  146. } else {
  147. /* XXX: any non-simple filter which would cause this branch to be taken
  148. * really should implement its own config_props() for this link. */
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. /**
  154. * A helper for query_formats() which sets all links to the same list of
  155. * formats. If there are no links hooked to this filter, the list of formats is
  156. * freed.
  157. *
  158. * FIXME: this will need changed for filters with a mix of pad types
  159. * (video + audio, etc)
  160. */
  161. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  162. {
  163. int count = 0, i;
  164. for (i = 0; i < ctx->input_count; i++) {
  165. if (ctx->inputs[i]) {
  166. avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
  167. count++;
  168. }
  169. }
  170. for (i = 0; i < ctx->output_count; i++) {
  171. if (ctx->outputs[i]) {
  172. avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
  173. count++;
  174. }
  175. }
  176. if (!count) {
  177. av_free(formats->formats);
  178. av_free(formats->refs);
  179. av_free(formats);
  180. }
  181. }
  182. int avfilter_default_query_formats(AVFilterContext *ctx)
  183. {
  184. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  185. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  186. AVMEDIA_TYPE_VIDEO;
  187. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  188. return 0;
  189. }
  190. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  191. {
  192. avfilter_start_frame(link->dst->outputs[0], picref);
  193. }
  194. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  195. {
  196. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  197. }
  198. void avfilter_null_end_frame(AVFilterLink *link)
  199. {
  200. avfilter_end_frame(link->dst->outputs[0]);
  201. }
  202. void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  203. {
  204. avfilter_filter_samples(link->dst->outputs[0], samplesref);
  205. }
  206. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  207. {
  208. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  209. }
  210. AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
  211. enum AVSampleFormat sample_fmt, int nb_samples,
  212. uint64_t channel_layout)
  213. {
  214. return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
  215. nb_samples, channel_layout);
  216. }