aboutsummaryrefslogtreecommitdiffstats
path: root/src/decoder
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2014-08-26 21:52:28 +0200
committerMax Kellermann <max@duempel.org>2014-08-26 22:27:04 +0200
commit0c2d767f6fca030a8da3202c05b3eb80ba176ef1 (patch)
tree481acab9b4a1fadf8e208142bde510167b406d7e /src/decoder
parent02e697032f7f375e67acc349ada6d849edf51aa3 (diff)
downloadmpd-0c2d767f6fca030a8da3202c05b3eb80ba176ef1.tar.gz
mpd-0c2d767f6fca030a8da3202c05b3eb80ba176ef1.tar.xz
mpd-0c2d767f6fca030a8da3202c05b3eb80ba176ef1.zip
DecoderAPI: use std::chrono::duration for decoder_seek*()
For type safety and code readability.
Diffstat (limited to 'src/decoder')
-rw-r--r--src/decoder/DecoderAPI.cxx24
-rw-r--r--src/decoder/DecoderAPI.hxx15
-rw-r--r--src/decoder/plugins/FfmpegDecoderPlugin.cxx14
-rw-r--r--src/decoder/plugins/GmeDecoderPlugin.cxx2
-rw-r--r--src/decoder/plugins/MadDecoderPlugin.cxx26
-rw-r--r--src/decoder/plugins/ModplugDecoderPlugin.cxx2
-rw-r--r--src/decoder/plugins/Mp4v2DecoderPlugin.cxx5
-rw-r--r--src/decoder/plugins/SidplayDecoderPlugin.cxx4
8 files changed, 40 insertions, 52 deletions
diff --git a/src/decoder/DecoderAPI.cxx b/src/decoder/DecoderAPI.cxx
index 7349b00db..c9bdb46e8 100644
--- a/src/decoder/DecoderAPI.cxx
+++ b/src/decoder/DecoderAPI.cxx
@@ -204,37 +204,21 @@ decoder_command_finished(Decoder &decoder)
dc.Unlock();
}
-double decoder_seek_where(gcc_unused Decoder & decoder)
+SongTime
+decoder_seek_time(Decoder &decoder)
{
const DecoderControl &dc = decoder.dc;
assert(dc.pipe != nullptr);
if (decoder.initial_seek_running)
- return dc.start_ms / 1000.;
+ return SongTime(dc.start_ms);
assert(dc.command == DecoderCommand::SEEK);
decoder.seeking = true;
- return dc.seek_where;
-}
-
-unsigned
-decoder_seek_where_ms(Decoder &decoder)
-{
- const DecoderControl &dc = decoder.dc;
-
- assert(dc.pipe != nullptr);
-
- if (decoder.initial_seek_running)
- return dc.start_ms;
-
- assert(dc.command == DecoderCommand::SEEK);
-
- decoder.seeking = true;
-
- return unsigned(dc.seek_where * 1000);
+ return SongTime::FromS(dc.seek_where);
}
uint64_t
diff --git a/src/decoder/DecoderAPI.hxx b/src/decoder/DecoderAPI.hxx
index 2464ee6fa..ca33e26c7 100644
--- a/src/decoder/DecoderAPI.hxx
+++ b/src/decoder/DecoderAPI.hxx
@@ -37,6 +37,7 @@
#include "AudioFormat.hxx"
#include "MixRampInfo.hxx"
#include "config/ConfigData.hxx"
+#include "Chrono.hxx"
// IWYU pragma: end_exports
@@ -84,21 +85,11 @@ decoder_command_finished(Decoder &decoder);
* Call this when you have received the DecoderCommand::SEEK command.
*
* @param decoder the decoder object
- * @return the destination position for the week
- */
-gcc_pure
-double
-decoder_seek_where(Decoder &decoder);
-
-/**
- * Call this when you have received the DecoderCommand::SEEK command.
- *
- * @param decoder the decoder object
* @return the destination position for the seek in milliseconds
*/
gcc_pure
-unsigned
-decoder_seek_where_ms(Decoder &decoder);
+SongTime
+decoder_seek_time(Decoder &decoder);
/**
* Call this when you have received the DecoderCommand::SEEK command.
diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx
index 2787d3705..e328da24b 100644
--- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx
+++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx
@@ -210,11 +210,19 @@ time_from_ffmpeg(int64_t t, const AVRational time_base)
/ (double)1024;
}
+template<typename Ratio>
+static constexpr AVRational
+RatioToAVRational()
+{
+ return { Ratio::num, Ratio::den };
+}
+
gcc_const
static int64_t
-time_to_ffmpeg(double t_ms, const AVRational time_base)
+time_to_ffmpeg(SongTime t, const AVRational time_base)
{
- return av_rescale_q(t_ms, (AVRational){1, 1000},
+ return av_rescale_q(t.count(),
+ RatioToAVRational<SongTime::period>(),
time_base);
}
@@ -547,7 +555,7 @@ ffmpeg_decode(Decoder &decoder, InputStream &input)
if (cmd == DecoderCommand::SEEK) {
int64_t where =
- time_to_ffmpeg(decoder_seek_where_ms(decoder),
+ time_to_ffmpeg(decoder_seek_time(decoder),
av_stream->time_base) +
start_time_fallback(*av_stream);
diff --git a/src/decoder/plugins/GmeDecoderPlugin.cxx b/src/decoder/plugins/GmeDecoderPlugin.cxx
index e10efcbed..5588e2a1e 100644
--- a/src/decoder/plugins/GmeDecoderPlugin.cxx
+++ b/src/decoder/plugins/GmeDecoderPlugin.cxx
@@ -194,7 +194,7 @@ gme_file_decode(Decoder &decoder, Path path_fs)
cmd = decoder_data(decoder, nullptr, buf, sizeof(buf), 0);
if (cmd == DecoderCommand::SEEK) {
- unsigned where = decoder_seek_where_ms(decoder);
+ unsigned where = decoder_seek_time(decoder).ToMS();
gme_err = gme_seek(emu, where);
if (gme_err != nullptr)
LogWarning(gme_domain, gme_err);
diff --git a/src/decoder/plugins/MadDecoderPlugin.cxx b/src/decoder/plugins/MadDecoderPlugin.cxx
index 5fa409435..7509faabb 100644
--- a/src/decoder/plugins/MadDecoderPlugin.cxx
+++ b/src/decoder/plugins/MadDecoderPlugin.cxx
@@ -67,6 +67,13 @@ static constexpr Domain mad_domain("mad");
static bool gapless_playback;
+gcc_const
+static SongTime
+ToSongTime(mad_timer_t t)
+{
+ return SongTime::FromMS(mad_timer_count(t, MAD_UNITS_MILLISECONDS));
+}
+
static inline int32_t
mad_fixed_to_24_sample(mad_fixed_t sample)
{
@@ -116,8 +123,8 @@ struct MadDecoder {
unsigned char input_buffer[READ_BUFFER_SIZE];
int32_t output_buffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
float total_time;
- unsigned elapsed_time;
- unsigned seek_where;
+ SongTime elapsed_time;
+ SongTime seek_time;
enum muteframe mute_frame;
long *frame_offsets;
mad_timer_t *times;
@@ -159,7 +166,7 @@ struct MadDecoder {
bool DecodeFirstFrame(Tag **tag);
gcc_pure
- long TimeToFrame(unsigned t) const;
+ long TimeToFrame(SongTime t) const;
void UpdateTimerNextFrame();
@@ -847,13 +854,12 @@ mad_decoder_total_file_time(InputStream &is)
}
long
-MadDecoder::TimeToFrame(unsigned t) const
+MadDecoder::TimeToFrame(SongTime t) const
{
unsigned long i;
for (i = 0; i < highest_frame; ++i) {
- unsigned frame_time =
- mad_timer_count(times[i], MAD_UNITS_MILLISECONDS);
+ auto frame_time = ToSongTime(times[i]);
if (frame_time >= t)
break;
}
@@ -884,7 +890,7 @@ MadDecoder::UpdateTimerNextFrame()
timer = times[current_frame];
current_frame++;
- elapsed_time = mad_timer_count(timer, MAD_UNITS_MILLISECONDS);
+ elapsed_time = ToSongTime(timer);
}
DecoderCommand
@@ -980,7 +986,7 @@ MadDecoder::Read()
mute_frame = MUTEFRAME_NONE;
break;
case MUTEFRAME_SEEK:
- if (elapsed_time >= seek_where)
+ if (elapsed_time >= seek_time)
mute_frame = MUTEFRAME_NONE;
break;
case MUTEFRAME_NONE:
@@ -989,7 +995,7 @@ MadDecoder::Read()
assert(input_stream.IsSeekable());
unsigned long j =
- TimeToFrame(decoder_seek_where_ms(*decoder));
+ TimeToFrame(decoder_seek_time(*decoder));
if (j < highest_frame) {
if (Seek(frame_offsets[j])) {
current_frame = j;
@@ -997,7 +1003,7 @@ MadDecoder::Read()
} else
decoder_seek_error(*decoder);
} else {
- seek_where = decoder_seek_where_ms(*decoder);
+ seek_time = decoder_seek_time(*decoder);
mute_frame = MUTEFRAME_SEEK;
decoder_command_finished(*decoder);
}
diff --git a/src/decoder/plugins/ModplugDecoderPlugin.cxx b/src/decoder/plugins/ModplugDecoderPlugin.cxx
index 8c56f34c1..c88249fa5 100644
--- a/src/decoder/plugins/ModplugDecoderPlugin.cxx
+++ b/src/decoder/plugins/ModplugDecoderPlugin.cxx
@@ -166,7 +166,7 @@ mod_decode(Decoder &decoder, InputStream &is)
0);
if (cmd == DecoderCommand::SEEK) {
- ModPlug_Seek(f, decoder_seek_where_ms(decoder));
+ ModPlug_Seek(f, decoder_seek_time(decoder).ToMS());
decoder_command_finished(decoder);
}
diff --git a/src/decoder/plugins/Mp4v2DecoderPlugin.cxx b/src/decoder/plugins/Mp4v2DecoderPlugin.cxx
index e968be20b..ef5284437 100644
--- a/src/decoder/plugins/Mp4v2DecoderPlugin.cxx
+++ b/src/decoder/plugins/Mp4v2DecoderPlugin.cxx
@@ -165,9 +165,8 @@ mp4_file_decode(Decoder &mpd_decoder, Path path_fs)
unsigned int data_length = 0;
if (cmd == DecoderCommand::SEEK) {
- const unsigned offset_ms =
- decoder_seek_where_ms(mpd_decoder);
- const MP4Timestamp offset = (offset_ms * scale) / 1000;
+ const MP4Timestamp offset =
+ decoder_seek_time(mpd_decoder).ToScale(scale);
sample = MP4GetSampleIdFromTime(handle, track, offset,
false);
diff --git a/src/decoder/plugins/SidplayDecoderPlugin.cxx b/src/decoder/plugins/SidplayDecoderPlugin.cxx
index e3e3b8d96..b0a398f56 100644
--- a/src/decoder/plugins/SidplayDecoderPlugin.cxx
+++ b/src/decoder/plugins/SidplayDecoderPlugin.cxx
@@ -314,8 +314,8 @@ sidplay_file_decode(Decoder &decoder, Path path_fs)
if (cmd == DecoderCommand::SEEK) {
unsigned data_time = player.time();
- unsigned target_time = (unsigned)
- (decoder_seek_where(decoder) * timebase);
+ unsigned target_time =
+ decoder_seek_time(decoder).ToScale(timebase);
/* can't rewind so return to zero and seek forward */
if(target_time<data_time) {