I have an HEVC sequence with 3500 frames and I am writing a decoder for reading it (read frame by frame and dump to yuv). In my main(), I have a for loop that calls a decoder() 3500 times (I am assuming at this stage that the main() knows how many frames there are).
So, for every call to decoder(), I need a complete frame to be returned. This is what the decoder() looks like..
bool decode(pFormatCtx, pCodecCtx)
{
int gotaFrame=0;
while (gotaFrame==0) {
printf("1\t");
if ( !av_read_frame(pFormatCtx, &packet) ) {
if(packet.stream_index==videoStreamIndex) {
// try decoding
avcodec_decode_video2(pCodecCtx, pFrame, &gotaFrame, &packet);
if (gotaFrame) { // decode success.
printf("2\t");
// dump to yuv ... not shown here.
// cleanup
av_frame_unref(pFrame);
av_frame_free(&pFrame);
av_free_packet(&packet);
return true;
}
}
}
}
}
The behavior is like this: 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 ...... it looks like it reads several frames before decoding one? The first frame is an I-frame, so shouldn't that be decoded right away?
With this code, I end up losing the several frames (indicated by the series of 1s). Can someone help me out here? Is there something I am doing wrong in my code?
Update: the test clip is video-only. No audio.