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.

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