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.

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