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.3KB

  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. AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
  31. {
  32. AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
  33. memcpy(ret, ref, sizeof(AVFilterPicRef));
  34. ret->perms &= pmask;
  35. ret->pic->refcount ++;
  36. return ret;
  37. }
  38. void avfilter_unref_pic(AVFilterPicRef *ref)
  39. {
  40. if(-- ref->pic->refcount == 0)
  41. ref->pic->free(ref->pic);
  42. av_free(ref);
  43. }
  44. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  45. AVFilterPad **pads, AVFilterLink ***links,
  46. AVFilterPad *newpad)
  47. {
  48. unsigned i;
  49. idx = FFMIN(idx, *count);
  50. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  51. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  52. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  53. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  54. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  55. (*links)[idx] = NULL;
  56. (*count) ++;
  57. for(i = idx+1; i < *count; i ++)
  58. if(*links[i])
  59. (*(unsigned *)((uint8_t *)(*links[i]) + padidx_off)) ++;
  60. }
  61. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  62. AVFilterContext *dst, unsigned dstpad)
  63. {
  64. AVFilterLink *link;
  65. if(src->output_count <= srcpad || dst->input_count <= dstpad ||
  66. src->outputs[srcpad] || dst->inputs[dstpad])
  67. return -1;
  68. src->outputs[srcpad] =
  69. dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
  70. link->src = src;
  71. link->dst = dst;
  72. link->srcpad = srcpad;
  73. link->dstpad = dstpad;
  74. link->cur_pic = NULL;
  75. link->format = -1;
  76. return 0;
  77. }
  78. int avfilter_config_link(AVFilterLink *link)
  79. {
  80. int *fmts[2], i, j;
  81. int (*config_link)(AVFilterLink *);
  82. if(!link)
  83. return 0;
  84. /* find a format both filters support - TODO: auto-insert conversion filter */
  85. link->format = -1;
  86. if(link->src->output_pads[link->srcpad].query_formats)
  87. fmts[0] = link->src->output_pads[link->srcpad].query_formats(link);
  88. else
  89. fmts[0] = avfilter_default_query_output_formats(link);
  90. fmts[1] = link->dst->input_pads[link->dstpad].query_formats(link);
  91. for(i = 0; fmts[0][i] != -1; i ++)
  92. for(j = 0; fmts[1][j] != -1; j ++)
  93. if(fmts[0][i] == fmts[1][j]) {
  94. link->format = fmts[0][i];
  95. goto format_done;
  96. }
  97. format_done:
  98. av_free(fmts[0]);
  99. av_free(fmts[1]);
  100. if(link->format == -1)
  101. return -1;
  102. if(!(config_link = link->src->output_pads[link->srcpad].config_props))
  103. config_link = avfilter_default_config_output_link;
  104. if(config_link(link))
  105. return -1;
  106. if(!(config_link = link->dst->input_pads[link->dstpad].config_props))
  107. config_link = avfilter_default_config_input_link;
  108. if(config_link(link))
  109. return -1;
  110. return 0;
  111. }
  112. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
  113. {
  114. AVFilterPicRef *ret = NULL;
  115. if(link->dst->input_pads[link->dstpad].get_video_buffer)
  116. ret = link->dst->input_pads[link->dstpad].get_video_buffer(link, perms);
  117. if(!ret)
  118. ret = avfilter_default_get_video_buffer(link, perms);
  119. return ret;
  120. }
  121. int avfilter_request_frame(AVFilterLink *link)
  122. {
  123. const AVFilterPad *pad = &link->src->output_pads[link->srcpad];
  124. if(pad->request_frame)
  125. return pad->request_frame(link);
  126. else if(link->src->inputs[0])
  127. return avfilter_request_frame(link->src->inputs[0]);
  128. else return -1;
  129. }
  130. /* XXX: should we do the duplicating of the picture ref here, instead of
  131. * forcing the source filter to do it? */
  132. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  133. {
  134. void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
  135. start_frame = link->dst->input_pads[link->dstpad].start_frame;
  136. if(!start_frame)
  137. start_frame = avfilter_default_start_frame;
  138. start_frame(link, picref);
  139. }
  140. void avfilter_end_frame(AVFilterLink *link)
  141. {
  142. void (*end_frame)(AVFilterLink *);
  143. end_frame = link->dst->input_pads[link->dstpad].end_frame;
  144. if(!end_frame)
  145. end_frame = avfilter_default_end_frame;
  146. end_frame(link);
  147. }
  148. void avfilter_draw_slice(AVFilterLink *link, int y, int h)
  149. {
  150. if(!link->dst->input_pads[link->dstpad].draw_slice)
  151. return;
  152. link->dst->input_pads[link->dstpad].draw_slice(link, y, h);
  153. }
  154. static int filter_cmp(const void *aa, const void *bb)
  155. {
  156. const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
  157. return strcmp(a->name, b->name);
  158. }
  159. AVFilter *avfilter_get_by_name(char *name)
  160. {
  161. AVFilter key = { .name = name, };
  162. AVFilter *key2 = &key;
  163. AVFilter **ret;
  164. ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
  165. if(ret)
  166. return *ret;
  167. return NULL;
  168. }
  169. /* FIXME: insert in order, rather than insert at end + resort */
  170. void avfilter_register(AVFilter *filter)
  171. {
  172. filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
  173. filters[filter_count] = filter;
  174. qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
  175. }
  176. void avfilter_init(void)
  177. {
  178. avfilter_register(&vsrc_dummy);
  179. avfilter_register(&vsrc_ppm);
  180. avfilter_register(&vf_crop);
  181. avfilter_register(&vf_fps);
  182. avfilter_register(&vf_graph);
  183. avfilter_register(&vf_graphdesc);
  184. avfilter_register(&vf_graphfile);
  185. avfilter_register(&vf_overlay);
  186. avfilter_register(&vf_passthrough);
  187. avfilter_register(&vf_rgb2bgr);
  188. avfilter_register(&vf_slicify);
  189. avfilter_register(&vf_vflip);
  190. }
  191. void avfilter_uninit(void)
  192. {
  193. av_freep(&filters);
  194. filter_count = 0;
  195. }
  196. static int pad_count(const AVFilterPad *pads)
  197. {
  198. AVFilterPad *p = (AVFilterPad *) pads;
  199. int count;
  200. for(count = 0; p->name; count ++) p ++;
  201. return count;
  202. }
  203. static const char *filter_name(void *p)
  204. {
  205. AVFilterContext *filter = p;
  206. return filter->filter->name;
  207. }
  208. AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name)
  209. {
  210. AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
  211. ret->av_class = av_mallocz(sizeof(AVClass));
  212. ret->av_class->item_name = filter_name;
  213. ret->filter = filter;
  214. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  215. ret->priv = av_mallocz(filter->priv_size);
  216. ret->input_count = pad_count(filter->inputs);
  217. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  218. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
  219. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  220. ret->output_count = pad_count(filter->outputs);
  221. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  222. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
  223. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  224. return ret;
  225. }
  226. void avfilter_destroy(AVFilterContext *filter)
  227. {
  228. int i;
  229. if(filter->filter->uninit)
  230. filter->filter->uninit(filter);
  231. for(i = 0; i < filter->input_count; i ++) {
  232. if(filter->inputs[i])
  233. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  234. av_free(filter->inputs[i]);
  235. }
  236. for(i = 0; i < filter->output_count; i ++) {
  237. if(filter->outputs[i])
  238. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  239. av_free(filter->outputs[i]);
  240. }
  241. av_free(filter->name);
  242. av_free(filter->input_pads);
  243. av_free(filter->output_pads);
  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, char *inst_name)
  251. {
  252. AVFilter *filt;
  253. if(!(filt = avfilter_get_by_name(name))) return NULL;
  254. return avfilter_create(filt, inst_name);
  255. }
  256. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  257. {
  258. int ret;
  259. if(filter->filter->init)
  260. if((ret = filter->filter->init(filter, args, opaque))) 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. }