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.

271 lines
9.1KB

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