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.

1060 lines
35KB

  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. #define MAX_REGISTERED_AVFILTERS_NB 256
  331. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  332. static int next_registered_avfilter_idx = 0;
  333. AVFilter *avfilter_get_by_name(const char *name)
  334. {
  335. int i;
  336. for (i = 0; registered_avfilters[i]; i++)
  337. if (!strcmp(registered_avfilters[i]->name, name))
  338. return registered_avfilters[i];
  339. return NULL;
  340. }
  341. int avfilter_register(AVFilter *filter)
  342. {
  343. int i;
  344. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  345. av_log(NULL, AV_LOG_ERROR,
  346. "Maximum number of registered filters %d reached, "
  347. "impossible to register filter with name '%s'\n",
  348. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  349. return AVERROR(ENOMEM);
  350. }
  351. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  352. const AVFilterPad *input = &filter->inputs[i];
  353. av_assert0( !input->filter_frame
  354. || (!input->start_frame && !input->end_frame));
  355. }
  356. registered_avfilters[next_registered_avfilter_idx++] = filter;
  357. return 0;
  358. }
  359. AVFilter **av_filter_next(AVFilter **filter)
  360. {
  361. return filter ? ++filter : &registered_avfilters[0];
  362. }
  363. void avfilter_uninit(void)
  364. {
  365. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  366. next_registered_avfilter_idx = 0;
  367. }
  368. static int pad_count(const AVFilterPad *pads)
  369. {
  370. int count;
  371. if (!pads)
  372. return 0;
  373. for(count = 0; pads->name; count ++) pads ++;
  374. return count;
  375. }
  376. static const char *default_filter_name(void *filter_ctx)
  377. {
  378. AVFilterContext *ctx = filter_ctx;
  379. return ctx->name ? ctx->name : ctx->filter->name;
  380. }
  381. static void *filter_child_next(void *obj, void *prev)
  382. {
  383. AVFilterContext *ctx = obj;
  384. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  385. return ctx->priv;
  386. return NULL;
  387. }
  388. static const AVClass *filter_child_class_next(const AVClass *prev)
  389. {
  390. AVFilter **f = NULL;
  391. /* find the filter that corresponds to prev */
  392. while (prev && *(f = av_filter_next(f)))
  393. if ((*f)->priv_class == prev)
  394. break;
  395. /* could not find filter corresponding to prev */
  396. if (prev && !(*f))
  397. return NULL;
  398. /* find next filter with specific options */
  399. while (*(f = av_filter_next(f)))
  400. if ((*f)->priv_class)
  401. return (*f)->priv_class;
  402. return NULL;
  403. }
  404. static const AVClass avfilter_class = {
  405. .class_name = "AVFilter",
  406. .item_name = default_filter_name,
  407. .version = LIBAVUTIL_VERSION_INT,
  408. .category = AV_CLASS_CATEGORY_FILTER,
  409. .child_next = filter_child_next,
  410. .child_class_next = filter_child_class_next,
  411. };
  412. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  413. {
  414. AVFilterContext *ret;
  415. *filter_ctx = NULL;
  416. if (!filter)
  417. return AVERROR(EINVAL);
  418. ret = av_mallocz(sizeof(AVFilterContext));
  419. if (!ret)
  420. return AVERROR(ENOMEM);
  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 = 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 = 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. *filter_ctx = ret;
  458. return 0;
  459. err:
  460. av_freep(&ret->inputs);
  461. av_freep(&ret->input_pads);
  462. ret->nb_inputs = 0;
  463. av_freep(&ret->outputs);
  464. av_freep(&ret->output_pads);
  465. ret->nb_outputs = 0;
  466. av_freep(&ret->priv);
  467. av_free(ret);
  468. return AVERROR(ENOMEM);
  469. }
  470. void avfilter_free(AVFilterContext *filter)
  471. {
  472. int i;
  473. AVFilterLink *link;
  474. if (!filter)
  475. return;
  476. if (filter->filter->uninit)
  477. filter->filter->uninit(filter);
  478. for (i = 0; i < filter->nb_inputs; i++) {
  479. if ((link = filter->inputs[i])) {
  480. if (link->src)
  481. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  482. ff_formats_unref(&link->in_formats);
  483. ff_formats_unref(&link->out_formats);
  484. ff_formats_unref(&link->in_samplerates);
  485. ff_formats_unref(&link->out_samplerates);
  486. ff_channel_layouts_unref(&link->in_channel_layouts);
  487. ff_channel_layouts_unref(&link->out_channel_layouts);
  488. }
  489. avfilter_link_free(&link);
  490. }
  491. for (i = 0; i < filter->nb_outputs; i++) {
  492. if ((link = filter->outputs[i])) {
  493. if (link->dst)
  494. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  495. ff_formats_unref(&link->in_formats);
  496. ff_formats_unref(&link->out_formats);
  497. ff_formats_unref(&link->in_samplerates);
  498. ff_formats_unref(&link->out_samplerates);
  499. ff_channel_layouts_unref(&link->in_channel_layouts);
  500. ff_channel_layouts_unref(&link->out_channel_layouts);
  501. }
  502. avfilter_link_free(&link);
  503. }
  504. if (filter->filter->priv_class || filter->filter->shorthand)
  505. av_opt_free(filter->priv);
  506. av_freep(&filter->name);
  507. av_freep(&filter->input_pads);
  508. av_freep(&filter->output_pads);
  509. av_freep(&filter->inputs);
  510. av_freep(&filter->outputs);
  511. av_freep(&filter->priv);
  512. while(filter->command_queue){
  513. ff_command_queue_pop(filter);
  514. }
  515. av_free(filter);
  516. }
  517. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  518. const char *args)
  519. {
  520. const AVOption *o = NULL;
  521. int ret, count = 0;
  522. char *av_uninit(parsed_key), *av_uninit(value);
  523. const char *key;
  524. int offset= -1;
  525. if (!args)
  526. return 0;
  527. while (*args) {
  528. const char *shorthand = NULL;
  529. o = av_opt_next(ctx->priv, o);
  530. if (o) {
  531. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  532. continue;
  533. offset = o->offset;
  534. shorthand = o->name;
  535. }
  536. ret = av_opt_get_key_value(&args, "=", ":",
  537. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  538. &parsed_key, &value);
  539. if (ret < 0) {
  540. if (ret == AVERROR(EINVAL))
  541. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  542. else
  543. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  544. av_err2str(ret));
  545. return ret;
  546. }
  547. if (*args)
  548. args++;
  549. if (parsed_key) {
  550. key = parsed_key;
  551. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  552. } else {
  553. key = shorthand;
  554. }
  555. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  556. av_dict_set(options, key, value, 0);
  557. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  558. if (ret == AVERROR_OPTION_NOT_FOUND)
  559. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  560. av_free(value);
  561. av_free(parsed_key);
  562. return ret;
  563. }
  564. av_free(value);
  565. av_free(parsed_key);
  566. count++;
  567. }
  568. return count;
  569. }
  570. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  571. {
  572. AVDictionary *options = NULL;
  573. AVDictionaryEntry *e;
  574. int ret=0;
  575. int anton_options =
  576. !strcmp(filter->filter->name, "allpass" ) ||
  577. !strcmp(filter->filter->name, "afade" ) ||
  578. !strcmp(filter->filter->name, "aformat") ||
  579. !strcmp(filter->filter->name, "amix" ) ||
  580. !strcmp(filter->filter->name, "apad" ) ||
  581. !strcmp(filter->filter->name, "aphaser" ) ||
  582. !strcmp(filter->filter->name, "asplit" ) ||
  583. !strcmp(filter->filter->name, "ass") ||
  584. !strcmp(filter->filter->name, "asyncts" ) ||
  585. !strcmp(filter->filter->name, "bandpass" ) ||
  586. !strcmp(filter->filter->name, "bandreject") ||
  587. !strcmp(filter->filter->name, "bass" ) ||
  588. !strcmp(filter->filter->name, "biquad" ) ||
  589. !strcmp(filter->filter->name, "blackframe") ||
  590. !strcmp(filter->filter->name, "blend" ) ||
  591. !strcmp(filter->filter->name, "boxblur" ) ||
  592. !strcmp(filter->filter->name, "cellauto") ||
  593. !strcmp(filter->filter->name, "channelmap") ||
  594. !strcmp(filter->filter->name, "channelsplit") ||
  595. !strcmp(filter->filter->name, "color" ) ||
  596. !strcmp(filter->filter->name, "colormatrix") ||
  597. !strcmp(filter->filter->name, "concat" ) ||
  598. !strcmp(filter->filter->name, "crop" ) ||
  599. !strcmp(filter->filter->name, "cropdetect") ||
  600. !strcmp(filter->filter->name, "curves" ) ||
  601. !strcmp(filter->filter->name, "decimate" ) ||
  602. !strcmp(filter->filter->name, "delogo" ) ||
  603. !strcmp(filter->filter->name, "drawbox" ) ||
  604. !strcmp(filter->filter->name, "drawtext" ) ||
  605. !strcmp(filter->filter->name, "ebur128" ) ||
  606. !strcmp(filter->filter->name, "edgedetect") ||
  607. !strcmp(filter->filter->name, "equalizer" ) ||
  608. !strcmp(filter->filter->name, "fade" ) ||
  609. !strcmp(filter->filter->name, "field" ) ||
  610. !strcmp(filter->filter->name, "fieldorder") ||
  611. !strcmp(filter->filter->name, "fps" ) ||
  612. !strcmp(filter->filter->name, "framestep" ) ||
  613. !strcmp(filter->filter->name, "frei0r" ) ||
  614. !strcmp(filter->filter->name, "frei0r_src") ||
  615. !strcmp(filter->filter->name, "geq" ) ||
  616. !strcmp(filter->filter->name, "gradfun" ) ||
  617. !strcmp(filter->filter->name, "highpass" ) ||
  618. !strcmp(filter->filter->name, "histeq" ) ||
  619. !strcmp(filter->filter->name, "histogram" ) ||
  620. !strcmp(filter->filter->name, "hqdn3d" ) ||
  621. !strcmp(filter->filter->name, "idet" ) ||
  622. !strcmp(filter->filter->name, "il" ) ||
  623. !strcmp(filter->filter->name, "join" ) ||
  624. !strcmp(filter->filter->name, "kerndeint" ) ||
  625. !strcmp(filter->filter->name, "ocv" ) ||
  626. !strcmp(filter->filter->name, "life" ) ||
  627. !strcmp(filter->filter->name, "lut" ) ||
  628. !strcmp(filter->filter->name, "lutyuv" ) ||
  629. !strcmp(filter->filter->name, "lutrgb" ) ||
  630. !strcmp(filter->filter->name, "lowpass" ) ||
  631. !strcmp(filter->filter->name, "mandelbrot" ) ||
  632. !strcmp(filter->filter->name, "mptestsrc" ) ||
  633. !strcmp(filter->filter->name, "movie" ) ||
  634. !strcmp(filter->filter->name, "amovie" ) ||
  635. !strcmp(filter->filter->name, "negate" ) ||
  636. !strcmp(filter->filter->name, "noise" ) ||
  637. !strcmp(filter->filter->name, "nullsrc" ) ||
  638. !strcmp(filter->filter->name, "overlay" ) ||
  639. !strcmp(filter->filter->name, "pad" ) ||
  640. !strcmp(filter->filter->name, "format") ||
  641. !strcmp(filter->filter->name, "noformat") ||
  642. !strcmp(filter->filter->name, "perms") ||
  643. !strcmp(filter->filter->name, "pp" ) ||
  644. !strcmp(filter->filter->name, "aperms") ||
  645. !strcmp(filter->filter->name, "resample") ||
  646. !strcmp(filter->filter->name, "rgbtestsrc") ||
  647. !strcmp(filter->filter->name, "setpts" ) ||
  648. !strcmp(filter->filter->name, "settb" ) ||
  649. !strcmp(filter->filter->name, "showspectrum") ||
  650. !strcmp(filter->filter->name, "silencedetect") ||
  651. !strcmp(filter->filter->name, "smartblur") ||
  652. !strcmp(filter->filter->name, "split" ) ||
  653. !strcmp(filter->filter->name, "stereo3d" ) ||
  654. !strcmp(filter->filter->name, "subtitles") ||
  655. !strcmp(filter->filter->name, "testsrc" ) ||
  656. !strcmp(filter->filter->name, "thumbnail") ||
  657. !strcmp(filter->filter->name, "tile") ||
  658. !strcmp(filter->filter->name, "tinterlace") ||
  659. !strcmp(filter->filter->name, "transpose") ||
  660. !strcmp(filter->filter->name, "treble" ) ||
  661. !strcmp(filter->filter->name, "unsharp" ) ||
  662. // !strcmp(filter->filter->name, "scale" ) ||
  663. !strcmp(filter->filter->name, "select") ||
  664. !strcmp(filter->filter->name, "volume" ) ||
  665. !strcmp(filter->filter->name, "yadif" ) ||
  666. 0
  667. ;
  668. if (filter->filter->shorthand) {
  669. av_assert0(filter->priv);
  670. av_assert0(filter->filter->priv_class);
  671. *(const AVClass **)filter->priv = filter->filter->priv_class;
  672. av_opt_set_defaults(filter->priv);
  673. ret = av_opt_set_from_string(filter->priv, args,
  674. filter->filter->shorthand, "=", ":");
  675. if (ret < 0)
  676. return ret;
  677. args = NULL;
  678. }
  679. if (anton_options && args && *args) {
  680. if (!filter->filter->priv_class) {
  681. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  682. "options, but options were provided: %s.\n", args);
  683. return AVERROR(EINVAL);
  684. }
  685. #if FF_API_OLD_FILTER_OPTS
  686. if (!strcmp(filter->filter->name, "scale") &&
  687. strchr(args, ':') < strchr(args, '=')) {
  688. /* old w:h:flags=<flags> syntax */
  689. char *copy = av_strdup(args);
  690. char *p;
  691. av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
  692. "syntax is deprecated. Use either <w>:<h>:<flags> or "
  693. "w=<w>:h=<h>:flags=<flags>.\n");
  694. if (!copy) {
  695. ret = AVERROR(ENOMEM);
  696. goto fail;
  697. }
  698. p = strrchr(copy, ':');
  699. if (p) {
  700. *p++ = 0;
  701. ret = av_dict_parse_string(&options, p, "=", ":", 0);
  702. }
  703. if (ret >= 0)
  704. ret = process_options(filter, &options, copy);
  705. av_freep(&copy);
  706. if (ret < 0)
  707. goto fail;
  708. } else if (!strcmp(filter->filter->name, "format") ||
  709. !strcmp(filter->filter->name, "noformat") ||
  710. !strcmp(filter->filter->name, "frei0r") ||
  711. !strcmp(filter->filter->name, "frei0r_src") ||
  712. !strcmp(filter->filter->name, "ocv")) {
  713. /* a hack for compatibility with the old syntax
  714. * replace colons with |s */
  715. char *copy = av_strdup(args);
  716. char *p = copy;
  717. int nb_leading = 0; // number of leading colons to skip
  718. if (!copy) {
  719. ret = AVERROR(ENOMEM);
  720. goto fail;
  721. }
  722. if (!strcmp(filter->filter->name, "frei0r") ||
  723. !strcmp(filter->filter->name, "ocv"))
  724. nb_leading = 1;
  725. else if (!strcmp(filter->filter->name, "frei0r_src"))
  726. nb_leading = 3;
  727. while (nb_leading--) {
  728. p = strchr(p, ':');
  729. if (!p) {
  730. p = copy + strlen(copy);
  731. break;
  732. }
  733. p++;
  734. }
  735. if (strchr(p, ':')) {
  736. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  737. "'|' to separate the list items.\n");
  738. }
  739. while ((p = strchr(p, ':')))
  740. *p++ = '|';
  741. ret = process_options(filter, &options, copy);
  742. av_freep(&copy);
  743. if (ret < 0)
  744. goto fail;
  745. #endif
  746. } else {
  747. ret = process_options(filter, &options, args);
  748. if (ret < 0)
  749. goto fail;
  750. }
  751. }
  752. if (anton_options && filter->filter->priv_class) {
  753. ret = av_opt_set_dict(filter->priv, &options);
  754. if (ret < 0) {
  755. av_log(filter, AV_LOG_ERROR, "Error applying options to the filter.\n");
  756. goto fail;
  757. }
  758. }
  759. if (filter->filter->init_opaque)
  760. ret = filter->filter->init_opaque(filter, args, opaque);
  761. else if (filter->filter->init)
  762. ret = filter->filter->init(filter, args);
  763. else if (filter->filter->init_dict)
  764. ret = filter->filter->init_dict(filter, &options);
  765. if (ret < 0)
  766. goto fail;
  767. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  768. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  769. ret = AVERROR_OPTION_NOT_FOUND;
  770. goto fail;
  771. }
  772. fail:
  773. av_dict_free(&options);
  774. return ret;
  775. }
  776. const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
  777. {
  778. return pads[pad_idx].name;
  779. }
  780. enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
  781. {
  782. return pads[pad_idx].type;
  783. }
  784. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  785. {
  786. return ff_filter_frame(link->dst->outputs[0], frame);
  787. }
  788. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  789. {
  790. int (*filter_frame)(AVFilterLink *, AVFrame *);
  791. AVFilterPad *dst = link->dstpad;
  792. AVFrame *out;
  793. int ret;
  794. AVFilterCommand *cmd= link->dst->command_queue;
  795. int64_t pts;
  796. if (link->closed) {
  797. av_frame_free(&frame);
  798. return AVERROR_EOF;
  799. }
  800. if (!(filter_frame = dst->filter_frame))
  801. filter_frame = default_filter_frame;
  802. /* copy the frame if needed */
  803. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  804. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  805. /* Maybe use ff_copy_buffer_ref instead? */
  806. switch (link->type) {
  807. case AVMEDIA_TYPE_VIDEO:
  808. out = ff_get_video_buffer(link, link->w, link->h);
  809. break;
  810. case AVMEDIA_TYPE_AUDIO:
  811. out = ff_get_audio_buffer(link, frame->nb_samples);
  812. break;
  813. default: return AVERROR(EINVAL);
  814. }
  815. if (!out) {
  816. av_frame_free(&frame);
  817. return AVERROR(ENOMEM);
  818. }
  819. av_frame_copy_props(out, frame);
  820. switch (link->type) {
  821. case AVMEDIA_TYPE_VIDEO:
  822. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  823. frame->format, frame->width, frame->height);
  824. break;
  825. case AVMEDIA_TYPE_AUDIO:
  826. av_samples_copy(out->extended_data, frame->extended_data,
  827. 0, 0, frame->nb_samples,
  828. av_get_channel_layout_nb_channels(frame->channel_layout),
  829. frame->format);
  830. break;
  831. default: return AVERROR(EINVAL);
  832. }
  833. av_frame_free(&frame);
  834. } else
  835. out = frame;
  836. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  837. av_log(link->dst, AV_LOG_DEBUG,
  838. "Processing command time:%f command:%s arg:%s\n",
  839. cmd->time, cmd->command, cmd->arg);
  840. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  841. ff_command_queue_pop(link->dst);
  842. cmd= link->dst->command_queue;
  843. }
  844. pts = out->pts;
  845. ret = filter_frame(link, out);
  846. link->frame_requested = 0;
  847. ff_update_link_current_pts(link, pts);
  848. return ret;
  849. }
  850. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  851. {
  852. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  853. AVFrame *pbuf = link->partial_buf;
  854. int nb_channels = av_frame_get_channels(frame);
  855. int ret = 0;
  856. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  857. /* Handle framing (min_samples, max_samples) */
  858. while (insamples) {
  859. if (!pbuf) {
  860. AVRational samples_tb = { 1, link->sample_rate };
  861. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  862. if (!pbuf) {
  863. av_log(link->dst, AV_LOG_WARNING,
  864. "Samples dropped due to memory allocation failure.\n");
  865. return 0;
  866. }
  867. av_frame_copy_props(pbuf, frame);
  868. pbuf->pts = frame->pts +
  869. av_rescale_q(inpos, samples_tb, link->time_base);
  870. pbuf->nb_samples = 0;
  871. }
  872. nb_samples = FFMIN(insamples,
  873. link->partial_buf_size - pbuf->nb_samples);
  874. av_samples_copy(pbuf->extended_data, frame->extended_data,
  875. pbuf->nb_samples, inpos,
  876. nb_samples, nb_channels, link->format);
  877. inpos += nb_samples;
  878. insamples -= nb_samples;
  879. pbuf->nb_samples += nb_samples;
  880. if (pbuf->nb_samples >= link->min_samples) {
  881. ret = ff_filter_frame_framed(link, pbuf);
  882. pbuf = NULL;
  883. }
  884. }
  885. av_frame_free(&frame);
  886. link->partial_buf = pbuf;
  887. return ret;
  888. }
  889. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  890. {
  891. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  892. /* Consistency checks */
  893. if (link->type == AVMEDIA_TYPE_VIDEO) {
  894. if (strcmp(link->dst->filter->name, "scale")) {
  895. av_assert1(frame->format == link->format);
  896. av_assert1(frame->width == link->w);
  897. av_assert1(frame->height == link->h);
  898. }
  899. } else {
  900. av_assert1(frame->format == link->format);
  901. av_assert1(av_frame_get_channels(frame) == link->channels);
  902. av_assert1(frame->channel_layout == link->channel_layout);
  903. av_assert1(frame->sample_rate == link->sample_rate);
  904. }
  905. /* Go directly to actual filtering if possible */
  906. if (link->type == AVMEDIA_TYPE_AUDIO &&
  907. link->min_samples &&
  908. (link->partial_buf ||
  909. frame->nb_samples < link->min_samples ||
  910. frame->nb_samples > link->max_samples)) {
  911. return ff_filter_frame_needs_framing(link, frame);
  912. } else {
  913. return ff_filter_frame_framed(link, frame);
  914. }
  915. }
  916. const AVClass *avfilter_get_class(void)
  917. {
  918. return &avfilter_class;
  919. }