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.

1133 lines
35KB

  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/atomic.h"
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/rational.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "audio.h"
  34. #include "avfilter.h"
  35. #include "formats.h"
  36. #include "internal.h"
  37. #include "audio.h"
  38. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame);
  39. void ff_tlog_ref(void *ctx, AVFrame *ref, int end)
  40. {
  41. av_unused char buf[16];
  42. ff_tlog(ctx,
  43. "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  44. ref, ref->buf, ref->data[0],
  45. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  46. ref->pts, av_frame_get_pkt_pos(ref));
  47. if (ref->width) {
  48. ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  49. ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
  50. ref->width, ref->height,
  51. !ref->interlaced_frame ? 'P' : /* Progressive */
  52. ref->top_field_first ? 'T' : 'B', /* Top / Bottom */
  53. ref->key_frame,
  54. av_get_picture_type_char(ref->pict_type));
  55. }
  56. if (ref->nb_samples) {
  57. ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  58. ref->channel_layout,
  59. ref->nb_samples,
  60. ref->sample_rate);
  61. }
  62. ff_tlog(ctx, "]%s", end ? "\n" : "");
  63. }
  64. unsigned avfilter_version(void)
  65. {
  66. av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
  67. return LIBAVFILTER_VERSION_INT;
  68. }
  69. const char *avfilter_configuration(void)
  70. {
  71. return FFMPEG_CONFIGURATION;
  72. }
  73. const char *avfilter_license(void)
  74. {
  75. #define LICENSE_PREFIX "libavfilter license: "
  76. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  77. }
  78. void ff_command_queue_pop(AVFilterContext *filter)
  79. {
  80. AVFilterCommand *c= filter->command_queue;
  81. av_freep(&c->arg);
  82. av_freep(&c->command);
  83. filter->command_queue= c->next;
  84. av_free(c);
  85. }
  86. void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  87. AVFilterPad **pads, AVFilterLink ***links,
  88. AVFilterPad *newpad)
  89. {
  90. unsigned i;
  91. idx = FFMIN(idx, *count);
  92. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  93. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  94. memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
  95. memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
  96. memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
  97. (*links)[idx] = NULL;
  98. (*count)++;
  99. for (i = idx + 1; i < *count; i++)
  100. if (*links[i])
  101. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  102. }
  103. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  104. AVFilterContext *dst, unsigned dstpad)
  105. {
  106. AVFilterLink *link;
  107. if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
  108. src->outputs[srcpad] || dst->inputs[dstpad])
  109. return -1;
  110. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  111. av_log(src, AV_LOG_ERROR,
  112. "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
  113. src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
  114. dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
  115. return AVERROR(EINVAL);
  116. }
  117. link = av_mallocz(sizeof(*link));
  118. if (!link)
  119. return AVERROR(ENOMEM);
  120. src->outputs[srcpad] = dst->inputs[dstpad] = link;
  121. link->src = src;
  122. link->dst = dst;
  123. link->srcpad = &src->output_pads[srcpad];
  124. link->dstpad = &dst->input_pads[dstpad];
  125. link->type = src->output_pads[srcpad].type;
  126. av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  127. link->format = -1;
  128. return 0;
  129. }
  130. void avfilter_link_free(AVFilterLink **link)
  131. {
  132. if (!*link)
  133. return;
  134. av_frame_free(&(*link)->partial_buf);
  135. av_freep(link);
  136. }
  137. int avfilter_link_get_channels(AVFilterLink *link)
  138. {
  139. return link->channels;
  140. }
  141. void avfilter_link_set_closed(AVFilterLink *link, int closed)
  142. {
  143. link->closed = closed;
  144. }
  145. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  146. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  147. {
  148. int ret;
  149. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  150. av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
  151. "between the filter '%s' and the filter '%s'\n",
  152. filt->name, link->src->name, link->dst->name);
  153. link->dst->inputs[dstpad_idx] = NULL;
  154. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  155. /* failed to link output filter to new filter */
  156. link->dst->inputs[dstpad_idx] = link;
  157. return ret;
  158. }
  159. /* re-hookup the link to the new destination filter we inserted */
  160. link->dst = filt;
  161. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  162. filt->inputs[filt_srcpad_idx] = link;
  163. /* if any information on supported media formats already exists on the
  164. * link, we need to preserve that */
  165. if (link->out_formats)
  166. ff_formats_changeref(&link->out_formats,
  167. &filt->outputs[filt_dstpad_idx]->out_formats);
  168. if (link->out_samplerates)
  169. ff_formats_changeref(&link->out_samplerates,
  170. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  171. if (link->out_channel_layouts)
  172. ff_channel_layouts_changeref(&link->out_channel_layouts,
  173. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  174. return 0;
  175. }
  176. int avfilter_config_links(AVFilterContext *filter)
  177. {
  178. int (*config_link)(AVFilterLink *);
  179. unsigned i;
  180. int ret;
  181. for (i = 0; i < filter->nb_inputs; i ++) {
  182. AVFilterLink *link = filter->inputs[i];
  183. AVFilterLink *inlink;
  184. if (!link) continue;
  185. inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
  186. link->current_pts = AV_NOPTS_VALUE;
  187. switch (link->init_state) {
  188. case AVLINK_INIT:
  189. continue;
  190. case AVLINK_STARTINIT:
  191. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  192. return 0;
  193. case AVLINK_UNINIT:
  194. link->init_state = AVLINK_STARTINIT;
  195. if ((ret = avfilter_config_links(link->src)) < 0)
  196. return ret;
  197. if (!(config_link = link->srcpad->config_props)) {
  198. if (link->src->nb_inputs != 1) {
  199. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  200. "with more than one input "
  201. "must set config_props() "
  202. "callbacks on all outputs\n");
  203. return AVERROR(EINVAL);
  204. }
  205. } else if ((ret = config_link(link)) < 0) {
  206. av_log(link->src, AV_LOG_ERROR,
  207. "Failed to configure output pad on %s\n",
  208. link->src->name);
  209. return ret;
  210. }
  211. switch (link->type) {
  212. case AVMEDIA_TYPE_VIDEO:
  213. if (!link->time_base.num && !link->time_base.den)
  214. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  215. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  216. link->sample_aspect_ratio = inlink ?
  217. inlink->sample_aspect_ratio : (AVRational){1,1};
  218. if (inlink && !link->frame_rate.num && !link->frame_rate.den)
  219. link->frame_rate = inlink->frame_rate;
  220. if (inlink) {
  221. if (!link->w)
  222. link->w = inlink->w;
  223. if (!link->h)
  224. link->h = inlink->h;
  225. } else if (!link->w || !link->h) {
  226. av_log(link->src, AV_LOG_ERROR,
  227. "Video source filters must set their output link's "
  228. "width and height\n");
  229. return AVERROR(EINVAL);
  230. }
  231. break;
  232. case AVMEDIA_TYPE_AUDIO:
  233. if (inlink) {
  234. if (!link->time_base.num && !link->time_base.den)
  235. link->time_base = inlink->time_base;
  236. }
  237. if (!link->time_base.num && !link->time_base.den)
  238. link->time_base = (AVRational) {1, link->sample_rate};
  239. }
  240. if ((config_link = link->dstpad->config_props))
  241. if ((ret = config_link(link)) < 0) {
  242. av_log(link->src, AV_LOG_ERROR,
  243. "Failed to configure input pad on %s\n",
  244. link->dst->name);
  245. return ret;
  246. }
  247. link->init_state = AVLINK_INIT;
  248. }
  249. }
  250. return 0;
  251. }
  252. void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
  253. {
  254. if (link->type == AVMEDIA_TYPE_VIDEO) {
  255. ff_tlog(ctx,
  256. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  257. link, link->w, link->h,
  258. av_get_pix_fmt_name(link->format),
  259. link->src ? link->src->filter->name : "",
  260. link->dst ? link->dst->filter->name : "",
  261. end ? "\n" : "");
  262. } else {
  263. char buf[128];
  264. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  265. ff_tlog(ctx,
  266. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  267. link, (int)link->sample_rate, buf,
  268. av_get_sample_fmt_name(link->format),
  269. link->src ? link->src->filter->name : "",
  270. link->dst ? link->dst->filter->name : "",
  271. end ? "\n" : "");
  272. }
  273. }
  274. int ff_request_frame(AVFilterLink *link)
  275. {
  276. int ret = -1;
  277. FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
  278. if (link->closed)
  279. return AVERROR_EOF;
  280. av_assert0(!link->frame_requested);
  281. link->frame_requested = 1;
  282. while (link->frame_requested) {
  283. if (link->srcpad->request_frame)
  284. ret = link->srcpad->request_frame(link);
  285. else if (link->src->inputs[0])
  286. ret = ff_request_frame(link->src->inputs[0]);
  287. if (ret == AVERROR_EOF && link->partial_buf) {
  288. AVFrame *pbuf = link->partial_buf;
  289. link->partial_buf = NULL;
  290. ret = ff_filter_frame_framed(link, pbuf);
  291. }
  292. if (ret < 0) {
  293. link->frame_requested = 0;
  294. if (ret == AVERROR_EOF)
  295. link->closed = 1;
  296. } else {
  297. av_assert0(!link->frame_requested ||
  298. link->flags & FF_LINK_FLAG_REQUEST_LOOP);
  299. }
  300. }
  301. return ret;
  302. }
  303. int ff_poll_frame(AVFilterLink *link)
  304. {
  305. int i, min = INT_MAX;
  306. if (link->srcpad->poll_frame)
  307. return link->srcpad->poll_frame(link);
  308. for (i = 0; i < link->src->nb_inputs; i++) {
  309. int val;
  310. if (!link->src->inputs[i])
  311. return -1;
  312. val = ff_poll_frame(link->src->inputs[i]);
  313. min = FFMIN(min, val);
  314. }
  315. return min;
  316. }
  317. static const char *const var_names[] = { "t", "n", "pos", NULL };
  318. enum { VAR_T, VAR_N, VAR_POS, VAR_VARS_NB };
  319. static int set_enable_expr(AVFilterContext *ctx, const char *expr)
  320. {
  321. int ret;
  322. char *expr_dup;
  323. AVExpr *old = ctx->enable;
  324. if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
  325. av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
  326. "with filter '%s'\n", ctx->filter->name);
  327. return AVERROR_PATCHWELCOME;
  328. }
  329. expr_dup = av_strdup(expr);
  330. if (!expr_dup)
  331. return AVERROR(ENOMEM);
  332. if (!ctx->var_values) {
  333. ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
  334. if (!ctx->var_values) {
  335. av_free(expr_dup);
  336. return AVERROR(ENOMEM);
  337. }
  338. }
  339. ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
  340. NULL, NULL, NULL, NULL, 0, ctx->priv);
  341. if (ret < 0) {
  342. av_log(ctx->priv, AV_LOG_ERROR,
  343. "Error when evaluating the expression '%s' for enable\n",
  344. expr_dup);
  345. av_free(expr_dup);
  346. return ret;
  347. }
  348. av_expr_free(old);
  349. av_free(ctx->enable_str);
  350. ctx->enable_str = expr_dup;
  351. return 0;
  352. }
  353. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  354. {
  355. if (pts == AV_NOPTS_VALUE)
  356. return;
  357. link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  358. /* TODO use duration */
  359. if (link->graph && link->age_index >= 0)
  360. ff_avfilter_graph_update_heap(link->graph, link);
  361. }
  362. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  363. {
  364. if(!strcmp(cmd, "ping")){
  365. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  366. return 0;
  367. }else if(!strcmp(cmd, "enable")) {
  368. return set_enable_expr(filter, arg);
  369. }else if(filter->filter->process_command) {
  370. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  371. }
  372. return AVERROR(ENOSYS);
  373. }
  374. static AVFilter *first_filter;
  375. AVFilter *avfilter_get_by_name(const char *name)
  376. {
  377. const AVFilter *f = NULL;
  378. if (!name)
  379. return NULL;
  380. while ((f = avfilter_next(f)))
  381. if (!strcmp(f->name, name))
  382. return (AVFilter *)f;
  383. return NULL;
  384. }
  385. int avfilter_register(AVFilter *filter)
  386. {
  387. AVFilter **f = &first_filter;
  388. int i;
  389. /* the filter must select generic or internal exclusively */
  390. av_assert0((filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE) != AVFILTER_FLAG_SUPPORT_TIMELINE);
  391. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  392. const AVFilterPad *input = &filter->inputs[i];
  393. av_assert0( !input->filter_frame
  394. || (!input->start_frame && !input->end_frame));
  395. }
  396. filter->next = NULL;
  397. while(avpriv_atomic_ptr_cas((void * volatile *)f, NULL, filter))
  398. f = &(*f)->next;
  399. return 0;
  400. }
  401. const AVFilter *avfilter_next(const AVFilter *prev)
  402. {
  403. return prev ? prev->next : first_filter;
  404. }
  405. #if FF_API_OLD_FILTER_REGISTER
  406. AVFilter **av_filter_next(AVFilter **filter)
  407. {
  408. return filter ? &(*filter)->next : &first_filter;
  409. }
  410. void avfilter_uninit(void)
  411. {
  412. }
  413. #endif
  414. int avfilter_pad_count(const AVFilterPad *pads)
  415. {
  416. int count;
  417. if (!pads)
  418. return 0;
  419. for (count = 0; pads->name; count++)
  420. pads++;
  421. return count;
  422. }
  423. static const char *default_filter_name(void *filter_ctx)
  424. {
  425. AVFilterContext *ctx = filter_ctx;
  426. return ctx->name ? ctx->name : ctx->filter->name;
  427. }
  428. static void *filter_child_next(void *obj, void *prev)
  429. {
  430. AVFilterContext *ctx = obj;
  431. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  432. return ctx->priv;
  433. return NULL;
  434. }
  435. static const AVClass *filter_child_class_next(const AVClass *prev)
  436. {
  437. const AVFilter *f = NULL;
  438. /* find the filter that corresponds to prev */
  439. while (prev && (f = avfilter_next(f)))
  440. if (f->priv_class == prev)
  441. break;
  442. /* could not find filter corresponding to prev */
  443. if (prev && !f)
  444. return NULL;
  445. /* find next filter with specific options */
  446. while ((f = avfilter_next(f)))
  447. if (f->priv_class)
  448. return f->priv_class;
  449. return NULL;
  450. }
  451. #define OFFSET(x) offsetof(AVFilterContext, x)
  452. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
  453. static const AVOption avfilter_options[] = {
  454. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  455. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  456. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
  457. { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  458. { NULL },
  459. };
  460. static const AVClass avfilter_class = {
  461. .class_name = "AVFilter",
  462. .item_name = default_filter_name,
  463. .version = LIBAVUTIL_VERSION_INT,
  464. .category = AV_CLASS_CATEGORY_FILTER,
  465. .child_next = filter_child_next,
  466. .child_class_next = filter_child_class_next,
  467. .option = avfilter_options,
  468. };
  469. static int default_execute(AVFilterContext *ctx, action_func *func, void *arg,
  470. int *ret, int nb_jobs)
  471. {
  472. int i;
  473. for (i = 0; i < nb_jobs; i++) {
  474. int r = func(ctx, arg, i, nb_jobs);
  475. if (ret)
  476. ret[i] = r;
  477. }
  478. return 0;
  479. }
  480. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  481. {
  482. AVFilterContext *ret;
  483. if (!filter)
  484. return NULL;
  485. ret = av_mallocz(sizeof(AVFilterContext));
  486. if (!ret)
  487. return NULL;
  488. ret->av_class = &avfilter_class;
  489. ret->filter = filter;
  490. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  491. if (filter->priv_size) {
  492. ret->priv = av_mallocz(filter->priv_size);
  493. if (!ret->priv)
  494. goto err;
  495. }
  496. av_opt_set_defaults(ret);
  497. if (filter->priv_class) {
  498. *(const AVClass**)ret->priv = filter->priv_class;
  499. av_opt_set_defaults(ret->priv);
  500. }
  501. ret->internal = av_mallocz(sizeof(*ret->internal));
  502. if (!ret->internal)
  503. goto err;
  504. ret->internal->execute = default_execute;
  505. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  506. if (ret->nb_inputs ) {
  507. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  508. if (!ret->input_pads)
  509. goto err;
  510. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  511. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  512. if (!ret->inputs)
  513. goto err;
  514. }
  515. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  516. if (ret->nb_outputs) {
  517. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  518. if (!ret->output_pads)
  519. goto err;
  520. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  521. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  522. if (!ret->outputs)
  523. goto err;
  524. }
  525. #if FF_API_FOO_COUNT
  526. FF_DISABLE_DEPRECATION_WARNINGS
  527. ret->output_count = ret->nb_outputs;
  528. ret->input_count = ret->nb_inputs;
  529. FF_ENABLE_DEPRECATION_WARNINGS
  530. #endif
  531. return ret;
  532. err:
  533. av_freep(&ret->inputs);
  534. av_freep(&ret->input_pads);
  535. ret->nb_inputs = 0;
  536. av_freep(&ret->outputs);
  537. av_freep(&ret->output_pads);
  538. ret->nb_outputs = 0;
  539. av_freep(&ret->priv);
  540. av_freep(&ret->internal);
  541. av_free(ret);
  542. return NULL;
  543. }
  544. #if FF_API_AVFILTER_OPEN
  545. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  546. {
  547. *filter_ctx = ff_filter_alloc(filter, inst_name);
  548. return *filter_ctx ? 0 : AVERROR(ENOMEM);
  549. }
  550. #endif
  551. static void free_link(AVFilterLink *link)
  552. {
  553. if (!link)
  554. return;
  555. if (link->src)
  556. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  557. if (link->dst)
  558. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  559. ff_formats_unref(&link->in_formats);
  560. ff_formats_unref(&link->out_formats);
  561. ff_formats_unref(&link->in_samplerates);
  562. ff_formats_unref(&link->out_samplerates);
  563. ff_channel_layouts_unref(&link->in_channel_layouts);
  564. ff_channel_layouts_unref(&link->out_channel_layouts);
  565. avfilter_link_free(&link);
  566. }
  567. void avfilter_free(AVFilterContext *filter)
  568. {
  569. int i;
  570. if (!filter)
  571. return;
  572. if (filter->graph)
  573. ff_filter_graph_remove_filter(filter->graph, filter);
  574. if (filter->filter->uninit)
  575. filter->filter->uninit(filter);
  576. for (i = 0; i < filter->nb_inputs; i++) {
  577. free_link(filter->inputs[i]);
  578. }
  579. for (i = 0; i < filter->nb_outputs; i++) {
  580. free_link(filter->outputs[i]);
  581. }
  582. if (filter->filter->priv_class)
  583. av_opt_free(filter->priv);
  584. av_freep(&filter->name);
  585. av_freep(&filter->input_pads);
  586. av_freep(&filter->output_pads);
  587. av_freep(&filter->inputs);
  588. av_freep(&filter->outputs);
  589. av_freep(&filter->priv);
  590. while(filter->command_queue){
  591. ff_command_queue_pop(filter);
  592. }
  593. av_opt_free(filter);
  594. av_expr_free(filter->enable);
  595. filter->enable = NULL;
  596. av_freep(&filter->var_values);
  597. av_freep(&filter->internal);
  598. av_free(filter);
  599. }
  600. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  601. const char *args)
  602. {
  603. const AVOption *o = NULL;
  604. int ret, count = 0;
  605. char *av_uninit(parsed_key), *av_uninit(value);
  606. const char *key;
  607. int offset= -1;
  608. if (!args)
  609. return 0;
  610. while (*args) {
  611. const char *shorthand = NULL;
  612. o = av_opt_next(ctx->priv, o);
  613. if (o) {
  614. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  615. continue;
  616. offset = o->offset;
  617. shorthand = o->name;
  618. }
  619. ret = av_opt_get_key_value(&args, "=", ":",
  620. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  621. &parsed_key, &value);
  622. if (ret < 0) {
  623. if (ret == AVERROR(EINVAL))
  624. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  625. else
  626. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  627. av_err2str(ret));
  628. return ret;
  629. }
  630. if (*args)
  631. args++;
  632. if (parsed_key) {
  633. key = parsed_key;
  634. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  635. } else {
  636. key = shorthand;
  637. }
  638. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  639. if (av_opt_find(ctx, key, NULL, 0, 0)) {
  640. ret = av_opt_set(ctx, key, value, 0);
  641. if (ret < 0) {
  642. av_free(value);
  643. av_free(parsed_key);
  644. return ret;
  645. }
  646. } else {
  647. av_dict_set(options, key, value, 0);
  648. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  649. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  650. if (ret == AVERROR_OPTION_NOT_FOUND)
  651. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  652. av_free(value);
  653. av_free(parsed_key);
  654. return ret;
  655. }
  656. }
  657. }
  658. av_free(value);
  659. av_free(parsed_key);
  660. count++;
  661. }
  662. if (ctx->enable_str) {
  663. ret = set_enable_expr(ctx, ctx->enable_str);
  664. if (ret < 0)
  665. return ret;
  666. }
  667. return count;
  668. }
  669. #if FF_API_AVFILTER_INIT_FILTER
  670. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  671. {
  672. return avfilter_init_str(filter, args);
  673. }
  674. #endif
  675. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  676. {
  677. int ret = 0;
  678. ret = av_opt_set_dict(ctx, options);
  679. if (ret < 0) {
  680. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  681. return ret;
  682. }
  683. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  684. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  685. ctx->graph->internal->thread_execute) {
  686. ctx->thread_type = AVFILTER_THREAD_SLICE;
  687. ctx->internal->execute = ctx->graph->internal->thread_execute;
  688. } else {
  689. ctx->thread_type = 0;
  690. }
  691. if (ctx->filter->priv_class) {
  692. ret = av_opt_set_dict(ctx->priv, options);
  693. if (ret < 0) {
  694. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  695. return ret;
  696. }
  697. }
  698. if (ctx->filter->init_opaque)
  699. ret = ctx->filter->init_opaque(ctx, NULL);
  700. else if (ctx->filter->init)
  701. ret = ctx->filter->init(ctx);
  702. else if (ctx->filter->init_dict)
  703. ret = ctx->filter->init_dict(ctx, options);
  704. return ret;
  705. }
  706. int avfilter_init_str(AVFilterContext *filter, const char *args)
  707. {
  708. AVDictionary *options = NULL;
  709. AVDictionaryEntry *e;
  710. int ret = 0;
  711. if (args && *args) {
  712. if (!filter->filter->priv_class) {
  713. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  714. "options, but options were provided: %s.\n", args);
  715. return AVERROR(EINVAL);
  716. }
  717. #if FF_API_OLD_FILTER_OPTS
  718. if ( !strcmp(filter->filter->name, "format") ||
  719. !strcmp(filter->filter->name, "noformat") ||
  720. !strcmp(filter->filter->name, "frei0r") ||
  721. !strcmp(filter->filter->name, "frei0r_src") ||
  722. !strcmp(filter->filter->name, "ocv") ||
  723. !strcmp(filter->filter->name, "pan") ||
  724. !strcmp(filter->filter->name, "pp") ||
  725. !strcmp(filter->filter->name, "aevalsrc")) {
  726. /* a hack for compatibility with the old syntax
  727. * replace colons with |s */
  728. char *copy = av_strdup(args);
  729. char *p = copy;
  730. int nb_leading = 0; // number of leading colons to skip
  731. int deprecated = 0;
  732. if (!copy) {
  733. ret = AVERROR(ENOMEM);
  734. goto fail;
  735. }
  736. if (!strcmp(filter->filter->name, "frei0r") ||
  737. !strcmp(filter->filter->name, "ocv"))
  738. nb_leading = 1;
  739. else if (!strcmp(filter->filter->name, "frei0r_src"))
  740. nb_leading = 3;
  741. while (nb_leading--) {
  742. p = strchr(p, ':');
  743. if (!p) {
  744. p = copy + strlen(copy);
  745. break;
  746. }
  747. p++;
  748. }
  749. deprecated = strchr(p, ':') != NULL;
  750. if (!strcmp(filter->filter->name, "aevalsrc")) {
  751. deprecated = 0;
  752. while ((p = strchr(p, ':')) && p[1] != ':') {
  753. const char *epos = strchr(p + 1, '=');
  754. const char *spos = strchr(p + 1, ':');
  755. const int next_token_is_opt = epos && (!spos || epos < spos);
  756. if (next_token_is_opt) {
  757. p++;
  758. break;
  759. }
  760. /* next token does not contain a '=', assume a channel expression */
  761. deprecated = 1;
  762. *p++ = '|';
  763. }
  764. if (p && *p == ':') { // double sep '::' found
  765. deprecated = 1;
  766. memmove(p, p + 1, strlen(p));
  767. }
  768. } else
  769. while ((p = strchr(p, ':')))
  770. *p++ = '|';
  771. if (deprecated)
  772. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  773. "'|' to separate the list items.\n");
  774. av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
  775. ret = process_options(filter, &options, copy);
  776. av_freep(&copy);
  777. if (ret < 0)
  778. goto fail;
  779. #endif
  780. } else {
  781. #if CONFIG_MP_FILTER
  782. if (!strcmp(filter->filter->name, "mp")) {
  783. char *escaped;
  784. if (!strncmp(args, "filter=", 7))
  785. args += 7;
  786. ret = av_escape(&escaped, args, ":=", AV_ESCAPE_MODE_BACKSLASH, 0);
  787. if (ret < 0) {
  788. av_log(filter, AV_LOG_ERROR, "Unable to escape MPlayer filters arg '%s'\n", args);
  789. goto fail;
  790. }
  791. ret = process_options(filter, &options, escaped);
  792. av_free(escaped);
  793. } else
  794. #endif
  795. ret = process_options(filter, &options, args);
  796. if (ret < 0)
  797. goto fail;
  798. }
  799. }
  800. ret = avfilter_init_dict(filter, &options);
  801. if (ret < 0)
  802. goto fail;
  803. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  804. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  805. ret = AVERROR_OPTION_NOT_FOUND;
  806. goto fail;
  807. }
  808. fail:
  809. av_dict_free(&options);
  810. return ret;
  811. }
  812. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  813. {
  814. return pads[pad_idx].name;
  815. }
  816. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  817. {
  818. return pads[pad_idx].type;
  819. }
  820. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  821. {
  822. return ff_filter_frame(link->dst->outputs[0], frame);
  823. }
  824. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  825. {
  826. int (*filter_frame)(AVFilterLink *, AVFrame *);
  827. AVFilterContext *dstctx = link->dst;
  828. AVFilterPad *dst = link->dstpad;
  829. AVFrame *out;
  830. int ret;
  831. AVFilterCommand *cmd= link->dst->command_queue;
  832. int64_t pts;
  833. if (link->closed) {
  834. av_frame_free(&frame);
  835. return AVERROR_EOF;
  836. }
  837. if (!(filter_frame = dst->filter_frame))
  838. filter_frame = default_filter_frame;
  839. /* copy the frame if needed */
  840. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  841. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  842. /* Maybe use ff_copy_buffer_ref instead? */
  843. switch (link->type) {
  844. case AVMEDIA_TYPE_VIDEO:
  845. out = ff_get_video_buffer(link, link->w, link->h);
  846. break;
  847. case AVMEDIA_TYPE_AUDIO:
  848. out = ff_get_audio_buffer(link, frame->nb_samples);
  849. break;
  850. default: return AVERROR(EINVAL);
  851. }
  852. if (!out) {
  853. av_frame_free(&frame);
  854. return AVERROR(ENOMEM);
  855. }
  856. av_frame_copy_props(out, frame);
  857. switch (link->type) {
  858. case AVMEDIA_TYPE_VIDEO:
  859. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  860. frame->format, frame->width, frame->height);
  861. break;
  862. case AVMEDIA_TYPE_AUDIO:
  863. av_samples_copy(out->extended_data, frame->extended_data,
  864. 0, 0, frame->nb_samples,
  865. av_get_channel_layout_nb_channels(frame->channel_layout),
  866. frame->format);
  867. break;
  868. default: return AVERROR(EINVAL);
  869. }
  870. av_frame_free(&frame);
  871. } else
  872. out = frame;
  873. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  874. av_log(link->dst, AV_LOG_DEBUG,
  875. "Processing command time:%f command:%s arg:%s\n",
  876. cmd->time, cmd->command, cmd->arg);
  877. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  878. ff_command_queue_pop(link->dst);
  879. cmd= link->dst->command_queue;
  880. }
  881. pts = out->pts;
  882. if (dstctx->enable_str) {
  883. int64_t pos = av_frame_get_pkt_pos(out);
  884. dstctx->var_values[VAR_N] = link->frame_count;
  885. dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
  886. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  887. dstctx->is_disabled = !av_expr_eval(dstctx->enable, dstctx->var_values, NULL);
  888. if (dstctx->is_disabled &&
  889. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  890. filter_frame = default_filter_frame;
  891. }
  892. ret = filter_frame(link, out);
  893. link->frame_count++;
  894. link->frame_requested = 0;
  895. ff_update_link_current_pts(link, pts);
  896. return ret;
  897. }
  898. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  899. {
  900. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  901. AVFrame *pbuf = link->partial_buf;
  902. int nb_channels = av_frame_get_channels(frame);
  903. int ret = 0;
  904. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  905. /* Handle framing (min_samples, max_samples) */
  906. while (insamples) {
  907. if (!pbuf) {
  908. AVRational samples_tb = { 1, link->sample_rate };
  909. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  910. if (!pbuf) {
  911. av_log(link->dst, AV_LOG_WARNING,
  912. "Samples dropped due to memory allocation failure.\n");
  913. return 0;
  914. }
  915. av_frame_copy_props(pbuf, frame);
  916. pbuf->pts = frame->pts +
  917. av_rescale_q(inpos, samples_tb, link->time_base);
  918. pbuf->nb_samples = 0;
  919. }
  920. nb_samples = FFMIN(insamples,
  921. link->partial_buf_size - pbuf->nb_samples);
  922. av_samples_copy(pbuf->extended_data, frame->extended_data,
  923. pbuf->nb_samples, inpos,
  924. nb_samples, nb_channels, link->format);
  925. inpos += nb_samples;
  926. insamples -= nb_samples;
  927. pbuf->nb_samples += nb_samples;
  928. if (pbuf->nb_samples >= link->min_samples) {
  929. ret = ff_filter_frame_framed(link, pbuf);
  930. pbuf = NULL;
  931. }
  932. }
  933. av_frame_free(&frame);
  934. link->partial_buf = pbuf;
  935. return ret;
  936. }
  937. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  938. {
  939. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  940. /* Consistency checks */
  941. if (link->type == AVMEDIA_TYPE_VIDEO) {
  942. if (strcmp(link->dst->filter->name, "scale")) {
  943. av_assert1(frame->format == link->format);
  944. av_assert1(frame->width == link->w);
  945. av_assert1(frame->height == link->h);
  946. }
  947. } else {
  948. av_assert1(frame->format == link->format);
  949. av_assert1(av_frame_get_channels(frame) == link->channels);
  950. av_assert1(frame->channel_layout == link->channel_layout);
  951. av_assert1(frame->sample_rate == link->sample_rate);
  952. }
  953. /* Go directly to actual filtering if possible */
  954. if (link->type == AVMEDIA_TYPE_AUDIO &&
  955. link->min_samples &&
  956. (link->partial_buf ||
  957. frame->nb_samples < link->min_samples ||
  958. frame->nb_samples > link->max_samples)) {
  959. return ff_filter_frame_needs_framing(link, frame);
  960. } else {
  961. return ff_filter_frame_framed(link, frame);
  962. }
  963. }
  964. const AVClass *avfilter_get_class(void)
  965. {
  966. return &avfilter_class;
  967. }