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.

741 lines
23KB

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