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.

362 lines
9.7KB

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