avfilter.c 28 KB

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