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.

390 lines
12KB

  1. /*
  2. * Copyright 2007 Bobby Bingham
  3. * Copyright Stefano Sabatini <stefasab gmail com>
  4. * Copyright Vitor Sessak <vitor1001 gmail com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/imgutils.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. #include "video.h"
  26. AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  27. {
  28. return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
  29. }
  30. AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  31. {
  32. int linesize[4];
  33. uint8_t *data[4];
  34. int i;
  35. AVFilterBufferRef *picref = NULL;
  36. AVFilterPool *pool = link->pool;
  37. if (pool) {
  38. for (i = 0; i < POOL_SIZE; i++) {
  39. picref = pool->pic[i];
  40. if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
  41. AVFilterBuffer *pic = picref->buf;
  42. pool->pic[i] = NULL;
  43. pool->count--;
  44. picref->video->w = w;
  45. picref->video->h = h;
  46. picref->perms = perms | AV_PERM_READ;
  47. picref->format = link->format;
  48. pic->refcount = 1;
  49. memcpy(picref->data, pic->data, sizeof(picref->data));
  50. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  51. pool->refcount++;
  52. return picref;
  53. }
  54. }
  55. } else {
  56. pool = link->pool = av_mallocz(sizeof(AVFilterPool));
  57. pool->refcount = 1;
  58. }
  59. // align: +2 is needed for swscaler, +16 to be SIMD-friendly
  60. if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
  61. return NULL;
  62. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  63. perms, w, h, link->format);
  64. if (!picref) {
  65. av_free(data[0]);
  66. return NULL;
  67. }
  68. memset(data[0], 128, i);
  69. picref->buf->priv = pool;
  70. picref->buf->free = NULL;
  71. pool->refcount++;
  72. return picref;
  73. }
  74. AVFilterBufferRef *
  75. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  76. int w, int h, enum PixelFormat format)
  77. {
  78. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  79. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  80. if (!pic || !picref)
  81. goto fail;
  82. picref->buf = pic;
  83. picref->buf->free = ff_avfilter_default_free_buffer;
  84. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  85. goto fail;
  86. pic->w = picref->video->w = w;
  87. pic->h = picref->video->h = h;
  88. /* make sure the buffer gets read permission or it's useless for output */
  89. picref->perms = perms | AV_PERM_READ;
  90. pic->refcount = 1;
  91. picref->type = AVMEDIA_TYPE_VIDEO;
  92. pic->format = picref->format = format;
  93. memcpy(pic->data, data, 4*sizeof(data[0]));
  94. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  95. memcpy(picref->data, pic->data, sizeof(picref->data));
  96. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  97. pic-> extended_data = pic->data;
  98. picref->extended_data = picref->data;
  99. picref->pts = AV_NOPTS_VALUE;
  100. return picref;
  101. fail:
  102. if (picref && picref->video)
  103. av_free(picref->video);
  104. av_free(picref);
  105. av_free(pic);
  106. return NULL;
  107. }
  108. AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  109. {
  110. AVFilterBufferRef *ret = NULL;
  111. av_unused char buf[16];
  112. FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
  113. ff_tlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  114. if (link->dstpad->get_video_buffer)
  115. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  116. if (!ret)
  117. ret = ff_default_get_video_buffer(link, perms, w, h);
  118. if (ret)
  119. ret->type = AVMEDIA_TYPE_VIDEO;
  120. FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
  121. return ret;
  122. }
  123. int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  124. {
  125. AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
  126. if (!buf_out)
  127. return AVERROR(ENOMEM);
  128. return ff_start_frame(link->dst->outputs[0], buf_out);
  129. }
  130. // for filters that support (but don't require) outpic==inpic
  131. int ff_inplace_start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  132. {
  133. AVFilterLink *outlink = inlink->dst->outputs[0];
  134. AVFilterBufferRef *outpicref = NULL, *for_next_filter;
  135. int ret = 0;
  136. if ((inpicref->perms & AV_PERM_WRITE) && !(inpicref->perms & AV_PERM_PRESERVE)) {
  137. outpicref = avfilter_ref_buffer(inpicref, ~0);
  138. if (!outpicref)
  139. return AVERROR(ENOMEM);
  140. } else {
  141. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  142. if (!outpicref)
  143. return AVERROR(ENOMEM);
  144. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  145. outpicref->video->w = outlink->w;
  146. outpicref->video->h = outlink->h;
  147. }
  148. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  149. if (for_next_filter)
  150. ret = ff_start_frame(outlink, for_next_filter);
  151. else
  152. ret = AVERROR(ENOMEM);
  153. if (ret < 0) {
  154. avfilter_unref_bufferp(&outpicref);
  155. return ret;
  156. }
  157. outlink->out_buf = outpicref;
  158. return 0;
  159. }
  160. static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  161. {
  162. AVFilterLink *outlink = NULL;
  163. if (inlink->dst->nb_outputs)
  164. outlink = inlink->dst->outputs[0];
  165. if (outlink) {
  166. AVFilterBufferRef *buf_out;
  167. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  168. if (!outlink->out_buf)
  169. return AVERROR(ENOMEM);
  170. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  171. outlink->out_buf->video->w = outlink->w;
  172. outlink->out_buf->video->h = outlink->h;
  173. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  174. if (!buf_out)
  175. return AVERROR(ENOMEM);
  176. return ff_start_frame(outlink, buf_out);
  177. }
  178. return 0;
  179. }
  180. static void clear_link(AVFilterLink *link)
  181. {
  182. avfilter_unref_bufferp(&link->cur_buf);
  183. avfilter_unref_bufferp(&link->src_buf);
  184. avfilter_unref_bufferp(&link->out_buf);
  185. }
  186. /* XXX: should we do the duplicating of the picture ref here, instead of
  187. * forcing the source filter to do it? */
  188. int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  189. {
  190. int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  191. AVFilterPad *dst = link->dstpad;
  192. int ret, perms = picref->perms;
  193. AVFilterCommand *cmd= link->dst->command_queue;
  194. int64_t pts;
  195. FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
  196. if (!(start_frame = dst->start_frame))
  197. start_frame = default_start_frame;
  198. if (picref->linesize[0] < 0)
  199. perms |= AV_PERM_NEG_LINESIZES;
  200. /* prepare to copy the picture if it has insufficient permissions */
  201. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  202. av_log(link->dst, AV_LOG_DEBUG,
  203. "frame copy needed (have perms %x, need %x, reject %x)\n",
  204. picref->perms,
  205. link->dstpad->min_perms, link->dstpad->rej_perms);
  206. link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
  207. if (!link->cur_buf) {
  208. avfilter_unref_bufferp(&picref);
  209. return AVERROR(ENOMEM);
  210. }
  211. link->src_buf = picref;
  212. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  213. /* copy palette if required */
  214. if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
  215. memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
  216. }
  217. else
  218. link->cur_buf = picref;
  219. while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
  220. av_log(link->dst, AV_LOG_DEBUG,
  221. "Processing command time:%f command:%s arg:%s\n",
  222. cmd->time, cmd->command, cmd->arg);
  223. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  224. ff_command_queue_pop(link->dst);
  225. cmd= link->dst->command_queue;
  226. }
  227. pts = link->cur_buf->pts;
  228. ret = start_frame(link, link->cur_buf);
  229. ff_update_link_current_pts(link, pts);
  230. if (ret < 0)
  231. clear_link(link);
  232. return ret;
  233. }
  234. int ff_null_start_frame_keep_ref(AVFilterLink *inlink,
  235. AVFilterBufferRef *picref)
  236. {
  237. return ff_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
  238. }
  239. int ff_null_end_frame(AVFilterLink *link)
  240. {
  241. return ff_end_frame(link->dst->outputs[0]);
  242. }
  243. static int default_end_frame(AVFilterLink *inlink)
  244. {
  245. AVFilterLink *outlink = NULL;
  246. if (inlink->dst->nb_outputs)
  247. outlink = inlink->dst->outputs[0];
  248. if (outlink) {
  249. return ff_end_frame(outlink);
  250. }
  251. return 0;
  252. }
  253. int ff_end_frame(AVFilterLink *link)
  254. {
  255. int (*end_frame)(AVFilterLink *);
  256. int ret;
  257. if (!(end_frame = link->dstpad->end_frame))
  258. end_frame = default_end_frame;
  259. ret = end_frame(link);
  260. clear_link(link);
  261. return ret;
  262. }
  263. int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  264. {
  265. return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  266. }
  267. static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  268. {
  269. AVFilterLink *outlink = NULL;
  270. if (inlink->dst->nb_outputs)
  271. outlink = inlink->dst->outputs[0];
  272. if (outlink)
  273. return ff_draw_slice(outlink, y, h, slice_dir);
  274. return 0;
  275. }
  276. int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  277. {
  278. uint8_t *src[4], *dst[4];
  279. int i, j, vsub, ret;
  280. int (*draw_slice)(AVFilterLink *, int, int, int);
  281. FF_TPRINTF_START(NULL, draw_slice); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
  282. /* copy the slice if needed for permission reasons */
  283. if (link->src_buf) {
  284. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  285. for (i = 0; i < 4; i++) {
  286. if (link->src_buf->data[i]) {
  287. src[i] = link->src_buf-> data[i] +
  288. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  289. dst[i] = link->cur_buf->data[i] +
  290. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  291. } else
  292. src[i] = dst[i] = NULL;
  293. }
  294. for (i = 0; i < 4; i++) {
  295. int planew =
  296. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  297. if (!src[i]) continue;
  298. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  299. memcpy(dst[i], src[i], planew);
  300. src[i] += link->src_buf->linesize[i];
  301. dst[i] += link->cur_buf->linesize[i];
  302. }
  303. }
  304. }
  305. if (!(draw_slice = link->dstpad->draw_slice))
  306. draw_slice = default_draw_slice;
  307. ret = draw_slice(link, y, h, slice_dir);
  308. if (ret < 0)
  309. clear_link(link);
  310. return ret;
  311. }
  312. int avfilter_default_end_frame(AVFilterLink *inlink)
  313. {
  314. return default_end_frame(inlink);
  315. }