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.

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