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.

597 lines
19KB

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