2009-07-16 16:48:47 +02:00
|
|
|
// Copyright (c) 2008-2009, Karl Blomster
|
2008-09-23 22:02:21 +02:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file audio_provider_ffmpegsource.cpp
|
|
|
|
/// @brief ffms2-based audio provider
|
2009-07-30 08:40:25 +02:00
|
|
|
/// @ingroup audio_input ffms
|
2009-07-29 07:43:02 +02:00
|
|
|
///
|
2008-09-23 22:02:21 +02:00
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#ifdef WITH_FFMS2
|
2014-07-09 16:22:49 +02:00
|
|
|
#include <libaegisub/audio/provider.h>
|
2012-01-08 02:33:39 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
#include "ffmpegsource_common.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2008-09-23 22:02:21 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
namespace {
|
2014-07-09 16:22:49 +02:00
|
|
|
class FFmpegSourceAudioProvider final : public agi::AudioProvider, FFmpegSourceProvider {
|
2014-03-24 03:30:03 +01:00
|
|
|
/// audio source object
|
|
|
|
agi::scoped_holder<FFMS_AudioSource*, void (FFMS_CC *)(FFMS_AudioSource*)> AudioSource;
|
|
|
|
|
|
|
|
mutable char FFMSErrMsg[1024]; ///< FFMS error message
|
|
|
|
mutable FFMS_ErrorInfo ErrInfo; ///< FFMS error codes/messages
|
|
|
|
|
|
|
|
void LoadAudio(agi::fs::path const& filename);
|
|
|
|
void FillBuffer(void *Buf, int64_t Start, int64_t Count) const override {
|
|
|
|
if (FFMS_GetAudio(AudioSource, Buf, Start, Count, &ErrInfo))
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioDecodeError(std::string("Failed to get audio samples: ") + ErrInfo.Buffer);
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-03-25 17:51:38 +01:00
|
|
|
FFmpegSourceAudioProvider(agi::fs::path const& filename, agi::BackgroundRunner *br);
|
2014-03-24 03:30:03 +01:00
|
|
|
|
|
|
|
bool NeedsCache() const override { return true; }
|
|
|
|
};
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Constructor
|
2012-02-20 19:22:12 +01:00
|
|
|
/// @param filename The filename to open
|
2014-03-25 17:51:38 +01:00
|
|
|
FFmpegSourceAudioProvider::FFmpegSourceAudioProvider(agi::fs::path const& filename, agi::BackgroundRunner *br) try
|
|
|
|
: FFmpegSourceProvider(br)
|
|
|
|
, AudioSource(nullptr, FFMS_DestroyAudioSource)
|
2010-08-02 08:32:01 +02:00
|
|
|
{
|
2009-09-26 23:58:00 +02:00
|
|
|
ErrInfo.Buffer = FFMSErrMsg;
|
|
|
|
ErrInfo.BufferSize = sizeof(FFMSErrMsg);
|
|
|
|
ErrInfo.ErrorType = FFMS_ERROR_SUCCESS;
|
|
|
|
ErrInfo.SubType = FFMS_ERROR_SUCCESS;
|
2009-07-24 03:41:16 +02:00
|
|
|
SetLogLevel();
|
|
|
|
|
2012-02-20 19:22:12 +01:00
|
|
|
LoadAudio(filename);
|
|
|
|
}
|
2014-06-11 00:28:45 +02:00
|
|
|
catch (agi::EnvironmentError const& err) {
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioProviderError(err.GetMessage());
|
2008-09-23 22:02:21 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void FFmpegSourceAudioProvider::LoadAudio(agi::fs::path const& filename) {
|
|
|
|
FFMS_Indexer *Indexer = FFMS_CreateIndexer(filename.string().c_str(), &ErrInfo);
|
2012-12-17 19:11:33 +01:00
|
|
|
if (!Indexer) {
|
|
|
|
if (ErrInfo.SubType == FFMS_ERROR_FILE_READ)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw agi::fs::FileNotFound(std::string(ErrInfo.Buffer));
|
2012-12-17 19:11:33 +01:00
|
|
|
else
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioDataNotFound(ErrInfo.Buffer);
|
2012-12-17 19:11:33 +01:00
|
|
|
}
|
2009-07-16 16:48:47 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::map<int, std::string> TrackList = GetTracksOfType(Indexer, FFMS_TYPE_AUDIO);
|
|
|
|
if (TrackList.empty())
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioDataNotFound("no audio tracks found");
|
2009-07-16 16:48:47 +02:00
|
|
|
|
|
|
|
// initialize the track number to an invalid value so we can detect later on
|
|
|
|
// whether the user actually had to choose a track or not
|
|
|
|
int TrackNumber = -1;
|
|
|
|
if (TrackList.size() > 1) {
|
|
|
|
TrackNumber = AskForTrackSelection(TrackList, FFMS_TYPE_AUDIO);
|
|
|
|
// if it's still -1 here, user pressed cancel
|
|
|
|
if (TrackNumber == -1)
|
2012-01-08 02:33:39 +01:00
|
|
|
throw agi::UserCancelException("audio loading canceled by user");
|
2009-07-16 16:48:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate a name for the cache file
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path CacheName = GetCacheFilename(filename);
|
2008-09-23 22:02:21 +02:00
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
// try to read index
|
2012-02-20 19:22:12 +01:00
|
|
|
agi::scoped_holder<FFMS_Index*, void (FFMS_CC*)(FFMS_Index*)>
|
2013-01-04 16:01:50 +01:00
|
|
|
Index(FFMS_ReadIndex(CacheName.string().c_str(), &ErrInfo), FFMS_DestroyIndex);
|
2012-02-20 19:22:12 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (Index && FFMS_IndexBelongsToFile(Index, filename.string().c_str(), &ErrInfo))
|
2012-11-13 17:51:01 +01:00
|
|
|
Index = nullptr;
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
// index valid but track number still not set?
|
2012-02-20 19:22:12 +01:00
|
|
|
if (Index) {
|
2009-07-16 16:48:47 +02:00
|
|
|
// track number not set? just grab the first track
|
|
|
|
if (TrackNumber < 0)
|
2009-09-26 23:58:00 +02:00
|
|
|
TrackNumber = FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, &ErrInfo);
|
2012-02-20 19:22:12 +01:00
|
|
|
if (TrackNumber < 0)
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioDataNotFound(std::string("Couldn't find any audio tracks: ") + ErrInfo.Buffer);
|
2008-09-24 01:30:27 +02:00
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
// index is valid and track number is now set,
|
|
|
|
// but do we have indexing info for the desired audio track?
|
2009-09-26 23:58:00 +02:00
|
|
|
FFMS_Track *TempTrackData = FFMS_GetTrackFromIndex(Index, TrackNumber);
|
2012-02-20 19:22:12 +01:00
|
|
|
if (FFMS_GetNumFrames(TempTrackData) <= 0)
|
2012-11-13 17:51:01 +01:00
|
|
|
Index = nullptr;
|
2008-09-24 01:30:27 +02:00
|
|
|
}
|
2009-11-21 23:08:03 +01:00
|
|
|
// no valid index exists and the file only has one audio track, index it
|
2012-03-25 06:05:06 +02:00
|
|
|
else if (TrackNumber < 0)
|
2009-07-16 23:40:17 +02:00
|
|
|
TrackNumber = FFMS_TRACKMASK_ALL;
|
2009-07-16 16:48:47 +02:00
|
|
|
// else: do nothing (keep track mask as it is)
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2012-06-07 23:03:11 +02:00
|
|
|
// reindex if the error handling mode has changed
|
|
|
|
FFMS_IndexErrorHandling ErrorHandling = GetErrorHandlingMode();
|
|
|
|
#if FFMS_VERSION >= ((2 << 24) | (17 << 16) | (2 << 8) | 0)
|
|
|
|
if (Index && FFMS_GetErrorHandling(Index) != ErrorHandling)
|
2012-11-13 17:51:01 +01:00
|
|
|
Index = nullptr;
|
2012-06-07 23:03:11 +02:00
|
|
|
#endif
|
|
|
|
|
2009-07-16 16:48:47 +02:00
|
|
|
// moment of truth
|
2012-02-20 19:22:12 +01:00
|
|
|
if (!Index) {
|
2009-11-21 23:08:03 +01:00
|
|
|
int TrackMask;
|
2010-05-21 03:13:36 +02:00
|
|
|
if (OPT_GET("Provider/FFmpegSource/Index All Tracks")->GetBool() || TrackNumber == FFMS_TRACKMASK_ALL)
|
2009-11-21 23:08:03 +01:00
|
|
|
TrackMask = FFMS_TRACKMASK_ALL;
|
|
|
|
else
|
|
|
|
TrackMask = (1 << TrackNumber);
|
|
|
|
|
2012-06-07 23:03:11 +02:00
|
|
|
Index = DoIndexing(Indexer, CacheName, TrackMask, ErrorHandling);
|
2009-11-21 23:08:03 +01:00
|
|
|
|
|
|
|
// if tracknumber still isn't set we need to set it now
|
|
|
|
if (TrackNumber == FFMS_TRACKMASK_ALL)
|
|
|
|
TrackNumber = FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, &ErrInfo);
|
2009-07-14 00:30:48 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else
|
2012-07-23 02:44:47 +02:00
|
|
|
FFMS_CancelIndexing(Indexer);
|
2008-09-23 22:02:21 +02:00
|
|
|
|
2009-05-03 20:05:30 +02:00
|
|
|
// update access time of index file so it won't get cleaned away
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::Touch(CacheName);
|
2009-05-03 20:05:30 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
AudioSource = FFMS_CreateAudioSource(filename.string().c_str(), TrackNumber, Index, -1, &ErrInfo);
|
2012-02-20 19:22:12 +01:00
|
|
|
if (!AudioSource)
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioProviderError(std::string("Failed to open audio track: ") + ErrInfo.Buffer);
|
2012-02-20 19:22:12 +01:00
|
|
|
|
2009-09-26 23:58:00 +02:00
|
|
|
const FFMS_AudioProperties AudioInfo = *FFMS_GetAudioProperties(AudioSource);
|
2008-09-23 22:02:21 +02:00
|
|
|
|
|
|
|
channels = AudioInfo.Channels;
|
|
|
|
sample_rate = AudioInfo.SampleRate;
|
2010-12-29 07:26:56 +01:00
|
|
|
num_samples = AudioInfo.NumSamples;
|
2014-04-22 21:34:20 +02:00
|
|
|
decoded_samples = AudioInfo.NumSamples;
|
2010-12-29 07:26:56 +01:00
|
|
|
if (channels <= 0 || sample_rate <= 0 || num_samples <= 0)
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioProviderError("sanity check failed, consult your local psychiatrist");
|
2008-09-23 22:02:21 +02:00
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
switch (AudioInfo.SampleFormat) {
|
|
|
|
case FFMS_FMT_U8: bytes_per_sample = 1; float_samples = false; break;
|
|
|
|
case FFMS_FMT_S16: bytes_per_sample = 2; float_samples = false; break;
|
|
|
|
case FFMS_FMT_S32: bytes_per_sample = 4; float_samples = false; break;
|
|
|
|
case FFMS_FMT_FLT: bytes_per_sample = 4; float_samples = true; break;
|
|
|
|
case FFMS_FMT_DBL: bytes_per_sample = 8; float_samples = true; break;
|
2008-09-23 22:02:21 +02:00
|
|
|
default:
|
2014-07-09 16:22:49 +02:00
|
|
|
throw agi::AudioProviderError("unknown or unsupported sample format");
|
2008-09-23 22:02:21 +02:00
|
|
|
}
|
2012-12-27 04:59:42 +01:00
|
|
|
|
|
|
|
#if FFMS_VERSION >= ((2 << 24) | (17 << 16) | (4 << 8) | 0)
|
|
|
|
if (channels > 1 || bytes_per_sample != 2) {
|
|
|
|
std::unique_ptr<FFMS_ResampleOptions, decltype(&FFMS_DestroyResampleOptions)>
|
|
|
|
opt(FFMS_CreateResampleOptions(AudioSource), FFMS_DestroyResampleOptions);
|
|
|
|
opt->ChannelLayout = FFMS_CH_FRONT_CENTER;
|
|
|
|
opt->SampleFormat = FFMS_FMT_S16;
|
|
|
|
|
|
|
|
// Might fail if FFMS2 wasn't built with libavresample
|
|
|
|
if (!FFMS_SetOutputFormatA(AudioSource, opt.get(), nullptr)) {
|
|
|
|
channels = 1;
|
|
|
|
bytes_per_sample = 2;
|
|
|
|
float_samples = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-09-23 22:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
std::unique_ptr<agi::AudioProvider> CreateFFmpegSourceAudioProvider(agi::fs::path const& file, agi::BackgroundRunner *br) {
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<FFmpegSourceAudioProvider>(file, br);
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#endif /* WITH_FFMS2 */
|