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.

1152 lines
36KB

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