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