defaults.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. AVFilterBufferRef *samplesref = NULL;
  79. int linesize[8];
  80. uint8_t *data[8];
  81. int nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  82. /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
  83. if (av_samples_alloc(data, linesize,
  84. nb_channels, nb_samples, sample_fmt,
  85. planar, 16) < 0)
  86. return NULL;
  87. samplesref =
  88. avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
  89. nb_samples, sample_fmt,
  90. channel_layout, planar);
  91. if (!samplesref) {
  92. av_free(data[0]);
  93. return NULL;
  94. }
  95. return samplesref;
  96. }
  97. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  98. {
  99. AVFilterLink *outlink = NULL;
  100. if (inlink->dst->output_count)
  101. outlink = inlink->dst->outputs[0];
  102. if (outlink) {
  103. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  104. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  105. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  106. }
  107. }
  108. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  109. {
  110. AVFilterLink *outlink = NULL;
  111. if (inlink->dst->output_count)
  112. outlink = inlink->dst->outputs[0];
  113. if (outlink)
  114. avfilter_draw_slice(outlink, y, h, slice_dir);
  115. }
  116. void avfilter_default_end_frame(AVFilterLink *inlink)
  117. {
  118. AVFilterLink *outlink = NULL;
  119. if (inlink->dst->output_count)
  120. outlink = inlink->dst->outputs[0];
  121. avfilter_unref_buffer(inlink->cur_buf);
  122. inlink->cur_buf = NULL;
  123. if (outlink) {
  124. if (outlink->out_buf) {
  125. avfilter_unref_buffer(outlink->out_buf);
  126. outlink->out_buf = NULL;
  127. }
  128. avfilter_end_frame(outlink);
  129. }
  130. }
  131. /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
  132. void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  133. {
  134. AVFilterLink *outlink = NULL;
  135. if (inlink->dst->output_count)
  136. outlink = inlink->dst->outputs[0];
  137. if (outlink) {
  138. outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
  139. samplesref->audio->nb_samples,
  140. samplesref->audio->channel_layout,
  141. samplesref->audio->planar);
  142. outlink->out_buf->pts = samplesref->pts;
  143. outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
  144. avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  145. avfilter_unref_buffer(outlink->out_buf);
  146. outlink->out_buf = NULL;
  147. }
  148. avfilter_unref_buffer(samplesref);
  149. inlink->cur_buf = NULL;
  150. }
  151. /**
  152. * default config_link() implementation for output video links to simplify
  153. * the implementation of one input one output video filters */
  154. int avfilter_default_config_output_link(AVFilterLink *link)
  155. {
  156. if (link->src->input_count && link->src->inputs[0]) {
  157. if (link->type == AVMEDIA_TYPE_VIDEO) {
  158. link->w = link->src->inputs[0]->w;
  159. link->h = link->src->inputs[0]->h;
  160. link->time_base = link->src->inputs[0]->time_base;
  161. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  162. link->channel_layout = link->src->inputs[0]->channel_layout;
  163. link->sample_rate = link->src->inputs[0]->sample_rate;
  164. }
  165. } else {
  166. /* XXX: any non-simple filter which would cause this branch to be taken
  167. * really should implement its own config_props() for this link. */
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
  173. enum AVMediaType type, int offin, int offout)
  174. {
  175. int i;
  176. for (i = 0; i < ctx->input_count; i++)
  177. if (ctx->inputs[i] && ctx->inputs[i]->type == type)
  178. avfilter_formats_ref(fmts,
  179. (AVFilterFormats**)((void*)ctx->inputs[i]+offout));
  180. for (i = 0; i < ctx->output_count; i++)
  181. if (ctx->outputs[i] && ctx->outputs[i]->type == type)
  182. avfilter_formats_ref(fmts,
  183. (AVFilterFormats**)((void*)ctx->outputs[i]+offin));
  184. if (!fmts->refcount) {
  185. av_free(fmts->formats);
  186. av_free(fmts->refs);
  187. av_free(fmts);
  188. }
  189. }
  190. void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  191. {
  192. set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
  193. offsetof(AVFilterLink, in_formats),
  194. offsetof(AVFilterLink, out_formats));
  195. }
  196. void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  197. {
  198. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  199. offsetof(AVFilterLink, in_formats),
  200. offsetof(AVFilterLink, out_formats));
  201. }
  202. void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
  203. {
  204. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  205. offsetof(AVFilterLink, in_chlayouts),
  206. offsetof(AVFilterLink, out_chlayouts));
  207. }
  208. int avfilter_default_query_formats(AVFilterContext *ctx)
  209. {
  210. avfilter_set_common_pixel_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_VIDEO));
  211. avfilter_set_common_sample_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_AUDIO));
  212. avfilter_set_common_channel_layouts(ctx, avfilter_all_channel_layouts());
  213. return 0;
  214. }
  215. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  216. {
  217. avfilter_start_frame(link->dst->outputs[0], picref);
  218. }
  219. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  220. {
  221. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  222. }
  223. void avfilter_null_end_frame(AVFilterLink *link)
  224. {
  225. avfilter_end_frame(link->dst->outputs[0]);
  226. }
  227. void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  228. {
  229. avfilter_filter_samples(link->dst->outputs[0], samplesref);
  230. }
  231. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  232. {
  233. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  234. }
  235. AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
  236. enum AVSampleFormat sample_fmt, int size,
  237. int64_t channel_layout, int packed)
  238. {
  239. return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
  240. size, channel_layout, packed);
  241. }