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.

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