src_buffer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2008 Vitor Sessak
  3. * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
  4. * Copyright (c) 2011 Mina Nagy Zaki
  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. /**
  23. * @file
  24. * memory buffer source filter
  25. */
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "audio.h"
  29. #include "avcodec.h"
  30. #include "buffersrc.h"
  31. #include "vsrc_buffer.h"
  32. #include "asrc_abuffer.h"
  33. #include "libavutil/audioconvert.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/fifo.h"
  36. #include "libavutil/imgutils.h"
  37. typedef struct {
  38. AVFifoBuffer *fifo;
  39. AVRational time_base; ///< time_base to set in the output link
  40. int eof;
  41. unsigned nb_failed_requests;
  42. /* Video only */
  43. AVFilterContext *scale;
  44. int h, w;
  45. enum PixelFormat pix_fmt;
  46. AVRational sample_aspect_ratio;
  47. char sws_param[256];
  48. /* Audio only */
  49. // Audio format of incoming buffers
  50. int sample_rate;
  51. unsigned int sample_format;
  52. int64_t channel_layout;
  53. // Normalization filters
  54. AVFilterContext *aconvert;
  55. AVFilterContext *aresample;
  56. } BufferSourceContext;
  57. static void buf_free(AVFilterBuffer *ptr)
  58. {
  59. av_free(ptr);
  60. return;
  61. }
  62. int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
  63. AVFilterBufferRef *picref, int flags)
  64. {
  65. return av_buffersrc_add_ref(buffer_filter, picref, 0);
  66. }
  67. #if CONFIG_AVCODEC
  68. #include "avcodec.h"
  69. int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
  70. const AVFrame *frame, int flags)
  71. {
  72. return av_buffersrc_add_frame(buffer_src, frame, 0);
  73. }
  74. #endif
  75. unsigned av_vsrc_buffer_get_nb_failed_requests(AVFilterContext *buffer_src)
  76. {
  77. return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
  78. }
  79. int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
  80. AVFilterBufferRef *samplesref,
  81. int av_unused flags)
  82. {
  83. return av_buffersrc_add_ref(ctx, samplesref, AV_BUFFERSRC_FLAG_NO_COPY);
  84. }
  85. int av_asrc_buffer_add_samples(AVFilterContext *ctx,
  86. uint8_t *data[8], int linesize[8],
  87. int nb_samples, int sample_rate,
  88. int sample_fmt, int64_t channel_layout, int planar,
  89. int64_t pts, int av_unused flags)
  90. {
  91. AVFilterBufferRef *samplesref;
  92. samplesref = avfilter_get_audio_buffer_ref_from_arrays(
  93. data, linesize[0], AV_PERM_WRITE,
  94. nb_samples,
  95. sample_fmt, channel_layout);
  96. if (!samplesref)
  97. return AVERROR(ENOMEM);
  98. samplesref->buf->free = buf_free;
  99. samplesref->pts = pts;
  100. samplesref->audio->sample_rate = sample_rate;
  101. AV_NOWARN_DEPRECATED(
  102. return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
  103. )
  104. }
  105. int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
  106. uint8_t *buf, int buf_size, int sample_rate,
  107. int sample_fmt, int64_t channel_layout, int planar,
  108. int64_t pts, int av_unused flags)
  109. {
  110. uint8_t *data[8] = {0};
  111. int linesize[8];
  112. int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
  113. nb_samples = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
  114. av_samples_fill_arrays(data, linesize,
  115. buf, nb_channels, nb_samples,
  116. sample_fmt, 16);
  117. AV_NOWARN_DEPRECATED(
  118. return av_asrc_buffer_add_samples(ctx,
  119. data, linesize, nb_samples,
  120. sample_rate,
  121. sample_fmt, channel_layout, planar,
  122. pts, flags);
  123. )
  124. }