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.

1049 lines
33KB

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