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.

446 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 fmt:%-16s %-16s->%-16s]%s",
  148. link, link->w, link->h,
  149. avcodec_get_pix_fmt_name(link->format),
  150. link->src ? link->src->filter->name : "",
  151. link->dst ? link->dst->filter->name : "",
  152. end ? "\n" : "");
  153. }
  154. #define DPRINTF_START(ctx, func) dprintf(NULL, "%-16s: ", #func)
  155. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  156. {
  157. AVFilterPicRef *ret = NULL;
  158. DPRINTF_START(NULL, get_video_buffer); dprintf_link(NULL, link, 0); dprintf(NULL, " perms:%d w:%d h:%d\n", perms, w, h);
  159. if(link_dpad(link).get_video_buffer)
  160. ret = link_dpad(link).get_video_buffer(link, perms, w, h);
  161. if(!ret && link->dst->output_count)
  162. ret = avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  163. if(!ret)
  164. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  165. DPRINTF_START(NULL, get_video_buffer); dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); dprintf_picref(NULL, ret, 1);
  166. return ret;
  167. }
  168. int avfilter_request_frame(AVFilterLink *link)
  169. {
  170. DPRINTF_START(NULL, request_frame); dprintf_link(NULL, link, 1);
  171. if(link_spad(link).request_frame)
  172. return link_spad(link).request_frame(link);
  173. else if(link->src->inputs[0])
  174. return avfilter_request_frame(link->src->inputs[0]);
  175. else return -1;
  176. }
  177. int avfilter_poll_frame(AVFilterLink *link)
  178. {
  179. int i, min=INT_MAX;
  180. if(link_spad(link).poll_frame)
  181. return link_spad(link).poll_frame(link);
  182. for (i=0; i<link->src->input_count; i++) {
  183. int val;
  184. if(!link->src->inputs[i])
  185. return -1;
  186. val = avfilter_poll_frame(link->src->inputs[i]);
  187. min = FFMIN(min, val);
  188. }
  189. return min;
  190. }
  191. /* XXX: should we do the duplicating of the picture ref here, instead of
  192. * forcing the source filter to do it? */
  193. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  194. {
  195. void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
  196. AVFilterPad *dst = &link_dpad(link);
  197. DPRINTF_START(NULL, start_frame); dprintf_link(NULL, link, 0); dprintf(NULL, " "); dprintf_picref(NULL, picref, 1);
  198. if(!(start_frame = dst->start_frame))
  199. start_frame = avfilter_default_start_frame;
  200. /* prepare to copy the picture if it has insufficient permissions */
  201. if((dst->min_perms & picref->perms) != dst->min_perms ||
  202. dst->rej_perms & picref->perms) {
  203. /*
  204. av_log(link->dst, AV_LOG_INFO,
  205. "frame copy needed (have perms %x, need %x, reject %x)\n",
  206. picref->perms,
  207. link_dpad(link).min_perms, link_dpad(link).rej_perms);
  208. */
  209. link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms, link->w, link->h);
  210. link->srcpic = picref;
  211. link->cur_pic->pts = link->srcpic->pts;
  212. link->cur_pic->pixel_aspect = link->srcpic->pixel_aspect;
  213. }
  214. else
  215. link->cur_pic = picref;
  216. start_frame(link, link->cur_pic);
  217. }
  218. void avfilter_end_frame(AVFilterLink *link)
  219. {
  220. void (*end_frame)(AVFilterLink *);
  221. if(!(end_frame = link_dpad(link).end_frame))
  222. end_frame = avfilter_default_end_frame;
  223. end_frame(link);
  224. /* unreference the source picture if we're feeding the destination filter
  225. * a copied version dues to permission issues */
  226. if(link->srcpic) {
  227. avfilter_unref_pic(link->srcpic);
  228. link->srcpic = NULL;
  229. }
  230. }
  231. void avfilter_draw_slice(AVFilterLink *link, int y, int h)
  232. {
  233. uint8_t *src[4], *dst[4];
  234. int i, j, hsub, vsub;
  235. void (*draw_slice)(AVFilterLink *, int, int);
  236. DPRINTF_START(NULL, draw_slice); dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d\n", y, h);
  237. /* copy the slice if needed for permission reasons */
  238. if(link->srcpic) {
  239. avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
  240. for(i = 0; i < 4; i ++) {
  241. if(link->srcpic->data[i]) {
  242. src[i] = link->srcpic-> data[i] +
  243. (y >> (i==0 ? 0 : vsub)) * link->srcpic-> linesize[i];
  244. dst[i] = link->cur_pic->data[i] +
  245. (y >> (i==0 ? 0 : vsub)) * link->cur_pic->linesize[i];
  246. } else
  247. src[i] = dst[i] = NULL;
  248. }
  249. for(i = 0; i < 4; i ++) {
  250. int planew =
  251. ff_get_plane_bytewidth(link->format, link->cur_pic->w, i);
  252. if(!src[i]) continue;
  253. for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
  254. memcpy(dst[i], src[i], planew);
  255. src[i] += link->srcpic ->linesize[i];
  256. dst[i] += link->cur_pic->linesize[i];
  257. }
  258. }
  259. }
  260. if(!(draw_slice = link_dpad(link).draw_slice))
  261. draw_slice = avfilter_default_draw_slice;
  262. draw_slice(link, y, h);
  263. }
  264. AVFilter *avfilter_get_by_name(const char *name)
  265. {
  266. struct FilterList *filt;
  267. for(filt = filters; filt; filt = filt->next)
  268. if(!strcmp(filt->filter->name, name))
  269. return filt->filter;
  270. return NULL;
  271. }
  272. void avfilter_register(AVFilter *filter)
  273. {
  274. struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
  275. newfilt->filter = filter;
  276. newfilt->next = filters;
  277. filters = newfilt;
  278. }
  279. void avfilter_uninit(void)
  280. {
  281. struct FilterList *tmp;
  282. for(; filters; filters = tmp) {
  283. tmp = filters->next;
  284. av_free(filters);
  285. }
  286. }
  287. static int pad_count(const AVFilterPad *pads)
  288. {
  289. int count;
  290. for(count = 0; pads->name; count ++) pads ++;
  291. return count;
  292. }
  293. static const char *filter_name(void *p)
  294. {
  295. AVFilterContext *filter = p;
  296. return filter->filter->name;
  297. }
  298. static const AVClass avfilter_class = {
  299. "AVFilter",
  300. filter_name
  301. };
  302. AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
  303. {
  304. AVFilterContext *ret;
  305. if (!filter)
  306. return 0;
  307. ret = av_mallocz(sizeof(AVFilterContext));
  308. ret->av_class = &avfilter_class;
  309. ret->filter = filter;
  310. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  311. ret->priv = av_mallocz(filter->priv_size);
  312. ret->input_count = pad_count(filter->inputs);
  313. if (ret->input_count) {
  314. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  315. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  316. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  317. }
  318. ret->output_count = pad_count(filter->outputs);
  319. if (ret->output_count) {
  320. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  321. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  322. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  323. }
  324. return ret;
  325. }
  326. void avfilter_destroy(AVFilterContext *filter)
  327. {
  328. int i;
  329. if(filter->filter->uninit)
  330. filter->filter->uninit(filter);
  331. for(i = 0; i < filter->input_count; i ++) {
  332. if(filter->inputs[i])
  333. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  334. av_freep(&filter->inputs[i]);
  335. }
  336. for(i = 0; i < filter->output_count; i ++) {
  337. if(filter->outputs[i])
  338. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  339. av_freep(&filter->outputs[i]);
  340. }
  341. av_freep(&filter->name);
  342. av_freep(&filter->input_pads);
  343. av_freep(&filter->output_pads);
  344. av_freep(&filter->inputs);
  345. av_freep(&filter->outputs);
  346. av_freep(&filter->priv);
  347. av_free(filter);
  348. }
  349. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  350. {
  351. int ret=0;
  352. if(filter->filter->init)
  353. ret = filter->filter->init(filter, args, opaque);
  354. return ret;
  355. }