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