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.

1082 lines
34KB

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