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.

1244 lines
38KB

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