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.

430 lines
13KB

  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavutil/pixdesc.h"
  23. #include "libavutil/rational.h"
  24. #include "libavutil/audioconvert.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. unsigned avfilter_version(void) {
  29. return LIBAVFILTER_VERSION_INT;
  30. }
  31. const char *avfilter_configuration(void)
  32. {
  33. return LIBAV_CONFIGURATION;
  34. }
  35. const char *avfilter_license(void)
  36. {
  37. #define LICENSE_PREFIX "libavfilter license: "
  38. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  39. }
  40. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  41. AVFilterPad **pads, AVFilterLink ***links,
  42. AVFilterPad *newpad)
  43. {
  44. unsigned i;
  45. idx = FFMIN(idx, *count);
  46. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  47. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  48. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  49. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  50. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  51. (*links)[idx] = NULL;
  52. (*count)++;
  53. for (i = idx+1; i < *count; i++)
  54. if (*links[i])
  55. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  56. }
  57. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  58. AVFilterContext *dst, unsigned dstpad)
  59. {
  60. AVFilterLink *link;
  61. if (src->output_count <= srcpad || dst->input_count <= dstpad ||
  62. src->outputs[srcpad] || dst->inputs[dstpad])
  63. return -1;
  64. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  65. av_log(src, AV_LOG_ERROR,
  66. "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
  67. src->name, srcpad, dst->name, dstpad);
  68. return AVERROR(EINVAL);
  69. }
  70. src->outputs[srcpad] =
  71. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  72. link->src = src;
  73. link->dst = dst;
  74. link->srcpad = &src->output_pads[srcpad];
  75. link->dstpad = &dst->input_pads[dstpad];
  76. link->type = src->output_pads[srcpad].type;
  77. assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  78. link->format = -1;
  79. return 0;
  80. }
  81. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  82. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  83. {
  84. int ret;
  85. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  86. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  87. "between the filter '%s' and the filter '%s'\n",
  88. filt->name, link->src->name, link->dst->name);
  89. link->dst->inputs[dstpad_idx] = NULL;
  90. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  91. /* failed to link output filter to new filter */
  92. link->dst->inputs[dstpad_idx] = link;
  93. return ret;
  94. }
  95. /* re-hookup the link to the new destination filter we inserted */
  96. link->dst = filt;
  97. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  98. filt->inputs[filt_srcpad_idx] = link;
  99. /* if any information on supported media formats already exists on the
  100. * link, we need to preserve that */
  101. if (link->out_formats)
  102. avfilter_formats_changeref(&link->out_formats,
  103. &filt->outputs[filt_dstpad_idx]->out_formats);
  104. if (link->out_samplerates)
  105. avfilter_formats_changeref(&link->out_samplerates,
  106. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  107. if (link->out_channel_layouts)
  108. ff_channel_layouts_changeref(&link->out_channel_layouts,
  109. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  110. return 0;
  111. }
  112. int avfilter_config_links(AVFilterContext *filter)
  113. {
  114. int (*config_link)(AVFilterLink *);
  115. unsigned i;
  116. int ret;
  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 ((ret = avfilter_config_links(link->src)) < 0)
  129. return ret;
  130. if (!(config_link = link->srcpad->config_props)) {
  131. if (link->src->input_count != 1) {
  132. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  133. "with more than one input "
  134. "must set config_props() "
  135. "callbacks on all outputs\n");
  136. return AVERROR(EINVAL);
  137. }
  138. } else if ((ret = config_link(link)) < 0)
  139. return ret;
  140. if (link->time_base.num == 0 && link->time_base.den == 0)
  141. link->time_base = link->src && link->src->input_count ?
  142. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  143. if (link->type == AVMEDIA_TYPE_VIDEO) {
  144. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  145. link->sample_aspect_ratio = link->src->input_count ?
  146. link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
  147. if (link->src->input_count) {
  148. if (!link->w)
  149. link->w = link->src->inputs[0]->w;
  150. if (!link->h)
  151. link->h = link->src->inputs[0]->h;
  152. } else if (!link->w || !link->h) {
  153. av_log(link->src, AV_LOG_ERROR,
  154. "Video source filters must set their output link's "
  155. "width and height\n");
  156. return AVERROR(EINVAL);
  157. }
  158. }
  159. if ((config_link = link->dstpad->config_props))
  160. if ((ret = config_link(link)) < 0)
  161. return ret;
  162. link->init_state = AVLINK_INIT;
  163. }
  164. }
  165. return 0;
  166. }
  167. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  168. {
  169. if (link->type == AVMEDIA_TYPE_VIDEO) {
  170. av_dlog(ctx,
  171. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  172. link, link->w, link->h,
  173. av_pix_fmt_descriptors[link->format].name,
  174. link->src ? link->src->filter->name : "",
  175. link->dst ? link->dst->filter->name : "",
  176. end ? "\n" : "");
  177. } else {
  178. char buf[128];
  179. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  180. av_dlog(ctx,
  181. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  182. link, link->sample_rate, buf,
  183. av_get_sample_fmt_name(link->format),
  184. link->src ? link->src->filter->name : "",
  185. link->dst ? link->dst->filter->name : "",
  186. end ? "\n" : "");
  187. }
  188. }
  189. int avfilter_request_frame(AVFilterLink *link)
  190. {
  191. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  192. if (link->srcpad->request_frame)
  193. return link->srcpad->request_frame(link);
  194. else if (link->src->inputs[0])
  195. return avfilter_request_frame(link->src->inputs[0]);
  196. else return -1;
  197. }
  198. int avfilter_poll_frame(AVFilterLink *link)
  199. {
  200. int i, min = INT_MAX;
  201. if (link->srcpad->poll_frame)
  202. return link->srcpad->poll_frame(link);
  203. for (i = 0; i < link->src->input_count; i++) {
  204. int val;
  205. if (!link->src->inputs[i])
  206. return -1;
  207. val = avfilter_poll_frame(link->src->inputs[i]);
  208. min = FFMIN(min, val);
  209. }
  210. return min;
  211. }
  212. #define MAX_REGISTERED_AVFILTERS_NB 64
  213. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  214. static int next_registered_avfilter_idx = 0;
  215. AVFilter *avfilter_get_by_name(const char *name)
  216. {
  217. int i;
  218. for (i = 0; registered_avfilters[i]; i++)
  219. if (!strcmp(registered_avfilters[i]->name, name))
  220. return registered_avfilters[i];
  221. return NULL;
  222. }
  223. int avfilter_register(AVFilter *filter)
  224. {
  225. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  226. return -1;
  227. registered_avfilters[next_registered_avfilter_idx++] = filter;
  228. return 0;
  229. }
  230. AVFilter **av_filter_next(AVFilter **filter)
  231. {
  232. return filter ? ++filter : &registered_avfilters[0];
  233. }
  234. void avfilter_uninit(void)
  235. {
  236. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  237. next_registered_avfilter_idx = 0;
  238. }
  239. static int pad_count(const AVFilterPad *pads)
  240. {
  241. int count;
  242. for(count = 0; pads->name; count ++) pads ++;
  243. return count;
  244. }
  245. static const char *filter_name(void *p)
  246. {
  247. AVFilterContext *filter = p;
  248. return filter->filter->name;
  249. }
  250. static const AVClass avfilter_class = {
  251. "AVFilter",
  252. filter_name,
  253. NULL,
  254. LIBAVUTIL_VERSION_INT,
  255. };
  256. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  257. {
  258. AVFilterContext *ret;
  259. *filter_ctx = NULL;
  260. if (!filter)
  261. return AVERROR(EINVAL);
  262. ret = av_mallocz(sizeof(AVFilterContext));
  263. if (!ret)
  264. return AVERROR(ENOMEM);
  265. ret->av_class = &avfilter_class;
  266. ret->filter = filter;
  267. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  268. if (filter->priv_size) {
  269. ret->priv = av_mallocz(filter->priv_size);
  270. if (!ret->priv)
  271. goto err;
  272. }
  273. ret->input_count = pad_count(filter->inputs);
  274. if (ret->input_count) {
  275. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  276. if (!ret->input_pads)
  277. goto err;
  278. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  279. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  280. if (!ret->inputs)
  281. goto err;
  282. }
  283. ret->output_count = pad_count(filter->outputs);
  284. if (ret->output_count) {
  285. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  286. if (!ret->output_pads)
  287. goto err;
  288. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  289. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  290. if (!ret->outputs)
  291. goto err;
  292. }
  293. *filter_ctx = ret;
  294. return 0;
  295. err:
  296. av_freep(&ret->inputs);
  297. av_freep(&ret->input_pads);
  298. ret->input_count = 0;
  299. av_freep(&ret->outputs);
  300. av_freep(&ret->output_pads);
  301. ret->output_count = 0;
  302. av_freep(&ret->priv);
  303. av_free(ret);
  304. return AVERROR(ENOMEM);
  305. }
  306. void avfilter_free(AVFilterContext *filter)
  307. {
  308. int i;
  309. AVFilterLink *link;
  310. if (filter->filter->uninit)
  311. filter->filter->uninit(filter);
  312. for (i = 0; i < filter->input_count; i++) {
  313. if ((link = filter->inputs[i])) {
  314. if (link->src)
  315. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  316. avfilter_formats_unref(&link->in_formats);
  317. avfilter_formats_unref(&link->out_formats);
  318. avfilter_formats_unref(&link->in_samplerates);
  319. avfilter_formats_unref(&link->out_samplerates);
  320. ff_channel_layouts_unref(&link->in_channel_layouts);
  321. ff_channel_layouts_unref(&link->out_channel_layouts);
  322. }
  323. av_freep(&link);
  324. }
  325. for (i = 0; i < filter->output_count; i++) {
  326. if ((link = filter->outputs[i])) {
  327. if (link->dst)
  328. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  329. avfilter_formats_unref(&link->in_formats);
  330. avfilter_formats_unref(&link->out_formats);
  331. avfilter_formats_unref(&link->in_samplerates);
  332. avfilter_formats_unref(&link->out_samplerates);
  333. ff_channel_layouts_unref(&link->in_channel_layouts);
  334. ff_channel_layouts_unref(&link->out_channel_layouts);
  335. }
  336. av_freep(&link);
  337. }
  338. av_freep(&filter->name);
  339. av_freep(&filter->input_pads);
  340. av_freep(&filter->output_pads);
  341. av_freep(&filter->inputs);
  342. av_freep(&filter->outputs);
  343. av_freep(&filter->priv);
  344. av_free(filter);
  345. }
  346. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  347. {
  348. int ret=0;
  349. if (filter->filter->init)
  350. ret = filter->filter->init(filter, args, opaque);
  351. return ret;
  352. }
  353. #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
  354. int avfilter_default_config_output_link(AVFilterLink *link)
  355. {
  356. return 0;
  357. }
  358. #endif