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.

213 lines
6.9KB

  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. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  82. {
  83. AVFilterLink *outlink = NULL;
  84. if (inlink->dst->output_count)
  85. outlink = inlink->dst->outputs[0];
  86. if (outlink) {
  87. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  88. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  89. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  90. }
  91. }
  92. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  93. {
  94. AVFilterLink *outlink = NULL;
  95. if (inlink->dst->output_count)
  96. outlink = inlink->dst->outputs[0];
  97. if (outlink)
  98. avfilter_draw_slice(outlink, y, h, slice_dir);
  99. }
  100. void avfilter_default_end_frame(AVFilterLink *inlink)
  101. {
  102. AVFilterLink *outlink = NULL;
  103. if (inlink->dst->output_count)
  104. outlink = inlink->dst->outputs[0];
  105. avfilter_unref_buffer(inlink->cur_buf);
  106. inlink->cur_buf = NULL;
  107. if (outlink) {
  108. if (outlink->out_buf) {
  109. avfilter_unref_buffer(outlink->out_buf);
  110. outlink->out_buf = NULL;
  111. }
  112. avfilter_end_frame(outlink);
  113. }
  114. }
  115. static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
  116. enum AVMediaType type, int offin, int offout)
  117. {
  118. int i;
  119. for (i = 0; i < ctx->input_count; i++)
  120. if (ctx->inputs[i] && ctx->inputs[i]->type == type)
  121. avfilter_formats_ref(fmts,
  122. (AVFilterFormats **)((uint8_t *)ctx->inputs[i]+offout));
  123. for (i = 0; i < ctx->output_count; i++)
  124. if (ctx->outputs[i] && ctx->outputs[i]->type == type)
  125. avfilter_formats_ref(fmts,
  126. (AVFilterFormats **)((uint8_t *)ctx->outputs[i]+offin));
  127. if (!fmts->refcount) {
  128. av_free(fmts->formats);
  129. av_free(fmts->refs);
  130. av_free(fmts);
  131. }
  132. }
  133. void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  134. {
  135. set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
  136. offsetof(AVFilterLink, in_formats),
  137. offsetof(AVFilterLink, out_formats));
  138. }
  139. void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  140. {
  141. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  142. offsetof(AVFilterLink, in_formats),
  143. offsetof(AVFilterLink, out_formats));
  144. }
  145. void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
  146. {
  147. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  148. offsetof(AVFilterLink, in_chlayouts),
  149. offsetof(AVFilterLink, out_chlayouts));
  150. }
  151. void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  152. {
  153. set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
  154. offsetof(AVFilterLink, in_packing),
  155. offsetof(AVFilterLink, out_packing));
  156. }
  157. int avfilter_default_query_formats(AVFilterContext *ctx)
  158. {
  159. avfilter_set_common_pixel_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_VIDEO));
  160. avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
  161. avfilter_set_common_channel_layouts(ctx, avfilter_make_all_channel_layouts());
  162. avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
  163. return 0;
  164. }
  165. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  166. {
  167. avfilter_start_frame(link->dst->outputs[0], picref);
  168. }
  169. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  170. {
  171. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  172. }
  173. void avfilter_null_end_frame(AVFilterLink *link)
  174. {
  175. avfilter_end_frame(link->dst->outputs[0]);
  176. }
  177. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  178. {
  179. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  180. }