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.

355 lines
9.5KB

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