avfilter.c 27 KB

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