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.

889 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. switch (link->init_state) {
  241. case AVLINK_INIT:
  242. continue;
  243. case AVLINK_STARTINIT:
  244. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  245. return 0;
  246. case AVLINK_UNINIT:
  247. link->init_state = AVLINK_STARTINIT;
  248. if ((ret = avfilter_config_links(link->src)) < 0)
  249. return ret;
  250. if (!(config_link = link->srcpad->config_props)) {
  251. if (link->src->input_count != 1) {
  252. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  253. "with more than one input "
  254. "must set config_props() "
  255. "callbacks on all outputs\n");
  256. return AVERROR(EINVAL);
  257. }
  258. } else if ((ret = config_link(link)) < 0)
  259. return ret;
  260. switch (link->type) {
  261. case AVMEDIA_TYPE_VIDEO:
  262. if (!link->time_base.num && !link->time_base.den)
  263. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  264. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  265. link->sample_aspect_ratio = inlink ?
  266. inlink->sample_aspect_ratio : (AVRational){1,1};
  267. if (inlink) {
  268. if (!link->w)
  269. link->w = inlink->w;
  270. if (!link->h)
  271. link->h = inlink->h;
  272. } else if (!link->w || !link->h) {
  273. av_log(link->src, AV_LOG_ERROR,
  274. "Video source filters must set their output link's "
  275. "width and height\n");
  276. return AVERROR(EINVAL);
  277. }
  278. break;
  279. case AVMEDIA_TYPE_AUDIO:
  280. if (inlink) {
  281. if (!link->sample_rate)
  282. link->sample_rate = inlink->sample_rate;
  283. if (!link->time_base.num && !link->time_base.den)
  284. link->time_base = inlink->time_base;
  285. } else if (!link->sample_rate) {
  286. av_log(link->src, AV_LOG_ERROR,
  287. "Audio source filters must set their output link's "
  288. "sample_rate\n");
  289. return AVERROR(EINVAL);
  290. }
  291. if (!link->time_base.num && !link->time_base.den)
  292. link->time_base = (AVRational) {1, link->sample_rate};
  293. }
  294. if ((config_link = link->dstpad->config_props))
  295. if ((ret = config_link(link)) < 0)
  296. return ret;
  297. link->init_state = AVLINK_INIT;
  298. }
  299. }
  300. return 0;
  301. }
  302. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  303. {
  304. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  305. perms & AV_PERM_READ ? "r" : "",
  306. perms & AV_PERM_WRITE ? "w" : "",
  307. perms & AV_PERM_PRESERVE ? "p" : "",
  308. perms & AV_PERM_REUSE ? "u" : "",
  309. perms & AV_PERM_REUSE2 ? "U" : "",
  310. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  311. return buf;
  312. }
  313. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  314. {
  315. av_unused char buf[16];
  316. av_dlog(ctx,
  317. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  318. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  319. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  320. ref->pts, ref->pos);
  321. if (ref->video) {
  322. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  323. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  324. ref->video->w, ref->video->h,
  325. !ref->video->interlaced ? 'P' : /* Progressive */
  326. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  327. ref->video->key_frame,
  328. av_get_picture_type_char(ref->video->pict_type));
  329. }
  330. if (ref->audio) {
  331. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
  332. ref->audio->channel_layout,
  333. ref->audio->nb_samples,
  334. ref->audio->sample_rate,
  335. ref->audio->planar);
  336. }
  337. av_dlog(ctx, "]%s", end ? "\n" : "");
  338. }
  339. static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  340. {
  341. if (link->type == AVMEDIA_TYPE_VIDEO) {
  342. av_dlog(ctx,
  343. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  344. link, link->w, link->h,
  345. av_pix_fmt_descriptors[link->format].name,
  346. link->src ? link->src->filter->name : "",
  347. link->dst ? link->dst->filter->name : "",
  348. end ? "\n" : "");
  349. } else {
  350. char buf[128];
  351. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  352. av_dlog(ctx,
  353. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  354. link, (int)link->sample_rate, buf,
  355. av_get_sample_fmt_name(link->format),
  356. link->src ? link->src->filter->name : "",
  357. link->dst ? link->dst->filter->name : "",
  358. end ? "\n" : "");
  359. }
  360. }
  361. #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
  362. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  363. {
  364. AVFilterBufferRef *ret = NULL;
  365. av_unused char buf[16];
  366. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  367. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  368. if (link->dstpad->get_video_buffer)
  369. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  370. if (!ret)
  371. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  372. if (ret)
  373. ret->type = AVMEDIA_TYPE_VIDEO;
  374. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  375. return ret;
  376. }
  377. AVFilterBufferRef *
  378. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  379. int w, int h, enum PixelFormat format)
  380. {
  381. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  382. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  383. if (!pic || !picref)
  384. goto fail;
  385. picref->buf = pic;
  386. picref->buf->free = ff_avfilter_default_free_buffer;
  387. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  388. goto fail;
  389. pic->w = picref->video->w = w;
  390. pic->h = picref->video->h = h;
  391. /* make sure the buffer gets read permission or it's useless for output */
  392. picref->perms = perms | AV_PERM_READ;
  393. pic->refcount = 1;
  394. picref->type = AVMEDIA_TYPE_VIDEO;
  395. pic->format = picref->format = format;
  396. memcpy(pic->data, data, 4*sizeof(data[0]));
  397. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  398. memcpy(picref->data, pic->data, sizeof(picref->data));
  399. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  400. return picref;
  401. fail:
  402. if (picref && picref->video)
  403. av_free(picref->video);
  404. av_free(picref);
  405. av_free(pic);
  406. return NULL;
  407. }
  408. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link,
  409. int perms, int nb_samples)
  410. {
  411. AVFilterBufferRef *ret = NULL;
  412. if (link->dstpad->get_audio_buffer)
  413. ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
  414. if (!ret)
  415. ret = avfilter_default_get_audio_buffer(link, perms, nb_samples);
  416. if (ret)
  417. ret->type = AVMEDIA_TYPE_AUDIO;
  418. return ret;
  419. }
  420. AVFilterBufferRef *
  421. avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
  422. int nb_samples, enum AVSampleFormat sample_fmt,
  423. uint64_t channel_layout, int planar)
  424. {
  425. AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
  426. AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
  427. if (!samples || !samplesref)
  428. goto fail;
  429. samplesref->buf = samples;
  430. samplesref->buf->free = ff_avfilter_default_free_buffer;
  431. if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
  432. goto fail;
  433. samplesref->audio->nb_samples = nb_samples;
  434. samplesref->audio->channel_layout = channel_layout;
  435. samplesref->audio->planar = planar;
  436. /* make sure the buffer gets read permission or it's useless for output */
  437. samplesref->perms = perms | AV_PERM_READ;
  438. samples->refcount = 1;
  439. samplesref->type = AVMEDIA_TYPE_AUDIO;
  440. samplesref->format = sample_fmt;
  441. memcpy(samples->data, data, sizeof(samples->data));
  442. memcpy(samples->linesize, linesize, sizeof(samples->linesize));
  443. memcpy(samplesref->data, data, sizeof(samplesref->data));
  444. memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
  445. return samplesref;
  446. fail:
  447. if (samplesref && samplesref->audio)
  448. av_freep(&samplesref->audio);
  449. av_freep(&samplesref);
  450. av_freep(&samples);
  451. return NULL;
  452. }
  453. int avfilter_request_frame(AVFilterLink *link)
  454. {
  455. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  456. if (link->srcpad->request_frame)
  457. return link->srcpad->request_frame(link);
  458. else if (link->src->inputs[0])
  459. return avfilter_request_frame(link->src->inputs[0]);
  460. else return -1;
  461. }
  462. int avfilter_poll_frame(AVFilterLink *link)
  463. {
  464. int i, min = INT_MAX;
  465. if (link->srcpad->poll_frame)
  466. return link->srcpad->poll_frame(link);
  467. for (i = 0; i < link->src->input_count; i++) {
  468. int val;
  469. if (!link->src->inputs[i])
  470. return -1;
  471. val = avfilter_poll_frame(link->src->inputs[i]);
  472. min = FFMIN(min, val);
  473. }
  474. return min;
  475. }
  476. /* XXX: should we do the duplicating of the picture ref here, instead of
  477. * forcing the source filter to do it? */
  478. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  479. {
  480. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  481. AVFilterPad *dst = link->dstpad;
  482. int perms = picref->perms;
  483. AVFilterCommand *cmd= link->dst->command_queue;
  484. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  485. if (!(start_frame = dst->start_frame))
  486. start_frame = avfilter_default_start_frame;
  487. if (picref->linesize[0] < 0)
  488. perms |= AV_PERM_NEG_LINESIZES;
  489. /* prepare to copy the picture if it has insufficient permissions */
  490. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  491. av_log(link->dst, AV_LOG_DEBUG,
  492. "frame copy needed (have perms %x, need %x, reject %x)\n",
  493. picref->perms,
  494. link->dstpad->min_perms, link->dstpad->rej_perms);
  495. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  496. link->src_buf = picref;
  497. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  498. }
  499. else
  500. link->cur_buf = picref;
  501. while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
  502. av_log(link->dst, AV_LOG_DEBUG,
  503. "Processing command time:%f command:%s arg:%s\n",
  504. cmd->time, cmd->command, cmd->arg);
  505. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  506. command_queue_pop(link->dst);
  507. cmd= link->dst->command_queue;
  508. }
  509. start_frame(link, link->cur_buf);
  510. }
  511. void avfilter_end_frame(AVFilterLink *link)
  512. {
  513. void (*end_frame)(AVFilterLink *);
  514. if (!(end_frame = link->dstpad->end_frame))
  515. end_frame = avfilter_default_end_frame;
  516. end_frame(link);
  517. /* unreference the source picture if we're feeding the destination filter
  518. * a copied version dues to permission issues */
  519. if (link->src_buf) {
  520. avfilter_unref_buffer(link->src_buf);
  521. link->src_buf = NULL;
  522. }
  523. }
  524. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  525. {
  526. uint8_t *src[4], *dst[4];
  527. int i, j, vsub;
  528. void (*draw_slice)(AVFilterLink *, int, int, int);
  529. 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);
  530. /* copy the slice if needed for permission reasons */
  531. if (link->src_buf) {
  532. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  533. for (i = 0; i < 4; i++) {
  534. if (link->src_buf->data[i]) {
  535. src[i] = link->src_buf-> data[i] +
  536. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  537. dst[i] = link->cur_buf->data[i] +
  538. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  539. } else
  540. src[i] = dst[i] = NULL;
  541. }
  542. for (i = 0; i < 4; i++) {
  543. int planew =
  544. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  545. if (!src[i]) continue;
  546. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  547. memcpy(dst[i], src[i], planew);
  548. src[i] += link->src_buf->linesize[i];
  549. dst[i] += link->cur_buf->linesize[i];
  550. }
  551. }
  552. }
  553. if (!(draw_slice = link->dstpad->draw_slice))
  554. draw_slice = avfilter_default_draw_slice;
  555. draw_slice(link, y, h, slice_dir);
  556. }
  557. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  558. {
  559. if(!strcmp(cmd, "ping")){
  560. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  561. return 0;
  562. }else if(filter->filter->process_command) {
  563. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  564. }
  565. return AVERROR(ENOSYS);
  566. }
  567. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  568. {
  569. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  570. AVFilterPad *dst = link->dstpad;
  571. int i;
  572. FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
  573. if (!(filter_samples = dst->filter_samples))
  574. filter_samples = avfilter_default_filter_samples;
  575. /* prepare to copy the samples if the buffer has insufficient permissions */
  576. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  577. dst->rej_perms & samplesref->perms) {
  578. av_log(link->dst, AV_LOG_DEBUG,
  579. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  580. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  581. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  582. samplesref->audio->nb_samples);
  583. link->cur_buf->pts = samplesref->pts;
  584. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  585. /* Copy actual data into new samples buffer */
  586. for (i = 0; samplesref->data[i] && i < 8; i++)
  587. memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
  588. avfilter_unref_buffer(samplesref);
  589. } else
  590. link->cur_buf = samplesref;
  591. filter_samples(link, link->cur_buf);
  592. }
  593. #define MAX_REGISTERED_AVFILTERS_NB 128
  594. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  595. static int next_registered_avfilter_idx = 0;
  596. AVFilter *avfilter_get_by_name(const char *name)
  597. {
  598. int i;
  599. for (i = 0; registered_avfilters[i]; i++)
  600. if (!strcmp(registered_avfilters[i]->name, name))
  601. return registered_avfilters[i];
  602. return NULL;
  603. }
  604. int avfilter_register(AVFilter *filter)
  605. {
  606. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  607. av_log(NULL, AV_LOG_ERROR,
  608. "Maximum number of registered filters %d reached, "
  609. "impossible to register filter with name '%s'\n",
  610. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  611. return AVERROR(ENOMEM);
  612. }
  613. registered_avfilters[next_registered_avfilter_idx++] = filter;
  614. return 0;
  615. }
  616. AVFilter **av_filter_next(AVFilter **filter)
  617. {
  618. return filter ? ++filter : &registered_avfilters[0];
  619. }
  620. void avfilter_uninit(void)
  621. {
  622. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  623. next_registered_avfilter_idx = 0;
  624. }
  625. static int pad_count(const AVFilterPad *pads)
  626. {
  627. int count;
  628. for(count = 0; pads->name; count ++) pads ++;
  629. return count;
  630. }
  631. static const char *filter_name(void *p)
  632. {
  633. AVFilterContext *filter = p;
  634. return filter->filter->name;
  635. }
  636. static const AVClass avfilter_class = {
  637. "AVFilter",
  638. filter_name,
  639. NULL,
  640. LIBAVUTIL_VERSION_INT,
  641. };
  642. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  643. {
  644. AVFilterContext *ret;
  645. *filter_ctx = NULL;
  646. if (!filter)
  647. return AVERROR(EINVAL);
  648. ret = av_mallocz(sizeof(AVFilterContext));
  649. if (!ret)
  650. return AVERROR(ENOMEM);
  651. ret->av_class = &avfilter_class;
  652. ret->filter = filter;
  653. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  654. if (filter->priv_size) {
  655. ret->priv = av_mallocz(filter->priv_size);
  656. if (!ret->priv)
  657. goto err;
  658. }
  659. ret->input_count = pad_count(filter->inputs);
  660. if (ret->input_count) {
  661. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  662. if (!ret->input_pads)
  663. goto err;
  664. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  665. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  666. if (!ret->inputs)
  667. goto err;
  668. }
  669. ret->output_count = pad_count(filter->outputs);
  670. if (ret->output_count) {
  671. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  672. if (!ret->output_pads)
  673. goto err;
  674. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  675. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  676. if (!ret->outputs)
  677. goto err;
  678. }
  679. *filter_ctx = ret;
  680. return 0;
  681. err:
  682. av_freep(&ret->inputs);
  683. av_freep(&ret->input_pads);
  684. ret->input_count = 0;
  685. av_freep(&ret->outputs);
  686. av_freep(&ret->output_pads);
  687. ret->output_count = 0;
  688. av_freep(&ret->priv);
  689. av_free(ret);
  690. return AVERROR(ENOMEM);
  691. }
  692. void avfilter_free(AVFilterContext *filter)
  693. {
  694. int i;
  695. AVFilterLink *link;
  696. if (!filter)
  697. return;
  698. if (filter->filter->uninit)
  699. filter->filter->uninit(filter);
  700. for (i = 0; i < filter->input_count; i++) {
  701. if ((link = filter->inputs[i])) {
  702. if (link->src)
  703. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  704. avfilter_formats_unref(&link->in_formats);
  705. avfilter_formats_unref(&link->out_formats);
  706. }
  707. avfilter_link_free(&link);
  708. }
  709. for (i = 0; i < filter->output_count; i++) {
  710. if ((link = filter->outputs[i])) {
  711. if (link->dst)
  712. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  713. avfilter_formats_unref(&link->in_formats);
  714. avfilter_formats_unref(&link->out_formats);
  715. }
  716. avfilter_link_free(&link);
  717. }
  718. av_freep(&filter->name);
  719. av_freep(&filter->input_pads);
  720. av_freep(&filter->output_pads);
  721. av_freep(&filter->inputs);
  722. av_freep(&filter->outputs);
  723. av_freep(&filter->priv);
  724. while(filter->command_queue){
  725. command_queue_pop(filter);
  726. }
  727. av_free(filter);
  728. }
  729. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  730. {
  731. int ret=0;
  732. if (filter->filter->init)
  733. ret = filter->filter->init(filter, args, opaque);
  734. return ret;
  735. }