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.

425 lines
12KB

  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_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  84. unsigned in, unsigned out)
  85. {
  86. AVFilterFormats *formats;
  87. av_log(NULL, AV_LOG_INFO, "auto-inserting filter '%s'\n",
  88. filt->filter->name);
  89. link->dst->inputs[link->dstpad] = NULL;
  90. if(avfilter_link(filt, out, link->dst, link->dstpad)) {
  91. /* failed to link output filter to new filter */
  92. link->dst->inputs[link->dstpad] = link;
  93. return -1;
  94. }
  95. /* re-hookup the link to the new destination filter we inserted */
  96. link->dst = filt;
  97. link->dstpad = in;
  98. filt->inputs[in] = link;
  99. /* if any information on supported colorspaces already exists on the
  100. * link, we need to preserve that */
  101. if((formats = link->out_formats))
  102. avfilter_formats_changeref(&link->out_formats,
  103. &filt->outputs[out]->out_formats);
  104. return 0;
  105. }
  106. int avfilter_config_links(AVFilterContext *filter)
  107. {
  108. int (*config_link)(AVFilterLink *);
  109. unsigned i;
  110. for(i = 0; i < filter->input_count; i ++) {
  111. AVFilterLink *link;
  112. if(!(link = filter->inputs[i])) continue;
  113. switch(link->init_state) {
  114. case AVLINK_INIT:
  115. continue;
  116. case AVLINK_STARTINIT:
  117. av_log(filter, AV_LOG_ERROR, "circular filter chain detected\n");
  118. return -1;
  119. case AVLINK_UNINIT:
  120. link->init_state = AVLINK_STARTINIT;
  121. if(avfilter_config_links(link->src))
  122. return -1;
  123. if(!(config_link = link_spad(link).config_props))
  124. config_link = avfilter_default_config_output_link;
  125. if(config_link(link))
  126. return -1;
  127. if((config_link = link_dpad(link).config_props))
  128. if(config_link(link))
  129. return -1;
  130. link->init_state = AVLINK_INIT;
  131. }
  132. }
  133. return 0;
  134. }
  135. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
  136. {
  137. AVFilterPicRef *ret = NULL;
  138. if(link_dpad(link).get_video_buffer)
  139. ret = link_dpad(link).get_video_buffer(link, perms);
  140. if(!ret)
  141. ret = avfilter_default_get_video_buffer(link, perms);
  142. return ret;
  143. }
  144. int avfilter_request_frame(AVFilterLink *link)
  145. {
  146. if(link_spad(link).request_frame)
  147. return link_spad(link).request_frame(link);
  148. else if(link->src->inputs[0])
  149. return avfilter_request_frame(link->src->inputs[0]);
  150. else return -1;
  151. }
  152. int avfilter_poll_frame(AVFilterLink *link)
  153. {
  154. int i, min=INT_MAX;
  155. if(link_spad(link).poll_frame)
  156. return link_spad(link).poll_frame(link);
  157. else
  158. for (i=0; i<link->src->input_count; i++) {
  159. if(!link->src->inputs[i])
  160. return -1;
  161. min = FFMIN(min, avfilter_poll_frame(link->src->inputs[i]));
  162. }
  163. return min;
  164. }
  165. /* XXX: should we do the duplicating of the picture ref here, instead of
  166. * forcing the source filter to do it? */
  167. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  168. {
  169. void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
  170. if(!(start_frame = link_dpad(link).start_frame))
  171. start_frame = avfilter_default_start_frame;
  172. /* prepare to copy the picture if it has insufficient permissions */
  173. if((link_dpad(link).min_perms & picref->perms) != link_dpad(link).min_perms ||
  174. link_dpad(link).rej_perms & picref->perms) {
  175. /*
  176. av_log(link->dst, AV_LOG_INFO,
  177. "frame copy needed (have perms %x, need %x, reject %x)\n",
  178. picref->perms,
  179. link_dpad(link).min_perms, link_dpad(link).rej_perms);
  180. */
  181. link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
  182. link->srcpic = picref;
  183. }
  184. else
  185. link->cur_pic = picref;
  186. start_frame(link, link->cur_pic);
  187. }
  188. void avfilter_end_frame(AVFilterLink *link)
  189. {
  190. void (*end_frame)(AVFilterLink *);
  191. if(!(end_frame = link_dpad(link).end_frame))
  192. end_frame = avfilter_default_end_frame;
  193. end_frame(link);
  194. /* unreference the source picture if we're feeding the destination filter
  195. * a copied version dues to permission issues */
  196. if(link->srcpic) {
  197. avfilter_unref_pic(link->srcpic);
  198. link->srcpic = NULL;
  199. }
  200. }
  201. void avfilter_draw_slice(AVFilterLink *link, int y, int h)
  202. {
  203. uint8_t *src[4], *dst[4];
  204. int i, j, hsub, vsub;
  205. /* copy the slice if needed for permission reasons */
  206. if(link->srcpic) {
  207. avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
  208. link->cur_pic->pts = link->srcpic->pts;
  209. src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
  210. dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
  211. for(i = 1; i < 4; i ++) {
  212. if(link->srcpic->data[i]) {
  213. src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
  214. dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
  215. } else
  216. src[i] = dst[i] = NULL;
  217. }
  218. for(j = 0; j < h; j ++) {
  219. memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
  220. src[0] += link->srcpic ->linesize[0];
  221. dst[0] += link->cur_pic->linesize[0];
  222. }
  223. for(i = 1; i < 4; i ++) {
  224. if(!src[i]) continue;
  225. for(j = 0; j < h >> vsub; j ++) {
  226. memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
  227. src[i] += link->srcpic ->linesize[i];
  228. dst[i] += link->cur_pic->linesize[i];
  229. }
  230. }
  231. }
  232. if(!link_dpad(link).draw_slice)
  233. return;
  234. link_dpad(link).draw_slice(link, y, h);
  235. }
  236. AVFilter *avfilter_get_by_name(const char *name)
  237. {
  238. struct FilterList *filt;
  239. for(filt = filters; filt; filt = filt->next)
  240. if(!strcmp(filt->filter->name, name))
  241. return filt->filter;
  242. return NULL;
  243. }
  244. void avfilter_register(AVFilter *filter)
  245. {
  246. struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
  247. newfilt->filter = filter;
  248. newfilt->next = filters;
  249. filters = newfilt;
  250. }
  251. void avfilter_init(void)
  252. {
  253. avfilter_register(&avfilter_vf_crop);
  254. avfilter_register(&avfilter_vf_fifo);
  255. avfilter_register(&avfilter_vf_format);
  256. avfilter_register(&avfilter_vf_fps);
  257. avfilter_register(&avfilter_vf_graph);
  258. avfilter_register(&avfilter_vf_graphdesc);
  259. avfilter_register(&avfilter_vf_graphfile);
  260. avfilter_register(&avfilter_vf_hflip);
  261. avfilter_register(&avfilter_vf_negate);
  262. avfilter_register(&avfilter_vf_noformat);
  263. avfilter_register(&avfilter_vf_overlay);
  264. avfilter_register(&avfilter_vf_rotate);
  265. avfilter_register(&avfilter_vf_scale);
  266. avfilter_register(&avfilter_vf_setpts);
  267. avfilter_register(&avfilter_vf_slicify);
  268. avfilter_register(&avfilter_vf_split);
  269. avfilter_register(&avfilter_vf_transpose);
  270. avfilter_register(&avfilter_vf_vflip);
  271. #ifdef CONFIG_AVFILTER_LAVF
  272. avfilter_register(&avfilter_vsrc_movie);
  273. #endif //CONFIG_AVFILTER_LAVF
  274. }
  275. void avfilter_uninit(void)
  276. {
  277. struct FilterList *tmp;
  278. for(; filters; filters = tmp) {
  279. tmp = filters->next;
  280. av_free(filters);
  281. }
  282. }
  283. static int pad_count(const AVFilterPad *pads)
  284. {
  285. int count;
  286. for(count = 0; pads->name; count ++) pads ++;
  287. return count;
  288. }
  289. static const char *filter_name(void *p)
  290. {
  291. AVFilterContext *filter = p;
  292. return filter->filter->name;
  293. }
  294. AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
  295. {
  296. AVFilterContext *ret;
  297. if (!filter)
  298. return 0;
  299. ret = av_malloc(sizeof(AVFilterContext));
  300. ret->av_class = av_mallocz(sizeof(AVClass));
  301. ret->av_class->item_name = filter_name;
  302. ret->filter = filter;
  303. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  304. ret->priv = av_mallocz(filter->priv_size);
  305. ret->input_count = pad_count(filter->inputs);
  306. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  307. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
  308. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  309. ret->output_count = pad_count(filter->outputs);
  310. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  311. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
  312. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  313. return ret;
  314. }
  315. void avfilter_destroy(AVFilterContext *filter)
  316. {
  317. int i;
  318. if(filter->filter->uninit)
  319. filter->filter->uninit(filter);
  320. for(i = 0; i < filter->input_count; i ++) {
  321. if(filter->inputs[i])
  322. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  323. av_freep(&filter->inputs[i]);
  324. }
  325. for(i = 0; i < filter->output_count; i ++) {
  326. if(filter->outputs[i])
  327. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  328. av_freep(&filter->outputs[i]);
  329. }
  330. av_freep(&filter->name);
  331. av_freep(&filter->input_pads);
  332. av_freep(&filter->output_pads);
  333. av_freep(&filter->inputs);
  334. av_freep(&filter->outputs);
  335. av_freep(&filter->priv);
  336. av_freep(&filter->av_class);
  337. av_free(filter);
  338. }
  339. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  340. {
  341. int ret;
  342. if(filter->filter->init)
  343. if((ret = filter->filter->init(filter, args, opaque))) return ret;
  344. return 0;
  345. }