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.

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