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.

449 lines
14KB

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