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.

441 lines
13KB

  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. static int common_format(int *fmts0, int *fmts1)
  84. {
  85. int i, j;
  86. for(i = 0; fmts0[i] != -1; i ++)
  87. for(j = 0; fmts1[j] != -1; j ++)
  88. if(fmts0[i] == fmts1[j])
  89. return fmts0[i];
  90. return -1;
  91. }
  92. int avfilter_config_link(AVFilterLink *link)
  93. {
  94. int *fmts[3] = {NULL,NULL,NULL};
  95. int (*config_link)(AVFilterLink *);
  96. int *(*query_formats)(AVFilterLink *link);
  97. AVFilterContext *scale;
  98. AVFilterLink *link2 = NULL;
  99. if(!link)
  100. return 0;
  101. /* find a format both filters support */
  102. if(!(query_formats = link_spad(link).query_formats))
  103. query_formats = avfilter_default_query_output_formats;
  104. fmts[0] = query_formats(link);
  105. fmts[1] = link_dpad(link).query_formats(link);
  106. if((link->format = common_format(fmts[0], fmts[1])) == -1) {
  107. /* no common format found. insert scale filter to convert */
  108. if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
  109. goto format_done;
  110. if(scale->filter->init(scale, NULL, NULL)) {
  111. avfilter_destroy(scale);
  112. goto format_done;
  113. }
  114. link->dst->inputs[link->dstpad] = NULL;
  115. if(avfilter_link(scale, 0, link->dst, link->dstpad)) {
  116. link->dst->inputs[link->dstpad] = link;
  117. goto format_done;
  118. }
  119. link2 = scale->outputs[0];
  120. link->dst = scale;
  121. link->dstpad = 0;
  122. scale->inputs[0] = link;
  123. /* now try again to find working colorspaces.
  124. * XXX: is it safe to assume that the scale filter always supports the
  125. * same input and output colorspaces? */
  126. fmts[2] = scale->input_pads[0].query_formats(link);
  127. link->format = common_format(fmts[0], fmts[2]);
  128. link2->format = common_format(fmts[1], fmts[2]);
  129. }
  130. format_done:
  131. av_free(fmts[0]);
  132. av_free(fmts[1]);
  133. av_free(fmts[2]);
  134. if(link->format == -1 || (link2 && link2->format == -1)) {
  135. if(link2) {
  136. link->dst = link2->dst;
  137. link->dstpad = link2->dstpad;
  138. link->dst->inputs[link->dstpad] = link;
  139. link->format = -1;
  140. avfilter_destroy(scale);
  141. av_free(link2);
  142. }
  143. return -1;
  144. }
  145. if(!(config_link = link_spad(link).config_props))
  146. config_link = avfilter_default_config_output_link;
  147. if(config_link(link))
  148. return -1;
  149. if(!(config_link = link_dpad(link).config_props))
  150. config_link = avfilter_default_config_input_link;
  151. if(config_link(link))
  152. return -1;
  153. if(link2) {
  154. if(!(config_link = link_spad(link2).config_props))
  155. config_link = avfilter_default_config_output_link;
  156. if(config_link(link2))
  157. return -1;
  158. if(!(config_link = link_dpad(link2).config_props))
  159. config_link = avfilter_default_config_input_link;
  160. if(config_link(link2))
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
  166. {
  167. AVFilterPicRef *ret = NULL;
  168. if(link_dpad(link).get_video_buffer)
  169. ret = link_dpad(link).get_video_buffer(link, perms);
  170. if(!ret)
  171. ret = avfilter_default_get_video_buffer(link, perms);
  172. return ret;
  173. }
  174. int avfilter_request_frame(AVFilterLink *link)
  175. {
  176. if(link_spad(link).request_frame)
  177. return link_spad(link).request_frame(link);
  178. else if(link->src->inputs[0])
  179. return avfilter_request_frame(link->src->inputs[0]);
  180. else return -1;
  181. }
  182. /* XXX: should we do the duplicating of the picture ref here, instead of
  183. * forcing the source filter to do it? */
  184. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  185. {
  186. void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
  187. if(!(start_frame = link_dpad(link).start_frame))
  188. start_frame = avfilter_default_start_frame;
  189. /* prepare to copy the picture if it has insufficient permissions */
  190. if((link_dpad(link).min_perms & picref->perms) != link_dpad(link).min_perms ||
  191. link_dpad(link).rej_perms & picref->perms) {
  192. av_log(link->dst, AV_LOG_INFO,
  193. "frame copy needed (have perms %x, need %x, reject %x)\n",
  194. picref->perms,
  195. link_dpad(link).min_perms, link_dpad(link).rej_perms);
  196. link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
  197. link->srcpic = picref;
  198. }
  199. else
  200. link->cur_pic = picref;
  201. start_frame(link, link->cur_pic);
  202. }
  203. void avfilter_end_frame(AVFilterLink *link)
  204. {
  205. void (*end_frame)(AVFilterLink *);
  206. /* unreference the source picture if we're feeding the destination filter
  207. * a copied version dues to permission issues */
  208. if(link->srcpic) {
  209. avfilter_unref_pic(link->srcpic);
  210. link->srcpic = NULL;
  211. }
  212. if(!(end_frame = link_dpad(link).end_frame))
  213. end_frame = avfilter_default_end_frame;
  214. end_frame(link);
  215. }
  216. void avfilter_draw_slice(AVFilterLink *link, int y, int h)
  217. {
  218. uint8_t *src[4], *dst[4];
  219. int i, j, hsub, vsub;
  220. /* copy the slice if needed for permission reasons */
  221. if(link->srcpic) {
  222. avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
  223. src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
  224. dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
  225. for(i = 1; i < 4; i ++) {
  226. if(link->srcpic->data[i]) {
  227. src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
  228. dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
  229. } else
  230. src[i] = dst[i] = NULL;
  231. }
  232. for(j = 0; j < h; j ++) {
  233. memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
  234. src[0] += link->srcpic ->linesize[0];
  235. dst[0] += link->cur_pic->linesize[0];
  236. }
  237. for(i = 1; i < 4; i ++) {
  238. if(!src[i]) continue;
  239. for(j = 0; j < h >> vsub; j ++) {
  240. memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
  241. src[i] += link->srcpic ->linesize[i];
  242. dst[i] += link->cur_pic->linesize[i];
  243. }
  244. }
  245. }
  246. if(!link_dpad(link).draw_slice)
  247. return;
  248. link_dpad(link).draw_slice(link, y, h);
  249. }
  250. AVFilter *avfilter_get_by_name(char *name)
  251. {
  252. struct FilterList *filt;
  253. for(filt = filters; filt; filt = filt->next)
  254. if(!strcmp(filt->filter->name, name))
  255. return filt->filter;
  256. return NULL;
  257. }
  258. /* FIXME: insert in order, rather than insert at end + resort */
  259. void avfilter_register(AVFilter *filter)
  260. {
  261. struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
  262. newfilt->filter = filter;
  263. newfilt->next = filters;
  264. filters = newfilt;
  265. }
  266. void avfilter_init(void)
  267. {
  268. avfilter_register(&avfilter_vsrc_dummy);
  269. avfilter_register(&avfilter_vsrc_ppm);
  270. avfilter_register(&avfilter_vf_crop);
  271. avfilter_register(&avfilter_vf_fifo);
  272. avfilter_register(&avfilter_vf_fps);
  273. avfilter_register(&avfilter_vf_graph);
  274. avfilter_register(&avfilter_vf_graphdesc);
  275. avfilter_register(&avfilter_vf_graphfile);
  276. avfilter_register(&avfilter_vf_overlay);
  277. avfilter_register(&avfilter_vf_passthrough);
  278. avfilter_register(&avfilter_vf_rgb2bgr);
  279. avfilter_register(&avfilter_vf_scale);
  280. avfilter_register(&avfilter_vf_slicify);
  281. avfilter_register(&avfilter_vf_split);
  282. avfilter_register(&avfilter_vf_vflip);
  283. }
  284. void avfilter_uninit(void)
  285. {
  286. struct FilterList *tmp;
  287. for(; filters; filters = tmp) {
  288. tmp = filters->next;
  289. av_free(filters);
  290. }
  291. }
  292. static int pad_count(const AVFilterPad *pads)
  293. {
  294. AVFilterPad *p = (AVFilterPad *) pads;
  295. int count;
  296. for(count = 0; p->name; count ++) p ++;
  297. return count;
  298. }
  299. static const char *filter_name(void *p)
  300. {
  301. AVFilterContext *filter = p;
  302. return filter->filter->name;
  303. }
  304. AVFilterContext *avfilter_open(AVFilter *filter, char *inst_name)
  305. {
  306. AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
  307. ret->av_class = av_mallocz(sizeof(AVClass));
  308. ret->av_class->item_name = filter_name;
  309. ret->filter = filter;
  310. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  311. ret->priv = av_mallocz(filter->priv_size);
  312. ret->input_count = pad_count(filter->inputs);
  313. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  314. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
  315. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  316. ret->output_count = pad_count(filter->outputs);
  317. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  318. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
  319. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  320. return ret;
  321. }
  322. void avfilter_destroy(AVFilterContext *filter)
  323. {
  324. int i;
  325. if(filter->filter->uninit)
  326. filter->filter->uninit(filter);
  327. for(i = 0; i < filter->input_count; i ++) {
  328. if(filter->inputs[i])
  329. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  330. av_freep(&filter->inputs[i]);
  331. }
  332. for(i = 0; i < filter->output_count; i ++) {
  333. if(filter->outputs[i])
  334. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  335. av_freep(&filter->outputs[i]);
  336. }
  337. av_freep(&filter->name);
  338. av_freep(&filter->input_pads);
  339. av_freep(&filter->output_pads);
  340. av_freep(&filter->inputs);
  341. av_freep(&filter->outputs);
  342. av_freep(&filter->priv);
  343. av_freep(&filter->av_class);
  344. av_free(filter);
  345. }
  346. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  347. {
  348. int ret;
  349. if(filter->filter->init)
  350. if((ret = filter->filter->init(filter, args, opaque))) return ret;
  351. return 0;
  352. }
  353. int *avfilter_make_format_list(int len, ...)
  354. {
  355. int *ret, i;
  356. va_list vl;
  357. ret = av_malloc(sizeof(int) * (len + 1));
  358. va_start(vl, len);
  359. for(i = 0; i < len; i ++)
  360. ret[i] = va_arg(vl, int);
  361. va_end(vl);
  362. ret[len] = -1;
  363. return ret;
  364. }