I use MediaCodec and MediaExtractor classes from android NDK for decoding mp3 files. For comparison input file(mp3) and outputfile(wav - it`s my decoded format) i look at tracks duration and i exactly see difference of duration 50 milliseconds. I check it on anothers tracks and this difference always present. So, i lost first near 50 milliseconds of my input track.
Algorithm of decoding used like in androin ndk-samples(native-codec) decoding fragment is below
ssize_t bufidx = -1;
if (!d->sawInputEOS)
{
bufidx = AMediaCodec_dequeueInputBuffer(codec, 2000);
LOGV("input buffer %zd", bufidx);
if (bufidx >= 0)
{
size_t bufsize;
uint8_t *buf = AMediaCodec_getInputBuffer(codec, bufidx, &bufsize);
ssize_t sampleSize = AMediaExtractor_readSampleData(ex, buf, bufsize);
if (sampleSize < 0)
{
sampleSize = 0;
d->sawInputEOS = true;
LOGV("EOS");
}
int64_t presentationTimeUs = AMediaExtractor_getSampleTime(extractor);
AMediaCodec_queueInputBuffer(codec, bufidx, 0, sampleSize, presentationTimeUs,
d->sawInputEOS ? AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM : 0);
AMediaExtractor_advance(ex);
}
}
if (!d->sawOutputEOS)
{
AMediaCodecBufferInfo info;
ssize_t status = AMediaCodec_dequeueOutputBuffer(codec, &info, 0);
if (status >= 0)
{
if (info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)
{
LOGV("output EOS");
d->sawOutputEOS = true;
}
AMediaCodec_releaseOutputBuffer(codec, status, info.size != 0);
}
else if (status == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
LOGV("output buffers changed");
} else if (status == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
AMediaFormat *format = NULL;
format = AMediaCodec_getOutputFormat(d->codec);
LOGV("format changed to: %s", AMediaFormat_toString(format));
AMediaFormat_delete(format);
} else if (status == AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
LOGV("no output buffer right now");
} else {
LOGV("unexpected info code: %zd", status);
}
}
This code provided with "native codec" sample in NDK.
How can i fix it? because of loss data are unacceptable