avfilter.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. /* #define DEBUG */
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/rational.h"
  24. #include "libavutil/audioconvert.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/avstring.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. unsigned avfilter_version(void) {
  31. av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
  32. return LIBAVFILTER_VERSION_INT;
  33. }
  34. const char *avfilter_configuration(void)
  35. {
  36. return FFMPEG_CONFIGURATION;
  37. }
  38. const char *avfilter_license(void)
  39. {
  40. #define LICENSE_PREFIX "libavfilter license: "
  41. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  42. }
  43. void ff_command_queue_pop(AVFilterContext *filter)
  44. {
  45. AVFilterCommand *c= filter->command_queue;
  46. av_freep(&c->arg);
  47. av_freep(&c->command);
  48. filter->command_queue= c->next;
  49. av_free(c);
  50. }
  51. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  52. AVFilterPad **pads, AVFilterLink ***links,
  53. AVFilterPad *newpad)
  54. {
  55. unsigned i;
  56. idx = FFMIN(idx, *count);
  57. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  58. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  59. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  60. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  61. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  62. (*links)[idx] = NULL;
  63. (*count)++;
  64. for (i = idx + 1; i < *count; i++)
  65. if ((*links)[i])
  66. (*(unsigned *)((uint8_t *) (*links)[i] + padidx_off))++;
  67. }
  68. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  69. AVFilterContext *dst, unsigned dstpad)
  70. {
  71. AVFilterLink *link;
  72. if (src->output_count <= srcpad || dst->input_count <= dstpad ||
  73. src->outputs[srcpad] || dst->inputs[dstpad])
  74. return -1;
  75. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  76. av_log(src, AV_LOG_ERROR,
  77. "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
  78. src->name, srcpad, dst->name, dstpad);
  79. return AVERROR(EINVAL);
  80. }
  81. src->outputs[srcpad] =
  82. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  83. link->src = src;
  84. link->dst = dst;
  85. link->srcpad = &src->output_pads[srcpad];
  86. link->dstpad = &dst->input_pads[dstpad];
  87. link->type = src->output_pads[srcpad].type;
  88. assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  89. link->format = -1;
  90. return 0;
  91. }
  92. void avfilter_link_free(AVFilterLink **link)
  93. {
  94. if (!*link)
  95. return;
  96. if ((*link)->pool)
  97. ff_free_pool((*link)->pool);
  98. av_freep(link);
  99. }
  100. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  101. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  102. {
  103. int ret;
  104. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  105. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  106. "between the filter '%s' and the filter '%s'\n",
  107. filt->name, link->src->name, link->dst->name);
  108. link->dst->inputs[dstpad_idx] = NULL;
  109. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  110. /* failed to link output filter to new filter */
  111. link->dst->inputs[dstpad_idx] = link;
  112. return ret;
  113. }
  114. /* re-hookup the link to the new destination filter we inserted */
  115. link->dst = filt;
  116. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  117. filt->inputs[filt_srcpad_idx] = link;
  118. /* if any information on supported media formats already exists on the
  119. * link, we need to preserve that */
  120. if (link->out_formats)
  121. avfilter_formats_changeref(&link->out_formats,
  122. &filt->outputs[filt_dstpad_idx]->out_formats);
  123. if (link->out_channel_layouts)
  124. ff_channel_layouts_changeref(&link->out_channel_layouts,
  125. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  126. if (link->out_samplerates)
  127. avfilter_formats_changeref(&link->out_samplerates,
  128. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  129. return 0;
  130. }
  131. int avfilter_config_links(AVFilterContext *filter)
  132. {
  133. int (*config_link)(AVFilterLink *);
  134. unsigned i;
  135. int ret;
  136. for (i = 0; i < filter->input_count; i ++) {
  137. AVFilterLink *link = filter->inputs[i];
  138. AVFilterLink *inlink = link->src->input_count ?
  139. link->src->inputs[0] : NULL;
  140. if (!link) continue;
  141. link->current_pts = AV_NOPTS_VALUE;
  142. switch (link->init_state) {
  143. case AVLINK_INIT:
  144. continue;
  145. case AVLINK_STARTINIT:
  146. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  147. return 0;
  148. case AVLINK_UNINIT:
  149. link->init_state = AVLINK_STARTINIT;
  150. if ((ret = avfilter_config_links(link->src)) < 0)
  151. return ret;
  152. if (!(config_link = link->srcpad->config_props)) {
  153. if (link->src->input_count != 1) {
  154. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  155. "with more than one input "
  156. "must set config_props() "
  157. "callbacks on all outputs\n");
  158. return AVERROR(EINVAL);
  159. }
  160. } else if ((ret = config_link(link)) < 0)
  161. return ret;
  162. switch (link->type) {
  163. case AVMEDIA_TYPE_VIDEO:
  164. if (!link->time_base.num && !link->time_base.den)
  165. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  166. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  167. link->sample_aspect_ratio = inlink ?
  168. inlink->sample_aspect_ratio : (AVRational){1,1};
  169. if (inlink) {
  170. if (!link->w)
  171. link->w = inlink->w;
  172. if (!link->h)
  173. link->h = inlink->h;
  174. } else if (!link->w || !link->h) {
  175. av_log(link->src, AV_LOG_ERROR,
  176. "Video source filters must set their output link's "
  177. "width and height\n");
  178. return AVERROR(EINVAL);
  179. }
  180. break;
  181. case AVMEDIA_TYPE_AUDIO:
  182. if (inlink) {
  183. if (!link->sample_rate)
  184. link->sample_rate = inlink->sample_rate;
  185. if (!link->time_base.num && !link->time_base.den)
  186. link->time_base = inlink->time_base;
  187. } else if (!link->sample_rate) {
  188. av_log(link->src, AV_LOG_ERROR,
  189. "Audio source filters must set their output link's "
  190. "sample_rate\n");
  191. return AVERROR(EINVAL);
  192. }
  193. if (!link->time_base.num && !link->time_base.den)
  194. link->time_base = (AVRational) {1, link->sample_rate};
  195. }
  196. if ((config_link = link->dstpad->config_props))
  197. if ((ret = config_link(link)) < 0)
  198. return ret;
  199. link->init_state = AVLINK_INIT;
  200. }
  201. }
  202. return 0;
  203. }
  204. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  205. {
  206. if (link->type == AVMEDIA_TYPE_VIDEO) {
  207. av_dlog(ctx,
  208. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  209. link, link->w, link->h,
  210. av_pix_fmt_descriptors[link->format].name,
  211. link->src ? link->src->filter->name : "",
  212. link->dst ? link->dst->filter->name : "",
  213. end ? "\n" : "");
  214. } else {
  215. char buf[128];
  216. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  217. av_dlog(ctx,
  218. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  219. link, (int)link->sample_rate, buf,
  220. av_get_sample_fmt_name(link->format),
  221. link->src ? link->src->filter->name : "",
  222. link->dst ? link->dst->filter->name : "",
  223. end ? "\n" : "");
  224. }
  225. }
  226. int avfilter_request_frame(AVFilterLink *link)
  227. {
  228. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  229. if (link->srcpad->request_frame)
  230. return link->srcpad->request_frame(link);
  231. else if (link->src->inputs[0])
  232. return avfilter_request_frame(link->src->inputs[0]);
  233. else return -1;
  234. }
  235. int avfilter_poll_frame(AVFilterLink *link)
  236. {
  237. int i, min = INT_MAX;
  238. if (link->srcpad->poll_frame)
  239. return link->srcpad->poll_frame(link);
  240. for (i = 0; i < link->src->input_count; i++) {
  241. int val;
  242. if (!link->src->inputs[i])
  243. return -1;
  244. val = avfilter_poll_frame(link->src->inputs[i]);
  245. min = FFMIN(min, val);
  246. }
  247. return min;
  248. }
  249. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  250. {
  251. if (pts == AV_NOPTS_VALUE)
  252. return;
  253. link->current_pts = pts; /* TODO use duration */
  254. if (link->graph && link->age_index >= 0)
  255. ff_avfilter_graph_update_heap(link->graph, link);
  256. }
  257. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  258. {
  259. if(!strcmp(cmd, "ping")){
  260. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  261. return 0;
  262. }else if(filter->filter->process_command) {
  263. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  264. }
  265. return AVERROR(ENOSYS);
  266. }
  267. #define MAX_REGISTERED_AVFILTERS_NB 128
  268. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  269. static int next_registered_avfilter_idx = 0;
  270. AVFilter *avfilter_get_by_name(const char *name)
  271. {
  272. int i;
  273. for (i = 0; registered_avfilters[i]; i++)
  274. if (!strcmp(registered_avfilters[i]->name, name))
  275. return registered_avfilters[i];
  276. return NULL;
  277. }
  278. int avfilter_register(AVFilter *filter)
  279. {
  280. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  281. av_log(NULL, AV_LOG_ERROR,
  282. "Maximum number of registered filters %d reached, "
  283. "impossible to register filter with name '%s'\n",
  284. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  285. return AVERROR(ENOMEM);
  286. }
  287. registered_avfilters[next_registered_avfilter_idx++] = filter;
  288. return 0;
  289. }
  290. AVFilter **av_filter_next(AVFilter **filter)
  291. {
  292. return filter ? ++filter : &registered_avfilters[0];
  293. }
  294. void avfilter_uninit(void)
  295. {
  296. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  297. next_registered_avfilter_idx = 0;
  298. }
  299. static int pad_count(const AVFilterPad *pads)
  300. {
  301. int count;
  302. for(count = 0; pads->name; count ++) pads ++;
  303. return count;
  304. }
  305. static const char *filter_name(void *p)
  306. {
  307. AVFilterContext *filter = p;
  308. return filter->filter->name;
  309. }
  310. static const AVClass avfilter_class = {
  311. "AVFilter",
  312. filter_name,
  313. NULL,
  314. LIBAVUTIL_VERSION_INT,
  315. };
  316. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  317. {
  318. AVFilterContext *ret;
  319. *filter_ctx = NULL;
  320. if (!filter)
  321. return AVERROR(EINVAL);
  322. ret = av_mallocz(sizeof(AVFilterContext));
  323. if (!ret)
  324. return AVERROR(ENOMEM);
  325. ret->av_class = &avfilter_class;
  326. ret->filter = filter;
  327. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  328. if (filter->priv_size) {
  329. ret->priv = av_mallocz(filter->priv_size);
  330. if (!ret->priv)
  331. goto err;
  332. }
  333. ret->input_count = pad_count(filter->inputs);
  334. if (ret->input_count) {
  335. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  336. if (!ret->input_pads)
  337. goto err;
  338. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  339. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  340. if (!ret->inputs)
  341. goto err;
  342. }
  343. ret->output_count = pad_count(filter->outputs);
  344. if (ret->output_count) {
  345. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  346. if (!ret->output_pads)
  347. goto err;
  348. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  349. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  350. if (!ret->outputs)
  351. goto err;
  352. }
  353. *filter_ctx = ret;
  354. return 0;
  355. err:
  356. av_freep(&ret->inputs);
  357. av_freep(&ret->input_pads);
  358. ret->input_count = 0;
  359. av_freep(&ret->outputs);
  360. av_freep(&ret->output_pads);
  361. ret->output_count = 0;
  362. av_freep(&ret->priv);
  363. av_free(ret);
  364. return AVERROR(ENOMEM);
  365. }
  366. void avfilter_free(AVFilterContext *filter)
  367. {
  368. int i;
  369. AVFilterLink *link;
  370. if (!filter)
  371. return;
  372. if (filter->filter->uninit)
  373. filter->filter->uninit(filter);
  374. for (i = 0; i < filter->input_count; i++) {
  375. if ((link = filter->inputs[i])) {
  376. if (link->src)
  377. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  378. avfilter_formats_unref(&link->in_formats);
  379. avfilter_formats_unref(&link->out_formats);
  380. avfilter_formats_unref(&link->in_samplerates);
  381. avfilter_formats_unref(&link->out_samplerates);
  382. ff_channel_layouts_unref(&link->in_channel_layouts);
  383. ff_channel_layouts_unref(&link->out_channel_layouts);
  384. }
  385. avfilter_link_free(&link);
  386. }
  387. for (i = 0; i < filter->output_count; i++) {
  388. if ((link = filter->outputs[i])) {
  389. if (link->dst)
  390. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  391. avfilter_formats_unref(&link->in_formats);
  392. avfilter_formats_unref(&link->out_formats);
  393. avfilter_formats_unref(&link->in_samplerates);
  394. avfilter_formats_unref(&link->out_samplerates);
  395. ff_channel_layouts_unref(&link->in_channel_layouts);
  396. ff_channel_layouts_unref(&link->out_channel_layouts);
  397. }
  398. avfilter_link_free(&link);
  399. }
  400. av_freep(&filter->name);
  401. av_freep(&filter->input_pads);
  402. av_freep(&filter->output_pads);
  403. av_freep(&filter->inputs);
  404. av_freep(&filter->outputs);
  405. av_freep(&filter->priv);
  406. while(filter->command_queue){
  407. ff_command_queue_pop(filter);
  408. }
  409. av_free(filter);
  410. }
  411. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  412. {
  413. int ret=0;
  414. if (filter->filter->init)
  415. ret = filter->filter->init(filter, args, opaque);
  416. return ret;
  417. }