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.

568 lines
18KB

  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. char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  31. {
  32. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  33. perms & AV_PERM_READ ? "r" : "",
  34. perms & AV_PERM_WRITE ? "w" : "",
  35. perms & AV_PERM_PRESERVE ? "p" : "",
  36. perms & AV_PERM_REUSE ? "u" : "",
  37. perms & AV_PERM_REUSE2 ? "U" : "",
  38. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  39. return buf;
  40. }
  41. void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  42. {
  43. av_unused char buf[16];
  44. av_dlog(ctx,
  45. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  46. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  47. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  48. ref->pts, ref->pos);
  49. if (ref->video) {
  50. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  51. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  52. ref->video->w, ref->video->h,
  53. !ref->video->interlaced ? 'P' : /* Progressive */
  54. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  55. ref->video->key_frame,
  56. av_get_picture_type_char(ref->video->pict_type));
  57. }
  58. if (ref->audio) {
  59. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  60. ref->audio->channel_layout,
  61. ref->audio->nb_samples,
  62. ref->audio->sample_rate);
  63. }
  64. av_dlog(ctx, "]%s", end ? "\n" : "");
  65. }
  66. unsigned avfilter_version(void) {
  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. void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  88. AVFilterPad **pads, AVFilterLink ***links,
  89. AVFilterPad *newpad)
  90. {
  91. unsigned i;
  92. idx = FFMIN(idx, *count);
  93. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  94. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  95. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  96. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  97. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  98. (*links)[idx] = NULL;
  99. (*count)++;
  100. for (i = idx+1; i < *count; i++)
  101. if (*links[i])
  102. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  103. }
  104. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  105. AVFilterContext *dst, unsigned dstpad)
  106. {
  107. AVFilterLink *link;
  108. if (src->output_count <= srcpad || dst->input_count <= dstpad ||
  109. src->outputs[srcpad] || dst->inputs[dstpad])
  110. return -1;
  111. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  112. av_log(src, AV_LOG_ERROR,
  113. "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
  114. src->name, srcpad, dst->name, dstpad);
  115. return AVERROR(EINVAL);
  116. }
  117. src->outputs[srcpad] =
  118. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  119. link->src = src;
  120. link->dst = dst;
  121. link->srcpad = &src->output_pads[srcpad];
  122. link->dstpad = &dst->input_pads[dstpad];
  123. link->type = src->output_pads[srcpad].type;
  124. assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  125. link->format = -1;
  126. return 0;
  127. }
  128. void avfilter_link_free(AVFilterLink **link)
  129. {
  130. if (!*link)
  131. return;
  132. if ((*link)->pool)
  133. ff_free_pool((*link)->pool);
  134. av_freep(link);
  135. }
  136. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  137. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  138. {
  139. int ret;
  140. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  141. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  142. "between the filter '%s' and the filter '%s'\n",
  143. filt->name, link->src->name, link->dst->name);
  144. link->dst->inputs[dstpad_idx] = NULL;
  145. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  146. /* failed to link output filter to new filter */
  147. link->dst->inputs[dstpad_idx] = link;
  148. return ret;
  149. }
  150. /* re-hookup the link to the new destination filter we inserted */
  151. link->dst = filt;
  152. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  153. filt->inputs[filt_srcpad_idx] = link;
  154. /* if any information on supported media formats already exists on the
  155. * link, we need to preserve that */
  156. if (link->out_formats)
  157. ff_formats_changeref(&link->out_formats,
  158. &filt->outputs[filt_dstpad_idx]->out_formats);
  159. if (link->out_samplerates)
  160. ff_formats_changeref(&link->out_samplerates,
  161. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  162. if (link->out_channel_layouts)
  163. ff_channel_layouts_changeref(&link->out_channel_layouts,
  164. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  165. return 0;
  166. }
  167. int avfilter_config_links(AVFilterContext *filter)
  168. {
  169. int (*config_link)(AVFilterLink *);
  170. unsigned i;
  171. int ret;
  172. for (i = 0; i < filter->input_count; i ++) {
  173. AVFilterLink *link = filter->inputs[i];
  174. AVFilterLink *inlink = link->src->input_count ?
  175. link->src->inputs[0] : NULL;
  176. if (!link) continue;
  177. link->current_pts = AV_NOPTS_VALUE;
  178. switch (link->init_state) {
  179. case AVLINK_INIT:
  180. continue;
  181. case AVLINK_STARTINIT:
  182. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  183. return 0;
  184. case AVLINK_UNINIT:
  185. link->init_state = AVLINK_STARTINIT;
  186. if ((ret = avfilter_config_links(link->src)) < 0)
  187. return ret;
  188. if (!(config_link = link->srcpad->config_props)) {
  189. if (link->src->input_count != 1) {
  190. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  191. "with more than one input "
  192. "must set config_props() "
  193. "callbacks on all outputs\n");
  194. return AVERROR(EINVAL);
  195. }
  196. } else if ((ret = config_link(link)) < 0)
  197. return ret;
  198. switch (link->type) {
  199. case AVMEDIA_TYPE_VIDEO:
  200. if (!link->time_base.num && !link->time_base.den)
  201. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  202. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  203. link->sample_aspect_ratio = inlink ?
  204. inlink->sample_aspect_ratio : (AVRational){1,1};
  205. if (inlink) {
  206. if (!link->w)
  207. link->w = inlink->w;
  208. if (!link->h)
  209. link->h = inlink->h;
  210. } else if (!link->w || !link->h) {
  211. av_log(link->src, AV_LOG_ERROR,
  212. "Video source filters must set their output link's "
  213. "width and height\n");
  214. return AVERROR(EINVAL);
  215. }
  216. break;
  217. case AVMEDIA_TYPE_AUDIO:
  218. if (inlink) {
  219. if (!link->sample_rate)
  220. link->sample_rate = inlink->sample_rate;
  221. if (!link->time_base.num && !link->time_base.den)
  222. link->time_base = inlink->time_base;
  223. } else if (!link->sample_rate) {
  224. av_log(link->src, AV_LOG_ERROR,
  225. "Audio source filters must set their output link's "
  226. "sample_rate\n");
  227. return AVERROR(EINVAL);
  228. }
  229. if (!link->time_base.num && !link->time_base.den)
  230. link->time_base = (AVRational) {1, link->sample_rate};
  231. }
  232. if ((config_link = link->dstpad->config_props))
  233. if ((ret = config_link(link)) < 0)
  234. return ret;
  235. link->init_state = AVLINK_INIT;
  236. }
  237. }
  238. return 0;
  239. }
  240. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  241. {
  242. if (link->type == AVMEDIA_TYPE_VIDEO) {
  243. av_dlog(ctx,
  244. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  245. link, link->w, link->h,
  246. av_pix_fmt_descriptors[link->format].name,
  247. link->src ? link->src->filter->name : "",
  248. link->dst ? link->dst->filter->name : "",
  249. end ? "\n" : "");
  250. } else {
  251. char buf[128];
  252. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  253. av_dlog(ctx,
  254. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  255. link, (int)link->sample_rate, buf,
  256. av_get_sample_fmt_name(link->format),
  257. link->src ? link->src->filter->name : "",
  258. link->dst ? link->dst->filter->name : "",
  259. end ? "\n" : "");
  260. }
  261. }
  262. int ff_request_frame(AVFilterLink *link)
  263. {
  264. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  265. if (link->srcpad->request_frame)
  266. return link->srcpad->request_frame(link);
  267. else if (link->src->inputs[0])
  268. return ff_request_frame(link->src->inputs[0]);
  269. else return -1;
  270. }
  271. int ff_poll_frame(AVFilterLink *link)
  272. {
  273. int i, min = INT_MAX;
  274. if (link->srcpad->poll_frame)
  275. return link->srcpad->poll_frame(link);
  276. for (i = 0; i < link->src->input_count; i++) {
  277. int val;
  278. if (!link->src->inputs[i])
  279. return -1;
  280. val = ff_poll_frame(link->src->inputs[i]);
  281. min = FFMIN(min, val);
  282. }
  283. return min;
  284. }
  285. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  286. {
  287. if (pts == AV_NOPTS_VALUE)
  288. return;
  289. link->current_pts = pts; /* TODO use duration */
  290. if (link->graph && link->age_index >= 0)
  291. ff_avfilter_graph_update_heap(link->graph, link);
  292. }
  293. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  294. {
  295. if(!strcmp(cmd, "ping")){
  296. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  297. return 0;
  298. }else if(filter->filter->process_command) {
  299. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  300. }
  301. return AVERROR(ENOSYS);
  302. }
  303. #define MAX_REGISTERED_AVFILTERS_NB 128
  304. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  305. static int next_registered_avfilter_idx = 0;
  306. AVFilter *avfilter_get_by_name(const char *name)
  307. {
  308. int i;
  309. for (i = 0; registered_avfilters[i]; i++)
  310. if (!strcmp(registered_avfilters[i]->name, name))
  311. return registered_avfilters[i];
  312. return NULL;
  313. }
  314. int avfilter_register(AVFilter *filter)
  315. {
  316. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  317. av_log(NULL, AV_LOG_ERROR,
  318. "Maximum number of registered filters %d reached, "
  319. "impossible to register filter with name '%s'\n",
  320. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  321. return AVERROR(ENOMEM);
  322. }
  323. registered_avfilters[next_registered_avfilter_idx++] = filter;
  324. return 0;
  325. }
  326. AVFilter **av_filter_next(AVFilter **filter)
  327. {
  328. return filter ? ++filter : &registered_avfilters[0];
  329. }
  330. void avfilter_uninit(void)
  331. {
  332. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  333. next_registered_avfilter_idx = 0;
  334. }
  335. static int pad_count(const AVFilterPad *pads)
  336. {
  337. int count;
  338. for(count = 0; pads->name; count ++) pads ++;
  339. return count;
  340. }
  341. static char *default_filter_name(void *filter_ctx)
  342. {
  343. AVFilterContext *ctx = filter_ctx;
  344. return ctx->name ? ctx->name : ctx->filter->name;
  345. }
  346. static const AVClass avfilter_class = {
  347. .class_name = "AVFilter",
  348. .item_name = default_filter_name,
  349. .version = LIBAVUTIL_VERSION_INT,
  350. .category = AV_CLASS_CATEGORY_FILTER,
  351. };
  352. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  353. {
  354. AVFilterContext *ret;
  355. *filter_ctx = NULL;
  356. if (!filter)
  357. return AVERROR(EINVAL);
  358. ret = av_mallocz(sizeof(AVFilterContext));
  359. if (!ret)
  360. return AVERROR(ENOMEM);
  361. ret->av_class = &avfilter_class;
  362. ret->filter = filter;
  363. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  364. if (filter->priv_size) {
  365. ret->priv = av_mallocz(filter->priv_size);
  366. if (!ret->priv)
  367. goto err;
  368. }
  369. ret->input_count = pad_count(filter->inputs);
  370. if (ret->input_count) {
  371. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  372. if (!ret->input_pads)
  373. goto err;
  374. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  375. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  376. if (!ret->inputs)
  377. goto err;
  378. }
  379. ret->output_count = pad_count(filter->outputs);
  380. if (ret->output_count) {
  381. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  382. if (!ret->output_pads)
  383. goto err;
  384. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  385. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  386. if (!ret->outputs)
  387. goto err;
  388. }
  389. *filter_ctx = ret;
  390. return 0;
  391. err:
  392. av_freep(&ret->inputs);
  393. av_freep(&ret->input_pads);
  394. ret->input_count = 0;
  395. av_freep(&ret->outputs);
  396. av_freep(&ret->output_pads);
  397. ret->output_count = 0;
  398. av_freep(&ret->priv);
  399. av_free(ret);
  400. return AVERROR(ENOMEM);
  401. }
  402. void avfilter_free(AVFilterContext *filter)
  403. {
  404. int i;
  405. AVFilterLink *link;
  406. if (!filter)
  407. return;
  408. if (filter->filter->uninit)
  409. filter->filter->uninit(filter);
  410. for (i = 0; i < filter->input_count; i++) {
  411. if ((link = filter->inputs[i])) {
  412. if (link->src)
  413. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  414. ff_formats_unref(&link->in_formats);
  415. ff_formats_unref(&link->out_formats);
  416. ff_formats_unref(&link->in_samplerates);
  417. ff_formats_unref(&link->out_samplerates);
  418. ff_channel_layouts_unref(&link->in_channel_layouts);
  419. ff_channel_layouts_unref(&link->out_channel_layouts);
  420. }
  421. avfilter_link_free(&link);
  422. }
  423. for (i = 0; i < filter->output_count; i++) {
  424. if ((link = filter->outputs[i])) {
  425. if (link->dst)
  426. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  427. ff_formats_unref(&link->in_formats);
  428. ff_formats_unref(&link->out_formats);
  429. ff_formats_unref(&link->in_samplerates);
  430. ff_formats_unref(&link->out_samplerates);
  431. ff_channel_layouts_unref(&link->in_channel_layouts);
  432. ff_channel_layouts_unref(&link->out_channel_layouts);
  433. }
  434. avfilter_link_free(&link);
  435. }
  436. av_freep(&filter->name);
  437. av_freep(&filter->input_pads);
  438. av_freep(&filter->output_pads);
  439. av_freep(&filter->inputs);
  440. av_freep(&filter->outputs);
  441. av_freep(&filter->priv);
  442. while(filter->command_queue){
  443. ff_command_queue_pop(filter);
  444. }
  445. av_free(filter);
  446. }
  447. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  448. {
  449. int ret=0;
  450. if (filter->filter->init)
  451. ret = filter->filter->init(filter, args, opaque);
  452. return ret;
  453. }
  454. #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
  455. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  456. AVFilterPad **pads, AVFilterLink ***links,
  457. AVFilterPad *newpad)
  458. {
  459. ff_insert_pad(idx, count, padidx_off, pads, links, newpad);
  460. }
  461. void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
  462. AVFilterPad *p)
  463. {
  464. ff_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
  465. &f->input_pads, &f->inputs, p);
  466. }
  467. void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
  468. AVFilterPad *p)
  469. {
  470. ff_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
  471. &f->output_pads, &f->outputs, p);
  472. }
  473. int avfilter_poll_frame(AVFilterLink *link)
  474. {
  475. return ff_poll_frame(link);
  476. }
  477. int avfilter_request_frame(AVFilterLink *link)
  478. {
  479. return ff_request_frame(link);
  480. }
  481. #endif