avfilter.c 11 KB

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