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
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
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-09-05 14:55:45 +02:00
|
|
|
#include "audio_provider_lavc.h"
|
2007-01-23 05:42:08 +01:00
|
|
|
#include "mkv_wrap.h"
|
|
|
|
#include "lavc_file.h"
|
|
|
|
#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;
|
2008-07-04 14:04:10 +02:00
|
|
|
throw _T("ffmpeg audio provider: 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;
|
2008-07-04 14:04:10 +02:00
|
|
|
throw _T("ffmpeg audio provider: 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)
|
2008-07-04 14:04:10 +02:00
|
|
|
throw _T("ffmpeg audio provider: Failed to open audio decoder");
|
2006-04-15 00:37:05 +02:00
|
|
|
|
2006-04-15 01:45:28 +02:00
|
|
|
sample_rate = Options.AsInt(_T("Audio Sample Rate"));
|
2008-08-04 09:13:41 +02:00
|
|
|
if (!sample_rate) {
|
|
|
|
/* aegisub wants audio with sample rate higher than 32khz */
|
|
|
|
if (codecContext->sample_rate < 32000)
|
|
|
|
sample_rate = 48000;
|
|
|
|
else
|
|
|
|
sample_rate = codecContext->sample_rate;
|
|
|
|
}
|
2006-04-15 01:45:28 +02:00
|
|
|
|
2008-08-04 09:13:41 +02:00
|
|
|
/* we rely on the intermediate audio provider to do downmixing for us later if necessary */
|
2008-07-16 15:22:06 +02:00
|
|
|
channels = codecContext->channels;
|
2006-04-15 01:45:28 +02:00
|
|
|
|
2008-09-05 14:55:45 +02:00
|
|
|
/* TODO: test if anything but S16 actually works! */
|
2008-08-04 09:13:41 +02:00
|
|
|
switch (codecContext->sample_fmt) {
|
2008-09-05 14:55:45 +02:00
|
|
|
case SAMPLE_FMT_U8: bytes_per_sample = 1; break;
|
|
|
|
case SAMPLE_FMT_S16: bytes_per_sample = 2; break;
|
|
|
|
case SAMPLE_FMT_S32: bytes_per_sample = 4; break; /* downmixing provider doesn't support this, will definitely not work */
|
2008-08-04 09:13:41 +02:00
|
|
|
default:
|
2008-09-05 14:55:45 +02:00
|
|
|
throw _T("ffmpeg audio provider: Unknown or unsupported sample format");
|
2008-08-04 09:13:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initiate resampling if necessary */
|
2008-07-16 15:22:06 +02:00
|
|
|
if (sample_rate != codecContext->sample_rate) {
|
|
|
|
rsct = audio_resample_init(channels, channels, sample_rate, codecContext->sample_rate);
|
2008-03-21 20:52:14 +01:00
|
|
|
if (!rsct)
|
2008-07-04 14:04:10 +02:00
|
|
|
throw _T("ffmpeg audio provider: Failed to initialize resampling");
|
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);
|
2008-08-04 09:13:41 +02:00
|
|
|
num_samples = (int64_t)(length * sample_rate); /* number of samples per channel */
|
2006-04-15 00:37:05 +02:00
|
|
|
|
|
|
|
buffer = (int16_t *)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
|
|
|
|
if (!buffer)
|
2008-07-04 14:04:10 +02:00
|
|
|
throw _T("ffmpeg audio provider: Failed to allocate audio decoding buffer, out of memory?");
|
2008-07-04 02:20:14 +02:00
|
|
|
|
2008-08-15 01:49:11 +02:00
|
|
|
leftover_samples = 0;
|
|
|
|
last_output_sample = -1;
|
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-07-04 14:04:10 +02:00
|
|
|
|
2008-08-15 01:49:11 +02:00
|
|
|
/* this exception disabled for now */
|
|
|
|
/* if (last_output_sample != start-1)
|
|
|
|
throw _T("ffmpeg audio provider: nonlinear access attempted, try loading audio to RAM or HD cache"); */
|
|
|
|
|
|
|
|
last_output_sample += count;
|
|
|
|
|
2008-07-16 15:22:06 +02:00
|
|
|
int64_t samples_to_decode = (num_samples - start) * channels; /* samples left to the end of the stream */
|
2008-03-21 20:52:14 +01:00
|
|
|
if (count < samples_to_decode) /* haven't reached the end yet, so just decode the requested number of samples */
|
2008-07-16 15:22:06 +02:00
|
|
|
samples_to_decode = count * channels; /* times the number of channels */
|
2008-03-21 20:52:14 +01:00
|
|
|
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 */
|
2008-08-04 10:17:35 +02:00
|
|
|
memset(_buf + samples_to_decode, 0, ((count * channels) - samples_to_decode) * bytes_per_sample);
|
2008-07-04 14:04:10 +02:00
|
|
|
|
|
|
|
/* do we have leftover samples from last time we were called? */
|
2008-08-14 18:10:31 +02:00
|
|
|
/* FIXME: this assumes that requests are always linear! attempts at random access give bogus results! */
|
2008-07-04 14:04:10 +02:00
|
|
|
if (leftover_samples > 0) {
|
2008-08-14 18:10:31 +02:00
|
|
|
int length = (samples_to_decode > leftover_samples) ? leftover_samples : samples_to_decode;
|
|
|
|
samples_to_decode -= length;
|
|
|
|
leftover_samples -= length;
|
|
|
|
|
2008-07-04 14:04:10 +02:00
|
|
|
/* put them in the output buffer */
|
|
|
|
samples_to_decode -= leftover_samples;
|
2008-08-14 18:10:31 +02:00
|
|
|
while (length > 0) {
|
|
|
|
*(_buf++) = *(overshoot_buffer++);
|
|
|
|
length--;
|
2008-07-04 14:04:10 +02:00
|
|
|
}
|
|
|
|
}
|
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;
|
2008-08-04 09:13:41 +02:00
|
|
|
uint8_t *data = packet.data;
|
2008-03-21 21:30:34 +01:00
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
int temp_output_buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; /* see constructor, it malloc()'s buffer to this */
|
2008-07-04 14:04:10 +02:00
|
|
|
int retval, decoded_bytes, decoded_samples;
|
2008-03-21 21:30:34 +01:00
|
|
|
|
2008-08-04 09:13:41 +02:00
|
|
|
retval = avcodec_decode_audio2(codecContext, buffer, &temp_output_buffer_size, data, size);
|
2008-12-17 07:22:05 +01:00
|
|
|
/* decoding failed, skip this packet and hope next one doesn't fail too */
|
|
|
|
if (retval < 0)
|
|
|
|
break;
|
|
|
|
/* throw _T("ffmpeg audio provider: failed to decode audio"); */
|
|
|
|
|
|
|
|
size -= retval;
|
|
|
|
data += retval;
|
|
|
|
/* decoding succeeded but this audio frame is empty, continue to next frame */
|
|
|
|
if (temp_output_buffer_size <= 0)
|
2008-07-04 14:04:10 +02:00
|
|
|
continue;
|
2008-03-21 21:30:34 +01:00
|
|
|
|
2008-07-04 14:04:10 +02:00
|
|
|
decoded_bytes = temp_output_buffer_size;
|
2008-08-04 10:17:35 +02:00
|
|
|
decoded_samples = decoded_bytes / bytes_per_sample; /* FIXME: stop assuming everything is 16-bit! */
|
2008-03-21 21:30:34 +01:00
|
|
|
|
|
|
|
/* do we need to resample? */
|
|
|
|
if (rsct) {
|
2008-07-04 14:04:10 +02:00
|
|
|
/* allocate some memory to save the resampled data in */
|
|
|
|
int16_t *temp_output_buffer = (int16_t *)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
|
|
|
|
if (!temp_output_buffer)
|
|
|
|
throw _T("ffmpeg audio provider: Failed to allocate audio resampling buffer, out of memory?");
|
|
|
|
|
2008-03-24 12:30:35 +01:00
|
|
|
/* do the actual resampling */
|
2008-07-04 14:04:10 +02:00
|
|
|
decoded_samples = audio_resample(rsct, temp_output_buffer, buffer, decoded_samples / codecContext->channels);
|
2008-03-21 21:30:34 +01:00
|
|
|
|
2008-07-04 14:04:10 +02:00
|
|
|
/* did we end up with more samples than we were asked for? */
|
2008-07-04 02:20:14 +02:00
|
|
|
if (decoded_samples > samples_to_decode) {
|
2008-07-04 14:04:10 +02:00
|
|
|
/* in that case, count them */
|
|
|
|
leftover_samples = decoded_samples - samples_to_decode;
|
|
|
|
/* and put them aside for later */
|
2008-08-14 18:10:31 +02:00
|
|
|
memcpy(buffer, &temp_output_buffer[samples_to_decode+1], leftover_samples * bytes_per_sample);
|
|
|
|
overshoot_buffer = buffer;
|
2008-07-04 14:04:10 +02:00
|
|
|
/* output the other samples that didn't overflow */
|
2008-08-04 10:17:35 +02:00
|
|
|
memcpy(_buf, temp_output_buffer, samples_to_decode * bytes_per_sample);
|
2008-07-04 14:04:10 +02:00
|
|
|
_buf += samples_to_decode;
|
|
|
|
} else {
|
2008-08-04 10:17:35 +02:00
|
|
|
memcpy(_buf, temp_output_buffer, decoded_samples * bytes_per_sample);
|
|
|
|
|
2008-07-04 14:04:10 +02:00
|
|
|
_buf += decoded_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(temp_output_buffer);
|
|
|
|
} else { /* no resampling needed */
|
|
|
|
/* overflow? (as above) */
|
|
|
|
if (decoded_samples > samples_to_decode) {
|
|
|
|
/* count sheep^H^H^H^H^Hsamples */
|
|
|
|
leftover_samples = decoded_samples - samples_to_decode;
|
|
|
|
/* and put them aside for later (mm, lamb chops) */
|
2008-08-14 18:10:31 +02:00
|
|
|
overshoot_buffer = &buffer[samples_to_decode+1];
|
2008-07-04 14:04:10 +02:00
|
|
|
/* output the other samples that didn't overflow */
|
2008-08-04 10:17:35 +02:00
|
|
|
memcpy(_buf, buffer, samples_to_decode * bytes_per_sample);
|
|
|
|
|
2008-07-04 14:04:10 +02:00
|
|
|
_buf += samples_to_decode;
|
|
|
|
} else {
|
|
|
|
/* just do a straight copy to buffer */
|
|
|
|
memcpy(_buf, buffer, decoded_bytes);
|
2008-08-04 10:17:35 +02:00
|
|
|
|
2008-07-04 14:04:10 +02:00
|
|
|
_buf += decoded_samples;
|
2008-07-04 02:20:14 +02:00
|
|
|
}
|
2008-03-21 21:30:34 +01:00
|
|
|
}
|
|
|
|
|
2008-08-15 01:49:11 +02:00
|
|
|
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);
|
|
|
|
}
|
2008-07-04 14:04:10 +02:00
|
|
|
|
2006-04-15 00:37:05 +02:00
|
|
|
}
|
|
|
|
|
2007-12-31 07:46:22 +01:00
|
|
|
#endif
|