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.

1141 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. char local_res[256] = {0};
  366. if (!res) {
  367. res = local_res;
  368. res_len = sizeof(local_res);
  369. }
  370. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  371. if (res == local_res)
  372. av_log(filter, AV_LOG_INFO, "%s", res);
  373. return 0;
  374. }else if(!strcmp(cmd, "enable")) {
  375. return set_enable_expr(filter, arg);
  376. }else if(filter->filter->process_command) {
  377. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  378. }
  379. return AVERROR(ENOSYS);
  380. }
  381. static AVFilter *first_filter;
  382. AVFilter *avfilter_get_by_name(const char *name)
  383. {
  384. const AVFilter *f = NULL;
  385. if (!name)
  386. return NULL;
  387. while ((f = avfilter_next(f)))
  388. if (!strcmp(f->name, name))
  389. return (AVFilter *)f;
  390. return NULL;
  391. }
  392. int avfilter_register(AVFilter *filter)
  393. {
  394. AVFilter **f = &first_filter;
  395. int i;
  396. /* the filter must select generic or internal exclusively */
  397. av_assert0((filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE) != AVFILTER_FLAG_SUPPORT_TIMELINE);
  398. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  399. const AVFilterPad *input = &filter->inputs[i];
  400. av_assert0( !input->filter_frame
  401. || (!input->start_frame && !input->end_frame));
  402. }
  403. filter->next = NULL;
  404. while(avpriv_atomic_ptr_cas((void * volatile *)f, NULL, filter))
  405. f = &(*f)->next;
  406. return 0;
  407. }
  408. const AVFilter *avfilter_next(const AVFilter *prev)
  409. {
  410. return prev ? prev->next : first_filter;
  411. }
  412. #if FF_API_OLD_FILTER_REGISTER
  413. AVFilter **av_filter_next(AVFilter **filter)
  414. {
  415. return filter ? &(*filter)->next : &first_filter;
  416. }
  417. void avfilter_uninit(void)
  418. {
  419. }
  420. #endif
  421. int avfilter_pad_count(const AVFilterPad *pads)
  422. {
  423. int count;
  424. if (!pads)
  425. return 0;
  426. for (count = 0; pads->name; count++)
  427. pads++;
  428. return count;
  429. }
  430. static const char *default_filter_name(void *filter_ctx)
  431. {
  432. AVFilterContext *ctx = filter_ctx;
  433. return ctx->name ? ctx->name : ctx->filter->name;
  434. }
  435. static void *filter_child_next(void *obj, void *prev)
  436. {
  437. AVFilterContext *ctx = obj;
  438. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  439. return ctx->priv;
  440. return NULL;
  441. }
  442. static const AVClass *filter_child_class_next(const AVClass *prev)
  443. {
  444. const AVFilter *f = NULL;
  445. /* find the filter that corresponds to prev */
  446. while (prev && (f = avfilter_next(f)))
  447. if (f->priv_class == prev)
  448. break;
  449. /* could not find filter corresponding to prev */
  450. if (prev && !f)
  451. return NULL;
  452. /* find next filter with specific options */
  453. while ((f = avfilter_next(f)))
  454. if (f->priv_class)
  455. return f->priv_class;
  456. return NULL;
  457. }
  458. #define OFFSET(x) offsetof(AVFilterContext, x)
  459. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
  460. static const AVOption avfilter_options[] = {
  461. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  462. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  463. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
  464. { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  465. { NULL },
  466. };
  467. static const AVClass avfilter_class = {
  468. .class_name = "AVFilter",
  469. .item_name = default_filter_name,
  470. .version = LIBAVUTIL_VERSION_INT,
  471. .category = AV_CLASS_CATEGORY_FILTER,
  472. .child_next = filter_child_next,
  473. .child_class_next = filter_child_class_next,
  474. .option = avfilter_options,
  475. };
  476. static int default_execute(AVFilterContext *ctx, action_func *func, void *arg,
  477. int *ret, int nb_jobs)
  478. {
  479. int i;
  480. for (i = 0; i < nb_jobs; i++) {
  481. int r = func(ctx, arg, i, nb_jobs);
  482. if (ret)
  483. ret[i] = r;
  484. }
  485. return 0;
  486. }
  487. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  488. {
  489. AVFilterContext *ret;
  490. if (!filter)
  491. return NULL;
  492. ret = av_mallocz(sizeof(AVFilterContext));
  493. if (!ret)
  494. return NULL;
  495. ret->av_class = &avfilter_class;
  496. ret->filter = filter;
  497. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  498. if (filter->priv_size) {
  499. ret->priv = av_mallocz(filter->priv_size);
  500. if (!ret->priv)
  501. goto err;
  502. }
  503. av_opt_set_defaults(ret);
  504. if (filter->priv_class) {
  505. *(const AVClass**)ret->priv = filter->priv_class;
  506. av_opt_set_defaults(ret->priv);
  507. }
  508. ret->internal = av_mallocz(sizeof(*ret->internal));
  509. if (!ret->internal)
  510. goto err;
  511. ret->internal->execute = default_execute;
  512. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  513. if (ret->nb_inputs ) {
  514. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  515. if (!ret->input_pads)
  516. goto err;
  517. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  518. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  519. if (!ret->inputs)
  520. goto err;
  521. }
  522. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  523. if (ret->nb_outputs) {
  524. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  525. if (!ret->output_pads)
  526. goto err;
  527. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  528. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  529. if (!ret->outputs)
  530. goto err;
  531. }
  532. #if FF_API_FOO_COUNT
  533. FF_DISABLE_DEPRECATION_WARNINGS
  534. ret->output_count = ret->nb_outputs;
  535. ret->input_count = ret->nb_inputs;
  536. FF_ENABLE_DEPRECATION_WARNINGS
  537. #endif
  538. return ret;
  539. err:
  540. av_freep(&ret->inputs);
  541. av_freep(&ret->input_pads);
  542. ret->nb_inputs = 0;
  543. av_freep(&ret->outputs);
  544. av_freep(&ret->output_pads);
  545. ret->nb_outputs = 0;
  546. av_freep(&ret->priv);
  547. av_freep(&ret->internal);
  548. av_free(ret);
  549. return NULL;
  550. }
  551. #if FF_API_AVFILTER_OPEN
  552. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  553. {
  554. *filter_ctx = ff_filter_alloc(filter, inst_name);
  555. return *filter_ctx ? 0 : AVERROR(ENOMEM);
  556. }
  557. #endif
  558. static void free_link(AVFilterLink *link)
  559. {
  560. if (!link)
  561. return;
  562. if (link->src)
  563. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  564. if (link->dst)
  565. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  566. ff_formats_unref(&link->in_formats);
  567. ff_formats_unref(&link->out_formats);
  568. ff_formats_unref(&link->in_samplerates);
  569. ff_formats_unref(&link->out_samplerates);
  570. ff_channel_layouts_unref(&link->in_channel_layouts);
  571. ff_channel_layouts_unref(&link->out_channel_layouts);
  572. avfilter_link_free(&link);
  573. }
  574. void avfilter_free(AVFilterContext *filter)
  575. {
  576. int i;
  577. if (!filter)
  578. return;
  579. if (filter->graph)
  580. ff_filter_graph_remove_filter(filter->graph, filter);
  581. if (filter->filter->uninit)
  582. filter->filter->uninit(filter);
  583. for (i = 0; i < filter->nb_inputs; i++) {
  584. free_link(filter->inputs[i]);
  585. }
  586. for (i = 0; i < filter->nb_outputs; i++) {
  587. free_link(filter->outputs[i]);
  588. }
  589. if (filter->filter->priv_class)
  590. av_opt_free(filter->priv);
  591. av_freep(&filter->name);
  592. av_freep(&filter->input_pads);
  593. av_freep(&filter->output_pads);
  594. av_freep(&filter->inputs);
  595. av_freep(&filter->outputs);
  596. av_freep(&filter->priv);
  597. while(filter->command_queue){
  598. ff_command_queue_pop(filter);
  599. }
  600. av_opt_free(filter);
  601. av_expr_free(filter->enable);
  602. filter->enable = NULL;
  603. av_freep(&filter->var_values);
  604. av_freep(&filter->internal);
  605. av_free(filter);
  606. }
  607. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  608. const char *args)
  609. {
  610. const AVOption *o = NULL;
  611. int ret, count = 0;
  612. char *av_uninit(parsed_key), *av_uninit(value);
  613. const char *key;
  614. int offset= -1;
  615. if (!args)
  616. return 0;
  617. while (*args) {
  618. const char *shorthand = NULL;
  619. o = av_opt_next(ctx->priv, o);
  620. if (o) {
  621. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  622. continue;
  623. offset = o->offset;
  624. shorthand = o->name;
  625. }
  626. ret = av_opt_get_key_value(&args, "=", ":",
  627. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  628. &parsed_key, &value);
  629. if (ret < 0) {
  630. if (ret == AVERROR(EINVAL))
  631. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  632. else
  633. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  634. av_err2str(ret));
  635. return ret;
  636. }
  637. if (*args)
  638. args++;
  639. if (parsed_key) {
  640. key = parsed_key;
  641. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  642. } else {
  643. key = shorthand;
  644. }
  645. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  646. if (av_opt_find(ctx, key, NULL, 0, 0)) {
  647. ret = av_opt_set(ctx, key, value, 0);
  648. if (ret < 0) {
  649. av_free(value);
  650. av_free(parsed_key);
  651. return ret;
  652. }
  653. } else {
  654. av_dict_set(options, key, value, 0);
  655. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  656. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  657. if (ret == AVERROR_OPTION_NOT_FOUND)
  658. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  659. av_free(value);
  660. av_free(parsed_key);
  661. return ret;
  662. }
  663. }
  664. }
  665. av_free(value);
  666. av_free(parsed_key);
  667. count++;
  668. }
  669. if (ctx->enable_str) {
  670. ret = set_enable_expr(ctx, ctx->enable_str);
  671. if (ret < 0)
  672. return ret;
  673. }
  674. return count;
  675. }
  676. #if FF_API_AVFILTER_INIT_FILTER
  677. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  678. {
  679. return avfilter_init_str(filter, args);
  680. }
  681. #endif
  682. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  683. {
  684. int ret = 0;
  685. ret = av_opt_set_dict(ctx, options);
  686. if (ret < 0) {
  687. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  688. return ret;
  689. }
  690. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  691. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  692. ctx->graph->internal->thread_execute) {
  693. ctx->thread_type = AVFILTER_THREAD_SLICE;
  694. ctx->internal->execute = ctx->graph->internal->thread_execute;
  695. } else {
  696. ctx->thread_type = 0;
  697. }
  698. if (ctx->filter->priv_class) {
  699. ret = av_opt_set_dict(ctx->priv, options);
  700. if (ret < 0) {
  701. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  702. return ret;
  703. }
  704. }
  705. if (ctx->filter->init_opaque)
  706. ret = ctx->filter->init_opaque(ctx, NULL);
  707. else if (ctx->filter->init)
  708. ret = ctx->filter->init(ctx);
  709. else if (ctx->filter->init_dict)
  710. ret = ctx->filter->init_dict(ctx, options);
  711. return ret;
  712. }
  713. int avfilter_init_str(AVFilterContext *filter, const char *args)
  714. {
  715. AVDictionary *options = NULL;
  716. AVDictionaryEntry *e;
  717. int ret = 0;
  718. if (args && *args) {
  719. if (!filter->filter->priv_class) {
  720. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  721. "options, but options were provided: %s.\n", args);
  722. return AVERROR(EINVAL);
  723. }
  724. #if FF_API_OLD_FILTER_OPTS
  725. if ( !strcmp(filter->filter->name, "format") ||
  726. !strcmp(filter->filter->name, "noformat") ||
  727. !strcmp(filter->filter->name, "frei0r") ||
  728. !strcmp(filter->filter->name, "frei0r_src") ||
  729. !strcmp(filter->filter->name, "ocv") ||
  730. !strcmp(filter->filter->name, "pan") ||
  731. !strcmp(filter->filter->name, "pp") ||
  732. !strcmp(filter->filter->name, "aevalsrc")) {
  733. /* a hack for compatibility with the old syntax
  734. * replace colons with |s */
  735. char *copy = av_strdup(args);
  736. char *p = copy;
  737. int nb_leading = 0; // number of leading colons to skip
  738. int deprecated = 0;
  739. if (!copy) {
  740. ret = AVERROR(ENOMEM);
  741. goto fail;
  742. }
  743. if (!strcmp(filter->filter->name, "frei0r") ||
  744. !strcmp(filter->filter->name, "ocv"))
  745. nb_leading = 1;
  746. else if (!strcmp(filter->filter->name, "frei0r_src"))
  747. nb_leading = 3;
  748. while (nb_leading--) {
  749. p = strchr(p, ':');
  750. if (!p) {
  751. p = copy + strlen(copy);
  752. break;
  753. }
  754. p++;
  755. }
  756. deprecated = strchr(p, ':') != NULL;
  757. if (!strcmp(filter->filter->name, "aevalsrc")) {
  758. deprecated = 0;
  759. while ((p = strchr(p, ':')) && p[1] != ':') {
  760. const char *epos = strchr(p + 1, '=');
  761. const char *spos = strchr(p + 1, ':');
  762. const int next_token_is_opt = epos && (!spos || epos < spos);
  763. if (next_token_is_opt) {
  764. p++;
  765. break;
  766. }
  767. /* next token does not contain a '=', assume a channel expression */
  768. deprecated = 1;
  769. *p++ = '|';
  770. }
  771. if (p && *p == ':') { // double sep '::' found
  772. deprecated = 1;
  773. memmove(p, p + 1, strlen(p));
  774. }
  775. } else
  776. while ((p = strchr(p, ':')))
  777. *p++ = '|';
  778. if (deprecated)
  779. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  780. "'|' to separate the list items.\n");
  781. av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
  782. ret = process_options(filter, &options, copy);
  783. av_freep(&copy);
  784. if (ret < 0)
  785. goto fail;
  786. #endif
  787. } else {
  788. #if CONFIG_MP_FILTER
  789. if (!strcmp(filter->filter->name, "mp")) {
  790. char *escaped;
  791. if (!strncmp(args, "filter=", 7))
  792. args += 7;
  793. ret = av_escape(&escaped, args, ":=", AV_ESCAPE_MODE_BACKSLASH, 0);
  794. if (ret < 0) {
  795. av_log(filter, AV_LOG_ERROR, "Unable to escape MPlayer filters arg '%s'\n", args);
  796. goto fail;
  797. }
  798. ret = process_options(filter, &options, escaped);
  799. av_free(escaped);
  800. } else
  801. #endif
  802. ret = process_options(filter, &options, args);
  803. if (ret < 0)
  804. goto fail;
  805. }
  806. }
  807. ret = avfilter_init_dict(filter, &options);
  808. if (ret < 0)
  809. goto fail;
  810. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  811. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  812. ret = AVERROR_OPTION_NOT_FOUND;
  813. goto fail;
  814. }
  815. fail:
  816. av_dict_free(&options);
  817. return ret;
  818. }
  819. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  820. {
  821. return pads[pad_idx].name;
  822. }
  823. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  824. {
  825. return pads[pad_idx].type;
  826. }
  827. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  828. {
  829. return ff_filter_frame(link->dst->outputs[0], frame);
  830. }
  831. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  832. {
  833. int (*filter_frame)(AVFilterLink *, AVFrame *);
  834. AVFilterContext *dstctx = link->dst;
  835. AVFilterPad *dst = link->dstpad;
  836. AVFrame *out;
  837. int ret;
  838. AVFilterCommand *cmd= link->dst->command_queue;
  839. int64_t pts;
  840. if (link->closed) {
  841. av_frame_free(&frame);
  842. return AVERROR_EOF;
  843. }
  844. if (!(filter_frame = dst->filter_frame))
  845. filter_frame = default_filter_frame;
  846. /* copy the frame if needed */
  847. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  848. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  849. /* Maybe use ff_copy_buffer_ref instead? */
  850. switch (link->type) {
  851. case AVMEDIA_TYPE_VIDEO:
  852. out = ff_get_video_buffer(link, link->w, link->h);
  853. break;
  854. case AVMEDIA_TYPE_AUDIO:
  855. out = ff_get_audio_buffer(link, frame->nb_samples);
  856. break;
  857. default: return AVERROR(EINVAL);
  858. }
  859. if (!out) {
  860. av_frame_free(&frame);
  861. return AVERROR(ENOMEM);
  862. }
  863. av_frame_copy_props(out, frame);
  864. switch (link->type) {
  865. case AVMEDIA_TYPE_VIDEO:
  866. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  867. frame->format, frame->width, frame->height);
  868. break;
  869. case AVMEDIA_TYPE_AUDIO:
  870. av_samples_copy(out->extended_data, frame->extended_data,
  871. 0, 0, frame->nb_samples,
  872. av_get_channel_layout_nb_channels(frame->channel_layout),
  873. frame->format);
  874. break;
  875. default: return AVERROR(EINVAL);
  876. }
  877. av_frame_free(&frame);
  878. } else
  879. out = frame;
  880. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  881. av_log(link->dst, AV_LOG_DEBUG,
  882. "Processing command time:%f command:%s arg:%s\n",
  883. cmd->time, cmd->command, cmd->arg);
  884. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  885. ff_command_queue_pop(link->dst);
  886. cmd= link->dst->command_queue;
  887. }
  888. pts = out->pts;
  889. if (dstctx->enable_str) {
  890. int64_t pos = av_frame_get_pkt_pos(out);
  891. dstctx->var_values[VAR_N] = link->frame_count;
  892. dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
  893. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  894. dstctx->is_disabled = !av_expr_eval(dstctx->enable, dstctx->var_values, NULL);
  895. if (dstctx->is_disabled &&
  896. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  897. filter_frame = default_filter_frame;
  898. }
  899. ret = filter_frame(link, out);
  900. link->frame_count++;
  901. link->frame_requested = 0;
  902. ff_update_link_current_pts(link, pts);
  903. return ret;
  904. }
  905. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  906. {
  907. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  908. AVFrame *pbuf = link->partial_buf;
  909. int nb_channels = av_frame_get_channels(frame);
  910. int ret = 0;
  911. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  912. /* Handle framing (min_samples, max_samples) */
  913. while (insamples) {
  914. if (!pbuf) {
  915. AVRational samples_tb = { 1, link->sample_rate };
  916. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  917. if (!pbuf) {
  918. av_log(link->dst, AV_LOG_WARNING,
  919. "Samples dropped due to memory allocation failure.\n");
  920. return 0;
  921. }
  922. av_frame_copy_props(pbuf, frame);
  923. pbuf->pts = frame->pts +
  924. av_rescale_q(inpos, samples_tb, link->time_base);
  925. pbuf->nb_samples = 0;
  926. }
  927. nb_samples = FFMIN(insamples,
  928. link->partial_buf_size - pbuf->nb_samples);
  929. av_samples_copy(pbuf->extended_data, frame->extended_data,
  930. pbuf->nb_samples, inpos,
  931. nb_samples, nb_channels, link->format);
  932. inpos += nb_samples;
  933. insamples -= nb_samples;
  934. pbuf->nb_samples += nb_samples;
  935. if (pbuf->nb_samples >= link->min_samples) {
  936. ret = ff_filter_frame_framed(link, pbuf);
  937. pbuf = NULL;
  938. }
  939. }
  940. av_frame_free(&frame);
  941. link->partial_buf = pbuf;
  942. return ret;
  943. }
  944. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  945. {
  946. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  947. /* Consistency checks */
  948. if (link->type == AVMEDIA_TYPE_VIDEO) {
  949. if (strcmp(link->dst->filter->name, "scale")) {
  950. av_assert1(frame->format == link->format);
  951. av_assert1(frame->width == link->w);
  952. av_assert1(frame->height == link->h);
  953. }
  954. } else {
  955. av_assert1(frame->format == link->format);
  956. av_assert1(av_frame_get_channels(frame) == link->channels);
  957. av_assert1(frame->channel_layout == link->channel_layout);
  958. av_assert1(frame->sample_rate == link->sample_rate);
  959. }
  960. /* Go directly to actual filtering if possible */
  961. if (link->type == AVMEDIA_TYPE_AUDIO &&
  962. link->min_samples &&
  963. (link->partial_buf ||
  964. frame->nb_samples < link->min_samples ||
  965. frame->nb_samples > link->max_samples)) {
  966. return ff_filter_frame_needs_framing(link, frame);
  967. } else {
  968. return ff_filter_frame_framed(link, frame);
  969. }
  970. }
  971. const AVClass *avfilter_get_class(void)
  972. {
  973. return &avfilter_class;
  974. }