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.

784 lines
25KB

  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. AVFilterBufferRef *
  353. avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
  354. int nb_samples, enum AVSampleFormat sample_fmt,
  355. int64_t channel_layout, int planar)
  356. {
  357. AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
  358. AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
  359. if (!samples || !samplesref)
  360. goto fail;
  361. samplesref->buf = samples;
  362. samplesref->buf->free = ff_avfilter_default_free_buffer;
  363. if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
  364. goto fail;
  365. samplesref->audio->nb_samples = nb_samples;
  366. samplesref->audio->channel_layout = channel_layout;
  367. samplesref->audio->planar = planar;
  368. /* make sure the buffer gets read permission or it's useless for output */
  369. samplesref->perms = perms | AV_PERM_READ;
  370. samples->refcount = 1;
  371. samplesref->type = AVMEDIA_TYPE_AUDIO;
  372. samplesref->format = sample_fmt;
  373. memcpy(samples->data, data, sizeof(samples->data));
  374. memcpy(samples->linesize, linesize, sizeof(samples->linesize));
  375. memcpy(samplesref->data, data, sizeof(samplesref->data));
  376. memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
  377. return samplesref;
  378. fail:
  379. if (samplesref && samplesref->audio)
  380. av_freep(&samplesref->audio);
  381. av_freep(&samplesref);
  382. av_freep(&samples);
  383. return NULL;
  384. }
  385. int avfilter_request_frame(AVFilterLink *link)
  386. {
  387. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  388. if (link->srcpad->request_frame)
  389. return link->srcpad->request_frame(link);
  390. else if (link->src->inputs[0])
  391. return avfilter_request_frame(link->src->inputs[0]);
  392. else return -1;
  393. }
  394. int avfilter_poll_frame(AVFilterLink *link)
  395. {
  396. int i, min = INT_MAX;
  397. if (link->srcpad->poll_frame)
  398. return link->srcpad->poll_frame(link);
  399. for (i = 0; i < link->src->input_count; i++) {
  400. int val;
  401. if (!link->src->inputs[i])
  402. return -1;
  403. val = avfilter_poll_frame(link->src->inputs[i]);
  404. min = FFMIN(min, val);
  405. }
  406. return min;
  407. }
  408. /* XXX: should we do the duplicating of the picture ref here, instead of
  409. * forcing the source filter to do it? */
  410. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  411. {
  412. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  413. AVFilterPad *dst = link->dstpad;
  414. int perms = picref->perms;
  415. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  416. if (!(start_frame = dst->start_frame))
  417. start_frame = avfilter_default_start_frame;
  418. if (picref->linesize[0] < 0)
  419. perms |= AV_PERM_NEG_LINESIZES;
  420. /* prepare to copy the picture if it has insufficient permissions */
  421. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  422. av_log(link->dst, AV_LOG_DEBUG,
  423. "frame copy needed (have perms %x, need %x, reject %x)\n",
  424. picref->perms,
  425. link->dstpad->min_perms, link->dstpad->rej_perms);
  426. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  427. link->src_buf = picref;
  428. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  429. }
  430. else
  431. link->cur_buf = picref;
  432. start_frame(link, link->cur_buf);
  433. }
  434. void avfilter_end_frame(AVFilterLink *link)
  435. {
  436. void (*end_frame)(AVFilterLink *);
  437. if (!(end_frame = link->dstpad->end_frame))
  438. end_frame = avfilter_default_end_frame;
  439. end_frame(link);
  440. /* unreference the source picture if we're feeding the destination filter
  441. * a copied version dues to permission issues */
  442. if (link->src_buf) {
  443. avfilter_unref_buffer(link->src_buf);
  444. link->src_buf = NULL;
  445. }
  446. }
  447. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  448. {
  449. uint8_t *src[4], *dst[4];
  450. int i, j, vsub;
  451. void (*draw_slice)(AVFilterLink *, int, int, int);
  452. 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);
  453. /* copy the slice if needed for permission reasons */
  454. if (link->src_buf) {
  455. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  456. for (i = 0; i < 4; i++) {
  457. if (link->src_buf->data[i]) {
  458. src[i] = link->src_buf-> data[i] +
  459. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  460. dst[i] = link->cur_buf->data[i] +
  461. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  462. } else
  463. src[i] = dst[i] = NULL;
  464. }
  465. for (i = 0; i < 4; i++) {
  466. int planew =
  467. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  468. if (!src[i]) continue;
  469. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  470. memcpy(dst[i], src[i], planew);
  471. src[i] += link->src_buf->linesize[i];
  472. dst[i] += link->cur_buf->linesize[i];
  473. }
  474. }
  475. }
  476. if (!(draw_slice = link->dstpad->draw_slice))
  477. draw_slice = avfilter_default_draw_slice;
  478. draw_slice(link, y, h, slice_dir);
  479. }
  480. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  481. {
  482. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  483. AVFilterPad *dst = link->dstpad;
  484. int i;
  485. FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
  486. if (!(filter_samples = dst->filter_samples))
  487. filter_samples = avfilter_default_filter_samples;
  488. /* prepare to copy the samples if the buffer has insufficient permissions */
  489. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  490. dst->rej_perms & samplesref->perms) {
  491. av_log(link->dst, AV_LOG_DEBUG,
  492. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  493. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  494. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  495. samplesref->format,
  496. samplesref->audio->nb_samples,
  497. samplesref->audio->channel_layout,
  498. samplesref->audio->planar);
  499. link->cur_buf->pts = samplesref->pts;
  500. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  501. /* Copy actual data into new samples buffer */
  502. for (i = 0; samplesref->data[i]; i++)
  503. memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
  504. avfilter_unref_buffer(samplesref);
  505. } else
  506. link->cur_buf = samplesref;
  507. filter_samples(link, link->cur_buf);
  508. }
  509. #define MAX_REGISTERED_AVFILTERS_NB 64
  510. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  511. static int next_registered_avfilter_idx = 0;
  512. AVFilter *avfilter_get_by_name(const char *name)
  513. {
  514. int i;
  515. for (i = 0; registered_avfilters[i]; i++)
  516. if (!strcmp(registered_avfilters[i]->name, name))
  517. return registered_avfilters[i];
  518. return NULL;
  519. }
  520. int avfilter_register(AVFilter *filter)
  521. {
  522. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  523. return -1;
  524. registered_avfilters[next_registered_avfilter_idx++] = filter;
  525. return 0;
  526. }
  527. AVFilter **av_filter_next(AVFilter **filter)
  528. {
  529. return filter ? ++filter : &registered_avfilters[0];
  530. }
  531. void avfilter_uninit(void)
  532. {
  533. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  534. next_registered_avfilter_idx = 0;
  535. }
  536. static int pad_count(const AVFilterPad *pads)
  537. {
  538. int count;
  539. for(count = 0; pads->name; count ++) pads ++;
  540. return count;
  541. }
  542. static const char *filter_name(void *p)
  543. {
  544. AVFilterContext *filter = p;
  545. return filter->filter->name;
  546. }
  547. static const AVClass avfilter_class = {
  548. "AVFilter",
  549. filter_name,
  550. NULL,
  551. LIBAVUTIL_VERSION_INT,
  552. };
  553. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  554. {
  555. AVFilterContext *ret;
  556. *filter_ctx = NULL;
  557. if (!filter)
  558. return AVERROR(EINVAL);
  559. ret = av_mallocz(sizeof(AVFilterContext));
  560. if (!ret)
  561. return AVERROR(ENOMEM);
  562. ret->av_class = &avfilter_class;
  563. ret->filter = filter;
  564. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  565. if (filter->priv_size) {
  566. ret->priv = av_mallocz(filter->priv_size);
  567. if (!ret->priv)
  568. goto err;
  569. }
  570. ret->input_count = pad_count(filter->inputs);
  571. if (ret->input_count) {
  572. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  573. if (!ret->input_pads)
  574. goto err;
  575. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  576. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  577. if (!ret->inputs)
  578. goto err;
  579. }
  580. ret->output_count = pad_count(filter->outputs);
  581. if (ret->output_count) {
  582. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  583. if (!ret->output_pads)
  584. goto err;
  585. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  586. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  587. if (!ret->outputs)
  588. goto err;
  589. }
  590. *filter_ctx = ret;
  591. return 0;
  592. err:
  593. av_freep(&ret->inputs);
  594. av_freep(&ret->input_pads);
  595. ret->input_count = 0;
  596. av_freep(&ret->outputs);
  597. av_freep(&ret->output_pads);
  598. ret->output_count = 0;
  599. av_freep(&ret->priv);
  600. av_free(ret);
  601. return AVERROR(ENOMEM);
  602. }
  603. void avfilter_free(AVFilterContext *filter)
  604. {
  605. int i;
  606. AVFilterLink *link;
  607. if (filter->filter->uninit)
  608. filter->filter->uninit(filter);
  609. for (i = 0; i < filter->input_count; i++) {
  610. if ((link = filter->inputs[i])) {
  611. if (link->src)
  612. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  613. avfilter_formats_unref(&link->in_formats);
  614. avfilter_formats_unref(&link->out_formats);
  615. }
  616. avfilter_link_free(&link);
  617. }
  618. for (i = 0; i < filter->output_count; i++) {
  619. if ((link = filter->outputs[i])) {
  620. if (link->dst)
  621. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  622. avfilter_formats_unref(&link->in_formats);
  623. avfilter_formats_unref(&link->out_formats);
  624. }
  625. avfilter_link_free(&link);
  626. }
  627. av_freep(&filter->name);
  628. av_freep(&filter->input_pads);
  629. av_freep(&filter->output_pads);
  630. av_freep(&filter->inputs);
  631. av_freep(&filter->outputs);
  632. av_freep(&filter->priv);
  633. av_free(filter);
  634. }
  635. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  636. {
  637. int ret=0;
  638. if (filter->filter->init)
  639. ret = filter->filter->init(filter, args, opaque);
  640. return ret;
  641. }