avfilter.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 "libavcore/imgutils.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. unsigned avfilter_version(void) {
  28. return LIBAVFILTER_VERSION_INT;
  29. }
  30. const char *avfilter_configuration(void)
  31. {
  32. return FFMPEG_CONFIGURATION;
  33. }
  34. const char *avfilter_license(void)
  35. {
  36. #define LICENSE_PREFIX "libavfilter license: "
  37. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  38. }
  39. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  40. {
  41. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  42. if (!ret)
  43. return NULL;
  44. *ret = *ref;
  45. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  46. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  47. if (!ret->video) {
  48. av_free(ret);
  49. return NULL;
  50. }
  51. *ret->video = *ref->video;
  52. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  53. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  54. if (!ret->audio) {
  55. av_free(ret);
  56. return NULL;
  57. }
  58. *ret->audio = *ref->audio;
  59. }
  60. ret->perms &= pmask;
  61. ret->buf->refcount ++;
  62. return ret;
  63. }
  64. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  65. {
  66. if (!(--ref->buf->refcount))
  67. ref->buf->free(ref->buf);
  68. av_free(ref->video);
  69. av_free(ref->audio);
  70. av_free(ref);
  71. }
  72. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  73. AVFilterPad **pads, AVFilterLink ***links,
  74. AVFilterPad *newpad)
  75. {
  76. unsigned i;
  77. idx = FFMIN(idx, *count);
  78. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  79. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  80. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  81. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  82. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  83. (*links)[idx] = NULL;
  84. (*count)++;
  85. for (i = idx+1; i < *count; i++)
  86. if (*links[i])
  87. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  88. }
  89. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  90. AVFilterContext *dst, unsigned dstpad)
  91. {
  92. AVFilterLink *link;
  93. if (src->output_count <= srcpad || dst->input_count <= dstpad ||
  94. src->outputs[srcpad] || dst->inputs[dstpad])
  95. return -1;
  96. src->outputs[srcpad] =
  97. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  98. link->src = src;
  99. link->dst = dst;
  100. link->srcpad = &src->output_pads[srcpad];
  101. link->dstpad = &dst->input_pads[dstpad];
  102. link->type = src->output_pads[srcpad].type;
  103. assert(PIX_FMT_NONE == -1 && SAMPLE_FMT_NONE == -1);
  104. link->format = -1;
  105. return 0;
  106. }
  107. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  108. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  109. {
  110. int ret;
  111. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  112. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  113. "between the filter '%s' and the filter '%s'\n",
  114. filt->name, link->src->name, link->dst->name);
  115. link->dst->inputs[dstpad_idx] = NULL;
  116. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  117. /* failed to link output filter to new filter */
  118. link->dst->inputs[dstpad_idx] = link;
  119. return ret;
  120. }
  121. /* re-hookup the link to the new destination filter we inserted */
  122. link->dst = filt;
  123. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  124. filt->inputs[filt_srcpad_idx] = link;
  125. /* if any information on supported media formats already exists on the
  126. * link, we need to preserve that */
  127. if (link->out_formats)
  128. avfilter_formats_changeref(&link->out_formats,
  129. &filt->outputs[filt_dstpad_idx]->out_formats);
  130. return 0;
  131. }
  132. int avfilter_config_links(AVFilterContext *filter)
  133. {
  134. int (*config_link)(AVFilterLink *);
  135. unsigned i;
  136. int ret;
  137. for (i = 0; i < filter->input_count; i ++) {
  138. AVFilterLink *link = filter->inputs[i];
  139. if (!link) continue;
  140. switch (link->init_state) {
  141. case AVLINK_INIT:
  142. continue;
  143. case AVLINK_STARTINIT:
  144. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  145. return 0;
  146. case AVLINK_UNINIT:
  147. link->init_state = AVLINK_STARTINIT;
  148. if ((ret = avfilter_config_links(link->src)) < 0)
  149. return ret;
  150. if (!(config_link = link->srcpad->config_props))
  151. config_link = avfilter_default_config_output_link;
  152. if ((ret = config_link(link)) < 0)
  153. return ret;
  154. if (link->time_base.num == 0 && link->time_base.den == 0)
  155. link->time_base = link->src && link->src->input_count ?
  156. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  157. if ((config_link = link->dstpad->config_props))
  158. if ((ret = config_link(link)) < 0)
  159. return ret;
  160. link->init_state = AVLINK_INIT;
  161. }
  162. }
  163. return 0;
  164. }
  165. char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  166. {
  167. snprintf(buf, buf_size, "%s%s%s%s%s",
  168. perms & AV_PERM_READ ? "r" : "",
  169. perms & AV_PERM_WRITE ? "w" : "",
  170. perms & AV_PERM_PRESERVE ? "p" : "",
  171. perms & AV_PERM_REUSE ? "r" : "",
  172. perms & AV_PERM_REUSE2 ? "R" : "");
  173. return buf;
  174. }
  175. void ff_dprintf_ref(void *ctx, AVFilterBufferRef *ref, int end)
  176. {
  177. av_unused char buf[16];
  178. dprintf(ctx,
  179. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  180. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  181. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  182. ref->pts, ref->pos);
  183. if (ref->video) {
  184. dprintf(ctx, " a:%d/%d s:%dx%d i:%c",
  185. ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
  186. ref->video->w, ref->video->h,
  187. !ref->video->interlaced ? 'P' : /* Progressive */
  188. ref->video->top_field_first ? 'T' : 'B'); /* Top / Bottom */
  189. }
  190. if (ref->audio) {
  191. dprintf(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
  192. ref->audio->channel_layout,
  193. ref->audio->samples_nb,
  194. ref->audio->size,
  195. ref->audio->sample_rate,
  196. ref->audio->planar);
  197. }
  198. dprintf(ctx, "]%s", end ? "\n" : "");
  199. }
  200. void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
  201. {
  202. dprintf(ctx,
  203. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  204. link, link->w, link->h,
  205. av_pix_fmt_descriptors[link->format].name,
  206. link->src ? link->src->filter->name : "",
  207. link->dst ? link->dst->filter->name : "",
  208. end ? "\n" : "");
  209. }
  210. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  211. {
  212. AVFilterBufferRef *ret = NULL;
  213. av_unused char buf[16];
  214. FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0);
  215. dprintf(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  216. if (link->dstpad->get_video_buffer)
  217. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  218. if (!ret)
  219. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  220. if (ret)
  221. ret->type = AVMEDIA_TYPE_VIDEO;
  222. FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_ref(NULL, ret, 1);
  223. return ret;
  224. }
  225. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
  226. enum SampleFormat sample_fmt, int size,
  227. int64_t channel_layout, int planar)
  228. {
  229. AVFilterBufferRef *ret = NULL;
  230. if (link->dstpad->get_audio_buffer)
  231. ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  232. if (!ret)
  233. ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  234. if (ret)
  235. ret->type = AVMEDIA_TYPE_AUDIO;
  236. return ret;
  237. }
  238. int avfilter_request_frame(AVFilterLink *link)
  239. {
  240. FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
  241. if (link->srcpad->request_frame)
  242. return link->srcpad->request_frame(link);
  243. else if (link->src->inputs[0])
  244. return avfilter_request_frame(link->src->inputs[0]);
  245. else return -1;
  246. }
  247. int avfilter_poll_frame(AVFilterLink *link)
  248. {
  249. int i, min = INT_MAX;
  250. if (link->srcpad->poll_frame)
  251. return link->srcpad->poll_frame(link);
  252. for (i = 0; i < link->src->input_count; i++) {
  253. int val;
  254. if (!link->src->inputs[i])
  255. return -1;
  256. val = avfilter_poll_frame(link->src->inputs[i]);
  257. min = FFMIN(min, val);
  258. }
  259. return min;
  260. }
  261. /* XXX: should we do the duplicating of the picture ref here, instead of
  262. * forcing the source filter to do it? */
  263. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  264. {
  265. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  266. AVFilterPad *dst = link->dstpad;
  267. FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_ref(NULL, picref, 1);
  268. if (!(start_frame = dst->start_frame))
  269. start_frame = avfilter_default_start_frame;
  270. /* prepare to copy the picture if it has insufficient permissions */
  271. if ((dst->min_perms & picref->perms) != dst->min_perms ||
  272. dst->rej_perms & picref->perms) {
  273. av_log(link->dst, AV_LOG_DEBUG,
  274. "frame copy needed (have perms %x, need %x, reject %x)\n",
  275. picref->perms,
  276. link->dstpad->min_perms, link->dstpad->rej_perms);
  277. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  278. link->src_buf = picref;
  279. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  280. }
  281. else
  282. link->cur_buf = picref;
  283. start_frame(link, link->cur_buf);
  284. }
  285. void avfilter_end_frame(AVFilterLink *link)
  286. {
  287. void (*end_frame)(AVFilterLink *);
  288. if (!(end_frame = link->dstpad->end_frame))
  289. end_frame = avfilter_default_end_frame;
  290. end_frame(link);
  291. /* unreference the source picture if we're feeding the destination filter
  292. * a copied version dues to permission issues */
  293. if (link->src_buf) {
  294. avfilter_unref_buffer(link->src_buf);
  295. link->src_buf = NULL;
  296. }
  297. }
  298. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  299. {
  300. uint8_t *src[4], *dst[4];
  301. int i, j, vsub;
  302. void (*draw_slice)(AVFilterLink *, int, int, int);
  303. FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
  304. /* copy the slice if needed for permission reasons */
  305. if (link->src_buf) {
  306. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  307. for (i = 0; i < 4; i++) {
  308. if (link->src_buf->data[i]) {
  309. src[i] = link->src_buf-> data[i] +
  310. (y >> (i==0 ? 0 : vsub)) * link->src_buf-> linesize[i];
  311. dst[i] = link->cur_buf->data[i] +
  312. (y >> (i==0 ? 0 : vsub)) * link->cur_buf->linesize[i];
  313. } else
  314. src[i] = dst[i] = NULL;
  315. }
  316. for (i = 0; i < 4; i++) {
  317. int planew =
  318. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  319. if (!src[i]) continue;
  320. for (j = 0; j < h >> (i==0 ? 0 : vsub); j++) {
  321. memcpy(dst[i], src[i], planew);
  322. src[i] += link->src_buf->linesize[i];
  323. dst[i] += link->cur_buf->linesize[i];
  324. }
  325. }
  326. }
  327. if (!(draw_slice = link->dstpad->draw_slice))
  328. draw_slice = avfilter_default_draw_slice;
  329. draw_slice(link, y, h, slice_dir);
  330. }
  331. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  332. {
  333. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  334. AVFilterPad *dst = link->dstpad;
  335. if (!(filter_samples = dst->filter_samples))
  336. filter_samples = avfilter_default_filter_samples;
  337. /* prepare to copy the samples if the buffer has insufficient permissions */
  338. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  339. dst->rej_perms & samplesref->perms) {
  340. av_log(link->dst, AV_LOG_DEBUG,
  341. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  342. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  343. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  344. samplesref->format,
  345. samplesref->audio->size,
  346. samplesref->audio->channel_layout,
  347. samplesref->audio->planar);
  348. link->cur_buf->pts = samplesref->pts;
  349. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  350. /* Copy actual data into new samples buffer */
  351. memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
  352. avfilter_unref_buffer(samplesref);
  353. } else
  354. link->cur_buf = samplesref;
  355. filter_samples(link, link->cur_buf);
  356. }
  357. #define MAX_REGISTERED_AVFILTERS_NB 64
  358. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  359. static int next_registered_avfilter_idx = 0;
  360. AVFilter *avfilter_get_by_name(const char *name)
  361. {
  362. int i;
  363. for (i = 0; registered_avfilters[i]; i++)
  364. if (!strcmp(registered_avfilters[i]->name, name))
  365. return registered_avfilters[i];
  366. return NULL;
  367. }
  368. int avfilter_register(AVFilter *filter)
  369. {
  370. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  371. return -1;
  372. registered_avfilters[next_registered_avfilter_idx++] = filter;
  373. return 0;
  374. }
  375. AVFilter **av_filter_next(AVFilter **filter)
  376. {
  377. return filter ? ++filter : &registered_avfilters[0];
  378. }
  379. void avfilter_uninit(void)
  380. {
  381. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  382. next_registered_avfilter_idx = 0;
  383. }
  384. static int pad_count(const AVFilterPad *pads)
  385. {
  386. int count;
  387. for(count = 0; pads->name; count ++) pads ++;
  388. return count;
  389. }
  390. static const char *filter_name(void *p)
  391. {
  392. AVFilterContext *filter = p;
  393. return filter->filter->name;
  394. }
  395. static const AVClass avfilter_class = {
  396. "AVFilter",
  397. filter_name,
  398. NULL,
  399. LIBAVUTIL_VERSION_INT,
  400. };
  401. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  402. {
  403. AVFilterContext *ret;
  404. *filter_ctx = NULL;
  405. if (!filter)
  406. return AVERROR(EINVAL);
  407. ret = av_mallocz(sizeof(AVFilterContext));
  408. ret->av_class = &avfilter_class;
  409. ret->filter = filter;
  410. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  411. ret->priv = av_mallocz(filter->priv_size);
  412. ret->input_count = pad_count(filter->inputs);
  413. if (ret->input_count) {
  414. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  415. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  416. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  417. }
  418. ret->output_count = pad_count(filter->outputs);
  419. if (ret->output_count) {
  420. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  421. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  422. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  423. }
  424. *filter_ctx = ret;
  425. return 0;
  426. }
  427. void avfilter_destroy(AVFilterContext *filter)
  428. {
  429. int i;
  430. AVFilterLink *link;
  431. if (filter->filter->uninit)
  432. filter->filter->uninit(filter);
  433. for (i = 0; i < filter->input_count; i++) {
  434. if ((link = filter->inputs[i])) {
  435. if (link->src)
  436. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  437. avfilter_formats_unref(&link->in_formats);
  438. avfilter_formats_unref(&link->out_formats);
  439. }
  440. av_freep(&link);
  441. }
  442. for (i = 0; i < filter->output_count; i++) {
  443. if ((link = filter->outputs[i])) {
  444. if (link->dst)
  445. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  446. avfilter_formats_unref(&link->in_formats);
  447. avfilter_formats_unref(&link->out_formats);
  448. }
  449. av_freep(&link);
  450. }
  451. av_freep(&filter->name);
  452. av_freep(&filter->input_pads);
  453. av_freep(&filter->output_pads);
  454. av_freep(&filter->inputs);
  455. av_freep(&filter->outputs);
  456. av_freep(&filter->priv);
  457. av_free(filter);
  458. }
  459. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  460. {
  461. int ret=0;
  462. if (filter->filter->init)
  463. ret = filter->filter->init(filter, args, opaque);
  464. return ret;
  465. }