diff --git a/aac-dec.c b/aac-dec.c index 3da70a3..ff06e32 100644 --- a/aac-dec.c +++ b/aac-dec.c @@ -55,53 +55,50 @@ int main(int argc, char *argv[]) { int n, i; UINT valid, packet_size; AAC_DECODER_ERROR err; - n = fread(packet, 1, 7, in); - if (n != 7) + n = fread(packet, 1, 150, in); + if (n <= 0) break; - if (packet[0] != 0xff || (packet[1] & 0xf0) != 0xf0) { - fprintf(stderr, "Not an ADTS packet\n"); - break; - } - packet_size = ((packet[3] & 0x03) << 11) | (packet[4] << 3) | (packet[5] >> 5); - n = fread(packet + 7, 1, packet_size - 7, in); - if (n != packet_size - 7) { - fprintf(stderr, "Partial packet\n"); - break; - } + packet_size = n; valid = packet_size; err = aacDecoder_Fill(handle, &ptr, &packet_size, &valid); if (err != AAC_DEC_OK) { fprintf(stderr, "Fill failed: %x\n", err); break; } - err = aacDecoder_DecodeFrame(handle, decode_buf, output_size / sizeof(INT_PCM), 0); - if (err == AAC_DEC_NOT_ENOUGH_BITS) - continue; - if (err != AAC_DEC_OK) { - fprintf(stderr, "Decode failed: %x\n", err); - continue; + if (valid != 0) { + fprintf(stderr, "Unable to feed all %d input bytes, %d bytes left\n", n, valid); } - if (!wav) { - CStreamInfo *info = aacDecoder_GetStreamInfo(handle); - if (!info || info->sampleRate <= 0) { - fprintf(stderr, "No stream info\n"); + while (1) { + err = aacDecoder_DecodeFrame(handle, decode_buf, output_size / sizeof(INT_PCM), 0); + if (err == AAC_DEC_NOT_ENOUGH_BITS) + break; + if (err != AAC_DEC_OK) { + fprintf(stderr, "Decode failed: %x\n", err); break; } - frame_size = info->frameSize * info->numChannels; - // Note, this probably doesn't return channels > 2 in the right order for wav - wav = wav_write_open(outfile, info->sampleRate, 16, info->numChannels); if (!wav) { - perror(outfile); - break; + CStreamInfo *info = aacDecoder_GetStreamInfo(handle); + if (!info || info->sampleRate <= 0) { + fprintf(stderr, "No stream info\n"); + goto end; + } + frame_size = info->frameSize * info->numChannels; + // Note, this probably doesn't return channels > 2 in the right order for wav + wav = wav_write_open(outfile, info->sampleRate, 16, info->numChannels); + if (!wav) { + perror(outfile); + goto end; + } } + for (i = 0; i < frame_size; i++) { + uint8_t* out = &output_buf[2*i]; + out[0] = decode_buf[i] & 0xff; + out[1] = decode_buf[i] >> 8; + } + wav_write_data(wav, output_buf, 2*frame_size); } - for (i = 0; i < frame_size; i++) { - uint8_t* out = &output_buf[2*i]; - out[0] = decode_buf[i] & 0xff; - out[1] = decode_buf[i] >> 8; - } - wav_write_data(wav, output_buf, 2*frame_size); } +end: free(output_buf); free(decode_buf); fclose(in);