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.

618 lines
19KB

  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->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 && AV_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. int ret;
  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 ((ret = avfilter_config_links(link->src)) < 0)
  149. return ret;
  150. if (!(config_link = link->srcpad->config_props))
  151. config_link = avfilter_default_config_output_link;
  152. if ((ret = config_link(link)) < 0)
  153. return ret;
  154. if (link->time_base.num == 0 && link->time_base.den == 0)
  155. link->time_base = link->src && link->src->input_count ?
  156. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  157. if ((config_link = link->dstpad->config_props))
  158. if ((ret = config_link(link)) < 0)
  159. return ret;
  160. link->init_state = AVLINK_INIT;
  161. }
  162. }
  163. return 0;
  164. }
  165. char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  166. {
  167. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  168. perms & AV_PERM_READ ? "r" : "",
  169. perms & AV_PERM_WRITE ? "w" : "",
  170. perms & AV_PERM_PRESERVE ? "p" : "",
  171. perms & AV_PERM_REUSE ? "u" : "",
  172. perms & AV_PERM_REUSE2 ? "U" : "",
  173. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  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 *
  227. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  228. int w, int h, enum PixelFormat format)
  229. {
  230. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  231. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  232. if (!pic || !picref)
  233. goto fail;
  234. picref->buf = pic;
  235. picref->buf->free = ff_avfilter_default_free_buffer;
  236. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  237. goto fail;
  238. picref->video->w = w;
  239. picref->video->h = h;
  240. /* make sure the buffer gets read permission or it's useless for output */
  241. picref->perms = perms | AV_PERM_READ;
  242. pic->refcount = 1;
  243. picref->type = AVMEDIA_TYPE_VIDEO;
  244. picref->format = format;
  245. memcpy(pic->data, data, sizeof(pic->data));
  246. memcpy(pic->linesize, linesize, sizeof(pic->linesize));
  247. memcpy(picref->data, pic->data, sizeof(picref->data));
  248. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  249. return picref;
  250. fail:
  251. if (picref && picref->video)
  252. av_free(picref->video);
  253. av_free(picref);
  254. av_free(pic);
  255. return NULL;
  256. }
  257. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
  258. enum AVSampleFormat sample_fmt, int size,
  259. int64_t channel_layout, int planar)
  260. {
  261. AVFilterBufferRef *ret = NULL;
  262. if (link->dstpad->get_audio_buffer)
  263. ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  264. if (!ret)
  265. ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
  266. if (ret)
  267. ret->type = AVMEDIA_TYPE_AUDIO;
  268. return ret;
  269. }
  270. int avfilter_request_frame(AVFilterLink *link)
  271. {
  272. FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
  273. if (link->srcpad->request_frame)
  274. return link->srcpad->request_frame(link);
  275. else if (link->src->inputs[0])
  276. return avfilter_request_frame(link->src->inputs[0]);
  277. else return -1;
  278. }
  279. int avfilter_poll_frame(AVFilterLink *link)
  280. {
  281. int i, min = INT_MAX;
  282. if (link->srcpad->poll_frame)
  283. return link->srcpad->poll_frame(link);
  284. for (i = 0; i < link->src->input_count; i++) {
  285. int val;
  286. if (!link->src->inputs[i])
  287. return -1;
  288. val = avfilter_poll_frame(link->src->inputs[i]);
  289. min = FFMIN(min, val);
  290. }
  291. return min;
  292. }
  293. /* XXX: should we do the duplicating of the picture ref here, instead of
  294. * forcing the source filter to do it? */
  295. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  296. {
  297. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  298. AVFilterPad *dst = link->dstpad;
  299. int perms = picref->perms;
  300. FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_ref(NULL, picref, 1);
  301. if (!(start_frame = dst->start_frame))
  302. start_frame = avfilter_default_start_frame;
  303. if (picref->linesize[0] < 0)
  304. perms |= AV_PERM_NEG_LINESIZES;
  305. /* prepare to copy the picture if it has insufficient permissions */
  306. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  307. av_log(link->dst, AV_LOG_DEBUG,
  308. "frame copy needed (have perms %x, need %x, reject %x)\n",
  309. picref->perms,
  310. link->dstpad->min_perms, link->dstpad->rej_perms);
  311. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  312. link->src_buf = picref;
  313. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  314. }
  315. else
  316. link->cur_buf = picref;
  317. start_frame(link, link->cur_buf);
  318. }
  319. void avfilter_end_frame(AVFilterLink *link)
  320. {
  321. void (*end_frame)(AVFilterLink *);
  322. if (!(end_frame = link->dstpad->end_frame))
  323. end_frame = avfilter_default_end_frame;
  324. end_frame(link);
  325. /* unreference the source picture if we're feeding the destination filter
  326. * a copied version dues to permission issues */
  327. if (link->src_buf) {
  328. avfilter_unref_buffer(link->src_buf);
  329. link->src_buf = NULL;
  330. }
  331. }
  332. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  333. {
  334. uint8_t *src[4], *dst[4];
  335. int i, j, vsub;
  336. void (*draw_slice)(AVFilterLink *, int, int, int);
  337. 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);
  338. /* copy the slice if needed for permission reasons */
  339. if (link->src_buf) {
  340. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  341. for (i = 0; i < 4; i++) {
  342. if (link->src_buf->data[i]) {
  343. src[i] = link->src_buf-> data[i] +
  344. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  345. dst[i] = link->cur_buf->data[i] +
  346. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  347. } else
  348. src[i] = dst[i] = NULL;
  349. }
  350. for (i = 0; i < 4; i++) {
  351. int planew =
  352. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  353. if (!src[i]) continue;
  354. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  355. memcpy(dst[i], src[i], planew);
  356. src[i] += link->src_buf->linesize[i];
  357. dst[i] += link->cur_buf->linesize[i];
  358. }
  359. }
  360. }
  361. if (!(draw_slice = link->dstpad->draw_slice))
  362. draw_slice = avfilter_default_draw_slice;
  363. draw_slice(link, y, h, slice_dir);
  364. }
  365. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  366. {
  367. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  368. AVFilterPad *dst = link->dstpad;
  369. if (!(filter_samples = dst->filter_samples))
  370. filter_samples = avfilter_default_filter_samples;
  371. /* prepare to copy the samples if the buffer has insufficient permissions */
  372. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  373. dst->rej_perms & samplesref->perms) {
  374. av_log(link->dst, AV_LOG_DEBUG,
  375. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  376. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  377. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  378. samplesref->format,
  379. samplesref->audio->size,
  380. samplesref->audio->channel_layout,
  381. samplesref->audio->planar);
  382. link->cur_buf->pts = samplesref->pts;
  383. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  384. /* Copy actual data into new samples buffer */
  385. memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
  386. avfilter_unref_buffer(samplesref);
  387. } else
  388. link->cur_buf = samplesref;
  389. filter_samples(link, link->cur_buf);
  390. }
  391. #define MAX_REGISTERED_AVFILTERS_NB 64
  392. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  393. static int next_registered_avfilter_idx = 0;
  394. AVFilter *avfilter_get_by_name(const char *name)
  395. {
  396. int i;
  397. for (i = 0; registered_avfilters[i]; i++)
  398. if (!strcmp(registered_avfilters[i]->name, name))
  399. return registered_avfilters[i];
  400. return NULL;
  401. }
  402. int avfilter_register(AVFilter *filter)
  403. {
  404. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  405. return -1;
  406. registered_avfilters[next_registered_avfilter_idx++] = filter;
  407. return 0;
  408. }
  409. AVFilter **av_filter_next(AVFilter **filter)
  410. {
  411. return filter ? ++filter : &registered_avfilters[0];
  412. }
  413. void avfilter_uninit(void)
  414. {
  415. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  416. next_registered_avfilter_idx = 0;
  417. }
  418. static int pad_count(const AVFilterPad *pads)
  419. {
  420. int count;
  421. for(count = 0; pads->name; count ++) pads ++;
  422. return count;
  423. }
  424. static const char *filter_name(void *p)
  425. {
  426. AVFilterContext *filter = p;
  427. return filter->filter->name;
  428. }
  429. static const AVClass avfilter_class = {
  430. "AVFilter",
  431. filter_name,
  432. NULL,
  433. LIBAVUTIL_VERSION_INT,
  434. };
  435. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  436. {
  437. AVFilterContext *ret;
  438. *filter_ctx = NULL;
  439. if (!filter)
  440. return AVERROR(EINVAL);
  441. ret = av_mallocz(sizeof(AVFilterContext));
  442. ret->av_class = &avfilter_class;
  443. ret->filter = filter;
  444. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  445. ret->priv = av_mallocz(filter->priv_size);
  446. ret->input_count = pad_count(filter->inputs);
  447. if (ret->input_count) {
  448. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  449. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  450. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  451. }
  452. ret->output_count = pad_count(filter->outputs);
  453. if (ret->output_count) {
  454. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  455. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  456. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  457. }
  458. *filter_ctx = ret;
  459. return 0;
  460. }
  461. void avfilter_free(AVFilterContext *filter)
  462. {
  463. int i;
  464. AVFilterLink *link;
  465. if (filter->filter->uninit)
  466. filter->filter->uninit(filter);
  467. for (i = 0; i < filter->input_count; i++) {
  468. if ((link = filter->inputs[i])) {
  469. if (link->src)
  470. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  471. avfilter_formats_unref(&link->in_formats);
  472. avfilter_formats_unref(&link->out_formats);
  473. }
  474. av_freep(&link);
  475. }
  476. for (i = 0; i < filter->output_count; i++) {
  477. if ((link = filter->outputs[i])) {
  478. if (link->dst)
  479. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  480. avfilter_formats_unref(&link->in_formats);
  481. avfilter_formats_unref(&link->out_formats);
  482. }
  483. av_freep(&link);
  484. }
  485. av_freep(&filter->name);
  486. av_freep(&filter->input_pads);
  487. av_freep(&filter->output_pads);
  488. av_freep(&filter->inputs);
  489. av_freep(&filter->outputs);
  490. av_freep(&filter->priv);
  491. av_free(filter);
  492. }
  493. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  494. {
  495. int ret=0;
  496. if (filter->filter->init)
  497. ret = filter->filter->init(filter, args, opaque);
  498. return ret;
  499. }