video.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/buffer.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/mem.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. AVFrame *ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
  32. {
  33. return ff_get_video_buffer(link->dst->outputs[0], w, h);
  34. }
  35. /* TODO: set the buffer's priv member to a context structure for the whole
  36. * filter chain. This will allow for a buffer pool instead of the constant
  37. * alloc & free cycle currently implemented. */
  38. AVFrame *ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
  39. {
  40. AVFrame *frame = av_frame_alloc();
  41. int ret;
  42. if (!frame)
  43. return NULL;
  44. frame->width = w;
  45. frame->height = h;
  46. frame->format = link->format;
  47. ret = av_frame_get_buffer(frame, 32);
  48. if (ret < 0)
  49. av_frame_free(&frame);
  50. return frame;
  51. }
  52. #if FF_API_AVFILTERBUFFER
  53. AVFilterBufferRef *
  54. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  55. int w, int h, enum AVPixelFormat format)
  56. {
  57. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  58. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  59. if (!pic || !picref)
  60. goto fail;
  61. picref->buf = pic;
  62. picref->buf->free = ff_avfilter_default_free_buffer;
  63. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  64. goto fail;
  65. pic->w = picref->video->w = w;
  66. pic->h = picref->video->h = h;
  67. /* make sure the buffer gets read permission or it's useless for output */
  68. picref->perms = perms | AV_PERM_READ;
  69. pic->refcount = 1;
  70. picref->type = AVMEDIA_TYPE_VIDEO;
  71. pic->format = picref->format = format;
  72. memcpy(pic->data, data, 4*sizeof(data[0]));
  73. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  74. memcpy(picref->data, pic->data, sizeof(picref->data));
  75. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  76. pic-> extended_data = pic->data;
  77. picref->extended_data = picref->data;
  78. picref->pts = AV_NOPTS_VALUE;
  79. return picref;
  80. fail:
  81. if (picref && picref->video)
  82. av_free(picref->video);
  83. av_free(picref);
  84. av_free(pic);
  85. return NULL;
  86. }
  87. #endif
  88. AVFrame *ff_get_video_buffer(AVFilterLink *link, int w, int h)
  89. {
  90. AVFrame *ret = NULL;
  91. FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
  92. if (link->dstpad->get_video_buffer)
  93. ret = link->dstpad->get_video_buffer(link, w, h);
  94. if (!ret)
  95. ret = ff_default_get_video_buffer(link, w, h);
  96. return ret;
  97. }