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.

830 lines
27KB

  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 "libavutil/avstring.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. unsigned avfilter_version(void) {
  31. return LIBAVFILTER_VERSION_INT;
  32. }
  33. const char *avfilter_configuration(void)
  34. {
  35. return FFMPEG_CONFIGURATION;
  36. }
  37. const char *avfilter_license(void)
  38. {
  39. #define LICENSE_PREFIX "libavfilter license: "
  40. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  41. }
  42. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  43. {
  44. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  45. if (!ret)
  46. return NULL;
  47. *ret = *ref;
  48. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  49. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  50. if (!ret->video) {
  51. av_free(ret);
  52. return NULL;
  53. }
  54. *ret->video = *ref->video;
  55. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  56. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  57. if (!ret->audio) {
  58. av_free(ret);
  59. return NULL;
  60. }
  61. *ret->audio = *ref->audio;
  62. }
  63. ret->perms &= pmask;
  64. ret->buf->refcount ++;
  65. return ret;
  66. }
  67. static void store_in_pool(AVFilterBufferRef *ref)
  68. {
  69. int i;
  70. AVFilterPool *pool= ref->buf->priv;
  71. av_assert0(ref->buf->data[0]);
  72. if (pool->count == POOL_SIZE) {
  73. AVFilterBufferRef *ref1 = pool->pic[0];
  74. av_freep(&ref1->video);
  75. av_freep(&ref1->audio);
  76. av_freep(&ref1->buf->data[0]);
  77. av_freep(&ref1->buf);
  78. av_free(ref1);
  79. memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
  80. pool->count--;
  81. pool->pic[POOL_SIZE-1] = NULL;
  82. }
  83. for (i = 0; i < POOL_SIZE; i++) {
  84. if (!pool->pic[i]) {
  85. pool->pic[i] = ref;
  86. pool->count++;
  87. break;
  88. }
  89. }
  90. }
  91. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  92. {
  93. if (!ref)
  94. return;
  95. if (!(--ref->buf->refcount)) {
  96. if (!ref->buf->free) {
  97. store_in_pool(ref);
  98. return;
  99. }
  100. ref->buf->free(ref->buf);
  101. }
  102. av_freep(&ref->video);
  103. av_freep(&ref->audio);
  104. av_free(ref);
  105. }
  106. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  107. AVFilterPad **pads, AVFilterLink ***links,
  108. AVFilterPad *newpad)
  109. {
  110. unsigned i;
  111. idx = FFMIN(idx, *count);
  112. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  113. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  114. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  115. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  116. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  117. (*links)[idx] = NULL;
  118. (*count)++;
  119. for (i = idx+1; i < *count; i++)
  120. if (*links[i])
  121. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  122. }
  123. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  124. AVFilterContext *dst, unsigned dstpad)
  125. {
  126. AVFilterLink *link;
  127. if (src->output_count <= srcpad || dst->input_count <= dstpad ||
  128. src->outputs[srcpad] || dst->inputs[dstpad])
  129. return -1;
  130. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  131. av_log(src, AV_LOG_ERROR,
  132. "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
  133. src->name, srcpad, dst->name, dstpad);
  134. return AVERROR(EINVAL);
  135. }
  136. src->outputs[srcpad] =
  137. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  138. link->src = src;
  139. link->dst = dst;
  140. link->srcpad = &src->output_pads[srcpad];
  141. link->dstpad = &dst->input_pads[dstpad];
  142. link->type = src->output_pads[srcpad].type;
  143. assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  144. link->format = -1;
  145. return 0;
  146. }
  147. void avfilter_link_free(AVFilterLink **link)
  148. {
  149. if (!*link)
  150. return;
  151. if ((*link)->pool) {
  152. int i;
  153. for (i = 0; i < POOL_SIZE; i++) {
  154. if ((*link)->pool->pic[i]) {
  155. AVFilterBufferRef *picref = (*link)->pool->pic[i];
  156. /* free buffer: picrefs stored in the pool are not
  157. * supposed to contain a free callback */
  158. av_freep(&picref->buf->data[0]);
  159. av_freep(&picref->buf);
  160. av_freep(&picref->audio);
  161. av_freep(&picref->video);
  162. av_freep(&(*link)->pool->pic[i]);
  163. }
  164. }
  165. (*link)->pool->count = 0;
  166. // av_freep(&(*link)->pool);
  167. }
  168. av_freep(link);
  169. }
  170. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  171. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  172. {
  173. int ret;
  174. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  175. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  176. "between the filter '%s' and the filter '%s'\n",
  177. filt->name, link->src->name, link->dst->name);
  178. link->dst->inputs[dstpad_idx] = NULL;
  179. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  180. /* failed to link output filter to new filter */
  181. link->dst->inputs[dstpad_idx] = link;
  182. return ret;
  183. }
  184. /* re-hookup the link to the new destination filter we inserted */
  185. link->dst = filt;
  186. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  187. filt->inputs[filt_srcpad_idx] = link;
  188. /* if any information on supported media formats already exists on the
  189. * link, we need to preserve that */
  190. if (link->out_formats)
  191. avfilter_formats_changeref(&link->out_formats,
  192. &filt->outputs[filt_dstpad_idx]->out_formats);
  193. if (link->out_chlayouts)
  194. avfilter_formats_changeref(&link->out_chlayouts,
  195. &filt->outputs[filt_dstpad_idx]->out_chlayouts);
  196. if (link->out_packing)
  197. avfilter_formats_changeref(&link->out_packing,
  198. &filt->outputs[filt_dstpad_idx]->out_packing);
  199. return 0;
  200. }
  201. int avfilter_config_links(AVFilterContext *filter)
  202. {
  203. int (*config_link)(AVFilterLink *);
  204. unsigned i;
  205. int ret;
  206. for (i = 0; i < filter->input_count; i ++) {
  207. AVFilterLink *link = filter->inputs[i];
  208. if (!link) continue;
  209. switch (link->init_state) {
  210. case AVLINK_INIT:
  211. continue;
  212. case AVLINK_STARTINIT:
  213. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  214. return 0;
  215. case AVLINK_UNINIT:
  216. link->init_state = AVLINK_STARTINIT;
  217. if ((ret = avfilter_config_links(link->src)) < 0)
  218. return ret;
  219. if (!(config_link = link->srcpad->config_props)) {
  220. if (link->src->input_count != 1) {
  221. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  222. "with more than one input "
  223. "must set config_props() "
  224. "callbacks on all outputs\n");
  225. return AVERROR(EINVAL);
  226. }
  227. } else if ((ret = config_link(link)) < 0)
  228. return ret;
  229. switch (link->type) {
  230. case AVMEDIA_TYPE_VIDEO:
  231. if (!link->time_base.num && !link->time_base.den)
  232. link->time_base = link->src->input_count ?
  233. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  234. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  235. link->sample_aspect_ratio = link->src->input_count ?
  236. link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
  237. if (link->src->input_count) {
  238. if (!link->w)
  239. link->w = link->src->inputs[0]->w;
  240. if (!link->h)
  241. link->h = link->src->inputs[0]->h;
  242. } else if (!link->w || !link->h) {
  243. av_log(link->src, AV_LOG_ERROR,
  244. "Video source filters must set their output link's "
  245. "width and height\n");
  246. return AVERROR(EINVAL);
  247. }
  248. break;
  249. case AVMEDIA_TYPE_AUDIO:
  250. if (link->src->input_count) {
  251. if (!link->sample_rate)
  252. link->sample_rate = link->src->inputs[0]->sample_rate;
  253. } else if (!link->sample_rate) {
  254. av_log(link->src, AV_LOG_ERROR,
  255. "Audio source filters must set their output link's "
  256. "sample_rate\n");
  257. return AVERROR(EINVAL);
  258. }
  259. }
  260. if ((config_link = link->dstpad->config_props))
  261. if ((ret = config_link(link)) < 0)
  262. return ret;
  263. link->init_state = AVLINK_INIT;
  264. }
  265. }
  266. return 0;
  267. }
  268. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  269. {
  270. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  271. perms & AV_PERM_READ ? "r" : "",
  272. perms & AV_PERM_WRITE ? "w" : "",
  273. perms & AV_PERM_PRESERVE ? "p" : "",
  274. perms & AV_PERM_REUSE ? "u" : "",
  275. perms & AV_PERM_REUSE2 ? "U" : "",
  276. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  277. return buf;
  278. }
  279. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  280. {
  281. av_unused char buf[16];
  282. av_dlog(ctx,
  283. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  284. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  285. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  286. ref->pts, ref->pos);
  287. if (ref->video) {
  288. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  289. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  290. ref->video->w, ref->video->h,
  291. !ref->video->interlaced ? 'P' : /* Progressive */
  292. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  293. ref->video->key_frame,
  294. av_get_picture_type_char(ref->video->pict_type));
  295. }
  296. if (ref->audio) {
  297. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
  298. ref->audio->channel_layout,
  299. ref->audio->nb_samples,
  300. ref->audio->sample_rate,
  301. ref->audio->planar);
  302. }
  303. av_dlog(ctx, "]%s", end ? "\n" : "");
  304. }
  305. static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  306. {
  307. if (link->type == AVMEDIA_TYPE_VIDEO) {
  308. av_dlog(ctx,
  309. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  310. link, link->w, link->h,
  311. av_pix_fmt_descriptors[link->format].name,
  312. link->src ? link->src->filter->name : "",
  313. link->dst ? link->dst->filter->name : "",
  314. end ? "\n" : "");
  315. } else {
  316. char buf[128];
  317. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  318. av_dlog(ctx,
  319. "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
  320. link, link->sample_rate, buf,
  321. av_get_sample_fmt_name(link->format),
  322. link->src ? link->src->filter->name : "",
  323. link->dst ? link->dst->filter->name : "",
  324. end ? "\n" : "");
  325. }
  326. }
  327. #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
  328. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  329. {
  330. AVFilterBufferRef *ret = NULL;
  331. av_unused char buf[16];
  332. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  333. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  334. if (link->dstpad->get_video_buffer)
  335. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  336. if (!ret)
  337. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  338. if (ret)
  339. ret->type = AVMEDIA_TYPE_VIDEO;
  340. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  341. return ret;
  342. }
  343. AVFilterBufferRef *
  344. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  345. int w, int h, enum PixelFormat format)
  346. {
  347. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  348. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  349. if (!pic || !picref)
  350. goto fail;
  351. picref->buf = pic;
  352. picref->buf->free = ff_avfilter_default_free_buffer;
  353. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  354. goto fail;
  355. pic->w = picref->video->w = w;
  356. pic->h = picref->video->h = h;
  357. /* make sure the buffer gets read permission or it's useless for output */
  358. picref->perms = perms | AV_PERM_READ;
  359. pic->refcount = 1;
  360. picref->type = AVMEDIA_TYPE_VIDEO;
  361. pic->format = picref->format = format;
  362. memcpy(pic->data, data, sizeof(pic->data));
  363. memcpy(pic->linesize, linesize, sizeof(pic->linesize));
  364. memcpy(picref->data, pic->data, sizeof(picref->data));
  365. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  366. return picref;
  367. fail:
  368. if (picref && picref->video)
  369. av_free(picref->video);
  370. av_free(picref);
  371. av_free(pic);
  372. return NULL;
  373. }
  374. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
  375. enum AVSampleFormat sample_fmt, int nb_samples,
  376. int64_t channel_layout, int planar)
  377. {
  378. AVFilterBufferRef *ret = NULL;
  379. if (link->dstpad->get_audio_buffer)
  380. ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
  381. if (!ret)
  382. ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
  383. if (ret)
  384. ret->type = AVMEDIA_TYPE_AUDIO;
  385. return ret;
  386. }
  387. AVFilterBufferRef *
  388. avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
  389. int nb_samples, enum AVSampleFormat sample_fmt,
  390. int64_t channel_layout, int planar)
  391. {
  392. AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
  393. AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
  394. if (!samples || !samplesref)
  395. goto fail;
  396. samplesref->buf = samples;
  397. samplesref->buf->free = ff_avfilter_default_free_buffer;
  398. if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
  399. goto fail;
  400. samplesref->audio->nb_samples = nb_samples;
  401. samplesref->audio->channel_layout = channel_layout;
  402. samplesref->audio->planar = planar;
  403. /* make sure the buffer gets read permission or it's useless for output */
  404. samplesref->perms = perms | AV_PERM_READ;
  405. samples->refcount = 1;
  406. samplesref->type = AVMEDIA_TYPE_AUDIO;
  407. samplesref->format = sample_fmt;
  408. memcpy(samples->data, data, sizeof(samples->data));
  409. memcpy(samples->linesize, linesize, sizeof(samples->linesize));
  410. memcpy(samplesref->data, data, sizeof(samplesref->data));
  411. memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
  412. return samplesref;
  413. fail:
  414. if (samplesref && samplesref->audio)
  415. av_freep(&samplesref->audio);
  416. av_freep(&samplesref);
  417. av_freep(&samples);
  418. return NULL;
  419. }
  420. int avfilter_request_frame(AVFilterLink *link)
  421. {
  422. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  423. if (link->srcpad->request_frame)
  424. return link->srcpad->request_frame(link);
  425. else if (link->src->inputs[0])
  426. return avfilter_request_frame(link->src->inputs[0]);
  427. else return -1;
  428. }
  429. int avfilter_poll_frame(AVFilterLink *link)
  430. {
  431. int i, min = INT_MAX;
  432. if (link->srcpad->poll_frame)
  433. return link->srcpad->poll_frame(link);
  434. for (i = 0; i < link->src->input_count; i++) {
  435. int val;
  436. if (!link->src->inputs[i])
  437. return -1;
  438. val = avfilter_poll_frame(link->src->inputs[i]);
  439. min = FFMIN(min, val);
  440. }
  441. return min;
  442. }
  443. /* XXX: should we do the duplicating of the picture ref here, instead of
  444. * forcing the source filter to do it? */
  445. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  446. {
  447. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  448. AVFilterPad *dst = link->dstpad;
  449. int perms = picref->perms;
  450. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  451. if (!(start_frame = dst->start_frame))
  452. start_frame = avfilter_default_start_frame;
  453. if (picref->linesize[0] < 0)
  454. perms |= AV_PERM_NEG_LINESIZES;
  455. /* prepare to copy the picture if it has insufficient permissions */
  456. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  457. av_log(link->dst, AV_LOG_DEBUG,
  458. "frame copy needed (have perms %x, need %x, reject %x)\n",
  459. picref->perms,
  460. link->dstpad->min_perms, link->dstpad->rej_perms);
  461. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  462. link->src_buf = picref;
  463. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  464. }
  465. else
  466. link->cur_buf = picref;
  467. start_frame(link, link->cur_buf);
  468. }
  469. void avfilter_end_frame(AVFilterLink *link)
  470. {
  471. void (*end_frame)(AVFilterLink *);
  472. if (!(end_frame = link->dstpad->end_frame))
  473. end_frame = avfilter_default_end_frame;
  474. end_frame(link);
  475. /* unreference the source picture if we're feeding the destination filter
  476. * a copied version dues to permission issues */
  477. if (link->src_buf) {
  478. avfilter_unref_buffer(link->src_buf);
  479. link->src_buf = NULL;
  480. }
  481. }
  482. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  483. {
  484. uint8_t *src[4], *dst[4];
  485. int i, j, vsub;
  486. void (*draw_slice)(AVFilterLink *, int, int, int);
  487. 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);
  488. /* copy the slice if needed for permission reasons */
  489. if (link->src_buf) {
  490. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  491. for (i = 0; i < 4; i++) {
  492. if (link->src_buf->data[i]) {
  493. src[i] = link->src_buf-> data[i] +
  494. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  495. dst[i] = link->cur_buf->data[i] +
  496. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  497. } else
  498. src[i] = dst[i] = NULL;
  499. }
  500. for (i = 0; i < 4; i++) {
  501. int planew =
  502. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  503. if (!src[i]) continue;
  504. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  505. memcpy(dst[i], src[i], planew);
  506. src[i] += link->src_buf->linesize[i];
  507. dst[i] += link->cur_buf->linesize[i];
  508. }
  509. }
  510. }
  511. if (!(draw_slice = link->dstpad->draw_slice))
  512. draw_slice = avfilter_default_draw_slice;
  513. draw_slice(link, y, h, slice_dir);
  514. }
  515. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  516. {
  517. if(!strcmp(cmd, "ping")){
  518. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  519. return 0;
  520. }else if(filter->filter->process_command) {
  521. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  522. }
  523. return AVERROR(ENOSYS);
  524. }
  525. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  526. {
  527. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  528. AVFilterPad *dst = link->dstpad;
  529. int i;
  530. FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
  531. if (!(filter_samples = dst->filter_samples))
  532. filter_samples = avfilter_default_filter_samples;
  533. /* prepare to copy the samples if the buffer has insufficient permissions */
  534. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  535. dst->rej_perms & samplesref->perms) {
  536. av_log(link->dst, AV_LOG_DEBUG,
  537. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  538. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  539. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  540. samplesref->format,
  541. samplesref->audio->nb_samples,
  542. samplesref->audio->channel_layout,
  543. samplesref->audio->planar);
  544. link->cur_buf->pts = samplesref->pts;
  545. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  546. /* Copy actual data into new samples buffer */
  547. for (i = 0; samplesref->data[i]; i++)
  548. memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
  549. avfilter_unref_buffer(samplesref);
  550. } else
  551. link->cur_buf = samplesref;
  552. filter_samples(link, link->cur_buf);
  553. }
  554. #define MAX_REGISTERED_AVFILTERS_NB 64
  555. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  556. static int next_registered_avfilter_idx = 0;
  557. AVFilter *avfilter_get_by_name(const char *name)
  558. {
  559. int i;
  560. for (i = 0; registered_avfilters[i]; i++)
  561. if (!strcmp(registered_avfilters[i]->name, name))
  562. return registered_avfilters[i];
  563. return NULL;
  564. }
  565. int avfilter_register(AVFilter *filter)
  566. {
  567. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  568. return -1;
  569. registered_avfilters[next_registered_avfilter_idx++] = filter;
  570. return 0;
  571. }
  572. AVFilter **av_filter_next(AVFilter **filter)
  573. {
  574. return filter ? ++filter : &registered_avfilters[0];
  575. }
  576. void avfilter_uninit(void)
  577. {
  578. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  579. next_registered_avfilter_idx = 0;
  580. }
  581. static int pad_count(const AVFilterPad *pads)
  582. {
  583. int count;
  584. for(count = 0; pads->name; count ++) pads ++;
  585. return count;
  586. }
  587. static const char *filter_name(void *p)
  588. {
  589. AVFilterContext *filter = p;
  590. return filter->filter->name;
  591. }
  592. static const AVClass avfilter_class = {
  593. "AVFilter",
  594. filter_name,
  595. NULL,
  596. LIBAVUTIL_VERSION_INT,
  597. };
  598. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  599. {
  600. AVFilterContext *ret;
  601. *filter_ctx = NULL;
  602. if (!filter)
  603. return AVERROR(EINVAL);
  604. ret = av_mallocz(sizeof(AVFilterContext));
  605. if (!ret)
  606. return AVERROR(ENOMEM);
  607. ret->av_class = &avfilter_class;
  608. ret->filter = filter;
  609. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  610. if (filter->priv_size) {
  611. ret->priv = av_mallocz(filter->priv_size);
  612. if (!ret->priv)
  613. goto err;
  614. }
  615. ret->input_count = pad_count(filter->inputs);
  616. if (ret->input_count) {
  617. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  618. if (!ret->input_pads)
  619. goto err;
  620. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  621. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  622. if (!ret->inputs)
  623. goto err;
  624. }
  625. ret->output_count = pad_count(filter->outputs);
  626. if (ret->output_count) {
  627. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  628. if (!ret->output_pads)
  629. goto err;
  630. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  631. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  632. if (!ret->outputs)
  633. goto err;
  634. }
  635. *filter_ctx = ret;
  636. return 0;
  637. err:
  638. av_freep(&ret->inputs);
  639. av_freep(&ret->input_pads);
  640. ret->input_count = 0;
  641. av_freep(&ret->outputs);
  642. av_freep(&ret->output_pads);
  643. ret->output_count = 0;
  644. av_freep(&ret->priv);
  645. av_free(ret);
  646. return AVERROR(ENOMEM);
  647. }
  648. void avfilter_free(AVFilterContext *filter)
  649. {
  650. int i;
  651. AVFilterLink *link;
  652. if (filter->filter->uninit)
  653. filter->filter->uninit(filter);
  654. for (i = 0; i < filter->input_count; i++) {
  655. if ((link = filter->inputs[i])) {
  656. if (link->src)
  657. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  658. avfilter_formats_unref(&link->in_formats);
  659. avfilter_formats_unref(&link->out_formats);
  660. }
  661. avfilter_link_free(&link);
  662. }
  663. for (i = 0; i < filter->output_count; i++) {
  664. if ((link = filter->outputs[i])) {
  665. if (link->dst)
  666. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  667. avfilter_formats_unref(&link->in_formats);
  668. avfilter_formats_unref(&link->out_formats);
  669. }
  670. avfilter_link_free(&link);
  671. }
  672. av_freep(&filter->name);
  673. av_freep(&filter->input_pads);
  674. av_freep(&filter->output_pads);
  675. av_freep(&filter->inputs);
  676. av_freep(&filter->outputs);
  677. av_freep(&filter->priv);
  678. av_free(filter);
  679. }
  680. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  681. {
  682. int ret=0;
  683. if (filter->filter->init)
  684. ret = filter->filter->init(filter, args, opaque);
  685. return ret;
  686. }