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.

1186 lines
36KB

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