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.

818 lines
26KB

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