Skip to content

Commit 7f491ae

Browse files
authored
Only call tell callback when necessary (#779)
This slightly speeds up the encoder by reducing the number of syscalls, but can result in a 30% speedup in extreme cases, mostly when using extremely small blocksizes.
1 parent 30cdb4d commit 7f491ae

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/libFLAC/stream_encoder.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -3032,17 +3032,18 @@ FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const
30323032
(void)is_last_block;
30333033
#endif
30343034

3035-
/* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
3036-
if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
3037-
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
3038-
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
3039-
}
3040-
30413035
/*
30423036
* Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
30433037
*/
30443038
if(samples == 0) {
30453039
FLAC__MetadataType type = (buffer[0] & 0x7f);
3040+
3041+
/* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
3042+
if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
3043+
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
3044+
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
3045+
}
3046+
30463047
if(type == FLAC__METADATA_TYPE_STREAMINFO)
30473048
encoder->protected_->streaminfo_offset = output_position;
30483049
else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
@@ -3066,6 +3067,12 @@ FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const
30663067
break;
30673068
}
30683069
else if(test_sample >= frame_first_sample) {
3070+
/* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
3071+
if(output_position == 0 && encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
3072+
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
3073+
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
3074+
}
3075+
30693076
encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
30703077
encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
30713078
encoder->private_->seek_table->points[i].frame_samples = blocksize;

0 commit comments

Comments
 (0)