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.
|
2008-07-03 17:22:25 +02:00
|
|
|
*/
|
|
|
|
/* Update: this used to be commented out but is now needed on Windows.
|
|
|
|
* Not sure about Linux, so it's wrapped in an ifdef.
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
2007-03-31 23:28:43 +02:00
|
|
|
#define __STDC_CONSTANT_MACROS 1
|
|
|
|
#include <stdint.h>
|
2008-07-03 17:22:25 +02:00
|
|
|
#endif /* WIN32 */
|
|
|
|
/* - done in posix/defines.h
|
2007-03-31 23:28:43 +02:00
|
|
|
*/
|
|
|
|
|
2008-07-03 17:22:25 +02:00
|
|
|
|
2007-08-08 00:10:44 +02:00
|
|
|
extern "C" {
|
2008-07-02 18:31:06 +02:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/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
|
2008-03-08 05:27:41 +01:00
|
|
|
LAVCAudioProvider::LAVCAudioProvider(Aegisub::String _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);
|
2008-03-26 00:01:29 +01:00
|
|
|
filename = _filename.c_str();
|
2006-04-15 00:37:05 +02:00
|
|
|
#if 0
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
audStream = -1;
|
2008-03-24 12:30:35 +01:00
|
|
|
for (int i = 0; i < (int)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
|
|
|
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;
|
2008-03-21 20:52:14 +01:00
|
|
|
/* FIXME: this entire provider always assumes 16-bit audio. Currently that isn't a problem since
|
|
|
|
ffmpeg always converts everything to 16-bit, but in the future it might become one. */
|
|
|
|
bytes_per_sample = 2;
|
2006-04-15 01:45:28 +02:00
|
|
|
|
2008-03-21 20:52:14 +01:00
|
|
|
/* aegisub currently supports mono only, so always resample unless it's mono with the desired samplerate */
|
|
|
|
if ((sample_rate != codecContext->sample_rate) || (codecContext->channels > 1)) {
|
|
|
|
rsct = audio_resample_init(1, codecContext->channels, sample_rate, codecContext->sample_rate);
|
|
|
|
if (!rsct)
|
|
|
|
throw _T("Failed to initialize resampling");
|
2006-04-15 01:45:28 +02:00
|
|
|
|
2008-03-21 20:52:14 +01:00
|
|
|
resample_ratio = (float)sample_rate / (float)codecContext->sample_rate;
|
|
|
|
}
|
2008-05-21 22:03:39 +02:00
|
|
|
|
|
|
|
/* libavcodec seems to give back invalid stream length values for Matroska files.
|
|
|
|
* As a workaround, we can use the overall file length.
|
|
|
|
*/
|
|
|
|
double length;
|
|
|
|
if(stream->duration == AV_NOPTS_VALUE)
|
|
|
|
length = (double)lavcfile->fctx->duration / AV_TIME_BASE;
|
|
|
|
else
|
|
|
|
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;
|
2008-03-21 20:52:14 +01:00
|
|
|
int64_t samples_to_decode = num_samples - start; /* samples left to the end of the stream */
|
|
|
|
if (count < samples_to_decode) /* haven't reached the end yet, so just decode the requested number of samples */
|
|
|
|
samples_to_decode = count;
|
|
|
|
if (samples_to_decode < 0) /* requested beyond the end of the stream */
|
|
|
|
samples_to_decode = 0;
|
2006-04-15 00:37:05 +02:00
|
|
|
|
2008-03-21 20:52:14 +01:00
|
|
|
/* if we got asked for more samples than there are left in the stream, add zeros to the decoding buffer until
|
|
|
|
we have enough to fill the request */
|
|
|
|
memset(_buf + samples_to_decode, 0, (count - samples_to_decode) * 2);
|
2006-04-15 00:37:05 +02:00
|
|
|
|
|
|
|
AVPacket packet;
|
2008-03-21 20:52:14 +01:00
|
|
|
while (samples_to_decode > 0 && av_read_frame(lavcfile->fctx, &packet) >= 0) {
|
2008-03-21 21:30:34 +01:00
|
|
|
/* we're not dealing with video packets in this here provider */
|
|
|
|
if (packet.stream_index == audStream) {
|
|
|
|
int size = packet.size;
|
|
|
|
uint8_t *data = packet.data;
|
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
int temp_output_buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; /* see constructor, it malloc()'s buffer to this */
|
|
|
|
int retval, decoded_samples;
|
|
|
|
|
2008-03-21 21:35:59 +01:00
|
|
|
retval = avcodec_decode_audio2(codecContext, buffer, &temp_output_buffer_size, data, size);
|
2008-03-21 21:30:34 +01:00
|
|
|
if (retval <= 0)
|
|
|
|
throw _T("Failed to decode audio");
|
2008-03-22 23:18:43 +01:00
|
|
|
if (temp_output_buffer_size <= 0) /* sanity checking, shouldn't ever happen */
|
|
|
|
throw _T("Audio decoder lied about output size! This can't happen and you didn't see this error message. Move along.");
|
2008-03-21 21:30:34 +01:00
|
|
|
|
2008-03-22 23:18:43 +01:00
|
|
|
decoded_samples = temp_output_buffer_size / 2; /* 2 bytes per sample */
|
2008-03-21 21:30:34 +01:00
|
|
|
size -= retval;
|
|
|
|
data += retval;
|
|
|
|
|
|
|
|
/* do we need to resample? */
|
|
|
|
if (rsct) {
|
2008-03-24 12:30:35 +01:00
|
|
|
/* do the actual resampling */
|
2008-03-21 21:30:34 +01:00
|
|
|
decoded_samples = audio_resample(rsct, _buf, buffer, decoded_samples / codecContext->channels);
|
|
|
|
|
2008-03-22 23:45:46 +01:00
|
|
|
/* make some noise if we somehow ended up with more samples than we wanted (will cause audio skew) */
|
2008-03-22 23:48:59 +01:00
|
|
|
if (decoded_samples > samples_to_decode)
|
2008-03-23 13:36:16 +01:00
|
|
|
wxLogMessage(wxString::Format(_T("Warning: decoder output more samples than requested, audio skew highly likely! (Wanted %d, got %d)"), (int)samples_to_decode, decoded_samples));
|
2008-03-21 21:30:34 +01:00
|
|
|
} else {
|
2008-03-24 12:30:35 +01:00
|
|
|
/* no resampling needed, just copy to the buffer, but first make noise if we got an overflow */
|
2008-03-22 23:48:59 +01:00
|
|
|
if (decoded_samples > samples_to_decode)
|
2008-03-23 13:36:16 +01:00
|
|
|
wxLogMessage(wxString::Format(_T("Warning: decoder output more samples than requested, audio skew highly likely! (Wanted %d, got %d)"), (int)samples_to_decode, decoded_samples));
|
2008-03-21 21:30:34 +01:00
|
|
|
|
|
|
|
memcpy(_buf, buffer, temp_output_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
_buf += decoded_samples;
|
|
|
|
samples_to_decode -= decoded_samples;
|
2006-04-15 01:25:10 +02:00
|
|
|
}
|
2006-04-15 00:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free_packet(&packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-31 07:46:22 +01:00
|
|
|
#endif
|