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.

302 lines
8.2KB

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