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.

397 lines
11KB

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