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.

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