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.

655 lines
21KB

  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. /* #define DEBUG */
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/rational.h"
  24. #include "libavutil/audioconvert.h"
  25. #include "libavutil/imgutils.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. unsigned avfilter_version(void) {
  29. return LIBAVFILTER_VERSION_INT;
  30. }
  31. const char *avfilter_configuration(void)
  32. {
  33. return FFMPEG_CONFIGURATION;
  34. }
  35. const char *avfilter_license(void)
  36. {
  37. #define LICENSE_PREFIX "libavfilter license: "
  38. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  39. }
  40. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  41. {
  42. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  43. if (!ret)
  44. return NULL;
  45. *ret = *ref;
  46. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  47. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  48. if (!ret->video) {
  49. av_free(ret);
  50. return NULL;
  51. }
  52. *ret->video = *ref->video;
  53. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  54. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  55. if (!ret->audio) {
  56. av_free(ret);
  57. return NULL;
  58. }
  59. *ret->audio = *ref->audio;
  60. }
  61. ret->perms &= pmask;
  62. ret->buf->refcount ++;
  63. return ret;
  64. }
  65. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  66. {
  67. if (!ref)
  68. return;
  69. if (!(--ref->buf->refcount))
  70. ref->buf->free(ref->buf);
  71. av_free(ref->video);
  72. av_free(ref->audio);
  73. av_free(ref);
  74. }
  75. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  76. AVFilterPad **pads, AVFilterLink ***links,
  77. AVFilterPad *newpad)
  78. {
  79. unsigned i;
  80. idx = FFMIN(idx, *count);
  81. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  82. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  83. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  84. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  85. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  86. (*links)[idx] = NULL;
  87. (*count)++;
  88. for (i = idx+1; i < *count; i++)
  89. if (*links[i])
  90. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  91. }
  92. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  93. AVFilterContext *dst, unsigned dstpad)
  94. {
  95. AVFilterLink *link;
  96. if (src->output_count <= srcpad || dst->input_count <= dstpad ||
  97. src->outputs[srcpad] || dst->inputs[dstpad])
  98. return -1;
  99. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  100. av_log(src, AV_LOG_ERROR,
  101. "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
  102. src->name, srcpad, dst->name, dstpad);
  103. return AVERROR(EINVAL);
  104. }
  105. src->outputs[srcpad] =
  106. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  107. link->src = src;
  108. link->dst = dst;
  109. link->srcpad = &src->output_pads[srcpad];
  110. link->dstpad = &dst->input_pads[dstpad];
  111. link->type = src->output_pads[srcpad].type;
  112. assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  113. link->format = -1;
  114. return 0;
  115. }
  116. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  117. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  118. {
  119. int ret;
  120. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  121. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  122. "between the filter '%s' and the filter '%s'\n",
  123. filt->name, link->src->name, link->dst->name);
  124. link->dst->inputs[dstpad_idx] = NULL;
  125. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  126. /* failed to link output filter to new filter */
  127. link->dst->inputs[dstpad_idx] = link;
  128. return ret;
  129. }
  130. /* re-hookup the link to the new destination filter we inserted */
  131. link->dst = filt;
  132. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  133. filt->inputs[filt_srcpad_idx] = link;
  134. /* if any information on supported media formats already exists on the
  135. * link, we need to preserve that */
  136. if (link->out_formats)
  137. avfilter_formats_changeref(&link->out_formats,
  138. &filt->outputs[filt_dstpad_idx]->out_formats);
  139. return 0;
  140. }
  141. int avfilter_config_links(AVFilterContext *filter)
  142. {
  143. int (*config_link)(AVFilterLink *);
  144. unsigned i;
  145. int ret;
  146. for (i = 0; i < filter->input_count; i ++) {
  147. AVFilterLink *link = filter->inputs[i];
  148. if (!link) continue;
  149. switch (link->init_state) {
  150. case AVLINK_INIT:
  151. continue;
  152. case AVLINK_STARTINIT:
  153. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  154. return 0;
  155. case AVLINK_UNINIT:
  156. link->init_state = AVLINK_STARTINIT;
  157. if ((ret = avfilter_config_links(link->src)) < 0)
  158. return ret;
  159. if (!(config_link = link->srcpad->config_props))
  160. config_link = avfilter_default_config_output_link;
  161. if ((ret = config_link(link)) < 0)
  162. return ret;
  163. if (link->time_base.num == 0 && link->time_base.den == 0)
  164. link->time_base = link->src && link->src->input_count ?
  165. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  166. if (link->sample_aspect_ratio.num == 0 && link->sample_aspect_ratio.den == 0)
  167. link->sample_aspect_ratio = link->src->input_count ?
  168. link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
  169. if (link->sample_rate == 0 && link->src && link->src->input_count)
  170. link->sample_rate = link->src->inputs[0]->sample_rate;
  171. if (link->channel_layout == 0 && link->src && link->src->input_count)
  172. link->channel_layout = link->src->inputs[0]->channel_layout;
  173. if ((config_link = link->dstpad->config_props))
  174. if ((ret = config_link(link)) < 0)
  175. return ret;
  176. link->init_state = AVLINK_INIT;
  177. }
  178. }
  179. return 0;
  180. }
  181. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  182. {
  183. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  184. perms & AV_PERM_READ ? "r" : "",
  185. perms & AV_PERM_WRITE ? "w" : "",
  186. perms & AV_PERM_PRESERVE ? "p" : "",
  187. perms & AV_PERM_REUSE ? "u" : "",
  188. perms & AV_PERM_REUSE2 ? "U" : "",
  189. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  190. return buf;
  191. }
  192. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  193. {
  194. av_unused char buf[16];
  195. av_dlog(ctx,
  196. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  197. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  198. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  199. ref->pts, ref->pos);
  200. if (ref->video) {
  201. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c",
  202. ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
  203. ref->video->w, ref->video->h,
  204. !ref->video->interlaced ? 'P' : /* Progressive */
  205. ref->video->top_field_first ? 'T' : 'B'); /* Top / Bottom */
  206. }
  207. if (ref->audio) {
  208. av_dlog(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
  209. ref->audio->channel_layout,
  210. ref->audio->nb_samples,
  211. ref->audio->size,
  212. ref->audio->sample_rate,
  213. ref->audio->planar);
  214. }
  215. av_dlog(ctx, "]%s", end ? "\n" : "");
  216. }
  217. static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  218. {
  219. if (link->type == AVMEDIA_TYPE_VIDEO) {
  220. av_dlog(ctx,
  221. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  222. link, link->w, link->h,
  223. av_pix_fmt_descriptors[link->format].name,
  224. link->src ? link->src->filter->name : "",
  225. link->dst ? link->dst->filter->name : "",
  226. end ? "\n" : "");
  227. } else {
  228. char buf[128];
  229. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  230. av_dlog(ctx,
  231. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  232. link, link->sample_rate, buf,
  233. av_get_sample_fmt_name(link->format),
  234. link->src ? link->src->filter->name : "",
  235. link->dst ? link->dst->filter->name : "",
  236. end ? "\n" : "");
  237. }
  238. }
  239. #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
  240. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  241. {
  242. AVFilterBufferRef *ret = NULL;
  243. av_unused char buf[16];
  244. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  245. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  246. if (link->dstpad->get_video_buffer)
  247. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  248. if (!ret)
  249. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  250. if (ret)
  251. ret->type = AVMEDIA_TYPE_VIDEO;
  252. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  253. return ret;
  254. }
  255. AVFilterBufferRef *
  256. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  257. int w, int h, enum PixelFormat format)
  258. {
  259. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  260. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  261. if (!pic || !picref)
  262. goto fail;
  263. picref->buf = pic;
  264. picref->buf->free = ff_avfilter_default_free_buffer;
  265. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  266. goto fail;
  267. pic->w = picref->video->w = w;
  268. pic->h = picref->video->h = h;
  269. /* make sure the buffer gets read permission or it's useless for output */
  270. picref->perms = perms | AV_PERM_READ;
  271. pic->refcount = 1;
  272. picref->type = AVMEDIA_TYPE_VIDEO;
  273. pic->format = picref->format = format;
  274. memcpy(pic->data, data, sizeof(pic->data));
  275. memcpy(pic->linesize, linesize, sizeof(pic->linesize));
  276. memcpy(picref->data, pic->data, sizeof(picref->data));
  277. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  278. return picref;
  279. fail:
  280. if (picref && picref->video)
  281. av_free(picref->video);
  282. av_free(picref);
  283. av_free(pic);
  284. return NULL;
  285. }
  286. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
  287. enum AVSampleFormat sample_fmt, int size,
  288. int64_t channel_layout, int planar)
  289. {
  290. AVFilterBufferRef *ret = NULL;
  291. if (link->dstpad->get_audio_buffer)
  292. ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  293. if (!ret)
  294. ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  295. if (ret)
  296. ret->type = AVMEDIA_TYPE_AUDIO;
  297. return ret;
  298. }
  299. int avfilter_request_frame(AVFilterLink *link)
  300. {
  301. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  302. if (link->srcpad->request_frame)
  303. return link->srcpad->request_frame(link);
  304. else if (link->src->inputs[0])
  305. return avfilter_request_frame(link->src->inputs[0]);
  306. else return -1;
  307. }
  308. int avfilter_poll_frame(AVFilterLink *link)
  309. {
  310. int i, min = INT_MAX;
  311. if (link->srcpad->poll_frame)
  312. return link->srcpad->poll_frame(link);
  313. for (i = 0; i < link->src->input_count; i++) {
  314. int val;
  315. if (!link->src->inputs[i])
  316. return -1;
  317. val = avfilter_poll_frame(link->src->inputs[i]);
  318. min = FFMIN(min, val);
  319. }
  320. return min;
  321. }
  322. /* XXX: should we do the duplicating of the picture ref here, instead of
  323. * forcing the source filter to do it? */
  324. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  325. {
  326. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  327. AVFilterPad *dst = link->dstpad;
  328. int perms = picref->perms;
  329. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  330. if (!(start_frame = dst->start_frame))
  331. start_frame = avfilter_default_start_frame;
  332. if (picref->linesize[0] < 0)
  333. perms |= AV_PERM_NEG_LINESIZES;
  334. /* prepare to copy the picture if it has insufficient permissions */
  335. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  336. av_log(link->dst, AV_LOG_DEBUG,
  337. "frame copy needed (have perms %x, need %x, reject %x)\n",
  338. picref->perms,
  339. link->dstpad->min_perms, link->dstpad->rej_perms);
  340. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  341. link->src_buf = picref;
  342. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  343. }
  344. else
  345. link->cur_buf = picref;
  346. start_frame(link, link->cur_buf);
  347. }
  348. void avfilter_end_frame(AVFilterLink *link)
  349. {
  350. void (*end_frame)(AVFilterLink *);
  351. if (!(end_frame = link->dstpad->end_frame))
  352. end_frame = avfilter_default_end_frame;
  353. end_frame(link);
  354. /* unreference the source picture if we're feeding the destination filter
  355. * a copied version dues to permission issues */
  356. if (link->src_buf) {
  357. avfilter_unref_buffer(link->src_buf);
  358. link->src_buf = NULL;
  359. }
  360. }
  361. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  362. {
  363. uint8_t *src[4], *dst[4];
  364. int i, j, vsub;
  365. void (*draw_slice)(AVFilterLink *, int, int, int);
  366. FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
  367. /* copy the slice if needed for permission reasons */
  368. if (link->src_buf) {
  369. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  370. for (i = 0; i < 4; i++) {
  371. if (link->src_buf->data[i]) {
  372. src[i] = link->src_buf-> data[i] +
  373. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  374. dst[i] = link->cur_buf->data[i] +
  375. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  376. } else
  377. src[i] = dst[i] = NULL;
  378. }
  379. for (i = 0; i < 4; i++) {
  380. int planew =
  381. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  382. if (!src[i]) continue;
  383. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  384. memcpy(dst[i], src[i], planew);
  385. src[i] += link->src_buf->linesize[i];
  386. dst[i] += link->cur_buf->linesize[i];
  387. }
  388. }
  389. }
  390. if (!(draw_slice = link->dstpad->draw_slice))
  391. draw_slice = avfilter_default_draw_slice;
  392. draw_slice(link, y, h, slice_dir);
  393. }
  394. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  395. {
  396. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  397. AVFilterPad *dst = link->dstpad;
  398. FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
  399. if (!(filter_samples = dst->filter_samples))
  400. filter_samples = avfilter_default_filter_samples;
  401. /* prepare to copy the samples if the buffer has insufficient permissions */
  402. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  403. dst->rej_perms & samplesref->perms) {
  404. av_log(link->dst, AV_LOG_DEBUG,
  405. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  406. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  407. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  408. samplesref->format,
  409. samplesref->audio->size,
  410. samplesref->audio->channel_layout,
  411. samplesref->audio->planar);
  412. link->cur_buf->pts = samplesref->pts;
  413. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  414. /* Copy actual data into new samples buffer */
  415. memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
  416. avfilter_unref_buffer(samplesref);
  417. } else
  418. link->cur_buf = samplesref;
  419. filter_samples(link, link->cur_buf);
  420. }
  421. #define MAX_REGISTERED_AVFILTERS_NB 64
  422. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  423. static int next_registered_avfilter_idx = 0;
  424. AVFilter *avfilter_get_by_name(const char *name)
  425. {
  426. int i;
  427. for (i = 0; registered_avfilters[i]; i++)
  428. if (!strcmp(registered_avfilters[i]->name, name))
  429. return registered_avfilters[i];
  430. return NULL;
  431. }
  432. int avfilter_register(AVFilter *filter)
  433. {
  434. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  435. return -1;
  436. registered_avfilters[next_registered_avfilter_idx++] = filter;
  437. return 0;
  438. }
  439. AVFilter **av_filter_next(AVFilter **filter)
  440. {
  441. return filter ? ++filter : &registered_avfilters[0];
  442. }
  443. void avfilter_uninit(void)
  444. {
  445. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  446. next_registered_avfilter_idx = 0;
  447. }
  448. static int pad_count(const AVFilterPad *pads)
  449. {
  450. int count;
  451. for(count = 0; pads->name; count ++) pads ++;
  452. return count;
  453. }
  454. static const char *filter_name(void *p)
  455. {
  456. AVFilterContext *filter = p;
  457. return filter->filter->name;
  458. }
  459. static const AVClass avfilter_class = {
  460. "AVFilter",
  461. filter_name,
  462. NULL,
  463. LIBAVUTIL_VERSION_INT,
  464. };
  465. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  466. {
  467. AVFilterContext *ret;
  468. *filter_ctx = NULL;
  469. if (!filter)
  470. return AVERROR(EINVAL);
  471. ret = av_mallocz(sizeof(AVFilterContext));
  472. ret->av_class = &avfilter_class;
  473. ret->filter = filter;
  474. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  475. ret->priv = av_mallocz(filter->priv_size);
  476. ret->input_count = pad_count(filter->inputs);
  477. if (ret->input_count) {
  478. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  479. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  480. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  481. }
  482. ret->output_count = pad_count(filter->outputs);
  483. if (ret->output_count) {
  484. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  485. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  486. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  487. }
  488. *filter_ctx = ret;
  489. return 0;
  490. }
  491. void avfilter_free(AVFilterContext *filter)
  492. {
  493. int i;
  494. AVFilterLink *link;
  495. if (filter->filter->uninit)
  496. filter->filter->uninit(filter);
  497. for (i = 0; i < filter->input_count; i++) {
  498. if ((link = filter->inputs[i])) {
  499. if (link->src)
  500. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  501. avfilter_formats_unref(&link->in_formats);
  502. avfilter_formats_unref(&link->out_formats);
  503. }
  504. av_freep(&link);
  505. }
  506. for (i = 0; i < filter->output_count; i++) {
  507. if ((link = filter->outputs[i])) {
  508. if (link->dst)
  509. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  510. avfilter_formats_unref(&link->in_formats);
  511. avfilter_formats_unref(&link->out_formats);
  512. }
  513. av_freep(&link);
  514. }
  515. av_freep(&filter->name);
  516. av_freep(&filter->input_pads);
  517. av_freep(&filter->output_pads);
  518. av_freep(&filter->inputs);
  519. av_freep(&filter->outputs);
  520. av_freep(&filter->priv);
  521. av_free(filter);
  522. }
  523. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  524. {
  525. int ret=0;
  526. if (filter->filter->init)
  527. ret = filter->filter->init(filter, args, opaque);
  528. return ret;
  529. }