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.

998 lines
31KB

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