avfilter.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 LIBAV_CONFIGURATION;
  34. }
  35. const char *avfilter_license(void)
  36. {
  37. #define LICENSE_PREFIX "libavfilter license: "
  38. return LICENSE_PREFIX LIBAV_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. #ifdef DEBUG
  182. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  183. {
  184. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  185. perms & AV_PERM_READ ? "r" : "",
  186. perms & AV_PERM_WRITE ? "w" : "",
  187. perms & AV_PERM_PRESERVE ? "p" : "",
  188. perms & AV_PERM_REUSE ? "u" : "",
  189. perms & AV_PERM_REUSE2 ? "U" : "",
  190. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  191. return buf;
  192. }
  193. #endif
  194. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  195. {
  196. av_unused char buf[16];
  197. av_dlog(ctx,
  198. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  199. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  200. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  201. ref->pts, ref->pos);
  202. if (ref->video) {
  203. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  204. ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
  205. ref->video->w, ref->video->h,
  206. !ref->video->interlaced ? 'P' : /* Progressive */
  207. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  208. ref->video->key_frame,
  209. av_get_picture_type_char(ref->video->pict_type));
  210. }
  211. if (ref->audio) {
  212. av_dlog(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
  213. ref->audio->channel_layout,
  214. ref->audio->nb_samples,
  215. ref->audio->size,
  216. ref->audio->sample_rate,
  217. ref->audio->planar);
  218. }
  219. av_dlog(ctx, "]%s", end ? "\n" : "");
  220. }
  221. static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  222. {
  223. if (link->type == AVMEDIA_TYPE_VIDEO) {
  224. av_dlog(ctx,
  225. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  226. link, link->w, link->h,
  227. av_pix_fmt_descriptors[link->format].name,
  228. link->src ? link->src->filter->name : "",
  229. link->dst ? link->dst->filter->name : "",
  230. end ? "\n" : "");
  231. } else {
  232. char buf[128];
  233. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  234. av_dlog(ctx,
  235. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  236. link, link->sample_rate, buf,
  237. av_get_sample_fmt_name(link->format),
  238. link->src ? link->src->filter->name : "",
  239. link->dst ? link->dst->filter->name : "",
  240. end ? "\n" : "");
  241. }
  242. }
  243. #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
  244. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  245. {
  246. AVFilterBufferRef *ret = NULL;
  247. av_unused char buf[16];
  248. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  249. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  250. if (link->dstpad->get_video_buffer)
  251. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  252. if (!ret)
  253. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  254. if (ret)
  255. ret->type = AVMEDIA_TYPE_VIDEO;
  256. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  257. return ret;
  258. }
  259. AVFilterBufferRef *
  260. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  261. int w, int h, enum PixelFormat format)
  262. {
  263. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  264. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  265. if (!pic || !picref)
  266. goto fail;
  267. picref->buf = pic;
  268. picref->buf->free = ff_avfilter_default_free_buffer;
  269. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  270. goto fail;
  271. pic->w = picref->video->w = w;
  272. pic->h = picref->video->h = h;
  273. /* make sure the buffer gets read permission or it's useless for output */
  274. picref->perms = perms | AV_PERM_READ;
  275. pic->refcount = 1;
  276. picref->type = AVMEDIA_TYPE_VIDEO;
  277. pic->format = picref->format = format;
  278. memcpy(pic->data, data, sizeof(pic->data));
  279. memcpy(pic->linesize, linesize, sizeof(pic->linesize));
  280. memcpy(picref->data, pic->data, sizeof(picref->data));
  281. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  282. return picref;
  283. fail:
  284. if (picref && picref->video)
  285. av_free(picref->video);
  286. av_free(picref);
  287. av_free(pic);
  288. return NULL;
  289. }
  290. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
  291. enum AVSampleFormat sample_fmt, int size,
  292. int64_t channel_layout, int planar)
  293. {
  294. AVFilterBufferRef *ret = NULL;
  295. if (link->dstpad->get_audio_buffer)
  296. ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  297. if (!ret)
  298. ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  299. if (ret)
  300. ret->type = AVMEDIA_TYPE_AUDIO;
  301. return ret;
  302. }
  303. int avfilter_request_frame(AVFilterLink *link)
  304. {
  305. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  306. if (link->srcpad->request_frame)
  307. return link->srcpad->request_frame(link);
  308. else if (link->src->inputs[0])
  309. return avfilter_request_frame(link->src->inputs[0]);
  310. else return -1;
  311. }
  312. int avfilter_poll_frame(AVFilterLink *link)
  313. {
  314. int i, min = INT_MAX;
  315. if (link->srcpad->poll_frame)
  316. return link->srcpad->poll_frame(link);
  317. for (i = 0; i < link->src->input_count; i++) {
  318. int val;
  319. if (!link->src->inputs[i])
  320. return -1;
  321. val = avfilter_poll_frame(link->src->inputs[i]);
  322. min = FFMIN(min, val);
  323. }
  324. return min;
  325. }
  326. /* XXX: should we do the duplicating of the picture ref here, instead of
  327. * forcing the source filter to do it? */
  328. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  329. {
  330. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  331. AVFilterPad *dst = link->dstpad;
  332. int perms = picref->perms;
  333. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  334. if (!(start_frame = dst->start_frame))
  335. start_frame = avfilter_default_start_frame;
  336. if (picref->linesize[0] < 0)
  337. perms |= AV_PERM_NEG_LINESIZES;
  338. /* prepare to copy the picture if it has insufficient permissions */
  339. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  340. av_log(link->dst, AV_LOG_DEBUG,
  341. "frame copy needed (have perms %x, need %x, reject %x)\n",
  342. picref->perms,
  343. link->dstpad->min_perms, link->dstpad->rej_perms);
  344. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  345. link->src_buf = picref;
  346. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  347. }
  348. else
  349. link->cur_buf = picref;
  350. start_frame(link, link->cur_buf);
  351. }
  352. void avfilter_end_frame(AVFilterLink *link)
  353. {
  354. void (*end_frame)(AVFilterLink *);
  355. if (!(end_frame = link->dstpad->end_frame))
  356. end_frame = avfilter_default_end_frame;
  357. end_frame(link);
  358. /* unreference the source picture if we're feeding the destination filter
  359. * a copied version dues to permission issues */
  360. if (link->src_buf) {
  361. avfilter_unref_buffer(link->src_buf);
  362. link->src_buf = NULL;
  363. }
  364. }
  365. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  366. {
  367. uint8_t *src[4], *dst[4];
  368. int i, j, vsub;
  369. void (*draw_slice)(AVFilterLink *, int, int, int);
  370. 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);
  371. /* copy the slice if needed for permission reasons */
  372. if (link->src_buf) {
  373. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  374. for (i = 0; i < 4; i++) {
  375. if (link->src_buf->data[i]) {
  376. src[i] = link->src_buf-> data[i] +
  377. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  378. dst[i] = link->cur_buf->data[i] +
  379. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  380. } else
  381. src[i] = dst[i] = NULL;
  382. }
  383. for (i = 0; i < 4; i++) {
  384. int planew =
  385. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  386. if (!src[i]) continue;
  387. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  388. memcpy(dst[i], src[i], planew);
  389. src[i] += link->src_buf->linesize[i];
  390. dst[i] += link->cur_buf->linesize[i];
  391. }
  392. }
  393. }
  394. if (!(draw_slice = link->dstpad->draw_slice))
  395. draw_slice = avfilter_default_draw_slice;
  396. draw_slice(link, y, h, slice_dir);
  397. }
  398. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  399. {
  400. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  401. AVFilterPad *dst = link->dstpad;
  402. FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
  403. if (!(filter_samples = dst->filter_samples))
  404. filter_samples = avfilter_default_filter_samples;
  405. /* prepare to copy the samples if the buffer has insufficient permissions */
  406. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  407. dst->rej_perms & samplesref->perms) {
  408. av_log(link->dst, AV_LOG_DEBUG,
  409. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  410. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  411. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  412. samplesref->format,
  413. samplesref->audio->size,
  414. samplesref->audio->channel_layout,
  415. samplesref->audio->planar);
  416. link->cur_buf->pts = samplesref->pts;
  417. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  418. /* Copy actual data into new samples buffer */
  419. memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
  420. avfilter_unref_buffer(samplesref);
  421. } else
  422. link->cur_buf = samplesref;
  423. filter_samples(link, link->cur_buf);
  424. }
  425. #define MAX_REGISTERED_AVFILTERS_NB 64
  426. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  427. static int next_registered_avfilter_idx = 0;
  428. AVFilter *avfilter_get_by_name(const char *name)
  429. {
  430. int i;
  431. for (i = 0; registered_avfilters[i]; i++)
  432. if (!strcmp(registered_avfilters[i]->name, name))
  433. return registered_avfilters[i];
  434. return NULL;
  435. }
  436. int avfilter_register(AVFilter *filter)
  437. {
  438. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  439. return -1;
  440. registered_avfilters[next_registered_avfilter_idx++] = filter;
  441. return 0;
  442. }
  443. AVFilter **av_filter_next(AVFilter **filter)
  444. {
  445. return filter ? ++filter : &registered_avfilters[0];
  446. }
  447. void avfilter_uninit(void)
  448. {
  449. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  450. next_registered_avfilter_idx = 0;
  451. }
  452. static int pad_count(const AVFilterPad *pads)
  453. {
  454. int count;
  455. for(count = 0; pads->name; count ++) pads ++;
  456. return count;
  457. }
  458. static const char *filter_name(void *p)
  459. {
  460. AVFilterContext *filter = p;
  461. return filter->filter->name;
  462. }
  463. static const AVClass avfilter_class = {
  464. "AVFilter",
  465. filter_name,
  466. NULL,
  467. LIBAVUTIL_VERSION_INT,
  468. };
  469. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  470. {
  471. AVFilterContext *ret;
  472. *filter_ctx = NULL;
  473. if (!filter)
  474. return AVERROR(EINVAL);
  475. ret = av_mallocz(sizeof(AVFilterContext));
  476. if (!ret)
  477. return AVERROR(ENOMEM);
  478. ret->av_class = &avfilter_class;
  479. ret->filter = filter;
  480. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  481. if (filter->priv_size) {
  482. ret->priv = av_mallocz(filter->priv_size);
  483. if (!ret->priv)
  484. goto err;
  485. }
  486. ret->input_count = pad_count(filter->inputs);
  487. if (ret->input_count) {
  488. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  489. if (!ret->input_pads)
  490. goto err;
  491. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  492. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  493. if (!ret->inputs)
  494. goto err;
  495. }
  496. ret->output_count = pad_count(filter->outputs);
  497. if (ret->output_count) {
  498. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  499. if (!ret->output_pads)
  500. goto err;
  501. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  502. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  503. if (!ret->outputs)
  504. goto err;
  505. }
  506. *filter_ctx = ret;
  507. return 0;
  508. err:
  509. av_freep(&ret->inputs);
  510. av_freep(&ret->input_pads);
  511. ret->input_count = 0;
  512. av_freep(&ret->outputs);
  513. av_freep(&ret->output_pads);
  514. ret->output_count = 0;
  515. av_freep(&ret->priv);
  516. av_free(ret);
  517. return AVERROR(ENOMEM);
  518. }
  519. void avfilter_free(AVFilterContext *filter)
  520. {
  521. int i;
  522. AVFilterLink *link;
  523. if (filter->filter->uninit)
  524. filter->filter->uninit(filter);
  525. for (i = 0; i < filter->input_count; i++) {
  526. if ((link = filter->inputs[i])) {
  527. if (link->src)
  528. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  529. avfilter_formats_unref(&link->in_formats);
  530. avfilter_formats_unref(&link->out_formats);
  531. }
  532. av_freep(&link);
  533. }
  534. for (i = 0; i < filter->output_count; i++) {
  535. if ((link = filter->outputs[i])) {
  536. if (link->dst)
  537. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  538. avfilter_formats_unref(&link->in_formats);
  539. avfilter_formats_unref(&link->out_formats);
  540. }
  541. av_freep(&link);
  542. }
  543. av_freep(&filter->name);
  544. av_freep(&filter->input_pads);
  545. av_freep(&filter->output_pads);
  546. av_freep(&filter->inputs);
  547. av_freep(&filter->outputs);
  548. av_freep(&filter->priv);
  549. av_free(filter);
  550. }
  551. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  552. {
  553. int ret=0;
  554. if (filter->filter->init)
  555. ret = filter->filter->init(filter, args, opaque);
  556. return ret;
  557. }