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.

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