defaults.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. void avfilter_default_free_video_buffer(AVFilterPic *pic)
  25. {
  26. av_free(pic->data[0]);
  27. av_free(pic);
  28. }
  29. #define ALIGN(a) do{ \
  30. (a) = ((a) + 15) & (~15); \
  31. } while(0);
  32. /* TODO: set the buffer's priv member to a context structure for the whole
  33. * filter chain. This will allow for a buffer pool instead of the constant
  34. * alloc & free cycle currently implemented. */
  35. AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
  36. {
  37. AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
  38. AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
  39. int i, tempsize;
  40. char *buf;
  41. ref->pic = pic;
  42. ref->w = link->w;
  43. ref->h = link->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. pic->format = link->format;
  48. pic->free = avfilter_default_free_video_buffer;
  49. ff_fill_linesize((AVPicture *)pic, pic->format, ref->w);
  50. for (i=0; i<4;i++)
  51. ALIGN(pic->linesize[i]);
  52. tempsize = ff_fill_pointer((AVPicture *)pic, NULL, pic->format, ref->h);
  53. buf = av_malloc(tempsize);
  54. ff_fill_pointer((AVPicture *)pic, buf, pic->format, ref->h);
  55. memcpy(ref->data, pic->data, sizeof(pic->data));
  56. memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
  57. return ref;
  58. }
  59. void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  60. {
  61. AVFilterLink *out = NULL;
  62. if(link->dst->output_count)
  63. out = link->dst->outputs[0];
  64. if(out) {
  65. out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE);
  66. out->outpic->pts = picref->pts;
  67. avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
  68. }
  69. }
  70. void avfilter_default_end_frame(AVFilterLink *link)
  71. {
  72. AVFilterLink *out = NULL;
  73. if(link->dst->output_count)
  74. out = link->dst->outputs[0];
  75. avfilter_unref_pic(link->cur_pic);
  76. link->cur_pic = NULL;
  77. if(out) {
  78. if(out->outpic) {
  79. avfilter_unref_pic(out->outpic);
  80. out->outpic = NULL;
  81. }
  82. avfilter_end_frame(out);
  83. }
  84. }
  85. /**
  86. * default config_link() implementation for output video links to simplify
  87. * the implementation of one input one output video filters */
  88. int avfilter_default_config_output_link(AVFilterLink *link)
  89. {
  90. if(link->src->input_count && link->src->inputs[0]) {
  91. link->w = link->src->inputs[0]->w;
  92. link->h = link->src->inputs[0]->h;
  93. } else {
  94. /* XXX: any non-simple filter which would cause this branch to be taken
  95. * really should implement its own config_props() for this link. */
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. /**
  101. * A helper for query_formats() which sets all links to the same list of
  102. * formats. If there are no links hooked to this filter, the list of formats is
  103. * freed.
  104. *
  105. * FIXME: this will need changed for filters with a mix of pad types
  106. * (video + audio, etc)
  107. */
  108. void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
  109. {
  110. int count = 0, i;
  111. for(i = 0; i < ctx->input_count; i ++) {
  112. if(ctx->inputs[i]) {
  113. avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
  114. count ++;
  115. }
  116. }
  117. for(i = 0; i < ctx->output_count; i ++) {
  118. if(ctx->outputs[i]) {
  119. avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
  120. count ++;
  121. }
  122. }
  123. if(!count) {
  124. av_free(formats->formats);
  125. av_free(formats->refs);
  126. av_free(formats);
  127. }
  128. }
  129. int avfilter_default_query_formats(AVFilterContext *ctx)
  130. {
  131. avfilter_set_common_formats(ctx, avfilter_all_colorspaces());
  132. return 0;
  133. }