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.

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