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.

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