2006-04-15 00:37:05 +02:00
|
|
|
// Copyright (c) 2005-2006, Rodrigo Braz Monteiro, Fredrik Mellbin
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AEGISUB
|
|
|
|
//
|
|
|
|
// Website: http://aegisub.cellosoft.com
|
|
|
|
// Contact: mailto:zeratul@cellosoft.com
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Headers
|
2007-12-31 07:46:22 +01:00
|
|
|
#ifdef WITH_FFMPEG
|
|
|
|
|
2007-04-22 02:23:21 +02:00
|
|
|
#ifdef WIN32
|
2007-01-23 05:42:08 +01:00
|
|
|
#define EMULATE_INTTYPES
|
2007-04-22 02:23:21 +02:00
|
|
|
#endif
|
2006-04-15 00:37:05 +02:00
|
|
|
#include <wx/wxprec.h>
|
2007-03-31 23:28:43 +02:00
|
|
|
|
|
|
|
/* avcodec.h uses INT64_C in a *single* place. This prolly breaks on Win32,
|
|
|
|
* but, well. Let's at least fix it for Linux.
|
|
|
|
*
|
|
|
|
#define __STDC_CONSTANT_MACROS 1
|
|
|
|
#include <stdint.h>
|
|
|
|
* - done in posix/defines.h
|
|
|
|
*/
|
|
|
|
|
2007-08-08 00:10:44 +02:00
|
|
|
extern "C" {
|
2007-01-23 05:42:08 +01:00
|
|
|
#include <ffmpeg/avcodec.h>
|
|
|
|
#include <ffmpeg/avformat.h>
|
2007-08-08 00:10:44 +02:00
|
|
|
}
|
2007-01-23 05:42:08 +01:00
|
|
|
#include "mkv_wrap.h"
|
|
|
|
#include "lavc_file.h"
|
2008-03-05 03:23:47 +01:00
|
|
|
#include "audio_provider_lavc.h"
|
2007-01-23 05:42:08 +01:00
|
|
|
#include "lavc_file.h"
|
2006-04-15 00:37:05 +02:00
|
|
|
#include "utils.h"
|
|
|
|
#include "options.h"
|
|
|
|
|
2007-01-23 05:42:08 +01:00
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Constructor
|
|
|
|
LAVCAudioProvider::LAVCAudioProvider(wxString _filename)
|
2006-04-15 00:37:05 +02:00
|
|
|
: lavcfile(NULL), codecContext(NULL), rsct(NULL), buffer(NULL)
|
|
|
|
{
|
2006-04-15 00:56:17 +02:00
|
|
|
try {
|
2006-04-15 00:37:05 +02:00
|
|
|
#if 0
|
|
|
|
/* since seeking currently is likely to be horribly broken with two
|
|
|
|
* providers accessing the same stream, this is disabled for now.
|
|
|
|
*/
|
|
|
|
LAVCVideoProvider *vpro_lavc = dynamic_cast<LAVCVideoProvider *>(vpro);
|
|
|
|
if (vpro_lavc) {
|
|
|
|
lavcfile = vpro->lavcfile->AddRef();
|
|
|
|
filename = vpro_lavc->GetFilename();
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
lavcfile = LAVCFile::Create(_filename);
|
|
|
|
filename = _filename;
|
|
|
|
#if 0
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
audStream = -1;
|
2007-04-04 22:42:44 +02:00
|
|
|
for (int i = 0; i < lavcfile->fctx->nb_streams; i++) {
|
2006-04-15 00:37:05 +02:00
|
|
|
codecContext = lavcfile->fctx->streams[i]->codec;
|
|
|
|
if (codecContext->codec_type == CODEC_TYPE_AUDIO) {
|
|
|
|
stream = lavcfile->fctx->streams[i];
|
|
|
|
audStream = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-03-29 23:05:22 +02:00
|
|
|
if (audStream == -1) {
|
|
|
|
codecContext = NULL;
|
2006-04-15 00:37:05 +02:00
|
|
|
throw _T("Could not find an audio stream");
|
2007-03-29 23:05:22 +02:00
|
|
|
}
|
2006-04-15 00:37:05 +02:00
|
|
|
AVCodec *codec = avcodec_find_decoder(codecContext->codec_id);
|
2007-03-29 23:05:22 +02:00
|
|
|
if (!codec) {
|
|
|
|
codecContext = NULL;
|
2006-04-15 00:37:05 +02:00
|
|
|
throw _T("Could not find a suitable audio decoder");
|
2007-03-29 23:05:22 +02:00
|
|
|
}
|
2006-04-15 00:37:05 +02:00
|
|
|
if (avcodec_open(codecContext, codec) < 0)
|
|
|
|
throw _T("Failed to open audio decoder");
|
|
|
|
|
2006-04-15 01:45:28 +02:00
|
|
|
/* aegisub currently supports mono only, so always resample */
|
|
|
|
|
|
|
|
sample_rate = Options.AsInt(_T("Audio Sample Rate"));
|
|
|
|
if (!sample_rate)
|
2006-04-15 00:37:05 +02:00
|
|
|
sample_rate = codecContext->sample_rate;
|
2006-04-15 01:45:28 +02:00
|
|
|
|
|
|
|
channels = 1;
|
|
|
|
bytes_per_sample = 2;
|
|
|
|
|
|
|
|
rsct = audio_resample_init(1, codecContext->channels, sample_rate, codecContext->sample_rate);
|
|
|
|
if (!rsct)
|
|
|
|
throw _T("Failed to initialize resampling");
|
|
|
|
|
|
|
|
resample_ratio = (float)sample_rate / (float)codecContext->sample_rate;
|
2006-04-15 11:57:36 +02:00
|
|
|
|
|
|
|
double length = (double)stream->duration * av_q2d(stream->time_base);
|
2007-08-31 16:11:35 +02:00
|
|
|
num_samples = (int64_t)(length * sample_rate);
|
2006-04-15 00:37:05 +02:00
|
|
|
|
|
|
|
buffer = (int16_t *)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
|
|
|
|
if (!buffer)
|
|
|
|
throw _T("Out of memory");
|
2006-04-15 00:56:17 +02:00
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
Destroy();
|
|
|
|
throw;
|
|
|
|
}
|
2006-04-15 00:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LAVCAudioProvider::~LAVCAudioProvider()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LAVCAudioProvider::Destroy()
|
|
|
|
{
|
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
|
|
|
if (rsct)
|
|
|
|
audio_resample_close(rsct);
|
|
|
|
if (codecContext)
|
|
|
|
avcodec_close(codecContext);
|
|
|
|
if (lavcfile)
|
|
|
|
lavcfile->Release();
|
|
|
|
}
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
void LAVCAudioProvider::GetAudio(void *buf, int64_t start, int64_t count)
|
2006-04-15 00:37:05 +02:00
|
|
|
{
|
|
|
|
int16_t *_buf = (int16_t *)buf;
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t _count = num_samples - start;
|
2006-04-15 00:37:05 +02:00
|
|
|
if (count < _count)
|
|
|
|
_count = count;
|
|
|
|
if (_count < 0)
|
|
|
|
_count = 0;
|
|
|
|
|
|
|
|
memset(_buf + _count, 0, (count - _count) << 1);
|
|
|
|
|
|
|
|
AVPacket packet;
|
|
|
|
while (_count > 0 && av_read_frame(lavcfile->fctx, &packet) >= 0) {
|
2006-04-15 01:59:24 +02:00
|
|
|
while (packet.stream_index == audStream) {
|
2006-04-15 00:37:05 +02:00
|
|
|
int bytesout = 0, samples;
|
|
|
|
if (avcodec_decode_audio(codecContext, buffer, &bytesout, packet.data, packet.size) < 0)
|
|
|
|
throw _T("Failed to decode audio");
|
2006-04-15 01:59:24 +02:00
|
|
|
if (bytesout == 0)
|
|
|
|
break;
|
|
|
|
|
2006-04-15 00:37:05 +02:00
|
|
|
samples = bytesout >> 1;
|
|
|
|
if (rsct) {
|
2007-08-31 16:11:35 +02:00
|
|
|
if ((int64_t)(samples * resample_ratio / codecContext->channels) > _count)
|
|
|
|
samples = (int64_t)(_count / resample_ratio * codecContext->channels);
|
2006-04-15 01:51:15 +02:00
|
|
|
samples = audio_resample(rsct, _buf, buffer, samples / codecContext->channels);
|
2006-04-15 00:37:05 +02:00
|
|
|
|
|
|
|
assert(samples <= _count);
|
2006-04-15 01:25:10 +02:00
|
|
|
} else {
|
2006-04-15 01:45:28 +02:00
|
|
|
/* currently dead code, rsct != NULL because we're resampling for mono */
|
2006-04-15 01:25:10 +02:00
|
|
|
if (samples > _count)
|
|
|
|
samples = _count;
|
2006-04-15 00:37:05 +02:00
|
|
|
memcpy(_buf, buffer, samples << 1);
|
2006-04-15 01:25:10 +02:00
|
|
|
}
|
2006-04-15 00:37:05 +02:00
|
|
|
|
|
|
|
_buf += samples;
|
|
|
|
_count -= samples;
|
2006-04-15 01:59:24 +02:00
|
|
|
break;
|
2006-04-15 00:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free_packet(&packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-31 07:46:22 +01:00
|
|
|
#endif
|