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.

483 lines
14KB

  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 "libavcore/imgutils.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. unsigned avfilter_version(void) {
  27. return LIBAVFILTER_VERSION_INT;
  28. }
  29. const char *avfilter_configuration(void)
  30. {
  31. return FFMPEG_CONFIGURATION;
  32. }
  33. const char *avfilter_license(void)
  34. {
  35. #define LICENSE_PREFIX "libavfilter license: "
  36. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  37. }
  38. /** helper macros to get the in/out pad on the dst/src filter */
  39. #define link_dpad(link) link->dst-> input_pads[link->dstpad]
  40. #define link_spad(link) link->src->output_pads[link->srcpad]
  41. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  42. {
  43. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  44. if (!ret)
  45. return NULL;
  46. *ret = *ref;
  47. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  48. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  49. if (!ret->video) {
  50. av_free(ret);
  51. return NULL;
  52. }
  53. *ret->video = *ref->video;
  54. }
  55. ret->perms &= pmask;
  56. ret->buf->refcount ++;
  57. return ret;
  58. }
  59. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  60. {
  61. if(!(--ref->buf->refcount))
  62. ref->buf->free(ref->buf);
  63. av_free(ref->video);
  64. av_free(ref);
  65. }
  66. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  67. AVFilterPad **pads, AVFilterLink ***links,
  68. AVFilterPad *newpad)
  69. {
  70. unsigned i;
  71. idx = FFMIN(idx, *count);
  72. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  73. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  74. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  75. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  76. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  77. (*links)[idx] = NULL;
  78. (*count) ++;
  79. for(i = idx+1; i < *count; i ++)
  80. if(*links[i])
  81. (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
  82. }
  83. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  84. AVFilterContext *dst, unsigned dstpad)
  85. {
  86. AVFilterLink *link;
  87. if(src->output_count <= srcpad || dst->input_count <= dstpad ||
  88. src->outputs[srcpad] || dst->inputs[dstpad])
  89. return -1;
  90. src->outputs[srcpad] =
  91. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  92. link->src = src;
  93. link->dst = dst;
  94. link->srcpad = srcpad;
  95. link->dstpad = dstpad;
  96. link->type = src->output_pads[srcpad].type;
  97. assert(PIX_FMT_NONE == -1 && SAMPLE_FMT_NONE == -1);
  98. link->format = -1;
  99. return 0;
  100. }
  101. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  102. unsigned in, unsigned out)
  103. {
  104. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  105. "between the filter '%s' and the filter '%s'\n",
  106. filt->name, link->src->name, link->dst->name);
  107. link->dst->inputs[link->dstpad] = NULL;
  108. if(avfilter_link(filt, out, link->dst, link->dstpad)) {
  109. /* failed to link output filter to new filter */
  110. link->dst->inputs[link->dstpad] = link;
  111. return -1;
  112. }
  113. /* re-hookup the link to the new destination filter we inserted */
  114. link->dst = filt;
  115. link->dstpad = in;
  116. filt->inputs[in] = link;
  117. /* if any information on supported media formats already exists on the
  118. * link, we need to preserve that */
  119. if(link->out_formats)
  120. avfilter_formats_changeref(&link->out_formats,
  121. &filt->outputs[out]->out_formats);
  122. return 0;
  123. }
  124. int avfilter_config_links(AVFilterContext *filter)
  125. {
  126. int (*config_link)(AVFilterLink *);
  127. unsigned i;
  128. for(i = 0; i < filter->input_count; i ++) {
  129. AVFilterLink *link = filter->inputs[i];
  130. if(!link) continue;
  131. switch(link->init_state) {
  132. case AVLINK_INIT:
  133. continue;
  134. case AVLINK_STARTINIT:
  135. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  136. return 0;
  137. case AVLINK_UNINIT:
  138. link->init_state = AVLINK_STARTINIT;
  139. if(avfilter_config_links(link->src))
  140. return -1;
  141. if(!(config_link = link_spad(link).config_props))
  142. config_link = avfilter_default_config_output_link;
  143. if(config_link(link))
  144. return -1;
  145. if((config_link = link_dpad(link).config_props))
  146. if(config_link(link))
  147. return -1;
  148. link->init_state = AVLINK_INIT;
  149. }
  150. }
  151. return 0;
  152. }
  153. void ff_dprintf_picref(void *ctx, AVFilterBufferRef *picref, int end)
  154. {
  155. dprintf(ctx,
  156. "picref[%p data[%p, %p, %p, %p] linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64" a:%d/%d s:%dx%d]%s",
  157. picref,
  158. picref->data [0], picref->data [1], picref->data [2], picref->data [3],
  159. picref->linesize[0], picref->linesize[1], picref->linesize[2], picref->linesize[3],
  160. picref->pts, picref->pos,
  161. picref->video->pixel_aspect.num, picref->video->pixel_aspect.den, picref->video->w, picref->video->h,
  162. end ? "\n" : "");
  163. }
  164. void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
  165. {
  166. dprintf(ctx,
  167. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  168. link, link->w, link->h,
  169. av_pix_fmt_descriptors[link->format].name,
  170. link->src ? link->src->filter->name : "",
  171. link->dst ? link->dst->filter->name : "",
  172. end ? "\n" : "");
  173. }
  174. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  175. {
  176. AVFilterBufferRef *ret = NULL;
  177. FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " perms:%d w:%d h:%d\n", perms, w, h);
  178. if(link_dpad(link).get_video_buffer)
  179. ret = link_dpad(link).get_video_buffer(link, perms, w, h);
  180. if(!ret)
  181. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  182. if (ret)
  183. ret->type = AVMEDIA_TYPE_VIDEO;
  184. FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_picref(NULL, ret, 1);
  185. return ret;
  186. }
  187. int avfilter_request_frame(AVFilterLink *link)
  188. {
  189. FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
  190. if(link_spad(link).request_frame)
  191. return link_spad(link).request_frame(link);
  192. else if(link->src->inputs[0])
  193. return avfilter_request_frame(link->src->inputs[0]);
  194. else return -1;
  195. }
  196. int avfilter_poll_frame(AVFilterLink *link)
  197. {
  198. int i, min=INT_MAX;
  199. if(link_spad(link).poll_frame)
  200. return link_spad(link).poll_frame(link);
  201. for (i=0; i<link->src->input_count; i++) {
  202. int val;
  203. if(!link->src->inputs[i])
  204. return -1;
  205. val = avfilter_poll_frame(link->src->inputs[i]);
  206. min = FFMIN(min, val);
  207. }
  208. return min;
  209. }
  210. /* XXX: should we do the duplicating of the picture ref here, instead of
  211. * forcing the source filter to do it? */
  212. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  213. {
  214. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  215. AVFilterPad *dst = &link_dpad(link);
  216. FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_picref(NULL, picref, 1);
  217. if(!(start_frame = dst->start_frame))
  218. start_frame = avfilter_default_start_frame;
  219. /* prepare to copy the picture if it has insufficient permissions */
  220. if((dst->min_perms & picref->perms) != dst->min_perms ||
  221. dst->rej_perms & picref->perms) {
  222. /*
  223. av_log(link->dst, AV_LOG_INFO,
  224. "frame copy needed (have perms %x, need %x, reject %x)\n",
  225. picref->perms,
  226. link_dpad(link).min_perms, link_dpad(link).rej_perms);
  227. */
  228. link->cur_buf = avfilter_default_get_video_buffer(link, dst->min_perms, link->w, link->h);
  229. link->src_buf = picref;
  230. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  231. }
  232. else
  233. link->cur_buf = picref;
  234. start_frame(link, link->cur_buf);
  235. }
  236. void avfilter_end_frame(AVFilterLink *link)
  237. {
  238. void (*end_frame)(AVFilterLink *);
  239. if(!(end_frame = link_dpad(link).end_frame))
  240. end_frame = avfilter_default_end_frame;
  241. end_frame(link);
  242. /* unreference the source picture if we're feeding the destination filter
  243. * a copied version dues to permission issues */
  244. if(link->src_buf) {
  245. avfilter_unref_buffer(link->src_buf);
  246. link->src_buf = NULL;
  247. }
  248. }
  249. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  250. {
  251. uint8_t *src[4], *dst[4];
  252. int i, j, vsub;
  253. void (*draw_slice)(AVFilterLink *, int, int, int);
  254. 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);
  255. /* copy the slice if needed for permission reasons */
  256. if(link->src_buf) {
  257. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  258. for(i = 0; i < 4; i ++) {
  259. if(link->src_buf->data[i]) {
  260. src[i] = link->src_buf-> data[i] +
  261. (y >> (i==0 ? 0 : vsub)) * link->src_buf-> linesize[i];
  262. dst[i] = link->cur_buf->data[i] +
  263. (y >> (i==0 ? 0 : vsub)) * link->cur_buf->linesize[i];
  264. } else
  265. src[i] = dst[i] = NULL;
  266. }
  267. for(i = 0; i < 4; i ++) {
  268. int planew =
  269. av_get_image_linesize(link->format, link->cur_buf->video->w, i);
  270. if(!src[i]) continue;
  271. for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
  272. memcpy(dst[i], src[i], planew);
  273. src[i] += link->src_buf ->linesize[i];
  274. dst[i] += link->cur_buf->linesize[i];
  275. }
  276. }
  277. }
  278. if(!(draw_slice = link_dpad(link).draw_slice))
  279. draw_slice = avfilter_default_draw_slice;
  280. draw_slice(link, y, h, slice_dir);
  281. }
  282. #define MAX_REGISTERED_AVFILTERS_NB 64
  283. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  284. static int next_registered_avfilter_idx = 0;
  285. AVFilter *avfilter_get_by_name(const char *name)
  286. {
  287. int i;
  288. for (i = 0; registered_avfilters[i]; i++)
  289. if (!strcmp(registered_avfilters[i]->name, name))
  290. return registered_avfilters[i];
  291. return NULL;
  292. }
  293. int avfilter_register(AVFilter *filter)
  294. {
  295. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
  296. return -1;
  297. registered_avfilters[next_registered_avfilter_idx++] = filter;
  298. return 0;
  299. }
  300. AVFilter **av_filter_next(AVFilter **filter)
  301. {
  302. return filter ? ++filter : &registered_avfilters[0];
  303. }
  304. void avfilter_uninit(void)
  305. {
  306. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  307. next_registered_avfilter_idx = 0;
  308. }
  309. static int pad_count(const AVFilterPad *pads)
  310. {
  311. int count;
  312. for(count = 0; pads->name; count ++) pads ++;
  313. return count;
  314. }
  315. static const char *filter_name(void *p)
  316. {
  317. AVFilterContext *filter = p;
  318. return filter->filter->name;
  319. }
  320. static const AVClass avfilter_class = {
  321. "AVFilter",
  322. filter_name,
  323. NULL,
  324. LIBAVUTIL_VERSION_INT,
  325. };
  326. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  327. {
  328. AVFilterContext *ret;
  329. *filter_ctx = NULL;
  330. if (!filter)
  331. return AVERROR(EINVAL);
  332. ret = av_mallocz(sizeof(AVFilterContext));
  333. ret->av_class = &avfilter_class;
  334. ret->filter = filter;
  335. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  336. ret->priv = av_mallocz(filter->priv_size);
  337. ret->input_count = pad_count(filter->inputs);
  338. if (ret->input_count) {
  339. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  340. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  341. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  342. }
  343. ret->output_count = pad_count(filter->outputs);
  344. if (ret->output_count) {
  345. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  346. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  347. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  348. }
  349. *filter_ctx = ret;
  350. return 0;
  351. }
  352. void avfilter_destroy(AVFilterContext *filter)
  353. {
  354. int i;
  355. if(filter->filter->uninit)
  356. filter->filter->uninit(filter);
  357. for(i = 0; i < filter->input_count; i ++) {
  358. if(filter->inputs[i]) {
  359. if (filter->inputs[i]->src)
  360. filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
  361. avfilter_formats_unref(&filter->inputs[i]->in_formats);
  362. avfilter_formats_unref(&filter->inputs[i]->out_formats);
  363. }
  364. av_freep(&filter->inputs[i]);
  365. }
  366. for(i = 0; i < filter->output_count; i ++) {
  367. if(filter->outputs[i]) {
  368. if (filter->outputs[i]->dst)
  369. filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
  370. avfilter_formats_unref(&filter->outputs[i]->in_formats);
  371. avfilter_formats_unref(&filter->outputs[i]->out_formats);
  372. }
  373. av_freep(&filter->outputs[i]);
  374. }
  375. av_freep(&filter->name);
  376. av_freep(&filter->input_pads);
  377. av_freep(&filter->output_pads);
  378. av_freep(&filter->inputs);
  379. av_freep(&filter->outputs);
  380. av_freep(&filter->priv);
  381. av_free(filter);
  382. }
  383. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  384. {
  385. int ret=0;
  386. if(filter->filter->init)
  387. ret = filter->filter->init(filter, args, opaque);
  388. return ret;
  389. }