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.

627 lines
20KB

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