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.

295 lines
9.8KB

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