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.

445 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/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 ff_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->nb_outputs <= srcpad || dst->nb_inputs <= 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_VERBOSE, "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. ff_formats_changeref(&link->out_formats,
  103. &filt->outputs[filt_dstpad_idx]->out_formats);
  104. if (link->out_samplerates)
  105. ff_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->nb_inputs; 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->nb_inputs != 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. av_log(link->src, AV_LOG_ERROR,
  140. "Failed to configure output pad on %s\n",
  141. link->src->name);
  142. return ret;
  143. }
  144. if (link->time_base.num == 0 && link->time_base.den == 0)
  145. link->time_base = link->src && link->src->nb_inputs ?
  146. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  147. if (link->type == AVMEDIA_TYPE_VIDEO) {
  148. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  149. link->sample_aspect_ratio = link->src->nb_inputs ?
  150. link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
  151. if (link->src->nb_inputs) {
  152. if (!link->w)
  153. link->w = link->src->inputs[0]->w;
  154. if (!link->h)
  155. link->h = link->src->inputs[0]->h;
  156. } else if (!link->w || !link->h) {
  157. av_log(link->src, AV_LOG_ERROR,
  158. "Video source filters must set their output link's "
  159. "width and height\n");
  160. return AVERROR(EINVAL);
  161. }
  162. }
  163. if ((config_link = link->dstpad->config_props))
  164. if ((ret = config_link(link)) < 0) {
  165. av_log(link->src, AV_LOG_ERROR,
  166. "Failed to configure input pad on %s\n",
  167. link->dst->name);
  168. return ret;
  169. }
  170. link->init_state = AVLINK_INIT;
  171. }
  172. }
  173. return 0;
  174. }
  175. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  176. {
  177. if (link->type == AVMEDIA_TYPE_VIDEO) {
  178. av_dlog(ctx,
  179. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  180. link, link->w, link->h,
  181. av_pix_fmt_descriptors[link->format].name,
  182. link->src ? link->src->filter->name : "",
  183. link->dst ? link->dst->filter->name : "",
  184. end ? "\n" : "");
  185. } else {
  186. char buf[128];
  187. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  188. av_dlog(ctx,
  189. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  190. link, link->sample_rate, buf,
  191. av_get_sample_fmt_name(link->format),
  192. link->src ? link->src->filter->name : "",
  193. link->dst ? link->dst->filter->name : "",
  194. end ? "\n" : "");
  195. }
  196. }
  197. int ff_request_frame(AVFilterLink *link)
  198. {
  199. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  200. if (link->srcpad->request_frame)
  201. return link->srcpad->request_frame(link);
  202. else if (link->src->inputs[0])
  203. return ff_request_frame(link->src->inputs[0]);
  204. else return -1;
  205. }
  206. int ff_poll_frame(AVFilterLink *link)
  207. {
  208. int i, min = INT_MAX;
  209. if (link->srcpad->poll_frame)
  210. return link->srcpad->poll_frame(link);
  211. for (i = 0; i < link->src->nb_inputs; i++) {
  212. int val;
  213. if (!link->src->inputs[i])
  214. return -1;
  215. val = ff_poll_frame(link->src->inputs[i]);
  216. min = FFMIN(min, val);
  217. }
  218. return min;
  219. }
  220. #define MAX_REGISTERED_AVFILTERS_NB 64
  221. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  222. static int next_registered_avfilter_idx = 0;
  223. AVFilter *avfilter_get_by_name(const char *name)
  224. {
  225. int i;
  226. for (i = 0; registered_avfilters[i]; i++)
  227. if (!strcmp(registered_avfilters[i]->name, name))
  228. return registered_avfilters[i];
  229. return NULL;
  230. }
  231. int avfilter_register(AVFilter *filter)
  232. {
  233. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  234. return -1;
  235. registered_avfilters[next_registered_avfilter_idx++] = filter;
  236. return 0;
  237. }
  238. AVFilter **av_filter_next(AVFilter **filter)
  239. {
  240. return filter ? ++filter : &registered_avfilters[0];
  241. }
  242. void avfilter_uninit(void)
  243. {
  244. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  245. next_registered_avfilter_idx = 0;
  246. }
  247. static int pad_count(const AVFilterPad *pads)
  248. {
  249. int count;
  250. for(count = 0; pads->name; count ++) pads ++;
  251. return count;
  252. }
  253. static const char *filter_name(void *p)
  254. {
  255. AVFilterContext *filter = p;
  256. return filter->filter->name;
  257. }
  258. static const AVClass avfilter_class = {
  259. "AVFilter",
  260. filter_name,
  261. NULL,
  262. LIBAVUTIL_VERSION_INT,
  263. };
  264. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  265. {
  266. AVFilterContext *ret;
  267. *filter_ctx = NULL;
  268. if (!filter)
  269. return AVERROR(EINVAL);
  270. ret = av_mallocz(sizeof(AVFilterContext));
  271. if (!ret)
  272. return AVERROR(ENOMEM);
  273. ret->av_class = &avfilter_class;
  274. ret->filter = filter;
  275. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  276. if (filter->priv_size) {
  277. ret->priv = av_mallocz(filter->priv_size);
  278. if (!ret->priv)
  279. goto err;
  280. }
  281. ret->nb_inputs = pad_count(filter->inputs);
  282. if (ret->nb_inputs ) {
  283. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  284. if (!ret->input_pads)
  285. goto err;
  286. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  287. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  288. if (!ret->inputs)
  289. goto err;
  290. }
  291. ret->nb_outputs = pad_count(filter->outputs);
  292. if (ret->nb_outputs) {
  293. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  294. if (!ret->output_pads)
  295. goto err;
  296. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  297. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  298. if (!ret->outputs)
  299. goto err;
  300. }
  301. #if FF_API_FOO_COUNT
  302. ret->output_count = ret->nb_outputs;
  303. ret->input_count = ret->nb_inputs;
  304. #endif
  305. *filter_ctx = ret;
  306. return 0;
  307. err:
  308. av_freep(&ret->inputs);
  309. av_freep(&ret->input_pads);
  310. ret->nb_inputs = 0;
  311. av_freep(&ret->outputs);
  312. av_freep(&ret->output_pads);
  313. ret->nb_outputs = 0;
  314. av_freep(&ret->priv);
  315. av_free(ret);
  316. return AVERROR(ENOMEM);
  317. }
  318. void avfilter_free(AVFilterContext *filter)
  319. {
  320. int i;
  321. AVFilterLink *link;
  322. if (filter->filter->uninit)
  323. filter->filter->uninit(filter);
  324. for (i = 0; i < filter->nb_inputs; i++) {
  325. if ((link = filter->inputs[i])) {
  326. if (link->src)
  327. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  328. ff_formats_unref(&link->in_formats);
  329. ff_formats_unref(&link->out_formats);
  330. ff_formats_unref(&link->in_samplerates);
  331. ff_formats_unref(&link->out_samplerates);
  332. ff_channel_layouts_unref(&link->in_channel_layouts);
  333. ff_channel_layouts_unref(&link->out_channel_layouts);
  334. }
  335. av_freep(&link);
  336. }
  337. for (i = 0; i < filter->nb_outputs; i++) {
  338. if ((link = filter->outputs[i])) {
  339. if (link->dst)
  340. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  341. ff_formats_unref(&link->in_formats);
  342. ff_formats_unref(&link->out_formats);
  343. ff_formats_unref(&link->in_samplerates);
  344. ff_formats_unref(&link->out_samplerates);
  345. ff_channel_layouts_unref(&link->in_channel_layouts);
  346. ff_channel_layouts_unref(&link->out_channel_layouts);
  347. }
  348. av_freep(&link);
  349. }
  350. av_freep(&filter->name);
  351. av_freep(&filter->input_pads);
  352. av_freep(&filter->output_pads);
  353. av_freep(&filter->inputs);
  354. av_freep(&filter->outputs);
  355. av_freep(&filter->priv);
  356. av_free(filter);
  357. }
  358. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  359. {
  360. int ret=0;
  361. if (filter->filter->init)
  362. ret = filter->filter->init(filter, args);
  363. return ret;
  364. }
  365. const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
  366. {
  367. return pads[pad_idx].name;
  368. }
  369. enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
  370. {
  371. return pads[pad_idx].type;
  372. }