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.

336 lines
9.0KB

  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 <stdarg.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include "avfilter.h"
  26. #include "allfilters.h"
  27. /** list of registered filters, sorted by name */
  28. static int filter_count = 0;
  29. static AVFilter **filters = NULL;
  30. /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
  31. void avfilter_default_free_video_buffer(AVFilterPic *pic)
  32. {
  33. avpicture_free((AVPicture *) pic);
  34. av_free(pic);
  35. }
  36. /* TODO: set the buffer's priv member to a context structure for the whole
  37. * filter chain. This will allow for a buffer pool instead of the constant
  38. * alloc & free cycle currently implemented. */
  39. AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
  40. {
  41. AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
  42. AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
  43. ref->pic = pic;
  44. ref->w = link->w;
  45. ref->h = link->h;
  46. ref->perms = perms;
  47. pic->refcount = 1;
  48. pic->format = link->format;
  49. pic->free = avfilter_default_free_video_buffer;
  50. avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
  51. memcpy(ref->data, pic->data, sizeof(pic->data));
  52. memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
  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. /**
  78. * default config_link() implementation for output video links to simplify
  79. * the implementation of one input one output video filters */
  80. static int default_config_output_link(AVFilterLink *link)
  81. {
  82. link->w = link->src->inputs[0]->w;
  83. link->h = link->src->inputs[0]->h;
  84. return 0;
  85. }
  86. /**
  87. * default query_formats() implementation for output video links to simplify
  88. * the implementation of one input one output video filters */
  89. static int *default_query_output_formats(AVFilterLink *link)
  90. {
  91. return avfilter_make_format_list(1, link->src->inputs[0]->format);
  92. }
  93. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  94. AVFilterContext *dst, unsigned dstpad)
  95. {
  96. AVFilterLink *link;
  97. int *fmts[2], i, j;
  98. if(src->outputs[srcpad] || dst->inputs[dstpad])
  99. return -1;
  100. src->outputs[srcpad] =
  101. dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
  102. link->src = src;
  103. link->dst = dst;
  104. link->srcpad = srcpad;
  105. link->dstpad = dstpad;
  106. link->cur_pic = NULL;
  107. /* find a format both filters support - TODO: auto-insert conversion filter */
  108. if(src->filter->outputs[srcpad].query_formats)
  109. fmts[0] = src->filter->outputs[srcpad].query_formats(link);
  110. else
  111. fmts[0] = default_query_output_formats(link);
  112. fmts[1] = dst->filter-> inputs[dstpad].query_formats(link);
  113. for(i = 0; fmts[0][i] != -1; i ++)
  114. for(j = 0; fmts[1][j] != -1; j ++)
  115. if(fmts[0][i] == fmts[1][j]) {
  116. link->format = fmts[0][i];
  117. goto format_done;
  118. }
  119. format_done:
  120. av_free(fmts[0]);
  121. av_free(fmts[1]);
  122. if(link->format == -1) {
  123. /* failed to find a format. fail at creating the link */
  124. av_free(link);
  125. src->outputs[srcpad] = NULL;
  126. dst->inputs[dstpad] = NULL;
  127. return -1;
  128. }
  129. if (src->filter->outputs[srcpad].config_props)
  130. src->filter->outputs[srcpad].config_props(link);
  131. else
  132. default_config_output_link(link);
  133. if (dst->filter->inputs[dstpad].config_props)
  134. dst->filter->inputs[dstpad].config_props(link);
  135. return 0;
  136. }
  137. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
  138. {
  139. AVFilterPicRef *ret = NULL;
  140. if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
  141. ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
  142. if(!ret)
  143. ret = avfilter_default_get_video_buffer(link, perms);
  144. return ret;
  145. }
  146. void avfilter_request_frame(AVFilterLink *link)
  147. {
  148. link->src->filter->outputs[link->srcpad].request_frame(link);
  149. }
  150. /* XXX: should we do the duplicating of the picture ref here, instead of
  151. * forcing the source filter to do it? */
  152. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  153. {
  154. void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
  155. start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
  156. if(!start_frame)
  157. start_frame = avfilter_default_start_frame;
  158. start_frame(link, picref);
  159. }
  160. void avfilter_end_frame(AVFilterLink *link)
  161. {
  162. void (*end_frame)(AVFilterLink *);
  163. end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
  164. if(!end_frame)
  165. end_frame = avfilter_default_end_frame;
  166. end_frame(link);
  167. }
  168. void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
  169. {
  170. link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
  171. }
  172. static int filter_cmp(const void *aa, const void *bb)
  173. {
  174. const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
  175. return strcmp(a->name, b->name);
  176. }
  177. AVFilter *avfilter_get_by_name(char *name)
  178. {
  179. AVFilter key = { .name = name, };
  180. AVFilter *key2 = &key;
  181. AVFilter **ret;
  182. ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
  183. if(ret)
  184. return *ret;
  185. return NULL;
  186. }
  187. /* FIXME: insert in order, rather than insert at end + resort */
  188. void avfilter_register(AVFilter *filter)
  189. {
  190. filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
  191. filters[filter_count] = filter;
  192. qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
  193. }
  194. void avfilter_init(void)
  195. {
  196. avfilter_register(&vsrc_dummy);
  197. avfilter_register(&vf_crop);
  198. avfilter_register(&vf_passthrough);
  199. avfilter_register(&vo_sdl);
  200. }
  201. void avfilter_uninit(void)
  202. {
  203. av_freep(&filters);
  204. filter_count = 0;
  205. }
  206. static int pad_count(const AVFilterPad *pads)
  207. {
  208. AVFilterPad *p = (AVFilterPad *) pads;
  209. int count;
  210. for(count = 0; p->name; count ++) p ++;
  211. return count;
  212. }
  213. static const char *filter_name(void *p)
  214. {
  215. AVFilterContext *filter = p;
  216. return filter->filter->name;
  217. }
  218. AVFilterContext *avfilter_create(AVFilter *filter)
  219. {
  220. AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
  221. ret->av_class = av_mallocz(sizeof(AVClass));
  222. ret->av_class->item_name = filter_name;
  223. ret->filter = filter;
  224. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
  225. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
  226. ret->priv = av_mallocz(filter->priv_size);
  227. return ret;
  228. }
  229. void avfilter_destroy(AVFilterContext *filter)
  230. {
  231. int i;
  232. if(filter->filter->uninit)
  233. filter->filter->uninit(filter);
  234. for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
  235. if(filter->inputs[i])
  236. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  237. av_free(filter->inputs[i]);
  238. }
  239. for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
  240. if(filter->outputs[i])
  241. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  242. av_free(filter->outputs[i]);
  243. }
  244. av_free(filter->inputs);
  245. av_free(filter->outputs);
  246. av_free(filter->priv);
  247. av_free(filter->av_class);
  248. av_free(filter);
  249. }
  250. AVFilterContext *avfilter_create_by_name(char *name)
  251. {
  252. AVFilter *filt;
  253. if(!(filt = avfilter_get_by_name(name))) return NULL;
  254. return avfilter_create(filt);
  255. }
  256. int avfilter_init_filter(AVFilterContext *filter, const char *args)
  257. {
  258. int ret, i;
  259. if(filter->filter->init)
  260. if((ret = filter->filter->init(filter, args))) return ret;
  261. return 0;
  262. }
  263. int *avfilter_make_format_list(int len, ...)
  264. {
  265. int *ret, i;
  266. va_list vl;
  267. ret = av_malloc(sizeof(int) * (len + 1));
  268. va_start(vl, len);
  269. for(i = 0; i < len; i ++)
  270. ret[i] = va_arg(vl, int);
  271. va_end(vl);
  272. ret[len] = -1;
  273. return ret;
  274. }