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.

1236 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 ((config_link = link->dstpad->config_props))
  270. if ((ret = config_link(link)) < 0) {
  271. av_log(link->dst, AV_LOG_ERROR,
  272. "Failed to configure input pad on %s\n",
  273. link->dst->name);
  274. return ret;
  275. }
  276. if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
  277. !link->hw_frames_ctx) {
  278. AVHWFramesContext *input_ctx = (AVHWFramesContext*)link->src->inputs[0]->hw_frames_ctx->data;
  279. if (input_ctx->format == link->format) {
  280. link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
  281. if (!link->hw_frames_ctx)
  282. return AVERROR(ENOMEM);
  283. }
  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. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  661. const char *args)
  662. {
  663. const AVOption *o = NULL;
  664. int ret, count = 0;
  665. char *av_uninit(parsed_key), *av_uninit(value);
  666. const char *key;
  667. int offset= -1;
  668. if (!args)
  669. return 0;
  670. while (*args) {
  671. const char *shorthand = NULL;
  672. o = av_opt_next(ctx->priv, o);
  673. if (o) {
  674. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  675. continue;
  676. offset = o->offset;
  677. shorthand = o->name;
  678. }
  679. ret = av_opt_get_key_value(&args, "=", ":",
  680. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  681. &parsed_key, &value);
  682. if (ret < 0) {
  683. if (ret == AVERROR(EINVAL))
  684. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  685. else
  686. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  687. av_err2str(ret));
  688. return ret;
  689. }
  690. if (*args)
  691. args++;
  692. if (parsed_key) {
  693. key = parsed_key;
  694. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  695. } else {
  696. key = shorthand;
  697. }
  698. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  699. if (av_opt_find(ctx, key, NULL, 0, 0)) {
  700. ret = av_opt_set(ctx, key, value, 0);
  701. if (ret < 0) {
  702. av_free(value);
  703. av_free(parsed_key);
  704. return ret;
  705. }
  706. } else {
  707. av_dict_set(options, key, value, 0);
  708. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  709. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  710. if (ret == AVERROR_OPTION_NOT_FOUND)
  711. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  712. av_free(value);
  713. av_free(parsed_key);
  714. return ret;
  715. }
  716. }
  717. }
  718. av_free(value);
  719. av_free(parsed_key);
  720. count++;
  721. }
  722. if (ctx->enable_str) {
  723. ret = set_enable_expr(ctx, ctx->enable_str);
  724. if (ret < 0)
  725. return ret;
  726. }
  727. return count;
  728. }
  729. #if FF_API_AVFILTER_INIT_FILTER
  730. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  731. {
  732. return avfilter_init_str(filter, args);
  733. }
  734. #endif
  735. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  736. {
  737. int ret = 0;
  738. ret = av_opt_set_dict(ctx, options);
  739. if (ret < 0) {
  740. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  741. return ret;
  742. }
  743. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  744. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  745. ctx->graph->internal->thread_execute) {
  746. ctx->thread_type = AVFILTER_THREAD_SLICE;
  747. ctx->internal->execute = ctx->graph->internal->thread_execute;
  748. } else {
  749. ctx->thread_type = 0;
  750. }
  751. if (ctx->filter->priv_class) {
  752. ret = av_opt_set_dict(ctx->priv, options);
  753. if (ret < 0) {
  754. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  755. return ret;
  756. }
  757. }
  758. if (ctx->filter->init_opaque)
  759. ret = ctx->filter->init_opaque(ctx, NULL);
  760. else if (ctx->filter->init)
  761. ret = ctx->filter->init(ctx);
  762. else if (ctx->filter->init_dict)
  763. ret = ctx->filter->init_dict(ctx, options);
  764. return ret;
  765. }
  766. int avfilter_init_str(AVFilterContext *filter, const char *args)
  767. {
  768. AVDictionary *options = NULL;
  769. AVDictionaryEntry *e;
  770. int ret = 0;
  771. if (args && *args) {
  772. if (!filter->filter->priv_class) {
  773. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  774. "options, but options were provided: %s.\n", args);
  775. return AVERROR(EINVAL);
  776. }
  777. #if FF_API_OLD_FILTER_OPTS || FF_API_OLD_FILTER_OPTS_ERROR
  778. if ( !strcmp(filter->filter->name, "format") ||
  779. !strcmp(filter->filter->name, "noformat") ||
  780. !strcmp(filter->filter->name, "frei0r") ||
  781. !strcmp(filter->filter->name, "frei0r_src") ||
  782. !strcmp(filter->filter->name, "ocv") ||
  783. !strcmp(filter->filter->name, "pan") ||
  784. !strcmp(filter->filter->name, "pp") ||
  785. !strcmp(filter->filter->name, "aevalsrc")) {
  786. /* a hack for compatibility with the old syntax
  787. * replace colons with |s */
  788. char *copy = av_strdup(args);
  789. char *p = copy;
  790. int nb_leading = 0; // number of leading colons to skip
  791. int deprecated = 0;
  792. if (!copy) {
  793. ret = AVERROR(ENOMEM);
  794. goto fail;
  795. }
  796. if (!strcmp(filter->filter->name, "frei0r") ||
  797. !strcmp(filter->filter->name, "ocv"))
  798. nb_leading = 1;
  799. else if (!strcmp(filter->filter->name, "frei0r_src"))
  800. nb_leading = 3;
  801. while (nb_leading--) {
  802. p = strchr(p, ':');
  803. if (!p) {
  804. p = copy + strlen(copy);
  805. break;
  806. }
  807. p++;
  808. }
  809. deprecated = strchr(p, ':') != NULL;
  810. if (!strcmp(filter->filter->name, "aevalsrc")) {
  811. deprecated = 0;
  812. while ((p = strchr(p, ':')) && p[1] != ':') {
  813. const char *epos = strchr(p + 1, '=');
  814. const char *spos = strchr(p + 1, ':');
  815. const int next_token_is_opt = epos && (!spos || epos < spos);
  816. if (next_token_is_opt) {
  817. p++;
  818. break;
  819. }
  820. /* next token does not contain a '=', assume a channel expression */
  821. deprecated = 1;
  822. *p++ = '|';
  823. }
  824. if (p && *p == ':') { // double sep '::' found
  825. deprecated = 1;
  826. memmove(p, p + 1, strlen(p));
  827. }
  828. } else
  829. while ((p = strchr(p, ':')))
  830. *p++ = '|';
  831. #if FF_API_OLD_FILTER_OPTS
  832. if (deprecated)
  833. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  834. "'|' to separate the list items.\n");
  835. av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
  836. ret = process_options(filter, &options, copy);
  837. #else
  838. if (deprecated) {
  839. av_log(filter, AV_LOG_ERROR, "This syntax is deprecated. Use "
  840. "'|' to separate the list items ('%s' instead of '%s')\n",
  841. copy, args);
  842. ret = AVERROR(EINVAL);
  843. } else {
  844. ret = process_options(filter, &options, copy);
  845. }
  846. #endif
  847. av_freep(&copy);
  848. if (ret < 0)
  849. goto fail;
  850. } else
  851. #endif
  852. {
  853. ret = process_options(filter, &options, args);
  854. if (ret < 0)
  855. goto fail;
  856. }
  857. }
  858. ret = avfilter_init_dict(filter, &options);
  859. if (ret < 0)
  860. goto fail;
  861. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  862. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  863. ret = AVERROR_OPTION_NOT_FOUND;
  864. goto fail;
  865. }
  866. fail:
  867. av_dict_free(&options);
  868. return ret;
  869. }
  870. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  871. {
  872. return pads[pad_idx].name;
  873. }
  874. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  875. {
  876. return pads[pad_idx].type;
  877. }
  878. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  879. {
  880. return ff_filter_frame(link->dst->outputs[0], frame);
  881. }
  882. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  883. {
  884. int (*filter_frame)(AVFilterLink *, AVFrame *);
  885. AVFilterContext *dstctx = link->dst;
  886. AVFilterPad *dst = link->dstpad;
  887. AVFrame *out = NULL;
  888. int ret;
  889. AVFilterCommand *cmd= link->dst->command_queue;
  890. int64_t pts;
  891. if (link->status) {
  892. av_frame_free(&frame);
  893. return link->status;
  894. }
  895. if (!(filter_frame = dst->filter_frame))
  896. filter_frame = default_filter_frame;
  897. /* copy the frame if needed */
  898. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  899. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  900. switch (link->type) {
  901. case AVMEDIA_TYPE_VIDEO:
  902. out = ff_get_video_buffer(link, link->w, link->h);
  903. break;
  904. case AVMEDIA_TYPE_AUDIO:
  905. out = ff_get_audio_buffer(link, frame->nb_samples);
  906. break;
  907. default:
  908. ret = AVERROR(EINVAL);
  909. goto fail;
  910. }
  911. if (!out) {
  912. ret = AVERROR(ENOMEM);
  913. goto fail;
  914. }
  915. ret = av_frame_copy_props(out, frame);
  916. if (ret < 0)
  917. goto fail;
  918. switch (link->type) {
  919. case AVMEDIA_TYPE_VIDEO:
  920. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  921. frame->format, frame->width, frame->height);
  922. break;
  923. case AVMEDIA_TYPE_AUDIO:
  924. av_samples_copy(out->extended_data, frame->extended_data,
  925. 0, 0, frame->nb_samples,
  926. av_get_channel_layout_nb_channels(frame->channel_layout),
  927. frame->format);
  928. break;
  929. default:
  930. ret = AVERROR(EINVAL);
  931. goto fail;
  932. }
  933. av_frame_free(&frame);
  934. } else
  935. out = frame;
  936. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  937. av_log(link->dst, AV_LOG_DEBUG,
  938. "Processing command time:%f command:%s arg:%s\n",
  939. cmd->time, cmd->command, cmd->arg);
  940. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  941. ff_command_queue_pop(link->dst);
  942. cmd= link->dst->command_queue;
  943. }
  944. pts = out->pts;
  945. if (dstctx->enable_str) {
  946. int64_t pos = av_frame_get_pkt_pos(out);
  947. dstctx->var_values[VAR_N] = link->frame_count;
  948. dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
  949. dstctx->var_values[VAR_W] = link->w;
  950. dstctx->var_values[VAR_H] = link->h;
  951. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  952. dstctx->is_disabled = fabs(av_expr_eval(dstctx->enable, dstctx->var_values, NULL)) < 0.5;
  953. if (dstctx->is_disabled &&
  954. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  955. filter_frame = default_filter_frame;
  956. }
  957. ret = filter_frame(link, out);
  958. link->frame_count++;
  959. ff_update_link_current_pts(link, pts);
  960. return ret;
  961. fail:
  962. av_frame_free(&out);
  963. av_frame_free(&frame);
  964. return ret;
  965. }
  966. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  967. {
  968. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  969. AVFrame *pbuf = link->partial_buf;
  970. int nb_channels = av_frame_get_channels(frame);
  971. int ret = 0;
  972. /* Handle framing (min_samples, max_samples) */
  973. while (insamples) {
  974. if (!pbuf) {
  975. AVRational samples_tb = { 1, link->sample_rate };
  976. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  977. if (!pbuf) {
  978. av_log(link->dst, AV_LOG_WARNING,
  979. "Samples dropped due to memory allocation failure.\n");
  980. return 0;
  981. }
  982. av_frame_copy_props(pbuf, frame);
  983. pbuf->pts = frame->pts;
  984. if (pbuf->pts != AV_NOPTS_VALUE)
  985. pbuf->pts += av_rescale_q(inpos, samples_tb, link->time_base);
  986. pbuf->nb_samples = 0;
  987. }
  988. nb_samples = FFMIN(insamples,
  989. link->partial_buf_size - pbuf->nb_samples);
  990. av_samples_copy(pbuf->extended_data, frame->extended_data,
  991. pbuf->nb_samples, inpos,
  992. nb_samples, nb_channels, link->format);
  993. inpos += nb_samples;
  994. insamples -= nb_samples;
  995. pbuf->nb_samples += nb_samples;
  996. if (pbuf->nb_samples >= link->min_samples) {
  997. ret = ff_filter_frame_framed(link, pbuf);
  998. pbuf = NULL;
  999. } else {
  1000. if (link->frame_wanted_out)
  1001. link->frame_wanted_in = 1;
  1002. }
  1003. }
  1004. av_frame_free(&frame);
  1005. link->partial_buf = pbuf;
  1006. return ret;
  1007. }
  1008. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  1009. {
  1010. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  1011. /* Consistency checks */
  1012. if (link->type == AVMEDIA_TYPE_VIDEO) {
  1013. if (strcmp(link->dst->filter->name, "buffersink") &&
  1014. strcmp(link->dst->filter->name, "format") &&
  1015. strcmp(link->dst->filter->name, "idet") &&
  1016. strcmp(link->dst->filter->name, "null") &&
  1017. strcmp(link->dst->filter->name, "scale")) {
  1018. av_assert1(frame->format == link->format);
  1019. av_assert1(frame->width == link->w);
  1020. av_assert1(frame->height == link->h);
  1021. }
  1022. } else {
  1023. if (frame->format != link->format) {
  1024. av_log(link->dst, AV_LOG_ERROR, "Format change is not supported\n");
  1025. goto error;
  1026. }
  1027. if (av_frame_get_channels(frame) != link->channels) {
  1028. av_log(link->dst, AV_LOG_ERROR, "Channel count change is not supported\n");
  1029. goto error;
  1030. }
  1031. if (frame->channel_layout != link->channel_layout) {
  1032. av_log(link->dst, AV_LOG_ERROR, "Channel layout change is not supported\n");
  1033. goto error;
  1034. }
  1035. if (frame->sample_rate != link->sample_rate) {
  1036. av_log(link->dst, AV_LOG_ERROR, "Sample rate change is not supported\n");
  1037. goto error;
  1038. }
  1039. }
  1040. link->frame_wanted_out = 0;
  1041. /* Go directly to actual filtering if possible */
  1042. if (link->type == AVMEDIA_TYPE_AUDIO &&
  1043. link->min_samples &&
  1044. (link->partial_buf ||
  1045. frame->nb_samples < link->min_samples ||
  1046. frame->nb_samples > link->max_samples)) {
  1047. return ff_filter_frame_needs_framing(link, frame);
  1048. } else {
  1049. return ff_filter_frame_framed(link, frame);
  1050. }
  1051. error:
  1052. av_frame_free(&frame);
  1053. return AVERROR_PATCHWELCOME;
  1054. }
  1055. const AVClass *avfilter_get_class(void)
  1056. {
  1057. return &avfilter_class;
  1058. }