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.

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