avfilter.c 36 KB

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