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.

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