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.

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