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.

571 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 && !link->frame_rate.num && !link->frame_rate.den)
  206. link->frame_rate = inlink->frame_rate;
  207. if (inlink) {
  208. if (!link->w)
  209. link->w = inlink->w;
  210. if (!link->h)
  211. link->h = inlink->h;
  212. } else if (!link->w || !link->h) {
  213. av_log(link->src, AV_LOG_ERROR,
  214. "Video source filters must set their output link's "
  215. "width and height\n");
  216. return AVERROR(EINVAL);
  217. }
  218. break;
  219. case AVMEDIA_TYPE_AUDIO:
  220. if (inlink) {
  221. if (!link->sample_rate)
  222. link->sample_rate = inlink->sample_rate;
  223. if (!link->time_base.num && !link->time_base.den)
  224. link->time_base = inlink->time_base;
  225. } else if (!link->sample_rate) {
  226. av_log(link->src, AV_LOG_ERROR,
  227. "Audio source filters must set their output link's "
  228. "sample_rate\n");
  229. return AVERROR(EINVAL);
  230. }
  231. if (!link->time_base.num && !link->time_base.den)
  232. link->time_base = (AVRational) {1, link->sample_rate};
  233. }
  234. if ((config_link = link->dstpad->config_props))
  235. if ((ret = config_link(link)) < 0)
  236. return ret;
  237. link->init_state = AVLINK_INIT;
  238. }
  239. }
  240. return 0;
  241. }
  242. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  243. {
  244. if (link->type == AVMEDIA_TYPE_VIDEO) {
  245. av_dlog(ctx,
  246. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  247. link, link->w, link->h,
  248. av_pix_fmt_descriptors[link->format].name,
  249. link->src ? link->src->filter->name : "",
  250. link->dst ? link->dst->filter->name : "",
  251. end ? "\n" : "");
  252. } else {
  253. char buf[128];
  254. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  255. av_dlog(ctx,
  256. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  257. link, (int)link->sample_rate, buf,
  258. av_get_sample_fmt_name(link->format),
  259. link->src ? link->src->filter->name : "",
  260. link->dst ? link->dst->filter->name : "",
  261. end ? "\n" : "");
  262. }
  263. }
  264. int ff_request_frame(AVFilterLink *link)
  265. {
  266. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  267. if (link->srcpad->request_frame)
  268. return link->srcpad->request_frame(link);
  269. else if (link->src->inputs[0])
  270. return ff_request_frame(link->src->inputs[0]);
  271. else return -1;
  272. }
  273. int ff_poll_frame(AVFilterLink *link)
  274. {
  275. int i, min = INT_MAX;
  276. if (link->srcpad->poll_frame)
  277. return link->srcpad->poll_frame(link);
  278. for (i = 0; i < link->src->input_count; i++) {
  279. int val;
  280. if (!link->src->inputs[i])
  281. return -1;
  282. val = ff_poll_frame(link->src->inputs[i]);
  283. min = FFMIN(min, val);
  284. }
  285. return min;
  286. }
  287. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  288. {
  289. if (pts == AV_NOPTS_VALUE)
  290. return;
  291. link->current_pts = pts; /* TODO use duration */
  292. if (link->graph && link->age_index >= 0)
  293. ff_avfilter_graph_update_heap(link->graph, link);
  294. }
  295. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  296. {
  297. if(!strcmp(cmd, "ping")){
  298. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  299. return 0;
  300. }else if(filter->filter->process_command) {
  301. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  302. }
  303. return AVERROR(ENOSYS);
  304. }
  305. #define MAX_REGISTERED_AVFILTERS_NB 128
  306. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  307. static int next_registered_avfilter_idx = 0;
  308. AVFilter *avfilter_get_by_name(const char *name)
  309. {
  310. int i;
  311. for (i = 0; registered_avfilters[i]; i++)
  312. if (!strcmp(registered_avfilters[i]->name, name))
  313. return registered_avfilters[i];
  314. return NULL;
  315. }
  316. int avfilter_register(AVFilter *filter)
  317. {
  318. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  319. av_log(NULL, AV_LOG_ERROR,
  320. "Maximum number of registered filters %d reached, "
  321. "impossible to register filter with name '%s'\n",
  322. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  323. return AVERROR(ENOMEM);
  324. }
  325. registered_avfilters[next_registered_avfilter_idx++] = filter;
  326. return 0;
  327. }
  328. AVFilter **av_filter_next(AVFilter **filter)
  329. {
  330. return filter ? ++filter : &registered_avfilters[0];
  331. }
  332. void avfilter_uninit(void)
  333. {
  334. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  335. next_registered_avfilter_idx = 0;
  336. }
  337. static int pad_count(const AVFilterPad *pads)
  338. {
  339. int count;
  340. for(count = 0; pads->name; count ++) pads ++;
  341. return count;
  342. }
  343. static char *default_filter_name(void *filter_ctx)
  344. {
  345. AVFilterContext *ctx = filter_ctx;
  346. return ctx->name ? ctx->name : ctx->filter->name;
  347. }
  348. static const AVClass avfilter_class = {
  349. .class_name = "AVFilter",
  350. .item_name = default_filter_name,
  351. .version = LIBAVUTIL_VERSION_INT,
  352. .category = AV_CLASS_CATEGORY_FILTER,
  353. };
  354. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  355. {
  356. AVFilterContext *ret;
  357. *filter_ctx = NULL;
  358. if (!filter)
  359. return AVERROR(EINVAL);
  360. ret = av_mallocz(sizeof(AVFilterContext));
  361. if (!ret)
  362. return AVERROR(ENOMEM);
  363. ret->av_class = &avfilter_class;
  364. ret->filter = filter;
  365. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  366. if (filter->priv_size) {
  367. ret->priv = av_mallocz(filter->priv_size);
  368. if (!ret->priv)
  369. goto err;
  370. }
  371. ret->input_count = pad_count(filter->inputs);
  372. if (ret->input_count) {
  373. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  374. if (!ret->input_pads)
  375. goto err;
  376. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  377. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  378. if (!ret->inputs)
  379. goto err;
  380. }
  381. ret->output_count = pad_count(filter->outputs);
  382. if (ret->output_count) {
  383. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  384. if (!ret->output_pads)
  385. goto err;
  386. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  387. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  388. if (!ret->outputs)
  389. goto err;
  390. }
  391. *filter_ctx = ret;
  392. return 0;
  393. err:
  394. av_freep(&ret->inputs);
  395. av_freep(&ret->input_pads);
  396. ret->input_count = 0;
  397. av_freep(&ret->outputs);
  398. av_freep(&ret->output_pads);
  399. ret->output_count = 0;
  400. av_freep(&ret->priv);
  401. av_free(ret);
  402. return AVERROR(ENOMEM);
  403. }
  404. void avfilter_free(AVFilterContext *filter)
  405. {
  406. int i;
  407. AVFilterLink *link;
  408. if (!filter)
  409. return;
  410. if (filter->filter->uninit)
  411. filter->filter->uninit(filter);
  412. for (i = 0; i < filter->input_count; i++) {
  413. if ((link = filter->inputs[i])) {
  414. if (link->src)
  415. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  416. ff_formats_unref(&link->in_formats);
  417. ff_formats_unref(&link->out_formats);
  418. ff_formats_unref(&link->in_samplerates);
  419. ff_formats_unref(&link->out_samplerates);
  420. ff_channel_layouts_unref(&link->in_channel_layouts);
  421. ff_channel_layouts_unref(&link->out_channel_layouts);
  422. }
  423. avfilter_link_free(&link);
  424. }
  425. for (i = 0; i < filter->output_count; i++) {
  426. if ((link = filter->outputs[i])) {
  427. if (link->dst)
  428. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  429. ff_formats_unref(&link->in_formats);
  430. ff_formats_unref(&link->out_formats);
  431. ff_formats_unref(&link->in_samplerates);
  432. ff_formats_unref(&link->out_samplerates);
  433. ff_channel_layouts_unref(&link->in_channel_layouts);
  434. ff_channel_layouts_unref(&link->out_channel_layouts);
  435. }
  436. avfilter_link_free(&link);
  437. }
  438. av_freep(&filter->name);
  439. av_freep(&filter->input_pads);
  440. av_freep(&filter->output_pads);
  441. av_freep(&filter->inputs);
  442. av_freep(&filter->outputs);
  443. av_freep(&filter->priv);
  444. while(filter->command_queue){
  445. ff_command_queue_pop(filter);
  446. }
  447. av_free(filter);
  448. }
  449. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  450. {
  451. int ret=0;
  452. if (filter->filter->init)
  453. ret = filter->filter->init(filter, args, opaque);
  454. return ret;
  455. }
  456. #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
  457. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  458. AVFilterPad **pads, AVFilterLink ***links,
  459. AVFilterPad *newpad)
  460. {
  461. ff_insert_pad(idx, count, padidx_off, pads, links, newpad);
  462. }
  463. void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
  464. AVFilterPad *p)
  465. {
  466. ff_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
  467. &f->input_pads, &f->inputs, p);
  468. }
  469. void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
  470. AVFilterPad *p)
  471. {
  472. ff_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
  473. &f->output_pads, &f->outputs, p);
  474. }
  475. int avfilter_poll_frame(AVFilterLink *link)
  476. {
  477. return ff_poll_frame(link);
  478. }
  479. int avfilter_request_frame(AVFilterLink *link)
  480. {
  481. return ff_request_frame(link);
  482. }
  483. #endif