avfilter.c 36 KB

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