avfilter.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. if (!link->src || !link->dst) {
  196. av_log(filter, AV_LOG_ERROR,
  197. "Not all input and output are properly linked (%d).\n", i);
  198. return AVERROR(EINVAL);
  199. }
  200. inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
  201. link->current_pts = AV_NOPTS_VALUE;
  202. switch (link->init_state) {
  203. case AVLINK_INIT:
  204. continue;
  205. case AVLINK_STARTINIT:
  206. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  207. return 0;
  208. case AVLINK_UNINIT:
  209. link->init_state = AVLINK_STARTINIT;
  210. if ((ret = avfilter_config_links(link->src)) < 0)
  211. return ret;
  212. if (!(config_link = link->srcpad->config_props)) {
  213. if (link->src->nb_inputs != 1) {
  214. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  215. "with more than one input "
  216. "must set config_props() "
  217. "callbacks on all outputs\n");
  218. return AVERROR(EINVAL);
  219. }
  220. } else if ((ret = config_link(link)) < 0) {
  221. av_log(link->src, AV_LOG_ERROR,
  222. "Failed to configure output pad on %s\n",
  223. link->src->name);
  224. return ret;
  225. }
  226. switch (link->type) {
  227. case AVMEDIA_TYPE_VIDEO:
  228. if (!link->time_base.num && !link->time_base.den)
  229. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  230. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  231. link->sample_aspect_ratio = inlink ?
  232. inlink->sample_aspect_ratio : (AVRational){1,1};
  233. if (inlink && !link->frame_rate.num && !link->frame_rate.den)
  234. link->frame_rate = inlink->frame_rate;
  235. if (inlink) {
  236. if (!link->w)
  237. link->w = inlink->w;
  238. if (!link->h)
  239. link->h = inlink->h;
  240. } else if (!link->w || !link->h) {
  241. av_log(link->src, AV_LOG_ERROR,
  242. "Video source filters must set their output link's "
  243. "width and height\n");
  244. return AVERROR(EINVAL);
  245. }
  246. break;
  247. case AVMEDIA_TYPE_AUDIO:
  248. if (inlink) {
  249. if (!link->time_base.num && !link->time_base.den)
  250. link->time_base = inlink->time_base;
  251. }
  252. if (!link->time_base.num && !link->time_base.den)
  253. link->time_base = (AVRational) {1, link->sample_rate};
  254. }
  255. if ((config_link = link->dstpad->config_props))
  256. if ((ret = config_link(link)) < 0) {
  257. av_log(link->dst, AV_LOG_ERROR,
  258. "Failed to configure input pad on %s\n",
  259. link->dst->name);
  260. return ret;
  261. }
  262. link->init_state = AVLINK_INIT;
  263. }
  264. }
  265. return 0;
  266. }
  267. void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
  268. {
  269. if (link->type == AVMEDIA_TYPE_VIDEO) {
  270. ff_tlog(ctx,
  271. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  272. link, link->w, link->h,
  273. av_get_pix_fmt_name(link->format),
  274. link->src ? link->src->filter->name : "",
  275. link->dst ? link->dst->filter->name : "",
  276. end ? "\n" : "");
  277. } else {
  278. char buf[128];
  279. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  280. ff_tlog(ctx,
  281. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  282. link, (int)link->sample_rate, buf,
  283. av_get_sample_fmt_name(link->format),
  284. link->src ? link->src->filter->name : "",
  285. link->dst ? link->dst->filter->name : "",
  286. end ? "\n" : "");
  287. }
  288. }
  289. int ff_request_frame(AVFilterLink *link)
  290. {
  291. int ret = -1;
  292. FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
  293. if (link->closed)
  294. return AVERROR_EOF;
  295. av_assert0(!link->frame_requested);
  296. link->frame_requested = 1;
  297. while (link->frame_requested) {
  298. if (link->srcpad->request_frame)
  299. ret = link->srcpad->request_frame(link);
  300. else if (link->src->inputs[0])
  301. ret = ff_request_frame(link->src->inputs[0]);
  302. if (ret == AVERROR_EOF && link->partial_buf) {
  303. AVFrame *pbuf = link->partial_buf;
  304. link->partial_buf = NULL;
  305. ret = ff_filter_frame_framed(link, pbuf);
  306. }
  307. if (ret < 0) {
  308. link->frame_requested = 0;
  309. if (ret == AVERROR_EOF)
  310. link->closed = 1;
  311. } else {
  312. av_assert0(!link->frame_requested ||
  313. link->flags & FF_LINK_FLAG_REQUEST_LOOP);
  314. }
  315. }
  316. return ret;
  317. }
  318. int ff_poll_frame(AVFilterLink *link)
  319. {
  320. int i, min = INT_MAX;
  321. if (link->srcpad->poll_frame)
  322. return link->srcpad->poll_frame(link);
  323. for (i = 0; i < link->src->nb_inputs; i++) {
  324. int val;
  325. if (!link->src->inputs[i])
  326. return -1;
  327. val = ff_poll_frame(link->src->inputs[i]);
  328. min = FFMIN(min, val);
  329. }
  330. return min;
  331. }
  332. static const char *const var_names[] = { "t", "n", "pos", NULL };
  333. enum { VAR_T, VAR_N, VAR_POS, VAR_VARS_NB };
  334. static int set_enable_expr(AVFilterContext *ctx, const char *expr)
  335. {
  336. int ret;
  337. char *expr_dup;
  338. AVExpr *old = ctx->enable;
  339. if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
  340. av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
  341. "with filter '%s'\n", ctx->filter->name);
  342. return AVERROR_PATCHWELCOME;
  343. }
  344. expr_dup = av_strdup(expr);
  345. if (!expr_dup)
  346. return AVERROR(ENOMEM);
  347. if (!ctx->var_values) {
  348. ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
  349. if (!ctx->var_values) {
  350. av_free(expr_dup);
  351. return AVERROR(ENOMEM);
  352. }
  353. }
  354. ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
  355. NULL, NULL, NULL, NULL, 0, ctx->priv);
  356. if (ret < 0) {
  357. av_log(ctx->priv, AV_LOG_ERROR,
  358. "Error when evaluating the expression '%s' for enable\n",
  359. expr_dup);
  360. av_free(expr_dup);
  361. return ret;
  362. }
  363. av_expr_free(old);
  364. av_free(ctx->enable_str);
  365. ctx->enable_str = expr_dup;
  366. return 0;
  367. }
  368. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  369. {
  370. if (pts == AV_NOPTS_VALUE)
  371. return;
  372. link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  373. /* TODO use duration */
  374. if (link->graph && link->age_index >= 0)
  375. ff_avfilter_graph_update_heap(link->graph, link);
  376. }
  377. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  378. {
  379. if(!strcmp(cmd, "ping")){
  380. char local_res[256] = {0};
  381. if (!res) {
  382. res = local_res;
  383. res_len = sizeof(local_res);
  384. }
  385. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  386. if (res == local_res)
  387. av_log(filter, AV_LOG_INFO, "%s", res);
  388. return 0;
  389. }else if(!strcmp(cmd, "enable")) {
  390. return set_enable_expr(filter, arg);
  391. }else if(filter->filter->process_command) {
  392. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  393. }
  394. return AVERROR(ENOSYS);
  395. }
  396. static AVFilter *first_filter;
  397. static AVFilter **last_filter = &first_filter;
  398. #if !FF_API_NOCONST_GET_NAME
  399. const
  400. #endif
  401. AVFilter *avfilter_get_by_name(const char *name)
  402. {
  403. const AVFilter *f = NULL;
  404. if (!name)
  405. return NULL;
  406. while ((f = avfilter_next(f)))
  407. if (!strcmp(f->name, name))
  408. return (AVFilter *)f;
  409. return NULL;
  410. }
  411. int avfilter_register(AVFilter *filter)
  412. {
  413. AVFilter **f = last_filter;
  414. int i;
  415. /* the filter must select generic or internal exclusively */
  416. av_assert0((filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE) != AVFILTER_FLAG_SUPPORT_TIMELINE);
  417. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  418. const AVFilterPad *input = &filter->inputs[i];
  419. av_assert0( !input->filter_frame
  420. || (!input->start_frame && !input->end_frame));
  421. }
  422. filter->next = NULL;
  423. while(*f || avpriv_atomic_ptr_cas((void * volatile *)f, NULL, filter))
  424. f = &(*f)->next;
  425. last_filter = &filter->next;
  426. return 0;
  427. }
  428. const AVFilter *avfilter_next(const AVFilter *prev)
  429. {
  430. return prev ? prev->next : first_filter;
  431. }
  432. #if FF_API_OLD_FILTER_REGISTER
  433. AVFilter **av_filter_next(AVFilter **filter)
  434. {
  435. return filter ? &(*filter)->next : &first_filter;
  436. }
  437. void avfilter_uninit(void)
  438. {
  439. }
  440. #endif
  441. int avfilter_pad_count(const AVFilterPad *pads)
  442. {
  443. int count;
  444. if (!pads)
  445. return 0;
  446. for (count = 0; pads->name; count++)
  447. pads++;
  448. return count;
  449. }
  450. static const char *default_filter_name(void *filter_ctx)
  451. {
  452. AVFilterContext *ctx = filter_ctx;
  453. return ctx->name ? ctx->name : ctx->filter->name;
  454. }
  455. static void *filter_child_next(void *obj, void *prev)
  456. {
  457. AVFilterContext *ctx = obj;
  458. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  459. return ctx->priv;
  460. return NULL;
  461. }
  462. static const AVClass *filter_child_class_next(const AVClass *prev)
  463. {
  464. const AVFilter *f = NULL;
  465. /* find the filter that corresponds to prev */
  466. while (prev && (f = avfilter_next(f)))
  467. if (f->priv_class == prev)
  468. break;
  469. /* could not find filter corresponding to prev */
  470. if (prev && !f)
  471. return NULL;
  472. /* find next filter with specific options */
  473. while ((f = avfilter_next(f)))
  474. if (f->priv_class)
  475. return f->priv_class;
  476. return NULL;
  477. }
  478. #define OFFSET(x) offsetof(AVFilterContext, x)
  479. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
  480. static const AVOption avfilter_options[] = {
  481. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  482. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  483. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
  484. { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  485. { NULL },
  486. };
  487. static const AVClass avfilter_class = {
  488. .class_name = "AVFilter",
  489. .item_name = default_filter_name,
  490. .version = LIBAVUTIL_VERSION_INT,
  491. .category = AV_CLASS_CATEGORY_FILTER,
  492. .child_next = filter_child_next,
  493. .child_class_next = filter_child_class_next,
  494. .option = avfilter_options,
  495. };
  496. static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
  497. int *ret, int nb_jobs)
  498. {
  499. int i;
  500. for (i = 0; i < nb_jobs; i++) {
  501. int r = func(ctx, arg, i, nb_jobs);
  502. if (ret)
  503. ret[i] = r;
  504. }
  505. return 0;
  506. }
  507. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  508. {
  509. AVFilterContext *ret;
  510. if (!filter)
  511. return NULL;
  512. ret = av_mallocz(sizeof(AVFilterContext));
  513. if (!ret)
  514. return NULL;
  515. ret->av_class = &avfilter_class;
  516. ret->filter = filter;
  517. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  518. if (filter->priv_size) {
  519. ret->priv = av_mallocz(filter->priv_size);
  520. if (!ret->priv)
  521. goto err;
  522. }
  523. av_opt_set_defaults(ret);
  524. if (filter->priv_class) {
  525. *(const AVClass**)ret->priv = filter->priv_class;
  526. av_opt_set_defaults(ret->priv);
  527. }
  528. ret->internal = av_mallocz(sizeof(*ret->internal));
  529. if (!ret->internal)
  530. goto err;
  531. ret->internal->execute = default_execute;
  532. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  533. if (ret->nb_inputs ) {
  534. ret->input_pads = av_malloc_array(ret->nb_inputs, sizeof(AVFilterPad));
  535. if (!ret->input_pads)
  536. goto err;
  537. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  538. ret->inputs = av_mallocz_array(ret->nb_inputs, sizeof(AVFilterLink*));
  539. if (!ret->inputs)
  540. goto err;
  541. }
  542. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  543. if (ret->nb_outputs) {
  544. ret->output_pads = av_malloc_array(ret->nb_outputs, sizeof(AVFilterPad));
  545. if (!ret->output_pads)
  546. goto err;
  547. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  548. ret->outputs = av_mallocz_array(ret->nb_outputs, sizeof(AVFilterLink*));
  549. if (!ret->outputs)
  550. goto err;
  551. }
  552. #if FF_API_FOO_COUNT
  553. FF_DISABLE_DEPRECATION_WARNINGS
  554. ret->output_count = ret->nb_outputs;
  555. ret->input_count = ret->nb_inputs;
  556. FF_ENABLE_DEPRECATION_WARNINGS
  557. #endif
  558. return ret;
  559. err:
  560. av_freep(&ret->inputs);
  561. av_freep(&ret->input_pads);
  562. ret->nb_inputs = 0;
  563. av_freep(&ret->outputs);
  564. av_freep(&ret->output_pads);
  565. ret->nb_outputs = 0;
  566. av_freep(&ret->priv);
  567. av_freep(&ret->internal);
  568. av_free(ret);
  569. return NULL;
  570. }
  571. #if FF_API_AVFILTER_OPEN
  572. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  573. {
  574. *filter_ctx = ff_filter_alloc(filter, inst_name);
  575. return *filter_ctx ? 0 : AVERROR(ENOMEM);
  576. }
  577. #endif
  578. static void free_link(AVFilterLink *link)
  579. {
  580. if (!link)
  581. return;
  582. if (link->src)
  583. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  584. if (link->dst)
  585. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  586. ff_formats_unref(&link->in_formats);
  587. ff_formats_unref(&link->out_formats);
  588. ff_formats_unref(&link->in_samplerates);
  589. ff_formats_unref(&link->out_samplerates);
  590. ff_channel_layouts_unref(&link->in_channel_layouts);
  591. ff_channel_layouts_unref(&link->out_channel_layouts);
  592. avfilter_link_free(&link);
  593. }
  594. void avfilter_free(AVFilterContext *filter)
  595. {
  596. int i;
  597. if (!filter)
  598. return;
  599. if (filter->graph)
  600. ff_filter_graph_remove_filter(filter->graph, filter);
  601. if (filter->filter->uninit)
  602. filter->filter->uninit(filter);
  603. for (i = 0; i < filter->nb_inputs; i++) {
  604. free_link(filter->inputs[i]);
  605. }
  606. for (i = 0; i < filter->nb_outputs; i++) {
  607. free_link(filter->outputs[i]);
  608. }
  609. if (filter->filter->priv_class)
  610. av_opt_free(filter->priv);
  611. av_freep(&filter->name);
  612. av_freep(&filter->input_pads);
  613. av_freep(&filter->output_pads);
  614. av_freep(&filter->inputs);
  615. av_freep(&filter->outputs);
  616. av_freep(&filter->priv);
  617. while(filter->command_queue){
  618. ff_command_queue_pop(filter);
  619. }
  620. av_opt_free(filter);
  621. av_expr_free(filter->enable);
  622. filter->enable = NULL;
  623. av_freep(&filter->var_values);
  624. av_freep(&filter->internal);
  625. av_free(filter);
  626. }
  627. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  628. const char *args)
  629. {
  630. const AVOption *o = NULL;
  631. int ret, count = 0;
  632. char *av_uninit(parsed_key), *av_uninit(value);
  633. const char *key;
  634. int offset= -1;
  635. if (!args)
  636. return 0;
  637. while (*args) {
  638. const char *shorthand = NULL;
  639. o = av_opt_next(ctx->priv, o);
  640. if (o) {
  641. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  642. continue;
  643. offset = o->offset;
  644. shorthand = o->name;
  645. }
  646. ret = av_opt_get_key_value(&args, "=", ":",
  647. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  648. &parsed_key, &value);
  649. if (ret < 0) {
  650. if (ret == AVERROR(EINVAL))
  651. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  652. else
  653. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  654. av_err2str(ret));
  655. return ret;
  656. }
  657. if (*args)
  658. args++;
  659. if (parsed_key) {
  660. key = parsed_key;
  661. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  662. } else {
  663. key = shorthand;
  664. }
  665. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  666. if (av_opt_find(ctx, key, NULL, 0, 0)) {
  667. ret = av_opt_set(ctx, key, value, 0);
  668. if (ret < 0) {
  669. av_free(value);
  670. av_free(parsed_key);
  671. return ret;
  672. }
  673. } else {
  674. av_dict_set(options, key, value, 0);
  675. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  676. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  677. if (ret == AVERROR_OPTION_NOT_FOUND)
  678. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  679. av_free(value);
  680. av_free(parsed_key);
  681. return ret;
  682. }
  683. }
  684. }
  685. av_free(value);
  686. av_free(parsed_key);
  687. count++;
  688. }
  689. if (ctx->enable_str) {
  690. ret = set_enable_expr(ctx, ctx->enable_str);
  691. if (ret < 0)
  692. return ret;
  693. }
  694. return count;
  695. }
  696. #if FF_API_AVFILTER_INIT_FILTER
  697. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  698. {
  699. return avfilter_init_str(filter, args);
  700. }
  701. #endif
  702. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  703. {
  704. int ret = 0;
  705. ret = av_opt_set_dict(ctx, options);
  706. if (ret < 0) {
  707. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  708. return ret;
  709. }
  710. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  711. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  712. ctx->graph->internal->thread_execute) {
  713. ctx->thread_type = AVFILTER_THREAD_SLICE;
  714. ctx->internal->execute = ctx->graph->internal->thread_execute;
  715. } else {
  716. ctx->thread_type = 0;
  717. }
  718. if (ctx->filter->priv_class) {
  719. ret = av_opt_set_dict(ctx->priv, options);
  720. if (ret < 0) {
  721. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  722. return ret;
  723. }
  724. }
  725. if (ctx->filter->init_opaque)
  726. ret = ctx->filter->init_opaque(ctx, NULL);
  727. else if (ctx->filter->init)
  728. ret = ctx->filter->init(ctx);
  729. else if (ctx->filter->init_dict)
  730. ret = ctx->filter->init_dict(ctx, options);
  731. return ret;
  732. }
  733. int avfilter_init_str(AVFilterContext *filter, const char *args)
  734. {
  735. AVDictionary *options = NULL;
  736. AVDictionaryEntry *e;
  737. int ret = 0;
  738. if (args && *args) {
  739. if (!filter->filter->priv_class) {
  740. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  741. "options, but options were provided: %s.\n", args);
  742. return AVERROR(EINVAL);
  743. }
  744. #if FF_API_OLD_FILTER_OPTS
  745. if ( !strcmp(filter->filter->name, "format") ||
  746. !strcmp(filter->filter->name, "noformat") ||
  747. !strcmp(filter->filter->name, "frei0r") ||
  748. !strcmp(filter->filter->name, "frei0r_src") ||
  749. !strcmp(filter->filter->name, "ocv") ||
  750. !strcmp(filter->filter->name, "pan") ||
  751. !strcmp(filter->filter->name, "pp") ||
  752. !strcmp(filter->filter->name, "aevalsrc")) {
  753. /* a hack for compatibility with the old syntax
  754. * replace colons with |s */
  755. char *copy = av_strdup(args);
  756. char *p = copy;
  757. int nb_leading = 0; // number of leading colons to skip
  758. int deprecated = 0;
  759. if (!copy) {
  760. ret = AVERROR(ENOMEM);
  761. goto fail;
  762. }
  763. if (!strcmp(filter->filter->name, "frei0r") ||
  764. !strcmp(filter->filter->name, "ocv"))
  765. nb_leading = 1;
  766. else if (!strcmp(filter->filter->name, "frei0r_src"))
  767. nb_leading = 3;
  768. while (nb_leading--) {
  769. p = strchr(p, ':');
  770. if (!p) {
  771. p = copy + strlen(copy);
  772. break;
  773. }
  774. p++;
  775. }
  776. deprecated = strchr(p, ':') != NULL;
  777. if (!strcmp(filter->filter->name, "aevalsrc")) {
  778. deprecated = 0;
  779. while ((p = strchr(p, ':')) && p[1] != ':') {
  780. const char *epos = strchr(p + 1, '=');
  781. const char *spos = strchr(p + 1, ':');
  782. const int next_token_is_opt = epos && (!spos || epos < spos);
  783. if (next_token_is_opt) {
  784. p++;
  785. break;
  786. }
  787. /* next token does not contain a '=', assume a channel expression */
  788. deprecated = 1;
  789. *p++ = '|';
  790. }
  791. if (p && *p == ':') { // double sep '::' found
  792. deprecated = 1;
  793. memmove(p, p + 1, strlen(p));
  794. }
  795. } else
  796. while ((p = strchr(p, ':')))
  797. *p++ = '|';
  798. if (deprecated)
  799. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  800. "'|' to separate the list items.\n");
  801. av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
  802. ret = process_options(filter, &options, copy);
  803. av_freep(&copy);
  804. if (ret < 0)
  805. goto fail;
  806. #endif
  807. } else {
  808. #if CONFIG_MP_FILTER
  809. if (!strcmp(filter->filter->name, "mp")) {
  810. char *escaped;
  811. if (!strncmp(args, "filter=", 7))
  812. args += 7;
  813. ret = av_escape(&escaped, args, ":=", AV_ESCAPE_MODE_BACKSLASH, 0);
  814. if (ret < 0) {
  815. av_log(filter, AV_LOG_ERROR, "Unable to escape MPlayer filters arg '%s'\n", args);
  816. goto fail;
  817. }
  818. ret = process_options(filter, &options, escaped);
  819. av_free(escaped);
  820. } else
  821. #endif
  822. ret = process_options(filter, &options, args);
  823. if (ret < 0)
  824. goto fail;
  825. }
  826. }
  827. ret = avfilter_init_dict(filter, &options);
  828. if (ret < 0)
  829. goto fail;
  830. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  831. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  832. ret = AVERROR_OPTION_NOT_FOUND;
  833. goto fail;
  834. }
  835. fail:
  836. av_dict_free(&options);
  837. return ret;
  838. }
  839. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  840. {
  841. return pads[pad_idx].name;
  842. }
  843. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  844. {
  845. return pads[pad_idx].type;
  846. }
  847. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  848. {
  849. return ff_filter_frame(link->dst->outputs[0], frame);
  850. }
  851. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  852. {
  853. int (*filter_frame)(AVFilterLink *, AVFrame *);
  854. AVFilterContext *dstctx = link->dst;
  855. AVFilterPad *dst = link->dstpad;
  856. AVFrame *out = NULL;
  857. int ret;
  858. AVFilterCommand *cmd= link->dst->command_queue;
  859. int64_t pts;
  860. if (link->closed) {
  861. av_frame_free(&frame);
  862. return AVERROR_EOF;
  863. }
  864. if (!(filter_frame = dst->filter_frame))
  865. filter_frame = default_filter_frame;
  866. /* copy the frame if needed */
  867. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  868. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  869. /* Maybe use ff_copy_buffer_ref instead? */
  870. switch (link->type) {
  871. case AVMEDIA_TYPE_VIDEO:
  872. out = ff_get_video_buffer(link, link->w, link->h);
  873. break;
  874. case AVMEDIA_TYPE_AUDIO:
  875. out = ff_get_audio_buffer(link, frame->nb_samples);
  876. break;
  877. default:
  878. ret = AVERROR(EINVAL);
  879. goto fail;
  880. }
  881. if (!out) {
  882. ret = AVERROR(ENOMEM);
  883. goto fail;
  884. }
  885. ret = av_frame_copy_props(out, frame);
  886. if (ret < 0)
  887. goto fail;
  888. switch (link->type) {
  889. case AVMEDIA_TYPE_VIDEO:
  890. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  891. frame->format, frame->width, frame->height);
  892. break;
  893. case AVMEDIA_TYPE_AUDIO:
  894. av_samples_copy(out->extended_data, frame->extended_data,
  895. 0, 0, frame->nb_samples,
  896. av_get_channel_layout_nb_channels(frame->channel_layout),
  897. frame->format);
  898. break;
  899. default:
  900. ret = AVERROR(EINVAL);
  901. goto fail;
  902. }
  903. av_frame_free(&frame);
  904. } else
  905. out = frame;
  906. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  907. av_log(link->dst, AV_LOG_DEBUG,
  908. "Processing command time:%f command:%s arg:%s\n",
  909. cmd->time, cmd->command, cmd->arg);
  910. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  911. ff_command_queue_pop(link->dst);
  912. cmd= link->dst->command_queue;
  913. }
  914. pts = out->pts;
  915. if (dstctx->enable_str) {
  916. int64_t pos = av_frame_get_pkt_pos(out);
  917. dstctx->var_values[VAR_N] = link->frame_count;
  918. dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
  919. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  920. dstctx->is_disabled = fabs(av_expr_eval(dstctx->enable, dstctx->var_values, NULL)) < 0.5;
  921. if (dstctx->is_disabled &&
  922. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  923. filter_frame = default_filter_frame;
  924. }
  925. ret = filter_frame(link, out);
  926. link->frame_count++;
  927. link->frame_requested = 0;
  928. ff_update_link_current_pts(link, pts);
  929. return ret;
  930. fail:
  931. av_frame_free(&out);
  932. av_frame_free(&frame);
  933. return ret;
  934. }
  935. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  936. {
  937. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  938. AVFrame *pbuf = link->partial_buf;
  939. int nb_channels = av_frame_get_channels(frame);
  940. int ret = 0;
  941. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  942. /* Handle framing (min_samples, max_samples) */
  943. while (insamples) {
  944. if (!pbuf) {
  945. AVRational samples_tb = { 1, link->sample_rate };
  946. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  947. if (!pbuf) {
  948. av_log(link->dst, AV_LOG_WARNING,
  949. "Samples dropped due to memory allocation failure.\n");
  950. return 0;
  951. }
  952. av_frame_copy_props(pbuf, frame);
  953. pbuf->pts = frame->pts;
  954. if (pbuf->pts != AV_NOPTS_VALUE)
  955. pbuf->pts += av_rescale_q(inpos, samples_tb, link->time_base);
  956. pbuf->nb_samples = 0;
  957. }
  958. nb_samples = FFMIN(insamples,
  959. link->partial_buf_size - pbuf->nb_samples);
  960. av_samples_copy(pbuf->extended_data, frame->extended_data,
  961. pbuf->nb_samples, inpos,
  962. nb_samples, nb_channels, link->format);
  963. inpos += nb_samples;
  964. insamples -= nb_samples;
  965. pbuf->nb_samples += nb_samples;
  966. if (pbuf->nb_samples >= link->min_samples) {
  967. ret = ff_filter_frame_framed(link, pbuf);
  968. pbuf = NULL;
  969. }
  970. }
  971. av_frame_free(&frame);
  972. link->partial_buf = pbuf;
  973. return ret;
  974. }
  975. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  976. {
  977. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  978. /* Consistency checks */
  979. if (link->type == AVMEDIA_TYPE_VIDEO) {
  980. if (strcmp(link->dst->filter->name, "scale")) {
  981. av_assert1(frame->format == link->format);
  982. av_assert1(frame->width == link->w);
  983. av_assert1(frame->height == link->h);
  984. }
  985. } else {
  986. av_assert1(frame->format == link->format);
  987. av_assert1(av_frame_get_channels(frame) == link->channels);
  988. av_assert1(frame->channel_layout == link->channel_layout);
  989. av_assert1(frame->sample_rate == link->sample_rate);
  990. }
  991. /* Go directly to actual filtering if possible */
  992. if (link->type == AVMEDIA_TYPE_AUDIO &&
  993. link->min_samples &&
  994. (link->partial_buf ||
  995. frame->nb_samples < link->min_samples ||
  996. frame->nb_samples > link->max_samples)) {
  997. return ff_filter_frame_needs_framing(link, frame);
  998. } else {
  999. return ff_filter_frame_framed(link, frame);
  1000. }
  1001. }
  1002. const AVClass *avfilter_get_class(void)
  1003. {
  1004. return &avfilter_class;
  1005. }