video.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 AVPixelFormat 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. }