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.

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