src_buffer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "asrc_abuffer.h"
  32. #include "libavutil/audioconvert.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/fifo.h"
  35. #include "libavutil/imgutils.h"
  36. typedef struct {
  37. AVFifoBuffer *fifo;
  38. AVRational time_base; ///< time_base to set in the output link
  39. int eof;
  40. unsigned nb_failed_requests;
  41. /* Video only */
  42. AVFilterContext *scale;
  43. int h, w;
  44. enum PixelFormat pix_fmt;
  45. AVRational sample_aspect_ratio;
  46. char sws_param[256];
  47. /* Audio only */
  48. // Audio format of incoming buffers
  49. int sample_rate;
  50. unsigned int sample_format;
  51. int64_t channel_layout;
  52. // Normalization filters
  53. AVFilterContext *aconvert;
  54. AVFilterContext *aresample;
  55. } BufferSourceContext;
  56. static void buf_free(AVFilterBuffer *ptr)
  57. {
  58. av_free(ptr);
  59. return;
  60. }
  61. int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
  62. AVFilterBufferRef *samplesref,
  63. int av_unused flags)
  64. {
  65. return av_buffersrc_add_ref(ctx, samplesref, AV_BUFFERSRC_FLAG_NO_COPY);
  66. }
  67. int av_asrc_buffer_add_samples(AVFilterContext *ctx,
  68. uint8_t *data[8], int linesize[8],
  69. int nb_samples, int sample_rate,
  70. int sample_fmt, int64_t channel_layout, int planar,
  71. int64_t pts, int av_unused flags)
  72. {
  73. AVFilterBufferRef *samplesref;
  74. samplesref = avfilter_get_audio_buffer_ref_from_arrays(
  75. data, linesize[0], AV_PERM_WRITE,
  76. nb_samples,
  77. sample_fmt, channel_layout);
  78. if (!samplesref)
  79. return AVERROR(ENOMEM);
  80. samplesref->buf->free = buf_free;
  81. samplesref->pts = pts;
  82. samplesref->audio->sample_rate = sample_rate;
  83. AV_NOWARN_DEPRECATED(
  84. return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
  85. )
  86. }
  87. int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
  88. uint8_t *buf, int buf_size, int sample_rate,
  89. int sample_fmt, int64_t channel_layout, int planar,
  90. int64_t pts, int av_unused flags)
  91. {
  92. uint8_t *data[8] = {0};
  93. int linesize[8];
  94. int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
  95. nb_samples = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
  96. av_samples_fill_arrays(data, linesize,
  97. buf, nb_channels, nb_samples,
  98. sample_fmt, 16);
  99. AV_NOWARN_DEPRECATED(
  100. return av_asrc_buffer_add_samples(ctx,
  101. data, linesize, nb_samples,
  102. sample_rate,
  103. sample_fmt, channel_layout, planar,
  104. pts, flags);
  105. )
  106. }