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.

191 lines
5.7KB

  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 "libavcore/imgutils.h"
  22. #include "avfilter.h"
  23. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  24. static void avfilter_default_free_buffer(AVFilterBuffer *ptr)
  25. {
  26. av_free(ptr->data[0]);
  27. av_free(ptr);
  28. }
  29. /* TODO: set the buffer's priv member to a context structure for the whole
  30. * filter chain. This will allow for a buffer pool instead of the constant
  31. * alloc & free cycle currently implemented. */
  32. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  33. {
  34. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  35. AVFilterBufferRef *ref = av_mallocz(sizeof(AVFilterBufferRef));
  36. int i, tempsize;
  37. char *buf;
  38. ref->buf = pic;
  39. ref->w = w;
  40. ref->h = h;
  41. /* make sure the buffer gets read permission or it's useless for output */
  42. ref->perms = perms | AV_PERM_READ;
  43. pic->refcount = 1;
  44. ref->format = link->format;
  45. pic->free = avfilter_default_free_buffer;
  46. av_fill_image_linesizes(pic->linesize, ref->format, ref->w);
  47. for (i=0; i<4;i++)
  48. pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
  49. tempsize = av_fill_image_pointers(pic->data, ref->format, ref->h, NULL, pic->linesize);
  50. buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
  51. // SIMD-friendly
  52. av_fill_image_pointers(pic->data, ref->format, ref->h, buf, pic->linesize);
  53. memcpy(ref->data, pic->data, 4*sizeof(pic->data[0]));
  54. memcpy(ref->linesize, pic->linesize, 4*sizeof(pic->linesize[0]));
  55. return ref;
  56. }
  57. void avfilter_default_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  58. {
  59. AVFilterLink *out = NULL;
  60. if(link->dst->output_count)
  61. out = link->dst->outputs[0];
  62. if(out) {
  63. out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE, out->w, out->h);
  64. avfilter_copy_buffer_ref_props(out->outpic, picref);
  65. avfilter_start_frame(out, avfilter_ref_buffer(out->outpic, ~0));
  66. }
  67. }
  68. void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  69. {
  70. AVFilterLink *out = NULL;
  71. if(link->dst->output_count)
  72. out = link->dst->outputs[0];
  73. if(out)
  74. avfilter_draw_slice(out, y, h, slice_dir);
  75. }
  76. void avfilter_default_end_frame(AVFilterLink *link)
  77. {
  78. AVFilterLink *out = NULL;
  79. if(link->dst->output_count)
  80. out = link->dst->outputs[0];
  81. avfilter_unref_buffer(link->cur_pic);
  82. link->cur_pic = NULL;
  83. if(out) {
  84. if(out->outpic) {
  85. avfilter_unref_buffer(out->outpic);
  86. out->outpic = NULL;
  87. }
  88. avfilter_end_frame(out);
  89. }
  90. }
  91. /**
  92. * default config_link() implementation for output video links to simplify
  93. * the implementation of one input one output video filters */
  94. int avfilter_default_config_output_link(AVFilterLink *link)
  95. {
  96. if(link->src->input_count && link->src->inputs[0]) {
  97. link->w = link->src->inputs[0]->w;
  98. link->h = link->src->inputs[0]->h;
  99. } else {
  100. /* XXX: any non-simple filter which would cause this branch to be taken
  101. * really should implement its own config_props() for this link. */
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. /**
  107. * A helper for query_formats() which sets all links to the same list of
  108. * formats. If there are no links hooked to this filter, the list of formats is
  109. * freed.
  110. *
  111. * FIXME: this will need changed for filters with a mix of pad types
  112. * (video + audio, etc)
  113. */
  114. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  115. {
  116. int count = 0, i;
  117. for(i = 0; i < ctx->input_count; i ++) {
  118. if(ctx->inputs[i]) {
  119. avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
  120. count ++;
  121. }
  122. }
  123. for(i = 0; i < ctx->output_count; i ++) {
  124. if(ctx->outputs[i]) {
  125. avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
  126. count ++;
  127. }
  128. }
  129. if(!count) {
  130. av_free(formats->formats);
  131. av_free(formats->refs);
  132. av_free(formats);
  133. }
  134. }
  135. int avfilter_default_query_formats(AVFilterContext *ctx)
  136. {
  137. enum AVMediaType type = ctx->inputs [0] ? ctx->inputs [0]->type :
  138. ctx->outputs[0] ? ctx->outputs[0]->type :
  139. AVMEDIA_TYPE_VIDEO;
  140. avfilter_set_common_formats(ctx, avfilter_all_formats(type));
  141. return 0;
  142. }
  143. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  144. {
  145. avfilter_start_frame(link->dst->outputs[0], picref);
  146. }
  147. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  148. {
  149. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  150. }
  151. void avfilter_null_end_frame(AVFilterLink *link)
  152. {
  153. avfilter_end_frame(link->dst->outputs[0]);
  154. }
  155. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  156. {
  157. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  158. }