avfilter.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 AVERROR(EINVAL);
  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 AVERROR(EINVAL);
  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[] = {
  333. "t",
  334. "n",
  335. "pos",
  336. "w",
  337. "h",
  338. NULL
  339. };
  340. enum {
  341. VAR_T,
  342. VAR_N,
  343. VAR_POS,
  344. VAR_W,
  345. VAR_H,
  346. VAR_VARS_NB
  347. };
  348. static int set_enable_expr(AVFilterContext *ctx, const char *expr)
  349. {
  350. int ret;
  351. char *expr_dup;
  352. AVExpr *old = ctx->enable;
  353. if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
  354. av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
  355. "with filter '%s'\n", ctx->filter->name);
  356. return AVERROR_PATCHWELCOME;
  357. }
  358. expr_dup = av_strdup(expr);
  359. if (!expr_dup)
  360. return AVERROR(ENOMEM);
  361. if (!ctx->var_values) {
  362. ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
  363. if (!ctx->var_values) {
  364. av_free(expr_dup);
  365. return AVERROR(ENOMEM);
  366. }
  367. }
  368. ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
  369. NULL, NULL, NULL, NULL, 0, ctx->priv);
  370. if (ret < 0) {
  371. av_log(ctx->priv, AV_LOG_ERROR,
  372. "Error when evaluating the expression '%s' for enable\n",
  373. expr_dup);
  374. av_free(expr_dup);
  375. return ret;
  376. }
  377. av_expr_free(old);
  378. av_free(ctx->enable_str);
  379. ctx->enable_str = expr_dup;
  380. return 0;
  381. }
  382. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  383. {
  384. if (pts == AV_NOPTS_VALUE)
  385. return;
  386. link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  387. /* TODO use duration */
  388. if (link->graph && link->age_index >= 0)
  389. ff_avfilter_graph_update_heap(link->graph, link);
  390. }
  391. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  392. {
  393. if(!strcmp(cmd, "ping")){
  394. char local_res[256] = {0};
  395. if (!res) {
  396. res = local_res;
  397. res_len = sizeof(local_res);
  398. }
  399. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  400. if (res == local_res)
  401. av_log(filter, AV_LOG_INFO, "%s", res);
  402. return 0;
  403. }else if(!strcmp(cmd, "enable")) {
  404. return set_enable_expr(filter, arg);
  405. }else if(filter->filter->process_command) {
  406. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  407. }
  408. return AVERROR(ENOSYS);
  409. }
  410. static AVFilter *first_filter;
  411. static AVFilter **last_filter = &first_filter;
  412. #if !FF_API_NOCONST_GET_NAME
  413. const
  414. #endif
  415. AVFilter *avfilter_get_by_name(const char *name)
  416. {
  417. const AVFilter *f = NULL;
  418. if (!name)
  419. return NULL;
  420. while ((f = avfilter_next(f)))
  421. if (!strcmp(f->name, name))
  422. return (AVFilter *)f;
  423. return NULL;
  424. }
  425. int avfilter_register(AVFilter *filter)
  426. {
  427. AVFilter **f = last_filter;
  428. int i;
  429. /* the filter must select generic or internal exclusively */
  430. av_assert0((filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE) != AVFILTER_FLAG_SUPPORT_TIMELINE);
  431. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  432. const AVFilterPad *input = &filter->inputs[i];
  433. av_assert0( !input->filter_frame
  434. || (!input->start_frame && !input->end_frame));
  435. }
  436. filter->next = NULL;
  437. while(*f || avpriv_atomic_ptr_cas((void * volatile *)f, NULL, filter))
  438. f = &(*f)->next;
  439. last_filter = &filter->next;
  440. return 0;
  441. }
  442. const AVFilter *avfilter_next(const AVFilter *prev)
  443. {
  444. return prev ? prev->next : first_filter;
  445. }
  446. #if FF_API_OLD_FILTER_REGISTER
  447. AVFilter **av_filter_next(AVFilter **filter)
  448. {
  449. return filter ? &(*filter)->next : &first_filter;
  450. }
  451. void avfilter_uninit(void)
  452. {
  453. }
  454. #endif
  455. int avfilter_pad_count(const AVFilterPad *pads)
  456. {
  457. int count;
  458. if (!pads)
  459. return 0;
  460. for (count = 0; pads->name; count++)
  461. pads++;
  462. return count;
  463. }
  464. static const char *default_filter_name(void *filter_ctx)
  465. {
  466. AVFilterContext *ctx = filter_ctx;
  467. return ctx->name ? ctx->name : ctx->filter->name;
  468. }
  469. static void *filter_child_next(void *obj, void *prev)
  470. {
  471. AVFilterContext *ctx = obj;
  472. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  473. return ctx->priv;
  474. return NULL;
  475. }
  476. static const AVClass *filter_child_class_next(const AVClass *prev)
  477. {
  478. const AVFilter *f = NULL;
  479. /* find the filter that corresponds to prev */
  480. while (prev && (f = avfilter_next(f)))
  481. if (f->priv_class == prev)
  482. break;
  483. /* could not find filter corresponding to prev */
  484. if (prev && !f)
  485. return NULL;
  486. /* find next filter with specific options */
  487. while ((f = avfilter_next(f)))
  488. if (f->priv_class)
  489. return f->priv_class;
  490. return NULL;
  491. }
  492. #define OFFSET(x) offsetof(AVFilterContext, x)
  493. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
  494. static const AVOption avfilter_options[] = {
  495. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  496. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  497. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
  498. { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  499. { NULL },
  500. };
  501. static const AVClass avfilter_class = {
  502. .class_name = "AVFilter",
  503. .item_name = default_filter_name,
  504. .version = LIBAVUTIL_VERSION_INT,
  505. .category = AV_CLASS_CATEGORY_FILTER,
  506. .child_next = filter_child_next,
  507. .child_class_next = filter_child_class_next,
  508. .option = avfilter_options,
  509. };
  510. static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
  511. int *ret, int nb_jobs)
  512. {
  513. int i;
  514. for (i = 0; i < nb_jobs; i++) {
  515. int r = func(ctx, arg, i, nb_jobs);
  516. if (ret)
  517. ret[i] = r;
  518. }
  519. return 0;
  520. }
  521. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  522. {
  523. AVFilterContext *ret;
  524. if (!filter)
  525. return NULL;
  526. ret = av_mallocz(sizeof(AVFilterContext));
  527. if (!ret)
  528. return NULL;
  529. ret->av_class = &avfilter_class;
  530. ret->filter = filter;
  531. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  532. if (filter->priv_size) {
  533. ret->priv = av_mallocz(filter->priv_size);
  534. if (!ret->priv)
  535. goto err;
  536. }
  537. av_opt_set_defaults(ret);
  538. if (filter->priv_class) {
  539. *(const AVClass**)ret->priv = filter->priv_class;
  540. av_opt_set_defaults(ret->priv);
  541. }
  542. ret->internal = av_mallocz(sizeof(*ret->internal));
  543. if (!ret->internal)
  544. goto err;
  545. ret->internal->execute = default_execute;
  546. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  547. if (ret->nb_inputs ) {
  548. ret->input_pads = av_malloc_array(ret->nb_inputs, sizeof(AVFilterPad));
  549. if (!ret->input_pads)
  550. goto err;
  551. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  552. ret->inputs = av_mallocz_array(ret->nb_inputs, sizeof(AVFilterLink*));
  553. if (!ret->inputs)
  554. goto err;
  555. }
  556. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  557. if (ret->nb_outputs) {
  558. ret->output_pads = av_malloc_array(ret->nb_outputs, sizeof(AVFilterPad));
  559. if (!ret->output_pads)
  560. goto err;
  561. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  562. ret->outputs = av_mallocz_array(ret->nb_outputs, sizeof(AVFilterLink*));
  563. if (!ret->outputs)
  564. goto err;
  565. }
  566. #if FF_API_FOO_COUNT
  567. FF_DISABLE_DEPRECATION_WARNINGS
  568. ret->output_count = ret->nb_outputs;
  569. ret->input_count = ret->nb_inputs;
  570. FF_ENABLE_DEPRECATION_WARNINGS
  571. #endif
  572. return ret;
  573. err:
  574. av_freep(&ret->inputs);
  575. av_freep(&ret->input_pads);
  576. ret->nb_inputs = 0;
  577. av_freep(&ret->outputs);
  578. av_freep(&ret->output_pads);
  579. ret->nb_outputs = 0;
  580. av_freep(&ret->priv);
  581. av_freep(&ret->internal);
  582. av_free(ret);
  583. return NULL;
  584. }
  585. #if FF_API_AVFILTER_OPEN
  586. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  587. {
  588. *filter_ctx = ff_filter_alloc(filter, inst_name);
  589. return *filter_ctx ? 0 : AVERROR(ENOMEM);
  590. }
  591. #endif
  592. static void free_link(AVFilterLink *link)
  593. {
  594. if (!link)
  595. return;
  596. if (link->src)
  597. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  598. if (link->dst)
  599. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  600. ff_formats_unref(&link->in_formats);
  601. ff_formats_unref(&link->out_formats);
  602. ff_formats_unref(&link->in_samplerates);
  603. ff_formats_unref(&link->out_samplerates);
  604. ff_channel_layouts_unref(&link->in_channel_layouts);
  605. ff_channel_layouts_unref(&link->out_channel_layouts);
  606. avfilter_link_free(&link);
  607. }
  608. void avfilter_free(AVFilterContext *filter)
  609. {
  610. int i;
  611. if (!filter)
  612. return;
  613. if (filter->graph)
  614. ff_filter_graph_remove_filter(filter->graph, filter);
  615. if (filter->filter->uninit)
  616. filter->filter->uninit(filter);
  617. for (i = 0; i < filter->nb_inputs; i++) {
  618. free_link(filter->inputs[i]);
  619. }
  620. for (i = 0; i < filter->nb_outputs; i++) {
  621. free_link(filter->outputs[i]);
  622. }
  623. if (filter->filter->priv_class)
  624. av_opt_free(filter->priv);
  625. av_freep(&filter->name);
  626. av_freep(&filter->input_pads);
  627. av_freep(&filter->output_pads);
  628. av_freep(&filter->inputs);
  629. av_freep(&filter->outputs);
  630. av_freep(&filter->priv);
  631. while(filter->command_queue){
  632. ff_command_queue_pop(filter);
  633. }
  634. av_opt_free(filter);
  635. av_expr_free(filter->enable);
  636. filter->enable = NULL;
  637. av_freep(&filter->var_values);
  638. av_freep(&filter->internal);
  639. av_free(filter);
  640. }
  641. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  642. const char *args)
  643. {
  644. const AVOption *o = NULL;
  645. int ret, count = 0;
  646. char *av_uninit(parsed_key), *av_uninit(value);
  647. const char *key;
  648. int offset= -1;
  649. if (!args)
  650. return 0;
  651. while (*args) {
  652. const char *shorthand = NULL;
  653. o = av_opt_next(ctx->priv, o);
  654. if (o) {
  655. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  656. continue;
  657. offset = o->offset;
  658. shorthand = o->name;
  659. }
  660. ret = av_opt_get_key_value(&args, "=", ":",
  661. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  662. &parsed_key, &value);
  663. if (ret < 0) {
  664. if (ret == AVERROR(EINVAL))
  665. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  666. else
  667. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  668. av_err2str(ret));
  669. return ret;
  670. }
  671. if (*args)
  672. args++;
  673. if (parsed_key) {
  674. key = parsed_key;
  675. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  676. } else {
  677. key = shorthand;
  678. }
  679. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  680. if (av_opt_find(ctx, key, NULL, 0, 0)) {
  681. ret = av_opt_set(ctx, key, value, 0);
  682. if (ret < 0) {
  683. av_free(value);
  684. av_free(parsed_key);
  685. return ret;
  686. }
  687. } else {
  688. av_dict_set(options, key, value, 0);
  689. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  690. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  691. if (ret == AVERROR_OPTION_NOT_FOUND)
  692. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  693. av_free(value);
  694. av_free(parsed_key);
  695. return ret;
  696. }
  697. }
  698. }
  699. av_free(value);
  700. av_free(parsed_key);
  701. count++;
  702. }
  703. if (ctx->enable_str) {
  704. ret = set_enable_expr(ctx, ctx->enable_str);
  705. if (ret < 0)
  706. return ret;
  707. }
  708. return count;
  709. }
  710. #if FF_API_AVFILTER_INIT_FILTER
  711. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  712. {
  713. return avfilter_init_str(filter, args);
  714. }
  715. #endif
  716. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  717. {
  718. int ret = 0;
  719. ret = av_opt_set_dict(ctx, options);
  720. if (ret < 0) {
  721. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  722. return ret;
  723. }
  724. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  725. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  726. ctx->graph->internal->thread_execute) {
  727. ctx->thread_type = AVFILTER_THREAD_SLICE;
  728. ctx->internal->execute = ctx->graph->internal->thread_execute;
  729. } else {
  730. ctx->thread_type = 0;
  731. }
  732. if (ctx->filter->priv_class) {
  733. ret = av_opt_set_dict(ctx->priv, options);
  734. if (ret < 0) {
  735. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  736. return ret;
  737. }
  738. }
  739. if (ctx->filter->init_opaque)
  740. ret = ctx->filter->init_opaque(ctx, NULL);
  741. else if (ctx->filter->init)
  742. ret = ctx->filter->init(ctx);
  743. else if (ctx->filter->init_dict)
  744. ret = ctx->filter->init_dict(ctx, options);
  745. return ret;
  746. }
  747. int avfilter_init_str(AVFilterContext *filter, const char *args)
  748. {
  749. AVDictionary *options = NULL;
  750. AVDictionaryEntry *e;
  751. int ret = 0;
  752. if (args && *args) {
  753. if (!filter->filter->priv_class) {
  754. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  755. "options, but options were provided: %s.\n", args);
  756. return AVERROR(EINVAL);
  757. }
  758. #if FF_API_OLD_FILTER_OPTS
  759. if ( !strcmp(filter->filter->name, "format") ||
  760. !strcmp(filter->filter->name, "noformat") ||
  761. !strcmp(filter->filter->name, "frei0r") ||
  762. !strcmp(filter->filter->name, "frei0r_src") ||
  763. !strcmp(filter->filter->name, "ocv") ||
  764. !strcmp(filter->filter->name, "pan") ||
  765. !strcmp(filter->filter->name, "pp") ||
  766. !strcmp(filter->filter->name, "aevalsrc")) {
  767. /* a hack for compatibility with the old syntax
  768. * replace colons with |s */
  769. char *copy = av_strdup(args);
  770. char *p = copy;
  771. int nb_leading = 0; // number of leading colons to skip
  772. int deprecated = 0;
  773. if (!copy) {
  774. ret = AVERROR(ENOMEM);
  775. goto fail;
  776. }
  777. if (!strcmp(filter->filter->name, "frei0r") ||
  778. !strcmp(filter->filter->name, "ocv"))
  779. nb_leading = 1;
  780. else if (!strcmp(filter->filter->name, "frei0r_src"))
  781. nb_leading = 3;
  782. while (nb_leading--) {
  783. p = strchr(p, ':');
  784. if (!p) {
  785. p = copy + strlen(copy);
  786. break;
  787. }
  788. p++;
  789. }
  790. deprecated = strchr(p, ':') != NULL;
  791. if (!strcmp(filter->filter->name, "aevalsrc")) {
  792. deprecated = 0;
  793. while ((p = strchr(p, ':')) && p[1] != ':') {
  794. const char *epos = strchr(p + 1, '=');
  795. const char *spos = strchr(p + 1, ':');
  796. const int next_token_is_opt = epos && (!spos || epos < spos);
  797. if (next_token_is_opt) {
  798. p++;
  799. break;
  800. }
  801. /* next token does not contain a '=', assume a channel expression */
  802. deprecated = 1;
  803. *p++ = '|';
  804. }
  805. if (p && *p == ':') { // double sep '::' found
  806. deprecated = 1;
  807. memmove(p, p + 1, strlen(p));
  808. }
  809. } else
  810. while ((p = strchr(p, ':')))
  811. *p++ = '|';
  812. if (deprecated)
  813. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  814. "'|' to separate the list items.\n");
  815. av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
  816. ret = process_options(filter, &options, copy);
  817. av_freep(&copy);
  818. if (ret < 0)
  819. goto fail;
  820. #endif
  821. } else {
  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_W] = link->w;
  920. dstctx->var_values[VAR_H] = link->h;
  921. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  922. dstctx->is_disabled = fabs(av_expr_eval(dstctx->enable, dstctx->var_values, NULL)) < 0.5;
  923. if (dstctx->is_disabled &&
  924. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  925. filter_frame = default_filter_frame;
  926. }
  927. ret = filter_frame(link, out);
  928. link->frame_count++;
  929. link->frame_requested = 0;
  930. ff_update_link_current_pts(link, pts);
  931. return ret;
  932. fail:
  933. av_frame_free(&out);
  934. av_frame_free(&frame);
  935. return ret;
  936. }
  937. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  938. {
  939. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  940. AVFrame *pbuf = link->partial_buf;
  941. int nb_channels = av_frame_get_channels(frame);
  942. int ret = 0;
  943. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  944. /* Handle framing (min_samples, max_samples) */
  945. while (insamples) {
  946. if (!pbuf) {
  947. AVRational samples_tb = { 1, link->sample_rate };
  948. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  949. if (!pbuf) {
  950. av_log(link->dst, AV_LOG_WARNING,
  951. "Samples dropped due to memory allocation failure.\n");
  952. return 0;
  953. }
  954. av_frame_copy_props(pbuf, frame);
  955. pbuf->pts = frame->pts;
  956. if (pbuf->pts != AV_NOPTS_VALUE)
  957. pbuf->pts += av_rescale_q(inpos, samples_tb, link->time_base);
  958. pbuf->nb_samples = 0;
  959. }
  960. nb_samples = FFMIN(insamples,
  961. link->partial_buf_size - pbuf->nb_samples);
  962. av_samples_copy(pbuf->extended_data, frame->extended_data,
  963. pbuf->nb_samples, inpos,
  964. nb_samples, nb_channels, link->format);
  965. inpos += nb_samples;
  966. insamples -= nb_samples;
  967. pbuf->nb_samples += nb_samples;
  968. if (pbuf->nb_samples >= link->min_samples) {
  969. ret = ff_filter_frame_framed(link, pbuf);
  970. pbuf = NULL;
  971. }
  972. }
  973. av_frame_free(&frame);
  974. link->partial_buf = pbuf;
  975. return ret;
  976. }
  977. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  978. {
  979. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  980. /* Consistency checks */
  981. if (link->type == AVMEDIA_TYPE_VIDEO) {
  982. if (strcmp(link->dst->filter->name, "scale") &&
  983. strcmp(link->dst->filter->name, "idet")) {
  984. av_assert1(frame->format == link->format);
  985. av_assert1(frame->width == link->w);
  986. av_assert1(frame->height == link->h);
  987. }
  988. } else {
  989. av_assert1(frame->format == link->format);
  990. av_assert1(av_frame_get_channels(frame) == link->channels);
  991. av_assert1(frame->channel_layout == link->channel_layout);
  992. av_assert1(frame->sample_rate == link->sample_rate);
  993. }
  994. /* Go directly to actual filtering if possible */
  995. if (link->type == AVMEDIA_TYPE_AUDIO &&
  996. link->min_samples &&
  997. (link->partial_buf ||
  998. frame->nb_samples < link->min_samples ||
  999. frame->nb_samples > link->max_samples)) {
  1000. return ff_filter_frame_needs_framing(link, frame);
  1001. } else {
  1002. return ff_filter_frame_framed(link, frame);
  1003. }
  1004. }
  1005. const AVClass *avfilter_get_class(void)
  1006. {
  1007. return &avfilter_class;
  1008. }