video.c 13 KB

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