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.

403 lines
12KB

  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. config_link = avfilter_default_config_output_link;
  132. if ((ret = config_link(link)) < 0)
  133. return ret;
  134. if (link->time_base.num == 0 && link->time_base.den == 0)
  135. link->time_base = link->src && link->src->input_count ?
  136. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  137. if (link->sample_aspect_ratio.num == 0 && link->sample_aspect_ratio.den == 0)
  138. link->sample_aspect_ratio = link->src->input_count ?
  139. link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
  140. if ((config_link = link->dstpad->config_props))
  141. if ((ret = config_link(link)) < 0)
  142. return ret;
  143. link->init_state = AVLINK_INIT;
  144. }
  145. }
  146. return 0;
  147. }
  148. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  149. {
  150. if (link->type == AVMEDIA_TYPE_VIDEO) {
  151. av_dlog(ctx,
  152. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  153. link, link->w, link->h,
  154. av_pix_fmt_descriptors[link->format].name,
  155. link->src ? link->src->filter->name : "",
  156. link->dst ? link->dst->filter->name : "",
  157. end ? "\n" : "");
  158. } else {
  159. char buf[128];
  160. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  161. av_dlog(ctx,
  162. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  163. link, link->sample_rate, buf,
  164. av_get_sample_fmt_name(link->format),
  165. link->src ? link->src->filter->name : "",
  166. link->dst ? link->dst->filter->name : "",
  167. end ? "\n" : "");
  168. }
  169. }
  170. int avfilter_request_frame(AVFilterLink *link)
  171. {
  172. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  173. if (link->srcpad->request_frame)
  174. return link->srcpad->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->srcpad->poll_frame)
  183. return link->srcpad->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. #define MAX_REGISTERED_AVFILTERS_NB 64
  194. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  195. static int next_registered_avfilter_idx = 0;
  196. AVFilter *avfilter_get_by_name(const char *name)
  197. {
  198. int i;
  199. for (i = 0; registered_avfilters[i]; i++)
  200. if (!strcmp(registered_avfilters[i]->name, name))
  201. return registered_avfilters[i];
  202. return NULL;
  203. }
  204. int avfilter_register(AVFilter *filter)
  205. {
  206. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  207. return -1;
  208. registered_avfilters[next_registered_avfilter_idx++] = filter;
  209. return 0;
  210. }
  211. AVFilter **av_filter_next(AVFilter **filter)
  212. {
  213. return filter ? ++filter : &registered_avfilters[0];
  214. }
  215. void avfilter_uninit(void)
  216. {
  217. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  218. next_registered_avfilter_idx = 0;
  219. }
  220. static int pad_count(const AVFilterPad *pads)
  221. {
  222. int count;
  223. for(count = 0; pads->name; count ++) pads ++;
  224. return count;
  225. }
  226. static const char *filter_name(void *p)
  227. {
  228. AVFilterContext *filter = p;
  229. return filter->filter->name;
  230. }
  231. static const AVClass avfilter_class = {
  232. "AVFilter",
  233. filter_name,
  234. NULL,
  235. LIBAVUTIL_VERSION_INT,
  236. };
  237. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  238. {
  239. AVFilterContext *ret;
  240. *filter_ctx = NULL;
  241. if (!filter)
  242. return AVERROR(EINVAL);
  243. ret = av_mallocz(sizeof(AVFilterContext));
  244. if (!ret)
  245. return AVERROR(ENOMEM);
  246. ret->av_class = &avfilter_class;
  247. ret->filter = filter;
  248. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  249. if (filter->priv_size) {
  250. ret->priv = av_mallocz(filter->priv_size);
  251. if (!ret->priv)
  252. goto err;
  253. }
  254. ret->input_count = pad_count(filter->inputs);
  255. if (ret->input_count) {
  256. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  257. if (!ret->input_pads)
  258. goto err;
  259. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  260. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  261. if (!ret->inputs)
  262. goto err;
  263. }
  264. ret->output_count = pad_count(filter->outputs);
  265. if (ret->output_count) {
  266. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  267. if (!ret->output_pads)
  268. goto err;
  269. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  270. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  271. if (!ret->outputs)
  272. goto err;
  273. }
  274. *filter_ctx = ret;
  275. return 0;
  276. err:
  277. av_freep(&ret->inputs);
  278. av_freep(&ret->input_pads);
  279. ret->input_count = 0;
  280. av_freep(&ret->outputs);
  281. av_freep(&ret->output_pads);
  282. ret->output_count = 0;
  283. av_freep(&ret->priv);
  284. av_free(ret);
  285. return AVERROR(ENOMEM);
  286. }
  287. void avfilter_free(AVFilterContext *filter)
  288. {
  289. int i;
  290. AVFilterLink *link;
  291. if (filter->filter->uninit)
  292. filter->filter->uninit(filter);
  293. for (i = 0; i < filter->input_count; i++) {
  294. if ((link = filter->inputs[i])) {
  295. if (link->src)
  296. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  297. avfilter_formats_unref(&link->in_formats);
  298. avfilter_formats_unref(&link->out_formats);
  299. avfilter_formats_unref(&link->in_samplerates);
  300. avfilter_formats_unref(&link->out_samplerates);
  301. ff_channel_layouts_unref(&link->in_channel_layouts);
  302. ff_channel_layouts_unref(&link->out_channel_layouts);
  303. }
  304. av_freep(&link);
  305. }
  306. for (i = 0; i < filter->output_count; i++) {
  307. if ((link = filter->outputs[i])) {
  308. if (link->dst)
  309. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  310. avfilter_formats_unref(&link->in_formats);
  311. avfilter_formats_unref(&link->out_formats);
  312. avfilter_formats_unref(&link->in_samplerates);
  313. avfilter_formats_unref(&link->out_samplerates);
  314. ff_channel_layouts_unref(&link->in_channel_layouts);
  315. ff_channel_layouts_unref(&link->out_channel_layouts);
  316. }
  317. av_freep(&link);
  318. }
  319. av_freep(&filter->name);
  320. av_freep(&filter->input_pads);
  321. av_freep(&filter->output_pads);
  322. av_freep(&filter->inputs);
  323. av_freep(&filter->outputs);
  324. av_freep(&filter->priv);
  325. av_free(filter);
  326. }
  327. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  328. {
  329. int ret=0;
  330. if (filter->filter->init)
  331. ret = filter->filter->init(filter, args, opaque);
  332. return ret;
  333. }