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