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.

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