]> git.sesse.net Git - casparcg/commitdiff
ffmpeg: Fixed broken build.
authorRobert Nagy <ronag@live.com>
Tue, 17 Dec 2013 18:08:40 +0000 (19:08 +0100)
committerRobert Nagy <ronag@live.com>
Tue, 17 Dec 2013 18:08:40 +0000 (19:08 +0100)
modules/ffmpeg/producer/audio/audio_resampler.cpp [new file with mode: 0644]
modules/ffmpeg/producer/audio/audio_resampler.h [new file with mode: 0644]

diff --git a/modules/ffmpeg/producer/audio/audio_resampler.cpp b/modules/ffmpeg/producer/audio/audio_resampler.cpp
new file mode 100644 (file)
index 0000000..6bbeec0
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+* Copyright 2013 Sveriges Television AB http://casparcg.com/
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../StdAfx.h"
+
+#include "audio_resampler.h"
+
+#include <common/exception/exceptions.h>
+
+#if defined(_MSC_VER)
+#pragma warning (push)
+#pragma warning (disable : 4244)
+#endif
+extern "C" 
+{
+       #include <libavcodec/avcodec.h>
+}
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+
+namespace caspar { namespace ffmpeg {
+
+struct audio_resampler::implementation
+{      
+       std::shared_ptr<ReSampleContext> resampler_;
+       
+       std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> copy_buffer_;
+       std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> buffer2_;
+
+       const size_t                    output_channels_;
+       const AVSampleFormat    output_sample_format_;
+
+       const size_t                    input_channels_;
+       const AVSampleFormat    input_sample_format_;
+
+       implementation(size_t output_channels, size_t input_channels, size_t output_sample_rate, size_t input_sample_rate, AVSampleFormat output_sample_format, AVSampleFormat input_sample_format)
+               : output_channels_(output_channels)
+               , output_sample_format_(output_sample_format)
+               , input_channels_(input_channels)
+               , input_sample_format_(input_sample_format)
+       {
+               if(input_channels               != output_channels || 
+                  input_sample_rate    != output_sample_rate ||
+                  input_sample_format  != output_sample_format)
+               {       
+                       auto resampler = av_audio_resample_init(output_channels,                input_channels,
+                                                                                                       output_sample_rate,             input_sample_rate,
+                                                                                                       output_sample_format,   input_sample_format,
+                                                                                                       16, 10, 0, 0.8);
+
+                       buffer2_.resize(480000*2);
+
+                       char sample_fmt_string[200];
+                       av_get_sample_fmt_string(sample_fmt_string, 200, input_sample_format);
+
+                       CASPAR_LOG(warning) << L"[audio-resampler]"             
+                                                               << L" sample-rate: "    << input_sample_rate 
+                                                               << L" channels: "               << input_channels 
+                                                               << L" sample-fmt: "             << widen(sample_fmt_string);
+
+                       if(resampler)
+                               resampler_.reset(resampler, audio_resample_close);
+                       else
+                               BOOST_THROW_EXCEPTION(caspar_exception());
+               }               
+       }
+
+       std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> resample(std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>>&& data)
+       {
+               if(resampler_ && !data.empty())
+               {
+                       buffer2_.resize(480000*2);
+                       auto ret = audio_resample(resampler_.get(),
+                                                                         reinterpret_cast<short*>(buffer2_.data()), 
+                                                                         reinterpret_cast<short*>(data.data()), 
+                                                                         data.size() / (av_get_bytes_per_sample(input_sample_format_) * input_channels_)); 
+                       buffer2_.resize(ret * av_get_bytes_per_sample(output_sample_format_) * output_channels_);
+                       std::swap(data, buffer2_);
+               }
+
+               return std::move(data);
+       }
+};
+
+
+audio_resampler::audio_resampler(size_t output_channels, size_t input_channels, size_t output_sample_rate, size_t input_sample_rate, AVSampleFormat output_sample_format, AVSampleFormat input_sample_format)
+                               : impl_(new implementation(output_channels, input_channels, output_sample_rate, input_sample_rate, output_sample_format, input_sample_format)){}
+std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> audio_resampler::resample(std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>>&& data){return impl_->resample(std::move(data));}
+
+}}
\ No newline at end of file
diff --git a/modules/ffmpeg/producer/audio/audio_resampler.h b/modules/ffmpeg/producer/audio/audio_resampler.h
new file mode 100644 (file)
index 0000000..87fb4fd
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+* Copyright 2013 Sveriges Television AB http://casparcg.com/
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#pragma once
+
+#include <memory>
+
+#include <libavutil/samplefmt.h>
+
+namespace caspar { namespace ffmpeg {
+
+class audio_resampler
+{
+public:
+       audio_resampler(size_t                  output_channels,                size_t                  input_channels, 
+                                       size_t                  output_sample_rate,             size_t                  input_sample_rate, 
+                                       AVSampleFormat  output_sample_format,   AVSampleFormat  input_sample_format);
+       
+       std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> resample(std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>>&& data);
+private:
+       struct implementation;
+       std::shared_ptr<implementation> impl_;
+};
+
+}}
\ No newline at end of file