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.

1199 lines
37KB

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