--- apps/codecs/aac.c (revision 30012) +++ apps/codecs/aac.c (working copy) @@ -58,6 +58,7 @@ stream_t input_stream; uint32_t sound_samples_done; uint32_t elapsed_time; + uint32_t sample_duration; int file_offset; int framelength; int lead_trim = 0; @@ -231,9 +232,23 @@ /* Gather number of samples for the decoded frame. */ framelength = (frame_info.samples >> 1) - lead_trim; - if (i == demux_res.num_sample_byte_sizes - 1) + if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0) { + /* Currently limited to at most one frame of tail_trim. + * Seems to be enough. + */ + if (ci->id3->tail_trim == 0 + && sample_duration < (frame_info.samples >> 1)) + { + /* Subtract lead_trim just in case we decode a file with + * only one audio frame with actual data. + */ + framelength = sample_duration - lead_trim; + } + else + { framelength -= ci->id3->tail_trim; + } } if (framelength > 0)