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.

409 lines
13KB

  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 <string.h>
  23. #include <stdio.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/mem.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  31. {
  32. return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
  33. }
  34. AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  35. {
  36. int linesize[4];
  37. uint8_t *data[4];
  38. int i;
  39. AVFilterBufferRef *picref = NULL;
  40. AVFilterPool *pool = link->pool;
  41. int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
  42. AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
  43. av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
  44. if (pool) {
  45. for (i = 0; i < POOL_SIZE; i++) {
  46. picref = pool->pic[i];
  47. if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
  48. AVFilterBuffer *pic = picref->buf;
  49. pool->pic[i] = NULL;
  50. pool->count--;
  51. picref->video->w = w;
  52. picref->video->h = h;
  53. picref->perms = full_perms;
  54. picref->format = link->format;
  55. pic->refcount = 1;
  56. memcpy(picref->data, pic->data, sizeof(picref->data));
  57. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  58. pool->refcount++;
  59. return picref;
  60. }
  61. }
  62. } else {
  63. pool = link->pool = av_mallocz(sizeof(AVFilterPool));
  64. pool->refcount = 1;
  65. }
  66. // align: +2 is needed for swscaler, +16 to be SIMD-friendly
  67. if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
  68. return NULL;
  69. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  70. full_perms, w, h, link->format);
  71. if (!picref) {
  72. av_free(data[0]);
  73. return NULL;
  74. }
  75. memset(data[0], 128, i);
  76. picref->buf->priv = pool;
  77. picref->buf->free = NULL;
  78. pool->refcount++;
  79. return picref;
  80. }
  81. AVFilterBufferRef *
  82. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  83. int w, int h, enum PixelFormat format)
  84. {
  85. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  86. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  87. if (!pic || !picref)
  88. goto fail;
  89. picref->buf = pic;
  90. picref->buf->free = ff_avfilter_default_free_buffer;
  91. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  92. goto fail;
  93. pic->w = picref->video->w = w;
  94. pic->h = picref->video->h = h;
  95. /* make sure the buffer gets read permission or it's useless for output */
  96. picref->perms = perms | AV_PERM_READ;
  97. pic->refcount = 1;
  98. picref->type = AVMEDIA_TYPE_VIDEO;
  99. pic->format = picref->format = format;
  100. memcpy(pic->data, data, 4*sizeof(data[0]));
  101. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  102. memcpy(picref->data, pic->data, sizeof(picref->data));
  103. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  104. pic-> extended_data = pic->data;
  105. picref->extended_data = picref->data;
  106. picref->pts = AV_NOPTS_VALUE;
  107. return picref;
  108. fail:
  109. if (picref && picref->video)
  110. av_free(picref->video);
  111. av_free(picref);
  112. av_free(pic);
  113. return NULL;
  114. }
  115. AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  116. {
  117. AVFilterBufferRef *ret = NULL;
  118. av_unused char buf[16];
  119. FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
  120. ff_tlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  121. if (link->dstpad->get_video_buffer)
  122. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  123. if (!ret)
  124. ret = ff_default_get_video_buffer(link, perms, w, h);
  125. if (ret)
  126. ret->type = AVMEDIA_TYPE_VIDEO;
  127. FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
  128. return ret;
  129. }
  130. int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  131. {
  132. AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
  133. if (!buf_out)
  134. return AVERROR(ENOMEM);
  135. return ff_start_frame(link->dst->outputs[0], buf_out);
  136. }
  137. // for filters that support (but don't require) outpic==inpic
  138. int ff_inplace_start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  139. {
  140. AVFilterLink *outlink = inlink->dst->outputs[0];
  141. AVFilterBufferRef *outpicref = NULL, *for_next_filter;
  142. int ret = 0;
  143. if (inpicref->perms & AV_PERM_WRITE) {
  144. outpicref = avfilter_ref_buffer(inpicref, ~0);
  145. if (!outpicref)
  146. return AVERROR(ENOMEM);
  147. } else {
  148. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  149. if (!outpicref)
  150. return AVERROR(ENOMEM);
  151. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  152. outpicref->video->w = outlink->w;
  153. outpicref->video->h = outlink->h;
  154. }
  155. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  156. if (for_next_filter)
  157. ret = ff_start_frame(outlink, for_next_filter);
  158. else
  159. ret = AVERROR(ENOMEM);
  160. if (ret < 0) {
  161. avfilter_unref_bufferp(&outpicref);
  162. return ret;
  163. }
  164. outlink->out_buf = outpicref;
  165. return 0;
  166. }
  167. static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  168. {
  169. AVFilterLink *outlink = NULL;
  170. if (inlink->dst->nb_outputs)
  171. outlink = inlink->dst->outputs[0];
  172. if (outlink) {
  173. AVFilterBufferRef *buf_out;
  174. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  175. if (!outlink->out_buf)
  176. return AVERROR(ENOMEM);
  177. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  178. outlink->out_buf->video->w = outlink->w;
  179. outlink->out_buf->video->h = outlink->h;
  180. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  181. if (!buf_out)
  182. return AVERROR(ENOMEM);
  183. return ff_start_frame(outlink, buf_out);
  184. }
  185. return 0;
  186. }
  187. static void clear_link(AVFilterLink *link)
  188. {
  189. avfilter_unref_bufferp(&link->cur_buf);
  190. avfilter_unref_bufferp(&link->src_buf);
  191. avfilter_unref_bufferp(&link->out_buf);
  192. link->cur_buf_copy = NULL; /* we do not own the reference */
  193. }
  194. /* XXX: should we do the duplicating of the picture ref here, instead of
  195. * forcing the source filter to do it? */
  196. int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  197. {
  198. int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  199. AVFilterPad *src = link->srcpad;
  200. AVFilterPad *dst = link->dstpad;
  201. int ret, perms;
  202. AVFilterCommand *cmd= link->dst->command_queue;
  203. int64_t pts;
  204. FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
  205. if (!(start_frame = dst->start_frame))
  206. start_frame = default_start_frame;
  207. av_assert1((picref->perms & src->min_perms) == src->min_perms);
  208. picref->perms &= ~ src->rej_perms;
  209. perms = picref->perms;
  210. if (picref->linesize[0] < 0)
  211. perms |= AV_PERM_NEG_LINESIZES;
  212. /* prepare to copy the picture if it has insufficient permissions */
  213. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  214. av_log(link->dst, AV_LOG_DEBUG,
  215. "frame copy needed (have perms %x, need %x, reject %x)\n",
  216. picref->perms,
  217. link->dstpad->min_perms, link->dstpad->rej_perms);
  218. link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
  219. if (!link->cur_buf) {
  220. avfilter_unref_bufferp(&picref);
  221. return AVERROR(ENOMEM);
  222. }
  223. link->src_buf = picref;
  224. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  225. /* copy palette if required */
  226. if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
  227. memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
  228. }
  229. else
  230. link->cur_buf = picref;
  231. link->cur_buf_copy = link->cur_buf;
  232. while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
  233. av_log(link->dst, AV_LOG_DEBUG,
  234. "Processing command time:%f command:%s arg:%s\n",
  235. cmd->time, cmd->command, cmd->arg);
  236. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  237. ff_command_queue_pop(link->dst);
  238. cmd= link->dst->command_queue;
  239. }
  240. pts = link->cur_buf->pts;
  241. ret = start_frame(link, link->cur_buf);
  242. ff_update_link_current_pts(link, pts);
  243. if (ret < 0)
  244. clear_link(link);
  245. else
  246. /* incoming buffers must not be freed in start frame,
  247. because they can still be in use by the automatic copy mechanism */
  248. av_assert1(link->cur_buf_copy->buf->refcount > 0);
  249. return ret;
  250. }
  251. int ff_null_end_frame(AVFilterLink *link)
  252. {
  253. return ff_end_frame(link->dst->outputs[0]);
  254. }
  255. static int default_end_frame(AVFilterLink *inlink)
  256. {
  257. AVFilterLink *outlink = NULL;
  258. if (inlink->dst->nb_outputs)
  259. outlink = inlink->dst->outputs[0];
  260. if (outlink) {
  261. return ff_end_frame(outlink);
  262. }
  263. return 0;
  264. }
  265. int ff_end_frame(AVFilterLink *link)
  266. {
  267. int (*end_frame)(AVFilterLink *);
  268. int ret;
  269. if (!(end_frame = link->dstpad->end_frame))
  270. end_frame = default_end_frame;
  271. ret = end_frame(link);
  272. clear_link(link);
  273. return ret;
  274. }
  275. int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  276. {
  277. return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  278. }
  279. static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  280. {
  281. AVFilterLink *outlink = NULL;
  282. if (inlink->dst->nb_outputs)
  283. outlink = inlink->dst->outputs[0];
  284. if (outlink)
  285. return ff_draw_slice(outlink, y, h, slice_dir);
  286. return 0;
  287. }
  288. int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  289. {
  290. uint8_t *src[4], *dst[4];
  291. int i, j, vsub, ret;
  292. int (*draw_slice)(AVFilterLink *, int, int, int);
  293. 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);
  294. /* copy the slice if needed for permission reasons */
  295. if (link->src_buf) {
  296. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  297. for (i = 0; i < 4; i++) {
  298. if (link->src_buf->data[i]) {
  299. src[i] = link->src_buf-> data[i] +
  300. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  301. dst[i] = link->cur_buf_copy->data[i] +
  302. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf_copy->linesize[i];
  303. } else
  304. src[i] = dst[i] = NULL;
  305. }
  306. for (i = 0; i < 4; i++) {
  307. int planew =
  308. av_image_get_linesize(link->format, link->cur_buf_copy->video->w, i);
  309. if (!src[i]) continue;
  310. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  311. memcpy(dst[i], src[i], planew);
  312. src[i] += link->src_buf->linesize[i];
  313. dst[i] += link->cur_buf_copy->linesize[i];
  314. }
  315. }
  316. }
  317. if (!(draw_slice = link->dstpad->draw_slice))
  318. draw_slice = default_draw_slice;
  319. ret = draw_slice(link, y, h, slice_dir);
  320. if (ret < 0)
  321. clear_link(link);
  322. else
  323. /* incoming buffers must not be freed in start frame,
  324. because they can still be in use by the automatic copy mechanism */
  325. av_assert1(link->cur_buf_copy->buf->refcount > 0);
  326. return ret;
  327. }
  328. int avfilter_default_end_frame(AVFilterLink *inlink)
  329. {
  330. return default_end_frame(inlink);
  331. }