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.

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