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.

383 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 */
  28. struct FilterList
  29. {
  30. AVFilter *filter;
  31. struct FilterList *next;
  32. } *filters = NULL;
  33. /** helper macros to get the in/out pad on the dst/src filter */
  34. #define link_dpad(link) link->dst-> input_pads[link->dstpad]
  35. #define link_spad(link) link->src->output_pads[link->srcpad]
  36. AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
  37. {
  38. AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
  39. *ret = *ref;
  40. ret->perms &= pmask;
  41. ret->pic->refcount ++;
  42. return ret;
  43. }
  44. void avfilter_unref_pic(AVFilterPicRef *ref)
  45. {
  46. if(-- ref->pic->refcount == 0)
  47. ref->pic->free(ref->pic);
  48. av_free(ref);
  49. }
  50. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  51. AVFilterPad **pads, AVFilterLink ***links,
  52. AVFilterPad *newpad)
  53. {
  54. unsigned i;
  55. idx = FFMIN(idx, *count);
  56. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  57. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  58. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  59. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  60. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  61. (*links)[idx] = NULL;
  62. (*count) ++;
  63. for(i = idx+1; i < *count; i ++)
  64. if(*links[i])
  65. (*(unsigned *)((uint8_t *)(*links[i]) + padidx_off)) ++;
  66. }
  67. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  68. AVFilterContext *dst, unsigned dstpad)
  69. {
  70. AVFilterLink *link;
  71. if(src->output_count <= srcpad || dst->input_count <= dstpad ||
  72. src->outputs[srcpad] || dst->inputs[dstpad])
  73. return -1;
  74. src->outputs[srcpad] =
  75. dst->inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  76. link->src = src;
  77. link->dst = dst;
  78. link->srcpad = srcpad;
  79. link->dstpad = dstpad;
  80. link->format = -1;
  81. return 0;
  82. }
  83. int avfilter_config_link(AVFilterLink *link)
  84. {
  85. int *fmts[2], i, j;
  86. int (*config_link)(AVFilterLink *);
  87. int *(*query_formats)(AVFilterLink *link);
  88. if(!link)
  89. return 0;
  90. /* find a format both filters support - TODO: auto-insert conversion filter */
  91. link->format = -1;
  92. if(!(query_formats = link_spad(link).query_formats))
  93. query_formats = avfilter_default_query_output_formats;
  94. fmts[0] = query_formats(link);
  95. fmts[1] = link_dpad(link).query_formats(link);
  96. for(i = 0; fmts[0][i] != -1; i ++)
  97. for(j = 0; fmts[1][j] != -1; j ++)
  98. if(fmts[0][i] == fmts[1][j]) {
  99. link->format = fmts[0][i];
  100. goto format_done;
  101. }
  102. format_done:
  103. av_free(fmts[0]);
  104. av_free(fmts[1]);
  105. if(link->format == -1)
  106. return -1;
  107. if(!(config_link = link_spad(link).config_props))
  108. config_link = avfilter_default_config_output_link;
  109. if(config_link(link))
  110. return -1;
  111. if(!(config_link = link_dpad(link).config_props))
  112. config_link = avfilter_default_config_input_link;
  113. if(config_link(link))
  114. return -1;
  115. return 0;
  116. }
  117. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
  118. {
  119. AVFilterPicRef *ret = NULL;
  120. if(link_dpad(link).get_video_buffer)
  121. ret = link_dpad(link).get_video_buffer(link, perms);
  122. if(!ret)
  123. ret = avfilter_default_get_video_buffer(link, perms);
  124. return ret;
  125. }
  126. int avfilter_request_frame(AVFilterLink *link)
  127. {
  128. if(link_spad(link).request_frame)
  129. return link_spad(link).request_frame(link);
  130. else if(link->src->inputs[0])
  131. return avfilter_request_frame(link->src->inputs[0]);
  132. else return -1;
  133. }
  134. /* XXX: should we do the duplicating of the picture ref here, instead of
  135. * forcing the source filter to do it? */
  136. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  137. {
  138. void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
  139. if(!(start_frame = link_dpad(link).start_frame))
  140. start_frame = avfilter_default_start_frame;
  141. /* prepare to copy the picture if it has insufficient permissions */
  142. if((link_dpad(link).min_perms & picref->perms) != link_dpad(link).min_perms ||
  143. link_dpad(link).rej_perms & picref->perms) {
  144. av_log(link->dst, AV_LOG_INFO,
  145. "frame copy needed (have perms %x, need %x, reject %x)\n",
  146. picref->perms,
  147. link_dpad(link).min_perms, link_dpad(link).rej_perms);
  148. link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
  149. link->srcpic = picref;
  150. }
  151. else
  152. link->cur_pic = picref;
  153. start_frame(link, link->cur_pic);
  154. }
  155. void avfilter_end_frame(AVFilterLink *link)
  156. {
  157. void (*end_frame)(AVFilterLink *);
  158. /* unreference the source picture if we're feeding the destination filter
  159. * a copied version dues to permission issues */
  160. if(link->srcpic) {
  161. avfilter_unref_pic(link->srcpic);
  162. link->srcpic = NULL;
  163. }
  164. if(!(end_frame = link_dpad(link).end_frame))
  165. end_frame = avfilter_default_end_frame;
  166. end_frame(link);
  167. }
  168. void avfilter_draw_slice(AVFilterLink *link, int y, int h)
  169. {
  170. uint8_t *src[4], *dst[4];
  171. int i, j, hsub, vsub;
  172. /* copy the slice if needed for permission reasons */
  173. if(link->srcpic) {
  174. avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
  175. src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
  176. dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
  177. for(i = 1; i < 4; i ++) {
  178. if(link->srcpic->data[i]) {
  179. src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
  180. dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
  181. } else
  182. src[i] = dst[i] = NULL;
  183. }
  184. for(j = 0; j < h; j ++) {
  185. memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
  186. src[0] += link->srcpic ->linesize[0];
  187. dst[0] += link->cur_pic->linesize[0];
  188. }
  189. for(i = 1; i < 4; i ++) {
  190. if(!src[i]) continue;
  191. for(j = 0; j < h >> vsub; j ++) {
  192. memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
  193. src[i] += link->srcpic ->linesize[i];
  194. dst[i] += link->cur_pic->linesize[i];
  195. }
  196. }
  197. }
  198. if(!link_dpad(link).draw_slice)
  199. return;
  200. link_dpad(link).draw_slice(link, y, h);
  201. }
  202. AVFilter *avfilter_get_by_name(char *name)
  203. {
  204. struct FilterList *filt;
  205. for(filt = filters; filt; filt = filt->next)
  206. if(!strcmp(filt->filter->name, name))
  207. return filt->filter;
  208. return NULL;
  209. }
  210. /* FIXME: insert in order, rather than insert at end + resort */
  211. void avfilter_register(AVFilter *filter)
  212. {
  213. struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
  214. newfilt->filter = filter;
  215. newfilt->next = filters;
  216. filters = newfilt;
  217. }
  218. void avfilter_init(void)
  219. {
  220. avfilter_register(&vsrc_dummy);
  221. avfilter_register(&vsrc_ppm);
  222. avfilter_register(&vf_buffer);
  223. avfilter_register(&vf_crop);
  224. avfilter_register(&vf_fps);
  225. avfilter_register(&vf_graph);
  226. avfilter_register(&vf_graphdesc);
  227. avfilter_register(&vf_graphfile);
  228. avfilter_register(&vf_overlay);
  229. avfilter_register(&vf_passthrough);
  230. avfilter_register(&vf_rgb2bgr);
  231. avfilter_register(&vf_slicify);
  232. avfilter_register(&vf_vflip);
  233. }
  234. void avfilter_uninit(void)
  235. {
  236. struct FilterList *tmp;
  237. for(; filters; filters = tmp) {
  238. tmp = filters->next;
  239. av_free(filters);
  240. }
  241. }
  242. static int pad_count(const AVFilterPad *pads)
  243. {
  244. AVFilterPad *p = (AVFilterPad *) pads;
  245. int count;
  246. for(count = 0; p->name; count ++) p ++;
  247. return count;
  248. }
  249. static const char *filter_name(void *p)
  250. {
  251. AVFilterContext *filter = p;
  252. return filter->filter->name;
  253. }
  254. AVFilterContext *avfilter_open(AVFilter *filter, char *inst_name)
  255. {
  256. AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
  257. ret->av_class = av_mallocz(sizeof(AVClass));
  258. ret->av_class->item_name = filter_name;
  259. ret->filter = filter;
  260. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  261. ret->priv = av_mallocz(filter->priv_size);
  262. ret->input_count = pad_count(filter->inputs);
  263. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  264. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
  265. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  266. ret->output_count = pad_count(filter->outputs);
  267. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  268. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
  269. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  270. return ret;
  271. }
  272. void avfilter_destroy(AVFilterContext *filter)
  273. {
  274. int i;
  275. if(filter->filter->uninit)
  276. filter->filter->uninit(filter);
  277. for(i = 0; i < filter->input_count; i ++) {
  278. if(filter->inputs[i])
  279. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  280. av_freep(&filter->inputs[i]);
  281. }
  282. for(i = 0; i < filter->output_count; i ++) {
  283. if(filter->outputs[i])
  284. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  285. av_freep(&filter->outputs[i]);
  286. }
  287. av_freep(&filter->name);
  288. av_freep(&filter->input_pads);
  289. av_freep(&filter->output_pads);
  290. av_freep(&filter->inputs);
  291. av_freep(&filter->outputs);
  292. av_freep(&filter->priv);
  293. av_freep(&filter->av_class);
  294. av_free(filter);
  295. }
  296. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  297. {
  298. int ret;
  299. if(filter->filter->init)
  300. if((ret = filter->filter->init(filter, args, opaque))) return ret;
  301. return 0;
  302. }
  303. int *avfilter_make_format_list(int len, ...)
  304. {
  305. int *ret, i;
  306. va_list vl;
  307. ret = av_malloc(sizeof(int) * (len + 1));
  308. va_start(vl, len);
  309. for(i = 0; i < len; i ++)
  310. ret[i] = va_arg(vl, int);
  311. va_end(vl);
  312. ret[len] = -1;
  313. return ret;
  314. }