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.

205 lines
6.0KB

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