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