From a0b57f378224a1711ad2dcbede9b1adadf577dce Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 28 Oct 2008 20:44:08 +0100 Subject: mp3: moved code to mp3_filesize_to_song_length() The function mp3_decode_first_frame() is too large. Move some code to separate smaller functions. --- src/decoder/mp3_plugin.c | 65 +++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 23 deletions(-) (limited to 'src/decoder') diff --git a/src/decoder/mp3_plugin.c b/src/decoder/mp3_plugin.c index cb7a64352..bcf6f9566 100644 --- a/src/decoder/mp3_plugin.c +++ b/src/decoder/mp3_plugin.c @@ -648,6 +648,46 @@ parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen) return true; } +static inline float +mp3_frame_duration(const struct mad_frame *frame) +{ + return mad_timer_count(frame->header.duration, + MAD_UNITS_MILLISECONDS) / 1000.0; +} + +static off_t +mp3_rest_including_this_frame(const struct mp3_data *data) +{ + off_t offset = data->input_stream->offset; + + if (data->stream.this_frame != NULL) + offset -= data->stream.bufend - data->stream.this_frame; + else + offset -= data->stream.bufend - data->stream.buffer; + + return data->input_stream->size - offset; +} + +/** + * Attempt to calulcate the length of the song from filesize + */ +static void +mp3_filesize_to_song_length(struct mp3_data *data) +{ + off_t rest = mp3_rest_including_this_frame(data); + + if (rest > 0) { + float frame_duration = mp3_frame_duration(&data->frame); + + data->total_time = (rest * 8.0) / (data->frame).header.bitrate; + data->max_frames = data->total_time / frame_duration + + FRAMES_CUSHION; + } else { + data->max_frames = FRAMES_CUSHION; + data->total_time = 0; + } +} + static bool mp3_decode_first_frame(struct mp3_data *data, struct tag **tag, ReplayGainInfo **replay_gain_info_r) @@ -682,29 +722,8 @@ mp3_decode_first_frame(struct mp3_data *data, struct tag **tag, ptr = data->stream.anc_ptr; bitlen = data->stream.anc_bitlen; - /* - * Attempt to calulcate the length of the song from filesize - */ - { - off_t offset = data->input_stream->offset; - mad_timer_t duration = data->frame.header.duration; - float frame_duration = ((float)mad_timer_count(duration, MAD_UNITS_MILLISECONDS)) / 1000; - - if (data->stream.this_frame != NULL) - offset -= data->stream.bufend - data->stream.this_frame; - else - offset -= data->stream.bufend - data->stream.buffer; - - if (data->input_stream->size >= offset) { - data->total_time = ((data->input_stream->size - offset) * - 8.0) / (data->frame).header.bitrate; - data->max_frames = data->total_time / frame_duration + - FRAMES_CUSHION; - } else { - data->max_frames = FRAMES_CUSHION; - data->total_time = 0; - } - } + mp3_filesize_to_song_length(data); + /* * if an xing tag exists, use that! */ -- cgit v1.2.3