Imported code from ffmpeg decode example.