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.

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