Index: apps/codecs/aac.c =================================================================== --- apps/codecs/aac.c (revision 30007) +++ apps/codecs/aac.c (working copy) @@ -72,6 +72,7 @@ unsigned char c = 0; void *ret; intptr_t param; + bool empty_first_frame = false; /* Clean and initialize decoder structures */ memset(&demux_res , 0, sizeof(demux_res)); @@ -145,6 +146,7 @@ if (i == 0) { lead_trim = ci->id3->lead_trim; + //printf("AAC: initial lead_trim: %d\n", lead_trim); } /* The main decoding loop */ @@ -213,6 +215,20 @@ /* Output the audio */ ci->yield(); + if (empty_first_frame && lead_trim > 0) + { + /* Remove the first frame from lead_trim, under the assumption that + * it really had the same size as this frame + */ + empty_first_frame = false; + lead_trim -= (frame_info.samples >> 1); + + if (lead_trim < 0) + { + lead_trim = 0; + } + } + /* Gather number of samples for the decoded frame. */ framelength = (frame_info.samples >> 1) - lead_trim; @@ -230,14 +246,27 @@ if (lead_trim > 0) { - /* frame_info.samples can be 0 for the first frame */ - lead_trim -= (i > 0 || frame_info.samples) - ? (frame_info.samples >> 1) : (uint32_t)framelength; + if (0 == i && 0 == frame_info.samples) + { + empty_first_frame = true; + } - if (lead_trim < 0 || ci->id3->lead_trim == 0) + /* frame_info.samples can be 0 for the first frame. If so, + * framelength calculated above is negative and should not be + * added to lead_trim + */ + if (0 != i || 0 != frame_info.samples) { + lead_trim -= (i > 0) + ? (frame_info.samples >> 1) : (uint32_t)framelength; + } + + if (lead_trim < 0) + { lead_trim = 0; } + //printf("AAC: lead_trim at frame %d: %d (framelength %d, samples %d)\n", + // i, lead_trim, framelength, frame_info.samples); } /* Update the elapsed-time indicator */