You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
16KB

  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_packing)
  127. avfilter_formats_changeref(&link->out_packing,
  128. &filt->outputs[filt_dstpad_idx]->out_packing);
  129. if (link->out_samplerates)
  130. avfilter_formats_changeref(&link->out_samplerates,
  131. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  132. return 0;
  133. }
  134. int avfilter_config_links(AVFilterContext *filter)
  135. {
  136. int (*config_link)(AVFilterLink *);
  137. unsigned i;
  138. int ret;
  139. for (i = 0; i < filter->input_count; i ++) {
  140. AVFilterLink *link = filter->inputs[i];
  141. AVFilterLink *inlink = link->src->input_count ?
  142. link->src->inputs[0] : NULL;
  143. if (!link) continue;
  144. link->current_pts = AV_NOPTS_VALUE;
  145. switch (link->init_state) {
  146. case AVLINK_INIT:
  147. continue;
  148. case AVLINK_STARTINIT:
  149. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  150. return 0;
  151. case AVLINK_UNINIT:
  152. link->init_state = AVLINK_STARTINIT;
  153. if ((ret = avfilter_config_links(link->src)) < 0)
  154. return ret;
  155. if (!(config_link = link->srcpad->config_props)) {
  156. if (link->src->input_count != 1) {
  157. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  158. "with more than one input "
  159. "must set config_props() "
  160. "callbacks on all outputs\n");
  161. return AVERROR(EINVAL);
  162. }
  163. } else if ((ret = config_link(link)) < 0)
  164. return ret;
  165. switch (link->type) {
  166. case AVMEDIA_TYPE_VIDEO:
  167. if (!link->time_base.num && !link->time_base.den)
  168. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  169. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  170. link->sample_aspect_ratio = inlink ?
  171. inlink->sample_aspect_ratio : (AVRational){1,1};
  172. #if 1
  173. if (inlink) {
  174. if (!link->w)
  175. link->w = inlink->w;
  176. if (!link->h)
  177. link->h = inlink->h;
  178. } else if (!link->w || !link->h) {
  179. av_log(link->src, AV_LOG_ERROR,
  180. "Video source filters must set their output link's "
  181. "width and height\n");
  182. return AVERROR(EINVAL);
  183. }
  184. break;
  185. case AVMEDIA_TYPE_AUDIO:
  186. if (inlink) {
  187. if (!link->sample_rate)
  188. link->sample_rate = inlink->sample_rate;
  189. if (!link->time_base.num && !link->time_base.den)
  190. link->time_base = inlink->time_base;
  191. } else if (!link->sample_rate) {
  192. av_log(link->src, AV_LOG_ERROR,
  193. "Audio source filters must set their output link's "
  194. "sample_rate\n");
  195. return AVERROR(EINVAL);
  196. }
  197. if (!link->time_base.num && !link->time_base.den)
  198. link->time_base = (AVRational) {1, link->sample_rate};
  199. }
  200. #endif
  201. if ((config_link = link->dstpad->config_props))
  202. if ((ret = config_link(link)) < 0)
  203. return ret;
  204. link->init_state = AVLINK_INIT;
  205. }
  206. }
  207. return 0;
  208. }
  209. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  210. {
  211. if (link->type == AVMEDIA_TYPE_VIDEO) {
  212. av_dlog(ctx,
  213. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  214. link, link->w, link->h,
  215. av_pix_fmt_descriptors[link->format].name,
  216. link->src ? link->src->filter->name : "",
  217. link->dst ? link->dst->filter->name : "",
  218. end ? "\n" : "");
  219. } else {
  220. char buf[128];
  221. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  222. av_dlog(ctx,
  223. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  224. link, (int)link->sample_rate, buf,
  225. av_get_sample_fmt_name(link->format),
  226. link->src ? link->src->filter->name : "",
  227. link->dst ? link->dst->filter->name : "",
  228. end ? "\n" : "");
  229. }
  230. }
  231. int avfilter_request_frame(AVFilterLink *link)
  232. {
  233. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  234. if (link->srcpad->request_frame)
  235. return link->srcpad->request_frame(link);
  236. else if (link->src->inputs[0])
  237. return avfilter_request_frame(link->src->inputs[0]);
  238. else return -1;
  239. }
  240. int avfilter_poll_frame(AVFilterLink *link)
  241. {
  242. int i, min = INT_MAX;
  243. if (link->srcpad->poll_frame)
  244. return link->srcpad->poll_frame(link);
  245. for (i = 0; i < link->src->input_count; i++) {
  246. int val;
  247. if (!link->src->inputs[i])
  248. return -1;
  249. val = avfilter_poll_frame(link->src->inputs[i]);
  250. min = FFMIN(min, val);
  251. }
  252. return min;
  253. }
  254. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  255. {
  256. if (pts == AV_NOPTS_VALUE)
  257. return;
  258. link->current_pts = pts; /* TODO use duration */
  259. if (link->graph && link->age_index >= 0)
  260. ff_avfilter_graph_update_heap(link->graph, link);
  261. }
  262. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  263. {
  264. if(!strcmp(cmd, "ping")){
  265. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  266. return 0;
  267. }else if(filter->filter->process_command) {
  268. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  269. }
  270. return AVERROR(ENOSYS);
  271. }
  272. #define MAX_REGISTERED_AVFILTERS_NB 128
  273. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  274. static int next_registered_avfilter_idx = 0;
  275. AVFilter *avfilter_get_by_name(const char *name)
  276. {
  277. int i;
  278. for (i = 0; registered_avfilters[i]; i++)
  279. if (!strcmp(registered_avfilters[i]->name, name))
  280. return registered_avfilters[i];
  281. return NULL;
  282. }
  283. int avfilter_register(AVFilter *filter)
  284. {
  285. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  286. av_log(NULL, AV_LOG_ERROR,
  287. "Maximum number of registered filters %d reached, "
  288. "impossible to register filter with name '%s'\n",
  289. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  290. return AVERROR(ENOMEM);
  291. }
  292. registered_avfilters[next_registered_avfilter_idx++] = filter;
  293. return 0;
  294. }
  295. AVFilter **av_filter_next(AVFilter **filter)
  296. {
  297. return filter ? ++filter : &registered_avfilters[0];
  298. }
  299. void avfilter_uninit(void)
  300. {
  301. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  302. next_registered_avfilter_idx = 0;
  303. }
  304. static int pad_count(const AVFilterPad *pads)
  305. {
  306. int count;
  307. for(count = 0; pads->name; count ++) pads ++;
  308. return count;
  309. }
  310. static const char *filter_name(void *p)
  311. {
  312. AVFilterContext *filter = p;
  313. return filter->filter->name;
  314. }
  315. static const AVClass avfilter_class = {
  316. "AVFilter",
  317. filter_name,
  318. NULL,
  319. LIBAVUTIL_VERSION_INT,
  320. };
  321. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  322. {
  323. AVFilterContext *ret;
  324. *filter_ctx = NULL;
  325. if (!filter)
  326. return AVERROR(EINVAL);
  327. ret = av_mallocz(sizeof(AVFilterContext));
  328. if (!ret)
  329. return AVERROR(ENOMEM);
  330. ret->av_class = &avfilter_class;
  331. ret->filter = filter;
  332. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  333. if (filter->priv_size) {
  334. ret->priv = av_mallocz(filter->priv_size);
  335. if (!ret->priv)
  336. goto err;
  337. }
  338. ret->input_count = pad_count(filter->inputs);
  339. if (ret->input_count) {
  340. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  341. if (!ret->input_pads)
  342. goto err;
  343. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  344. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  345. if (!ret->inputs)
  346. goto err;
  347. }
  348. ret->output_count = pad_count(filter->outputs);
  349. if (ret->output_count) {
  350. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  351. if (!ret->output_pads)
  352. goto err;
  353. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  354. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  355. if (!ret->outputs)
  356. goto err;
  357. }
  358. *filter_ctx = ret;
  359. return 0;
  360. err:
  361. av_freep(&ret->inputs);
  362. av_freep(&ret->input_pads);
  363. ret->input_count = 0;
  364. av_freep(&ret->outputs);
  365. av_freep(&ret->output_pads);
  366. ret->output_count = 0;
  367. av_freep(&ret->priv);
  368. av_free(ret);
  369. return AVERROR(ENOMEM);
  370. }
  371. void avfilter_free(AVFilterContext *filter)
  372. {
  373. int i;
  374. AVFilterLink *link;
  375. if (!filter)
  376. return;
  377. if (filter->filter->uninit)
  378. filter->filter->uninit(filter);
  379. for (i = 0; i < filter->input_count; i++) {
  380. if ((link = filter->inputs[i])) {
  381. if (link->src)
  382. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  383. avfilter_formats_unref(&link->in_formats);
  384. avfilter_formats_unref(&link->out_formats);
  385. avfilter_formats_unref(&link->in_samplerates);
  386. avfilter_formats_unref(&link->out_samplerates);
  387. ff_channel_layouts_unref(&link->in_channel_layouts);
  388. ff_channel_layouts_unref(&link->out_channel_layouts);
  389. }
  390. avfilter_link_free(&link);
  391. }
  392. for (i = 0; i < filter->output_count; i++) {
  393. if ((link = filter->outputs[i])) {
  394. if (link->dst)
  395. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  396. avfilter_formats_unref(&link->in_formats);
  397. avfilter_formats_unref(&link->out_formats);
  398. avfilter_formats_unref(&link->in_samplerates);
  399. avfilter_formats_unref(&link->out_samplerates);
  400. ff_channel_layouts_unref(&link->in_channel_layouts);
  401. ff_channel_layouts_unref(&link->out_channel_layouts);
  402. }
  403. avfilter_link_free(&link);
  404. }
  405. av_freep(&filter->name);
  406. av_freep(&filter->input_pads);
  407. av_freep(&filter->output_pads);
  408. av_freep(&filter->inputs);
  409. av_freep(&filter->outputs);
  410. av_freep(&filter->priv);
  411. while(filter->command_queue){
  412. ff_command_queue_pop(filter);
  413. }
  414. av_free(filter);
  415. }
  416. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  417. {
  418. int ret=0;
  419. if (filter->filter->init)
  420. ret = filter->filter->init(filter, args, opaque);
  421. return ret;
  422. }