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.

269 lines
7.1KB

  1. /*
  2. * Filter layer
  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 <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include "avfilter.h"
  25. /** list of registered filters, sorted by name */
  26. static int filter_count = 0;
  27. static AVFilter **filters = NULL;
  28. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  29. void avfilter_default_free_video_buffer(AVFilterPic *pic)
  30. {
  31. avpicture_free((AVPicture *) pic);
  32. av_free(pic);
  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. AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
  38. {
  39. AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
  40. AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
  41. ref->pic = pic;
  42. ref->w = link->w;
  43. ref->h = link->h;
  44. ref->perms = perms;
  45. pic->refcount = 1;
  46. pic->format = link->format;
  47. pic->free = avfilter_default_free_video_buffer;
  48. avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
  49. ref->data[0] = pic->data[0];
  50. ref->data[1] = pic->data[1];
  51. ref->data[2] = pic->data[2];
  52. ref->data[3] = pic->data[3];
  53. return ref;
  54. }
  55. void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  56. {
  57. link->cur_pic = picref;
  58. }
  59. void avfilter_default_end_frame(AVFilterLink *link)
  60. {
  61. avfilter_unref_pic(link->cur_pic);
  62. link->cur_pic = NULL;
  63. }
  64. AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref)
  65. {
  66. AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
  67. memcpy(ret, ref, sizeof(AVFilterPicRef));
  68. ret->pic->refcount ++;
  69. return ret;
  70. }
  71. void avfilter_unref_pic(AVFilterPicRef *ref)
  72. {
  73. if(-- ref->pic->refcount == 0)
  74. ref->pic->free(ref->pic);
  75. av_free(ref);
  76. }
  77. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  78. AVFilterContext *dst, unsigned dstpad)
  79. {
  80. AVFilterLink *link;
  81. if(src->outputs[srcpad] || dst->inputs[dstpad])
  82. return -1;
  83. src->outputs[srcpad] =
  84. dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
  85. link->src = src;
  86. link->dst = dst;
  87. link->srcpad = srcpad;
  88. link->dstpad = dstpad;
  89. link->cur_pic = NULL;
  90. src->filter->outputs[dstpad].set_video_props(link);
  91. return 0;
  92. }
  93. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
  94. {
  95. AVFilterPicRef *ret = NULL;
  96. if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
  97. ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
  98. if(!ret)
  99. ret = avfilter_default_get_video_buffer(link, perms);
  100. return ret;
  101. }
  102. void avfilter_request_frame(AVFilterLink *link)
  103. {
  104. link->src->filter->outputs[link->srcpad].request_frame(link);
  105. }
  106. /* XXX: should we do the duplicating of the picture ref here, instead of
  107. * forcing the source filter to do it? */
  108. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  109. {
  110. void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
  111. start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
  112. if(!start_frame)
  113. start_frame = avfilter_default_start_frame;
  114. start_frame(link, picref);
  115. }
  116. void avfilter_end_frame(AVFilterLink *link)
  117. {
  118. void (*end_frame)(AVFilterLink *);
  119. end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
  120. if(!end_frame)
  121. end_frame = avfilter_default_end_frame;
  122. end_frame(link);
  123. }
  124. void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
  125. {
  126. link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
  127. }
  128. static int filter_cmp(const void *aa, const void *bb)
  129. {
  130. const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
  131. return strcmp(a->name, b->name);
  132. }
  133. AVFilter *avfilter_get_by_name(char *name)
  134. {
  135. AVFilter key = { .name = name, };
  136. AVFilter *key2 = &key;
  137. AVFilter **ret;
  138. ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
  139. if(ret)
  140. return *ret;
  141. return NULL;
  142. }
  143. /* FIXME: insert in order, rather than insert at end + resort */
  144. void avfilter_register(AVFilter *filter)
  145. {
  146. filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
  147. filters[filter_count] = filter;
  148. qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
  149. }
  150. void avfilter_init(void)
  151. {
  152. }
  153. void avfilter_uninit(void)
  154. {
  155. av_freep(&filters);
  156. filter_count = 0;
  157. }
  158. static int pad_count(const AVFilterPad *pads)
  159. {
  160. AVFilterPad *p = (AVFilterPad *) pads;
  161. int count;
  162. for(count = 0; p->name; count ++) p ++;
  163. return count;
  164. }
  165. static const char *filter_name(void *p)
  166. {
  167. AVFilterContext *filter = p;
  168. return filter->filter->name;
  169. }
  170. AVFilterContext *avfilter_create(AVFilter *filter)
  171. {
  172. AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
  173. ret->av_class = av_mallocz(sizeof(AVClass));
  174. ret->av_class->item_name = filter_name;
  175. ret->filter = filter;
  176. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
  177. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
  178. ret->priv = av_mallocz(filter->priv_size);
  179. return ret;
  180. }
  181. void avfilter_destroy(AVFilterContext *filter)
  182. {
  183. int i;
  184. if(filter->filter->uninit)
  185. filter->filter->uninit(filter);
  186. for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
  187. if(filter->inputs[i])
  188. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  189. av_free(filter->inputs[i]);
  190. }
  191. for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
  192. if(filter->outputs[i])
  193. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  194. av_free(filter->outputs[i]);
  195. }
  196. av_free(filter->inputs);
  197. av_free(filter->outputs);
  198. av_free(filter->priv);
  199. av_free(filter->av_class);
  200. av_free(filter);
  201. }
  202. AVFilterContext *avfilter_create_by_name(char *name)
  203. {
  204. AVFilter *filt;
  205. if(!(filt = avfilter_get_by_name(name))) return NULL;
  206. return avfilter_create(filt);
  207. }
  208. int avfilter_init_filter(AVFilterContext *filter)
  209. {
  210. int ret, i;
  211. if(filter->filter->init)
  212. if((ret = filter->filter->init(filter))) return ret;
  213. for(i = 0; i < pad_count(filter->filter->outputs); i ++)
  214. if(filter->outputs[i])
  215. filter->filter->outputs[i].set_video_props(filter->outputs[i]);
  216. return 0;
  217. }