2017-05-19 17:21:22 +02:00
|
|
|
#include "coding.h"
|
2016-07-17 08:02:27 +02:00
|
|
|
|
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
/* internal sizes, can be any value */
|
2018-09-10 02:19:34 +02:00
|
|
|
#define FFMPEG_DEFAULT_SAMPLE_BUFFER_SIZE 2048
|
2017-05-19 17:21:22 +02:00
|
|
|
#define FFMPEG_DEFAULT_IO_BUFFER_SIZE 128 * 1024
|
|
|
|
|
|
|
|
|
|
|
|
static volatile int g_ffmpeg_initialized = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* ******************************************** */
|
|
|
|
/* INTERNAL UTILS */
|
|
|
|
/* ******************************************** */
|
|
|
|
|
|
|
|
/* Global FFmpeg init */
|
|
|
|
static void g_init_ffmpeg() {
|
|
|
|
if (g_ffmpeg_initialized == 1) {
|
|
|
|
while (g_ffmpeg_initialized < 2); /* active wait for lack of a better way */
|
|
|
|
}
|
|
|
|
else if (g_ffmpeg_initialized == 0) {
|
|
|
|
g_ffmpeg_initialized = 1;
|
|
|
|
av_log_set_flags(AV_LOG_SKIP_REPEATED);
|
|
|
|
av_log_set_level(AV_LOG_ERROR);
|
2018-08-15 00:18:05 +02:00
|
|
|
//av_register_all(); /* not needed in newer versions */
|
2017-05-19 17:21:22 +02:00
|
|
|
g_ffmpeg_initialized = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* converts codec's samples (can be in any format, ex. Ogg's float32) to PCM16 */
|
2018-10-06 22:39:34 +02:00
|
|
|
static void convert_audio_pcm16(sample *outbuf, const uint8_t *inbuf, int fullSampleCount, int bitsPerSample, int floatingPoint) {
|
2016-07-17 08:02:27 +02:00
|
|
|
int s;
|
|
|
|
switch (bitsPerSample) {
|
2018-10-06 22:39:34 +02:00
|
|
|
case 8: {
|
|
|
|
for (s = 0; s < fullSampleCount; s++) {
|
2016-07-17 08:02:27 +02:00
|
|
|
*outbuf++ = ((int)(*(inbuf++))-0x80) << 8;
|
|
|
|
}
|
|
|
|
break;
|
2018-10-06 22:39:34 +02:00
|
|
|
}
|
|
|
|
case 16: {
|
2016-07-17 08:02:27 +02:00
|
|
|
int16_t *s16 = (int16_t *)inbuf;
|
2018-10-06 22:39:34 +02:00
|
|
|
for (s = 0; s < fullSampleCount; s++) {
|
2016-07-17 08:02:27 +02:00
|
|
|
*outbuf++ = *(s16++);
|
|
|
|
}
|
|
|
|
break;
|
2018-10-06 22:39:34 +02:00
|
|
|
}
|
|
|
|
case 32: {
|
2016-07-17 08:02:27 +02:00
|
|
|
if (!floatingPoint) {
|
|
|
|
int32_t *s32 = (int32_t *)inbuf;
|
2018-10-06 22:39:34 +02:00
|
|
|
for (s = 0; s < fullSampleCount; s++) {
|
2016-07-17 08:02:27 +02:00
|
|
|
*outbuf++ = (*(s32++)) >> 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
float *s32 = (float *)inbuf;
|
2018-10-06 22:39:34 +02:00
|
|
|
for (s = 0; s < fullSampleCount; s++) {
|
2016-07-17 08:02:27 +02:00
|
|
|
float sample = *s32++;
|
|
|
|
int s16 = (int)(sample * 32768.0f);
|
|
|
|
if ((unsigned)(s16 + 0x8000) & 0xFFFF0000) {
|
|
|
|
s16 = (s16 >> 31) ^ 0x7FFF;
|
|
|
|
}
|
|
|
|
*outbuf++ = s16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-10-06 22:39:34 +02:00
|
|
|
}
|
|
|
|
case 64: {
|
2016-07-17 08:02:27 +02:00
|
|
|
if (floatingPoint) {
|
|
|
|
double *s64 = (double *)inbuf;
|
2018-10-06 22:39:34 +02:00
|
|
|
for (s = 0; s < fullSampleCount; s++) {
|
2016-07-17 08:02:27 +02:00
|
|
|
double sample = *s64++;
|
|
|
|
int s16 = (int)(sample * 32768.0f);
|
|
|
|
if ((unsigned)(s16 + 0x8000) & 0xFFFF0000) {
|
|
|
|
s16 = (s16 >> 31) ^ 0x7FFF;
|
|
|
|
}
|
|
|
|
*outbuf++ = s16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-10-06 22:39:34 +02:00
|
|
|
}
|
2016-07-17 08:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
/**
|
|
|
|
* Special patching for FFmpeg's buggy seek code.
|
|
|
|
*
|
|
|
|
* To seek with avformat_seek_file/av_seek_frame, FFmpeg's demuxers can implement read_seek2 (newest API)
|
|
|
|
* or read_seek (older API), with various search modes. If none are available it will use seek_frame_generic,
|
|
|
|
* which manually reads frame by frame until the selected timestamp. However, the prev frame will be consumed
|
|
|
|
* (so after seeking to 0 next av_read_frame will actually give the second frame and so on).
|
|
|
|
*
|
|
|
|
* Fortunately seek_frame_generic can use an index to find the correct position. This function reads the
|
|
|
|
* first frame/packet and sets up index to timestamp 0. This ensures faulty demuxers will seek to 0 correctly.
|
|
|
|
* Some formats may not seek to 0 even with this, though.
|
|
|
|
*/
|
|
|
|
static int init_seek(ffmpeg_codec_data * data) {
|
|
|
|
int ret, ts_index, found_first = 0;
|
2018-10-06 22:39:34 +02:00
|
|
|
int64_t ts = 0; /* seek timestamp */
|
|
|
|
int64_t pos = 0; /* data offset */
|
|
|
|
int size = 0; /* data size (block align) */
|
|
|
|
int distance = 0; /* always? ("duration") */
|
2017-05-19 17:21:22 +02:00
|
|
|
|
2018-10-06 22:39:34 +02:00
|
|
|
AVStream * stream = data->formatCtx->streams[data->streamIndex];
|
|
|
|
AVPacket * pkt = data->lastReadPacket;
|
2017-05-19 17:21:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* read_seek shouldn't need this index, but direct access to FFmpeg's internals is no good */
|
|
|
|
/* if (data->formatCtx->iformat->read_seek || data->formatCtx->iformat->read_seek2)
|
|
|
|
return 0; */
|
|
|
|
|
|
|
|
/* some formats already have a proper index (e.g. M4A) */
|
2018-10-06 22:39:34 +02:00
|
|
|
//todo: can't seek to 0 when there index but no ts 0
|
2017-05-19 17:21:22 +02:00
|
|
|
ts_index = av_index_search_timestamp(stream, ts, AVSEEK_FLAG_ANY);
|
|
|
|
if (ts_index>=0)
|
|
|
|
goto test_seek;
|
|
|
|
|
|
|
|
|
|
|
|
/* find the first + second packets to get pos/size */
|
|
|
|
while (1) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
ret = av_read_frame(data->formatCtx, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
if (pkt->stream_index != data->streamIndex)
|
|
|
|
continue; /* ignore non-selected streams */
|
|
|
|
|
|
|
|
if (!found_first) { /* first found */
|
|
|
|
found_first = 1;
|
|
|
|
pos = pkt->pos;
|
|
|
|
ts = pkt->dts;
|
|
|
|
continue;
|
2018-10-06 22:39:34 +02:00
|
|
|
} else { /* second found */ //todo test with only one packet, pos can be -1
|
2017-05-19 17:21:22 +02:00
|
|
|
size = pkt->pos - pos; /* coded, pkt->size is decoded size */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found_first)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* in rare cases there is only one packet */
|
|
|
|
/* if (size == 0) { size = data_end - pos; } */ /* no easy way to know, ignore (most formats don's need size) */
|
|
|
|
|
|
|
|
/* some formats (XMA1) don't seem to have packet.dts, pretend it's 0 */
|
|
|
|
if (ts == INT64_MIN)
|
|
|
|
ts = 0;
|
|
|
|
|
|
|
|
/* Some streams start with negative DTS (observed in Ogg). For Ogg seeking to negative or 0 doesn't alter the output.
|
|
|
|
* It does seem seeking before decoding alters a bunch of (inaudible) +-1 lower bytes though. */
|
|
|
|
VGM_ASSERT(ts != 0, "FFMPEG: negative start_ts (%li)\n", (long)ts);
|
|
|
|
if (ts != 0)
|
|
|
|
ts = 0;
|
|
|
|
|
|
|
|
/* add index 0 */
|
|
|
|
ret = av_add_index_entry(stream, pos, ts, size, distance, AVINDEX_KEYFRAME);
|
|
|
|
if ( ret < 0 )
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
|
|
test_seek:
|
|
|
|
/* seek to 0 test / move back to beginning, since we just consumed packets */
|
|
|
|
ret = avformat_seek_file(data->formatCtx, data->streamIndex, ts, ts, ts, AVSEEK_FLAG_ANY);
|
|
|
|
if ( ret < 0 )
|
|
|
|
return ret; /* we can't even reset_vgmstream the file */
|
|
|
|
|
|
|
|
avcodec_flush_buffers(data->codecCtx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ******************************************** */
|
|
|
|
/* AVIO CALLBACKS */
|
|
|
|
/* ******************************************** */
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
/* AVIO callback: read stream, handling custom data */
|
2018-09-10 02:19:34 +02:00
|
|
|
static int ffmpeg_read(void *opaque, uint8_t *buf, int read_size) {
|
2017-05-19 17:21:22 +02:00
|
|
|
ffmpeg_codec_data *data = (ffmpeg_codec_data *) opaque;
|
2018-09-10 02:19:34 +02:00
|
|
|
int bytes = 0;
|
2017-05-19 17:21:22 +02:00
|
|
|
int max_to_copy = 0;
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
/* clamp reads */
|
2018-09-10 02:19:34 +02:00
|
|
|
if (data->logical_offset + read_size > data->logical_size)
|
|
|
|
read_size = data->logical_size - data->logical_offset;
|
|
|
|
if (read_size == 0)
|
|
|
|
return bytes;
|
2017-09-24 18:40:59 +02:00
|
|
|
|
|
|
|
/* handle reads on inserted header */
|
2018-09-10 02:19:34 +02:00
|
|
|
if (data->header_size && data->logical_offset < data->header_size) {
|
|
|
|
max_to_copy = (int)(data->header_size - data->logical_offset);
|
|
|
|
if (max_to_copy > read_size)
|
|
|
|
max_to_copy = read_size;
|
|
|
|
|
|
|
|
memcpy(buf, data->header_insert_block + data->logical_offset, max_to_copy);
|
|
|
|
buf += max_to_copy;
|
|
|
|
read_size -= max_to_copy;
|
|
|
|
data->logical_offset += max_to_copy;
|
|
|
|
|
|
|
|
if (read_size == 0) {
|
|
|
|
return max_to_copy; /* offset still in header */
|
2017-05-19 17:21:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
/* main read */
|
2018-09-10 02:19:34 +02:00
|
|
|
bytes = read_streamfile(buf, data->offset, read_size, data->streamfile);
|
|
|
|
data->logical_offset += bytes;
|
|
|
|
data->offset += bytes;
|
|
|
|
return bytes + max_to_copy;
|
2017-05-19 17:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* AVIO callback: write stream not needed */
|
|
|
|
static int ffmpeg_write(void *opaque, uint8_t *buf, int buf_size) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
/* AVIO callback: seek stream, handling custom data */
|
2017-05-19 17:21:22 +02:00
|
|
|
static int64_t ffmpeg_seek(void *opaque, int64_t offset, int whence) {
|
|
|
|
ffmpeg_codec_data *data = (ffmpeg_codec_data *) opaque;
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
/* get cache'd size */
|
2017-05-19 17:21:22 +02:00
|
|
|
if (whence & AVSEEK_SIZE) {
|
2018-09-10 02:19:34 +02:00
|
|
|
return data->logical_size;
|
2017-05-19 17:21:22 +02:00
|
|
|
}
|
2017-09-24 18:40:59 +02:00
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
whence &= ~(AVSEEK_SIZE | AVSEEK_FORCE);
|
2017-09-24 18:40:59 +02:00
|
|
|
/* find the final offset FFmpeg sees (within fake header + virtual size) */
|
2017-05-19 17:21:22 +02:00
|
|
|
switch (whence) {
|
2017-09-24 18:40:59 +02:00
|
|
|
case SEEK_SET: /* absolute */
|
2017-05-19 17:21:22 +02:00
|
|
|
break;
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
case SEEK_CUR: /* relative to current */
|
2018-09-10 02:19:34 +02:00
|
|
|
offset += data->logical_offset;
|
2017-05-19 17:21:22 +02:00
|
|
|
break;
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
case SEEK_END: /* relative to file end (should be negative) */
|
2018-09-10 02:19:34 +02:00
|
|
|
offset += data->logical_size;
|
2017-05-19 17:21:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
/* clamp offset; fseek does this too */
|
2018-09-10 02:19:34 +02:00
|
|
|
if (offset > data->logical_size)
|
|
|
|
offset = data->logical_size;
|
2017-09-24 18:40:59 +02:00
|
|
|
else if (offset < 0)
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
/* seeks inside fake header */
|
|
|
|
if (offset < data->header_size) {
|
2018-09-10 02:19:34 +02:00
|
|
|
data->logical_offset = offset;
|
|
|
|
data->offset = data->start;
|
2017-09-24 18:40:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* main seek */
|
2018-09-10 02:19:34 +02:00
|
|
|
data->logical_offset = offset;
|
|
|
|
data->offset = data->start + (offset - data->header_size);
|
2017-05-19 17:21:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ******************************************** */
|
|
|
|
/* MAIN INIT/DECODER */
|
|
|
|
/* ******************************************** */
|
|
|
|
|
|
|
|
ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size) {
|
2018-09-10 02:19:34 +02:00
|
|
|
return init_ffmpeg_header_offset(streamFile, NULL,0, start,size);
|
2017-08-19 10:28:28 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
/**
|
|
|
|
* Manually init FFmpeg, from a fake header / offset.
|
|
|
|
*
|
|
|
|
* 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.
|
2017-08-19 10:28:28 +02:00
|
|
|
* NULL header can used given if the stream has internal data recognized by FFmpeg at offset.
|
2018-09-10 02:19:34 +02:00
|
|
|
* Stream index can be passed to FFmpeg in the streamFile, if the format has multiple streams (1=first).
|
2017-05-19 17:21:22 +02:00
|
|
|
*/
|
2018-09-10 02:19:34 +02:00
|
|
|
ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size) {
|
2017-05-19 17:21:22 +02:00
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
ffmpeg_codec_data * data;
|
|
|
|
int errcode, i;
|
2018-09-10 02:19:34 +02:00
|
|
|
int targetSubsong = streamFile->stream_index;
|
2017-05-19 17:21:22 +02:00
|
|
|
int streamIndex, streamCount;
|
2018-09-10 02:19:34 +02:00
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
AVStream *stream;
|
2017-08-18 18:56:43 +02:00
|
|
|
AVCodecParameters *codecPar = NULL;
|
2017-05-19 17:21:22 +02:00
|
|
|
AVRational tb;
|
|
|
|
|
|
|
|
|
|
|
|
/* basic setup */
|
|
|
|
g_init_ffmpeg();
|
|
|
|
|
|
|
|
data = ( ffmpeg_codec_data * ) calloc(1, sizeof(ffmpeg_codec_data));
|
|
|
|
if (!data) return NULL;
|
|
|
|
|
|
|
|
streamFile->get_name( streamFile, filename, sizeof(filename) );
|
|
|
|
data->streamfile = streamFile->open(streamFile, filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (!data->streamfile) goto fail;
|
|
|
|
|
2018-03-31 13:07:26 +02:00
|
|
|
/* ignore bad combos */
|
|
|
|
if ((header && !header_size) || (!header && header_size))
|
|
|
|
goto fail;
|
|
|
|
|
2017-09-24 18:40:59 +02:00
|
|
|
/* fake header to trick FFmpeg into demuxing/decoding the stream */
|
2017-05-19 17:21:22 +02:00
|
|
|
if (header_size > 0) {
|
|
|
|
data->header_size = header_size;
|
|
|
|
data->header_insert_block = av_memdup(header, header_size);
|
|
|
|
if (!data->header_insert_block) goto fail;
|
|
|
|
}
|
|
|
|
|
2018-09-10 02:19:34 +02:00
|
|
|
data->start = start;
|
|
|
|
data->offset = start;
|
|
|
|
data->size = size;
|
|
|
|
if (data->size == 0 || data->start + data->size > get_streamfile_size(streamFile)) {
|
2018-08-15 22:47:22 +02:00
|
|
|
VGM_LOG("FFmpeg: wrong start+size found\n");
|
2018-09-10 02:19:34 +02:00
|
|
|
data->size = get_streamfile_size(streamFile) - data->start;
|
2018-08-15 22:47:22 +02:00
|
|
|
}
|
2018-09-10 02:19:34 +02:00
|
|
|
data->logical_offset = 0;
|
|
|
|
data->logical_size = data->header_size + data->size;
|
2017-09-24 18:40:59 +02:00
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
/* setup IO, attempt to autodetect format and gather some info */
|
|
|
|
data->buffer = av_malloc(FFMPEG_DEFAULT_IO_BUFFER_SIZE);
|
|
|
|
if (!data->buffer) goto fail;
|
|
|
|
|
|
|
|
data->ioCtx = avio_alloc_context(data->buffer, FFMPEG_DEFAULT_IO_BUFFER_SIZE, 0, data, ffmpeg_read, ffmpeg_write, ffmpeg_seek);
|
|
|
|
if (!data->ioCtx) goto fail;
|
|
|
|
|
|
|
|
data->formatCtx = avformat_alloc_context();
|
|
|
|
if (!data->formatCtx) goto fail;
|
|
|
|
|
|
|
|
data->formatCtx->pb = data->ioCtx;
|
|
|
|
|
|
|
|
if ((errcode = avformat_open_input(&data->formatCtx, "", NULL, NULL)) < 0) goto fail; /* autodetect */
|
|
|
|
|
|
|
|
if ((errcode = avformat_find_stream_info(data->formatCtx, NULL)) < 0) goto fail;
|
|
|
|
|
|
|
|
|
2017-08-18 18:56:43 +02:00
|
|
|
/* find valid audio stream */
|
2017-05-19 17:21:22 +02:00
|
|
|
streamIndex = -1;
|
2017-08-18 18:56:43 +02:00
|
|
|
streamCount = 0;
|
2017-05-19 17:21:22 +02:00
|
|
|
|
|
|
|
for (i = 0; i < data->formatCtx->nb_streams; ++i) {
|
|
|
|
stream = data->formatCtx->streams[i];
|
2017-08-18 18:56:43 +02:00
|
|
|
|
2018-02-17 23:21:41 +01:00
|
|
|
if (stream->codecpar && stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2017-05-19 17:21:22 +02:00
|
|
|
streamCount++;
|
|
|
|
|
2017-08-18 18:56:43 +02:00
|
|
|
/* select Nth audio stream if specified, or first one */
|
2018-09-10 02:19:34 +02:00
|
|
|
if (streamIndex < 0 || (targetSubsong > 0 && streamCount == targetSubsong)) {
|
2017-08-18 18:56:43 +02:00
|
|
|
codecPar = stream->codecpar;
|
|
|
|
streamIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != streamIndex)
|
|
|
|
stream->discard = AVDISCARD_ALL; /* disable demuxing for other streams */
|
|
|
|
}
|
2018-09-10 02:19:34 +02:00
|
|
|
if (streamCount < targetSubsong) goto fail;
|
2017-08-18 18:56:43 +02:00
|
|
|
if (streamIndex < 0 || !codecPar) goto fail;
|
2017-05-19 17:21:22 +02:00
|
|
|
|
|
|
|
data->streamIndex = streamIndex;
|
|
|
|
stream = data->formatCtx->streams[streamIndex];
|
|
|
|
data->streamCount = streamCount;
|
|
|
|
|
|
|
|
|
|
|
|
/* prepare codec and frame/packet buffers */
|
|
|
|
data->codecCtx = avcodec_alloc_context3(NULL);
|
|
|
|
if (!data->codecCtx) goto fail;
|
|
|
|
|
|
|
|
if ((errcode = avcodec_parameters_to_context(data->codecCtx, codecPar)) < 0) goto fail;
|
|
|
|
|
2018-08-15 00:18:05 +02:00
|
|
|
//av_codec_set_pkt_timebase(data->codecCtx, stream->time_base); /* deprecated and seemingly not needed */
|
2017-05-19 17:21:22 +02:00
|
|
|
|
|
|
|
data->codec = avcodec_find_decoder(data->codecCtx->codec_id);
|
|
|
|
if (!data->codec) goto fail;
|
|
|
|
|
|
|
|
if ((errcode = avcodec_open2(data->codecCtx, data->codec, NULL)) < 0) goto fail;
|
|
|
|
|
|
|
|
data->lastDecodedFrame = av_frame_alloc();
|
|
|
|
if (!data->lastDecodedFrame) goto fail;
|
|
|
|
av_frame_unref(data->lastDecodedFrame);
|
|
|
|
|
|
|
|
data->lastReadPacket = malloc(sizeof(AVPacket));
|
|
|
|
if (!data->lastReadPacket) goto fail;
|
|
|
|
av_new_packet(data->lastReadPacket, 0);
|
|
|
|
|
|
|
|
data->readNextPacket = 1;
|
|
|
|
data->bytesConsumedFromDecodedFrame = INT_MAX;
|
|
|
|
|
|
|
|
|
2017-08-18 18:56:43 +02:00
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
/* other setup */
|
|
|
|
data->sampleRate = data->codecCtx->sample_rate;
|
|
|
|
data->channels = data->codecCtx->channels;
|
|
|
|
data->floatingPoint = 0;
|
|
|
|
|
|
|
|
switch (data->codecCtx->sample_fmt) {
|
|
|
|
case AV_SAMPLE_FMT_U8:
|
|
|
|
case AV_SAMPLE_FMT_U8P:
|
|
|
|
data->bitsPerSample = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
|
|
|
case AV_SAMPLE_FMT_S16P:
|
|
|
|
data->bitsPerSample = 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_SAMPLE_FMT_S32:
|
|
|
|
case AV_SAMPLE_FMT_S32P:
|
|
|
|
data->bitsPerSample = 32;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
|
|
|
data->bitsPerSample = 32;
|
|
|
|
data->floatingPoint = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_SAMPLE_FMT_DBL:
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
|
|
|
data->bitsPerSample = 64;
|
|
|
|
data->floatingPoint = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->bitrate = (int)(data->codecCtx->bit_rate);
|
|
|
|
data->endOfStream = 0;
|
|
|
|
data->endOfAudio = 0;
|
|
|
|
|
|
|
|
/* try to guess frames/samples (duration isn't always set) */
|
|
|
|
tb.num = 1; tb.den = data->codecCtx->sample_rate;
|
|
|
|
data->totalSamples = av_rescale_q(stream->duration, stream->time_base, tb);
|
|
|
|
if (data->totalSamples < 0)
|
|
|
|
data->totalSamples = 0; /* caller must consider this */
|
|
|
|
|
|
|
|
data->blockAlign = data->codecCtx->block_align;
|
|
|
|
data->frameSize = data->codecCtx->frame_size;
|
|
|
|
if(data->frameSize == 0) /* some formats don't set frame_size but can get on request, and vice versa */
|
|
|
|
data->frameSize = av_get_audio_frame_duration(data->codecCtx,0);
|
|
|
|
|
|
|
|
/* setup decode buffer */
|
2018-09-10 02:19:34 +02:00
|
|
|
data->sampleBufferBlock = FFMPEG_DEFAULT_SAMPLE_BUFFER_SIZE;
|
2017-05-19 17:21:22 +02:00
|
|
|
data->sampleBuffer = av_malloc( data->sampleBufferBlock * (data->bitsPerSample / 8) * data->channels );
|
|
|
|
if (!data->sampleBuffer)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
/* setup decent seeking for faulty formats */
|
|
|
|
errcode = init_seek(data);
|
|
|
|
if (errcode < 0) goto fail;
|
|
|
|
|
|
|
|
/* expose start samples to be skipped (encoder delay, usually added by MDCT-based encoders like AAC/MP3/ATRAC3/XMA/etc)
|
|
|
|
* get after init_seek because some demuxers like AAC only fill skip_samples for the first packet */
|
|
|
|
if (stream->start_skip_samples) /* samples to skip in the first packet */
|
|
|
|
data->skipSamples = stream->start_skip_samples;
|
|
|
|
else if (stream->skip_samples) /* samples to skip in any packet (first in this case), used sometimes instead (ex. AAC) */
|
|
|
|
data->skipSamples = stream->skip_samples;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free_ffmpeg(data);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode samples of any kind of FFmpeg format */
|
2016-12-03 11:42:38 +01:00
|
|
|
void decode_ffmpeg(VGMSTREAM *vgmstream, sample * outbuf, int32_t samples_to_do, int channels) {
|
2018-10-06 22:39:34 +02:00
|
|
|
ffmpeg_codec_data *data = vgmstream->codec_data;
|
|
|
|
int samplesReadNow;
|
|
|
|
//todo use either channels / data->channels / codecCtx->channels
|
|
|
|
|
|
|
|
AVFormatContext *formatCtx = data->formatCtx;
|
|
|
|
AVCodecContext *codecCtx = data->codecCtx;
|
|
|
|
AVPacket *packet = data->lastReadPacket;
|
|
|
|
AVFrame *frame = data->lastDecodedFrame;
|
|
|
|
int planar = av_sample_fmt_is_planar(data->codecCtx->sample_fmt);
|
|
|
|
|
|
|
|
int readNextPacket = data->readNextPacket;
|
|
|
|
int endOfStream = data->endOfStream;
|
|
|
|
int endOfAudio = data->endOfAudio;
|
|
|
|
int bytesConsumedFromDecodedFrame = data->bytesConsumedFromDecodedFrame;
|
|
|
|
|
|
|
|
int bytesPerSample = data->bitsPerSample / 8;
|
|
|
|
int bytesRead, bytesToRead;
|
|
|
|
|
|
|
|
|
|
|
|
/* ignore once file is done (but not at endOfStream as FFmpeg can still output samples until endOfAudio) */
|
|
|
|
if (/*endOfStream ||*/ endOfAudio) {
|
|
|
|
VGM_LOG("FFMPEG: decode after end of audio\n");
|
2016-07-17 08:02:27 +02:00
|
|
|
memset(outbuf, 0, samples_to_do * channels * sizeof(sample));
|
|
|
|
return;
|
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
2016-07-17 08:02:27 +02:00
|
|
|
bytesRead = 0;
|
2018-10-06 22:39:34 +02:00
|
|
|
bytesToRead = samples_to_do * (bytesPerSample * codecCtx->channels);
|
|
|
|
|
|
|
|
|
|
|
|
/* keep reading and decoding packets until the requested number of samples (in bytes for FFmpeg calcs) */
|
2016-07-17 08:02:27 +02:00
|
|
|
while (bytesRead < bytesToRead) {
|
2018-10-06 22:39:34 +02:00
|
|
|
int dataSize, toConsume, errcode;
|
2016-12-01 23:49:00 +01:00
|
|
|
|
2018-10-06 22:39:34 +02:00
|
|
|
/* get sample data size from current frame (dataSize will be < 0 when nb_samples = 0) */
|
|
|
|
dataSize = av_samples_get_buffer_size(NULL, codecCtx->channels, frame->nb_samples, codecCtx->sample_fmt, 1);
|
2016-07-17 08:02:27 +02:00
|
|
|
if (dataSize < 0)
|
|
|
|
dataSize = 0;
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
/* read new data packet when requested */
|
2016-07-17 08:02:27 +02:00
|
|
|
while (readNextPacket && !endOfAudio) {
|
|
|
|
if (!endOfStream) {
|
2018-10-06 22:39:34 +02:00
|
|
|
/* reset old packet */
|
|
|
|
av_packet_unref(packet);
|
|
|
|
|
|
|
|
/* get compressed data from demuxer into packet */
|
|
|
|
errcode = av_read_frame(formatCtx, packet);
|
|
|
|
if (errcode < 0) {
|
2016-07-17 08:02:27 +02:00
|
|
|
if (errcode == AVERROR_EOF) {
|
2018-10-06 22:39:34 +02:00
|
|
|
endOfStream = 1; /* no more data, but may still output samples */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VGM_LOG("FFMPEG: av_read_frame errcode %i\n", errcode);
|
2016-07-17 08:02:27 +02:00
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
if (formatCtx->pb && formatCtx->pb->error) {
|
2016-07-17 08:02:27 +02:00
|
|
|
break;
|
2018-10-06 22:39:34 +02:00
|
|
|
}
|
2016-07-17 08:02:27 +02:00
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
if (packet->stream_index != data->streamIndex)
|
2016-12-01 23:49:00 +01:00
|
|
|
continue; /* ignore non-selected streams */
|
2016-07-17 08:02:27 +02:00
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
/* send compressed data to decoder in packet (NULL at EOF to "drain") */
|
|
|
|
errcode = avcodec_send_packet(codecCtx, endOfStream ? NULL : packet);
|
|
|
|
if (errcode < 0) {
|
2016-07-17 08:02:27 +02:00
|
|
|
if (errcode != AVERROR(EAGAIN)) {
|
2018-10-06 22:39:34 +02:00
|
|
|
VGM_LOG("FFMPEG: avcodec_send_packet errcode %i\n", errcode);
|
2016-07-17 08:02:27 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 22:39:34 +02:00
|
|
|
readNextPacket = 0; /* got compressed data */
|
2016-07-17 08:02:27 +02:00
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
/* decode packet into frame's sample data (if we don't have bytes to consume from previous frame) */
|
2016-07-17 08:02:27 +02:00
|
|
|
if (dataSize <= bytesConsumedFromDecodedFrame) {
|
2018-10-06 22:39:34 +02:00
|
|
|
if (endOfAudio) {
|
2016-07-17 08:02:27 +02:00
|
|
|
break;
|
2018-10-06 22:39:34 +02:00
|
|
|
}
|
|
|
|
|
2016-07-17 08:02:27 +02:00
|
|
|
bytesConsumedFromDecodedFrame = 0;
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
/* receive uncompressed sample data from decoder in frame */
|
|
|
|
errcode = avcodec_receive_frame(codecCtx, frame);
|
|
|
|
if (errcode < 0) {
|
2016-07-17 08:02:27 +02:00
|
|
|
if (errcode == AVERROR_EOF) {
|
2018-10-06 22:39:34 +02:00
|
|
|
endOfAudio = 1; /* no more samples, file is fully decoded */
|
2016-07-17 08:02:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (errcode == AVERROR(EAGAIN)) {
|
2018-10-06 22:39:34 +02:00
|
|
|
readNextPacket = 1; /* request more compressed data */
|
2016-07-17 08:02:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2018-10-06 22:39:34 +02:00
|
|
|
VGM_LOG("FFMPEG: avcodec_receive_frame errcode %i\n", errcode);
|
2016-07-17 08:02:27 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
/* get sample data size of current frame */
|
|
|
|
dataSize = av_samples_get_buffer_size(NULL, codecCtx->channels, frame->nb_samples, codecCtx->sample_fmt, 1);
|
2016-07-17 08:02:27 +02:00
|
|
|
if (dataSize < 0)
|
|
|
|
dataSize = 0;
|
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
2016-07-17 08:02:27 +02:00
|
|
|
toConsume = FFMIN((dataSize - bytesConsumedFromDecodedFrame), (bytesToRead - bytesRead));
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
|
2016-12-01 23:49:00 +01:00
|
|
|
/* discard decoded frame if needed (fully or partially) */
|
2016-08-15 00:18:34 +02:00
|
|
|
if (data->samplesToDiscard) {
|
2018-10-06 22:39:34 +02:00
|
|
|
int samplesDataSize = dataSize / (bytesPerSample * channels);
|
2016-11-20 15:39:21 +01:00
|
|
|
|
2016-12-03 18:17:37 +01:00
|
|
|
if (data->samplesToDiscard >= samplesDataSize) {
|
|
|
|
/* discard all of the frame's samples and continue to the next */
|
2016-08-15 00:18:34 +02:00
|
|
|
bytesConsumedFromDecodedFrame = dataSize;
|
2016-12-03 18:17:37 +01:00
|
|
|
data->samplesToDiscard -= samplesDataSize;
|
2016-08-15 00:18:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-12-03 18:17:37 +01:00
|
|
|
else {
|
|
|
|
/* discard part of the frame and copy the rest below */
|
2018-10-06 22:39:34 +02:00
|
|
|
int bytesToDiscard = data->samplesToDiscard * (bytesPerSample * channels);
|
2016-12-03 18:17:37 +01:00
|
|
|
int dataSizeLeft = dataSize - bytesToDiscard;
|
|
|
|
|
|
|
|
bytesConsumedFromDecodedFrame += bytesToDiscard;
|
2016-08-15 00:18:34 +02:00
|
|
|
data->samplesToDiscard = 0;
|
2016-12-03 18:17:37 +01:00
|
|
|
if (toConsume > dataSizeLeft)
|
2018-10-06 22:39:34 +02:00
|
|
|
toConsume = dataSizeLeft;
|
2016-08-15 00:18:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-20 15:39:21 +01:00
|
|
|
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
/* copy decoded sample data to buffer */
|
|
|
|
if (!planar || channels == 1) { /* 1 sample per channel, already mixed */
|
|
|
|
memmove(data->sampleBuffer + bytesRead, (frame->data[0] + bytesConsumedFromDecodedFrame), toConsume);
|
2016-07-17 08:02:27 +02:00
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
else { /* N samples per channel, mix to 1 sample per channel */
|
|
|
|
uint8_t * out = (uint8_t *) data->sampleBuffer + bytesRead;
|
2016-07-17 08:02:27 +02:00
|
|
|
int bytesConsumedPerPlane = bytesConsumedFromDecodedFrame / channels;
|
|
|
|
int toConsumePerPlane = toConsume / channels;
|
|
|
|
int s, ch;
|
|
|
|
for (s = 0; s < toConsumePerPlane; s += bytesPerSample) {
|
|
|
|
for (ch = 0; ch < channels; ++ch) {
|
2018-10-06 22:39:34 +02:00
|
|
|
memcpy(out, frame->extended_data[ch] + bytesConsumedPerPlane + s, bytesPerSample);
|
2016-07-17 08:02:27 +02:00
|
|
|
out += bytesPerSample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
2016-11-20 15:39:21 +01:00
|
|
|
/* consume */
|
2016-07-17 08:02:27 +02:00
|
|
|
bytesConsumedFromDecodedFrame += toConsume;
|
|
|
|
bytesRead += toConsume;
|
|
|
|
}
|
2018-10-06 22:39:34 +02:00
|
|
|
|
|
|
|
|
2016-07-17 08:02:27 +02:00
|
|
|
end:
|
2018-10-06 22:39:34 +02:00
|
|
|
/* convert native sample format into PCM16 outbuf */
|
|
|
|
samplesReadNow = bytesRead / (bytesPerSample * channels);
|
|
|
|
convert_audio_pcm16(outbuf, data->sampleBuffer, samplesReadNow * channels, data->bitsPerSample, data->floatingPoint);
|
|
|
|
|
|
|
|
/* clean buffer when requested more samples than possible */
|
|
|
|
if (endOfAudio && samplesReadNow < samples_to_do) {
|
|
|
|
VGM_LOG("FFMPEG: decode after end of audio\n");
|
|
|
|
memset(outbuf + (samplesReadNow * channels), 0, (samples_to_do - samplesReadNow) * channels * sizeof(sample));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy state back */
|
2016-07-17 08:02:27 +02:00
|
|
|
data->readNextPacket = readNextPacket;
|
|
|
|
data->endOfStream = endOfStream;
|
|
|
|
data->endOfAudio = endOfAudio;
|
2018-10-06 22:39:34 +02:00
|
|
|
data->bytesConsumedFromDecodedFrame = bytesConsumedFromDecodedFrame;
|
2016-07-17 08:02:27 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 23:48:49 +01:00
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
/* ******************************************** */
|
|
|
|
/* UTILS */
|
|
|
|
/* ******************************************** */
|
|
|
|
|
2016-12-01 19:58:51 +01:00
|
|
|
void reset_ffmpeg(VGMSTREAM *vgmstream) {
|
|
|
|
ffmpeg_codec_data *data = (ffmpeg_codec_data *) vgmstream->codec_data;
|
2018-03-10 16:59:00 +01:00
|
|
|
if (!data) return;
|
2016-12-01 19:58:51 +01:00
|
|
|
|
|
|
|
if (data->formatCtx) {
|
2016-12-01 23:49:00 +01:00
|
|
|
avformat_seek_file(data->formatCtx, data->streamIndex, 0, 0, 0, AVSEEK_FLAG_ANY);
|
2016-12-01 19:58:51 +01:00
|
|
|
}
|
|
|
|
if (data->codecCtx) {
|
|
|
|
avcodec_flush_buffers(data->codecCtx);
|
|
|
|
}
|
|
|
|
data->readNextPacket = 1;
|
|
|
|
data->bytesConsumedFromDecodedFrame = INT_MAX;
|
|
|
|
data->endOfStream = 0;
|
|
|
|
data->endOfAudio = 0;
|
|
|
|
data->samplesToDiscard = 0;
|
2017-02-12 13:17:01 +01:00
|
|
|
|
|
|
|
/* consider skip samples (encoder delay), if manually set (otherwise let FFmpeg handle it) */
|
|
|
|
if (data->skipSamplesSet) {
|
|
|
|
AVStream *stream = data->formatCtx->streams[data->streamIndex];
|
|
|
|
/* sometimes (ex. AAC) after seeking to the first packet skip_samples is restored, but we want our value */
|
|
|
|
stream->skip_samples = 0;
|
|
|
|
stream->start_skip_samples = 0;
|
|
|
|
|
|
|
|
data->samplesToDiscard += data->skipSamples;
|
|
|
|
}
|
2016-12-01 19:58:51 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 23:48:49 +01:00
|
|
|
void seek_ffmpeg(VGMSTREAM *vgmstream, int32_t num_sample) {
|
|
|
|
ffmpeg_codec_data *data = (ffmpeg_codec_data *) vgmstream->codec_data;
|
|
|
|
int64_t ts;
|
2018-10-06 22:39:34 +02:00
|
|
|
if (!data)
|
|
|
|
return;
|
2016-11-30 23:48:49 +01:00
|
|
|
|
2017-02-12 13:17:01 +01:00
|
|
|
/* Start from 0 and discard samples until loop_start (slower but not too noticeable).
|
|
|
|
* Due to various FFmpeg quirks seeking to a sample is erratic in many formats (would need extra steps). */
|
2016-11-30 23:48:49 +01:00
|
|
|
data->samplesToDiscard = num_sample;
|
|
|
|
ts = 0;
|
|
|
|
|
2016-12-01 23:49:00 +01:00
|
|
|
avformat_seek_file(data->formatCtx, data->streamIndex, ts, ts, ts, AVSEEK_FLAG_ANY);
|
2016-11-30 23:48:49 +01:00
|
|
|
avcodec_flush_buffers(data->codecCtx);
|
|
|
|
|
|
|
|
data->readNextPacket = 1;
|
|
|
|
data->bytesConsumedFromDecodedFrame = INT_MAX;
|
|
|
|
data->endOfStream = 0;
|
|
|
|
data->endOfAudio = 0;
|
|
|
|
|
2017-02-12 13:17:01 +01:00
|
|
|
/* consider skip samples (encoder delay), if manually set (otherwise let FFmpeg handle it) */
|
|
|
|
if (data->skipSamplesSet) {
|
|
|
|
AVStream *stream = data->formatCtx->streams[data->streamIndex];
|
|
|
|
/* sometimes (ex. AAC) after seeking to the first packet skip_samples is restored, but we want our value */
|
|
|
|
stream->skip_samples = 0;
|
|
|
|
stream->start_skip_samples = 0;
|
|
|
|
|
|
|
|
data->samplesToDiscard += data->skipSamples;
|
|
|
|
}
|
2016-11-30 23:48:49 +01:00
|
|
|
}
|
|
|
|
|
2017-05-19 17:21:22 +02:00
|
|
|
void free_ffmpeg(ffmpeg_codec_data *data) {
|
|
|
|
if (data == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (data->lastReadPacket) {
|
|
|
|
av_packet_unref(data->lastReadPacket);
|
|
|
|
free(data->lastReadPacket);
|
|
|
|
data->lastReadPacket = NULL;
|
|
|
|
}
|
|
|
|
if (data->lastDecodedFrame) {
|
|
|
|
av_free(data->lastDecodedFrame);
|
|
|
|
data->lastDecodedFrame = NULL;
|
|
|
|
}
|
|
|
|
if (data->codecCtx) {
|
|
|
|
avcodec_close(data->codecCtx);
|
|
|
|
avcodec_free_context(&(data->codecCtx));
|
|
|
|
data->codecCtx = NULL;
|
|
|
|
}
|
|
|
|
if (data->formatCtx) {
|
|
|
|
avformat_close_input(&(data->formatCtx));
|
|
|
|
data->formatCtx = NULL;
|
|
|
|
}
|
|
|
|
if (data->ioCtx) {
|
|
|
|
// buffer passed in is occasionally freed and replaced.
|
|
|
|
// the replacement must be freed as well.
|
|
|
|
data->buffer = data->ioCtx->buffer;
|
|
|
|
av_free(data->ioCtx);
|
|
|
|
data->ioCtx = NULL;
|
|
|
|
}
|
|
|
|
if (data->buffer) {
|
|
|
|
av_free(data->buffer);
|
|
|
|
data->buffer = NULL;
|
|
|
|
}
|
|
|
|
if (data->sampleBuffer) {
|
|
|
|
av_free(data->sampleBuffer);
|
|
|
|
data->sampleBuffer = NULL;
|
|
|
|
}
|
|
|
|
if (data->header_insert_block) {
|
|
|
|
av_free(data->header_insert_block);
|
|
|
|
data->header_insert_block = NULL;
|
|
|
|
}
|
|
|
|
if (data->streamfile) {
|
|
|
|
close_streamfile(data->streamfile);
|
|
|
|
data->streamfile = NULL;
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 13:17:01 +01:00
|
|
|
/**
|
|
|
|
* Sets the number of samples to skip at the beginning of the stream, needed by some "gapless" formats.
|
|
|
|
* (encoder delay, usually added by MDCT-based encoders like AAC/MP3/ATRAC3/XMA/etc to "set up" the decoder).
|
|
|
|
* - should be used at the beginning of the stream
|
|
|
|
* - should check if there are data->skipSamples before using this, to avoid overwritting FFmpeg's value (ex. AAC).
|
|
|
|
*
|
|
|
|
* This could be added per format in FFmpeg directly, but it's here for flexibility and due to bugs
|
|
|
|
* (FFmpeg's stream->(start_)skip_samples causes glitches in XMA).
|
|
|
|
*/
|
|
|
|
void ffmpeg_set_skip_samples(ffmpeg_codec_data * data, int skip_samples) {
|
|
|
|
AVStream *stream = NULL;
|
|
|
|
if (!data->formatCtx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* overwrite FFmpeg's skip samples */
|
|
|
|
stream = data->formatCtx->streams[data->streamIndex];
|
|
|
|
stream->start_skip_samples = 0; /* used for the first packet *if* pts=0 */
|
|
|
|
stream->skip_samples = 0; /* skip_samples can be used for any packet */
|
|
|
|
|
|
|
|
/* set skip samples with our internal discard */
|
|
|
|
data->skipSamplesSet = 1;
|
|
|
|
data->samplesToDiscard = skip_samples;
|
|
|
|
|
|
|
|
/* expose (info only) */
|
|
|
|
data->skipSamples = skip_samples;
|
|
|
|
}
|
2017-01-13 23:47:52 +01:00
|
|
|
|
2016-07-17 08:02:27 +02:00
|
|
|
#endif
|