avfilter.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/buffer.h"
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/hwcontext.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/rational.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/thread.h"
  35. #define FF_INTERNAL_FIELDS 1
  36. #include "framequeue.h"
  37. #include "audio.h"
  38. #include "avfilter.h"
  39. #include "filters.h"
  40. #include "formats.h"
  41. #include "internal.h"
  42. #include "libavutil/ffversion.h"
  43. const char av_filter_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  44. void ff_tlog_ref(void *ctx, AVFrame *ref, int end)
  45. {
  46. av_unused char buf[16];
  47. ff_tlog(ctx,
  48. "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  49. ref, ref->buf, ref->data[0],
  50. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  51. ref->pts, ref->pkt_pos);
  52. if (ref->width) {
  53. ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  54. ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
  55. ref->width, ref->height,
  56. !ref->interlaced_frame ? 'P' : /* Progressive */
  57. ref->top_field_first ? 'T' : 'B', /* Top / Bottom */
  58. ref->key_frame,
  59. av_get_picture_type_char(ref->pict_type));
  60. }
  61. if (ref->nb_samples) {
  62. ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  63. ref->channel_layout,
  64. ref->nb_samples,
  65. ref->sample_rate);
  66. }
  67. ff_tlog(ctx, "]%s", end ? "\n" : "");
  68. }
  69. unsigned avfilter_version(void)
  70. {
  71. av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
  72. return LIBAVFILTER_VERSION_INT;
  73. }
  74. const char *avfilter_configuration(void)
  75. {
  76. return FFMPEG_CONFIGURATION;
  77. }
  78. const char *avfilter_license(void)
  79. {
  80. #define LICENSE_PREFIX "libavfilter license: "
  81. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  82. }
  83. void ff_command_queue_pop(AVFilterContext *filter)
  84. {
  85. AVFilterCommand *c= filter->command_queue;
  86. av_freep(&c->arg);
  87. av_freep(&c->command);
  88. filter->command_queue= c->next;
  89. av_free(c);
  90. }
  91. int ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  92. AVFilterPad **pads, AVFilterLink ***links,
  93. AVFilterPad *newpad)
  94. {
  95. AVFilterLink **newlinks;
  96. AVFilterPad *newpads;
  97. unsigned i;
  98. idx = FFMIN(idx, *count);
  99. newpads = av_realloc_array(*pads, *count + 1, sizeof(AVFilterPad));
  100. newlinks = av_realloc_array(*links, *count + 1, sizeof(AVFilterLink*));
  101. if (newpads)
  102. *pads = newpads;
  103. if (newlinks)
  104. *links = newlinks;
  105. if (!newpads || !newlinks)
  106. return AVERROR(ENOMEM);
  107. memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
  108. memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
  109. memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
  110. (*links)[idx] = NULL;
  111. (*count)++;
  112. for (i = idx + 1; i < *count; i++)
  113. if ((*links)[i])
  114. (*(unsigned *)((uint8_t *) (*links)[i] + padidx_off))++;
  115. return 0;
  116. }
  117. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  118. AVFilterContext *dst, unsigned dstpad)
  119. {
  120. AVFilterLink *link;
  121. av_assert0(src->graph);
  122. av_assert0(dst->graph);
  123. av_assert0(src->graph == dst->graph);
  124. if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
  125. src->outputs[srcpad] || dst->inputs[dstpad])
  126. return AVERROR(EINVAL);
  127. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  128. av_log(src, AV_LOG_ERROR,
  129. "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
  130. src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
  131. dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
  132. return AVERROR(EINVAL);
  133. }
  134. link = av_mallocz(sizeof(*link));
  135. if (!link)
  136. return AVERROR(ENOMEM);
  137. src->outputs[srcpad] = dst->inputs[dstpad] = link;
  138. link->src = src;
  139. link->dst = dst;
  140. link->srcpad = &src->output_pads[srcpad];
  141. link->dstpad = &dst->input_pads[dstpad];
  142. link->type = src->output_pads[srcpad].type;
  143. av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  144. link->format = -1;
  145. ff_framequeue_init(&link->fifo, &src->graph->internal->frame_queues);
  146. return 0;
  147. }
  148. void avfilter_link_free(AVFilterLink **link)
  149. {
  150. if (!*link)
  151. return;
  152. av_frame_free(&(*link)->partial_buf);
  153. ff_framequeue_free(&(*link)->fifo);
  154. ff_frame_pool_uninit((FFFramePool**)&(*link)->frame_pool);
  155. av_freep(link);
  156. }
  157. #if FF_API_FILTER_GET_SET
  158. int avfilter_link_get_channels(AVFilterLink *link)
  159. {
  160. return link->channels;
  161. }
  162. #endif
  163. void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
  164. {
  165. filter->ready = FFMAX(filter->ready, priority);
  166. }
  167. /**
  168. * Clear frame_blocked_in on all outputs.
  169. * This is necessary whenever something changes on input.
  170. */
  171. static void filter_unblock(AVFilterContext *filter)
  172. {
  173. unsigned i;
  174. for (i = 0; i < filter->nb_outputs; i++)
  175. filter->outputs[i]->frame_blocked_in = 0;
  176. }
  177. void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts)
  178. {
  179. if (link->status_in == status)
  180. return;
  181. av_assert0(!link->status_in);
  182. link->status_in = status;
  183. link->status_in_pts = pts;
  184. link->frame_wanted_out = 0;
  185. link->frame_blocked_in = 0;
  186. filter_unblock(link->dst);
  187. ff_filter_set_ready(link->dst, 200);
  188. }
  189. void ff_avfilter_link_set_out_status(AVFilterLink *link, int status, int64_t pts)
  190. {
  191. av_assert0(!link->frame_wanted_out);
  192. av_assert0(!link->status_out);
  193. link->status_out = status;
  194. if (pts != AV_NOPTS_VALUE)
  195. ff_update_link_current_pts(link, pts);
  196. filter_unblock(link->dst);
  197. ff_filter_set_ready(link->src, 200);
  198. }
  199. void avfilter_link_set_closed(AVFilterLink *link, int closed)
  200. {
  201. ff_avfilter_link_set_out_status(link, closed ? AVERROR_EOF : 0, AV_NOPTS_VALUE);
  202. }
  203. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  204. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  205. {
  206. int ret;
  207. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  208. av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
  209. "between the filter '%s' and the filter '%s'\n",
  210. filt->name, link->src->name, link->dst->name);
  211. link->dst->inputs[dstpad_idx] = NULL;
  212. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  213. /* failed to link output filter to new filter */
  214. link->dst->inputs[dstpad_idx] = link;
  215. return ret;
  216. }
  217. /* re-hookup the link to the new destination filter we inserted */
  218. link->dst = filt;
  219. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  220. filt->inputs[filt_srcpad_idx] = link;
  221. /* if any information on supported media formats already exists on the
  222. * link, we need to preserve that */
  223. if (link->out_formats)
  224. ff_formats_changeref(&link->out_formats,
  225. &filt->outputs[filt_dstpad_idx]->out_formats);
  226. if (link->out_samplerates)
  227. ff_formats_changeref(&link->out_samplerates,
  228. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  229. if (link->out_channel_layouts)
  230. ff_channel_layouts_changeref(&link->out_channel_layouts,
  231. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  232. return 0;
  233. }
  234. int avfilter_config_links(AVFilterContext *filter)
  235. {
  236. int (*config_link)(AVFilterLink *);
  237. unsigned i;
  238. int ret;
  239. for (i = 0; i < filter->nb_inputs; i ++) {
  240. AVFilterLink *link = filter->inputs[i];
  241. AVFilterLink *inlink;
  242. if (!link) continue;
  243. if (!link->src || !link->dst) {
  244. av_log(filter, AV_LOG_ERROR,
  245. "Not all input and output are properly linked (%d).\n", i);
  246. return AVERROR(EINVAL);
  247. }
  248. inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
  249. link->current_pts =
  250. link->current_pts_us = AV_NOPTS_VALUE;
  251. switch (link->init_state) {
  252. case AVLINK_INIT:
  253. continue;
  254. case AVLINK_STARTINIT:
  255. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  256. return 0;
  257. case AVLINK_UNINIT:
  258. link->init_state = AVLINK_STARTINIT;
  259. if ((ret = avfilter_config_links(link->src)) < 0)
  260. return ret;
  261. if (!(config_link = link->srcpad->config_props)) {
  262. if (link->src->nb_inputs != 1) {
  263. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  264. "with more than one input "
  265. "must set config_props() "
  266. "callbacks on all outputs\n");
  267. return AVERROR(EINVAL);
  268. }
  269. } else if ((ret = config_link(link)) < 0) {
  270. av_log(link->src, AV_LOG_ERROR,
  271. "Failed to configure output pad on %s\n",
  272. link->src->name);
  273. return ret;
  274. }
  275. switch (link->type) {
  276. case AVMEDIA_TYPE_VIDEO:
  277. if (!link->time_base.num && !link->time_base.den)
  278. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  279. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  280. link->sample_aspect_ratio = inlink ?
  281. inlink->sample_aspect_ratio : (AVRational){1,1};
  282. if (inlink) {
  283. if (!link->frame_rate.num && !link->frame_rate.den)
  284. link->frame_rate = inlink->frame_rate;
  285. if (!link->w)
  286. link->w = inlink->w;
  287. if (!link->h)
  288. link->h = inlink->h;
  289. } else if (!link->w || !link->h) {
  290. av_log(link->src, AV_LOG_ERROR,
  291. "Video source filters must set their output link's "
  292. "width and height\n");
  293. return AVERROR(EINVAL);
  294. }
  295. break;
  296. case AVMEDIA_TYPE_AUDIO:
  297. if (inlink) {
  298. if (!link->time_base.num && !link->time_base.den)
  299. link->time_base = inlink->time_base;
  300. }
  301. if (!link->time_base.num && !link->time_base.den)
  302. link->time_base = (AVRational) {1, link->sample_rate};
  303. }
  304. if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
  305. !(link->src->filter->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
  306. av_assert0(!link->hw_frames_ctx &&
  307. "should not be set by non-hwframe-aware filter");
  308. link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
  309. if (!link->hw_frames_ctx)
  310. return AVERROR(ENOMEM);
  311. }
  312. if ((config_link = link->dstpad->config_props))
  313. if ((ret = config_link(link)) < 0) {
  314. av_log(link->dst, AV_LOG_ERROR,
  315. "Failed to configure input pad on %s\n",
  316. link->dst->name);
  317. return ret;
  318. }
  319. link->init_state = AVLINK_INIT;
  320. }
  321. }
  322. return 0;
  323. }
  324. void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
  325. {
  326. if (link->type == AVMEDIA_TYPE_VIDEO) {
  327. ff_tlog(ctx,
  328. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  329. link, link->w, link->h,
  330. av_get_pix_fmt_name(link->format),
  331. link->src ? link->src->filter->name : "",
  332. link->dst ? link->dst->filter->name : "",
  333. end ? "\n" : "");
  334. } else {
  335. char buf[128];
  336. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  337. ff_tlog(ctx,
  338. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  339. link, (int)link->sample_rate, buf,
  340. av_get_sample_fmt_name(link->format),
  341. link->src ? link->src->filter->name : "",
  342. link->dst ? link->dst->filter->name : "",
  343. end ? "\n" : "");
  344. }
  345. }
  346. int ff_request_frame(AVFilterLink *link)
  347. {
  348. FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
  349. av_assert1(!link->dst->filter->activate);
  350. if (link->status_out)
  351. return link->status_out;
  352. if (link->status_in) {
  353. if (ff_framequeue_queued_frames(&link->fifo)) {
  354. av_assert1(!link->frame_wanted_out);
  355. av_assert1(link->dst->ready >= 300);
  356. return 0;
  357. } else {
  358. /* Acknowledge status change. Filters using ff_request_frame() will
  359. handle the change automatically. Filters can also check the
  360. status directly but none do yet. */
  361. ff_avfilter_link_set_out_status(link, link->status_in, link->status_in_pts);
  362. return link->status_out;
  363. }
  364. }
  365. link->frame_wanted_out = 1;
  366. ff_filter_set_ready(link->src, 100);
  367. return 0;
  368. }
  369. static int64_t guess_status_pts(AVFilterContext *ctx, int status, AVRational link_time_base)
  370. {
  371. unsigned i;
  372. int64_t r = INT64_MAX;
  373. for (i = 0; i < ctx->nb_inputs; i++)
  374. if (ctx->inputs[i]->status_out == status)
  375. r = FFMIN(r, av_rescale_q(ctx->inputs[i]->current_pts, ctx->inputs[i]->time_base, link_time_base));
  376. if (r < INT64_MAX)
  377. return r;
  378. av_log(ctx, AV_LOG_WARNING, "EOF timestamp not reliable\n");
  379. for (i = 0; i < ctx->nb_inputs; i++)
  380. r = FFMIN(r, av_rescale_q(ctx->inputs[i]->status_in_pts, ctx->inputs[i]->time_base, link_time_base));
  381. if (r < INT64_MAX)
  382. return r;
  383. return AV_NOPTS_VALUE;
  384. }
  385. static int ff_request_frame_to_filter(AVFilterLink *link)
  386. {
  387. int ret = -1;
  388. FF_TPRINTF_START(NULL, request_frame_to_filter); ff_tlog_link(NULL, link, 1);
  389. /* Assume the filter is blocked, let the method clear it if not */
  390. link->frame_blocked_in = 1;
  391. if (link->srcpad->request_frame)
  392. ret = link->srcpad->request_frame(link);
  393. else if (link->src->inputs[0])
  394. ret = ff_request_frame(link->src->inputs[0]);
  395. if (ret < 0) {
  396. if (ret != AVERROR(EAGAIN) && ret != link->status_in)
  397. ff_avfilter_link_set_in_status(link, ret, guess_status_pts(link->src, ret, link->time_base));
  398. if (ret == AVERROR_EOF)
  399. ret = 0;
  400. }
  401. return ret;
  402. }
  403. int ff_poll_frame(AVFilterLink *link)
  404. {
  405. int i, min = INT_MAX;
  406. if (link->srcpad->poll_frame)
  407. return link->srcpad->poll_frame(link);
  408. for (i = 0; i < link->src->nb_inputs; i++) {
  409. int val;
  410. if (!link->src->inputs[i])
  411. return AVERROR(EINVAL);
  412. val = ff_poll_frame(link->src->inputs[i]);
  413. min = FFMIN(min, val);
  414. }
  415. return min;
  416. }
  417. static const char *const var_names[] = {
  418. "t",
  419. "n",
  420. "pos",
  421. "w",
  422. "h",
  423. NULL
  424. };
  425. enum {
  426. VAR_T,
  427. VAR_N,
  428. VAR_POS,
  429. VAR_W,
  430. VAR_H,
  431. VAR_VARS_NB
  432. };
  433. static int set_enable_expr(AVFilterContext *ctx, const char *expr)
  434. {
  435. int ret;
  436. char *expr_dup;
  437. AVExpr *old = ctx->enable;
  438. if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
  439. av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
  440. "with filter '%s'\n", ctx->filter->name);
  441. return AVERROR_PATCHWELCOME;
  442. }
  443. expr_dup = av_strdup(expr);
  444. if (!expr_dup)
  445. return AVERROR(ENOMEM);
  446. if (!ctx->var_values) {
  447. ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
  448. if (!ctx->var_values) {
  449. av_free(expr_dup);
  450. return AVERROR(ENOMEM);
  451. }
  452. }
  453. ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
  454. NULL, NULL, NULL, NULL, 0, ctx->priv);
  455. if (ret < 0) {
  456. av_log(ctx->priv, AV_LOG_ERROR,
  457. "Error when evaluating the expression '%s' for enable\n",
  458. expr_dup);
  459. av_free(expr_dup);
  460. return ret;
  461. }
  462. av_expr_free(old);
  463. av_free(ctx->enable_str);
  464. ctx->enable_str = expr_dup;
  465. return 0;
  466. }
  467. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  468. {
  469. if (pts == AV_NOPTS_VALUE)
  470. return;
  471. link->current_pts = pts;
  472. link->current_pts_us = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  473. /* TODO use duration */
  474. if (link->graph && link->age_index >= 0)
  475. ff_avfilter_graph_update_heap(link->graph, link);
  476. }
  477. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  478. {
  479. if(!strcmp(cmd, "ping")){
  480. char local_res[256] = {0};
  481. if (!res) {
  482. res = local_res;
  483. res_len = sizeof(local_res);
  484. }
  485. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  486. if (res == local_res)
  487. av_log(filter, AV_LOG_INFO, "%s", res);
  488. return 0;
  489. }else if(!strcmp(cmd, "enable")) {
  490. return set_enable_expr(filter, arg);
  491. }else if(filter->filter->process_command) {
  492. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  493. }
  494. return AVERROR(ENOSYS);
  495. }
  496. int avfilter_pad_count(const AVFilterPad *pads)
  497. {
  498. int count;
  499. if (!pads)
  500. return 0;
  501. for (count = 0; pads->name; count++)
  502. pads++;
  503. return count;
  504. }
  505. static const char *default_filter_name(void *filter_ctx)
  506. {
  507. AVFilterContext *ctx = filter_ctx;
  508. return ctx->name ? ctx->name : ctx->filter->name;
  509. }
  510. static void *filter_child_next(void *obj, void *prev)
  511. {
  512. AVFilterContext *ctx = obj;
  513. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  514. return ctx->priv;
  515. return NULL;
  516. }
  517. static const AVClass *filter_child_class_next(const AVClass *prev)
  518. {
  519. void *opaque = NULL;
  520. const AVFilter *f = NULL;
  521. /* find the filter that corresponds to prev */
  522. while (prev && (f = av_filter_iterate(&opaque)))
  523. if (f->priv_class == prev)
  524. break;
  525. /* could not find filter corresponding to prev */
  526. if (prev && !f)
  527. return NULL;
  528. /* find next filter with specific options */
  529. while ((f = av_filter_iterate(&opaque)))
  530. if (f->priv_class)
  531. return f->priv_class;
  532. return NULL;
  533. }
  534. #define OFFSET(x) offsetof(AVFilterContext, x)
  535. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
  536. static const AVOption avfilter_options[] = {
  537. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  538. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  539. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
  540. { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  541. { "threads", "Allowed number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
  542. { .i64 = 0 }, 0, INT_MAX, FLAGS },
  543. { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
  544. OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  545. { NULL },
  546. };
  547. static const AVClass avfilter_class = {
  548. .class_name = "AVFilter",
  549. .item_name = default_filter_name,
  550. .version = LIBAVUTIL_VERSION_INT,
  551. .category = AV_CLASS_CATEGORY_FILTER,
  552. .child_next = filter_child_next,
  553. .child_class_next = filter_child_class_next,
  554. .option = avfilter_options,
  555. };
  556. static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
  557. int *ret, int nb_jobs)
  558. {
  559. int i;
  560. for (i = 0; i < nb_jobs; i++) {
  561. int r = func(ctx, arg, i, nb_jobs);
  562. if (ret)
  563. ret[i] = r;
  564. }
  565. return 0;
  566. }
  567. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  568. {
  569. AVFilterContext *ret;
  570. int preinited = 0;
  571. if (!filter)
  572. return NULL;
  573. ret = av_mallocz(sizeof(AVFilterContext));
  574. if (!ret)
  575. return NULL;
  576. ret->av_class = &avfilter_class;
  577. ret->filter = filter;
  578. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  579. if (filter->priv_size) {
  580. ret->priv = av_mallocz(filter->priv_size);
  581. if (!ret->priv)
  582. goto err;
  583. }
  584. if (filter->preinit) {
  585. if (filter->preinit(ret) < 0)
  586. goto err;
  587. preinited = 1;
  588. }
  589. av_opt_set_defaults(ret);
  590. if (filter->priv_class) {
  591. *(const AVClass**)ret->priv = filter->priv_class;
  592. av_opt_set_defaults(ret->priv);
  593. }
  594. ret->internal = av_mallocz(sizeof(*ret->internal));
  595. if (!ret->internal)
  596. goto err;
  597. ret->internal->execute = default_execute;
  598. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  599. if (ret->nb_inputs ) {
  600. ret->input_pads = av_malloc_array(ret->nb_inputs, sizeof(AVFilterPad));
  601. if (!ret->input_pads)
  602. goto err;
  603. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  604. ret->inputs = av_mallocz_array(ret->nb_inputs, sizeof(AVFilterLink*));
  605. if (!ret->inputs)
  606. goto err;
  607. }
  608. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  609. if (ret->nb_outputs) {
  610. ret->output_pads = av_malloc_array(ret->nb_outputs, sizeof(AVFilterPad));
  611. if (!ret->output_pads)
  612. goto err;
  613. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  614. ret->outputs = av_mallocz_array(ret->nb_outputs, sizeof(AVFilterLink*));
  615. if (!ret->outputs)
  616. goto err;
  617. }
  618. return ret;
  619. err:
  620. if (preinited)
  621. filter->uninit(ret);
  622. av_freep(&ret->inputs);
  623. av_freep(&ret->input_pads);
  624. ret->nb_inputs = 0;
  625. av_freep(&ret->outputs);
  626. av_freep(&ret->output_pads);
  627. ret->nb_outputs = 0;
  628. av_freep(&ret->priv);
  629. av_freep(&ret->internal);
  630. av_free(ret);
  631. return NULL;
  632. }
  633. static void free_link(AVFilterLink *link)
  634. {
  635. if (!link)
  636. return;
  637. if (link->src)
  638. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  639. if (link->dst)
  640. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  641. av_buffer_unref(&link->hw_frames_ctx);
  642. ff_formats_unref(&link->in_formats);
  643. ff_formats_unref(&link->out_formats);
  644. ff_formats_unref(&link->in_samplerates);
  645. ff_formats_unref(&link->out_samplerates);
  646. ff_channel_layouts_unref(&link->in_channel_layouts);
  647. ff_channel_layouts_unref(&link->out_channel_layouts);
  648. avfilter_link_free(&link);
  649. }
  650. void avfilter_free(AVFilterContext *filter)
  651. {
  652. int i;
  653. if (!filter)
  654. return;
  655. if (filter->graph)
  656. ff_filter_graph_remove_filter(filter->graph, filter);
  657. if (filter->filter->uninit)
  658. filter->filter->uninit(filter);
  659. for (i = 0; i < filter->nb_inputs; i++) {
  660. free_link(filter->inputs[i]);
  661. }
  662. for (i = 0; i < filter->nb_outputs; i++) {
  663. free_link(filter->outputs[i]);
  664. }
  665. if (filter->filter->priv_class)
  666. av_opt_free(filter->priv);
  667. av_buffer_unref(&filter->hw_device_ctx);
  668. av_freep(&filter->name);
  669. av_freep(&filter->input_pads);
  670. av_freep(&filter->output_pads);
  671. av_freep(&filter->inputs);
  672. av_freep(&filter->outputs);
  673. av_freep(&filter->priv);
  674. while(filter->command_queue){
  675. ff_command_queue_pop(filter);
  676. }
  677. av_opt_free(filter);
  678. av_expr_free(filter->enable);
  679. filter->enable = NULL;
  680. av_freep(&filter->var_values);
  681. av_freep(&filter->internal);
  682. av_free(filter);
  683. }
  684. int ff_filter_get_nb_threads(AVFilterContext *ctx)
  685. {
  686. if (ctx->nb_threads > 0)
  687. return FFMIN(ctx->nb_threads, ctx->graph->nb_threads);
  688. return ctx->graph->nb_threads;
  689. }
  690. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  691. const char *args)
  692. {
  693. const AVOption *o = NULL;
  694. int ret, count = 0;
  695. char *av_uninit(parsed_key), *av_uninit(value);
  696. const char *key;
  697. int offset= -1;
  698. if (!args)
  699. return 0;
  700. while (*args) {
  701. const char *shorthand = NULL;
  702. o = av_opt_next(ctx->priv, o);
  703. if (o) {
  704. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  705. continue;
  706. offset = o->offset;
  707. shorthand = o->name;
  708. }
  709. ret = av_opt_get_key_value(&args, "=", ":",
  710. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  711. &parsed_key, &value);
  712. if (ret < 0) {
  713. if (ret == AVERROR(EINVAL))
  714. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  715. else
  716. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  717. av_err2str(ret));
  718. return ret;
  719. }
  720. if (*args)
  721. args++;
  722. if (parsed_key) {
  723. key = parsed_key;
  724. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  725. } else {
  726. key = shorthand;
  727. }
  728. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  729. if (av_opt_find(ctx, key, NULL, 0, 0)) {
  730. ret = av_opt_set(ctx, key, value, 0);
  731. if (ret < 0) {
  732. av_free(value);
  733. av_free(parsed_key);
  734. return ret;
  735. }
  736. } else {
  737. av_dict_set(options, key, value, 0);
  738. if ((ret = av_opt_set(ctx->priv, key, value, AV_OPT_SEARCH_CHILDREN)) < 0) {
  739. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  740. if (ret == AVERROR_OPTION_NOT_FOUND)
  741. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  742. av_free(value);
  743. av_free(parsed_key);
  744. return ret;
  745. }
  746. }
  747. }
  748. av_free(value);
  749. av_free(parsed_key);
  750. count++;
  751. }
  752. if (ctx->enable_str) {
  753. ret = set_enable_expr(ctx, ctx->enable_str);
  754. if (ret < 0)
  755. return ret;
  756. }
  757. return count;
  758. }
  759. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  760. {
  761. int ret = 0;
  762. ret = av_opt_set_dict(ctx, options);
  763. if (ret < 0) {
  764. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  765. return ret;
  766. }
  767. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  768. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  769. ctx->graph->internal->thread_execute) {
  770. ctx->thread_type = AVFILTER_THREAD_SLICE;
  771. ctx->internal->execute = ctx->graph->internal->thread_execute;
  772. } else {
  773. ctx->thread_type = 0;
  774. }
  775. if (ctx->filter->priv_class) {
  776. ret = av_opt_set_dict2(ctx->priv, options, AV_OPT_SEARCH_CHILDREN);
  777. if (ret < 0) {
  778. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  779. return ret;
  780. }
  781. }
  782. if (ctx->filter->init_opaque)
  783. ret = ctx->filter->init_opaque(ctx, NULL);
  784. else if (ctx->filter->init)
  785. ret = ctx->filter->init(ctx);
  786. else if (ctx->filter->init_dict)
  787. ret = ctx->filter->init_dict(ctx, options);
  788. return ret;
  789. }
  790. int avfilter_init_str(AVFilterContext *filter, const char *args)
  791. {
  792. AVDictionary *options = NULL;
  793. AVDictionaryEntry *e;
  794. int ret = 0;
  795. if (args && *args) {
  796. if (!filter->filter->priv_class) {
  797. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  798. "options, but options were provided: %s.\n", args);
  799. return AVERROR(EINVAL);
  800. }
  801. #if FF_API_OLD_FILTER_OPTS_ERROR
  802. if ( !strcmp(filter->filter->name, "format") ||
  803. !strcmp(filter->filter->name, "noformat") ||
  804. !strcmp(filter->filter->name, "frei0r") ||
  805. !strcmp(filter->filter->name, "frei0r_src") ||
  806. !strcmp(filter->filter->name, "ocv") ||
  807. !strcmp(filter->filter->name, "pan") ||
  808. !strcmp(filter->filter->name, "pp") ||
  809. !strcmp(filter->filter->name, "aevalsrc")) {
  810. /* a hack for compatibility with the old syntax
  811. * replace colons with |s */
  812. char *copy = av_strdup(args);
  813. char *p = copy;
  814. int nb_leading = 0; // number of leading colons to skip
  815. int deprecated = 0;
  816. if (!copy) {
  817. ret = AVERROR(ENOMEM);
  818. goto fail;
  819. }
  820. if (!strcmp(filter->filter->name, "frei0r") ||
  821. !strcmp(filter->filter->name, "ocv"))
  822. nb_leading = 1;
  823. else if (!strcmp(filter->filter->name, "frei0r_src"))
  824. nb_leading = 3;
  825. while (nb_leading--) {
  826. p = strchr(p, ':');
  827. if (!p) {
  828. p = copy + strlen(copy);
  829. break;
  830. }
  831. p++;
  832. }
  833. deprecated = strchr(p, ':') != NULL;
  834. if (!strcmp(filter->filter->name, "aevalsrc")) {
  835. deprecated = 0;
  836. while ((p = strchr(p, ':')) && p[1] != ':') {
  837. const char *epos = strchr(p + 1, '=');
  838. const char *spos = strchr(p + 1, ':');
  839. const int next_token_is_opt = epos && (!spos || epos < spos);
  840. if (next_token_is_opt) {
  841. p++;
  842. break;
  843. }
  844. /* next token does not contain a '=', assume a channel expression */
  845. deprecated = 1;
  846. *p++ = '|';
  847. }
  848. if (p && *p == ':') { // double sep '::' found
  849. deprecated = 1;
  850. memmove(p, p + 1, strlen(p));
  851. }
  852. } else
  853. while ((p = strchr(p, ':')))
  854. *p++ = '|';
  855. if (deprecated) {
  856. av_log(filter, AV_LOG_ERROR, "This syntax is deprecated. Use "
  857. "'|' to separate the list items ('%s' instead of '%s')\n",
  858. copy, args);
  859. ret = AVERROR(EINVAL);
  860. } else {
  861. ret = process_options(filter, &options, copy);
  862. }
  863. av_freep(&copy);
  864. if (ret < 0)
  865. goto fail;
  866. } else
  867. #endif
  868. {
  869. ret = process_options(filter, &options, args);
  870. if (ret < 0)
  871. goto fail;
  872. }
  873. }
  874. ret = avfilter_init_dict(filter, &options);
  875. if (ret < 0)
  876. goto fail;
  877. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  878. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  879. ret = AVERROR_OPTION_NOT_FOUND;
  880. goto fail;
  881. }
  882. fail:
  883. av_dict_free(&options);
  884. return ret;
  885. }
  886. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  887. {
  888. return pads[pad_idx].name;
  889. }
  890. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  891. {
  892. return pads[pad_idx].type;
  893. }
  894. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  895. {
  896. return ff_filter_frame(link->dst->outputs[0], frame);
  897. }
  898. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  899. {
  900. int (*filter_frame)(AVFilterLink *, AVFrame *);
  901. AVFilterContext *dstctx = link->dst;
  902. AVFilterPad *dst = link->dstpad;
  903. int ret;
  904. if (!(filter_frame = dst->filter_frame))
  905. filter_frame = default_filter_frame;
  906. if (dst->needs_writable) {
  907. ret = ff_inlink_make_frame_writable(link, &frame);
  908. if (ret < 0)
  909. goto fail;
  910. }
  911. ff_inlink_process_commands(link, frame);
  912. dstctx->is_disabled = !ff_inlink_evaluate_timeline_at_frame(link, frame);
  913. if (dstctx->is_disabled &&
  914. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  915. filter_frame = default_filter_frame;
  916. ret = filter_frame(link, frame);
  917. link->frame_count_out++;
  918. return ret;
  919. fail:
  920. av_frame_free(&frame);
  921. return ret;
  922. }
  923. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  924. {
  925. int ret;
  926. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  927. /* Consistency checks */
  928. if (link->type == AVMEDIA_TYPE_VIDEO) {
  929. if (strcmp(link->dst->filter->name, "buffersink") &&
  930. strcmp(link->dst->filter->name, "format") &&
  931. strcmp(link->dst->filter->name, "idet") &&
  932. strcmp(link->dst->filter->name, "null") &&
  933. strcmp(link->dst->filter->name, "scale")) {
  934. av_assert1(frame->format == link->format);
  935. av_assert1(frame->width == link->w);
  936. av_assert1(frame->height == link->h);
  937. }
  938. } else {
  939. if (frame->format != link->format) {
  940. av_log(link->dst, AV_LOG_ERROR, "Format change is not supported\n");
  941. goto error;
  942. }
  943. if (frame->channels != link->channels) {
  944. av_log(link->dst, AV_LOG_ERROR, "Channel count change is not supported\n");
  945. goto error;
  946. }
  947. if (frame->channel_layout != link->channel_layout) {
  948. av_log(link->dst, AV_LOG_ERROR, "Channel layout change is not supported\n");
  949. goto error;
  950. }
  951. if (frame->sample_rate != link->sample_rate) {
  952. av_log(link->dst, AV_LOG_ERROR, "Sample rate change is not supported\n");
  953. goto error;
  954. }
  955. }
  956. link->frame_blocked_in = link->frame_wanted_out = 0;
  957. link->frame_count_in++;
  958. filter_unblock(link->dst);
  959. ret = ff_framequeue_add(&link->fifo, frame);
  960. if (ret < 0) {
  961. av_frame_free(&frame);
  962. return ret;
  963. }
  964. ff_filter_set_ready(link->dst, 300);
  965. return 0;
  966. error:
  967. av_frame_free(&frame);
  968. return AVERROR_PATCHWELCOME;
  969. }
  970. static int samples_ready(AVFilterLink *link, unsigned min)
  971. {
  972. return ff_framequeue_queued_frames(&link->fifo) &&
  973. (ff_framequeue_queued_samples(&link->fifo) >= min ||
  974. link->status_in);
  975. }
  976. static int take_samples(AVFilterLink *link, unsigned min, unsigned max,
  977. AVFrame **rframe)
  978. {
  979. AVFrame *frame0, *frame, *buf;
  980. unsigned nb_samples, nb_frames, i, p;
  981. int ret;
  982. /* Note: this function relies on no format changes and must only be
  983. called with enough samples. */
  984. av_assert1(samples_ready(link, link->min_samples));
  985. frame0 = frame = ff_framequeue_peek(&link->fifo, 0);
  986. if (!link->fifo.samples_skipped && frame->nb_samples >= min && frame->nb_samples <= max) {
  987. *rframe = ff_framequeue_take(&link->fifo);
  988. return 0;
  989. }
  990. nb_frames = 0;
  991. nb_samples = 0;
  992. while (1) {
  993. if (nb_samples + frame->nb_samples > max) {
  994. if (nb_samples < min)
  995. nb_samples = max;
  996. break;
  997. }
  998. nb_samples += frame->nb_samples;
  999. nb_frames++;
  1000. if (nb_frames == ff_framequeue_queued_frames(&link->fifo))
  1001. break;
  1002. frame = ff_framequeue_peek(&link->fifo, nb_frames);
  1003. }
  1004. buf = ff_get_audio_buffer(link, nb_samples);
  1005. if (!buf)
  1006. return AVERROR(ENOMEM);
  1007. ret = av_frame_copy_props(buf, frame0);
  1008. if (ret < 0) {
  1009. av_frame_free(&buf);
  1010. return ret;
  1011. }
  1012. buf->pts = frame0->pts;
  1013. p = 0;
  1014. for (i = 0; i < nb_frames; i++) {
  1015. frame = ff_framequeue_take(&link->fifo);
  1016. av_samples_copy(buf->extended_data, frame->extended_data, p, 0,
  1017. frame->nb_samples, link->channels, link->format);
  1018. p += frame->nb_samples;
  1019. av_frame_free(&frame);
  1020. }
  1021. if (p < nb_samples) {
  1022. unsigned n = nb_samples - p;
  1023. frame = ff_framequeue_peek(&link->fifo, 0);
  1024. av_samples_copy(buf->extended_data, frame->extended_data, p, 0, n,
  1025. link->channels, link->format);
  1026. ff_framequeue_skip_samples(&link->fifo, n, link->time_base);
  1027. }
  1028. *rframe = buf;
  1029. return 0;
  1030. }
  1031. static int ff_filter_frame_to_filter(AVFilterLink *link)
  1032. {
  1033. AVFrame *frame = NULL;
  1034. AVFilterContext *dst = link->dst;
  1035. int ret;
  1036. av_assert1(ff_framequeue_queued_frames(&link->fifo));
  1037. ret = link->min_samples ?
  1038. ff_inlink_consume_samples(link, link->min_samples, link->max_samples, &frame) :
  1039. ff_inlink_consume_frame(link, &frame);
  1040. av_assert1(ret);
  1041. if (ret < 0) {
  1042. av_assert1(!frame);
  1043. return ret;
  1044. }
  1045. /* The filter will soon have received a new frame, that may allow it to
  1046. produce one or more: unblock its outputs. */
  1047. filter_unblock(dst);
  1048. /* AVFilterPad.filter_frame() expect frame_count_out to have the value
  1049. before the frame; ff_filter_frame_framed() will re-increment it. */
  1050. link->frame_count_out--;
  1051. ret = ff_filter_frame_framed(link, frame);
  1052. if (ret < 0 && ret != link->status_out) {
  1053. ff_avfilter_link_set_out_status(link, ret, AV_NOPTS_VALUE);
  1054. } else {
  1055. /* Run once again, to see if several frames were available, or if
  1056. the input status has also changed, or any other reason. */
  1057. ff_filter_set_ready(dst, 300);
  1058. }
  1059. return ret;
  1060. }
  1061. static int forward_status_change(AVFilterContext *filter, AVFilterLink *in)
  1062. {
  1063. unsigned out = 0, progress = 0;
  1064. int ret;
  1065. av_assert0(!in->status_out);
  1066. if (!filter->nb_outputs) {
  1067. /* not necessary with the current API and sinks */
  1068. return 0;
  1069. }
  1070. while (!in->status_out) {
  1071. if (!filter->outputs[out]->status_in) {
  1072. progress++;
  1073. ret = ff_request_frame_to_filter(filter->outputs[out]);
  1074. if (ret < 0)
  1075. return ret;
  1076. }
  1077. if (++out == filter->nb_outputs) {
  1078. if (!progress) {
  1079. /* Every output already closed: input no longer interesting
  1080. (example: overlay in shortest mode, other input closed). */
  1081. ff_avfilter_link_set_out_status(in, in->status_in, in->status_in_pts);
  1082. return 0;
  1083. }
  1084. progress = 0;
  1085. out = 0;
  1086. }
  1087. }
  1088. ff_filter_set_ready(filter, 200);
  1089. return 0;
  1090. }
  1091. static int ff_filter_activate_default(AVFilterContext *filter)
  1092. {
  1093. unsigned i;
  1094. for (i = 0; i < filter->nb_inputs; i++) {
  1095. if (samples_ready(filter->inputs[i], filter->inputs[i]->min_samples)) {
  1096. return ff_filter_frame_to_filter(filter->inputs[i]);
  1097. }
  1098. }
  1099. for (i = 0; i < filter->nb_inputs; i++) {
  1100. if (filter->inputs[i]->status_in && !filter->inputs[i]->status_out) {
  1101. av_assert1(!ff_framequeue_queued_frames(&filter->inputs[i]->fifo));
  1102. return forward_status_change(filter, filter->inputs[i]);
  1103. }
  1104. }
  1105. for (i = 0; i < filter->nb_outputs; i++) {
  1106. if (filter->outputs[i]->frame_wanted_out &&
  1107. !filter->outputs[i]->frame_blocked_in) {
  1108. return ff_request_frame_to_filter(filter->outputs[i]);
  1109. }
  1110. }
  1111. return FFERROR_NOT_READY;
  1112. }
  1113. /*
  1114. Filter scheduling and activation
  1115. When a filter is activated, it must:
  1116. - if possible, output a frame;
  1117. - else, if relevant, forward the input status change;
  1118. - else, check outputs for wanted frames and forward the requests.
  1119. The following AVFilterLink fields are used for activation:
  1120. - frame_wanted_out:
  1121. This field indicates if a frame is needed on this input of the
  1122. destination filter. A positive value indicates that a frame is needed
  1123. to process queued frames or internal data or to satisfy the
  1124. application; a zero value indicates that a frame is not especially
  1125. needed but could be processed anyway; a negative value indicates that a
  1126. frame would just be queued.
  1127. It is set by filters using ff_request_frame() or ff_request_no_frame(),
  1128. when requested by the application through a specific API or when it is
  1129. set on one of the outputs.
  1130. It is cleared when a frame is sent from the source using
  1131. ff_filter_frame().
  1132. It is also cleared when a status change is sent from the source using
  1133. ff_avfilter_link_set_in_status().
  1134. - frame_blocked_in:
  1135. This field means that the source filter can not generate a frame as is.
  1136. Its goal is to avoid repeatedly calling the request_frame() method on
  1137. the same link.
  1138. It is set by the framework on all outputs of a filter before activating it.
  1139. It is automatically cleared by ff_filter_frame().
  1140. It is also automatically cleared by ff_avfilter_link_set_in_status().
  1141. It is also cleared on all outputs (using filter_unblock()) when
  1142. something happens on an input: processing a frame or changing the
  1143. status.
  1144. - fifo:
  1145. Contains the frames queued on a filter input. If it contains frames and
  1146. frame_wanted_out is not set, then the filter can be activated. If that
  1147. result in the filter not able to use these frames, the filter must set
  1148. frame_wanted_out to ask for more frames.
  1149. - status_in and status_in_pts:
  1150. Status (EOF or error code) of the link and timestamp of the status
  1151. change (in link time base, same as frames) as seen from the input of
  1152. the link. The status change is considered happening after the frames
  1153. queued in fifo.
  1154. It is set by the source filter using ff_avfilter_link_set_in_status().
  1155. - status_out:
  1156. Status of the link as seen from the output of the link. The status
  1157. change is considered having already happened.
  1158. It is set by the destination filter using
  1159. ff_avfilter_link_set_out_status().
  1160. Filters are activated according to the ready field, set using the
  1161. ff_filter_set_ready(). Eventually, a priority queue will be used.
  1162. ff_filter_set_ready() is called whenever anything could cause progress to
  1163. be possible. Marking a filter ready when it is not is not a problem,
  1164. except for the small overhead it causes.
  1165. Conditions that cause a filter to be marked ready are:
  1166. - frames added on an input link;
  1167. - changes in the input or output status of an input link;
  1168. - requests for a frame on an output link;
  1169. - after any actual processing using the legacy methods (filter_frame(),
  1170. and request_frame() to acknowledge status changes), to run once more
  1171. and check if enough input was present for several frames.
  1172. Examples of scenarios to consider:
  1173. - buffersrc: activate if frame_wanted_out to notify the application;
  1174. activate when the application adds a frame to push it immediately.
  1175. - testsrc: activate only if frame_wanted_out to produce and push a frame.
  1176. - concat (not at stitch points): can process a frame on any output.
  1177. Activate if frame_wanted_out on output to forward on the corresponding
  1178. input. Activate when a frame is present on input to process it
  1179. immediately.
  1180. - framesync: needs at least one frame on each input; extra frames on the
  1181. wrong input will accumulate. When a frame is first added on one input,
  1182. set frame_wanted_out<0 on it to avoid getting more (would trigger
  1183. testsrc) and frame_wanted_out>0 on the other to allow processing it.
  1184. Activation of old filters:
  1185. In order to activate a filter implementing the legacy filter_frame() and
  1186. request_frame() methods, perform the first possible of the following
  1187. actions:
  1188. - If an input has frames in fifo and frame_wanted_out == 0, dequeue a
  1189. frame and call filter_frame().
  1190. Rationale: filter frames as soon as possible instead of leaving them
  1191. queued; frame_wanted_out < 0 is not possible since the old API does not
  1192. set it nor provides any similar feedback; frame_wanted_out > 0 happens
  1193. when min_samples > 0 and there are not enough samples queued.
  1194. - If an input has status_in set but not status_out, try to call
  1195. request_frame() on one of the outputs in the hope that it will trigger
  1196. request_frame() on the input with status_in and acknowledge it. This is
  1197. awkward and fragile, filters with several inputs or outputs should be
  1198. updated to direct activation as soon as possible.
  1199. - If an output has frame_wanted_out > 0 and not frame_blocked_in, call
  1200. request_frame().
  1201. Rationale: checking frame_blocked_in is necessary to avoid requesting
  1202. repeatedly on a blocked input if another is not blocked (example:
  1203. [buffersrc1][testsrc1][buffersrc2][testsrc2]concat=v=2).
  1204. TODO: respect needs_fifo and remove auto-inserted fifos.
  1205. */
  1206. int ff_filter_activate(AVFilterContext *filter)
  1207. {
  1208. int ret;
  1209. /* Generic timeline support is not yet implemented but should be easy */
  1210. av_assert1(!(filter->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC &&
  1211. filter->filter->activate));
  1212. filter->ready = 0;
  1213. ret = filter->filter->activate ? filter->filter->activate(filter) :
  1214. ff_filter_activate_default(filter);
  1215. if (ret == FFERROR_NOT_READY)
  1216. ret = 0;
  1217. return ret;
  1218. }
  1219. int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
  1220. {
  1221. *rpts = link->current_pts;
  1222. if (ff_framequeue_queued_frames(&link->fifo))
  1223. return *rstatus = 0;
  1224. if (link->status_out)
  1225. return *rstatus = link->status_out;
  1226. if (!link->status_in)
  1227. return *rstatus = 0;
  1228. *rstatus = link->status_out = link->status_in;
  1229. ff_update_link_current_pts(link, link->status_in_pts);
  1230. *rpts = link->current_pts;
  1231. return 1;
  1232. }
  1233. size_t ff_inlink_queued_frames(AVFilterLink *link)
  1234. {
  1235. return ff_framequeue_queued_frames(&link->fifo);
  1236. }
  1237. int ff_inlink_check_available_frame(AVFilterLink *link)
  1238. {
  1239. return ff_framequeue_queued_frames(&link->fifo) > 0;
  1240. }
  1241. int ff_inlink_queued_samples(AVFilterLink *link)
  1242. {
  1243. return ff_framequeue_queued_samples(&link->fifo);
  1244. }
  1245. int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
  1246. {
  1247. uint64_t samples = ff_framequeue_queued_samples(&link->fifo);
  1248. av_assert1(min);
  1249. return samples >= min || (link->status_in && samples);
  1250. }
  1251. static void consume_update(AVFilterLink *link, const AVFrame *frame)
  1252. {
  1253. ff_update_link_current_pts(link, frame->pts);
  1254. ff_inlink_process_commands(link, frame);
  1255. link->dst->is_disabled = !ff_inlink_evaluate_timeline_at_frame(link, frame);
  1256. link->frame_count_out++;
  1257. }
  1258. int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
  1259. {
  1260. AVFrame *frame;
  1261. *rframe = NULL;
  1262. if (!ff_inlink_check_available_frame(link))
  1263. return 0;
  1264. if (link->fifo.samples_skipped) {
  1265. frame = ff_framequeue_peek(&link->fifo, 0);
  1266. return ff_inlink_consume_samples(link, frame->nb_samples, frame->nb_samples, rframe);
  1267. }
  1268. frame = ff_framequeue_take(&link->fifo);
  1269. consume_update(link, frame);
  1270. *rframe = frame;
  1271. return 1;
  1272. }
  1273. int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
  1274. AVFrame **rframe)
  1275. {
  1276. AVFrame *frame;
  1277. int ret;
  1278. av_assert1(min);
  1279. *rframe = NULL;
  1280. if (!ff_inlink_check_available_samples(link, min))
  1281. return 0;
  1282. if (link->status_in)
  1283. min = FFMIN(min, ff_framequeue_queued_samples(&link->fifo));
  1284. ret = take_samples(link, min, max, &frame);
  1285. if (ret < 0)
  1286. return ret;
  1287. consume_update(link, frame);
  1288. *rframe = frame;
  1289. return 1;
  1290. }
  1291. AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
  1292. {
  1293. return ff_framequeue_peek(&link->fifo, idx);
  1294. }
  1295. int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
  1296. {
  1297. AVFrame *frame = *rframe;
  1298. AVFrame *out;
  1299. int ret;
  1300. if (av_frame_is_writable(frame))
  1301. return 0;
  1302. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  1303. switch (link->type) {
  1304. case AVMEDIA_TYPE_VIDEO:
  1305. out = ff_get_video_buffer(link, link->w, link->h);
  1306. break;
  1307. case AVMEDIA_TYPE_AUDIO:
  1308. out = ff_get_audio_buffer(link, frame->nb_samples);
  1309. break;
  1310. default:
  1311. return AVERROR(EINVAL);
  1312. }
  1313. if (!out)
  1314. return AVERROR(ENOMEM);
  1315. ret = av_frame_copy_props(out, frame);
  1316. if (ret < 0) {
  1317. av_frame_free(&out);
  1318. return ret;
  1319. }
  1320. switch (link->type) {
  1321. case AVMEDIA_TYPE_VIDEO:
  1322. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  1323. frame->format, frame->width, frame->height);
  1324. break;
  1325. case AVMEDIA_TYPE_AUDIO:
  1326. av_samples_copy(out->extended_data, frame->extended_data,
  1327. 0, 0, frame->nb_samples,
  1328. frame->channels,
  1329. frame->format);
  1330. break;
  1331. default:
  1332. av_assert0(!"reached");
  1333. }
  1334. av_frame_free(&frame);
  1335. *rframe = out;
  1336. return 0;
  1337. }
  1338. int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame)
  1339. {
  1340. AVFilterCommand *cmd = link->dst->command_queue;
  1341. while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
  1342. av_log(link->dst, AV_LOG_DEBUG,
  1343. "Processing command time:%f command:%s arg:%s\n",
  1344. cmd->time, cmd->command, cmd->arg);
  1345. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  1346. ff_command_queue_pop(link->dst);
  1347. cmd= link->dst->command_queue;
  1348. }
  1349. return 0;
  1350. }
  1351. int ff_inlink_evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame)
  1352. {
  1353. AVFilterContext *dstctx = link->dst;
  1354. int64_t pts = frame->pts;
  1355. int64_t pos = frame->pkt_pos;
  1356. if (!dstctx->enable_str)
  1357. return 1;
  1358. dstctx->var_values[VAR_N] = link->frame_count_out;
  1359. dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
  1360. dstctx->var_values[VAR_W] = link->w;
  1361. dstctx->var_values[VAR_H] = link->h;
  1362. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  1363. return fabs(av_expr_eval(dstctx->enable, dstctx->var_values, NULL)) >= 0.5;
  1364. }
  1365. void ff_inlink_request_frame(AVFilterLink *link)
  1366. {
  1367. av_assert1(!link->status_in);
  1368. av_assert1(!link->status_out);
  1369. link->frame_wanted_out = 1;
  1370. ff_filter_set_ready(link->src, 100);
  1371. }
  1372. void ff_inlink_set_status(AVFilterLink *link, int status)
  1373. {
  1374. if (link->status_out)
  1375. return;
  1376. link->frame_wanted_out = 0;
  1377. link->frame_blocked_in = 0;
  1378. ff_avfilter_link_set_out_status(link, status, AV_NOPTS_VALUE);
  1379. while (ff_framequeue_queued_frames(&link->fifo)) {
  1380. AVFrame *frame = ff_framequeue_take(&link->fifo);
  1381. av_frame_free(&frame);
  1382. }
  1383. if (!link->status_in)
  1384. link->status_in = status;
  1385. }
  1386. int ff_outlink_get_status(AVFilterLink *link)
  1387. {
  1388. return link->status_in;
  1389. }
  1390. const AVClass *avfilter_get_class(void)
  1391. {
  1392. return &avfilter_class;
  1393. }
  1394. int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
  1395. int default_pool_size)
  1396. {
  1397. AVHWFramesContext *frames;
  1398. // Must already be set by caller.
  1399. av_assert0(link->hw_frames_ctx);
  1400. frames = (AVHWFramesContext*)link->hw_frames_ctx->data;
  1401. if (frames->initial_pool_size == 0) {
  1402. // Dynamic allocation is necessarily supported.
  1403. } else if (avctx->extra_hw_frames >= 0) {
  1404. frames->initial_pool_size += avctx->extra_hw_frames;
  1405. } else {
  1406. frames->initial_pool_size = default_pool_size;
  1407. }
  1408. return 0;
  1409. }