avfilter.c 25 KB

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