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.

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