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.

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