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.

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