copy avcodec_free_framei() method from libav