avfilter.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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/channel_layout.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/rational.h"
  28. #include "libavutil/samplefmt.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "audio.h"
  34. static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame);
  35. char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  36. {
  37. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  38. perms & AV_PERM_READ ? "r" : "",
  39. perms & AV_PERM_WRITE ? "w" : "",
  40. perms & AV_PERM_PRESERVE ? "p" : "",
  41. perms & AV_PERM_REUSE ? "u" : "",
  42. perms & AV_PERM_REUSE2 ? "U" : "",
  43. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  44. return buf;
  45. }
  46. void ff_tlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  47. {
  48. av_unused char buf[16];
  49. ff_tlog(ctx,
  50. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  51. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  52. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  53. ref->pts, ref->pos);
  54. if (ref->video) {
  55. ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  56. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  57. ref->video->w, ref->video->h,
  58. !ref->video->interlaced ? 'P' : /* Progressive */
  59. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  60. ref->video->key_frame,
  61. av_get_picture_type_char(ref->video->pict_type));
  62. }
  63. if (ref->audio) {
  64. ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  65. ref->audio->channel_layout,
  66. ref->audio->nb_samples,
  67. ref->audio->sample_rate);
  68. }
  69. ff_tlog(ctx, "]%s", end ? "\n" : "");
  70. }
  71. unsigned avfilter_version(void) {
  72. av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
  73. return LIBAVFILTER_VERSION_INT;
  74. }
  75. const char *avfilter_configuration(void)
  76. {
  77. return FFMPEG_CONFIGURATION;
  78. }
  79. const char *avfilter_license(void)
  80. {
  81. #define LICENSE_PREFIX "libavfilter license: "
  82. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  83. }
  84. void ff_command_queue_pop(AVFilterContext *filter)
  85. {
  86. AVFilterCommand *c= filter->command_queue;
  87. av_freep(&c->arg);
  88. av_freep(&c->command);
  89. filter->command_queue= c->next;
  90. av_free(c);
  91. }
  92. void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  93. AVFilterPad **pads, AVFilterLink ***links,
  94. AVFilterPad *newpad)
  95. {
  96. unsigned i;
  97. idx = FFMIN(idx, *count);
  98. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  99. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  100. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  101. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  102. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  103. (*links)[idx] = NULL;
  104. (*count)++;
  105. for (i = idx + 1; i < *count; i++)
  106. if ((*links)[i])
  107. (*(unsigned *)((uint8_t *) (*links)[i] + padidx_off))++;
  108. }
  109. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  110. AVFilterContext *dst, unsigned dstpad)
  111. {
  112. AVFilterLink *link;
  113. if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
  114. src->outputs[srcpad] || dst->inputs[dstpad])
  115. return -1;
  116. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  117. av_log(src, AV_LOG_ERROR,
  118. "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
  119. src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
  120. dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
  121. return AVERROR(EINVAL);
  122. }
  123. src->outputs[srcpad] =
  124. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  125. link->src = src;
  126. link->dst = dst;
  127. link->srcpad = &src->output_pads[srcpad];
  128. link->dstpad = &dst->input_pads[dstpad];
  129. link->type = src->output_pads[srcpad].type;
  130. av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  131. link->format = -1;
  132. return 0;
  133. }
  134. void avfilter_link_free(AVFilterLink **link)
  135. {
  136. if (!*link)
  137. return;
  138. if ((*link)->pool)
  139. ff_free_pool((*link)->pool);
  140. avfilter_unref_bufferp(&(*link)->partial_buf);
  141. av_freep(link);
  142. }
  143. int avfilter_link_get_channels(AVFilterLink *link)
  144. {
  145. return link->channels;
  146. }
  147. void avfilter_link_set_closed(AVFilterLink *link, int closed)
  148. {
  149. link->closed = closed;
  150. }
  151. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  152. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  153. {
  154. int ret;
  155. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  156. av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
  157. "between the filter '%s' and the filter '%s'\n",
  158. filt->name, link->src->name, link->dst->name);
  159. link->dst->inputs[dstpad_idx] = NULL;
  160. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  161. /* failed to link output filter to new filter */
  162. link->dst->inputs[dstpad_idx] = link;
  163. return ret;
  164. }
  165. /* re-hookup the link to the new destination filter we inserted */
  166. link->dst = filt;
  167. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  168. filt->inputs[filt_srcpad_idx] = link;
  169. /* if any information on supported media formats already exists on the
  170. * link, we need to preserve that */
  171. if (link->out_formats)
  172. ff_formats_changeref(&link->out_formats,
  173. &filt->outputs[filt_dstpad_idx]->out_formats);
  174. if (link->out_samplerates)
  175. ff_formats_changeref(&link->out_samplerates,
  176. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  177. if (link->out_channel_layouts)
  178. ff_channel_layouts_changeref(&link->out_channel_layouts,
  179. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  180. return 0;
  181. }
  182. int avfilter_config_links(AVFilterContext *filter)
  183. {
  184. int (*config_link)(AVFilterLink *);
  185. unsigned i;
  186. int ret;
  187. for (i = 0; i < filter->nb_inputs; i ++) {
  188. AVFilterLink *link = filter->inputs[i];
  189. AVFilterLink *inlink = link->src->nb_inputs ?
  190. link->src->inputs[0] : NULL;
  191. if (!link) continue;
  192. link->current_pts = AV_NOPTS_VALUE;
  193. switch (link->init_state) {
  194. case AVLINK_INIT:
  195. continue;
  196. case AVLINK_STARTINIT:
  197. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  198. return 0;
  199. case AVLINK_UNINIT:
  200. link->init_state = AVLINK_STARTINIT;
  201. if ((ret = avfilter_config_links(link->src)) < 0)
  202. return ret;
  203. if (!(config_link = link->srcpad->config_props)) {
  204. if (link->src->nb_inputs != 1) {
  205. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  206. "with more than one input "
  207. "must set config_props() "
  208. "callbacks on all outputs\n");
  209. return AVERROR(EINVAL);
  210. }
  211. } else if ((ret = config_link(link)) < 0) {
  212. av_log(link->src, AV_LOG_ERROR,
  213. "Failed to configure output pad on %s\n",
  214. link->src->name);
  215. return ret;
  216. }
  217. switch (link->type) {
  218. case AVMEDIA_TYPE_VIDEO:
  219. if (!link->time_base.num && !link->time_base.den)
  220. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  221. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  222. link->sample_aspect_ratio = inlink ?
  223. inlink->sample_aspect_ratio : (AVRational){1,1};
  224. if (inlink && !link->frame_rate.num && !link->frame_rate.den)
  225. link->frame_rate = inlink->frame_rate;
  226. if (inlink) {
  227. if (!link->w)
  228. link->w = inlink->w;
  229. if (!link->h)
  230. link->h = inlink->h;
  231. } else if (!link->w || !link->h) {
  232. av_log(link->src, AV_LOG_ERROR,
  233. "Video source filters must set their output link's "
  234. "width and height\n");
  235. return AVERROR(EINVAL);
  236. }
  237. break;
  238. case AVMEDIA_TYPE_AUDIO:
  239. if (inlink) {
  240. if (!link->time_base.num && !link->time_base.den)
  241. link->time_base = inlink->time_base;
  242. }
  243. if (!link->time_base.num && !link->time_base.den)
  244. link->time_base = (AVRational) {1, link->sample_rate};
  245. }
  246. if ((config_link = link->dstpad->config_props))
  247. if ((ret = config_link(link)) < 0) {
  248. av_log(link->src, AV_LOG_ERROR,
  249. "Failed to configure input pad on %s\n",
  250. link->dst->name);
  251. return ret;
  252. }
  253. link->init_state = AVLINK_INIT;
  254. }
  255. }
  256. return 0;
  257. }
  258. void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
  259. {
  260. if (link->type == AVMEDIA_TYPE_VIDEO) {
  261. ff_tlog(ctx,
  262. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  263. link, link->w, link->h,
  264. av_get_pix_fmt_name(link->format),
  265. link->src ? link->src->filter->name : "",
  266. link->dst ? link->dst->filter->name : "",
  267. end ? "\n" : "");
  268. } else {
  269. char buf[128];
  270. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  271. ff_tlog(ctx,
  272. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  273. link, (int)link->sample_rate, buf,
  274. av_get_sample_fmt_name(link->format),
  275. link->src ? link->src->filter->name : "",
  276. link->dst ? link->dst->filter->name : "",
  277. end ? "\n" : "");
  278. }
  279. }
  280. int ff_request_frame(AVFilterLink *link)
  281. {
  282. int ret = -1;
  283. FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
  284. if (link->closed)
  285. return AVERROR_EOF;
  286. if (link->srcpad->request_frame)
  287. ret = link->srcpad->request_frame(link);
  288. else if (link->src->inputs[0])
  289. ret = ff_request_frame(link->src->inputs[0]);
  290. if (ret == AVERROR_EOF && link->partial_buf) {
  291. AVFilterBufferRef *pbuf = link->partial_buf;
  292. link->partial_buf = NULL;
  293. ff_filter_frame_framed(link, pbuf);
  294. return 0;
  295. }
  296. if (ret == AVERROR_EOF)
  297. link->closed = 1;
  298. return ret;
  299. }
  300. int ff_poll_frame(AVFilterLink *link)
  301. {
  302. int i, min = INT_MAX;
  303. if (link->srcpad->poll_frame)
  304. return link->srcpad->poll_frame(link);
  305. for (i = 0; i < link->src->nb_inputs; i++) {
  306. int val;
  307. if (!link->src->inputs[i])
  308. return -1;
  309. val = ff_poll_frame(link->src->inputs[i]);
  310. min = FFMIN(min, val);
  311. }
  312. return min;
  313. }
  314. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  315. {
  316. if (pts == AV_NOPTS_VALUE)
  317. return;
  318. link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  319. /* TODO use duration */
  320. if (link->graph && link->age_index >= 0)
  321. ff_avfilter_graph_update_heap(link->graph, link);
  322. }
  323. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  324. {
  325. if(!strcmp(cmd, "ping")){
  326. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  327. return 0;
  328. }else if(filter->filter->process_command) {
  329. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  330. }
  331. return AVERROR(ENOSYS);
  332. }
  333. #define MAX_REGISTERED_AVFILTERS_NB 128
  334. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  335. static int next_registered_avfilter_idx = 0;
  336. AVFilter *avfilter_get_by_name(const char *name)
  337. {
  338. int i;
  339. for (i = 0; registered_avfilters[i]; i++)
  340. if (!strcmp(registered_avfilters[i]->name, name))
  341. return registered_avfilters[i];
  342. return NULL;
  343. }
  344. int avfilter_register(AVFilter *filter)
  345. {
  346. int i;
  347. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  348. av_log(NULL, AV_LOG_ERROR,
  349. "Maximum number of registered filters %d reached, "
  350. "impossible to register filter with name '%s'\n",
  351. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  352. return AVERROR(ENOMEM);
  353. }
  354. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  355. const AVFilterPad *input = &filter->inputs[i];
  356. av_assert0( !input->filter_frame
  357. || (!input->start_frame && !input->end_frame));
  358. }
  359. registered_avfilters[next_registered_avfilter_idx++] = filter;
  360. return 0;
  361. }
  362. AVFilter **av_filter_next(AVFilter **filter)
  363. {
  364. return filter ? ++filter : &registered_avfilters[0];
  365. }
  366. void avfilter_uninit(void)
  367. {
  368. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  369. next_registered_avfilter_idx = 0;
  370. }
  371. static int pad_count(const AVFilterPad *pads)
  372. {
  373. int count;
  374. if (!pads)
  375. return 0;
  376. for(count = 0; pads->name; count ++) pads ++;
  377. return count;
  378. }
  379. static const char *default_filter_name(void *filter_ctx)
  380. {
  381. AVFilterContext *ctx = filter_ctx;
  382. return ctx->name ? ctx->name : ctx->filter->name;
  383. }
  384. static void *filter_child_next(void *obj, void *prev)
  385. {
  386. AVFilterContext *ctx = obj;
  387. if (!prev && ctx->filter && ctx->filter->priv_class)
  388. return ctx->priv;
  389. return NULL;
  390. }
  391. static const AVClass *filter_child_class_next(const AVClass *prev)
  392. {
  393. AVFilter **filter_ptr = NULL;
  394. /* find the filter that corresponds to prev */
  395. while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
  396. if ((*filter_ptr)->priv_class == prev)
  397. break;
  398. /* could not find filter corresponding to prev */
  399. if (prev && !(*filter_ptr))
  400. return NULL;
  401. /* find next filter with specific options */
  402. while (*(filter_ptr = av_filter_next(filter_ptr)))
  403. if ((*filter_ptr)->priv_class)
  404. return (*filter_ptr)->priv_class;
  405. return NULL;
  406. }
  407. static const AVClass avfilter_class = {
  408. .class_name = "AVFilter",
  409. .item_name = default_filter_name,
  410. .version = LIBAVUTIL_VERSION_INT,
  411. .category = AV_CLASS_CATEGORY_FILTER,
  412. .child_next = filter_child_next,
  413. .child_class_next = filter_child_class_next,
  414. };
  415. const AVClass *avfilter_get_class(void)
  416. {
  417. return &avfilter_class;
  418. }
  419. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  420. {
  421. AVFilterContext *ret;
  422. *filter_ctx = NULL;
  423. if (!filter)
  424. return AVERROR(EINVAL);
  425. ret = av_mallocz(sizeof(AVFilterContext));
  426. if (!ret)
  427. return AVERROR(ENOMEM);
  428. ret->av_class = &avfilter_class;
  429. ret->filter = filter;
  430. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  431. if (filter->priv_size) {
  432. ret->priv = av_mallocz(filter->priv_size);
  433. if (!ret->priv)
  434. goto err;
  435. }
  436. ret->nb_inputs = pad_count(filter->inputs);
  437. if (ret->nb_inputs ) {
  438. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  439. if (!ret->input_pads)
  440. goto err;
  441. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  442. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  443. if (!ret->inputs)
  444. goto err;
  445. }
  446. ret->nb_outputs = pad_count(filter->outputs);
  447. if (ret->nb_outputs) {
  448. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  449. if (!ret->output_pads)
  450. goto err;
  451. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  452. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  453. if (!ret->outputs)
  454. goto err;
  455. }
  456. #if FF_API_FOO_COUNT
  457. ret->output_count = ret->nb_outputs;
  458. ret->input_count = ret->nb_inputs;
  459. #endif
  460. *filter_ctx = ret;
  461. return 0;
  462. err:
  463. av_freep(&ret->inputs);
  464. av_freep(&ret->input_pads);
  465. ret->nb_inputs = 0;
  466. av_freep(&ret->outputs);
  467. av_freep(&ret->output_pads);
  468. ret->nb_outputs = 0;
  469. av_freep(&ret->priv);
  470. av_free(ret);
  471. return AVERROR(ENOMEM);
  472. }
  473. void avfilter_free(AVFilterContext *filter)
  474. {
  475. int i;
  476. AVFilterLink *link;
  477. if (!filter)
  478. return;
  479. if (filter->filter->uninit)
  480. filter->filter->uninit(filter);
  481. for (i = 0; i < filter->nb_inputs; i++) {
  482. if ((link = filter->inputs[i])) {
  483. if (link->src)
  484. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  485. ff_formats_unref(&link->in_formats);
  486. ff_formats_unref(&link->out_formats);
  487. ff_formats_unref(&link->in_samplerates);
  488. ff_formats_unref(&link->out_samplerates);
  489. ff_channel_layouts_unref(&link->in_channel_layouts);
  490. ff_channel_layouts_unref(&link->out_channel_layouts);
  491. }
  492. avfilter_link_free(&link);
  493. }
  494. for (i = 0; i < filter->nb_outputs; i++) {
  495. if ((link = filter->outputs[i])) {
  496. if (link->dst)
  497. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  498. ff_formats_unref(&link->in_formats);
  499. ff_formats_unref(&link->out_formats);
  500. ff_formats_unref(&link->in_samplerates);
  501. ff_formats_unref(&link->out_samplerates);
  502. ff_channel_layouts_unref(&link->in_channel_layouts);
  503. ff_channel_layouts_unref(&link->out_channel_layouts);
  504. }
  505. avfilter_link_free(&link);
  506. }
  507. av_freep(&filter->name);
  508. av_freep(&filter->input_pads);
  509. av_freep(&filter->output_pads);
  510. av_freep(&filter->inputs);
  511. av_freep(&filter->outputs);
  512. av_freep(&filter->priv);
  513. while(filter->command_queue){
  514. ff_command_queue_pop(filter);
  515. }
  516. av_free(filter);
  517. }
  518. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  519. {
  520. int ret=0;
  521. if (filter->filter->init_opaque)
  522. ret = filter->filter->init_opaque(filter, args, opaque);
  523. else if (filter->filter->init)
  524. ret = filter->filter->init(filter, args);
  525. return ret;
  526. }
  527. const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
  528. {
  529. return pads[pad_idx].name;
  530. }
  531. enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
  532. {
  533. return pads[pad_idx].type;
  534. }
  535. static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
  536. {
  537. return ff_filter_frame(link->dst->outputs[0], frame);
  538. }
  539. static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame)
  540. {
  541. int (*filter_frame)(AVFilterLink *, AVFilterBufferRef *);
  542. AVFilterPad *src = link->srcpad;
  543. AVFilterPad *dst = link->dstpad;
  544. AVFilterBufferRef *out;
  545. int perms, ret;
  546. AVFilterCommand *cmd= link->dst->command_queue;
  547. int64_t pts;
  548. if (link->closed) {
  549. avfilter_unref_buffer(frame);
  550. return AVERROR_EOF;
  551. }
  552. if (!(filter_frame = dst->filter_frame))
  553. filter_frame = default_filter_frame;
  554. av_assert1((frame->perms & src->min_perms) == src->min_perms);
  555. frame->perms &= ~ src->rej_perms;
  556. perms = frame->perms;
  557. if (frame->linesize[0] < 0)
  558. perms |= AV_PERM_NEG_LINESIZES;
  559. /* prepare to copy the frame if the buffer has insufficient permissions */
  560. if ((dst->min_perms & perms) != dst->min_perms ||
  561. dst->rej_perms & perms) {
  562. av_log(link->dst, AV_LOG_DEBUG,
  563. "Copying data in avfilter (have perms %x, need %x, reject %x)\n",
  564. perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  565. /* Maybe use ff_copy_buffer_ref instead? */
  566. switch (link->type) {
  567. case AVMEDIA_TYPE_VIDEO:
  568. out = ff_get_video_buffer(link, dst->min_perms,
  569. link->w, link->h);
  570. break;
  571. case AVMEDIA_TYPE_AUDIO:
  572. out = ff_get_audio_buffer(link, dst->min_perms,
  573. frame->audio->nb_samples);
  574. break;
  575. default: return AVERROR(EINVAL);
  576. }
  577. if (!out) {
  578. avfilter_unref_buffer(frame);
  579. return AVERROR(ENOMEM);
  580. }
  581. avfilter_copy_buffer_ref_props(out, frame);
  582. switch (link->type) {
  583. case AVMEDIA_TYPE_VIDEO:
  584. av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
  585. frame->format, frame->video->w, frame->video->h);
  586. break;
  587. case AVMEDIA_TYPE_AUDIO:
  588. av_samples_copy(out->extended_data, frame->extended_data,
  589. 0, 0, frame->audio->nb_samples,
  590. av_get_channel_layout_nb_channels(frame->audio->channel_layout),
  591. frame->format);
  592. break;
  593. default: return AVERROR(EINVAL);
  594. }
  595. avfilter_unref_buffer(frame);
  596. } else
  597. out = frame;
  598. while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
  599. av_log(link->dst, AV_LOG_DEBUG,
  600. "Processing command time:%f command:%s arg:%s\n",
  601. cmd->time, cmd->command, cmd->arg);
  602. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  603. ff_command_queue_pop(link->dst);
  604. cmd= link->dst->command_queue;
  605. }
  606. pts = out->pts;
  607. ret = filter_frame(link, out);
  608. ff_update_link_current_pts(link, pts);
  609. return ret;
  610. }
  611. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFilterBufferRef *frame)
  612. {
  613. int insamples = frame->audio->nb_samples, inpos = 0, nb_samples;
  614. AVFilterBufferRef *pbuf = link->partial_buf;
  615. int nb_channels = frame->audio->channels;
  616. int ret = 0;
  617. /* Handle framing (min_samples, max_samples) */
  618. while (insamples) {
  619. if (!pbuf) {
  620. AVRational samples_tb = { 1, link->sample_rate };
  621. int perms = link->dstpad->min_perms | AV_PERM_WRITE;
  622. pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
  623. if (!pbuf) {
  624. av_log(link->dst, AV_LOG_WARNING,
  625. "Samples dropped due to memory allocation failure.\n");
  626. return 0;
  627. }
  628. avfilter_copy_buffer_ref_props(pbuf, frame);
  629. pbuf->pts = frame->pts +
  630. av_rescale_q(inpos, samples_tb, link->time_base);
  631. pbuf->audio->nb_samples = 0;
  632. }
  633. nb_samples = FFMIN(insamples,
  634. link->partial_buf_size - pbuf->audio->nb_samples);
  635. av_samples_copy(pbuf->extended_data, frame->extended_data,
  636. pbuf->audio->nb_samples, inpos,
  637. nb_samples, nb_channels, link->format);
  638. inpos += nb_samples;
  639. insamples -= nb_samples;
  640. pbuf->audio->nb_samples += nb_samples;
  641. if (pbuf->audio->nb_samples >= link->min_samples) {
  642. ret = ff_filter_frame_framed(link, pbuf);
  643. pbuf = NULL;
  644. }
  645. }
  646. avfilter_unref_buffer(frame);
  647. link->partial_buf = pbuf;
  648. return ret;
  649. }
  650. int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
  651. {
  652. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  653. /* Consistency checks */
  654. if (link->type == AVMEDIA_TYPE_VIDEO) {
  655. if (strcmp(link->dst->filter->name, "scale")) {
  656. av_assert1(frame->format == link->format);
  657. av_assert1(frame->video->w == link->w);
  658. av_assert1(frame->video->h == link->h);
  659. }
  660. } else {
  661. av_assert1(frame->format == link->format);
  662. av_assert1(frame->audio->channels == link->channels);
  663. av_assert1(frame->audio->channel_layout == link->channel_layout);
  664. av_assert1(frame->audio->sample_rate == link->sample_rate);
  665. }
  666. /* Go directly to actual filtering if possible */
  667. if (link->type == AVMEDIA_TYPE_AUDIO &&
  668. link->min_samples &&
  669. (link->partial_buf ||
  670. frame->audio->nb_samples < link->min_samples ||
  671. frame->audio->nb_samples > link->max_samples)) {
  672. return ff_filter_frame_needs_framing(link, frame);
  673. } else {
  674. return ff_filter_frame_framed(link, frame);
  675. }
  676. }