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.

166 lines
5.5KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "libavutil/imgutils.h"
  21. #include "libavutil/mem.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include "video.h"
  25. #ifdef DEBUG
  26. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  27. {
  28. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  29. perms & AV_PERM_READ ? "r" : "",
  30. perms & AV_PERM_WRITE ? "w" : "",
  31. perms & AV_PERM_PRESERVE ? "p" : "",
  32. perms & AV_PERM_REUSE ? "u" : "",
  33. perms & AV_PERM_REUSE2 ? "U" : "",
  34. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  35. return buf;
  36. }
  37. #endif
  38. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  39. {
  40. av_unused char buf[16];
  41. av_dlog(ctx,
  42. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  43. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  44. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  45. ref->pts, ref->pos);
  46. if (ref->video) {
  47. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  48. ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
  49. ref->video->w, ref->video->h,
  50. !ref->video->interlaced ? 'P' : /* Progressive */
  51. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  52. ref->video->key_frame,
  53. av_get_picture_type_char(ref->video->pict_type));
  54. }
  55. if (ref->audio) {
  56. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
  57. ref->audio->channel_layout,
  58. ref->audio->nb_samples,
  59. ref->audio->sample_rate,
  60. ref->audio->planar);
  61. }
  62. av_dlog(ctx, "]%s", end ? "\n" : "");
  63. }
  64. AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  65. {
  66. return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
  67. }
  68. /* TODO: set the buffer's priv member to a context structure for the whole
  69. * filter chain. This will allow for a buffer pool instead of the constant
  70. * alloc & free cycle currently implemented. */
  71. AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  72. {
  73. int linesize[4];
  74. uint8_t *data[4];
  75. AVFilterBufferRef *picref = NULL;
  76. // +2 is needed for swscaler, +16 to be SIMD-friendly
  77. if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
  78. return NULL;
  79. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  80. perms, w, h, link->format);
  81. if (!picref) {
  82. av_free(data[0]);
  83. return NULL;
  84. }
  85. return picref;
  86. }
  87. AVFilterBufferRef *
  88. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  89. int w, int h, enum AVPixelFormat format)
  90. {
  91. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  92. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  93. if (!pic || !picref)
  94. goto fail;
  95. picref->buf = pic;
  96. picref->buf->free = ff_avfilter_default_free_buffer;
  97. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  98. goto fail;
  99. pic->w = picref->video->w = w;
  100. pic->h = picref->video->h = h;
  101. /* make sure the buffer gets read permission or it's useless for output */
  102. picref->perms = perms | AV_PERM_READ;
  103. pic->refcount = 1;
  104. picref->type = AVMEDIA_TYPE_VIDEO;
  105. pic->format = picref->format = format;
  106. memcpy(pic->data, data, 4*sizeof(data[0]));
  107. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  108. memcpy(picref->data, pic->data, sizeof(picref->data));
  109. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  110. pic-> extended_data = pic->data;
  111. picref->extended_data = picref->data;
  112. picref->pts = AV_NOPTS_VALUE;
  113. return picref;
  114. fail:
  115. if (picref && picref->video)
  116. av_free(picref->video);
  117. av_free(picref);
  118. av_free(pic);
  119. return NULL;
  120. }
  121. AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  122. {
  123. AVFilterBufferRef *ret = NULL;
  124. av_unused char buf[16];
  125. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  126. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  127. if (link->dstpad->get_video_buffer)
  128. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  129. if (!ret)
  130. ret = ff_default_get_video_buffer(link, perms, w, h);
  131. if (ret)
  132. ret->type = AVMEDIA_TYPE_VIDEO;
  133. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  134. return ret;
  135. }