defaults.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/avassert.h"
  22. #include "libavutil/audioconvert.h"
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/samplefmt.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  28. {
  29. av_free(ptr->data[0]);
  30. av_free(ptr);
  31. }
  32. /* TODO: set the buffer's priv member to a context structure for the whole
  33. * filter chain. This will allow for a buffer pool instead of the constant
  34. * alloc & free cycle currently implemented. */
  35. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  36. {
  37. int linesize[4];
  38. uint8_t *data[4];
  39. int i;
  40. AVFilterBufferRef *picref = NULL;
  41. AVFilterPool *pool = link->pool;
  42. if (pool) {
  43. for (i = 0; i < POOL_SIZE; i++) {
  44. picref = pool->pic[i];
  45. if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
  46. AVFilterBuffer *pic = picref->buf;
  47. pool->pic[i] = NULL;
  48. pool->count--;
  49. picref->video->w = w;
  50. picref->video->h = h;
  51. picref->perms = perms | AV_PERM_READ;
  52. picref->format = link->format;
  53. pic->refcount = 1;
  54. memcpy(picref->data, pic->data, sizeof(picref->data));
  55. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  56. pool->refcount++;
  57. return picref;
  58. }
  59. }
  60. } else {
  61. pool = link->pool = av_mallocz(sizeof(AVFilterPool));
  62. pool->refcount = 1;
  63. }
  64. // align: +2 is needed for swscaler, +16 to be SIMD-friendly
  65. if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
  66. return NULL;
  67. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  68. perms, w, h, link->format);
  69. if (!picref) {
  70. av_free(data[0]);
  71. return NULL;
  72. }
  73. memset(data[0], 128, i);
  74. picref->buf->priv = pool;
  75. picref->buf->free = NULL;
  76. pool->refcount++;
  77. return picref;
  78. }
  79. AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
  80. int nb_samples)
  81. {
  82. AVFilterBufferRef *samplesref = NULL;
  83. int linesize[8] = {0};
  84. uint8_t *data[8] = {0};
  85. int ch, nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  86. /* right now we don't support more than 8 channels */
  87. av_assert0(nb_channels <= 8);
  88. /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
  89. if (av_samples_alloc(data, linesize,
  90. nb_channels, nb_samples,
  91. av_get_alt_sample_fmt(link->format, link->planar),
  92. 16) < 0)
  93. return NULL;
  94. for (ch = 1; link->planar && ch < nb_channels; ch++)
  95. linesize[ch] = linesize[0];
  96. samplesref =
  97. avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
  98. nb_samples, link->format,
  99. link->channel_layout, link->planar);
  100. if (!samplesref) {
  101. av_free(data[0]);
  102. return NULL;
  103. }
  104. return samplesref;
  105. }
  106. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  107. {
  108. AVFilterLink *outlink = NULL;
  109. if (inlink->dst->output_count)
  110. outlink = inlink->dst->outputs[0];
  111. if (outlink) {
  112. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  113. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  114. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  115. }
  116. }
  117. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  118. {
  119. AVFilterLink *outlink = NULL;
  120. if (inlink->dst->output_count)
  121. outlink = inlink->dst->outputs[0];
  122. if (outlink)
  123. avfilter_draw_slice(outlink, y, h, slice_dir);
  124. }
  125. void avfilter_default_end_frame(AVFilterLink *inlink)
  126. {
  127. AVFilterLink *outlink = NULL;
  128. if (inlink->dst->output_count)
  129. outlink = inlink->dst->outputs[0];
  130. avfilter_unref_buffer(inlink->cur_buf);
  131. inlink->cur_buf = NULL;
  132. if (outlink) {
  133. if (outlink->out_buf) {
  134. avfilter_unref_buffer(outlink->out_buf);
  135. outlink->out_buf = NULL;
  136. }
  137. avfilter_end_frame(outlink);
  138. }
  139. }
  140. /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
  141. void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  142. {
  143. AVFilterLink *outlink = NULL;
  144. if (inlink->dst->output_count)
  145. outlink = inlink->dst->outputs[0];
  146. if (outlink) {
  147. outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE,
  148. samplesref->audio->nb_samples);
  149. outlink->out_buf->pts = samplesref->pts;
  150. outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
  151. avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  152. avfilter_unref_buffer(outlink->out_buf);
  153. outlink->out_buf = NULL;
  154. }
  155. avfilter_unref_buffer(samplesref);
  156. inlink->cur_buf = NULL;
  157. }
  158. static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
  159. enum AVMediaType type, int offin, int offout)
  160. {
  161. int i;
  162. for (i = 0; i < ctx->input_count; i++)
  163. if (ctx->inputs[i] && ctx->inputs[i]->type == type)
  164. avfilter_formats_ref(fmts,
  165. (AVFilterFormats **)((uint8_t *)ctx->inputs[i]+offout));
  166. for (i = 0; i < ctx->output_count; i++)
  167. if (ctx->outputs[i] && ctx->outputs[i]->type == type)
  168. avfilter_formats_ref(fmts,
  169. (AVFilterFormats **)((uint8_t *)ctx->outputs[i]+offin));
  170. if (!fmts->refcount) {
  171. av_free(fmts->formats);
  172. av_free(fmts->refs);
  173. av_free(fmts);
  174. }
  175. }
  176. void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  177. {
  178. set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
  179. offsetof(AVFilterLink, in_formats),
  180. offsetof(AVFilterLink, out_formats));
  181. }
  182. void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  183. {
  184. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  185. offsetof(AVFilterLink, in_formats),
  186. offsetof(AVFilterLink, out_formats));
  187. }
  188. void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
  189. {
  190. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  191. offsetof(AVFilterLink, in_chlayouts),
  192. offsetof(AVFilterLink, out_chlayouts));
  193. }
  194. void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  195. {
  196. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  197. offsetof(AVFilterLink, in_packing),
  198. offsetof(AVFilterLink, out_packing));
  199. }
  200. int avfilter_default_query_formats(AVFilterContext *ctx)
  201. {
  202. avfilter_set_common_pixel_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_VIDEO));
  203. avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
  204. avfilter_set_common_channel_layouts(ctx, avfilter_make_all_channel_layouts());
  205. avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
  206. return 0;
  207. }
  208. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  209. {
  210. avfilter_start_frame(link->dst->outputs[0], picref);
  211. }
  212. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  213. {
  214. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  215. }
  216. void avfilter_null_end_frame(AVFilterLink *link)
  217. {
  218. avfilter_end_frame(link->dst->outputs[0]);
  219. }
  220. void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  221. {
  222. avfilter_filter_samples(link->dst->outputs[0], samplesref);
  223. }
  224. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  225. {
  226. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  227. }
  228. AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
  229. int nb_samples)
  230. {
  231. return avfilter_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
  232. }