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.

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