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.

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