From 0103f449d0d6a811daea96114d47885ebfc5a426 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 19 Aug 2017 10:28:28 +0200 Subject: [PATCH] Pass stream_index to FFmpeg manually for flexibility --- src/coding/coding.h | 2 ++ src/coding/ffmpeg_decoder.c | 22 +++++++++++++--------- src/meta/bik.c | 3 ++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/coding/coding.h b/src/coding/coding.h index baf35d17..da848242 100644 --- a/src/coding/coding.h +++ b/src/coding/coding.h @@ -198,7 +198,9 @@ void free_at3plus(maiatrac3plus_codec_data *data); #ifdef VGM_USE_FFMPEG /* ffmpeg_decoder */ ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size); +ffmpeg_codec_data * init_ffmpeg_offset_index(STREAMFILE *streamFile, uint64_t start, uint64_t size, int stream_index); ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size); +ffmpeg_codec_data * init_ffmpeg_header_offset_index(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size, int stream_index); void decode_ffmpeg(VGMSTREAM *stream, sample * outbuf, int32_t samples_to_do, int channels); void reset_ffmpeg(VGMSTREAM *vgmstream); diff --git a/src/coding/ffmpeg_decoder.c b/src/coding/ffmpeg_decoder.c index 6838a3fc..ae31a4be 100644 --- a/src/coding/ffmpeg_decoder.c +++ b/src/coding/ffmpeg_decoder.c @@ -272,13 +272,16 @@ static int64_t ffmpeg_seek(void *opaque, int64_t offset, int whence) { /* MAIN INIT/DECODER */ /* ******************************************** */ -/** - * Manually init FFmpeg, from an offset. - * Used if the stream has internal data recognized by FFmpeg. - */ ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size) { - return init_ffmpeg_header_offset(streamFile, NULL, 0, start, size); + return init_ffmpeg_header_offset_index(streamFile, NULL,0, start,size, 0); } +ffmpeg_codec_data * init_ffmpeg_offset_index(STREAMFILE *streamFile, uint64_t start, uint64_t size, int stream_index) { + return init_ffmpeg_header_offset_index(streamFile, NULL,0, start,size, stream_index); +} +ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size) { + return init_ffmpeg_header_offset_index(streamFile, header,header_size, start,size, 0); +} + /** * Manually init FFmpeg, from a fake header / offset. @@ -286,8 +289,10 @@ ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, u * Takes a fake header, to trick FFmpeg into demuxing/decoding the stream. * This header will be seamlessly inserted before 'start' offset, and total filesize will be 'header_size' + 'size'. * The header buffer will be copied and memory-managed internally. + * NULL header can used given if the stream has internal data recognized by FFmpeg at offset. + * Stream index can be passed to FFmpeg, if the format has multiple streams (1=first). */ -ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size) { +ffmpeg_codec_data * init_ffmpeg_header_offset_index(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size, int stream_index) { char filename[PATH_LIMIT]; ffmpeg_codec_data * data; int errcode, i; @@ -349,8 +354,7 @@ ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * streamCount++; /* select Nth audio stream if specified, or first one */ - if (streamIndex < 0 || - (streamFile->stream_index > 0 && streamCount == streamFile->stream_index)) { + if (streamIndex < 0 || (stream_index > 0 && streamCount == stream_index)) { codecPar = stream->codecpar; streamIndex = i; } @@ -359,7 +363,7 @@ ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * if (i != streamIndex) stream->discard = AVDISCARD_ALL; /* disable demuxing for other streams */ } - if (streamCount < streamFile->stream_index) goto fail; + if (streamCount < stream_index) goto fail; if (streamIndex < 0 || !codecPar) goto fail; data->streamIndex = streamIndex; diff --git a/src/meta/bik.c b/src/meta/bik.c index d178508a..20ca20ff 100644 --- a/src/meta/bik.c +++ b/src/meta/bik.c @@ -8,6 +8,7 @@ static int bink_get_info(STREAMFILE *streamFile, int * out_total_streams, int * VGMSTREAM * init_vgmstream_bik(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; int channel_count = 0, loop_flag = 0, sample_rate = 0, num_samples = 0, total_streams = 0; + int stream_index = streamFile->stream_index; /* check extension, case insensitive (bika = manually demuxed audio) */ @@ -34,7 +35,7 @@ VGMSTREAM * init_vgmstream_bik(STREAMFILE *streamFile) { #ifdef VGM_USE_FFMPEG { /* init_FFmpeg uses streamFile->stream_index internally, if specified */ - vgmstream->codec_data = init_ffmpeg_offset(streamFile, 0x0, get_streamfile_size(streamFile)); + vgmstream->codec_data = init_ffmpeg_offset_index(streamFile, 0x0, get_streamfile_size(streamFile), stream_index); if (!vgmstream->codec_data) goto fail; vgmstream->coding_type = coding_FFmpeg; }