avfilter.c 22 KB

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