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.

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