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.

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