Skip to content

Commit d028149

Browse files
committed
Avoid redundant WAVE64 conditionals
1 parent 247ac5c commit d028149

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/flac/decode.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ static int DecoderSession_finish_error(DecoderSession *d);
120120
static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, uint32_t sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
121121
static FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples);
122122
static FLAC__uint64 calculate_total_riff_wave_fmt_chunk_size(FileFormat format, FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize);
123-
static FLAC__uint64 calculate_riff_wave_fmt_chunk_body_size(FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize);
123+
static FLAC__uint64 calculate_riff_wave_fmt_chunk_size_field(FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize);
124+
static FLAC__uint64 calculate_wave64_fmt_chunk_size_field(FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize);
124125
static FLAC__bool write_riff_wave_fmt_chunk_body(FILE *f, FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize, uint32_t bps, uint32_t channels, uint32_t sample_rate, FLAC__uint32 channel_mask);
125126
static FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, uint32_t bps, uint32_t channels, uint32_t sample_rate, FileFormat format, FileSubFormat subformat, FLAC__uint32 comm_length);
126127
static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
@@ -874,15 +875,15 @@ FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uin
874875
if(format != FORMAT_WAVE64) {
875876
if(flac__utils_fwrite("fmt ", 1, 4, f) != 4)
876877
return false;
877-
if(!write_little_endian_uint32(f, calculate_riff_wave_fmt_chunk_body_size(is_waveformatextensible, preserve_dummy_cbsize))) /* chunk size */
878+
if(!write_little_endian_uint32(f, calculate_riff_wave_fmt_chunk_size_field(is_waveformatextensible, preserve_dummy_cbsize))) /* chunk size */
878879
return false;
879880
}
880881
else { /* Wave64 */
881882
/* fmt GUID 20746D66-ACF3-11D3-8CD1-00C04F8EDB8A */
882883
if(flac__utils_fwrite("\x66\x6D\x74\x20\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
883884
return false;
884-
/* chunk size (+16+8 for GUID and size fields, but without padding) */
885-
if(!write_little_endian_uint64(f, 16 + 8 + calculate_riff_wave_fmt_chunk_body_size(is_waveformatextensible, preserve_dummy_cbsize)))
885+
/* chunk size */
886+
if(!write_little_endian_uint64(f, calculate_wave64_fmt_chunk_size_field(is_waveformatextensible, preserve_dummy_cbsize)))
886887
return false;
887888
}
888889

@@ -987,23 +988,47 @@ FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uin
987988
FLAC__uint64 calculate_total_riff_wave_fmt_chunk_size(FileFormat format, FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize)
988989
{
989990
if(format == FORMAT_WAVE64) {
990-
FLAC__uint16 extra_padding = (!is_waveformatextensible && preserve_dummy_cbsize) ? 6 : 0;
991-
/* +16+8 for GUID and size for WAVE64 */
992-
return (16 + 8 + calculate_riff_wave_fmt_chunk_body_size(is_waveformatextensible, preserve_dummy_cbsize) + extra_padding);
991+
/* round up to nearest 8 bytes */
992+
return ((calculate_wave64_fmt_chunk_size_field(is_waveformatextensible, preserve_dummy_cbsize) + 7) & (~7u));
993993
}
994994
else {
995995
/* +8 for fmt chunk header */
996-
return (8 + calculate_riff_wave_fmt_chunk_body_size(is_waveformatextensible, preserve_dummy_cbsize));
996+
return (8 + calculate_riff_wave_fmt_chunk_size_field(is_waveformatextensible, preserve_dummy_cbsize));
997997
}
998998
}
999999

1000-
FLAC__uint64 calculate_riff_wave_fmt_chunk_body_size(FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize)
1000+
FLAC__uint64 calculate_wave64_fmt_chunk_size_field(FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize)
10011001
{
10021002
if(is_waveformatextensible)
1003-
return 40;
1003+
/* +16+8 for GUID and size fields */
1004+
/* +16 for regular fmt body */
1005+
/* +2 for cbSize */
1006+
/* +22 for remaining WAVEFORMATEXTENSIBLE structure */
1007+
return (16 + 8 + 16 + 2 + 22);
10041008
else if(preserve_dummy_cbsize)
1005-
return 18;
1009+
/* +16+8 for GUID and size fields */
1010+
/* +16 for regular fmt body */
1011+
/* +2 for cbSize */
1012+
return (16 + 8 + 16 + 2);
10061013
else
1014+
/* +16+8 for GUID and size fields */
1015+
/* +16 for regular fmt body */
1016+
return (16 + 8 + 16);
1017+
}
1018+
1019+
FLAC__uint64 calculate_riff_wave_fmt_chunk_size_field(FLAC__bool is_waveformatextensible, FLAC__bool preserve_dummy_cbsize)
1020+
{
1021+
if(is_waveformatextensible)
1022+
/* +16 for regular fmt body */
1023+
/* +2 for cbSize */
1024+
/* +22 for remaining WAVEFORMATEXTENSIBLE structure */
1025+
return (16 + 2 + 22);
1026+
else if(preserve_dummy_cbsize)
1027+
/* +16 for regular fmt body */
1028+
/* +2 for cbSize */
1029+
return (16 + 2);
1030+
else
1031+
/* +16 for regular fmt body */
10071032
return 16;
10081033
}
10091034

0 commit comments

Comments
 (0)