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.

904 lines
28KB

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