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.

391 lines
10KB

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