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.

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