You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
18KB

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