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.

308 lines
8.1KB

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