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.

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