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 "libavcodec/imgconvert.h"
  22. #include "avfilter.h"
  23. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  24. static void avfilter_default_free_video_buffer(AVFilterPic *pic)
  25. {
  26. av_free(pic->data[0]);
  27. av_free(pic);
  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. AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  33. {
  34. AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
  35. AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
  36. int i, tempsize;
  37. char *buf;
  38. ref->pic = pic;
  39. ref->w = pic->w = w;
  40. ref->h = pic->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. pic->format = link->format;
  45. pic->free = avfilter_default_free_video_buffer;
  46. ff_fill_linesize((AVPicture *)pic, pic->format, ref->w);
  47. for (i=0; i<4;i++)
  48. pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
  49. tempsize = ff_fill_pointer((AVPicture *)pic, NULL, pic->format, ref->h);
  50. buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
  51. // SIMD-friendly
  52. ff_fill_pointer((AVPicture *)pic, buf, pic->format, ref->h);
  53. memcpy(ref->data, pic->data, sizeof(pic->data));
  54. memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
  55. return ref;
  56. }
  57. void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *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. out->outpic->pts = picref->pts;
  65. out->outpic->pos = picref->pos;
  66. out->outpic->pixel_aspect = picref->pixel_aspect;
  67. out->outpic->interlaced = picref->interlaced;
  68. out->outpic->top_field_first = picref->top_field_first;
  69. avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
  70. }
  71. }
  72. void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  73. {
  74. AVFilterLink *out = NULL;
  75. if(link->dst->output_count)
  76. out = link->dst->outputs[0];
  77. if(out)
  78. avfilter_draw_slice(out, y, h, slice_dir);
  79. }
  80. void avfilter_default_end_frame(AVFilterLink *link)
  81. {
  82. AVFilterLink *out = NULL;
  83. if(link->dst->output_count)
  84. out = link->dst->outputs[0];
  85. avfilter_unref_pic(link->cur_pic);
  86. link->cur_pic = NULL;
  87. if(out) {
  88. if(out->outpic) {
  89. avfilter_unref_pic(out->outpic);
  90. out->outpic = NULL;
  91. }
  92. avfilter_end_frame(out);
  93. }
  94. }
  95. /**
  96. * default config_link() implementation for output video links to simplify
  97. * the implementation of one input one output video filters */
  98. int avfilter_default_config_output_link(AVFilterLink *link)
  99. {
  100. if(link->src->input_count && link->src->inputs[0]) {
  101. link->w = link->src->inputs[0]->w;
  102. link->h = link->src->inputs[0]->h;
  103. } else {
  104. /* XXX: any non-simple filter which would cause this branch to be taken
  105. * really should implement its own config_props() for this link. */
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. /**
  111. * A helper for query_formats() which sets all links to the same list of
  112. * formats. If there are no links hooked to this filter, the list of formats is
  113. * freed.
  114. *
  115. * FIXME: this will need changed for filters with a mix of pad types
  116. * (video + audio, etc)
  117. */
  118. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  119. {
  120. int count = 0, i;
  121. for(i = 0; i < ctx->input_count; i ++) {
  122. if(ctx->inputs[i]) {
  123. avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
  124. count ++;
  125. }
  126. }
  127. for(i = 0; i < ctx->output_count; i ++) {
  128. if(ctx->outputs[i]) {
  129. avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
  130. count ++;
  131. }
  132. }
  133. if(!count) {
  134. av_free(formats->formats);
  135. av_free(formats->refs);
  136. av_free(formats);
  137. }
  138. }
  139. int avfilter_default_query_formats(AVFilterContext *ctx)
  140. {
  141. avfilter_set_common_formats(ctx, avfilter_all_colorspaces());
  142. return 0;
  143. }
  144. void avfilter_null_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  145. {
  146. avfilter_start_frame(link->dst->outputs[0], picref);
  147. }
  148. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  149. {
  150. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  151. }
  152. void avfilter_null_end_frame(AVFilterLink *link)
  153. {
  154. avfilter_end_frame(link->dst->outputs[0]);
  155. }
  156. AVFilterPicRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  157. {
  158. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  159. }