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.

417 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. 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. #ifdef DEBUG
  149. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  150. {
  151. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  152. perms & AV_PERM_READ ? "r" : "",
  153. perms & AV_PERM_WRITE ? "w" : "",
  154. perms & AV_PERM_PRESERVE ? "p" : "",
  155. perms & AV_PERM_REUSE ? "u" : "",
  156. perms & AV_PERM_REUSE2 ? "U" : "",
  157. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  158. return buf;
  159. }
  160. #endif
  161. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  162. {
  163. if (link->type == AVMEDIA_TYPE_VIDEO) {
  164. av_dlog(ctx,
  165. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  166. link, link->w, link->h,
  167. av_pix_fmt_descriptors[link->format].name,
  168. link->src ? link->src->filter->name : "",
  169. link->dst ? link->dst->filter->name : "",
  170. end ? "\n" : "");
  171. } else {
  172. char buf[128];
  173. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  174. av_dlog(ctx,
  175. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  176. link, link->sample_rate, buf,
  177. av_get_sample_fmt_name(link->format),
  178. link->src ? link->src->filter->name : "",
  179. link->dst ? link->dst->filter->name : "",
  180. end ? "\n" : "");
  181. }
  182. }
  183. int avfilter_request_frame(AVFilterLink *link)
  184. {
  185. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  186. if (link->srcpad->request_frame)
  187. return link->srcpad->request_frame(link);
  188. else if (link->src->inputs[0])
  189. return avfilter_request_frame(link->src->inputs[0]);
  190. else return -1;
  191. }
  192. int avfilter_poll_frame(AVFilterLink *link)
  193. {
  194. int i, min = INT_MAX;
  195. if (link->srcpad->poll_frame)
  196. return link->srcpad->poll_frame(link);
  197. for (i = 0; i < link->src->input_count; i++) {
  198. int val;
  199. if (!link->src->inputs[i])
  200. return -1;
  201. val = avfilter_poll_frame(link->src->inputs[i]);
  202. min = FFMIN(min, val);
  203. }
  204. return min;
  205. }
  206. #define MAX_REGISTERED_AVFILTERS_NB 64
  207. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  208. static int next_registered_avfilter_idx = 0;
  209. AVFilter *avfilter_get_by_name(const char *name)
  210. {
  211. int i;
  212. for (i = 0; registered_avfilters[i]; i++)
  213. if (!strcmp(registered_avfilters[i]->name, name))
  214. return registered_avfilters[i];
  215. return NULL;
  216. }
  217. int avfilter_register(AVFilter *filter)
  218. {
  219. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  220. return -1;
  221. registered_avfilters[next_registered_avfilter_idx++] = filter;
  222. return 0;
  223. }
  224. AVFilter **av_filter_next(AVFilter **filter)
  225. {
  226. return filter ? ++filter : &registered_avfilters[0];
  227. }
  228. void avfilter_uninit(void)
  229. {
  230. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  231. next_registered_avfilter_idx = 0;
  232. }
  233. static int pad_count(const AVFilterPad *pads)
  234. {
  235. int count;
  236. for(count = 0; pads->name; count ++) pads ++;
  237. return count;
  238. }
  239. static const char *filter_name(void *p)
  240. {
  241. AVFilterContext *filter = p;
  242. return filter->filter->name;
  243. }
  244. static const AVClass avfilter_class = {
  245. "AVFilter",
  246. filter_name,
  247. NULL,
  248. LIBAVUTIL_VERSION_INT,
  249. };
  250. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  251. {
  252. AVFilterContext *ret;
  253. *filter_ctx = NULL;
  254. if (!filter)
  255. return AVERROR(EINVAL);
  256. ret = av_mallocz(sizeof(AVFilterContext));
  257. if (!ret)
  258. return AVERROR(ENOMEM);
  259. ret->av_class = &avfilter_class;
  260. ret->filter = filter;
  261. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  262. if (filter->priv_size) {
  263. ret->priv = av_mallocz(filter->priv_size);
  264. if (!ret->priv)
  265. goto err;
  266. }
  267. ret->input_count = pad_count(filter->inputs);
  268. if (ret->input_count) {
  269. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  270. if (!ret->input_pads)
  271. goto err;
  272. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  273. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  274. if (!ret->inputs)
  275. goto err;
  276. }
  277. ret->output_count = pad_count(filter->outputs);
  278. if (ret->output_count) {
  279. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  280. if (!ret->output_pads)
  281. goto err;
  282. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  283. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  284. if (!ret->outputs)
  285. goto err;
  286. }
  287. *filter_ctx = ret;
  288. return 0;
  289. err:
  290. av_freep(&ret->inputs);
  291. av_freep(&ret->input_pads);
  292. ret->input_count = 0;
  293. av_freep(&ret->outputs);
  294. av_freep(&ret->output_pads);
  295. ret->output_count = 0;
  296. av_freep(&ret->priv);
  297. av_free(ret);
  298. return AVERROR(ENOMEM);
  299. }
  300. void avfilter_free(AVFilterContext *filter)
  301. {
  302. int i;
  303. AVFilterLink *link;
  304. if (filter->filter->uninit)
  305. filter->filter->uninit(filter);
  306. for (i = 0; i < filter->input_count; i++) {
  307. if ((link = filter->inputs[i])) {
  308. if (link->src)
  309. link->src->outputs[link->srcpad - link->src->output_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. for (i = 0; i < filter->output_count; i++) {
  320. if ((link = filter->outputs[i])) {
  321. if (link->dst)
  322. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  323. avfilter_formats_unref(&link->in_formats);
  324. avfilter_formats_unref(&link->out_formats);
  325. avfilter_formats_unref(&link->in_samplerates);
  326. avfilter_formats_unref(&link->out_samplerates);
  327. ff_channel_layouts_unref(&link->in_channel_layouts);
  328. ff_channel_layouts_unref(&link->out_channel_layouts);
  329. }
  330. av_freep(&link);
  331. }
  332. av_freep(&filter->name);
  333. av_freep(&filter->input_pads);
  334. av_freep(&filter->output_pads);
  335. av_freep(&filter->inputs);
  336. av_freep(&filter->outputs);
  337. av_freep(&filter->priv);
  338. av_free(filter);
  339. }
  340. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  341. {
  342. int ret=0;
  343. if (filter->filter->init)
  344. ret = filter->filter->init(filter, args, opaque);
  345. return ret;
  346. }