avfilter.c 22 KB

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