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.

621 lines
20KB

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