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.

742 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 n:%d r:%d p:%d",
  263. ref->audio->channel_layout,
  264. ref->audio->nb_samples,
  265. ref->audio->sample_rate,
  266. ref->audio->planar);
  267. }
  268. av_dlog(ctx, "]%s", end ? "\n" : "");
  269. }
  270. static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  271. {
  272. if (link->type == AVMEDIA_TYPE_VIDEO) {
  273. av_dlog(ctx,
  274. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  275. link, link->w, link->h,
  276. av_pix_fmt_descriptors[link->format].name,
  277. link->src ? link->src->filter->name : "",
  278. link->dst ? link->dst->filter->name : "",
  279. end ? "\n" : "");
  280. } else {
  281. char buf[128];
  282. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  283. av_dlog(ctx,
  284. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  285. link, link->sample_rate, buf,
  286. av_get_sample_fmt_name(link->format),
  287. link->src ? link->src->filter->name : "",
  288. link->dst ? link->dst->filter->name : "",
  289. end ? "\n" : "");
  290. }
  291. }
  292. #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
  293. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  294. {
  295. AVFilterBufferRef *ret = NULL;
  296. av_unused char buf[16];
  297. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  298. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  299. if (link->dstpad->get_video_buffer)
  300. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  301. if (!ret)
  302. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  303. if (ret)
  304. ret->type = AVMEDIA_TYPE_VIDEO;
  305. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  306. return ret;
  307. }
  308. AVFilterBufferRef *
  309. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  310. int w, int h, enum PixelFormat format)
  311. {
  312. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  313. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  314. if (!pic || !picref)
  315. goto fail;
  316. picref->buf = pic;
  317. picref->buf->free = ff_avfilter_default_free_buffer;
  318. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  319. goto fail;
  320. pic->w = picref->video->w = w;
  321. pic->h = picref->video->h = h;
  322. /* make sure the buffer gets read permission or it's useless for output */
  323. picref->perms = perms | AV_PERM_READ;
  324. pic->refcount = 1;
  325. picref->type = AVMEDIA_TYPE_VIDEO;
  326. pic->format = picref->format = format;
  327. memcpy(pic->data, data, sizeof(pic->data));
  328. memcpy(pic->linesize, linesize, sizeof(pic->linesize));
  329. memcpy(picref->data, pic->data, sizeof(picref->data));
  330. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  331. return picref;
  332. fail:
  333. if (picref && picref->video)
  334. av_free(picref->video);
  335. av_free(picref);
  336. av_free(pic);
  337. return NULL;
  338. }
  339. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
  340. enum AVSampleFormat sample_fmt, int nb_samples,
  341. int64_t channel_layout, int planar)
  342. {
  343. AVFilterBufferRef *ret = NULL;
  344. if (link->dstpad->get_audio_buffer)
  345. ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
  346. if (!ret)
  347. ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
  348. if (ret)
  349. ret->type = AVMEDIA_TYPE_AUDIO;
  350. return ret;
  351. }
  352. int avfilter_request_frame(AVFilterLink *link)
  353. {
  354. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  355. if (link->srcpad->request_frame)
  356. return link->srcpad->request_frame(link);
  357. else if (link->src->inputs[0])
  358. return avfilter_request_frame(link->src->inputs[0]);
  359. else return -1;
  360. }
  361. int avfilter_poll_frame(AVFilterLink *link)
  362. {
  363. int i, min = INT_MAX;
  364. if (link->srcpad->poll_frame)
  365. return link->srcpad->poll_frame(link);
  366. for (i = 0; i < link->src->input_count; i++) {
  367. int val;
  368. if (!link->src->inputs[i])
  369. return -1;
  370. val = avfilter_poll_frame(link->src->inputs[i]);
  371. min = FFMIN(min, val);
  372. }
  373. return min;
  374. }
  375. /* XXX: should we do the duplicating of the picture ref here, instead of
  376. * forcing the source filter to do it? */
  377. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  378. {
  379. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  380. AVFilterPad *dst = link->dstpad;
  381. int perms = picref->perms;
  382. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  383. if (!(start_frame = dst->start_frame))
  384. start_frame = avfilter_default_start_frame;
  385. if (picref->linesize[0] < 0)
  386. perms |= AV_PERM_NEG_LINESIZES;
  387. /* prepare to copy the picture if it has insufficient permissions */
  388. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  389. av_log(link->dst, AV_LOG_DEBUG,
  390. "frame copy needed (have perms %x, need %x, reject %x)\n",
  391. picref->perms,
  392. link->dstpad->min_perms, link->dstpad->rej_perms);
  393. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  394. link->src_buf = picref;
  395. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  396. }
  397. else
  398. link->cur_buf = picref;
  399. start_frame(link, link->cur_buf);
  400. }
  401. void avfilter_end_frame(AVFilterLink *link)
  402. {
  403. void (*end_frame)(AVFilterLink *);
  404. if (!(end_frame = link->dstpad->end_frame))
  405. end_frame = avfilter_default_end_frame;
  406. end_frame(link);
  407. /* unreference the source picture if we're feeding the destination filter
  408. * a copied version dues to permission issues */
  409. if (link->src_buf) {
  410. avfilter_unref_buffer(link->src_buf);
  411. link->src_buf = NULL;
  412. }
  413. }
  414. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  415. {
  416. uint8_t *src[4], *dst[4];
  417. int i, j, vsub;
  418. void (*draw_slice)(AVFilterLink *, int, int, int);
  419. 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);
  420. /* copy the slice if needed for permission reasons */
  421. if (link->src_buf) {
  422. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  423. for (i = 0; i < 4; i++) {
  424. if (link->src_buf->data[i]) {
  425. src[i] = link->src_buf-> data[i] +
  426. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  427. dst[i] = link->cur_buf->data[i] +
  428. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  429. } else
  430. src[i] = dst[i] = NULL;
  431. }
  432. for (i = 0; i < 4; i++) {
  433. int planew =
  434. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  435. if (!src[i]) continue;
  436. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  437. memcpy(dst[i], src[i], planew);
  438. src[i] += link->src_buf->linesize[i];
  439. dst[i] += link->cur_buf->linesize[i];
  440. }
  441. }
  442. }
  443. if (!(draw_slice = link->dstpad->draw_slice))
  444. draw_slice = avfilter_default_draw_slice;
  445. draw_slice(link, y, h, slice_dir);
  446. }
  447. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  448. {
  449. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  450. AVFilterPad *dst = link->dstpad;
  451. int i;
  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->nb_samples,
  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. for (i = 0; samplesref->data[i]; i++)
  470. memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
  471. avfilter_unref_buffer(samplesref);
  472. } else
  473. link->cur_buf = samplesref;
  474. filter_samples(link, link->cur_buf);
  475. }
  476. #define MAX_REGISTERED_AVFILTERS_NB 64
  477. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  478. static int next_registered_avfilter_idx = 0;
  479. AVFilter *avfilter_get_by_name(const char *name)
  480. {
  481. int i;
  482. for (i = 0; registered_avfilters[i]; i++)
  483. if (!strcmp(registered_avfilters[i]->name, name))
  484. return registered_avfilters[i];
  485. return NULL;
  486. }
  487. int avfilter_register(AVFilter *filter)
  488. {
  489. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  490. return -1;
  491. registered_avfilters[next_registered_avfilter_idx++] = filter;
  492. return 0;
  493. }
  494. AVFilter **av_filter_next(AVFilter **filter)
  495. {
  496. return filter ? ++filter : &registered_avfilters[0];
  497. }
  498. void avfilter_uninit(void)
  499. {
  500. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  501. next_registered_avfilter_idx = 0;
  502. }
  503. static int pad_count(const AVFilterPad *pads)
  504. {
  505. int count;
  506. for(count = 0; pads->name; count ++) pads ++;
  507. return count;
  508. }
  509. static const char *filter_name(void *p)
  510. {
  511. AVFilterContext *filter = p;
  512. return filter->filter->name;
  513. }
  514. static const AVClass avfilter_class = {
  515. "AVFilter",
  516. filter_name,
  517. NULL,
  518. LIBAVUTIL_VERSION_INT,
  519. };
  520. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  521. {
  522. AVFilterContext *ret;
  523. *filter_ctx = NULL;
  524. if (!filter)
  525. return AVERROR(EINVAL);
  526. ret = av_mallocz(sizeof(AVFilterContext));
  527. if (!ret)
  528. return AVERROR(ENOMEM);
  529. ret->av_class = &avfilter_class;
  530. ret->filter = filter;
  531. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  532. if (filter->priv_size) {
  533. ret->priv = av_mallocz(filter->priv_size);
  534. if (!ret->priv)
  535. goto err;
  536. }
  537. ret->input_count = pad_count(filter->inputs);
  538. if (ret->input_count) {
  539. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  540. if (!ret->input_pads)
  541. goto err;
  542. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  543. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  544. if (!ret->inputs)
  545. goto err;
  546. }
  547. ret->output_count = pad_count(filter->outputs);
  548. if (ret->output_count) {
  549. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  550. if (!ret->output_pads)
  551. goto err;
  552. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  553. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  554. if (!ret->outputs)
  555. goto err;
  556. }
  557. *filter_ctx = ret;
  558. return 0;
  559. err:
  560. av_freep(&ret->inputs);
  561. av_freep(&ret->input_pads);
  562. ret->input_count = 0;
  563. av_freep(&ret->outputs);
  564. av_freep(&ret->output_pads);
  565. ret->output_count = 0;
  566. av_freep(&ret->priv);
  567. av_free(ret);
  568. return AVERROR(ENOMEM);
  569. }
  570. void avfilter_free(AVFilterContext *filter)
  571. {
  572. int i;
  573. AVFilterLink *link;
  574. if (filter->filter->uninit)
  575. filter->filter->uninit(filter);
  576. for (i = 0; i < filter->input_count; i++) {
  577. if ((link = filter->inputs[i])) {
  578. if (link->src)
  579. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  580. avfilter_formats_unref(&link->in_formats);
  581. avfilter_formats_unref(&link->out_formats);
  582. }
  583. avfilter_link_free(&link);
  584. }
  585. for (i = 0; i < filter->output_count; i++) {
  586. if ((link = filter->outputs[i])) {
  587. if (link->dst)
  588. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  589. avfilter_formats_unref(&link->in_formats);
  590. avfilter_formats_unref(&link->out_formats);
  591. }
  592. avfilter_link_free(&link);
  593. }
  594. av_freep(&filter->name);
  595. av_freep(&filter->input_pads);
  596. av_freep(&filter->output_pads);
  597. av_freep(&filter->inputs);
  598. av_freep(&filter->outputs);
  599. av_freep(&filter->priv);
  600. av_free(filter);
  601. }
  602. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  603. {
  604. int ret=0;
  605. if (filter->filter->init)
  606. ret = filter->filter->init(filter, args, opaque);
  607. return ret;
  608. }