From 0c2d767f6fca030a8da3202c05b3eb80ba176ef1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 26 Aug 2014 21:52:28 +0200 Subject: DecoderAPI: use std::chrono::duration for decoder_seek*() For type safety and code readability. --- src/decoder/plugins/FfmpegDecoderPlugin.cxx | 14 +++++++++++--- src/decoder/plugins/GmeDecoderPlugin.cxx | 2 +- src/decoder/plugins/MadDecoderPlugin.cxx | 26 ++++++++++++++++---------- src/decoder/plugins/ModplugDecoderPlugin.cxx | 2 +- src/decoder/plugins/Mp4v2DecoderPlugin.cxx | 5 ++--- src/decoder/plugins/SidplayDecoderPlugin.cxx | 4 ++-- 6 files changed, 33 insertions(+), 20 deletions(-) (limited to 'src/decoder/plugins') 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 +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(), 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