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.

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