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.

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