avfilter.c 38 KB

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