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.

518 lines
16KB

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