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.

276 lines
9.3KB

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